Total Complexity | 132 |
Total Lines | 1107 |
Duplicated Lines | 11.47 % |
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 | def KnotsId2dGUI(mod, twidth=0.75, use_arrows=False, arrow_pos=1.0): |
||
218 | """ |
||
219 | Prompt a GUI to select identified knots and alter their label, reprenting their 2D |
||
220 | spatial distribution in different times. |
||
221 | |||
222 | It can be used inside jupyter notebooks, using '%matplotlib widget' first. |
||
223 | |||
224 | Parameters: |
||
225 | ----------- |
||
226 | mod : :pd.DataFrame: |
||
227 | pandas.DataFrame containing every knot, with at least columns |
||
228 | 'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
||
229 | |||
230 | Returns: |
||
231 | -------- |
||
232 | mod : :pd.DataFrame: |
||
233 | pandas.DataFrame containing every knot with their altered labels, with |
||
234 | at least columns 'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
||
235 | """ |
||
236 | |||
237 | mod = mod.copy() |
||
238 | |||
239 | knots = dict(tuple(mod.groupby('label'))) |
||
240 | knots_names = list(knots.keys()) |
||
241 | knots_values = list(knots.values()) |
||
242 | knots_jyears = {k:Time(knots[k]['date'].to_numpy()).jyear for k in knots} |
||
243 | knots_X = {k:knots[k]['X'].to_numpy() for k in knots} |
||
244 | knots_Y = {k:knots[k]['Y'].to_numpy() for k in knots} |
||
245 | |||
246 | |||
247 | from matplotlib.widgets import Slider, Button, TextBox, RectangleSelector |
||
248 | |||
249 | out = get_output() |
||
250 | |||
251 | fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8,8)) |
||
252 | |||
253 | lineas = list() |
||
254 | flechas = list() |
||
255 | textos = list() |
||
256 | |||
257 | def draw_all(val=2008): |
||
258 | nonlocal lineas, flechas, textos |
||
259 | |||
260 | # instead of clearing the whole axis, remove artists |
||
261 | for linea in lineas: |
||
262 | if linea is not None: |
||
263 | linea.remove() |
||
264 | for texto in textos: |
||
265 | if texto is not None: |
||
266 | texto.remove() |
||
267 | for flecha in flechas: |
||
268 | if flecha is not None: |
||
269 | flecha.remove() |
||
270 | ax.set_prop_cycle(None) |
||
271 | |||
272 | lineas = list() |
||
273 | flechas = list() |
||
274 | textos = list() |
||
275 | |||
276 | xlim, ylim = ax.get_xlim(), ax.get_ylim() |
||
277 | |||
278 | #ax.clear() # either clear the whole axis or remove every artist separetly |
||
279 | |||
280 | for i, label in enumerate(knots_names): |
||
281 | years = knots_jyears[label] |
||
282 | idx = (val-twidth < years) & (years < val) |
||
283 | x = knots_X[label][idx] |
||
284 | y = knots_Y[label][idx] |
||
285 | |||
286 | if len(x) > 0: |
||
287 | if label == '' or label == 'None': # if it is unlabelled, do not connect it. |
||
288 | lineas.append(ax.plot(x, y, '.', linewidth=0.6, alpha=0.4, label=label, color='black')[0]) |
||
289 | else: |
||
290 | lineas.append(ax.plot(x, y, '.-', linewidth=0.6, alpha=0.4, label=label)[0]) |
||
291 | else: |
||
292 | lineas.append(None) |
||
293 | |||
294 | if use_arrows: |
||
295 | if len(x) > 1 and label != '' and label != 'None': |
||
296 | flechas.append(ax.quiver(x[:-1], |
||
297 | y[:-1], |
||
298 | arrow_pos*(x[1:] - x[:-1]), |
||
299 | arrow_pos*(y[1:] - y[:-1]), |
||
300 | scale_units='xy', angles='xy', scale=1, |
||
301 | width=0.0015, headwidth=10, headlength=10, headaxislength=6, |
||
302 | alpha=0.5, color=lineas[i].get_color())) |
||
303 | else: |
||
304 | flechas.append(None) |
||
305 | |||
306 | if len(x) > 0: |
||
307 | #textos.append(ax.annotate(label, (x[0], y[0]), (-28,-10), textcoords='offset points', color=lineas[i].get_color(), fontsize=14)) |
||
308 | textos.append(ax.text(x[-1]+0.015, y[-1]+0.015, label, {'color':lineas[i].get_color(), 'fontsize':14})) |
||
309 | else: |
||
310 | textos.append(None) |
||
311 | |||
312 | ax.set_xlim(xlim) |
||
313 | ax.set_ylim(ylim) |
||
314 | ax.set_aspect('equal') |
||
315 | |||
316 | fig.canvas.draw_idle() # if removed every artist separately instead of ax.clear() |
||
317 | |||
318 | draw_all() |
||
319 | |||
320 | def update(val): |
||
321 | nonlocal lineas, flechas, textos |
||
322 | |||
323 | for i, label in enumerate(knots_names): |
||
324 | years = knots_jyears[label] |
||
325 | idx = (val-twidth < years) & (years < val) |
||
326 | x = knots_X[label][idx] |
||
327 | y = knots_Y[label][idx] |
||
328 | |||
329 | View Code Duplication | if lineas[i] is not None: |
|
330 | if len(x) > 0: |
||
331 | lineas[i].set_xdata(x) |
||
332 | lineas[i].set_ydata(y) |
||
333 | else: |
||
334 | lineas[i].remove() |
||
335 | lineas[i] = None |
||
336 | else: |
||
337 | if len(x) > 0: |
||
338 | if label == '' or label == 'None': # if it is unlabelled, do not connect it. |
||
339 | lineas[i] = ax.plot(x, y, '.', linewidth=0.8, alpha=0.5, label=label, color='black')[0] |
||
340 | else: |
||
341 | lineas[i] = ax.plot(x, y, '.-', linewidth=0.8, alpha=0.5, label=label)[0] |
||
342 | |||
343 | |||
344 | if textos[i] is not None: |
||
345 | if len(x) > 0: |
||
346 | textos[i].set_position((x[-1]+0.015, y[-1]+0.015)) |
||
347 | textos[i].set_text(label) |
||
348 | else: |
||
349 | textos[i].remove() |
||
350 | textos[i] = None |
||
351 | #textos[i].set_position((10, 10)) |
||
352 | else: |
||
353 | if len(x) > 0: |
||
354 | textos[i] = ax.text(x[-1]+0.02, y[-1]+0.02, label, {'color':lineas[i].get_color(), 'fontsize':14}) |
||
355 | |||
356 | if use_arrows: |
||
357 | if flechas[i] is not None: |
||
358 | flechas[i].remove() |
||
359 | flechas[i] = None |
||
360 | if len(x) > 1 and label != '' and label != 'None': |
||
361 | flechas[i] = ax.quiver(x[:-1], |
||
362 | y[:-1], |
||
363 | arrow_pos*(x[1:] - x[:-1]), |
||
364 | arrow_pos*(y[1:] - y[:-1]), |
||
365 | scale_units='xy', angles='xy', scale=1, |
||
366 | width=0.0015, headwidth=10, headlength=10, headaxislength=6, |
||
367 | alpha=0.5, color=lineas[i].get_color()) |
||
368 | |||
369 | fig.canvas.draw_idle() |
||
370 | |||
371 | |||
372 | |||
373 | selected_knot = None |
||
374 | selected_ind = None |
||
375 | selected_x = None |
||
376 | selected_y = None |
||
377 | |||
378 | |||
379 | View Code Duplication | def submit_textbox(text): |
|
380 | nonlocal mod, knots, knots_names, knots_values, knots_jyears, knots_X, knots_Y |
||
381 | |||
382 | log.debug('Submited with:') |
||
383 | log.debug(f' selected_knot {selected_knot}') |
||
384 | log.debug(f' selected_ind {selected_ind}') |
||
385 | log.debug(f' selected_x {selected_x}') |
||
386 | log.debug(f' selected_y {selected_y}') |
||
387 | |||
388 | if selected_knot is not None: |
||
389 | mod.loc[selected_ind, 'label'] = text.upper().strip(' ') |
||
390 | |||
391 | knots = dict(tuple(mod.groupby('label'))) |
||
392 | knots_names = list(knots.keys()) |
||
393 | knots_values = list(knots.values()) |
||
394 | knots_jyears = {k:Time(knots[k]['date'].to_numpy()).jyear for k in knots} |
||
395 | knots_X = {k:knots[k]['X'].to_numpy() for k in knots} |
||
396 | knots_Y = {k:knots[k]['Y'].to_numpy() for k in knots} |
||
397 | |||
398 | log.debug(f"Updated index {selected_ind} to {text.upper()}") |
||
399 | else: |
||
400 | pass |
||
401 | |||
402 | draw_all(slider_date.val) |
||
403 | |||
404 | |||
405 | def line_select_callback(eclick, erelease): |
||
406 | nonlocal selected_knot,selected_x, selected_y, selected_ind |
||
407 | |||
408 | # 1 eclick and erelease are the press and release events |
||
409 | x1, y1 = eclick.xdata, eclick.ydata |
||
410 | x2, y2 = erelease.xdata, erelease.ydata |
||
411 | log.debug("GUI: (%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2)) |
||
412 | log.debug("GUI: The button you used were: %s %s" % (eclick.button, erelease.button)) |
||
413 | |||
414 | selected_knot = None |
||
415 | selected_x = None |
||
416 | selected_y = None |
||
417 | selected_ind = None |
||
418 | |||
419 | for i, label in enumerate(knots_names): |
||
420 | years = knots_jyears[label] |
||
421 | idx = (slider_date.val-twidth < years) & (years < slider_date.val) |
||
422 | |||
423 | if np.sum(idx) == 0: |
||
424 | continue # we did not select any component from this component, next one |
||
425 | |||
426 | x = np.array(knots_X[label]) |
||
427 | y = np.array(knots_Y[label]) |
||
428 | |||
429 | # get points iside current rectangle for current date |
||
430 | rect_idx = (x1 < x) & ( x < x2) & (y1 < y) & ( y < y2) & idx |
||
431 | |||
432 | View Code Duplication | if np.sum(rect_idx) > 0: |
|
433 | textbox.set_val(label) |
||
434 | |||
435 | selected_knot = label |
||
436 | selected_x = x[rect_idx].ravel() |
||
437 | selected_y = y[rect_idx].ravel() |
||
438 | selected_ind = knots[label].index[rect_idx] |
||
439 | |||
440 | log.debug(f'Selected {label} points rect_idx {rect_idx} x {x[rect_idx]}, y {y[rect_idx]} with indices {selected_ind}') |
||
441 | |||
442 | textbox.begin_typing(None) |
||
443 | break # if we find selected components in this epoch, continue with renaming |
||
444 | else: |
||
445 | pass |
||
446 | |||
447 | # print epoch of selected points: |
||
448 | if selected_knot is not None: |
||
449 | out.clear_output() |
||
450 | out.append_stdout('Selected:\n') |
||
451 | for idx in selected_ind: |
||
452 | #print(f"Selected {mod.loc[idx, 'label']} {mod.loc[idx, 'date']}") |
||
453 | out.append_stdout(f" -> {mod.loc[idx, 'label']} {mod.loc[idx, 'date'].strftime('%Y-%m-%d')}\n") |
||
454 | |||
455 | update(slider_date.val) |
||
456 | |||
457 | |||
458 | View Code Duplication | def toggle_selector(event): |
|
459 | log.debug('GUI: Key pressed.') |
||
460 | if event.key in ['Q', 'q'] and toggle_selector.RS.active: |
||
461 | log.debug('Selector deactivated.') |
||
462 | toggle_selector.RS.set_active(False) |
||
463 | if event.key in ['S', 's'] and not toggle_selector.RS.active: |
||
464 | log.debug('Selector activated.') |
||
465 | toggle_selector.RS.set_active(True) |
||
466 | if event.key in ['R', 'r']: |
||
467 | log.debug('Selector deactivated.') |
||
468 | toggle_selector.RS.set_active(False) |
||
469 | textbox.begin_typing(None) |
||
470 | #textbox.set_val('') |
||
471 | if event.key in ['E', 'e']: |
||
472 | out.append_stdout('-> Close\n') |
||
473 | plt.close() |
||
474 | |||
475 | |||
476 | toggle_selector.RS = RectangleSelector(ax, line_select_callback, |
||
477 | drawtype='box', useblit=True, |
||
478 | button=[1, 3], # don't use middle button |
||
479 | minspanx=0, minspany=0, |
||
480 | spancoords='data', |
||
481 | interactive=False) |
||
482 | |||
483 | |||
484 | #plt.connect('key_press_event', toggle_selector) |
||
485 | fig.canvas.mpl_connect('key_press_event', toggle_selector) |
||
486 | |||
487 | |||
488 | |||
489 | from mpl_toolkits.axes_grid1 import make_axes_locatable |
||
490 | |||
491 | divider_slider = make_axes_locatable(ax) |
||
492 | slider_ax = divider_slider.append_axes("top", size="3%", pad="4%") |
||
493 | slider_date = Slider(ax=slider_ax, label="Date", valmin=2007, valmax=2020, valinit=2008, valstep=0.2, orientation="horizontal") |
||
494 | slider_date.on_changed(update) |
||
495 | |||
496 | #divider_textbox = make_axes_locatable(ax) |
||
497 | #textbox_ax = divider_textbox.append_axes("bottom", size="3%", pad="4%") |
||
498 | textbox_ax = fig.add_axes([0.3,0.015,0.5,0.05]) |
||
499 | textbox = TextBox(textbox_ax, 'Knot name:', initial='None') |
||
500 | textbox.on_submit(submit_textbox) |
||
501 | |||
502 | |||
503 | |||
504 | ax.set_xlim([-1.0, +1.0]) |
||
505 | ax.set_ylim([-1.0, +1.0]) |
||
506 | ax.set_aspect('equal') |
||
507 | |||
508 | fig.suptitle('S to select, R to rename, Q to deactivate selector, E to exit') |
||
509 | |||
510 | print('Usage:', |
||
511 | '-> S to select, R to rename, Q to deactivate selector, E to exit', |
||
512 | '-> To delete the knot, name it with a whitespace', |
||
513 | '-> You can select points from one component at a time', |
||
514 | '-> If you use the zoom or movement tools, remember to unselect them', |
||
515 | sep='\n') |
||
516 | |||
517 | plt.show() |
||
518 | |||
519 | out.display_output() |
||
520 | |||
521 | return mod |
||
522 | |||
523 | |||
524 | |||
525 | |||
526 | |||
527 | |||
528 | def KnotsIdGUI(mod): |
||
529 | """ |
||
530 | Prompt a GUI to select identified knots and alter their label, reprenting their |
||
531 | time evolution. |
||
532 | |||
533 | It can be used inside jupyter notebooks, using '%matplotlib widget' first. |
||
534 | |||
535 | Parameters: |
||
536 | ----------- |
||
537 | mod : :pd.DataFrame: |
||
538 | pandas.DataFrame containing every knot, with at least columns |
||
539 | 'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
||
540 | |||
541 | Returns: |
||
542 | -------- |
||
543 | mod : :pd.DataFrame: |
||
544 | pandas.DataFrame containing every knot with their altered labels, with |
||
545 | at least columns 'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
||
546 | """ |
||
547 | |||
548 | mod = mod.copy() |
||
549 | |||
550 | knots = dict(tuple(mod.groupby('label'))) |
||
551 | knots_names = list(knots.keys()) |
||
552 | knots_values = list(knots.values()) |
||
553 | knots_jyears = {k:Time(knots[k]['date'].to_numpy()).jyear for k in knots} |
||
554 | knots_dates = {k:knots[k]['date'].to_numpy() for k in knots} |
||
555 | knots_fluxes = {k:knots[k]['Flux (Jy)'].to_numpy() for k in knots} |
||
556 | |||
557 | from matplotlib.widgets import TextBox, RectangleSelector |
||
558 | |||
559 | out = get_output() |
||
560 | |||
561 | fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8,5)) |
||
562 | |||
563 | lineas = list() |
||
564 | textos = list() |
||
565 | |||
566 | def draw_all(): |
||
567 | nonlocal lineas, textos |
||
568 | |||
569 | for linea in lineas: |
||
570 | if linea is not None: |
||
571 | linea.remove() |
||
572 | for texto in textos: |
||
573 | if texto is not None: |
||
574 | texto.remove() |
||
575 | |||
576 | ax.set_prop_cycle(None) |
||
577 | |||
578 | lineas = list() |
||
579 | textos = list() |
||
580 | |||
581 | #ax.clear() # either clear the whole axis or remove every artist separetly |
||
582 | |||
583 | for i, label in enumerate(knots_names): |
||
584 | x, y = knots_jyears[label], knots_fluxes[label] |
||
585 | |||
586 | if len(x) > 0: |
||
587 | if label == '' or label == 'None': # if it is unlabelled, do not connect it. |
||
588 | lineas.append(ax.plot(x, y, '.', linewidth=0.8, alpha=0.5, label=label, color='black')[0]) |
||
589 | else: |
||
590 | lineas.append(ax.plot(x, y, '.-', linewidth=0.8, alpha=0.5, label=label)[0]) |
||
591 | else: |
||
592 | lineas.append(None) |
||
593 | |||
594 | if len(x) > 0: |
||
595 | #textos.append(ax.annotate(label, (x[0], y[0]), (-28,-10), textcoords='offset points', color=lineas[i].get_color(), fontsize=14)) |
||
596 | textos.append(ax.text(x[0], y[0], label, {'color':lineas[i].get_color(), 'fontsize':14})) |
||
597 | else: |
||
598 | textos.append(None) |
||
599 | |||
600 | fig.canvas.draw_idle() # if removed every artist separately instead of ax.clear() |
||
601 | |||
602 | |||
603 | def update(): |
||
604 | nonlocal lineas, textos |
||
605 | |||
606 | for i, label in enumerate(knots_names): |
||
607 | x, y = knots_jyears[label], knots_fluxes[label] |
||
608 | |||
609 | View Code Duplication | if lineas[i] is not None: |
|
610 | if len(x) > 0: |
||
611 | lineas[i].set_xdata(x) |
||
612 | lineas[i].set_ydata(y) |
||
613 | else: |
||
614 | lineas[i].remove() |
||
615 | lineas[i] = None |
||
616 | else: |
||
617 | if len(x) > 0: |
||
618 | if label == '' or label == 'None': # if it is unlabelled, do not connect it. |
||
619 | lineas[i] = ax.plot(x, y, '.', linewidth=0.8, alpha=0.5, label=label, color='black')[0] |
||
620 | else: |
||
621 | lineas[i] = ax.plot(x, y, '.-', linewidth=0.8, alpha=0.5, label=label)[0] |
||
622 | |||
623 | if textos[i] is not None: |
||
624 | if len(x) > 0: |
||
625 | textos[i].set_position((x[0], y[0])) |
||
626 | textos[i].set_text(label) |
||
627 | else: |
||
628 | textos[i].remove() |
||
629 | textos[i] = None |
||
630 | #textos[i].set_position((10, 10)) |
||
631 | else: |
||
632 | if len(x) > 0: |
||
633 | #textos[i] = ax.annotate(label, (x[0], y[0]), (-24,-10), textcoords='offset points', color=lineas[i].get_color(), fontsize=15) |
||
634 | textos[i] = ax.text(x[0], y[0], label, {'color':lineas[i].get_color(), 'fontsize':14}) |
||
635 | |||
636 | fig.canvas.draw_idle() |
||
637 | |||
638 | |||
639 | selected_knot = None |
||
640 | selected_ind = None |
||
641 | selected_date = None |
||
642 | selected_flux = None |
||
643 | |||
644 | View Code Duplication | def submit_textbox(text): |
|
645 | nonlocal mod, knots, knots_names, knots_values, knots_jyears, knots_dates, knots_fluxes |
||
646 | |||
647 | log.debug('Submited with:') |
||
648 | log.debug(f' selected_knot {selected_knot}') |
||
649 | log.debug(f' selected_ind {selected_ind}') |
||
650 | log.debug(f' selected_flux {selected_flux}') |
||
651 | log.debug(f' selected_date {selected_date}') |
||
652 | |||
653 | if selected_knot is not None: |
||
654 | mod.loc[selected_ind, 'label'] = text.upper().strip(' ') |
||
655 | |||
656 | knots = dict(tuple(mod.groupby('label'))) |
||
657 | knots_names = list(knots.keys()) |
||
658 | knots_values = list(knots.values()) |
||
659 | knots_jyears = {k:Time(knots[k]['date'].to_numpy()).jyear for k in knots} |
||
660 | knots_dates = {k:knots[k]['date'].to_numpy() for k in knots} |
||
661 | knots_fluxes = {k:knots[k]['Flux (Jy)'].to_numpy() for k in knots} |
||
662 | |||
663 | print(f"Updated index {selected_ind} to {text.upper()}") |
||
664 | else: |
||
665 | pass |
||
666 | |||
667 | draw_all() |
||
668 | |||
669 | def line_select_callback(eclick, erelease): |
||
670 | nonlocal selected_knot,selected_date, selected_flux, selected_ind |
||
671 | |||
672 | # 1 eclick and erelease are the press and release events |
||
673 | x1, y1 = eclick.xdata, eclick.ydata |
||
674 | x2, y2 = erelease.xdata, erelease.ydata |
||
675 | log.debug("GUI: (%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2)) |
||
676 | log.debug("GUI: The button you used were: %s %s" % (eclick.button, erelease.button)) |
||
677 | |||
678 | selected_knot = None |
||
679 | selected_date = None |
||
680 | selected_flux = None |
||
681 | selected_ind = None |
||
682 | |||
683 | for i, label in enumerate(knots_names): |
||
684 | years = knots_jyears[label] |
||
685 | fluxes = knots_fluxes[label] |
||
686 | |||
687 | # get points iside current rectangle for current date |
||
688 | rect_idx = (x1 < years) & ( years < x2) & (y1 < fluxes) & ( fluxes < y2) |
||
689 | |||
690 | if np.sum(rect_idx) == 0: |
||
691 | continue # we did not select any component from this component, next one |
||
692 | |||
693 | x = np.array(years) |
||
694 | y = np.array(fluxes) |
||
695 | |||
696 | View Code Duplication | if np.sum(rect_idx) > 0: |
|
697 | textbox.set_val(label) |
||
698 | selected_knot = label |
||
699 | selected_x = x[rect_idx].ravel() |
||
700 | selected_y = y[rect_idx].ravel() |
||
701 | selected_ind = knots[label].index[rect_idx] |
||
702 | log.debug(f'Selected {label} points rect_idx {rect_idx} date {x[rect_idx]}, flux {y[rect_idx]} with indices {selected_ind}') |
||
703 | break # if we find selected components in this epoch, continue with renaming |
||
704 | else: |
||
705 | pass |
||
706 | |||
707 | # print epoch of selected points: |
||
708 | if selected_knot is not None: |
||
709 | out.clear_output() |
||
710 | out.append_stdout('Selected:\n') |
||
711 | for idx in selected_ind: |
||
712 | #print(f"Selected {mod.loc[idx, 'label']} {mod.loc[idx, 'date']}") |
||
713 | out.append_stdout(f" -> {mod.loc[idx, 'label']} {mod.loc[idx, 'date'].strftime('%Y-%m-%d')}\n") |
||
714 | |||
715 | update() |
||
716 | |||
717 | |||
718 | View Code Duplication | def toggle_selector(event): |
|
719 | log.debug('GUI: Key pressed.') |
||
720 | if event.key in ['Q', 'q'] and toggle_selector.RS.active: |
||
721 | log.debug('Selector deactivated.') |
||
722 | toggle_selector.RS.set_active(False) |
||
723 | if event.key in ['S', 's'] and not toggle_selector.RS.active: |
||
724 | log.debug('Selector activated.') |
||
725 | toggle_selector.RS.set_active(True) |
||
726 | if event.key in ['R', 'r']: |
||
727 | log.debug('Selector deactivated.') |
||
728 | toggle_selector.RS.set_active(False) |
||
729 | textbox.begin_typing(None) |
||
730 | #textbox.set_val('') |
||
731 | if event.key in ['E', 'e']: |
||
732 | out.append_stdout('-> Close\n') |
||
733 | plt.close() |
||
734 | |||
735 | |||
736 | toggle_selector.RS = RectangleSelector(ax, line_select_callback, |
||
737 | drawtype='box', useblit=True, |
||
738 | button=[1, 3], # don't use middle button |
||
739 | minspanx=0, minspany=0, |
||
740 | spancoords='data', |
||
741 | interactive=False) |
||
742 | |||
743 | |||
744 | fig.canvas.mpl_connect('key_press_event', toggle_selector) |
||
745 | |||
746 | from mpl_toolkits.axes_grid1 import make_axes_locatable |
||
747 | |||
748 | divider_textbox = make_axes_locatable(ax) |
||
749 | textbox_ax = divider_textbox.append_axes("bottom", size="10%", pad="15%") |
||
750 | #textbox_ax = fig.add_axes([0.3,0,0.5,0.05]) |
||
751 | textbox = TextBox(textbox_ax, 'Knot name:', initial='None') |
||
752 | textbox.on_submit(submit_textbox) |
||
753 | |||
754 | draw_all() |
||
755 | |||
756 | #ax.autoscale() |
||
757 | ax.set_xlabel('date (year)') |
||
758 | ax.set_ylabel('Flux (Jy)') |
||
759 | ax.set_title('Flux from each component') |
||
760 | |||
761 | xlims = Time(np.amin(mod['date'])).jyear, Time(np.amax(mod['date'])).jyear |
||
762 | ax.set_xlim((xlims[0]-0.03*np.abs(xlims[1]-xlims[0]), xlims[1]+0.03*np.abs(xlims[1]-xlims[0]))) |
||
763 | |||
764 | fig.suptitle('S to select, R to rename, Q to deactivate selector, E to exit') |
||
765 | |||
766 | print('Usage:', |
||
767 | '-> S to select, R to rename, Q to deactivate selector, E to exit', |
||
768 | '-> To delete the knot, name it with a whitespace', |
||
769 | '-> You can select points from one component at a time', |
||
770 | '-> If you use the zoom or movement tools, remember to unselect them', |
||
771 | sep='\n') |
||
772 | |||
773 | plt.show() |
||
774 | |||
775 | out.display_output() |
||
776 | |||
777 | return mod |
||
778 | |||
779 | |||
780 | |||
781 | def KnotsIdReadMod_from_diffmap(path=None): |
||
782 | """ |
||
783 | Read *_mod.mod files as printed by diffmap, return a dataframe containing all information ready |
||
784 | to be worked on for labelling and to be used with these GUIs. |
||
785 | |||
786 | Parameters: |
||
787 | ----------- |
||
788 | path : :str: |
||
789 | string indicating the path to the mod files to be used, their names must end in the format |
||
790 | '%Y-%m-%d_mod.mod', for example, path = 'vlbi/ftree/*/*_mod.mod'. |
||
791 | |||
792 | Returns: |
||
793 | ----------- |
||
794 | mod : :pd.DataFrame: |
||
795 | pandas.DataFrame containing every knot, with at least columns |
||
796 | 'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
||
797 | """ |
||
798 | |||
799 | if path is None: |
||
800 | log.error('Path not specified') |
||
801 | raise Exception('Path not specified') |
||
802 | |||
803 | mod_dates_str = list() |
||
804 | mod_dates = list() |
||
805 | mod_data = list() |
||
806 | |||
807 | for f in glob.glob(f'{path}'): |
||
808 | match = re.findall(r'([0-9]{4}-[0-9]{2}-[0-9]{2})_mod.mod', f) |
||
809 | if not len(match) > 0: |
||
810 | continue |
||
811 | |||
812 | date_str = match[0] |
||
813 | date = datetime.strptime(date_str, '%Y-%m-%d') |
||
814 | |||
815 | mod_dates_str.append(date_str) |
||
816 | mod_dates.append(date) |
||
817 | 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'])) |
||
818 | |||
819 | # sort by date |
||
820 | idx = np.argsort(mod_dates) |
||
821 | mod_dates_str = list(np.array(mod_dates_str, dtype=object)[idx]) |
||
822 | mod_dates = list(np.array(mod_dates, dtype=object)[idx]) |
||
823 | mod_data = list(np.array(mod_data, dtype=object)[idx]) |
||
824 | |||
825 | # fix stupid 'v' in columns, insert a label field, add X, Y columns |
||
826 | for i in range(len(mod_dates)): |
||
827 | mod_data[i].insert(0, 'label', value=None) |
||
828 | mod_data[i].insert(1, 'date', value=mod_dates[i]) |
||
829 | |||
830 | mod_data[i]['Flux (Jy)'] = mod_data[i]['Flux (Jy)'].str.strip('v').astype(float) |
||
831 | mod_data[i]['Radius (mas)'] = mod_data[i]['Radius (mas)'].str.strip('v').astype(float) |
||
832 | mod_data[i]['Theta (deg)'] = mod_data[i]['Theta (deg)'].str.strip('v').astype(float) |
||
833 | |||
834 | mod_data[i].insert(5, 'X', mod_data[i]['Radius (mas)']*np.cos(np.pi/180*(mod_data[i]['Theta (deg)']-90))) |
||
835 | mod_data[i].insert(6, 'Y', mod_data[i]['Radius (mas)']*np.sin(np.pi/180*(mod_data[i]['Theta (deg)']-90))) |
||
836 | |||
837 | |||
838 | mod = pd.concat(mod_data, ignore_index=True) |
||
839 | |||
840 | return mod |
||
841 | |||
842 | |||
843 | |||
844 | def KnotsIdSaveMod(mod, path=None, rewrite=False, drop_blanks=False): |
||
845 | """ |
||
846 | Save Knots data to *_mod.mod files ready to be re-proccessed with diffmap. |
||
847 | |||
848 | Actually only useful to remove stupid knots and fit it again with diffmap. |
||
849 | |||
850 | Parameters: |
||
851 | ----------- |
||
852 | mod : :pd.DataFrame: |
||
853 | pandas.DataFrame containing every knot |
||
854 | path: :str: |
||
855 | string containing the path to which the files are to be saved, eg: |
||
856 | path = 'my_knots/' |
||
857 | rewrite: :bool: |
||
858 | If rewrite is True, path is interpreted as a regex for which files |
||
859 | to overwrite. |
||
860 | drop_blanks: :bool: |
||
861 | If True, knots without label (or whose label is a whitspace) are dropped. |
||
862 | If False (default), they are saved without label. |
||
863 | |||
864 | Returns: |
||
865 | -------- |
||
866 | None |
||
867 | """ |
||
868 | |||
869 | if path is None: |
||
870 | log.error('Path not specified') |
||
871 | raise Exception('Path not specified') |
||
872 | |||
873 | |||
874 | mod = mod.copy() |
||
875 | |||
876 | |||
877 | if drop_blanks: |
||
878 | mod = mod.drop(mod[mod['label'].str.strip('') == ''].index) |
||
879 | |||
880 | |||
881 | mod_dict = dict(list(mod.groupby('date'))) |
||
882 | |||
883 | |||
884 | if rewrite: |
||
885 | file_list = list() |
||
886 | for f in glob.glob(f'{path}'): |
||
887 | match = re.findall(r'([0-9]{4}-[0-9]{2}-[0-9]{2})_mod.mod', f) |
||
888 | |||
889 | if not len(match) > 0: |
||
890 | continue |
||
891 | |||
892 | date = datetime.strptime(match[0], '%Y-%m-%d') |
||
893 | |||
894 | if date not in mod_dict: |
||
895 | raise Exception('Specified regex includes files for which there are no knots') |
||
896 | |||
897 | file_list.append(f) |
||
898 | |||
899 | if len(file_list) != len(mod_dict): |
||
900 | raise Exception(f'Specified files by path regex does not match the number of epochs ({len(file_list)} vs {len(mod_dict)})') |
||
901 | else: |
||
902 | file_list = list() |
||
903 | for date in mod_dict.keys(): |
||
904 | file_list.append(f"{path}/{date.strftime('%Y-%m-%d')}_mod.mod") |
||
905 | |||
906 | |||
907 | for fname, (date, data) in zip(file_list, mod_dict.items()): |
||
908 | data = data.copy() |
||
909 | |||
910 | #data = data.drop(columns=['label']) |
||
911 | #data = data.drop(columns=['X']) |
||
912 | #data = data.drop(columns=['Y']) |
||
913 | |||
914 | data['Flux (Jy)'] = data['Flux (Jy)'].astype(str) + 'v' |
||
915 | data['Radius (mas)'] = data['Radius (mas)'].astype(str) + 'v' |
||
916 | data['Theta (deg)'] = data['Theta (deg)'].astype(str) + 'v' |
||
917 | |||
918 | ## add previous labels in comments (so diffmap does not get stuck with it) |
||
919 | data['label'] = '!' + data['label'] |
||
920 | |||
921 | with open(fname, 'w') as f: |
||
922 | f.write("! Generated by mutis\n" |
||
923 | "! Columns\n" |
||
924 | "! 'Flux (Jy)', 'Radius (mas)', 'Theta (deg)', 'Major FWHM (mas)', 'Axial ratio', 'Phi (deg)', 'T', 'Freq (Hz)', 'SpecIndex', '!label'\n") |
||
925 | data.to_csv(f, |
||
926 | columns=['Flux (Jy)', 'Radius (mas)', 'Theta (deg)', 'Major FWHM (mas)', 'Axial ratio', 'Phi (deg)', 'T', 'Freq (Hz)', 'SpecIndex', 'label'], |
||
927 | index=False, sep=' ', header=None) |
||
928 | |||
929 | |||
930 | |||
931 | def KnotsIdReadCSV(path=None): |
||
932 | """ |
||
933 | Read Knots data to .csv files (as done by Svetlana? ##) |
||
934 | |||
935 | Each knot label has its own {label}.csv. To be compatible with Svetlana's format, |
||
936 | columns should be modified to: |
||
937 | 'Date' (jyear), 'MJD', 'X(mas)', 'Y(mas)', 'Flux(Jy)' |
||
938 | These columns are derived from the ones in `mod`, old ones are removed. |
||
939 | |||
940 | Parameters: |
||
941 | ----------- |
||
942 | path: :str: |
||
943 | string containing the path to read files from eg: |
||
944 | path = 'myknows/*.csv' |
||
945 | Returns: |
||
946 | -------- |
||
947 | mod : :pd.DataFrame: |
||
948 | pandas.DataFrame containing every knot, with at least columns |
||
949 | 'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
||
950 | """ |
||
951 | |||
952 | if path is None: |
||
953 | log.error('Path not specified') |
||
954 | raise Exception('Path not specified') |
||
955 | |||
956 | dataL = list() |
||
957 | |||
958 | for f in glob.glob(f'{path}'): |
||
959 | match = re.findall(r'/(.*).csv', f) |
||
960 | |||
961 | if not len(match) > 0: |
||
962 | continue |
||
963 | |||
964 | knot_name = match[0] |
||
965 | |||
966 | log.debug(f'Loading {knot_name} from {f}') |
||
967 | |||
968 | knot_data = pd.read_csv(f, parse_dates=['date'], date_parser=pd.to_datetime) |
||
969 | |||
970 | dataL.append((knot_name, knot_data)) |
||
971 | |||
972 | mod = pd.concat(dict(dataL), ignore_index=True) |
||
973 | |||
974 | return mod |
||
975 | |||
976 | |||
977 | |||
978 | def KnotsIdSaveCSV(mod, path=None): |
||
979 | """ |
||
980 | Save Knots data to .csv files (as done by Svetlana? ##) |
||
981 | |||
982 | Each knot label has its own {label}.csv. To be compatible with Svetlana's format, |
||
983 | columns should be modified to: |
||
984 | 'Date' (jyear), 'MJD', 'X(mas)', 'Y(mas)', 'Flux(Jy)' |
||
985 | These columns are derived from the ones in `mod`, old ones are removed. |
||
986 | |||
987 | Parameters: |
||
988 | ----------- |
||
989 | mod : :pd.DataFrame: |
||
990 | pandas.DataFrame containing every knot, with at least columns |
||
991 | 'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
||
992 | path: :str: |
||
993 | string containing the path to which the files are to be saved, eg: |
||
994 | path = 'my_knots/' |
||
995 | |||
996 | Returns: |
||
997 | -------- |
||
998 | None |
||
999 | """ |
||
1000 | |||
1001 | if path is None: |
||
1002 | log.error('Path not specified') |
||
1003 | raise Exception('Path not specified') |
||
1004 | |||
1005 | |||
1006 | mod = mod.copy() |
||
1007 | |||
1008 | |||
1009 | mod_dict = dict(list(mod.groupby('label'))) |
||
1010 | |||
1011 | for label, data in mod_dict.items(): |
||
1012 | data = data.copy() |
||
1013 | |||
1014 | #data.insert(0, 'Date', Time(data['date']).jyear) |
||
1015 | #data.insert(1, 'MJD', Time(data['date']).mjd) |
||
1016 | #data = data.rename(columns={'X': 'X (mas)', 'Y': 'Y (mas)'}) |
||
1017 | #data = data.drop(columns=['date']) |
||
1018 | #data = data.drop(columns=['label']) |
||
1019 | #if 'Radius (mas)' in data.columns: |
||
1020 | # data = data.rename(columns={'Radius (mas)':'R(mas)'}) |
||
1021 | #data.columns = data.columns.str.replace(' ', '') |
||
1022 | |||
1023 | data.to_csv(f'{path}/{label}.csv', index=False) |
||
1024 | |||
1025 | |||
1026 | |||
1027 | def KnotsIdReadMod(path=None): |
||
1028 | """ |
||
1029 | Read *_mod.mod files as printed by diffmap, return a dataframe containing all information ready |
||
1030 | to be worked on for labelling and to be used with these GUIs. |
||
1031 | |||
1032 | It detects whether the file was generated by mutis. If it was then |
||
1033 | it searchs for a hidden label column to load previous label names (unnamed labels |
||
1034 | |||
1035 | Parameters: |
||
1036 | ----------- |
||
1037 | path : :str: |
||
1038 | string indicating the path to the mod files to be used, their names must end in the format |
||
1039 | '%Y-%m-%d_mod.mod', for example, path = 'vlbi/ftree/*/*_mod.mod'. |
||
1040 | |||
1041 | Returns: |
||
1042 | ----------- |
||
1043 | mod : :pd.DataFrame: |
||
1044 | pandas.DataFrame containing every knot, with at least columns |
||
1045 | 'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
||
1046 | """ |
||
1047 | |||
1048 | if path is None: |
||
1049 | log.error('Path not specified') |
||
1050 | raise Exception('Path not specified') |
||
1051 | |||
1052 | mod_dates_str = list() |
||
1053 | mod_dates = list() |
||
1054 | mod_data = list() |
||
1055 | |||
1056 | for f in glob.glob(f'{path}'): |
||
1057 | match = re.findall(r'([0-9]{4}-[0-9]{2}-[0-9]{2})_mod.mod', f) |
||
1058 | if not len(match) > 0: |
||
1059 | continue |
||
1060 | |||
1061 | date_str = match[0] |
||
1062 | date = datetime.strptime(date_str, '%Y-%m-%d') |
||
1063 | |||
1064 | mod_dates_str.append(date_str) |
||
1065 | mod_dates.append(date) |
||
1066 | |||
1067 | with open(f) as fobj: |
||
1068 | firstline = fobj.readline() |
||
1069 | if firstline == '! Generated by mutis\n': |
||
1070 | generated_by_mutis = True |
||
1071 | else: |
||
1072 | generated_by_mutis = False |
||
1073 | |||
1074 | if generated_by_mutis: |
||
1075 | data = pd.read_csv(f, sep='\s+', |
||
1076 | skiprows=3, |
||
1077 | #converters={'label':(lambda l: '' if l == '' else l)}, |
||
1078 | dtype={'label':str}, |
||
1079 | names=['Flux (Jy)', 'Radius (mas)', 'Theta (deg)', 'Major FWHM (mas)', 'Axial ratio', 'Phi (deg)', 'T', 'Freq (Hz)', 'SpecIndex', |
||
1080 | 'label']) |
||
1081 | data['label'] = data['label'].str.strip('!') |
||
1082 | else: |
||
1083 | 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']) |
||
1084 | #data.insert(0, 'label', value=None) |
||
1085 | data.insert(0, 'label', value='') |
||
1086 | |||
1087 | data.insert(1, 'date', value=date) |
||
1088 | |||
1089 | data['Flux (Jy)'] = data['Flux (Jy)'].str.strip('v').astype(float) |
||
1090 | data['Radius (mas)'] = data['Radius (mas)'].str.strip('v').astype(float) |
||
1091 | data['Theta (deg)'] = data['Theta (deg)'].str.strip('v').astype(float) |
||
1092 | |||
1093 | data.insert(5, 'X', data['Radius (mas)']*np.cos(np.pi/180*(data['Theta (deg)']-90))) |
||
1094 | data.insert(6, 'Y', data['Radius (mas)']*np.sin(np.pi/180*(data['Theta (deg)']-90))) |
||
1095 | |||
1096 | mod_data.append(data) |
||
1097 | |||
1098 | # sort by date |
||
1099 | idx = np.argsort(mod_dates) |
||
1100 | mod_dates_str = list(np.array(mod_dates_str, dtype=object)[idx]) |
||
1101 | mod_dates = list(np.array(mod_dates, dtype=object)[idx]) |
||
1102 | mod_data = list(np.array(mod_data, dtype=object)[idx]) |
||
1103 | |||
1104 | mod = pd.concat(mod_data, ignore_index=True) |
||
1105 | |||
1106 | return mod |
||