-
Notifications
You must be signed in to change notification settings - Fork 1
/
arrivaltimes.py
1653 lines (1505 loc) · 49.8 KB
/
arrivaltimes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
import your
from matplotlib.gridspec import GridSpec
from matplotlib.patches import Rectangle, Ellipse
import scipy, glob
from itertools import zip_longest, cycle
from tqdm import tqdm
import pandas as pd
# from sklearn.mixture import GaussianMixture
import driftrate
from driftrate import scilabel, subburst_suffixes
# Based on https://github.com/mef51/subdriftlaw/blob/master/ArrivalTimes.ipynb
def zero_line_model(nu, dtdnu):
return dtdnu * nu
def line_model(nu, dtdnu, t_b):
return dtdnu * nu + t_b
def gauss_model(x, a, xo, sigma):
return a*np.exp(-(x-xo)**2/(2*(sigma**2)))
def smallestdivisor(n):
for i in range(2, n):
if n % i == 0:
return i
def listnpzs(path):
""" List all npz files in path """
files = glob.glob(path+'*.npz')
[print(f) for f in sorted(files)]
exit()
# N component model
def gaussmix_model(x, *p):
n = len(p)//3
model = 0
for i in range(0, n): # stops at n-1
model += gauss_model(x, p[0*n+i], p[1*n+i], p[2*n+i])
return model
def fitgauss(data, duration):
# use curve-fit (non-linear leastsq)
if len(data) == 0:
popt = [np.nan, np.nan, np.nan]
pcov = [np.nan, np.nan, np.nan]
return popt, pcov
if np.max(data) != 0:
data = data / np.max(data) # normalize
x = np.linspace(0, duration, num=len(data))
xo = sum(x*data)/sum(data)
try:
popt, pcov = scipy.optimize.curve_fit(
gauss_model,
x,
data,
p0=[
np.max(data),
xo,
np.sqrt(abs(sum(data*(x-xo)**2)/sum(data))) # sigma
],
)
except RuntimeError as e:
popt = [np.nan, np.nan, np.nan]
pcov = [np.nan, np.nan, np.nan]
finally:
return popt, pcov
def fitgaussmix(data, duration, xos, sigmas=None, fix_xos=False, tol=0.01):
n = len(xos) # Number of components
if np.max(data) != 0:
data = data / np.max(data) # normalize
x = np.linspace(0, duration, num=len(data))
if not sigmas:
sigmas = np.sqrt(abs(sum(data*(x-np.mean(xos))**2)/sum(data)))/4
guess = [*[np.max(data)]*n, *xos, *[sigmas]*n]
else:
guess = [*[np.max(data)]*n, *xos, *sigmas]
bounds = (-np.inf, np.inf)
if fix_xos:
bounds = ( # fix xos
[*[-np.inf]*n, *[xoi - tol for xoi in xos], *[-np.inf]*n],
[*[np.inf]*n, *[xoi + tol for xoi in xos], *[np.inf]*n]
)
try:
popt, pcov = scipy.optimize.curve_fit(
gaussmix_model,
x,
data,
p0=guess,
bounds=bounds
)
except RuntimeError as e:
popt = [0]*3*n
pcov = [0]*3*n
finally:
return popt, pcov
def fitrows(wfall, dt, freqs, plot=False):
fitdata = np.zeros((wfall.shape[0], 10))
for i, row in enumerate(wfall):
popt, pcov = fitgauss(row, wfall.shape[1]*dt)
# print(f'row {i}: {popt = } {np.mean(row) = } {freqs[i] = }')
perr = np.sqrt(np.diag(pcov))
if len(perr.shape) == 2: perr = np.diag(perr) # handles when pcov is nans
sigma = abs(popt[2])
tstart = (popt[1]-np.sqrt(2)*sigma)
tstart_err = np.sqrt(perr[1]**2 + 2*perr[2]**2)
tend = (popt[1]+np.sqrt(2)*sigma)
fitdata[i,:] = [freqs[i], tstart, tend, popt[0], popt[1], tstart_err, sigma, *perr]
return pd.DataFrame(data=fitdata, columns=[
'freqs',
'tstart',
'tend',
'amp',
'xo',
'tstart_err',
'sigma',
'amp_err',
'xo_err',
'sigma_err'
])
def plotburst(data, band, retfig=False, extent=None):
fig, axs = plt.subplot_mosaic(
'''
T.
WB
''',
figsize=(8, 7),
width_ratios=[3,1],
height_ratios=[1,3],
gridspec_kw={
'hspace': 0,
'wspace': 0
}
)
axs['W'].imshow(
data,
aspect='auto',
origin='lower',
interpolation='none',
extent=extent,
norm='linear',
vmax=np.quantile(data, 0.999),
)
if not extent:
extent = [0, data.shape[1], 0, data.shape[0]]
axs['T'].plot(np.linspace(*extent[:2], num=data.shape[1]), np.nanmean(data, axis=0))
axs['B'].stairs(band, np.linspace(*extent[2:], num=len(band)+1), orientation='horizontal')
if extent:
axs['W'].set_xlabel('Time (ms)')
axs['W'].set_ylabel('Frequency (MHz)')
axs['B'].yaxis.set_tick_params(labelleft=False)
axs['T'].sharex(axs['W'])
axs['B'].sharey(axs['W'])
if retfig:
return fig, axs
else:
plt.show()
plt.close()
logdebug = False
def printd(*args):
if logdebug: print(*args)
results_columns = [
'name',
'DM',
't0 (ms)',
't0_err',
'center_f (MHz)',
'center_f_err',
'duration (ms)',
'duration_err',
'bandwidth (MHz)',
'bandwidth_err',
'dtdnu (ms/MHz)',
'dtdnu_err',
'tb (ms)',
'tb_err',
'num_arrtimes'
]
def measureburst(
filename,
xos=[],
cuts=[],
sigmas=None,
fix_xos=False,
tolms=0.01,
targetDM=None,
correctTimes=False,
downfactors=(1,1),
subtractbg=False,
bw_filter='data_cutoff',
bw_width_factor=3, # burst based filter factors are likely to help a lot with complex and blended components, and avoids having to do submasks
snr_cutoff=3,
t_filter_factor=2,
crop=None,
# postcrop=None, # unimplemented
masks=[],
submasks=None,
bandmask_thres=None,
measure_drift=True,
show=True,
figsize=(10, 8), # (10, 9) for paper, (10,8) for screen
show_components=False,
cmap_norm='linear',
cmap='viridis',
save=True,
outdir='',
outfmt='.png',
return_arrivaltimes=False,
return_fig=False,
loadonly=False,
save_solutions=False,
load_solutions=None,
hide_legend=False,
legendloc=1,
label_components=False,
tpoint='tstart'
):
""" Measure spectro-temporal properties of a burst, and output a figure
Compute the inverse sub-burst slope (dt/dnu) using the per-row arrival time method.
Compute the duration and bandwidth by finding a 1-dimensional gaussian model
to the integrated time series and spectrum, respectively.
The duration and bandwidth are the 1 sigma widths of either fit.
Compute the center frequency as the center of the 1d spectrum model.
If multiple components are present, split them up and measure individually. The number
of components to fit for is equal to ``len(xos)``.
Args:
filename (str): filename to .npz of a *dedispersed* burst waterfall. File is expected to follow frbgui's :ref:`burstformat`.
xos (List[float] or 2-tuple of List[float], optional): List of times in ms of sub-burst centers.
Can be approximate. If a 2-tuple, the second list is used as the location(s) to cut the waterfall.
Using the ``cuts`` option instead is equivalent to using the 2 tuple option.
cuts (List[float], optional): List of times in ms to cut the waterfall. Useful for blended bursts.
User must make sure their cuts make sense (i.e. in between burst centers).
Typically, if one cut is needed, then all bursts should be cut as well even if well separated.
sigmas (List[float], optional): initial guesses for the width sigma when finding the 1-dimensional gaussian model
to the time series. Must be the same length as ``xos``
fix_xos (bool, optional): Default False. Whether or not to fix the passed ``xos`` when fitting the 1d model.
Useful when bursts are blended and one can visually distinguish where a burst should be from the waterfall
even if it appears completely absorbed in the integrated time series.
tolms (float, optional): Tolerance in milliseconds to use when ``fix_xos`` is True. Default is 0.01 ms.
targetDM (float, optional): the DM (pc/cm^3) to perform the measurement at.
Default is to perform the measurement at the DM of the npz file.
correctTimes (bool, optional): Shift xos and cuts to account for dispersive shift
when applying a targetDM other than the burst DM. Note that this shift will occur even when ``fix_xos`` is True.
downfactors (tuple[int], optional): 2-tuple of factors to downsample by in frequency and time (respectively)
subtractbg (bool, tuple[bool], optional): Perform a second background subtraction on subbursts.
By default will do a background subtraction using 10% of channels on the whole waterfall.
Pass ``(False, False)`` to skip both rounds of background subtraction.
bw_filter (str, optional): The type of spectral/bandwidth filter to apply on arrival times. Default is ``'data_cutoff'``. Options are
1. ``'data_cutoff'``: filter out arrival times in channels where the 1σ on-pulse mean amplitude is < 3 (see ``snr_cutoff``) times the noise amplitude
2. ``'model_cutoff'``: filter out arrival times in channels where the 1d spectral model amplitude is < 3 (see ``snr_cutoff``) times the noise amplitude
3. ``'model_width'``: filter out arrival times that lie beyond a multiple of the 1d spectral model width (σ). See ``bw_width_factor``.
bw_width_factor (int, optional): When using ``bw_filter=model_width``, 3σ of the burst bandwidth is applied as a spectral filter.
For bursts with lots of frequency structure this may be inadequate,
and this parameter can be used to override the filter width. It's recommended to try downsampling first. Note that a
high ``bw_width_factor`` such as 10-15 likely indicates the bandwidth measurement is being understimated.
snr_cutoff (int, optional): The S/N cutoff to use when ``bw_filter='data_cutoff'`` or ``bw_filter='model_cutoff'``.
By default equals 3.
t_filter_factor (int, optional): By default 2σ of the burst duration is applied as a temporal filter.
outdir (str, optional): string of output folder for figures. Defaults to ''.
crop (tuple[int], optional): pair of indices to crop the waterfall in time
masks (List[int], optional): frequency indices to mask. Masks are applied before downsampling
submasks (tuple[List[int]], optional): tuple of length `xos` of lists of indices to mask on a subcomponent's waterfall.
Note that contrary to ``masks``, these are applied after downsampling.
Indices are scaled from the original size to the downsampled size and so can cover more than one channel.
The length of ``submasks`` must match the length of ``xos``.
Example: To specify a mask on the 4th component of a waterfall with 4 components, pass
``submask=([],[],[],[22])``.
This is also a good way to filter out misbehaving components in an otherwise well-measured waterfall
and is useful for complicated bursts.
bandmask_thres (float or list[float], optional): The intensity threshold in the integrated spectrumabove which
points should be ignored when performing burst fits. This is primarily useful for masking scintillation peaks in order to
obtain more accurate burst bandwidth measurements. The appropriate level can be inferred from the output plot, or, if there are
multiple components, using the plot output when ``show_components=True``, since the spectrum normally shown is integrated
over all components. When a single value is specified, it will be applied to all components if there are more than one. To specify
a different threshold for each component, pass a list of values, using ``None`` when no spectral masking is to be applied.
For example: a 3-component waterfall may have ``bandmask_thres=[None, 0.1, 0.12]``. Note that this will only affect the bandwidth
measurement and not interact with the arrival times in each channel.
measure_drift (bool, optional): When True (default), and if ``len(xos) > 1`` (i.e. there are multiple burst components), will measure
the drift rate using the times and center frequencies of the bursts to fit a line. Will also plot a corresponding
line showing the drift rate measurement. Set to False to disable this behaviour.
show (bool, optional): if True show interactive figure window for each file
figsize (tuple, optional): figsize passed to matplotlib used for main output figure.
show_components (bool, optional): if True show figure window for each sub-burst
cmap_norm (str, optional) The colormap normalization ``norm`` parameter passed to matplotlib's imshow command
when plotting the waterfall. Default is 'linear', other options are 'log', 'symlog', 'logit',
or matplotlib's Normalize class.
cmap (str, optional): matplotlib colormap to use for waterfall
return_arrivaltimes (bool, optional): If True, will a dataframe of the arrival times per channel
return_fig (bool, optional): if True, return the matplotlib figure. The figure will not be closed.
save (bool, optional): if True save a figure displaying the measurements.
loadonly (bool, optional): if True will perform loading steps such as masking, dedispersing,
and downsampling, then return a tuple of wfall, freqs, times_ms, t_popt, DM, etc.
outfmt (str, optional): string of file format to save figure as. Default is '.png'. Include the '.' character.
save_solutions (bool, optional): setting to True will save a file inside of the folder specified by outdir
that contains the fit solution data for the 1d time series and the spectrum of each component. Useful for reviewing
measurements of bursts with many components that take a long time to analyse. Output filename will be of the form
``f'{bname}.sols.npz'``. Default False
load_solutions (str, optional): Filename of solutions file generated by ``save_solutions`` option. Default is None
hide_legend (bool, optional): Hides the legend in the output if True.
legendloc (int or str, optional): Set the location of the legend. Passed to matplotlib's loc argument when the legend is called.
label_components (bool, optional): If True, label components filters in the time series plot. Useful for complicated waterfalls.
tpoint (str, optional): One of 'tstart' (default), 'tend', or 'xo'.
Use to measure the slope based on the central peak in each channel, or based on the ending time in each channel.
Returns:
(list): ``results`` list of lists where each list is the result of the measurement.
This array can be used to make a pandas dataframe in the following way:
.. code-block:: python
resultsdf = pd.DataFrame(
data=results,
columns=arrivaltimes.results_columns
).set_index('name')
where the columns of the dataframe are
.. code-block:: python
'name',
'DM',
'center_f (MHz)',
'center_f_err',
'duration (ms)',
'duration_err',
'bandwidth (MHz)',
'bandwidth_err',
'dtdnu (ms/MHz)',
'dtdnu_err',
'tb (ms)', # t_b
'tb_err'
arrtimesdf (pd.DataFrame): Only returned when `return_arrivaltimes` is True.
fig (matplotlib.fig): Matplotlib figure. Only returned when `return_fig` is True.
"""
if type(xos) == tuple:
if len(xos) != 2:
raise "Error: xos must be a list or tuple of two lists"
cuts = xos[1]
xos = xos[0]
if type(xos) != list:
raise "Error: xos must be a list"
xos = sorted(xos)
cuts = sorted(cuts)
presubtractbg = True
if type(subtractbg) == tuple:
presubtractbg = subtractbg[0]
subtractbg = subtractbg[1]
results = []
bname = filename.split('/')[-1].split('.npz')[0]
data = np.load(filename, allow_pickle=True)
wfall = np.copy(data['wfall'])
if targetDM:
print(f"Info: Dedispersing from {data['DM']} to {targetDM} pc/cm3")
ddm = targetDM - data['DM']
wfall = driftrate.dedisperse(
wfall,
ddm,
min(data['dfs']),
data['bandwidth']/wfall.shape[0],
1000*data['duration']/wfall.shape[1]
)
else:
targetDM = data['DM']
for mask in masks:
wfall[mask] = 0
if presubtractbg:
wfall = driftrate.subtractbg(wfall, 0, int(wfall.shape[1]*0.1))
if type(crop) == tuple or type(crop) == list and len(crop) == 2:
wfall = wfall[..., crop[0]:crop[1]]
print(f"Info: {bname}: cropped to {wfall.shape = }")
wfall = driftrate.subsample(
wfall,
wfall.shape[0]//downfactors[0],
wfall.shape[1]//downfactors[1]
)
# determine resolutions accounting for downsampling and cropping
freqs_bin0 = min(data['dfs'])
res_freq = downfactors[0] * data['bandwidth'] / data['wfall'].shape[0] # MHz
res_time_ms = downfactors[1] * 1000*data['duration'] / data['wfall'].shape[1] # ms
duration = wfall.shape[1] * res_time_ms # duration of potentially cropped waterfall
print(f"Info: {res_freq = :.3f} MHz {res_time_ms = :.5f} ms {min(data['dfs']) = } -- {max(data['dfs']) = } MHz")
if targetDM and correctTimes:
ddm = targetDM - data['DM']
a_dm = 4.14937759336e6
center_i, errorsumi = driftrate.findCenter(wfall)
center_f = center_i*res_freq + freqs_bin0
high_ref_freq = max(data['dfs'])
deltat = - a_dm * (center_f**-2 - high_ref_freq**-2) * ddm
xos = [x+deltat for x in xos]
cuts = [c+deltat for c in cuts]
print(f'Info: shifting xos and cuts by {deltat} ms for {targetDM = } pc/cm3')
freqs = np.linspace(freqs_bin0, max(data['dfs']), num=wfall.shape[0]) # channel width/2 is already added
times_ms = np.linspace(0, duration, num=wfall.shape[1]) # array of timestamps
tseries = np.nanmean(wfall, axis=0)
tpoint = 'tstart' # 'tend', 'xo'
pktime = np.nanargmax(tseries)*res_time_ms
t_popt, _ = fitgauss(tseries, duration) # whether one or many components, for ref in plot
print(f"Info: {bname}: {data['wfall'].shape = }, {wfall.shape = }.")
print(f"Info: Using {bw_filter = } and {snr_cutoff = }")
if loadonly:
return (
wfall,
freqs,
times_ms,
res_freq,
res_time_ms,
targetDM,
t_popt
)
if len(xos) == 0:
xos.append(pktime)
## Assuming 1 burst:
# window = t_filter_factor*abs(t_popt[2]) # 2*stddev of a guassian fit to the integrated time series
##### multi component model: use multiple 1d gaussians to make multiple windows in time,
# then use the time windows to make frequency windows
n_bursts = len(xos)
if load_solutions:
solsdata = np.load(load_solutions, allow_pickle=True)
tmix_popt, tmix_perr = solsdata['tmix_popt'], solsdata['tmix_perr']
subbandpopts, subbandperrs = list(solsdata['subbandpopts']), list(solsdata['subbandperrs'])
subbandmodels = []
else:
tmix_popt, tmix_pcov = fitgaussmix(
tseries,
duration,
xos=xos,
sigmas=sigmas,
fix_xos=fix_xos,
tol=tolms
)
tmix_perr = np.sqrt(np.diag(tmix_pcov))
subbandpopts, subbandmodels, subbandperrs = [], [], []
if len(tmix_perr.shape) == 2: tmix_perr = np.diag(tmix_perr) # handles when pcov is nans
tmix_amps = tmix_popt[:n_bursts]
tmix_xos = tmix_popt[n_bursts:n_bursts*2]
tmix_xos_errs = tmix_perr[n_bursts:n_bursts*2]
tmix_sigmas = tmix_popt[n_bursts*2:n_bursts*3]
tmix_sigma_errs = tmix_perr[n_bursts*2:n_bursts*3]
printd(f"'sigmas': {[f for f in tmix_sigmas]}")
xos = tmix_xos if type(tmix_xos) == list else tmix_xos.tolist() # align to fit component centers
xos_errs = tmix_xos_errs if type(tmix_xos_errs) == list else tmix_xos_errs.tolist()
if not submasks:
submasks = ([],)*len(xos)
else:
if len(submasks) != len(xos):
raise ValueError(f"Please ensure the length of xos and submasks match. {len(xos) = } {len(submasks) = }")
if not bandmask_thres:
bandmask_thres = (None,)*len(xos)
elif type(bandmask_thres) != list: # assuming its a number, apply it to all components
bandmask_thres = [bandmask_thres,]*len(xos)
elif type(bandmask_thres) == list and (len(bandmask_thres) != len(xos)):
raise ValueError(f"Please ensure the length of xos and bandmask_thres match. {len(xos) = } {len(bandmask_thres) = }")
subfalls = []
subbands = []
# sample of noise levels matching
# number of channels used in corresponding subband integration
# from beginning of waterfall
noisesmpls = []
bandpass = np.zeros(wfall.shape[0])
xos_chans = np.floor(np.array(xos)/res_time_ms)
noise_edges = []
for xoi, s in zip(xos_chans, tmix_sigmas):
s4 = np.floor(4*np.abs(s)/res_time_ms)
s1 = np.floor(1*np.abs(s)/res_time_ms)
if s4 == 0 or s1 == 0:
s4 = 4 # hack
s1 = 1 # hack
lbl = subburst_suffixes[np.where(xos_chans == xoi)[0][0]]
print(
f"Warning: Component ({lbl}) has width below the time resolution, possibly due to poor 1D fit. Using 1σ width as 1 channel."
)
if len(cuts) == 0:
# account for when the edge is outside of wfall
if xoi-s4 < 0:
subfall = wfall[..., :int(xoi+s4)+1]
else:
subfall = wfall[..., int(xoi-s4):int(xoi+s4)+1] # 4sigma window around burst
else:
cutchans = np.floor(np.array(cuts)/res_time_ms).astype(int)
if xoi < cutchans[0]: # left edge
subfall = wfall[..., :cutchans[0]]
elif xoi > cutchans[-1]: # right edge
subfall = wfall[..., cutchans[-1]:]
else: # middle
ci = 0
while xoi > cutchans[ci]: ci += 1
prev_ci = ci-1
ci = -1
while xoi < cutchans[ci]: ci -= 1
next_ci = ci+1
subfall = wfall[..., cutchans[prev_ci]:cutchans[next_ci]]
# Compute component and sample noise
# Slicing syntax: a[start:stop] means items start through stop-1
# Therefore When slicing we add 1 to the end to include the ending channel.
ddof = 0 # Bessel's correction = 1
if xoi-s1 < 0: # left edge
print("Info: Spectral filter noise level sampled from end of waterfall.")
subband = wfall[..., :int(xoi+s1)+1].mean(axis=1)
noisesmpls.append(
wfall[..., -int(xoi+s1):].std(axis=1, ddof=ddof)
)
noise_edges.append((len(wfall)-int(xoi+s1), len(wfall)-1))
elif xoi-s1 > wfall.shape[1]:
subband = wfall.mean(axis=1) # probably bad fit, take it all as a fallback
noisesmpls.append(wfall.std(axis=1, ddof=ddof))
noise_edges.append((0, len(wfall)-1))
if bw_filter != 'model_width':
print("Warning: Noise sample taken from whole waterfall. Spectral filter may be overly aggressive.")
else: # Compute spectrum by summing only 1 sigma from burst peak
if int(xoi-s1) <= int(xoi+s1)-int(xoi-s1):
print("Warning: Noise sample overlaps with pulse region.")
subband = wfall[..., int(xoi-s1):int(xoi+s1)+1].mean(axis=1)
noisesmpls.append(
wfall[..., :int(xoi+s1)-int(xoi-s1)+1].std(axis=1, ddof=ddof)
)
noise_edges.append((0, int(xoi+s1)-int(xoi-s1)+1))
printd(
f"{int(xoi-s1) = }, {int(xoi+s1) = }",
wfall[..., :int(xoi+s1)-int(xoi-s1)+1].shape,
wfall[..., int(xoi-s1):int(xoi+s1)+1].shape,
int(xoi-s1),
int(xoi+s1)
)
if wfall[..., :int(xoi+s1)-int(xoi-s1)+1].shape != wfall[..., int(xoi-s1):int(xoi+s1)+1].shape:
print("Warning!!!: Subband and noise sample regions differ in size. Check xos.")
bandpass += subband
if len(cuts) == 0 and subtractbg: # need to be careful about bg subtraction when cutting
subfall = driftrate.subtractbg(subfall, 0, 10) # subtract bg again, left
subfall = driftrate.subtractbg(subfall, subfall.shape[1]-1-10, None) # right
subfalls.append(subfall)
subbands.append(subband)
#### Measurements
dtdnus, intercepts, subdfs = [], [], []
colors = cycle([
'white',
'black',
'red',
'green',
'blue',
'yellow',
'darkgreen',
'brown'
])
for subfall, subband, xosi, xosi_err, sigma, sigma_err, submask, bandmask_thresi, noisesmpl in zip(
subfalls,
subbands,
xos,
xos_errs,
tmix_sigmas,
tmix_sigma_errs,
submasks,
bandmask_thres,
noisesmpls
):
for m in submask:
if type(m) == range:
m = np.array(m)
subfall[m//downfactors[0]] = 0
sigma = abs(sigma)
subdf = fitrows(subfall, res_time_ms, freqs) # Fit a 1d gaussian in each row of the waterfall
if len(cuts) == 0:
subpktime = 4*sigma # since we made a 4 sigma window
else:
ci, edge = 0, 0
while ci < len(cuts) and xosi >= cuts[ci]:
edge = cuts[ci]
ci += 1
subpktime = xosi - edge
# Fit 1d gauss to burst spectrum
fo = sum(freqs*subband)/sum(subband) # this is an estimate of center_f
if load_solutions:
subband_popt, subband_perr = subbandpopts[0], subbandperrs[0]
subbandpopts = subbandpopts[1:]
subbandperrs = subbandperrs[1:]
else:
if bandmask_thresi: # remove points above the threshold for fitting
subband_fit = subband[subband < bandmask_thresi]
freqs_fit = freqs[(subband < bandmask_thresi).nonzero()]
else:
subband_fit, freqs_fit = subband, freqs
try:
subband_popt, subband_pcov = scipy.optimize.curve_fit(
gauss_model,
freqs_fit,
subband_fit/np.max(subband_fit),
p0=[
1,
fo,
np.sqrt(abs(sum(subband_fit*(freqs_fit-fo)**2)/sum(subband_fit))) # sigma
],
)
subband_popt[0] *= np.max(subband_fit)
subband_perr = np.sqrt(np.diag(subband_pcov))
## Diagnostic plot for spectrum fit:
if False:
plt.plot(freqs, subband, 'rX', ms=4)
plt.plot(freqs_fit, subband_fit, 'k.')
if bandmask_thresi:
plt.axhline(y=bandmask_thresi, c='r', ls='--')
plt.plot(freqs,gauss_model(freqs, *subband_popt))
plt.show();plt.close()
except (RuntimeError,ValueError) as e:
print(f"Warning: Spectrum fit failed.", e)
subband_popt, subband_perr = [0, 1, 1], [0, 0, 0]
bwidth, bwidth_err = subband_popt[2], subband_perr[2] # sigma of spetrum fit
pkfreq, pkfreq_err = subband_popt[1], subband_perr[1] # this is fitted center_f and center_f_err
## Apply time and spectral filters to points
printd(f"Debug: pre-filters {len(subdf) = }")
printd(f"Info: Applying '{bw_filter}' spectral filter ")
if bw_filter not in ['data_cutoff', 'model_cutoff', 'model_width']:
print(f"Warning: unrecognized {bw_filter = }. Reverting to 'data_cutoff'")
bw_filter = 'data_cutoff'
if bw_filter == 'data_cutoff':
if logdebug:
for f,n,s in zip(freqs,noisesmpl,subband):
printd(f"{f:.3f} MHz: {n = :.8f}\t{s = :.8f}\t{s/n = :.8f}")
subdf = subdf[ # freqs is the implied axis
subband/noisesmpl > snr_cutoff
]
elif bw_filter == 'model_cutoff' and bwidth != 1: # there must be a fit
model = gauss_model(subdf['freqs'], *subband_popt)
subdf = subdf[
model/noisesmpl > snr_cutoff
]
elif bw_filter == 'model_width' and bwidth != 1: # there must be a fit
subdf = subdf[
(pkfreq-bw_width_factor*bwidth < subdf['freqs']) &
(subdf['freqs'] < pkfreq+bw_width_factor*bwidth)
]
subdf = subdf[(subdf.amp > 0)]
subdf = subdf[subdf.tstart_err/subdf.tstart < 10]
subdf = subdf[ # time window filter
(subpktime-t_filter_factor*sigma < subdf[tpoint]) &
(subdf[tpoint] < subpktime+t_filter_factor*sigma) # full width
# (subdf[tpoint] < subpktime) # arrival time must be before pktime, just playing
]
printd(f"Debug: post-filters {len(subdf) = }")
# Measure dt/dnu
if len(subdf) > 1: # only fit a line if more than 1 point
popt, pcov = scipy.optimize.curve_fit(
line_model,
subdf['freqs'],
subdf[tpoint] - subpktime,
sigma=subdf[f'{tpoint}_err'],
absolute_sigma=True,
)
perr = np.sqrt(np.diag(pcov))
dtdnu, dtdnu_err = popt[0], perr[0]
t_b, tb_err = popt[1], perr[1]
# print(f"{dtdnu = :.5e} +/- {dtdnu_err:.5e} ms/MHz")
# print(f"{dtdnu2 = :.5e} +/- {dtdnu_err2:.5e} ms/MHz")
# print(f"{nu0fit = } +/- {nu0fit_err = }")
# print(f"{t_b = :.5f} +/- {tb_err:.5f} ms")
else: # no measurement
dtdnu, dtdnu_err = 0, 0
t_b, tb_err = 0, 0
# Sub-burst plot
if show_components:
subfig, subaxs = plotburst(
subfall,
subband,#.reshape(-1, 4).mean(axis=1),
retfig=True,
extent=[
0,
res_time_ms*subfall.shape[1],
freqs_bin0,
freqs_bin0 + res_freq*wfall.shape[0]
]
)
subcolors = [(1, 1, 1, alpha) for alpha in np.clip(subdf['amp'], 0, 1)]
subaxs['W'].scatter(
(subdf[tpoint]),
(subdf['freqs']),
c=subcolors,
edgecolor='r',
marker='o',
s=25
)
subaxs['W'].set_xlim(0, res_time_ms*subfall.shape[1])
subaxs['W'].set_ylim(freqs_bin0, freqs_bin0 + res_freq*wfall.shape[0])
subtimes = np.linspace(0, res_time_ms*subfall.shape[1], num=1000)
if dtdnu != 0:
subaxs['W'].plot(
subtimes,
(1/dtdnu)*(subtimes-subpktime),
'w--',
label=f'{dtdnu=:.2e} ms/MHz'
)
subaxs['W'].legend()
subaxs['B'].plot(
gauss_model(freqs, *subband_popt),
freqs
)
if bandmask_thresi:
plt.axvline(x=bandmask_thresi, c='r', ls='--')
plt.show()
plt.close()
subbandmodels.append(gauss_model(freqs, *subband_popt))
# transform times to full waterfall times
if len(cuts) == 0:
subdf[tpoint] = subdf[tpoint] + (xosi-4*sigma)
elif len(cuts) > 0:
if xosi < cuts[0]: # left edge
pass # times are already good
elif xosi > cuts[-1]: # right edge
subdf[tpoint] = subdf[tpoint] + cuts[-1]
else: # middle
ci = 0
while xosi > cuts[ci]: ci += 1
prev_ci = ci-1
subdf[tpoint] = subdf[tpoint] + cuts[prev_ci]
subdf['color'] = next(colors) # assign color to points
dtdnus.append((dtdnu, dtdnu_err))
intercepts.append((t_b, tb_err))
subbandpopts.append(subband_popt)
subbandperrs.append(subband_perr)
subdfs.append(subdf)
# print(f"{dtdnu = } +/- {dtdnu_err = }")
# print(f"{rowname} number of arrival times: {len(subdf) = }")
print(f"{bwidth = :.3f} +/- {bwidth_err:.3f} MHz")
rowname = bname if len(xos) == 1 else f'{bname}_{subburst_suffixes[xos.index(xosi)]}'
results.append([ # see `results_columns`
rowname, # 'name',
targetDM, # 'DM',
xosi, # 't0 (ms)',
xosi_err, # 't0_err'
pkfreq, # 'center_f (MHz)',
pkfreq_err, # 'center_f_err',
sigma, # 'duration (ms)',
sigma_err, # 'duration_err',
bwidth, # 'bandwidth (MHz)',
bwidth_err, # 'bandwidth_err',
dtdnu, # 'dtdnu (ms/MHz)',
dtdnu_err, # 'dtdnu_err',
t_b, # 'tb (ms)',
tb_err, # 'tb_err'
len(subdf) # 'num_arrtimes'
])
subdf = pd.concat(subdfs)
if save_solutions:
if outdir == '' or outdir[-1] == '/':
solname = f"{outdir}{bname}-{datetime.now().strftime('%b-%d-%Y')}.sols.npz"
else:
solname = f"{outdir}/{bname}-{datetime.now().strftime('%b-%d-%Y')}.sols.npz"
np.savez(
solname,
tmix_popt=tmix_popt,
tmix_perr=tmix_perr,
subbandpopts=subbandpopts,
subbandperrs=subbandperrs
)
print(f'Info: Saved {solname} solutions file')
##### Plotting
extent = [
-pktime,
res_time_ms*wfall.shape[1]-pktime,
freqs_bin0,
freqs_bin0 + res_freq*wfall.shape[0]
]
fig, axs = plt.subplot_mosaic(
'''
T.
AS
AS
AS
EE
''',
figsize=figsize,
width_ratios=[3,1],
# gridspec_kw={'hspace':0.464}
)
### Waterfall
ax_wfall = axs['A']
ax_wfall.imshow(
wfall,
aspect='auto',
origin='lower',
interpolation='none',
cmap=cmap,
extent=extent,
norm=cmap_norm,
vmax=np.quantile(wfall, 0.999),
# vmin=5, # hewitt microshots
)
ax_wfall.annotate(
f"DM = {targetDM:.3f} pc/cm$^3$",
xy=(0.05, 0.925),
xycoords='axes fraction',
color='white',
weight='black',
size=10,
bbox={"boxstyle":"round"}
)
if len(subdf) > 0:
ax_wfall.scatter( # component fit points
subdf[tpoint]-pktime,
subdf['freqs'],
c='w',
edgecolors=subdf['color'],
marker='o',
s=25,
alpha=np.clip(subdf['amp'], 0, 1)
)
ax_wfall.set_xlabel("Time (ms)")
ax_wfall.set_ylabel("Frequency (MHz)")
# Component lines
for (dtdnu, dtdnu_err), (tb, tb_err), xoi in zip(dtdnus, intercepts, xos):
if dtdnu != 0:
ax_wfall.plot(
times_ms-pktime,
(1/dtdnu)*(times_ms-xoi) - tb/dtdnu,
'w-.',
alpha=0.75,
# label=f'$dt/d\\nu = $ {dtdnu:.2e} $\\pm$ {dtdnu_err:.2e}'
label=f'{subburst_suffixes[xos.index(xoi)]}. $dt/d\\nu =$ {scilabel(dtdnu, dtdnu_err)} ms/MHz'
)
# Noise sample lines
# s1 = np.floor(1*np.abs(s)/res_time_ms)
for ns in noise_edges:
for n in ns:
ax_wfall.axvline(
x=n*res_time_ms-pktime,
c='r',
ls='--'
)
if len(xos) > 1 and measure_drift:
targetdf = pd.DataFrame(
data=results,
columns=results_columns
).set_index('name')
odrjob = scipy.odr.ODR(
scipy.odr.RealData(
targetdf['center_f (MHz)'],
targetdf['t0 (ms)']-pktime,
sx=targetdf['center_f_err'],
sy=targetdf['t0_err'],
),
scipy.odr.Model(lambda B, x: B[0]*x + B[1]),
beta0=[-1, 0]
)
odrjob.set_job(fit_type=0)
odrfit = odrjob.run()
drift, drift_err = odrfit.beta[0], np.sqrt(np.diag(odrfit.cov_beta))[0]
ax_wfall.plot(
times_ms-pktime,
(1/drift)*(times_ms-pktime)+(-odrfit.beta[1]/drift),
'r-.',
label=f'Drift: $\\Delta t / \\Delta \\nu = ${scilabel(drift, drift_err)} ms/MHz',
)
ax_wfall.errorbar(
targetdf['t0 (ms)']-pktime,
targetdf['center_f (MHz)'],
xerr=targetdf['t0_err'],
yerr=targetdf['center_f_err'],
fmt='rX',
markeredgecolor='k'
)
# Test line for 1 parameter line model:
# ax_wfall.plot(
# times_ms-pktime,
# (1/dtdnu2)*(times_ms-xoi)+pkfreq,
# 'y-.',
# alpha=0.75,
# # label=f'$dt/d\\nu = $ {dtdnu2:.2e} $\\pm$ {dtdnu_err:.2e}'
# label=f'{subburst_suffixes[xos.index(xoi)]}. $dt/d\\nu =$ {scilabel(dtdnu2, dtdnu_err2)} ms/MHz'
# )
ax_wfall.set_title(f"{bname}")
if not hide_legend: ax_wfall.legend(loc=legendloc, handlelength=0)
ax_tseries = axs['T']
ax_tseries.plot(times_ms-pktime, tseries)
# plot filter windows (time)
sp = 0
for s, xoi in zip(tmix_sigmas, xos):
w = t_filter_factor*np.abs(s)
ax_tseries.add_patch(Rectangle(
(xoi-pktime-w, ax_tseries.get_ylim()[0] + sp*(np.max(tseries)*0.075)),
width=2*w,
height=np.max(tseries)*0.075,
color='tomato',
alpha=0.5
))
if label_components:
ax_tseries.annotate(
f"{subburst_suffixes[xos.index(xoi)]}",
(xoi-pktime, ax_tseries.get_ylim()[0] + sp*(np.max(tseries)*0.075)),
)
sp += 1
ax_tseries.plot(
times_ms-pktime,
gauss_model(
times_ms-pktime,
np.max(tseries)*t_popt[0],
t_popt[1]-pktime,
t_popt[2]
),
'k--',
alpha=0.1
)
# Gaussian mix model
tmix_popt[:n_bursts] = [a*np.max(tseries) for a in tmix_amps]
tmix_popt[n_bursts:n_bursts*2] = [x-pktime for x in tmix_xos]
ax_tseries.plot(
times_ms-pktime,
gaussmix_model(
times_ms-pktime,
*tmix_popt
),
'k--',
alpha=0.8
)
### Summed Spectrum (summed over burst widths). Total and individual
downband = 1
if len(bandpass) % downband != 0:
downband = smallestdivisor(len(bandpass))
bandpass_down = bandpass.reshape(-1, downband).mean(axis=1)
axs['S'].stairs(
bandpass_down,
np.linspace(*extent[2:], num=len(bandpass_down)+1),
orientation='horizontal',
# lw=2