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
|
|
|
|
24
|
|
|
def pol_angle_reshape(s): |
|
|
|
|
25
|
|
|
""" |
26
|
|
|
Reshape a signal as a polarization angle: shifting by 180 degrees in a way it varies as smoothly as possible. |
|
|
|
|
27
|
|
|
""" |
28
|
|
|
|
|
|
|
|
29
|
|
|
s = np.array(s) |
30
|
|
|
s = np.mod(s,180) # s % 180 |
|
|
|
|
31
|
|
|
|
32
|
|
|
sn = np.empty(s.shape) |
|
|
|
|
33
|
|
|
for i in range(1,len(s)): |
|
|
|
|
34
|
|
|
#if t[i]-t[i-1] < 35: |
35
|
|
|
d = 181 |
|
|
|
|
36
|
|
|
n = 0 |
|
|
|
|
37
|
|
|
for m in range(0,100): |
|
|
|
|
38
|
|
|
m2 = (-1)**(m+1)*np.floor((m+1)/2) |
|
|
|
|
39
|
|
|
if np.abs(s[i]+180*m2-sn[i-1]) < d: |
40
|
|
|
d = np.abs(s[i]+180*m2-sn[i-1]) |
|
|
|
|
41
|
|
|
n = m2 |
|
|
|
|
42
|
|
|
sn[i] = s[i]+180*n |
43
|
|
|
return sn |
|
|
|
|
44
|
|
|
|
45
|
|
|
|
46
|
|
|
|
47
|
|
|
|
48
|
|
|
|
49
|
|
|
|
50
|
|
|
######################################### |
51
|
|
|
################# Knots ################# |
52
|
|
|
######################################### |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
|
56
|
|
|
def KnotsIdAuto(mod): |
|
|
|
|
57
|
|
|
""" |
58
|
|
|
Identify the knots appearing in several epochs giving them names, based on their position. |
59
|
|
|
|
|
|
|
|
60
|
|
|
Parameters: |
61
|
|
|
----------- |
62
|
|
|
mod : :pd.DataFrame: |
63
|
|
|
pandas.DataFrame containing every knot, with |
|
|
|
|
64
|
|
|
at least columns 'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
65
|
|
|
|
|
|
|
|
66
|
|
|
Returns: mod |
67
|
|
|
-------- |
68
|
|
|
mod : :pd.DataFrame: |
69
|
|
|
pandas.DataFrame containing every knot with their altered labels, with |
|
|
|
|
70
|
|
|
at least columns 'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
71
|
|
|
|
|
|
|
|
72
|
|
|
""" |
73
|
|
|
|
|
|
|
|
74
|
|
|
mod_date_dict = dict(list((mod.groupby('date')))) |
75
|
|
|
|
|
|
|
|
76
|
|
|
mod_dates = list(Time(list(mod_date_dict.keys())).datetime) |
77
|
|
|
mod_dates_str = list(Time(list(mod_date_dict.keys())).strftime('%Y-%m-%d')) |
78
|
|
|
mod_data = list(mod_date_dict.values()) |
79
|
|
|
|
|
|
|
|
80
|
|
|
Bx = [[]]*len(mod_dates) |
|
|
|
|
81
|
|
|
By = [[]]*len(mod_dates) |
|
|
|
|
82
|
|
|
B = [[]]*len(mod_dates) |
|
|
|
|
83
|
|
|
|
84
|
|
|
|
85
|
|
|
thresh = 0.03 #0.062 |
86
|
|
|
|
87
|
|
|
news = 0 |
88
|
|
|
|
89
|
|
|
for i, (date, data) in enumerate(zip(mod_dates, mod_data)): |
|
|
|
|
90
|
|
|
log.debug(f'Analysing epoch i {i:3d} ({mod_dates_str[i]})') |
|
|
|
|
91
|
|
|
|
92
|
|
|
if not len(data.index) > 0: |
93
|
|
|
log.error(' -> Error, no components found') |
94
|
|
|
break |
95
|
|
|
|
96
|
|
|
log.debug(f' it has {len(data.index)} components') |
|
|
|
|
97
|
|
|
|
98
|
|
|
Bx[i] = np.ravel(data['X']) |
99
|
|
|
By[i] = np.ravel(data['Y']) |
100
|
|
|
B[i] = np.full(len(data.index), fill_value=None) |
101
|
|
|
|
102
|
|
|
# if first epoch, just give them new names...: |
103
|
|
|
if i == 0: |
104
|
|
|
log.debug(' first epoch, giving names...') |
105
|
|
|
|
106
|
|
|
for n in range(0,len(mod_data[i].index)): |
|
|
|
|
107
|
|
|
if data['Flux (Jy)'].iloc[n] < 0.001: |
108
|
|
|
log.debug(' skipping, too weak') |
109
|
|
|
break |
110
|
|
|
if n == 0: |
111
|
|
|
B[i][n] = 'A0' |
112
|
|
|
else: |
113
|
|
|
news = news + 1 |
114
|
|
|
B[i][n] = f'B{news}' |
115
|
|
|
log.debug(f' -> FOUND: {B[i]}\n') |
|
|
|
|
116
|
|
|
continue |
117
|
|
|
|
118
|
|
|
# if not first epoch...: |
119
|
|
|
|
120
|
|
|
for n in range(0,len(mod_data[i].index)): |
|
|
|
|
121
|
|
|
if data['Flux (Jy)'].iloc[n] < 0.001: |
122
|
|
|
log.debug(' skipping, too weak') |
|
|
|
|
123
|
|
|
break |
|
|
|
|
124
|
|
|
if n == 0: |
125
|
|
|
B[i][n] = 'A0' |
126
|
|
|
else: |
127
|
|
|
log.debug(f' -> id component {n}...') |
|
|
|
|
128
|
|
|
|
129
|
|
|
close = None |
130
|
|
|
|
131
|
|
|
a = 0 |
|
|
|
|
132
|
|
|
while ( i - a >= 0 and a < 4 and close is None): |
|
|
|
|
133
|
|
|
a = a + 1 |
|
|
|
|
134
|
|
|
|
135
|
|
|
if not len(Bx[i-a])>0: |
|
|
|
|
136
|
|
|
break |
137
|
|
|
|
138
|
|
|
dist = ( (Bx[i-a]-Bx[i][n]) ** 2 + (By[i-a]-By[i][n]) ** 2 ) ** 0.5 |
|
|
|
|
139
|
|
|
|
140
|
|
|
for m in range(len(dist)): |
|
|
|
|
141
|
|
|
if B[i-a][m] in B[i]: |
142
|
|
|
dist[m] = np.inf |
143
|
|
|
|
144
|
|
|
if np.amin(dist) < thresh*a**1.5: |
145
|
|
|
close = np.argmin(dist) |
146
|
|
|
|
147
|
|
|
if B[i-a][close] in B[i]: |
148
|
|
|
close = None |
|
|
|
|
149
|
|
|
|
150
|
|
|
|
151
|
|
|
if close is None: |
152
|
|
|
news = news + 1 |
153
|
|
|
B[i][n] = f'B{news}' |
154
|
|
|
log.debug(f' component {n} is new, naming it {B[i][n]}') |
|
|
|
|
155
|
|
|
else: |
156
|
|
|
log.debug(f' component {n} is close to {B[i-a][close]} of previous epoch ({a} epochs before)') |
|
|
|
|
157
|
|
|
B[i][n] = B[i-a][close] |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
log.debug(f' -> FOUND: {B[i]}\n') |
|
|
|
|
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
|
163
|
|
|
for i, (date, data) in enumerate(zip(mod_dates, mod_data)): |
164
|
|
|
data['label'] = B[i] |
165
|
|
|
|
|
|
|
|
166
|
|
|
mod = pd.concat(mod_data, ignore_index=True) |
167
|
|
|
|
|
|
|
|
168
|
|
|
return mod |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
|
172
|
|
|
def KnotsId2dGUI(mod, use_arrows=False, arrow_pos=1.0): |
|
|
|
|
173
|
|
|
""" |
174
|
|
|
Prompt a GUI to select identified knots and alter their label, reprenting their 2D |
175
|
|
|
spatial distribution in different times. |
176
|
|
|
|
|
|
|
|
177
|
|
|
It can be used inside jupyter notebooks, using '%matplotlib widget' first. |
178
|
|
|
|
|
|
|
|
179
|
|
|
Parameters: |
180
|
|
|
----------- |
181
|
|
|
mod : :pd.DataFrame: |
182
|
|
|
pandas.DataFrame containing every knot, with at least columns |
|
|
|
|
183
|
|
|
'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
184
|
|
|
|
|
|
|
|
185
|
|
|
Returns: |
186
|
|
|
-------- |
187
|
|
|
mod : :pd.DataFrame: |
188
|
|
|
pandas.DataFrame containing every knot with their altered labels, with |
|
|
|
|
189
|
|
|
at least columns 'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
190
|
|
|
""" |
191
|
|
|
|
|
|
|
|
192
|
|
|
mod = mod.copy() |
193
|
|
|
|
|
|
|
|
194
|
|
|
knots = dict(tuple(mod.groupby('label'))) |
195
|
|
|
knots_names = list(knots.keys()) |
196
|
|
|
knots_values = list(knots.values()) |
197
|
|
|
knots_jyears = {k:Time(knots[k]['date'].to_numpy()).jyear for k in knots} |
198
|
|
|
knots_X = {k:knots[k]['X'].to_numpy() for k in knots} |
|
|
|
|
199
|
|
|
knots_Y = {k:knots[k]['Y'].to_numpy() for k in knots} |
|
|
|
|
200
|
|
|
|
201
|
|
|
|
202
|
|
|
from matplotlib.widgets import Slider, Button, TextBox, RectangleSelector |
|
|
|
|
203
|
|
|
|
204
|
|
|
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8,8)) |
|
|
|
|
205
|
|
|
|
206
|
|
|
lineas = list() |
207
|
|
|
flechas = list() |
208
|
|
|
textos = list() |
209
|
|
|
|
210
|
|
|
def draw_all(val=2008): |
211
|
|
|
nonlocal lineas, flechas, textos |
|
|
|
|
212
|
|
|
|
213
|
|
|
# instead of clearing the whole axis, remove artists |
214
|
|
|
for linea in lineas: |
215
|
|
|
if linea is not None: |
216
|
|
|
linea.remove() |
217
|
|
|
for texto in textos: |
218
|
|
|
if texto is not None: |
219
|
|
|
texto.remove() |
220
|
|
|
for flecha in flechas: |
221
|
|
|
if flecha is not None: |
222
|
|
|
flecha.remove() |
223
|
|
|
ax.set_prop_cycle(None) |
224
|
|
|
|
|
|
|
|
225
|
|
|
lineas = list() |
226
|
|
|
flechas = list() |
227
|
|
|
textos = list() |
228
|
|
|
|
229
|
|
|
xlim, ylim = ax.get_xlim(), ax.get_ylim() |
230
|
|
|
|
|
|
|
|
231
|
|
|
#ax.clear() # either clear the whole axis or remove every artist separetly |
232
|
|
|
|
|
|
|
|
233
|
|
|
for i, label in enumerate(knots_names): |
234
|
|
|
years = knots_jyears[label] |
235
|
|
|
idx = (val-1.5 < years) & (years < val) |
236
|
|
|
x = knots_X[label][idx] |
|
|
|
|
237
|
|
|
y = knots_Y[label][idx] |
|
|
|
|
238
|
|
|
|
239
|
|
|
lineas.append(ax.plot(x, y, '.-', linewidth=0.6, alpha=0.4, label=label)[0]) |
240
|
|
|
|
241
|
|
|
if use_arrows: |
242
|
|
|
if len(x) > 1: |
243
|
|
|
flechas.append(ax.quiver(x[:-1], |
|
|
|
|
244
|
|
|
y[:-1], |
|
|
|
|
245
|
|
|
arrow_pos*(x[1:] - x[:-1]), |
|
|
|
|
246
|
|
|
arrow_pos*(y[1:] - y[:-1]), |
|
|
|
|
247
|
|
|
scale_units='xy', angles='xy', scale=1, |
|
|
|
|
248
|
|
|
width=0.0015, headwidth=10, headlength=10, headaxislength=6, |
|
|
|
|
249
|
|
|
alpha=0.5, color=lineas[i].get_color())) |
|
|
|
|
250
|
|
|
else: |
251
|
|
|
flechas.append(None) |
252
|
|
|
|
253
|
|
|
if len(x) > 0: |
254
|
|
|
#textos.append(ax.annotate(label, (x[0], y[0]), (-28,-10), textcoords='offset points', color=lineas[i].get_color(), fontsize=14)) |
|
|
|
|
255
|
|
|
textos.append(ax.text(x[-1]+0.015, y[-1]+0.015, label, {'color':lineas[i].get_color(), 'fontsize':14})) |
|
|
|
|
256
|
|
|
else: |
257
|
|
|
textos.append(None) |
258
|
|
|
|
259
|
|
|
ax.set_xlim(xlim) |
260
|
|
|
ax.set_ylim(ylim) |
261
|
|
|
ax.set_aspect('equal') |
262
|
|
|
|
|
|
|
|
263
|
|
|
fig.canvas.draw_idle() # if removed every artist separately instead of ax.clear() |
264
|
|
|
|
265
|
|
|
draw_all() |
266
|
|
|
|
267
|
|
|
def update(val): |
268
|
|
|
nonlocal lineas, flechas, textos |
|
|
|
|
269
|
|
|
|
270
|
|
|
for i, label in enumerate(knots_names): |
271
|
|
|
years = knots_jyears[label] |
272
|
|
|
idx = (val-1.5 < years) & (years < val) |
273
|
|
|
x = knots_X[label][idx] |
|
|
|
|
274
|
|
|
y = knots_Y[label][idx] |
|
|
|
|
275
|
|
|
|
276
|
|
|
lineas[i].set_xdata(x) |
277
|
|
|
lineas[i].set_ydata(y) |
278
|
|
|
|
279
|
|
|
if textos[i] is not None: |
280
|
|
|
if len(x) > 0: |
|
|
|
|
281
|
|
|
textos[i].set_position((x[-1]+0.015, y[-1]+0.015)) |
282
|
|
|
textos[i].set_text(label) |
283
|
|
|
else: |
284
|
|
|
textos[i].remove() |
285
|
|
|
textos[i] = None |
286
|
|
|
#textos[i].set_position((10, 10)) |
287
|
|
|
else: |
288
|
|
|
if len(x) > 0: |
|
|
|
|
289
|
|
|
textos[i] = ax.text(x[-1]+0.02, y[-1]+0.02, label, {'color':lineas[i].get_color(), 'fontsize':14}) |
|
|
|
|
290
|
|
|
|
291
|
|
|
if use_arrows: |
292
|
|
|
if flechas[i] is not None: |
293
|
|
|
flechas[i].remove() |
294
|
|
|
flechas[i] = None |
295
|
|
|
|
296
|
|
|
flechas[i] = 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
|
|
|
|
304
|
|
|
fig.canvas.draw_idle() |
305
|
|
|
|
306
|
|
|
|
307
|
|
|
|
308
|
|
|
selected_knot = None |
309
|
|
|
selected_ind = None |
310
|
|
|
selected_x = None |
311
|
|
|
selected_y = None |
312
|
|
|
|
313
|
|
|
|
314
|
|
View Code Duplication |
def submit_textbox(text): |
|
|
|
|
315
|
|
|
nonlocal mod, knots, knots_names, knots_values, knots_jyears, knots_X, knots_Y |
|
|
|
|
316
|
|
|
|
317
|
|
|
log.debug('Submited with:') |
318
|
|
|
log.debug(f' selected_knot {selected_knot}') |
|
|
|
|
319
|
|
|
log.debug(f' selected_ind {selected_ind}') |
|
|
|
|
320
|
|
|
log.debug(f' selected_x {selected_x}') |
|
|
|
|
321
|
|
|
log.debug(f' selected_y {selected_y}') |
|
|
|
|
322
|
|
|
|
323
|
|
|
if selected_knot is not None: |
|
|
|
|
324
|
|
|
mod.loc[selected_ind, 'label'] = text.upper() |
325
|
|
|
|
326
|
|
|
knots = dict(tuple(mod.groupby('label'))) |
327
|
|
|
knots_names = list(knots.keys()) |
328
|
|
|
knots_values = list(knots.values()) |
329
|
|
|
knots_jyears = {k:Time(knots[k]['date'].to_numpy()).jyear for k in knots} |
330
|
|
|
knots_X = {k:knots[k]['X'].to_numpy() for k in knots} |
|
|
|
|
331
|
|
|
knots_Y = {k:knots[k]['Y'].to_numpy() for k in knots} |
|
|
|
|
332
|
|
|
|
333
|
|
|
print(f"Updated index {selected_ind} to {text.upper()}") |
334
|
|
|
else: |
335
|
|
|
pass |
336
|
|
|
|
337
|
|
|
draw_all(slider_date.val) |
338
|
|
|
|
339
|
|
|
def line_select_callback(eclick, erelease): |
|
|
|
|
340
|
|
|
nonlocal selected_knot,selected_x, selected_y, selected_ind |
|
|
|
|
341
|
|
|
|
342
|
|
|
# 1 eclick and erelease are the press and release events |
343
|
|
|
x1, y1 = eclick.xdata, eclick.ydata |
|
|
|
|
344
|
|
|
x2, y2 = erelease.xdata, erelease.ydata |
|
|
|
|
345
|
|
|
log.debug("GUI: (%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2)) |
|
|
|
|
346
|
|
|
log.debug("GUI: The button you used were: %s %s" % (eclick.button, erelease.button)) |
|
|
|
|
347
|
|
|
|
348
|
|
|
selected_knot = None |
349
|
|
|
selected_x = None |
350
|
|
|
selected_y = None |
351
|
|
|
selected_ind = None |
352
|
|
|
|
353
|
|
|
for i, label in enumerate(knots_names): |
|
|
|
|
354
|
|
|
years = knots_jyears[label] |
355
|
|
|
idx = (slider_date.val-1.5 < years) & (years < slider_date.val) |
356
|
|
|
|
357
|
|
|
if np.sum(idx) == 0: |
358
|
|
|
continue # we did not select any component from this component, next one |
359
|
|
|
|
360
|
|
|
x = np.array(knots_X[label]) |
|
|
|
|
361
|
|
|
y = np.array(knots_Y[label]) |
|
|
|
|
362
|
|
|
|
363
|
|
|
# get points iside current rectangle for current date |
364
|
|
|
rect_idx = (x1 < x) & ( x < x2) & (y1 < y) & ( y < y2) & idx |
|
|
|
|
365
|
|
|
|
366
|
|
View Code Duplication |
if np.sum(rect_idx) > 0: |
|
|
|
|
367
|
|
|
textbox.set_val(label) |
368
|
|
|
selected_knot = label |
369
|
|
|
selected_x = x[rect_idx].ravel() |
370
|
|
|
selected_y = y[rect_idx].ravel() |
371
|
|
|
selected_ind = knots[label].index[rect_idx] |
372
|
|
|
log.debug(f'Selected {label} points rect_idx {rect_idx} x {x[rect_idx]}, y {y[rect_idx]} with indices {selected_ind}') |
|
|
|
|
373
|
|
|
textbox.begin_typing(None) |
374
|
|
|
break # if we find selected components in this epoch, continue with renaming |
375
|
|
|
else: |
376
|
|
|
pass |
377
|
|
|
|
378
|
|
|
update(slider_date.val) |
379
|
|
|
|
380
|
|
|
|
381
|
|
View Code Duplication |
def toggle_selector(event): |
|
|
|
|
382
|
|
|
log.debug('GUI: Key pressed.') |
383
|
|
|
if event.key in ['Q', 'q'] and toggle_selector.RS.active: |
384
|
|
|
log.debug('Selector deactivated.') |
385
|
|
|
toggle_selector.RS.set_active(False) |
386
|
|
|
if event.key in ['S', 's'] and not toggle_selector.RS.active: |
387
|
|
|
log.debug('Selector activated.') |
388
|
|
|
toggle_selector.RS.set_active(True) |
389
|
|
|
if event.key in ['R', 'r']: |
390
|
|
|
log.debug('Selector deactivated.') |
391
|
|
|
toggle_selector.RS.set_active(False) |
392
|
|
|
textbox.begin_typing(None) |
393
|
|
|
#textbox.set_val('') |
394
|
|
|
|
395
|
|
|
|
396
|
|
|
toggle_selector.RS = RectangleSelector(ax, line_select_callback, |
397
|
|
|
drawtype='box', useblit=True, |
398
|
|
|
button=[1, 3], # don't use middle button |
399
|
|
|
minspanx=0, minspany=0, |
400
|
|
|
spancoords='data', |
401
|
|
|
interactive=False) |
402
|
|
|
|
403
|
|
|
|
404
|
|
|
#plt.connect('key_press_event', toggle_selector) |
405
|
|
|
fig.canvas.mpl_connect('key_press_event', toggle_selector) |
406
|
|
|
|
407
|
|
|
|
408
|
|
|
|
409
|
|
|
from mpl_toolkits.axes_grid1 import make_axes_locatable |
|
|
|
|
410
|
|
|
|
411
|
|
|
divider_slider = make_axes_locatable(ax) |
412
|
|
|
slider_ax = divider_slider.append_axes("top", size="3%", pad="4%") |
|
|
|
|
413
|
|
|
slider_date = Slider(ax=slider_ax, label="Date", valmin=2007, valmax=2020, valinit=2008, valstep=0.2, orientation="horizontal") |
|
|
|
|
414
|
|
|
slider_date.on_changed(update) |
415
|
|
|
|
416
|
|
|
#divider_textbox = make_axes_locatable(ax) |
417
|
|
|
#textbox_ax = divider_textbox.append_axes("bottom", size="3%", pad="4%") |
|
|
|
|
418
|
|
|
textbox_ax = fig.add_axes([0.3,0.015,0.5,0.05]) |
|
|
|
|
419
|
|
|
textbox = TextBox(textbox_ax, 'Knot name:', initial='None') |
420
|
|
|
textbox.on_submit(submit_textbox) |
421
|
|
|
|
422
|
|
|
|
423
|
|
|
|
424
|
|
|
ax.set_xlim([-1.0, +1.0]) |
425
|
|
|
ax.set_ylim([-1.0, +1.0]) |
426
|
|
|
ax.set_aspect('equal') |
427
|
|
|
|
428
|
|
|
fig.suptitle('S to select, R to rename, Q to deactivate selector') |
429
|
|
|
|
430
|
|
|
print('S to select, R to rename, Q to deactivate selector') |
431
|
|
|
print('(you can select points from one component at a time)') |
432
|
|
|
print('(if you use the zoom or movement tools, remember to unselect them)') |
433
|
|
|
|
434
|
|
|
plt.show() |
435
|
|
|
|
|
|
|
|
436
|
|
|
return mod |
437
|
|
|
|
438
|
|
|
|
439
|
|
|
|
440
|
|
|
|
441
|
|
|
|
442
|
|
|
|
443
|
|
|
def KnotsIdGUI(mod): |
|
|
|
|
444
|
|
|
""" |
445
|
|
|
Prompt a GUI to select identified knots and alter their label, reprenting their |
446
|
|
|
time evolution. |
447
|
|
|
|
|
|
|
|
448
|
|
|
It can be used inside jupyter notebooks, using '%matplotlib widget' first. |
449
|
|
|
|
|
|
|
|
450
|
|
|
Parameters: |
451
|
|
|
----------- |
452
|
|
|
mod : :pd.DataFrame: |
453
|
|
|
pandas.DataFrame containing every knot, with at least columns |
|
|
|
|
454
|
|
|
'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
455
|
|
|
|
|
|
|
|
456
|
|
|
Returns: |
457
|
|
|
-------- |
458
|
|
|
mod : :pd.DataFrame: |
459
|
|
|
pandas.DataFrame containing every knot with their altered labels, with |
|
|
|
|
460
|
|
|
at least columns 'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
461
|
|
|
""" |
462
|
|
|
|
|
|
|
|
463
|
|
|
mod = mod.copy() |
464
|
|
|
|
|
|
|
|
465
|
|
|
knots = dict(tuple(mod.groupby('label'))) |
466
|
|
|
knots_names = list(knots.keys()) |
467
|
|
|
knots_values = list(knots.values()) |
468
|
|
|
knots_jyears = {k:Time(knots[k]['date'].to_numpy()).jyear for k in knots} |
469
|
|
|
knots_dates = {k:knots[k]['date'].to_numpy() for k in knots} |
470
|
|
|
knots_fluxes = {k:knots[k]['Flux (Jy)'].to_numpy() for k in knots} |
471
|
|
|
|
472
|
|
|
from matplotlib.widgets import TextBox, RectangleSelector |
|
|
|
|
473
|
|
|
|
474
|
|
|
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8,5)) |
|
|
|
|
475
|
|
|
|
|
|
|
|
476
|
|
|
lineas = list() |
477
|
|
|
textos = list() |
478
|
|
|
|
|
|
|
|
479
|
|
|
def draw_all(): |
480
|
|
|
nonlocal lineas, textos |
|
|
|
|
481
|
|
|
|
482
|
|
|
for linea in lineas: |
483
|
|
|
if linea is not None: |
484
|
|
|
linea.remove() |
485
|
|
|
for texto in textos: |
486
|
|
|
if texto is not None: |
487
|
|
|
texto.remove() |
488
|
|
|
|
489
|
|
|
ax.set_prop_cycle(None) |
490
|
|
|
|
|
|
|
|
491
|
|
|
lineas = list() |
492
|
|
|
textos = list() |
493
|
|
|
|
|
|
|
|
494
|
|
|
#ax.clear() # either clear the whole axis or remove every artist separetly |
495
|
|
|
|
|
|
|
|
496
|
|
|
for i, label in enumerate(knots_names): |
497
|
|
|
x, y = knots_jyears[label], knots_fluxes[label] |
|
|
|
|
498
|
|
|
|
499
|
|
|
if len(x) > 0: |
500
|
|
|
lineas.append(ax.plot(x, y, '.-', linewidth=0.8, alpha=0.5, label=label)[0]) |
501
|
|
|
else: |
502
|
|
|
lineas.append(None) |
503
|
|
|
|
|
|
|
|
504
|
|
|
if len(x) > 0: |
505
|
|
|
#textos.append(ax.annotate(label, (x[0], y[0]), (-28,-10), textcoords='offset points', color=lineas[i].get_color(), fontsize=14)) |
|
|
|
|
506
|
|
|
textos.append(ax.text(x[0], y[0], label, {'color':lineas[i].get_color(), 'fontsize':14})) |
|
|
|
|
507
|
|
|
else: |
508
|
|
|
textos.append(None) |
509
|
|
|
|
|
|
|
|
510
|
|
|
fig.canvas.draw_idle() # if removed every artist separately instead of ax.clear() |
511
|
|
|
|
512
|
|
|
|
513
|
|
|
def update(): |
514
|
|
|
nonlocal lineas, textos |
|
|
|
|
515
|
|
|
|
516
|
|
|
for i, label in enumerate(knots_names): |
|
|
|
|
517
|
|
|
x, y = knots_jyears[label], knots_fluxes[label] |
|
|
|
|
518
|
|
|
|
519
|
|
|
if lineas[i] is not None: |
520
|
|
|
if len(x) > 0: |
521
|
|
|
lineas[i].set_xdata(x) |
522
|
|
|
lineas[i].set_ydata(y) |
523
|
|
|
else: |
524
|
|
|
lineas[i].remove() |
525
|
|
|
lineas[i] = None |
526
|
|
|
else: |
527
|
|
|
if len(x) > 0: |
|
|
|
|
528
|
|
|
lineas[i] = ax.plot(x, y, '.-', linewidth=0.8, alpha=0.5, label=label)[0] |
529
|
|
|
|
|
|
|
|
530
|
|
|
if textos[i] is not None: |
531
|
|
|
if len(x) > 0: |
|
|
|
|
532
|
|
|
textos[i].set_position((x[0], y[0])) |
533
|
|
|
textos[i].set_text(label) |
534
|
|
|
else: |
535
|
|
|
textos[i].remove() |
536
|
|
|
textos[i] = None |
537
|
|
|
#textos[i].set_position((10, 10)) |
538
|
|
|
else: |
539
|
|
|
if len(x) > 0: |
|
|
|
|
540
|
|
|
#textos[i] = ax.annotate(label, (x[0], y[0]), (-24,-10), textcoords='offset points', color=lineas[i].get_color(), fontsize=15) |
|
|
|
|
541
|
|
|
textos[i] = ax.text(x[0], y[0], label, {'color':lineas[i].get_color(), 'fontsize':14}) |
|
|
|
|
542
|
|
|
|
|
|
|
|
543
|
|
|
fig.canvas.draw_idle() |
|
|
|
|
544
|
|
|
|
|
|
|
|
545
|
|
|
|
546
|
|
|
selected_knot = None |
547
|
|
|
selected_ind = None |
548
|
|
|
selected_date = None |
549
|
|
|
selected_flux = None |
550
|
|
|
|
|
|
|
|
551
|
|
View Code Duplication |
def submit_textbox(text): |
|
|
|
|
552
|
|
|
nonlocal mod, knots, knots_names, knots_values, knots_jyears, knots_dates, knots_fluxes |
|
|
|
|
553
|
|
|
|
554
|
|
|
log.debug('Submited with:') |
555
|
|
|
log.debug(f' selected_knot {selected_knot}') |
|
|
|
|
556
|
|
|
log.debug(f' selected_ind {selected_ind}') |
|
|
|
|
557
|
|
|
log.debug(f' selected_flux {selected_flux}') |
|
|
|
|
558
|
|
|
log.debug(f' selected_date {selected_date}') |
|
|
|
|
559
|
|
|
|
560
|
|
|
if selected_knot is not None: |
|
|
|
|
561
|
|
|
mod.loc[selected_ind, 'label'] = text.upper() |
562
|
|
|
|
563
|
|
|
knots = dict(tuple(mod.groupby('label'))) |
564
|
|
|
knots_names = list(knots.keys()) |
565
|
|
|
knots_values = list(knots.values()) |
566
|
|
|
knots_jyears = {k:Time(knots[k]['date'].to_numpy()).jyear for k in knots} |
567
|
|
|
knots_dates = {k:knots[k]['date'].to_numpy() for k in knots} |
568
|
|
|
knots_fluxes = {k:knots[k]['Flux (Jy)'].to_numpy() for k in knots} |
569
|
|
|
|
570
|
|
|
print(f"Updated index {selected_ind} to {text.upper()}") |
571
|
|
|
else: |
572
|
|
|
pass |
573
|
|
|
|
|
|
|
|
574
|
|
|
draw_all() |
575
|
|
|
|
576
|
|
|
def line_select_callback(eclick, erelease): |
|
|
|
|
577
|
|
|
nonlocal selected_knot,selected_date, selected_flux, selected_ind |
|
|
|
|
578
|
|
|
|
579
|
|
|
# 1 eclick and erelease are the press and release events |
580
|
|
|
x1, y1 = eclick.xdata, eclick.ydata |
|
|
|
|
581
|
|
|
x2, y2 = erelease.xdata, erelease.ydata |
|
|
|
|
582
|
|
|
log.debug("GUI: (%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2)) |
|
|
|
|
583
|
|
|
log.debug("GUI: The button you used were: %s %s" % (eclick.button, erelease.button)) |
|
|
|
|
584
|
|
|
|
585
|
|
|
selected_knot = None |
586
|
|
|
selected_date = None |
587
|
|
|
selected_flux = None |
588
|
|
|
selected_ind = None |
589
|
|
|
|
590
|
|
|
for i, label in enumerate(knots_names): |
|
|
|
|
591
|
|
|
years = knots_jyears[label] |
592
|
|
|
fluxes = knots_fluxes[label] |
593
|
|
|
|
|
|
|
|
594
|
|
|
# get points iside current rectangle for current date |
595
|
|
|
rect_idx = (x1 < years) & ( years < x2) & (y1 < fluxes) & ( fluxes < y2) |
|
|
|
|
596
|
|
|
|
|
|
|
|
597
|
|
|
if np.sum(rect_idx) == 0: |
598
|
|
|
continue # we did not select any component from this component, next one |
599
|
|
|
|
600
|
|
|
x = np.array(years) |
|
|
|
|
601
|
|
|
y = np.array(fluxes) |
|
|
|
|
602
|
|
|
|
603
|
|
View Code Duplication |
if np.sum(rect_idx) > 0: |
|
|
|
|
604
|
|
|
textbox.set_val(label) |
605
|
|
|
selected_knot = label |
606
|
|
|
selected_x = x[rect_idx].ravel() |
|
|
|
|
607
|
|
|
selected_y = y[rect_idx].ravel() |
|
|
|
|
608
|
|
|
selected_ind = knots[label].index[rect_idx] |
609
|
|
|
log.debug(f'Selected {label} points rect_idx {rect_idx} date {x[rect_idx]}, flux {y[rect_idx]} with indices {selected_ind}') |
|
|
|
|
610
|
|
|
break # if we find selected components in this epoch, continue with renaming |
611
|
|
|
else: |
612
|
|
|
pass |
613
|
|
|
|
614
|
|
|
update() |
615
|
|
|
|
616
|
|
|
|
617
|
|
View Code Duplication |
def toggle_selector(event): |
|
|
|
|
618
|
|
|
log.debug('GUI: Key pressed.') |
619
|
|
|
if event.key in ['Q', 'q'] and toggle_selector.RS.active: |
620
|
|
|
log.debug('Selector deactivated.') |
621
|
|
|
toggle_selector.RS.set_active(False) |
622
|
|
|
if event.key in ['S', 's'] and not toggle_selector.RS.active: |
623
|
|
|
log.debug('Selector activated.') |
624
|
|
|
toggle_selector.RS.set_active(True) |
625
|
|
|
if event.key in ['R', 'r']: |
626
|
|
|
log.debug('Selector deactivated.') |
627
|
|
|
toggle_selector.RS.set_active(False) |
628
|
|
|
textbox.begin_typing(None) |
629
|
|
|
#textbox.set_val('') |
630
|
|
|
|
631
|
|
|
|
632
|
|
|
toggle_selector.RS = RectangleSelector(ax, line_select_callback, |
633
|
|
|
drawtype='box', useblit=True, |
634
|
|
|
button=[1, 3], # don't use middle button |
635
|
|
|
minspanx=0, minspany=0, |
636
|
|
|
spancoords='data', |
637
|
|
|
interactive=False) |
638
|
|
|
|
639
|
|
|
|
640
|
|
|
fig.canvas.mpl_connect('key_press_event', toggle_selector) |
641
|
|
|
|
|
|
|
|
642
|
|
|
from mpl_toolkits.axes_grid1 import make_axes_locatable |
|
|
|
|
643
|
|
|
|
644
|
|
|
divider_textbox = make_axes_locatable(ax) |
645
|
|
|
textbox_ax = divider_textbox.append_axes("bottom", size="10%", pad="15%") |
|
|
|
|
646
|
|
|
#textbox_ax = fig.add_axes([0.3,0,0.5,0.05]) |
647
|
|
|
textbox = TextBox(textbox_ax, 'Knot name:', initial='None') |
648
|
|
|
textbox.on_submit(submit_textbox) |
649
|
|
|
|
|
|
|
|
650
|
|
|
draw_all() |
651
|
|
|
|
|
|
|
|
652
|
|
|
#ax.autoscale() |
653
|
|
|
ax.set_xlabel('date (year)') |
654
|
|
|
ax.set_ylabel('Flux (Jy)') |
655
|
|
|
ax.set_title('Flux from each component') |
656
|
|
|
|
|
|
|
|
657
|
|
|
xlims = Time(np.amin(mod['date'])).jyear, Time(np.amax(mod['date'])).jyear |
658
|
|
|
ax.set_xlim((xlims[0]-0.03*np.abs(xlims[1]-xlims[0]), xlims[1]+0.03*np.abs(xlims[1]-xlims[0]))) |
659
|
|
|
plt.show() |
660
|
|
|
|
661
|
|
|
return mod |
662
|
|
|
|
663
|
|
|
|
664
|
|
|
|
665
|
|
|
def KnotsIdReadMod(path=None, file_list=None): |
|
|
|
|
666
|
|
|
""" |
667
|
|
|
Read *_mod.mod files as printed by diffmap, return a dataframe containing all information ready |
|
|
|
|
668
|
|
|
to be worked on for labelling and to be used with these GUIs. |
669
|
|
|
|
|
|
|
|
670
|
|
|
Parameters: |
671
|
|
|
----------- |
672
|
|
|
path : :str: |
673
|
|
|
string indicating the path to the mod files to be used, their names must end in the format |
|
|
|
|
674
|
|
|
'%Y-%m-%d_mod.mod', for example, path = 'vlbi/ftree/*/*_mod.mod'. |
675
|
|
|
|
|
|
|
|
676
|
|
|
Returns: |
677
|
|
|
----------- |
678
|
|
|
mod : :pd.DataFrame: |
679
|
|
|
pandas.DataFrame containing every knot, with at least columns |
|
|
|
|
680
|
|
|
'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
681
|
|
|
""" |
682
|
|
|
|
|
|
|
|
683
|
|
|
mod_dates_str = list() |
684
|
|
|
mod_dates = list() |
685
|
|
|
mod_data = list() |
686
|
|
|
|
687
|
|
|
for f in glob.glob(f'{path}'): |
|
|
|
|
688
|
|
|
match = re.findall(r'([0-9]{4}-[0-9]{2}-[0-9]{2})_mod.mod', f) |
689
|
|
|
if not len(match) > 0: |
690
|
|
|
continue |
691
|
|
|
|
692
|
|
|
date_str = match[0] |
693
|
|
|
date = datetime.strptime(date_str, '%Y-%m-%d') |
694
|
|
|
|
695
|
|
|
mod_dates_str.append(date_str) |
696
|
|
|
mod_dates.append(date) |
697
|
|
|
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'])) |
|
|
|
|
698
|
|
|
|
699
|
|
|
# sort by date |
|
|
|
|
700
|
|
|
idx = np.argsort(mod_dates) |
701
|
|
|
mod_dates_str = list(np.array(mod_dates_str, dtype=object)[idx]) |
702
|
|
|
mod_dates = list(np.array(mod_dates, dtype=object)[idx]) |
703
|
|
|
mod_data = list(np.array(mod_data, dtype=object)[idx]) |
704
|
|
|
|
705
|
|
|
# fix stupid 'v' in columns, insert a label field, add X, Y columns |
706
|
|
|
for i in range(len(mod_dates)): |
|
|
|
|
707
|
|
|
mod_data[i].insert(0, 'label', value=None) |
708
|
|
|
mod_data[i].insert(1, 'date', value=mod_dates[i]) |
709
|
|
|
|
710
|
|
|
mod_data[i]['Flux (Jy)'] = mod_data[i]['Flux (Jy)'].str.strip('v').astype(float) |
711
|
|
|
mod_data[i]['Radius (mas)'] = mod_data[i]['Radius (mas)'].str.strip('v').astype(float) |
712
|
|
|
mod_data[i]['Theta (deg)'] = mod_data[i]['Theta (deg)'].str.strip('v').astype(float) |
713
|
|
|
|
714
|
|
|
mod_data[i].insert(5, 'X', mod_data[i]['Radius (mas)']*np.cos(np.pi/180*(mod_data[i]['Theta (deg)']-90))) |
|
|
|
|
715
|
|
|
mod_data[i].insert(6, 'Y', mod_data[i]['Radius (mas)']*np.sin(np.pi/180*(mod_data[i]['Theta (deg)']-90))) |
|
|
|
|
716
|
|
|
|
717
|
|
|
|
|
|
|
|
718
|
|
|
mod = pd.concat(mod_data, ignore_index=True) |
719
|
|
|
|
|
|
|
|
720
|
|
|
return mod |
721
|
|
|
|
722
|
|
|
|
723
|
|
|
def KnotsIdSaveMod(mod, path=None): |
|
|
|
|
724
|
|
|
pass |
725
|
|
|
|
726
|
|
|
def KnotsIdReadCSV(path=None): |
|
|
|
|
727
|
|
|
""" |
728
|
|
|
Read Knots data to .csv files (as done by Svetlana? ##) |
729
|
|
|
|
|
|
|
|
730
|
|
|
Each knot label has its own {label}.csv. To be compatible with Svetlana's format, |
|
|
|
|
731
|
|
|
columns should be modified to: |
732
|
|
|
'Date' (jyear), 'MJD', 'X(mas)', 'Y(mas)', 'Flux(Jy)' |
733
|
|
|
These columns are derived from the ones in `mod`, old ones are removed. |
734
|
|
|
|
|
|
|
|
735
|
|
|
Parameters: |
736
|
|
|
----------- |
737
|
|
|
path: :str: |
738
|
|
|
string containing the path to read files from eg: |
739
|
|
|
path = 'myknows/*.csv' |
740
|
|
|
Returns: |
741
|
|
|
-------- |
742
|
|
|
mod : :pd.DataFrame: |
743
|
|
|
pandas.DataFrame containing every knot, with at least columns |
|
|
|
|
744
|
|
|
'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
745
|
|
|
""" |
746
|
|
|
|
|
|
|
|
747
|
|
|
if path is None: |
748
|
|
|
log.error('Path not specified') |
749
|
|
|
raise Exception('Path not specified') |
750
|
|
|
|
|
|
|
|
751
|
|
|
dataL = list() |
|
|
|
|
752
|
|
|
|
|
|
|
|
753
|
|
|
for f in glob.glob(f'{path}'): |
|
|
|
|
754
|
|
|
match = re.findall(r'/(.*).csv', f) |
755
|
|
|
|
|
|
|
|
756
|
|
|
if not len(match) > 0: |
757
|
|
|
continue |
758
|
|
|
|
|
|
|
|
759
|
|
|
knot_name = match[0] |
760
|
|
|
|
|
|
|
|
761
|
|
|
log.debug(f'Loading {knot_name} from {f}') |
|
|
|
|
762
|
|
|
|
|
|
|
|
763
|
|
|
knot_data = pd.read_csv(f, parse_dates=['date'], date_parser=pd.to_datetime) |
764
|
|
|
|
|
|
|
|
765
|
|
|
dataL.append((knot_name, knot_data)) |
766
|
|
|
|
|
|
|
|
767
|
|
|
mod = pd.concat(dict(dataL), ignore_index=True) |
768
|
|
|
|
769
|
|
|
return mod |
770
|
|
|
|
771
|
|
|
|
772
|
|
|
|
773
|
|
|
def KnotsIdSaveCSV(mod, path=None): |
|
|
|
|
774
|
|
|
""" |
775
|
|
|
Save Knots data to .csv files (as done by Svetlana? ##) |
776
|
|
|
|
|
|
|
|
777
|
|
|
Each knot label has its own {label}.csv. To be compatible with Svetlana's format, |
|
|
|
|
778
|
|
|
columns should be modified to: |
779
|
|
|
'Date' (jyear), 'MJD', 'X(mas)', 'Y(mas)', 'Flux(Jy)' |
780
|
|
|
These columns are derived from the ones in `mod`, old ones are removed. |
781
|
|
|
|
|
|
|
|
782
|
|
|
Parameters: |
783
|
|
|
----------- |
784
|
|
|
mod : :pd.DataFrame: |
785
|
|
|
pandas.DataFrame containing every knot, with at least columns |
|
|
|
|
786
|
|
|
'label', 'date', 'X', 'Y', 'Flux (Jy)'. |
787
|
|
|
path: :str: |
788
|
|
|
string containing the path to which the files are to be saved, eg: |
789
|
|
|
path = 'my_knots/' |
790
|
|
|
|
|
|
|
|
791
|
|
|
Returns: |
792
|
|
|
-------- |
793
|
|
|
None |
794
|
|
|
""" |
795
|
|
|
|
|
|
|
|
796
|
|
|
if path is None: |
797
|
|
|
log.error('Path not specified') |
798
|
|
|
raise Exception('Path not specified') |
799
|
|
|
|
|
|
|
|
800
|
|
|
|
|
|
|
|
801
|
|
|
mod = mod.copy() |
802
|
|
|
|
|
|
|
|
803
|
|
|
|
|
|
|
|
804
|
|
|
mod_dict = dict(list(mod.groupby('label'))) |
805
|
|
|
|
|
|
|
|
806
|
|
|
for label, data in mod_dict.items(): |
807
|
|
|
data = data.copy() |
808
|
|
|
|
|
|
|
|
809
|
|
|
#data.insert(0, 'Date', Time(data['date']).jyear) |
810
|
|
|
#data.insert(1, 'MJD', Time(data['date']).mjd) |
811
|
|
|
#data = data.rename(columns={'X': 'X (mas)', 'Y': 'Y (mas)'}) |
812
|
|
|
#data = data.drop(columns=['date']) |
813
|
|
|
#data = data.drop(columns=['label']) |
814
|
|
|
#if 'Radius (mas)' in data.columns: |
815
|
|
|
# data = data.rename(columns={'Radius (mas)':'R(mas)'}) |
816
|
|
|
#data.columns = data.columns.str.replace(' ', '') |
817
|
|
|
|
|
|
|
|
818
|
|
|
data.to_csv(f'{path}/{label}.csv', index=False) |
|
|
|
|