-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_functions.py
1228 lines (1033 loc) · 45.5 KB
/
helper_functions.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
#!/usr/bin/python
import sys
import os
import warnings
import numpy as np
import pandas as pd
# bilby for likelihoods
from bilby import likelihood
import george
import inspect
# for gp fitting
from scipy.linalg import cholesky, cho_solve
from scipy.special import erf
####################################################################
# SED functions
# Define function to model power law SED
def linear_sed_func(x, C, alpha):
y = np.zeros(len(np.atleast_1d(x)))
y = C * (x**alpha)
return y
def orienti_sed_func(x, a, b, c):
"""
Parabola in log-space
a: curvature
b: linear param
c: quadratic param
"""
y = np.zeros(len(np.atleast_1d(x)))
y = 10 ** (a + np.log10(x) * (b + c * np.log10(x)))
return y
def snellen_sed_func(x, a, b, c, d):
"""
Generic peaked model with power laws either side of the peak
a: peak frequency
b: peak flux
c: alpha_thick
d: alpha_thin
"""
y = (
b
/ (1.0 - np.exp(-1.0))
* ((x / a) ** c)
* (1.0 - np.exp(-((x / a) ** (d - c))))
)
return y
def retriggered_sed_func(x, a, b, c, d, S_norm, alpha):
"""
Parameters:
a = peak frequency (MHz)
b = peak flux (Jy)
c = alpha_thick (spectral index in thick region below the peak)
d = alpha_thin (spectral index in thin region above the peak)
S_norm = normalisation factor for the additional power law bit
alpha = spectral index of the additional power law bit
"""
y = np.zeros(len(np.atleast_1d(x)))
y = (
b
/ (1.0 - np.exp(-1.0))
* ((x / a) ** c)
* (1.0 - np.exp(-((x / a) ** (d - c))))
+ S_norm * x**alpha
)
return y
####################################################################
# Classes of the above for Gaussian process fitting
class LinearModel(george.modeling.Model):
parameter_names = ("C", "alpha")
def get_value(self, t):
return self.C * (t.flatten() ** self.alpha)
class OrientiModel(george.modeling.Model):
parameter_names = ("a", "b", "c")
def get_value(self, t):
return 10 ** (
self.a + np.log10(t.flatten()) * (self.b + self.c * np.log10(t.flatten()))
)
class SnellenModel(george.modeling.Model):
parameter_names = ("a", "b", "c", "d")
def get_value(self, t):
return (
self.b
/ (1.0 - np.exp(-1.0))
* ((t.flatten() / self.a) ** self.c)
* (1.0 - np.exp(-((t.flatten() / self.a) ** (self.d - self.c))))
)
class RetriggeredModel(george.modeling.Model):
parameter_names = ("a", "b", "c", "d", "S_norm", "alpha")
def get_value(self, t):
return (
self.b
/ (1.0 - np.exp(-1.0))
* ((t.flatten() / self.a) ** self.c)
* (1.0 - np.exp(-((t.flatten() / self.a) ** (self.d - self.c))))
+ self.S_norm * t.flatten() ** self.alpha
)
####################################################################
# other useful functions
def get_param_strs_toplot(model):
"""Takes a RaiSEDModel objects and returns the strings outlining the parameter
values to be added to a plot
Parameters
==========
model: an input RaiSEDModel object that has been run through bilby
Returns
==========
plot_str_list: a list of strings to be put on the plot, 1 string per parameter
"""
plot_str_list = []
if model.gp:
prefix = "mean:"
else:
prefix = ""
# linear
if "PL" in model.model_type:
fit_param_intervals = model.get_param_medians_errors()
# prior order is C, alpha
c_str = " S$_{{norm}}= {:.2f}^{{+{:.2f}}}_{{-{:.2f}}}$".format(
fit_param_intervals[0].median,
fit_param_intervals[0].plus,
fit_param_intervals[0].minus,
)
alpha_str = "$\\alpha = {:.2f}^{{+{:.2f}}}_{{-{:.2f}}}$".format(
fit_param_intervals[1].median,
fit_param_intervals[1].plus,
fit_param_intervals[1].minus,
)
plot_str_list = [c_str, alpha_str]
# orienti - put this back in!!
elif "curved" in model.model_type:
if not model.param_intervals:
raise AttributeError(
'{} is the best model but its intervals have not been calculated yet, please call "get_orienti_intervals(model_object)" before plotting'.format(
model.model_type
)
)
# prior order is a, b, c
peak_freq_str = "$\\nu_{{peak}} = {:.2f}^{{+{:.2f}}}_{{-{:.2f}}}$ MHz".format(
model.param_intervals["peak_freq_interval"][0],
model.param_intervals["peak_freq_interval"][2],
model.param_intervals["peak_freq_interval"][1],
)
peak_flux_str = "$S_{{peak}} = {:.2f}^{{+{:.2f}}}_{{-{:.2f}}}$ Jy".format(
model.param_intervals["peak_flux_interval"][0],
model.param_intervals["peak_flux_interval"][2],
model.param_intervals["peak_flux_interval"][1],
)
alpha_thick_str = "$\\alpha_{{thick}} = {:.2f}^{{+{:.2f}}}_{{-{:.2f}}}$".format(
model.param_intervals["alpha_thick_interval"][0],
model.param_intervals["alpha_thick_interval"][2],
model.param_intervals["alpha_thick_interval"][1],
)
alpha_thin_str = "$\\alpha_{{thin}} = {:.2f}^{{+{:.2f}}}_{{-{:.2f}}}$".format(
model.param_intervals["alpha_thin_interval"][0],
model.param_intervals["alpha_thin_interval"][2],
model.param_intervals["alpha_thin_interval"][1],
)
plot_str_list = [peak_freq_str, peak_flux_str, alpha_thick_str, alpha_thin_str]
# snellen
elif "PS" in model.model_type:
fit_param_intervals = model.get_param_medians_errors()
# prior order is a,b,c,d
peak_freq_str = "$\\nu_{{peak}} = {:.2f}^{{+{:.2f}}}_{{-{:.2f}}}$ MHz".format(
fit_param_intervals[0].median,
fit_param_intervals[0].plus,
fit_param_intervals[0].minus,
)
peak_flux_str = "$S_{{peak}} = {:.2f}^{{+{:.2f}}}_{{-{:.2f}}}$ Jy".format(
fit_param_intervals[1].median,
fit_param_intervals[1].plus,
fit_param_intervals[1].minus,
)
alpha_thick_str = "$\\alpha_{{thick}} = {:.2f}^{{+{:.2f}}}_{{-{:.2f}}}$".format(
fit_param_intervals[2].median,
fit_param_intervals[2].plus,
fit_param_intervals[2].minus,
)
alpha_thin_str = "$\\alpha_{{thin}} = {:.2f}^{{+{:.2f}}}_{{-{:.2f}}}$".format(
fit_param_intervals[3].median,
fit_param_intervals[3].plus,
fit_param_intervals[3].minus,
)
plot_str_list = [peak_freq_str, peak_flux_str, alpha_thick_str, alpha_thin_str]
# retriggered
elif "retrig" in model.model_type:
# use our other helper function which should have already been called!!
if not model.param_intervals:
raise AttributeError(
'{} is the best model but its intervals have not been calculated yet, please call "get_retrig_intervals(model_object)" before plotting'.format(
model.model_type
)
)
# peak_freq_interval, peak_flux_interval, trough_freq_interval, trough_flux_interval, alpha_retrig_interval, \
# alpha_thick_interval, alpha_thin_interval, func_type = get_retrig_intervals(result = model, SED_func = retriggered_sed_func, gp = model.gp, min_obs_freq = min_obs_freq, max_obs_freq = max_obs_freq)
# prior order is a,b,c,d,Snorm,alpha
peak_freq_str = "$\\nu_{{peak}} = {:.2f}^{{+{:.2f}}}_{{-{:.2f}}}$ MHz".format(
model.param_intervals["peak_freq_interval"][0],
model.param_intervals["peak_freq_interval"][2],
model.param_intervals["peak_freq_interval"][1],
)
peak_flux_str = "$S_{{peak}} = {:.2f}^{{+{:.2f}}}_{{-{:.2f}}}$ Jy".format(
model.param_intervals["peak_flux_interval"][0],
model.param_intervals["peak_flux_interval"][2],
model.param_intervals["peak_flux_interval"][1],
)
trough_freq_str = "$\\nu_{{min}} = {:.2f}^{{+{:.2f}}}_{{-{:.2f}}}$ MHz".format(
model.param_intervals["trough_freq_interval"][0],
model.param_intervals["trough_freq_interval"][2],
model.param_intervals["trough_freq_interval"][1],
)
trough_flux_str = "$S_{{min}} = {:.2f}^{{+{:.2f}}}_{{-{:.2f}}}$ Jy".format(
model.param_intervals["trough_flux_interval"][0],
model.param_intervals["trough_flux_interval"][2],
model.param_intervals["trough_flux_interval"][1],
)
alpha_retrig_str = (
"$\\alpha_{{retrig}} = {:.2f}^{{+{:.2f}}}_{{-{:.2f}}}$".format(
model.param_intervals["alpha_retrig_interval"][0],
model.param_intervals["alpha_retrig_interval"][2],
model.param_intervals["alpha_retrig_interval"][1],
)
)
alpha_thick_str = "$\\alpha_{{thick}} = {:.2f}^{{+{:.2f}}}_{{-{:.2f}}}$".format(
model.param_intervals["alpha_thick_interval"][0],
model.param_intervals["alpha_thick_interval"][2],
model.param_intervals["alpha_thick_interval"][1],
)
alpha_thin_str = "$\\alpha_{{thin}} = {:.2f}^{{+{:.2f}}}_{{-{:.2f}}}$".format(
model.param_intervals["alpha_thin_interval"][0],
model.param_intervals["alpha_thin_interval"][2],
model.param_intervals["alpha_thin_interval"][1],
)
plot_str_list = [
peak_freq_str,
peak_flux_str,
trough_freq_str,
trough_flux_str,
alpha_retrig_str,
alpha_thick_str,
alpha_thin_str,
]
else:
raise TypeError(
"I cannot find this type of model to extract parameter strings. (Yikes!)"
)
return plot_str_list
def get_best_model(array):
"""Takes an array of RaiSEDModel objects and orders them so that the best-fitting
model is first, i.e. in decreasing logZ order.
NOTE: This code adopts the Jeffrey's scale as slightly modified by Kass & Rafferty (1995)
to aid in model selection:
0 < log10 B12 < 0.5 --> marginal preference for model 1 (<~ 3 times more likely)
0.5 < log10 B12 < 1 --> preference for model 1 (3 < x < 10 times more likely)
1 < log10 B12 < 2 --> strong preference for model 1 (10 < x < 100 times more likely)
log10 B12 > 2 --> very strong preference for model 1 (> 100 times more likely)
Parameters
==========
array: an input array of RaiSEDModel objects that have been run through bilby
Returns
==========
sorted_array: the input array sorted so that the best fitting model comes first
"""
log10Z_arr = np.array([x.get_log10z()[0] for x in array])
log10Z_err_arr = np.array([x.get_log10z()[1] for x in array])
log10Z_noise_arr = np.array([x.get_log10z()[2] for x in array])
bmd_array = np.array([x.get_bmd() for x in array])
md_array = np.array([x.get_md() for x in array])
# now sort them
bmd_array = bmd_array[np.argsort(log10Z_arr)[::-1]]
md_array = md_array[np.argsort(log10Z_arr)[::-1]]
array = array[np.argsort(log10Z_arr)[::-1]]
log10Z_err_arr = log10Z_err_arr[np.argsort(log10Z_arr)[::-1]]
log10Z_noise_arr = log10Z_noise_arr[np.argsort(log10Z_arr)[::-1]]
log10Z_arr = log10Z_arr[np.argsort(log10Z_arr)[::-1]]
# print('EVIDENCE, NOISE AND ERROR ARRAYS:')
# print(log10Z_arr)
# print(log10Z_noise_arr)
# print(log10Z_err_arr)
# if the log10 of the evidence error is > 1 then the fit is suspicious and (from visual inspection)
# likely bimodal
# now look at the first model, and see what the bayes factor is!
bf1 = log10Z_arr[0] - log10Z_arr[1]
bf1_lim = log10Z_arr[0]
# print('bayes factor: {} and dimensional difference: {}'.format(bf1, (md_array[0] - md_array[1])))
# if there is a marginal prefernce for model 1 and model 2 has fewer dimensions
# according to the Bayesian Model Dimensionality, call model 2 the best fit
if (
bf1 > 0 and (md_array[0] - md_array[1]) > 0
): # make sure that the second model is simpler
if (
bf1 < 0.5
): # and bmd_array[1] < bmd_array[0]: #np.log10(md_array[0] - md_array[1] + 1):
# swap models 1 and 2, since the evidence for 1 over 2 is marginal
# and 2 is the simpler model
log10Z_arr[[0, 1]] = log10Z_arr[[1, 0]]
bmd_array[[0, 1]] = bmd_array[[1, 0]]
md_array[[0, 1]] = md_array[[1, 0]]
array[[0, 1]] = array[[1, 0]]
# now do the same but with the first and third models, in case that is preferred!
if log10Z_arr.shape[0] > 2:
bf2 = log10Z_arr[0] - log10Z_arr[2]
if (
bf2 > 0 and (md_array[0] - md_array[2]) > 0
): # make sure that the third model is simpler
if (
bf2 < 0.5
): # and bmd_array[2] < bmd_array[0]: #np.log10(md_array[0] - md_array[2] + 1):
log10Z_arr[[0, 2]] = log10Z_arr[[2, 0]]
bmd_array[[0, 2]] = bmd_array[[2, 0]]
md_array[[0, 2]] = md_array[[2, 0]]
array[[0, 2]] = array[[2, 0]]
return array, log10Z_arr, bmd_array
def get_credible_interval(array, interval=np.nan, lower_quant=0.16, upper_quant=0.84):
"""Calculate the median and error bar for a given array
Parameters
==========
array:
The data for which to calculate the median and error bar
interval: float [0,1]
An interval for which to calculate the error bars, can be used as
an alternative to specifying lower_quant and upper_quant
lower_quant: float
The lower quantile to calculate the error bars for.
upper_quant: float
The lower quantile to calculate the error bars for.
The default is a 68% credible interval (which can be thought of as
1 sigma), corresponding to lower_quant = 0.16, upper_quant = 0.84
Returns
=======
summary: list
A list of: median, lower, upper
"""
# if interval specified, calculate lower and upper quantiles
if not np.isnan(interval):
if interval >= 1 or interval <= 0:
raise ValueError("interval must be between 0 and 1")
# convert to quantiles
lower_quant = (1 - interval) / 2
upper_quant = 1 - lower_quant
# remove nans from array
array = array[~np.isnan(array)]
# now get the quantiles!
quants_to_compute = np.array([lower_quant, 0.5, upper_quant])
quants = np.percentile(array, quants_to_compute * 100)
median = quants[1]
plus = quants[2] - median
minus = median - quants[0]
return [median, minus, plus]
def get_orienti_intervals(
result,
SED_func,
gp=False,
freq_min=1e7,
freq_max=1e11,
bilby=True,
min_obs_freq=np.nan,
max_obs_freq=np.nan,
):
"""
gets the confidence intervals for:
the peak frequency and corresponding flux
a linear approximation of the spectral index either side of the turning point
ordered as:
alpha1 = alpha_below_peak
alpha2 = alpha_above_peak
Returns
==========
A list comprising the following elements:
peak_freq_interval = [val, p, m]
peak_flux_interval = [val, p, m]
alpha_thick_interval = [val, p, m]
alpha_thin_interval = [val, p, m]
func_type = str
"""
if gp:
prefix = "mean:"
else:
prefix = ""
print(min_obs_freq, max_obs_freq)
# a hack so this function works with both bilby and jaxns
if bilby:
# parameter posteriors
a_result = result.result.posterior[prefix + "a"].values
b_result = result.result.posterior[prefix + "b"].values
c_result = result.result.posterior[prefix + "c"].values
else:
a_result = result[prefix + "a"]
b_result = result[prefix + "b"]
c_result = result[prefix + "c"]
# get physical params, where peak_freq = -b/2*c and peak_flux = y_val at peak_Freq
peak_freq_dist = 10 ** (-b_result / (2 * c_result))
peak_flux_dist = SED_func(peak_freq_dist, a_result, b_result, c_result)
# get median and stdev, as well as upper and lower errors (68% credible intervals)
freq_credible_interval = get_credible_interval(peak_freq_dist)
[peak_freq_med, peak_freq_plus, peak_freq_minus] = freq_credible_interval
flux_credible_interval = get_credible_interval(peak_flux_dist)
[peak_flux_med, peak_flux_plus, peak_flux_minus] = flux_credible_interval
# get set of points to do spectral indices on: We are fitting in MHz!!
freq_arr = 10 ** (
np.linspace(np.log10(freq_min / 1e6), np.log10(freq_max / 1e6), 1000)
) # np.linspace(np.log10(70), np.log10(30e3)
min_obs_idx = np.argmin(np.abs(freq_arr - min_obs_freq / 1e6))
max_obs_idx = np.argmin(np.abs(freq_arr - max_obs_freq / 1e6))
# broadcast it for later use
freq_arr_broadcast = np.tile(freq_arr, (a_result.shape[0], 1))
# get the fluxes for each combination of parameters in the posterior
flux_dist = SED_func(freq_arr.reshape(-1, 1), a_result, b_result, c_result)
flux_dist = flux_dist.transpose()
# now get the alpha_thick and alpha_thin indices away from the peak!
# taking index at last observed point, following an old snellen paper (FIND REFERENCE!) where they did this
alpha_thick_grads = 2 * c_result * np.log10(freq_arr_broadcast[:, min_obs_idx]) + b_result
alpha_thick_interval = get_credible_interval(alpha_thick_grads)
# thin_idxs = np.around(freq_arr.shape[0] + (freq_arr.shape[0] - peak_idxs)*0.5)
# alpha_thin_grads = 2*c_result*flux_dist[thin_idxs] + b_result
alpha_thin_grads = 2 * c_result * np.log10(freq_arr_broadcast[:, max_obs_idx]) + b_result
alpha_thin_interval = get_credible_interval(alpha_thin_grads)
# write dict to result object
result.param_intervals["peak_freq_interval"] = freq_credible_interval
result.param_intervals["peak_flux_interval"] = flux_credible_interval
result.param_intervals["alpha_thick_interval"] = alpha_thick_interval
result.param_intervals["alpha_thin_interval"] = alpha_thin_interval
return (
freq_credible_interval,
flux_credible_interval,
alpha_thick_interval,
alpha_thin_interval,
)
def get_retrig_intervals(
result,
SED_func,
gp=False,
freq_min=1e7,
freq_max=1e11,
bilby=True,
min_obs_freq=np.nan,
max_obs_freq=np.nan,
):
"""
gets the confidence intervals for:
the peak frequency and corresponding flux
the trough (local minimum) and corresponding flux
a linear approximation of the spectral index either side of the turning points, and in between them,
ordered as:
alpha1 = alpha_retriggered
alpha2 = alpha_below_peak
alpha3 = alpha_above_peak
Returns
==========
A list comprising the following elements:
peak_freq_interval = [val, p, m]
peak_flux_interval = [val, p, m]
trough_freq_interval = [val, p, m]
trough_flux_interval = [val, p, m]
alpha_retrig_interval = [val, p, m]
alpha_thick_interval = [val, p, m]
alpha_thin_interval = [val, p, m]
func_type = str
"""
if gp:
prefix = "mean:"
else:
prefix = ""
# a hack so this function works with both bilby and jaxns
if bilby:
# parameter posteriors
a_result = result.result.posterior[prefix + "a"].values
b_result = result.result.posterior[prefix + "b"].values
c_result = result.result.posterior[prefix + "c"].values
d_result = result.result.posterior[prefix + "d"].values
s_norm_result = result.result.posterior[prefix + "S_norm"].values
alpha_result = result.result.posterior[prefix + "alpha"].values
else:
a_result = result[prefix + "a"]
b_result = result[prefix + "b"]
c_result = result[prefix + "c"]
d_result = result[prefix + "d"]
s_norm_result = result[prefix + "snorm"]
alpha_result = result[prefix + "alpha"]
# get physical param distributions! We are fitting in MHz!!
freq_arr = 10 ** (
np.linspace(np.log10(freq_min / 1e6), np.log10(freq_max / 1e6), 1000)
) # np.linspace(np.log10(70), np.log10(30e3)
# broadcast it for later use
freq_arr_broadcast = np.tile(freq_arr, (a_result.shape[0], 1))
# get the fluxes for each combination oxf parameters in the posterior
flux_dist = SED_func(
freq_arr.reshape(-1, 1),
a_result,
b_result,
c_result,
d_result,
s_norm_result,
alpha_result,
)
flux_dist = flux_dist.transpose()
#mask out things below 10-5 because we run into jumps due to the machine
#floor down here
if flux_dist[flux_dist < 10e-5].shape[0] > 0:
unique_keys, indices = np.unique(np.argwhere(flux_dist < 10e-5)[:,0], return_index=True)
freq_max_new = freq_arr[np.min(np.argwhere(flux_dist < 20e-5)[indices, 1])]
freq_max_new = 1e6*freq_max_new
#print('new max freq: {}GHz'.format(freq_max_new/1e6))
if freq_max_new/1e6 < 500:
freq_max_new = freq_arr[np.max(np.argwhere(flux_dist < 20e-5)[indices, 1])]
freq_max_new = 1e6*freq_max_new
#print('new max freq (second try): {}GHz'.format(freq_max_new/1e6))
freq_arr = 10 ** (
np.linspace(np.log10(freq_min / 1e6), np.log10(freq_max_new / 1e6), 1000)
) # np.linspace(np.log10(70), np.log10(30e3)
# broadcast it for later use
freq_arr_broadcast = np.tile(freq_arr, (a_result.shape[0], 1))
# get the fluxes for each combination of parameters in the posterior
flux_dist = SED_func(
freq_arr.reshape(-1, 1),
a_result,
b_result,
c_result,
d_result,
s_norm_result,
alpha_result,
)
flux_dist = flux_dist.transpose()
#extra check to make sure this is really gone
'''
print('number below error threshold:')
print(flux_dist[flux_dist < 10e-5].shape[0])
freq_max_loop = freq_max
while flux_dist[flux_dist < 10e-5].shape[0] > 0:
print(flux_dist[flux_dist < 10e-5].shape[0])
# get physical param distributions! We are fitting in MHz!!
freq_max_loop *= 0.9
print(freq_max_loop)
freq_arr = 10 ** (
np.linspace(np.log10(freq_min / 1e6), np.log10(freq_max_loop / 1e6), 1000)
) # np.linspace(np.log10(70), np.log10(30e3)
# broadcast it for later use
freq_arr_broadcast = np.tile(freq_arr, (a_result.shape[0], 1))
# get the fluxes for each combination of parameters in the posterior
flux_dist = SED_func(
freq_arr.reshape(-1, 1),
a_result,
b_result,
c_result,
d_result,
s_norm_result,
alpha_result,
)
flux_dist = flux_dist.transpose()
'''
# use np.diff to find turning pts
diff_dist = np.diff(flux_dist, axis=1)
# get the sign of each point
diff_sign = np.sign(diff_dist)
# THIS IS TO IGNORE INFLEXION POINTS!! Added 09/09
diff_sign[diff_sign == 0] = 1
# look for a sign change
signchange = ((diff_sign - np.roll(diff_sign, 1, axis=1)) > 0).astype(int)
signchange -= ((diff_sign - np.roll(diff_sign, 1, axis=1)) < 0).astype(int)
# make the first row zero
signchange[0, :] = 0
# add on a column to keep it the same size!
signchange = np.c_[signchange, np.zeros(a_result.shape)]
# get turning pts, where signchange = 1 in each row
local_min_idxs = np.where(signchange > 0)
local_max_idxs = np.where(signchange < 0)
'''
print(flux_dist.shape)
print(freq_arr_broadcast.shape)
print(local_min_idxs[0].shape)
print(local_max_idxs[0].shape)
'''
# get max and min fluxes based on this
max_fluxes = flux_dist[local_max_idxs]
min_fluxes = flux_dist[local_min_idxs]
# get max and min corresponding frequencies
max_freqs = freq_arr_broadcast[local_max_idxs]
min_freqs = freq_arr_broadcast[local_min_idxs]
# now get median values and errorbars (68% credible intervals)
try:
peak_freq_interval = get_credible_interval(max_freqs)
peak_flux_interval = get_credible_interval(max_fluxes)
except IndexError as e:
peak_freq_interval = [-1, 0, 0]
peak_flux_interval = [-1, 0, 0]
try:
trough_freq_interval = get_credible_interval(min_freqs)
trough_flux_interval = get_credible_interval(min_fluxes)
except IndexError as e:
trough_freq_interval = [-1, 0, 0]
trough_flux_interval = [-1, 0, 0]
# now get interval idxs, make it simple and do 30% away from the max/min on either side
distance_factor = 0.3 # per cent
max_lower_idx = np.argmin(
np.abs(freq_arr - peak_freq_interval[0] * (1 - distance_factor))
)
max_upper_idx = np.argmin(
np.abs(freq_arr - peak_freq_interval[0] * (1 + distance_factor))
)
min_lower_idx = np.argmin(
np.abs(freq_arr - trough_freq_interval[0] * (1 - distance_factor))
)
min_upper_idx = np.argmin(
np.abs(freq_arr - trough_freq_interval[0] * (1 + distance_factor))
)
# find if the peak is above or below the trough in frequency
# print('peak and trough intervals')
# print(peak_freq_interval)
# print(trough_freq_interval)
# print('min and max obs')
# print(min_obs_freq, max_obs_freq)
# print(min_obs_freq, max_obs_freq)
# print(np.min(freq_arr), np.max(freq_arr))
# if peak_freq_interval[0] < min_obs_freq/1e6 or peak_freq_interval[0] > max_obs_freq/1e6:
# peak_freq_interval = [-1,0,0]
# peak_flux_interval = [-1,0,0]
# if trough_freq_interval[0] < min_obs_freq/1e6:
# trough_freq_interval = [-1,0,0]
# trough_flux_interval = [-1,0,0]
if min_lower_idx == 0:
distance_factor = 0.1 # x100 = per cent
min_lower_idx = np.argmin(
np.abs(freq_arr - trough_freq_interval[0] * (1 - distance_factor))
)
if max_upper_idx == len(freq_arr) - 1:
distance_factor = 0.1 # x100 = per cent
max_upper_idx = np.argmin(
np.abs(freq_arr - peak_freq_interval[0] * (1 + distance_factor))
)
# if it's still zero then make it even smaller!
# if min_lower_idx == 0:
# min_lower_idx = 1
# if max_upper_idx == len(freq_arr) - 1:
# max_upper_idx = len(freq_arr) - 2
#if max_lower = min_upper shift them apart a bit
if max_lower_idx == min_upper_idx:
max_lower_idx -= 3
min_upper_idx += 3
if max_lower_idx == 0:
max_lower_idx += 1
'''
print(
"calculating indices at: {:.2f}, {:.2f}, {:.2f}, {:.2f} MHz".format(
freq_arr[max_lower_idx],
freq_arr[max_upper_idx],
freq_arr[min_lower_idx],
freq_arr[min_upper_idx],
)
)
'''
#if we have no turning pts, asusme linear and just return the spectral index
if (min_lower_idx == max_upper_idx and min_lower_idx == 0) or (max_lower_idx == min_lower_idx and max_upper_idx == min_upper_idx):
alphagrads = (
np.log10(flux_dist[:, 0])
- np.log10(flux_dist[:, -1])
) / (
np.log10(freq_arr_broadcast[:, 0])
- np.log10(freq_arr_broadcast[:, -1])
)
# get the credible interval based on this
alpha_interval = get_credible_interval(alphagrads)
alpha_retrig_interval = [-1, 0, 0]
alpha_thick_interval = [-1, 0, 0]
alpha_thin_interval = alpha_interval
func_type = 'linear'
# write dict to result object
result.param_intervals["peak_freq_interval"] = [-1, 0, 0]
result.param_intervals["peak_flux_interval"] = [-1, 0, 0]
result.param_intervals["trough_freq_interval"] = [-1, 0, 0]
result.param_intervals["trough_flux_interval"] = [-1, 0, 0]
result.param_intervals["alpha_retrig_interval"] = [-1, 0, 0]
result.param_intervals["alpha_thick_interval"] = [-1, 0, 0]
result.param_intervals["alpha_thin_interval"] = alpha_interval
return (
peak_freq_interval,
peak_flux_interval,
trough_freq_interval,
trough_flux_interval,
alpha_retrig_interval,
alpha_thick_interval,
alpha_thin_interval,
func_type,
)
# print(trough_freq_interval[0], trough_freq_interval[0]*(1-distance_factor), freq_arr)
# peak above trough
if (
peak_freq_interval[0] > trough_freq_interval[0]
and peak_freq_interval[0] > freq_min / 1e6
and trough_freq_interval[0] > freq_min / 1e6
):
func_type = "minfirst"
# go from min to max
# alphathickgrads = (np.log10(flux_dist[local_max_idxs]) - np.log10(flux_dist[local_min_idxs]))/(np.log10(freq_arr_broadcast[local_max_idxs]) - np.log10(freq_arr_broadcast[local_min_idxs]))
alphathickgrads = (
np.log10(flux_dist[:, max_lower_idx])
- np.log10(flux_dist[:, min_upper_idx])
) / (
np.log10(freq_arr_broadcast[:, max_lower_idx])
- np.log10(freq_arr_broadcast[:, min_upper_idx])
)
# get the credible interval based on this
alpha_thick_interval = get_credible_interval(alphathickgrads)
# calculate the gradient in each realisation
# retriggrads = (np.log10(flux_dist[local_min_idxs]) - np.log10(flux_dist[local_min_idxs[0],0]))/(np.log10(freq_arr_broadcast[local_min_idxs]) - np.log10(freq_arr_broadcast[local_min_idxs[0],0]))
retriggrads = (
np.log10(flux_dist[:, min_lower_idx]) - np.log10(flux_dist[:, 0])
) / (
np.log10(freq_arr_broadcast[:, min_lower_idx])
- np.log10(freq_arr_broadcast[:, 0])
)
print("lower idx: {}".format(min_lower_idx))
# get the credible interval based on this
alpha_retrig_interval = get_credible_interval(retriggrads)
# calculate the gradient in each realisation
# thingrads = (np.log10(flux_dist[local_max_idxs[0],-1]) - np.log10(flux_dist[local_max_idxs]))/(np.log10(freq_arr_broadcast[local_max_idxs[0],-1]) - np.log10(freq_arr_broadcast[local_max_idxs]))
thingrads = (
np.log10(flux_dist[:, -1]) - np.log10(flux_dist[:, max_upper_idx])
) / (
np.log10(freq_arr_broadcast[:, -1])
- np.log10(freq_arr_broadcast[:, max_upper_idx])
)
# get the credible interval based on this
alpha_thin_interval = get_credible_interval(thingrads)
elif (
peak_freq_interval[0] < trough_freq_interval[0]
and peak_freq_interval[0] > 0
and trough_freq_interval[0] > 0
):
func_type = "maxfirst"
# go from 0 to max
# alphathickgrads = (np.log10(flux_dist[local_max_idxs]) - np.log10(flux_dist[local_max_idxs[0],0]))/(np.log10(freq_arr_broadcast[local_max_idxs]) - np.log10(freq_arr_broadcast[local_max_idxs[0],0]))
alphathickgrads = (
np.log10(flux_dist[:, max_lower_idx]) - np.log10(flux_dist[:, 0])
) / (
np.log10(freq_arr_broadcast[:, max_lower_idx])
- np.log10(freq_arr_broadcast[:, 0])
)
# get the credible interval based on this
alpha_thick_interval = get_credible_interval(alphathickgrads)
# go from local min to end
# retriggrads = (np.log10(flux_dist[local_min_idxs[0],-1]) - np.log10(flux_dist[local_min_idxs]))/(np.log10(freq_arr_broadcast[local_min_idxs[0],-1]) - np.log10(freq_arr_broadcast[local_min_idxs]))
retriggrads = (
np.log10(flux_dist[:, -1]) - np.log10(flux_dist[:, min_upper_idx])
) / (
np.log10(freq_arr_broadcast[:, -1])
- np.log10(freq_arr_broadcast[:, min_upper_idx])
)
# get the credible interval based on this
alpha_retrig_interval = get_credible_interval(retriggrads)
# go from max to min
# thingrads = (np.log10(flux_dist[local_min_idxs]) - np.log10(flux_dist[local_max_idxs]))/(np.log10(freq_arr_broadcast[local_min_idxs]) - np.log10(freq_arr_broadcast[local_max_idxs]))
thingrads = (
np.log10(flux_dist[:, max_upper_idx])
- np.log10(flux_dist[:, min_lower_idx])
) / (
np.log10(freq_arr_broadcast[:, max_upper_idx])
- np.log10(freq_arr_broadcast[:, min_lower_idx])
)
# get the credible interval based on this
alpha_thin_interval = get_credible_interval(thingrads)
# we have only a trough and no peak
elif peak_freq_interval[0] < 0 and trough_freq_interval[0] > 0:
func_type = "minonly"
# go from min to peak
# alphathickgrads = (np.log10(flux_dist[local_min_idxs[0],-1]) - np.log10(flux_dist[local_min_idxs]))/(np.log10(freq_arr_broadcast[local_min_idxs[0],-1]) - np.log10(freq_arr_broadcast[local_min_idxs]))
alphathickgrads = (
np.log10(flux_dist[:, -1]) - np.log10(flux_dist[:, min_upper_idx])
) / (
np.log10(freq_arr_broadcast[:, -1])
- np.log10(freq_arr_broadcast[:, min_upper_idx])
)
# get the credible interval based on this
alpha_thick_interval = get_credible_interval(alphathickgrads)
# go from start to min
# retriggrads = (np.log10(flux_dist[local_min_idxs]) - np.log10(flux_dist[local_min_idxs[0],0]))/(np.log10(freq_arr_broadcast[local_min_idxs]) - np.log10(freq_arr_broadcast[local_min_idxs[0],0]))
retriggrads = (
np.log10(flux_dist[:, min_lower_idx]) - np.log10(flux_dist[:, 0])
) / (
np.log10(freq_arr_broadcast[:, min_lower_idx])
- np.log10(freq_arr_broadcast[:, 0])
)
# get the credible interval based on this
alpha_retrig_interval = get_credible_interval(retriggrads)
# get the credible interval based on this
alpha_thin_interval = [np.nan, 0, 0]
# we have only a peak and no trough - shouldn't happen we should pick SNELLEN
elif trough_freq_interval[0] <= freq_min / 1e6 and peak_freq_interval[0] > 0:
warnings.warn("Only a peak no trough, despite being fit by a retriggered model")
func_type = "maxonly"
# go from start to peak
# alphathickgrads = (np.log10(flux_dist[local_max_idxs]) - np.log10(flux_dist[local_max_idxs[0],0]))/(np.log10(freq_arr_broadcast[local_max_idxs]) - np.log10(freq_arr_broadcast[local_max_idxs[0],0]))
alphathickgrads = (
np.log10(flux_dist[:, max_lower_idx]) - np.log10(flux_dist[:, 0])
) / (
np.log10(freq_arr_broadcast[:, max_lower_idx])
- np.log10(freq_arr_broadcast[:, 0])
)
# print(alphathickgrads)
# exit()
# get the credible interval based on this
alpha_thick_interval = get_credible_interval(alphathickgrads)
# go from peak to end
# thingrads = (np.log10(flux_dist[local_max_idxs[0],-1]) - np.log10(flux_dist[local_max_idxs]))/(np.log10(freq_arr_broadcast[local_max_idxs[0],-1]) - np.log10(freq_arr_broadcast[local_max_idxs]))
thingrads = (
np.log10(flux_dist[:, -1]) - np.log10(flux_dist[:, max_upper_idx])
) / (
np.log10(freq_arr_broadcast[:, -1])
- np.log10(freq_arr_broadcast[:, max_upper_idx])
)
# get the credible interval based on this
alpha_thin_interval = get_credible_interval(thingrads)
# get the credible interval based on this
alpha_retrig_interval = [np.nan, 0, 0]
elif trough_freq_interval[0] < 0 and peak_freq_interval[0] < 0:
# it's basically linear!
func_type = "linear"
retriggrads = (np.log10(flux_dist[:, -1]) - np.log10(flux_dist[:, 0])) / (
np.log10(freq_arr_broadcast[:, -1]) - np.log10(freq_arr_broadcast[:, 0])
)
# get the credible interval based on this
alpha_retrig_interval = get_credible_interval(retriggrads)
# get the credible interval based on this
alpha_thin_interval = [-1, 0, 0]
alpha_thick_interval = [np.nan, 0, 0]
else:
print(peak_freq_interval)
print(peak_flux_interval)
print("Something has gone wrong in fitting, please go back and check the bilby logs")
exit()
# write dict to result object
result.param_intervals["peak_freq_interval"] = peak_freq_interval
result.param_intervals["peak_flux_interval"] = peak_flux_interval
result.param_intervals["trough_freq_interval"] = trough_freq_interval
result.param_intervals["trough_flux_interval"] = trough_flux_interval
result.param_intervals["alpha_retrig_interval"] = alpha_retrig_interval
result.param_intervals["alpha_thick_interval"] = alpha_thick_interval
result.param_intervals["alpha_thin_interval"] = alpha_thin_interval
return (
peak_freq_interval,
peak_flux_interval,
trough_freq_interval,
trough_flux_interval,