Total Complexity | 132 |
Total Lines | 1131 |
Duplicated Lines | 11.23 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like mutis.astro often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # Licensed under a 3-clause BSD style license - see LICENSE |
||
|
|||
2 | """Utils specific to the field of astrophysics""" |
||
3 | |||
4 | import logging |
||
5 | |||
6 | import numpy as np |
||
7 | import pandas as pd |
||
8 | |||
9 | import matplotlib as mplt |
||
10 | import matplotlib.pyplot as plt |
||
11 | |||
12 | import glob |
||
13 | import re |
||
14 | from datetime import datetime |
||
15 | |||
16 | from astropy.time import Time |
||
17 | |||
18 | |||
19 | #__all__ = ["Astro"] |
||
20 | |||
21 | log = logging.getLogger(__name__) |
||
22 | |||
23 | # TODO: to be able to save previous labels in _mod.mod and be kepto after processing with diffmap |
||
24 | |||
25 | ############################################################################# |
||
26 | ### Output compatible with IPython widgets and with plain python console: ### |
||
27 | # |
||
28 | # With this we can follow follow a commen scheme: |
||
29 | # |
||
30 | # def someGUIfunction(): |
||
31 | # out = get_output() |
||
32 | # ... gui things... |
||
33 | # out.clear_output() |
||
34 | # out.append_stdout('some msg') |
||
35 | # out.display_output() |
||
36 | # |
||
37 | class plain_printer: |
||
38 | @staticmethod |
||
39 | def append_stdout(msg): |
||
40 | print(msg, end='') |
||
41 | @staticmethod |
||
42 | def clear_output(): |
||
43 | print('\n') |
||
44 | @staticmethod |
||
45 | def display_output(): |
||
46 | pass |
||
47 | |||
48 | def get_output(): |
||
49 | try: |
||
50 | get_ipython().__class__.__name__ |
||
51 | |||
52 | import ipywidgets |
||
53 | class pretty_printer(ipywidgets.Output): |
||
54 | def __init__(self, *args, **kwargs): |
||
55 | super().__init__(*args, **kwargs) |
||
56 | def display_output(self): |
||
57 | display(self) |
||
58 | |||
59 | out = pretty_printer(layout={'border': '1px solid black'}) |
||
60 | |||
61 | return out |
||
62 | except: |
||
63 | return plain_printer |
||
64 | ############################################################################# |
||
65 | |||
66 | |||
67 | def pol_angle_reshape(s): |
||
68 | """ |
||
69 | Reshape a signal as a polarization angle: shifting by 180 degrees in a way it varies as smoothly as possible. |
||
70 | """ |
||
71 | |||
72 | s = np.array(s) |
||
73 | s = np.mod(s,180) # s % 180 |
||
74 | |||
75 | sn = np.empty(s.shape) |
||
76 | for i in range(1,len(s)): |
||
77 | #if t[i]-t[i-1] < 35: |
||
78 | d = 181 |
||
79 | n = 0 |
||
80 | for m in range(0,100): |
||
81 | m2 = (-1)**(m+1)*np.floor((m+1)/2) |
||
82 | if np.abs(s[i]+180*m2-sn[i-1]) < d: |
||
83 | d = np.abs(s[i]+180*m2-sn[i-1]) |
||
84 | n = m2 |
||
85 | sn[i] = s[i]+180*n |
||
86 | return sn |
||
87 | |||
88 | |||
89 | |||
90 | |||
91 | |||
92 | |||
93 | ########################################################################### |
||
94 | ################################## Knots ################################## |
||
95 | ########################################################################### |
||
96 | |||
97 | |||
98 | |||
99 | def KnotsIdAuto(mod): |
||
100 | """ |
||
101 | Identify the knots appearing in several epochs giving them names, based on their position. |
||
102 | |||
103 | Parameters: |
||
104 | ----------- |
||
105 | mod : :pd.DataFrame: |
||
106 | pandas.DataFrame containing every knot, with |
||
107 | at least columns 'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
||
108 | |||
109 | Returns: mod |
||
110 | -------- |
||
111 | mod : :pd.DataFrame: |
||
112 | pandas.DataFrame containing every knot with their altered labels, with |
||
113 | at least columns 'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
||
114 | |||
115 | """ |
||
116 | |||
117 | mod_date_dict = dict(list((mod.groupby('date')))) |
||
118 | |||
119 | mod_dates = list(Time(list(mod_date_dict.keys())).datetime) |
||
120 | mod_dates_str = list(Time(list(mod_date_dict.keys())).strftime('%Y-%m-%d')) |
||
121 | mod_data = list(mod_date_dict.values()) |
||
122 | |||
123 | Bx = [[]]*len(mod_dates) |
||
124 | By = [[]]*len(mod_dates) |
||
125 | B = [[]]*len(mod_dates) |
||
126 | |||
127 | |||
128 | thresh = 0.03 #0.062 |
||
129 | |||
130 | news = 0 |
||
131 | |||
132 | for i, (date, data) in enumerate(zip(mod_dates, mod_data)): |
||
133 | log.debug(f'Analysing epoch i {i:3d} ({mod_dates_str[i]})') |
||
134 | |||
135 | if not len(data.index) > 0: |
||
136 | log.error(' -> Error, no components found') |
||
137 | break |
||
138 | |||
139 | log.debug(f' it has {len(data.index)} components') |
||
140 | |||
141 | Bx[i] = np.ravel(data['X']) |
||
142 | By[i] = np.ravel(data['Y']) |
||
143 | B[i] = np.full(len(data.index), fill_value=None) |
||
144 | |||
145 | # if first epoch, just give them new names...: |
||
146 | if i == 0: |
||
147 | log.debug(' first epoch, giving names...') |
||
148 | |||
149 | for n in range(0,len(mod_data[i].index)): |
||
150 | if data['Flux (Jy)'].iloc[n] < 0.001: |
||
151 | log.debug(' skipping, too weak') |
||
152 | break |
||
153 | if n == 0: |
||
154 | B[i][n] = 'A0' |
||
155 | else: |
||
156 | news = news + 1 |
||
157 | B[i][n] = f'B{news}' |
||
158 | log.debug(f' -> FOUND: {B[i]}\n') |
||
159 | continue |
||
160 | |||
161 | # if not first epoch...: |
||
162 | |||
163 | for n in range(0,len(mod_data[i].index)): |
||
164 | if data['Flux (Jy)'].iloc[n] < 0.001: |
||
165 | log.debug(' skipping, too weak') |
||
166 | break |
||
167 | if n == 0: |
||
168 | B[i][n] = 'A0' |
||
169 | else: |
||
170 | log.debug(f' -> id component {n}...') |
||
171 | |||
172 | close = None |
||
173 | |||
174 | a = 0 |
||
175 | while ( i - a >= 0 and a < 4 and close is None): |
||
176 | a = a + 1 |
||
177 | |||
178 | if not len(Bx[i-a])>0: |
||
179 | break |
||
180 | |||
181 | dist = ( (Bx[i-a]-Bx[i][n]) ** 2 + (By[i-a]-By[i][n]) ** 2 ) ** 0.5 |
||
182 | |||
183 | for m in range(len(dist)): |
||
184 | if B[i-a][m] in B[i]: |
||
185 | dist[m] = np.inf |
||
186 | |||
187 | if np.amin(dist) < thresh*a**1.5: |
||
188 | close = np.argmin(dist) |
||
189 | |||
190 | if B[i-a][close] in B[i]: |
||
191 | close = None |
||
192 | |||
193 | |||
194 | if close is None: |
||
195 | news = news + 1 |
||
196 | B[i][n] = f'B{news}' |
||
197 | log.debug(f' component {n} is new, naming it {B[i][n]}') |
||
198 | else: |
||
199 | log.debug(f' component {n} is close to {B[i-a][close]} of previous epoch ({a} epochs before)') |
||
200 | B[i][n] = B[i-a][close] |
||
201 | |||
202 | |||
203 | log.debug(f' -> FOUND: {B[i]}\n') |
||
204 | |||
205 | |||
206 | for i, (date, data) in enumerate(zip(mod_dates, mod_data)): |
||
207 | data['label'] = B[i] |
||
208 | |||
209 | mod = pd.concat(mod_data, ignore_index=True) |
||
210 | |||
211 | log.debug('Finito!') |
||
212 | |||
213 | return mod |
||
214 | |||
215 | |||
216 | |||
217 | |||
218 | ############################################ |
||
219 | ############## Knots 2D GUI ################ |
||
220 | ############################################ |
||
221 | |||
222 | def KnotsId2dGUI(mod, twidth=0.75, use_arrows=False, arrow_pos=1.0): |
||
223 | """ |
||
224 | Prompt a GUI to select identified knots and alter their label, reprenting their 2D |
||
225 | spatial distribution in different times. |
||
226 | |||
227 | It can be used inside jupyter notebooks, using '%matplotlib widget' first. |
||
228 | |||
229 | Parameters: |
||
230 | ----------- |
||
231 | mod : :pd.DataFrame: |
||
232 | pandas.DataFrame containing every knot, with at least columns |
||
233 | 'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
||
234 | |||
235 | Returns: |
||
236 | -------- |
||
237 | mod : :pd.DataFrame: |
||
238 | pandas.DataFrame containing every knot with their altered labels, with |
||
239 | at least columns 'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
||
240 | """ |
||
241 | |||
242 | mod = mod.copy() |
||
243 | |||
244 | knots = dict(tuple(mod.groupby('label'))) |
||
245 | knots_names = list(knots.keys()) |
||
246 | knots_values = list(knots.values()) |
||
247 | knots_jyears = {k:Time(knots[k]['date'].to_numpy()).jyear for k in knots} |
||
248 | knots_X = {k:knots[k]['X'].to_numpy() for k in knots} |
||
249 | knots_Y = {k:knots[k]['Y'].to_numpy() for k in knots} |
||
250 | |||
251 | |||
252 | from matplotlib.widgets import Slider, Button, TextBox, RectangleSelector |
||
253 | |||
254 | out = get_output() |
||
255 | |||
256 | fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8,8)) |
||
257 | |||
258 | lineas = list() |
||
259 | flechas = list() |
||
260 | textos = list() |
||
261 | |||
262 | def draw_all(val=2008): |
||
263 | nonlocal lineas, flechas, textos |
||
264 | |||
265 | # instead of clearing the whole axis, remove artists |
||
266 | for linea in lineas: |
||
267 | if linea is not None: |
||
268 | linea.remove() |
||
269 | for texto in textos: |
||
270 | if texto is not None: |
||
271 | texto.remove() |
||
272 | for flecha in flechas: |
||
273 | if flecha is not None: |
||
274 | flecha.remove() |
||
275 | ax.set_prop_cycle(None) |
||
276 | |||
277 | lineas = list() |
||
278 | flechas = list() |
||
279 | textos = list() |
||
280 | |||
281 | xlim, ylim = ax.get_xlim(), ax.get_ylim() |
||
282 | |||
283 | #ax.clear() # either clear the whole axis or remove every artist separetly |
||
284 | |||
285 | for i, label in enumerate(knots_names): |
||
286 | years = knots_jyears[label] |
||
287 | idx = (val-twidth < years) & (years < val) |
||
288 | x = knots_X[label][idx] |
||
289 | y = knots_Y[label][idx] |
||
290 | |||
291 | if len(x) > 0: |
||
292 | if label == '' or label == 'None': # if it is unlabelled, do not connect it. |
||
293 | lineas.append(ax.plot(x, y, '.', linewidth=0.6, alpha=0.4, label=label, color='black')[0]) |
||
294 | else: |
||
295 | lineas.append(ax.plot(x, y, '.-', linewidth=0.6, alpha=0.4, label=label)[0]) |
||
296 | else: |
||
297 | lineas.append(None) |
||
298 | |||
299 | if use_arrows: |
||
300 | if len(x) > 1 and label != '' and label != 'None': |
||
301 | flechas.append(ax.quiver(x[:-1], |
||
302 | y[:-1], |
||
303 | arrow_pos*(x[1:] - x[:-1]), |
||
304 | arrow_pos*(y[1:] - y[:-1]), |
||
305 | scale_units='xy', angles='xy', scale=1, |
||
306 | width=0.0015, headwidth=10, headlength=10, headaxislength=6, |
||
307 | alpha=0.5, color=lineas[i].get_color())) |
||
308 | else: |
||
309 | flechas.append(None) |
||
310 | |||
311 | if len(x) > 0: |
||
312 | #textos.append(ax.annotate(label, (x[0], y[0]), (-28,-10), textcoords='offset points', color=lineas[i].get_color(), fontsize=14)) |
||
313 | textos.append(ax.text(x[-1]+0.015, y[-1]+0.015, label, {'color':lineas[i].get_color(), 'fontsize':14})) |
||
314 | else: |
||
315 | textos.append(None) |
||
316 | |||
317 | ax.set_xlim(xlim) |
||
318 | ax.set_ylim(ylim) |
||
319 | ax.set_aspect('equal') |
||
320 | |||
321 | fig.canvas.draw_idle() # if removed every artist separately instead of ax.clear() |
||
322 | |||
323 | draw_all() |
||
324 | |||
325 | def update(val): |
||
326 | nonlocal lineas, flechas, textos |
||
327 | |||
328 | for i, label in enumerate(knots_names): |
||
329 | years = knots_jyears[label] |
||
330 | idx = (val-twidth < years) & (years < val) |
||
331 | x = knots_X[label][idx] |
||
332 | y = knots_Y[label][idx] |
||
333 | |||
334 | View Code Duplication | if lineas[i] is not None: |
|
335 | if len(x) > 0: |
||
336 | lineas[i].set_xdata(x) |
||
337 | lineas[i].set_ydata(y) |
||
338 | else: |
||
339 | lineas[i].remove() |
||
340 | lineas[i] = None |
||
341 | else: |
||
342 | if len(x) > 0: |
||
343 | if label == '' or label == 'None': # if it is unlabelled, do not connect it. |
||
344 | lineas[i] = ax.plot(x, y, '.', linewidth=0.8, alpha=0.5, label=label, color='black')[0] |
||
345 | else: |
||
346 | lineas[i] = ax.plot(x, y, '.-', linewidth=0.8, alpha=0.5, label=label)[0] |
||
347 | |||
348 | |||
349 | if textos[i] is not None: |
||
350 | if len(x) > 0: |
||
351 | textos[i].set_position((x[-1]+0.015, y[-1]+0.015)) |
||
352 | textos[i].set_text(label) |
||
353 | else: |
||
354 | textos[i].remove() |
||
355 | textos[i] = None |
||
356 | #textos[i].set_position((10, 10)) |
||
357 | else: |
||
358 | if len(x) > 0: |
||
359 | textos[i] = ax.text(x[-1]+0.02, y[-1]+0.02, label, {'color':lineas[i].get_color(), 'fontsize':14}) |
||
360 | |||
361 | if use_arrows: |
||
362 | if flechas[i] is not None: |
||
363 | flechas[i].remove() |
||
364 | flechas[i] = None |
||
365 | if len(x) > 1 and label != '' and label != 'None': |
||
366 | flechas[i] = ax.quiver(x[:-1], |
||
367 | y[:-1], |
||
368 | arrow_pos*(x[1:] - x[:-1]), |
||
369 | arrow_pos*(y[1:] - y[:-1]), |
||
370 | scale_units='xy', angles='xy', scale=1, |
||
371 | width=0.0015, headwidth=10, headlength=10, headaxislength=6, |
||
372 | alpha=0.5, color=lineas[i].get_color()) |
||
373 | |||
374 | fig.canvas.draw_idle() |
||
375 | |||
376 | |||
377 | |||
378 | selected_knot = None |
||
379 | selected_ind = None |
||
380 | selected_x = None |
||
381 | selected_y = None |
||
382 | |||
383 | |||
384 | View Code Duplication | def submit_textbox(text): |
|
385 | nonlocal mod, knots, knots_names, knots_values, knots_jyears, knots_X, knots_Y |
||
386 | |||
387 | log.debug('Submited with:') |
||
388 | log.debug(f' selected_knot {selected_knot}') |
||
389 | log.debug(f' selected_ind {selected_ind}') |
||
390 | log.debug(f' selected_x {selected_x}') |
||
391 | log.debug(f' selected_y {selected_y}') |
||
392 | |||
393 | if selected_knot is not None: |
||
394 | mod.loc[selected_ind, 'label'] = text.upper().strip(' ') |
||
395 | |||
396 | knots = dict(tuple(mod.groupby('label'))) |
||
397 | knots_names = list(knots.keys()) |
||
398 | knots_values = list(knots.values()) |
||
399 | knots_jyears = {k:Time(knots[k]['date'].to_numpy()).jyear for k in knots} |
||
400 | knots_X = {k:knots[k]['X'].to_numpy() for k in knots} |
||
401 | knots_Y = {k:knots[k]['Y'].to_numpy() for k in knots} |
||
402 | |||
403 | log.debug(f"Updated index {selected_ind} to {text.upper()}") |
||
404 | else: |
||
405 | pass |
||
406 | |||
407 | draw_all(slider_date.val) |
||
408 | |||
409 | |||
410 | def line_select_callback(eclick, erelease): |
||
411 | nonlocal selected_knot,selected_x, selected_y, selected_ind |
||
412 | |||
413 | # 1 eclick and erelease are the press and release events |
||
414 | x1, y1 = eclick.xdata, eclick.ydata |
||
415 | x2, y2 = erelease.xdata, erelease.ydata |
||
416 | log.debug("GUI: (%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2)) |
||
417 | log.debug("GUI: The button you used were: %s %s" % (eclick.button, erelease.button)) |
||
418 | |||
419 | selected_knot = None |
||
420 | selected_x = None |
||
421 | selected_y = None |
||
422 | selected_ind = None |
||
423 | |||
424 | for i, label in enumerate(knots_names): |
||
425 | years = knots_jyears[label] |
||
426 | idx = (slider_date.val-twidth < years) & (years < slider_date.val) |
||
427 | |||
428 | if np.sum(idx) == 0: |
||
429 | continue # we did not select any component from this component, next one |
||
430 | |||
431 | x = np.array(knots_X[label]) |
||
432 | y = np.array(knots_Y[label]) |
||
433 | |||
434 | # get points iside current rectangle for current date |
||
435 | rect_idx = (x1 < x) & ( x < x2) & (y1 < y) & ( y < y2) & idx |
||
436 | |||
437 | View Code Duplication | if np.sum(rect_idx) > 0: |
|
438 | textbox.set_val(label) |
||
439 | |||
440 | selected_knot = label |
||
441 | selected_x = x[rect_idx].ravel() |
||
442 | selected_y = y[rect_idx].ravel() |
||
443 | selected_ind = knots[label].index[rect_idx] |
||
444 | |||
445 | log.debug(f'Selected {label} points rect_idx {rect_idx} x {x[rect_idx]}, y {y[rect_idx]} with indices {selected_ind}') |
||
446 | |||
447 | textbox.begin_typing(None) |
||
448 | break # if we find selected components in this epoch, continue with renaming |
||
449 | else: |
||
450 | pass |
||
451 | |||
452 | # print epoch of selected points: |
||
453 | if selected_knot is not None: |
||
454 | out.clear_output() |
||
455 | out.append_stdout('Selected:\n') |
||
456 | for idx in selected_ind: |
||
457 | #print(f"Selected {mod.loc[idx, 'label']} {mod.loc[idx, 'date']}") |
||
458 | out.append_stdout(f" -> {mod.loc[idx, 'label']} {mod.loc[idx, 'date'].strftime('%Y-%m-%d')}\n") |
||
459 | |||
460 | update(slider_date.val) |
||
461 | |||
462 | |||
463 | View Code Duplication | def toggle_selector(event): |
|
464 | log.debug('GUI: Key pressed.') |
||
465 | if event.key in ['Q', 'q'] and toggle_selector.RS.active: |
||
466 | log.debug('Selector deactivated.') |
||
467 | toggle_selector.RS.set_active(False) |
||
468 | if event.key in ['S', 's'] and not toggle_selector.RS.active: |
||
469 | log.debug('Selector activated.') |
||
470 | toggle_selector.RS.set_active(True) |
||
471 | if event.key in ['R', 'r']: |
||
472 | log.debug('Selector deactivated.') |
||
473 | toggle_selector.RS.set_active(False) |
||
474 | textbox.begin_typing(None) |
||
475 | #textbox.set_val('') |
||
476 | if event.key in ['E', 'e']: |
||
477 | out.append_stdout('-> Close\n') |
||
478 | plt.close() |
||
479 | |||
480 | |||
481 | toggle_selector.RS = RectangleSelector(ax, line_select_callback, |
||
482 | drawtype='box', useblit=True, |
||
483 | button=[1, 3], # don't use middle button |
||
484 | minspanx=0, minspany=0, |
||
485 | spancoords='data', |
||
486 | interactive=False) |
||
487 | |||
488 | |||
489 | #plt.connect('key_press_event', toggle_selector) |
||
490 | fig.canvas.mpl_connect('key_press_event', toggle_selector) |
||
491 | |||
492 | |||
493 | |||
494 | from mpl_toolkits.axes_grid1 import make_axes_locatable |
||
495 | |||
496 | divider_slider = make_axes_locatable(ax) |
||
497 | slider_ax = divider_slider.append_axes("top", size="3%", pad="4%") |
||
498 | slider_date = Slider(ax=slider_ax, label="Date", valmin=2007, valmax=2020, valinit=2008, valstep=0.2, orientation="horizontal") |
||
499 | slider_date.on_changed(update) |
||
500 | |||
501 | #divider_textbox = make_axes_locatable(ax) |
||
502 | #textbox_ax = divider_textbox.append_axes("bottom", size="3%", pad="4%") |
||
503 | textbox_ax = fig.add_axes([0.3,0.015,0.5,0.05]) |
||
504 | textbox = TextBox(textbox_ax, 'Knot name:', initial='None') |
||
505 | textbox.on_submit(submit_textbox) |
||
506 | |||
507 | |||
508 | |||
509 | ax.set_xlim([-1.0, +1.0]) |
||
510 | ax.set_ylim([-1.0, +1.0]) |
||
511 | ax.set_aspect('equal') |
||
512 | |||
513 | fig.suptitle('S to select, R to rename, Q to deactivate selector, E to exit') |
||
514 | |||
515 | print('Usage:', |
||
516 | '-> S to select, R to rename, Q to deactivate selector, E to exit', |
||
517 | '-> To delete the knot, name it with a whitespace', |
||
518 | '-> You can select points from one component at a time', |
||
519 | '-> If you use the zoom or movement tools, remember to unselect them', |
||
520 | sep='\n') |
||
521 | |||
522 | plt.show() |
||
523 | |||
524 | out.display_output() |
||
525 | |||
526 | return mod |
||
527 | |||
528 | |||
529 | |||
530 | |||
531 | ############################################ |
||
532 | ################ Knots GUI ################# |
||
533 | ############################################ |
||
534 | |||
535 | |||
536 | |||
537 | def KnotsIdGUI(mod): |
||
538 | """ |
||
539 | Prompt a GUI to select identified knots and alter their label, reprenting their |
||
540 | time evolution. |
||
541 | |||
542 | It can be used inside jupyter notebooks, using '%matplotlib widget' first. |
||
543 | |||
544 | Parameters: |
||
545 | ----------- |
||
546 | mod : :pd.DataFrame: |
||
547 | pandas.DataFrame containing every knot, with at least columns |
||
548 | 'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
||
549 | |||
550 | Returns: |
||
551 | -------- |
||
552 | mod : :pd.DataFrame: |
||
553 | pandas.DataFrame containing every knot with their altered labels, with |
||
554 | at least columns 'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
||
555 | """ |
||
556 | |||
557 | mod = mod.copy() |
||
558 | |||
559 | knots = dict(tuple(mod.groupby('label'))) |
||
560 | knots_names = list(knots.keys()) |
||
561 | knots_values = list(knots.values()) |
||
562 | knots_jyears = {k:Time(knots[k]['date'].to_numpy()).jyear for k in knots} |
||
563 | knots_dates = {k:knots[k]['date'].to_numpy() for k in knots} |
||
564 | knots_fluxes = {k:knots[k]['Flux (Jy)'].to_numpy() for k in knots} |
||
565 | |||
566 | from matplotlib.widgets import TextBox, RectangleSelector |
||
567 | |||
568 | out = get_output() |
||
569 | |||
570 | fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8,5)) |
||
571 | |||
572 | lineas = list() |
||
573 | textos = list() |
||
574 | |||
575 | def draw_all(): |
||
576 | nonlocal lineas, textos |
||
577 | |||
578 | for linea in lineas: |
||
579 | if linea is not None: |
||
580 | linea.remove() |
||
581 | for texto in textos: |
||
582 | if texto is not None: |
||
583 | texto.remove() |
||
584 | |||
585 | ax.set_prop_cycle(None) |
||
586 | |||
587 | lineas = list() |
||
588 | textos = list() |
||
589 | |||
590 | #ax.clear() # either clear the whole axis or remove every artist separetly |
||
591 | |||
592 | for i, label in enumerate(knots_names): |
||
593 | x, y = knots_jyears[label], knots_fluxes[label] |
||
594 | |||
595 | if len(x) > 0: |
||
596 | if label == '' or label == 'None': # if it is unlabelled, do not connect it. |
||
597 | lineas.append(ax.plot(x, y, '.', linewidth=0.8, alpha=0.5, label=label, color='black')[0]) |
||
598 | else: |
||
599 | lineas.append(ax.plot(x, y, '.-', linewidth=0.8, alpha=0.5, label=label)[0]) |
||
600 | else: |
||
601 | lineas.append(None) |
||
602 | |||
603 | if len(x) > 0: |
||
604 | #textos.append(ax.annotate(label, (x[0], y[0]), (-28,-10), textcoords='offset points', color=lineas[i].get_color(), fontsize=14)) |
||
605 | textos.append(ax.text(x[0], y[0], label, {'color':lineas[i].get_color(), 'fontsize':14})) |
||
606 | else: |
||
607 | textos.append(None) |
||
608 | |||
609 | fig.canvas.draw_idle() # if removed every artist separately instead of ax.clear() |
||
610 | |||
611 | |||
612 | def update(): |
||
613 | nonlocal lineas, textos |
||
614 | |||
615 | for i, label in enumerate(knots_names): |
||
616 | x, y = knots_jyears[label], knots_fluxes[label] |
||
617 | |||
618 | View Code Duplication | if lineas[i] is not None: |
|
619 | if len(x) > 0: |
||
620 | lineas[i].set_xdata(x) |
||
621 | lineas[i].set_ydata(y) |
||
622 | else: |
||
623 | lineas[i].remove() |
||
624 | lineas[i] = None |
||
625 | else: |
||
626 | if len(x) > 0: |
||
627 | if label == '' or label == 'None': # if it is unlabelled, do not connect it. |
||
628 | lineas[i] = ax.plot(x, y, '.', linewidth=0.8, alpha=0.5, label=label, color='black')[0] |
||
629 | else: |
||
630 | lineas[i] = ax.plot(x, y, '.-', linewidth=0.8, alpha=0.5, label=label)[0] |
||
631 | |||
632 | if textos[i] is not None: |
||
633 | if len(x) > 0: |
||
634 | textos[i].set_position((x[0], y[0])) |
||
635 | textos[i].set_text(label) |
||
636 | else: |
||
637 | textos[i].remove() |
||
638 | textos[i] = None |
||
639 | #textos[i].set_position((10, 10)) |
||
640 | else: |
||
641 | if len(x) > 0: |
||
642 | #textos[i] = ax.annotate(label, (x[0], y[0]), (-24,-10), textcoords='offset points', color=lineas[i].get_color(), fontsize=15) |
||
643 | textos[i] = ax.text(x[0], y[0], label, {'color':lineas[i].get_color(), 'fontsize':14}) |
||
644 | |||
645 | fig.canvas.draw_idle() |
||
646 | |||
647 | |||
648 | selected_knot = None |
||
649 | selected_ind = None |
||
650 | selected_date = None |
||
651 | selected_flux = None |
||
652 | |||
653 | View Code Duplication | def submit_textbox(text): |
|
654 | nonlocal mod, knots, knots_names, knots_values, knots_jyears, knots_dates, knots_fluxes |
||
655 | |||
656 | log.debug('Submited with:') |
||
657 | log.debug(f' selected_knot {selected_knot}') |
||
658 | log.debug(f' selected_ind {selected_ind}') |
||
659 | log.debug(f' selected_flux {selected_flux}') |
||
660 | log.debug(f' selected_date {selected_date}') |
||
661 | |||
662 | if selected_knot is not None: |
||
663 | mod.loc[selected_ind, 'label'] = text.upper().strip(' ') |
||
664 | |||
665 | knots = dict(tuple(mod.groupby('label'))) |
||
666 | knots_names = list(knots.keys()) |
||
667 | knots_values = list(knots.values()) |
||
668 | knots_jyears = {k:Time(knots[k]['date'].to_numpy()).jyear for k in knots} |
||
669 | knots_dates = {k:knots[k]['date'].to_numpy() for k in knots} |
||
670 | knots_fluxes = {k:knots[k]['Flux (Jy)'].to_numpy() for k in knots} |
||
671 | |||
672 | print(f"Updated index {selected_ind} to {text.upper()}") |
||
673 | else: |
||
674 | pass |
||
675 | |||
676 | draw_all() |
||
677 | |||
678 | def line_select_callback(eclick, erelease): |
||
679 | nonlocal selected_knot,selected_date, selected_flux, selected_ind |
||
680 | |||
681 | # 1 eclick and erelease are the press and release events |
||
682 | x1, y1 = eclick.xdata, eclick.ydata |
||
683 | x2, y2 = erelease.xdata, erelease.ydata |
||
684 | log.debug("GUI: (%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2)) |
||
685 | log.debug("GUI: The button you used were: %s %s" % (eclick.button, erelease.button)) |
||
686 | |||
687 | selected_knot = None |
||
688 | selected_date = None |
||
689 | selected_flux = None |
||
690 | selected_ind = None |
||
691 | |||
692 | for i, label in enumerate(knots_names): |
||
693 | years = knots_jyears[label] |
||
694 | fluxes = knots_fluxes[label] |
||
695 | |||
696 | # get points iside current rectangle for current date |
||
697 | rect_idx = (x1 < years) & ( years < x2) & (y1 < fluxes) & ( fluxes < y2) |
||
698 | |||
699 | if np.sum(rect_idx) == 0: |
||
700 | continue # we did not select any component from this component, next one |
||
701 | |||
702 | x = np.array(years) |
||
703 | y = np.array(fluxes) |
||
704 | |||
705 | View Code Duplication | if np.sum(rect_idx) > 0: |
|
706 | textbox.set_val(label) |
||
707 | selected_knot = label |
||
708 | selected_x = x[rect_idx].ravel() |
||
709 | selected_y = y[rect_idx].ravel() |
||
710 | selected_ind = knots[label].index[rect_idx] |
||
711 | log.debug(f'Selected {label} points rect_idx {rect_idx} date {x[rect_idx]}, flux {y[rect_idx]} with indices {selected_ind}') |
||
712 | break # if we find selected components in this epoch, continue with renaming |
||
713 | else: |
||
714 | pass |
||
715 | |||
716 | # print epoch of selected points: |
||
717 | if selected_knot is not None: |
||
718 | out.clear_output() |
||
719 | out.append_stdout('Selected:\n') |
||
720 | for idx in selected_ind: |
||
721 | #print(f"Selected {mod.loc[idx, 'label']} {mod.loc[idx, 'date']}") |
||
722 | out.append_stdout(f" -> {mod.loc[idx, 'label']} {mod.loc[idx, 'date'].strftime('%Y-%m-%d')}\n") |
||
723 | |||
724 | update() |
||
725 | |||
726 | |||
727 | View Code Duplication | def toggle_selector(event): |
|
728 | log.debug('GUI: Key pressed.') |
||
729 | if event.key in ['Q', 'q'] and toggle_selector.RS.active: |
||
730 | log.debug('Selector deactivated.') |
||
731 | toggle_selector.RS.set_active(False) |
||
732 | if event.key in ['S', 's'] and not toggle_selector.RS.active: |
||
733 | log.debug('Selector activated.') |
||
734 | toggle_selector.RS.set_active(True) |
||
735 | if event.key in ['R', 'r']: |
||
736 | log.debug('Selector deactivated.') |
||
737 | toggle_selector.RS.set_active(False) |
||
738 | textbox.begin_typing(None) |
||
739 | #textbox.set_val('') |
||
740 | if event.key in ['E', 'e']: |
||
741 | out.append_stdout('-> Close\n') |
||
742 | plt.close() |
||
743 | |||
744 | |||
745 | toggle_selector.RS = RectangleSelector(ax, line_select_callback, |
||
746 | drawtype='box', useblit=True, |
||
747 | button=[1, 3], # don't use middle button |
||
748 | minspanx=0, minspany=0, |
||
749 | spancoords='data', |
||
750 | interactive=False) |
||
751 | |||
752 | |||
753 | fig.canvas.mpl_connect('key_press_event', toggle_selector) |
||
754 | |||
755 | from mpl_toolkits.axes_grid1 import make_axes_locatable |
||
756 | |||
757 | divider_textbox = make_axes_locatable(ax) |
||
758 | textbox_ax = divider_textbox.append_axes("bottom", size="10%", pad="15%") |
||
759 | #textbox_ax = fig.add_axes([0.3,0,0.5,0.05]) |
||
760 | textbox = TextBox(textbox_ax, 'Knot name:', initial='None') |
||
761 | textbox.on_submit(submit_textbox) |
||
762 | |||
763 | draw_all() |
||
764 | |||
765 | #ax.autoscale() |
||
766 | ax.set_xlabel('date (year)') |
||
767 | ax.set_ylabel('Flux (Jy)') |
||
768 | ax.set_title('Flux from each component') |
||
769 | |||
770 | xlims = Time(np.amin(mod['date'])).jyear, Time(np.amax(mod['date'])).jyear |
||
771 | ax.set_xlim((xlims[0]-0.03*np.abs(xlims[1]-xlims[0]), xlims[1]+0.03*np.abs(xlims[1]-xlims[0]))) |
||
772 | |||
773 | fig.suptitle('S to select, R to rename, Q to deactivate selector, E to exit') |
||
774 | |||
775 | print('Usage:', |
||
776 | '-> S to select, R to rename, Q to deactivate selector, E to exit', |
||
777 | '-> To delete the knot, name it with a whitespace', |
||
778 | '-> You can select points from one component at a time', |
||
779 | '-> If you use the zoom or movement tools, remember to unselect them', |
||
780 | sep='\n') |
||
781 | |||
782 | plt.show() |
||
783 | |||
784 | out.display_output() |
||
785 | |||
786 | return mod |
||
787 | |||
788 | |||
789 | |||
790 | ################################################### |
||
791 | ########### Knots Read Mod (diffmap only) ######### |
||
792 | ################################################### |
||
793 | |||
794 | def KnotsIdReadMod_from_diffmap(path=None): |
||
795 | """ |
||
796 | Read *_mod.mod files as printed by diffmap, return a dataframe containing all information ready |
||
797 | to be worked on for labelling and to be used with these GUIs. |
||
798 | |||
799 | Parameters: |
||
800 | ----------- |
||
801 | path : :str: |
||
802 | string indicating the path to the mod files to be used, their names must end in the format |
||
803 | '%Y-%m-%d_mod.mod', for example, path = 'vlbi/ftree/*/*_mod.mod'. |
||
804 | |||
805 | Returns: |
||
806 | ----------- |
||
807 | mod : :pd.DataFrame: |
||
808 | pandas.DataFrame containing every knot, with at least columns |
||
809 | 'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
||
810 | """ |
||
811 | |||
812 | if path is None: |
||
813 | log.error('Path not specified') |
||
814 | raise Exception('Path not specified') |
||
815 | |||
816 | mod_dates_str = list() |
||
817 | mod_dates = list() |
||
818 | mod_data = list() |
||
819 | |||
820 | for f in glob.glob(f'{path}'): |
||
821 | match = re.findall(r'([0-9]{4}-[0-9]{2}-[0-9]{2})_mod.mod', f) |
||
822 | if not len(match) > 0: |
||
823 | continue |
||
824 | |||
825 | date_str = match[0] |
||
826 | date = datetime.strptime(date_str, '%Y-%m-%d') |
||
827 | |||
828 | mod_dates_str.append(date_str) |
||
829 | mod_dates.append(date) |
||
830 | mod_data.append(pd.read_csv(f, sep='\s+', comment='!', names=['Flux (Jy)', 'Radius (mas)', 'Theta (deg)', 'Major FWHM (mas)', 'Axial ratio', 'Phi (deg)', 'T', 'Freq (Hz)', 'SpecIndex'])) |
||
831 | |||
832 | # sort by date |
||
833 | idx = np.argsort(mod_dates) |
||
834 | mod_dates_str = list(np.array(mod_dates_str, dtype=object)[idx]) |
||
835 | mod_dates = list(np.array(mod_dates, dtype=object)[idx]) |
||
836 | mod_data = list(np.array(mod_data, dtype=object)[idx]) |
||
837 | |||
838 | # fix stupid 'v' in columns, insert a label field, add X, Y columns |
||
839 | for i in range(len(mod_dates)): |
||
840 | mod_data[i].insert(0, 'label', value=None) |
||
841 | mod_data[i].insert(1, 'date', value=mod_dates[i]) |
||
842 | |||
843 | mod_data[i]['Flux (Jy)'] = mod_data[i]['Flux (Jy)'].str.strip('v').astype(float) |
||
844 | mod_data[i]['Radius (mas)'] = mod_data[i]['Radius (mas)'].str.strip('v').astype(float) |
||
845 | mod_data[i]['Theta (deg)'] = mod_data[i]['Theta (deg)'].str.strip('v').astype(float) |
||
846 | |||
847 | mod_data[i].insert(5, 'X', mod_data[i]['Radius (mas)']*np.cos(np.pi/180*(mod_data[i]['Theta (deg)']-90))) |
||
848 | mod_data[i].insert(6, 'Y', mod_data[i]['Radius (mas)']*np.sin(np.pi/180*(mod_data[i]['Theta (deg)']-90))) |
||
849 | |||
850 | |||
851 | mod = pd.concat(mod_data, ignore_index=True) |
||
852 | |||
853 | return mod |
||
854 | |||
855 | |||
856 | ################################################### |
||
857 | ################## Knots Save Mod ################# |
||
858 | ################################################### |
||
859 | |||
860 | def KnotsIdSaveMod(mod, path=None, rewrite=False, drop_blanks=False): |
||
861 | """ |
||
862 | Save Knots data to *_mod.mod files ready to be re-proccessed with diffmap. |
||
863 | |||
864 | Actually only useful to remove stupid knots and fit it again with diffmap. |
||
865 | |||
866 | Parameters: |
||
867 | ----------- |
||
868 | mod : :pd.DataFrame: |
||
869 | pandas.DataFrame containing every knot |
||
870 | path: :str: |
||
871 | string containing the path to which the files are to be saved, eg: |
||
872 | path = 'my_knots/' |
||
873 | rewrite: :bool: |
||
874 | If rewrite is True, path is interpreted as a regex for which files |
||
875 | to overwrite. |
||
876 | drop_blanks: :bool: |
||
877 | If True, knots without label (or whose label is a whitspace) are dropped. |
||
878 | If False (default), they are saved without label. |
||
879 | |||
880 | Returns: |
||
881 | -------- |
||
882 | None |
||
883 | """ |
||
884 | |||
885 | if path is None: |
||
886 | log.error('Path not specified') |
||
887 | raise Exception('Path not specified') |
||
888 | |||
889 | |||
890 | mod = mod.copy() |
||
891 | |||
892 | |||
893 | if drop_blanks: |
||
894 | mod = mod.drop(mod[mod['label'].str.strip('') == ''].index) |
||
895 | |||
896 | |||
897 | mod_dict = dict(list(mod.groupby('date'))) |
||
898 | |||
899 | |||
900 | if rewrite: |
||
901 | file_list = list() |
||
902 | for f in glob.glob(f'{path}'): |
||
903 | match = re.findall(r'([0-9]{4}-[0-9]{2}-[0-9]{2})_mod.mod', f) |
||
904 | |||
905 | if not len(match) > 0: |
||
906 | continue |
||
907 | |||
908 | date = datetime.strptime(match[0], '%Y-%m-%d') |
||
909 | |||
910 | if date not in mod_dict: |
||
911 | raise Exception('Specified regex includes files for which there are no knots') |
||
912 | |||
913 | file_list.append(f) |
||
914 | |||
915 | if len(file_list) != len(mod_dict): |
||
916 | raise Exception(f'Specified files by path regex does not match the number of epochs ({len(file_list)} vs {len(mod_dict)})') |
||
917 | else: |
||
918 | file_list = list() |
||
919 | for date in mod_dict.keys(): |
||
920 | file_list.append(f"{path}/{date.strftime('%Y-%m-%d')}_mod.mod") |
||
921 | |||
922 | |||
923 | for fname, (date, data) in zip(file_list, mod_dict.items()): |
||
924 | data = data.copy() |
||
925 | |||
926 | #data = data.drop(columns=['label']) |
||
927 | #data = data.drop(columns=['X']) |
||
928 | #data = data.drop(columns=['Y']) |
||
929 | |||
930 | data['Flux (Jy)'] = data['Flux (Jy)'].astype(str) + 'v' |
||
931 | data['Radius (mas)'] = data['Radius (mas)'].astype(str) + 'v' |
||
932 | data['Theta (deg)'] = data['Theta (deg)'].astype(str) + 'v' |
||
933 | |||
934 | ## add previous labels in comments (so diffmap does not get stuck with it) |
||
935 | data['label'] = '!' + data['label'] |
||
936 | |||
937 | with open(fname, 'w') as f: |
||
938 | f.write("! Generated by mutis\n" |
||
939 | "! Columns\n" |
||
940 | "! 'Flux (Jy)', 'Radius (mas)', 'Theta (deg)', 'Major FWHM (mas)', 'Axial ratio', 'Phi (deg)', 'T', 'Freq (Hz)', 'SpecIndex', '!label'\n") |
||
941 | data.to_csv(f, |
||
942 | columns=['Flux (Jy)', 'Radius (mas)', 'Theta (deg)', 'Major FWHM (mas)', 'Axial ratio', 'Phi (deg)', 'T', 'Freq (Hz)', 'SpecIndex', 'label'], |
||
943 | index=False, sep=' ', header=None) |
||
944 | |||
945 | |||
946 | |||
947 | ################################################### |
||
948 | ################## Knots Read Mod ################# |
||
949 | ################################################### |
||
950 | |||
951 | def KnotsIdReadMod(path=None): |
||
952 | """ |
||
953 | Read *_mod.mod files as printed by diffmap, return a dataframe containing all information ready |
||
954 | to be worked on for labelling and to be used with these GUIs. |
||
955 | |||
956 | It detects whether the file was generated by mutis. If it was then |
||
957 | it searchs for a hidden label column to load previous label names (unnamed labels |
||
958 | |||
959 | Parameters: |
||
960 | ----------- |
||
961 | path : :str: |
||
962 | string indicating the path to the mod files to be used, their names must end in the format |
||
963 | '%Y-%m-%d_mod.mod', for example, path = 'vlbi/ftree/*/*_mod.mod'. |
||
964 | |||
965 | Returns: |
||
966 | ----------- |
||
967 | mod : :pd.DataFrame: |
||
968 | pandas.DataFrame containing every knot, with at least columns |
||
969 | 'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
||
970 | """ |
||
971 | |||
972 | if path is None: |
||
973 | log.error('Path not specified') |
||
974 | raise Exception('Path not specified') |
||
975 | |||
976 | mod_dates_str = list() |
||
977 | mod_dates = list() |
||
978 | mod_data = list() |
||
979 | |||
980 | for f in glob.glob(f'{path}'): |
||
981 | match = re.findall(r'([0-9]{4}-[0-9]{2}-[0-9]{2})_mod.mod', f) |
||
982 | if not len(match) > 0: |
||
983 | continue |
||
984 | |||
985 | date_str = match[0] |
||
986 | date = datetime.strptime(date_str, '%Y-%m-%d') |
||
987 | |||
988 | mod_dates_str.append(date_str) |
||
989 | mod_dates.append(date) |
||
990 | |||
991 | with open(f) as fobj: |
||
992 | firstline = fobj.readline() |
||
993 | if firstline == '! Generated by mutis\n': |
||
994 | generated_by_mutis = True |
||
995 | else: |
||
996 | generated_by_mutis = False |
||
997 | |||
998 | if generated_by_mutis: |
||
999 | data = pd.read_csv(f, sep='\s+', |
||
1000 | skiprows=3, |
||
1001 | #converters={'label':(lambda l: '' if l == '' else l)}, |
||
1002 | dtype={'label':str}, |
||
1003 | names=['Flux (Jy)', 'Radius (mas)', 'Theta (deg)', 'Major FWHM (mas)', 'Axial ratio', 'Phi (deg)', 'T', 'Freq (Hz)', 'SpecIndex', |
||
1004 | 'label']) |
||
1005 | data['label'] = data['label'].str.strip('!') |
||
1006 | else: |
||
1007 | data = pd.read_csv(f, sep='\s+', comment='!', names=['Flux (Jy)', 'Radius (mas)', 'Theta (deg)', 'Major FWHM (mas)', 'Axial ratio', 'Phi (deg)', 'T', 'Freq (Hz)', 'SpecIndex']) |
||
1008 | #data.insert(0, 'label', value=None) |
||
1009 | data.insert(0, 'label', value='') |
||
1010 | |||
1011 | data.insert(1, 'date', value=date) |
||
1012 | |||
1013 | data['Flux (Jy)'] = data['Flux (Jy)'].str.strip('v').astype(float) |
||
1014 | data['Radius (mas)'] = data['Radius (mas)'].str.strip('v').astype(float) |
||
1015 | data['Theta (deg)'] = data['Theta (deg)'].str.strip('v').astype(float) |
||
1016 | |||
1017 | data.insert(5, 'X', data['Radius (mas)']*np.cos(np.pi/180*(data['Theta (deg)']-90))) |
||
1018 | data.insert(6, 'Y', data['Radius (mas)']*np.sin(np.pi/180*(data['Theta (deg)']-90))) |
||
1019 | |||
1020 | mod_data.append(data) |
||
1021 | |||
1022 | # sort by date |
||
1023 | idx = np.argsort(mod_dates) |
||
1024 | mod_dates_str = list(np.array(mod_dates_str, dtype=object)[idx]) |
||
1025 | mod_dates = list(np.array(mod_dates, dtype=object)[idx]) |
||
1026 | mod_data = list(np.array(mod_data, dtype=object)[idx]) |
||
1027 | |||
1028 | mod = pd.concat(mod_data, ignore_index=True) |
||
1029 | |||
1030 | return mod |
||
1031 | |||
1032 | |||
1033 | |||
1034 | ################################################### |
||
1035 | ########### Knots CSV final outputting ############ |
||
1036 | ################################################### |
||
1037 | |||
1038 | def KnotsIdSaveCSV(mod, path=None): |
||
1039 | """ |
||
1040 | Save Knots data to .csv files (as done by Svetlana? ##) |
||
1041 | |||
1042 | Each knot label has its own {label}.csv. To be compatible with Svetlana's format, |
||
1043 | columns should be modified to: |
||
1044 | 'Date' (jyear), 'MJD', 'X(mas)', 'Y(mas)', 'Flux(Jy)' |
||
1045 | These columns are derived from the ones in `mod`, old ones are removed. |
||
1046 | |||
1047 | Parameters: |
||
1048 | ----------- |
||
1049 | mod : :pd.DataFrame: |
||
1050 | pandas.DataFrame containing every knot, with at least columns |
||
1051 | 'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
||
1052 | path: :str: |
||
1053 | string containing the path to which the files are to be saved, eg: |
||
1054 | path = 'my_knots/' |
||
1055 | |||
1056 | Returns: |
||
1057 | -------- |
||
1058 | None |
||
1059 | """ |
||
1060 | |||
1061 | if path is None: |
||
1062 | log.error('Path not specified') |
||
1063 | raise Exception('Path not specified') |
||
1064 | |||
1065 | |||
1066 | mod = mod.copy() |
||
1067 | |||
1068 | |||
1069 | mod_dict = dict(list(mod.groupby('label'))) |
||
1070 | |||
1071 | for label, data in mod_dict.items(): |
||
1072 | data = data.copy() |
||
1073 | |||
1074 | #data.insert(0, 'Date', Time(data['date']).jyear) |
||
1075 | #data.insert(1, 'MJD', Time(data['date']).mjd) |
||
1076 | #data = data.rename(columns={'X': 'X (mas)', 'Y': 'Y (mas)'}) |
||
1077 | #data = data.drop(columns=['date']) |
||
1078 | #data = data.drop(columns=['label']) |
||
1079 | #if 'Radius (mas)' in data.columns: |
||
1080 | # data = data.rename(columns={'Radius (mas)':'R(mas)'}) |
||
1081 | #data.columns = data.columns.str.replace(' ', '') |
||
1082 | |||
1083 | data.to_csv(f'{path}/{label}.csv', index=False) |
||
1084 | |||
1085 | |||
1086 | |||
1087 | def KnotsIdReadCSV(path=None): |
||
1088 | """ |
||
1089 | Read Knots data to .csv files (as done by Svetlana? ##) |
||
1090 | |||
1091 | Each knot label has its own {label}.csv. To be compatible with Svetlana's format, |
||
1092 | columns should be modified to: |
||
1093 | 'Date' (jyear), 'MJD', 'X(mas)', 'Y(mas)', 'Flux(Jy)' |
||
1094 | These columns are derived from the ones in `mod`, old ones are removed. |
||
1095 | |||
1096 | Parameters: |
||
1097 | ----------- |
||
1098 | path: :str: |
||
1099 | string containing the path to read files from eg: |
||
1100 | path = 'myknows/*.csv' |
||
1101 | Returns: |
||
1102 | -------- |
||
1103 | mod : :pd.DataFrame: |
||
1104 | pandas.DataFrame containing every knot, with at least columns |
||
1105 | 'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
||
1106 | """ |
||
1107 | |||
1108 | if path is None: |
||
1109 | log.error('Path not specified') |
||
1110 | raise Exception('Path not specified') |
||
1111 | |||
1112 | dataL = list() |
||
1113 | |||
1114 | for f in glob.glob(f'{path}'): |
||
1115 | match = re.findall(r'/(.*).csv', f) |
||
1116 | |||
1117 | if not len(match) > 0: |
||
1118 | continue |
||
1119 | |||
1120 | knot_name = match[0] |
||
1121 | |||
1122 | log.debug(f'Loading {knot_name} from {f}') |
||
1123 | |||
1124 | knot_data = pd.read_csv(f, parse_dates=['date'], date_parser=pd.to_datetime) |
||
1125 | |||
1126 | dataL.append((knot_name, knot_data)) |
||
1127 | |||
1128 | mod = pd.concat(dict(dataL), ignore_index=True) |
||
1129 | |||
1130 | return mod |
||
1131 |