-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnltm_plot_utils_v3.py
958 lines (896 loc) · 40.6 KB
/
nltm_plot_utils_v3.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
import matplotlib.pyplot as plt
import matplotlib as mpl
from cycler import cycler
import numpy as np
import scipy.stats as sps
import json, pickle, os, corner, glob
import enterprise.signals.utils as utils
from enterprise.pulsar import Pulsar
import arviz as az
from collections import defaultdict
import pandas as pd
import la_forge.diagnostics as dg
import la_forge.core as co
from la_forge.rednoise import plot_rednoise_spectrum, plot_free_spec
from la_forge.utils import epoch_ave_resid
color_cycle_wong = [
"#000000",
"#E69F00",
"#009E73",
"#56B4E9",
"#0072B2",
"#F0E442",
"#D55E00",
"#CC79A7",
]
mpl.rcParams["axes.prop_cycle"] = cycler(color=color_cycle_wong)
current_path = os.getcwd()
splt_path = current_path.split("/")
# top_path_idx = splt_path.index("akaiser")
top_path_idx = splt_path.index("nanograv")
top_dir = "/".join(splt_path[0 : top_path_idx + 1])
def get_psrs(psrlist, datareleases):
"""Loads in par and tim files for all psrs in psrlist for each datarelease in datareleases.
:psrlist:
list, list of pulsars
:datareleases:
list, list of datareleases
"""
parfiles = []
timfiles = []
for datarelease in datareleases:
datadir = top_dir + "/{}".format(datarelease)
if datarelease == "12p5yr":
if "J0740+6620" in psrlist:
tmp_parfiles = sorted(glob.glob(datadir + "/*.par"))
tmp_timfiles = sorted(glob.glob(datadir + "/*.tim"))
else:
tmp_parfiles = sorted(glob.glob(datadir + "/narrowband/par/*.par"))
tmp_timfiles = sorted(glob.glob(datadir + "/narrowband/tim/*.tim"))
else:
tmp_parfiles = sorted(glob.glob(datadir + "/par/*.par"))
tmp_timfiles = sorted(glob.glob(datadir + "/tim/*.tim"))
# filter
for psr in psrlist:
parfiles = np.concatenate(
(
parfiles,
[x for x in tmp_parfiles if psr in x],
),
axis=0,
)
timfiles = np.concatenate(
(
timfiles,
[x for x in tmp_timfiles if psr in x],
),
axis=0,
)
psrs = []
for p, t in zip(parfiles, timfiles):
print(p, t)
psr = Pulsar(p, t, ephem="DE436", clk=None, drop_t2pulsar=False)
psrs.append(psr)
return psrs
def get_pardict(psrs, datareleases):
"""assigns a parameter dictionary for each psr per dataset the parfile values/errors
:psrs:
objs, enterprise pulsar instances corresponding to datareleases
:datareleases:
list, list of datareleases
"""
pardict = {}
for psr, dataset in zip(psrs, datareleases):
pardict[dataset] = {}
print(dataset)
pardict[dataset][psr.name] = {}
if np.any(np.isnan(psr.t2pulsar.errs())) or np.any(
[err == 0.0 for err in psr.t2pulsar.errs()]
):
psr.t2pulsar.fit()
for par, vals, errs in zip(
psr.fitpars[1:], psr.t2pulsar.vals(), psr.t2pulsar.errs()
):
pardict[dataset][psr.name][par] = {}
pardict[dataset][psr.name][par]["val"] = vals
pardict[dataset][psr.name][par]["err"] = errs
return pardict
def get_chaindir_indices(chaindir_list):
"""separates list of chains by priors, puts corresponding indices in a list in a dictionary
:chaindir_list:
list, list of chain locations
"""
chaindir_indices = {}
for i, chaindir in enumerate(chaindir_list):
sep = chaindir.split("/")
dataset = sep[8]
name = sep[-1]
splt_name = name.split("_")
if "uniform" in splt_name and not "timing" in splt_name:
if "uniform" not in chaindir_indices.keys():
chaindir_indices["uniform"] = {}
if "PX" in splt_name and not any(
ephem in ["DE405", "DE421", "DE436", "DE438"] for ephem in splt_name
):
if "vlbi_priors" not in chaindir_indices["uniform"].keys():
chaindir_indices["uniform"]["vlbi_priors"] = defaultdict(list)
chaindir_indices["uniform"]["vlbi_priors"][dataset].append(i)
elif "PX" in splt_name and any(
ephem in ["DE405", "DE421", "DE436", "DE438"] for ephem in splt_name
):
for item in splt_name:
if item in ["DE405", "DE421", "DE436", "DE438"]:
new_item = "vlbi_priors_" + item
if new_item not in chaindir_indices["uniform"].keys():
chaindir_indices["uniform"][new_item] = defaultdict(list)
chaindir_indices["uniform"][new_item][dataset].append(i)
elif "PX" not in splt_name and any(
ephem in ["DE405", "DE421", "DE436", "DE438"] for ephem in splt_name
):
for item in splt_name:
if item in ["DE405", "DE421", "DE436", "DE438"]:
new_item = "parfile_priors_" + item
if new_item not in chaindir_indices["uniform"].keys():
chaindir_indices["uniform"][new_item] = defaultdict(list)
chaindir_indices["uniform"][new_item][dataset].append(i)
else:
if "parfile_priors" not in chaindir_indices["uniform"].keys():
chaindir_indices["uniform"]["parfile_priors"] = defaultdict(list)
chaindir_indices["uniform"]["parfile_priors"][dataset].append(i)
elif "bounded" in splt_name:
if "bounded" not in chaindir_indices.keys():
chaindir_indices["bounded"] = {}
if "PX" in splt_name and not any(
ephem in ["DE405", "DE421", "DE436", "DE438"] for ephem in splt_name
):
if "vlbi_priors" not in chaindir_indices["bounded"].keys():
chaindir_indices["bounded"]["vlbi_priors"] = defaultdict(list)
chaindir_indices["bounded"]["vlbi_priors"][dataset].append(i)
elif "PX" in splt_name and any(
ephem in ["DE405", "DE421", "DE436", "DE438"] for ephem in splt_name
):
for item in splt_name:
if item in ["DE405", "DE421", "DE436", "DE438"]:
new_item = "vlbi_priors_" + item
if new_item not in chaindir_indices["bounded"].keys():
chaindir_indices["bounded"][new_item] = defaultdict(list)
chaindir_indices["bounded"][new_item][dataset].append(i)
elif "PX" not in splt_name and any(
ephem in ["DE405", "DE421", "DE436", "DE438"] for ephem in splt_name
):
for item in splt_name:
if item in ["DE405", "DE421", "DE436", "DE438"]:
new_item = "parfile_priors_" + item
if new_item not in chaindir_indices["bounded"].keys():
chaindir_indices["bounded"][new_item] = defaultdict(list)
chaindir_indices["bounded"][new_item][dataset].append(i)
else:
if "parfile_priors" not in chaindir_indices["bounded"].keys():
chaindir_indices["bounded"]["parfile_priors"] = defaultdict(list)
chaindir_indices["bounded"]["parfile_priors"][dataset].append(i)
else:
if "misc" not in chaindir_indices.keys():
chaindir_indices["misc"] = {}
chaindir_indices["misc"][dataset].append(i)
return chaindir_indices
def get_chain_tmparam_lists(chaindir_list, burn=None):
"""separates list of chains by priors, puts corresponding indices in a list in a dictionary
:chaindir_list:
list, list of chain locations
"""
chain_list = []
tmparam_list = []
for chaindir in chaindir_list:
tmp_tmparam = []
tmp_tmparam.extend(np.loadtxt(chaindir + "/pars.txt", dtype="S").astype("U"))
tmp_tmparam.extend(("lnlike", "lnprior", "chain accept", "pt chain accept"))
tmparam_list.append(tmp_tmparam)
chainpaths = sorted(glob.glob(chaindir + "/chain*.txt"))
chain = pd.read_csv(chainpaths[0], sep="\t", dtype=float, header=None).values
hot_chains = {}
if len(chainpaths) > 1:
for chp in chainpaths[1:]:
try:
ch = pd.read_csv(chp, sep="\t", dtype=float, header=None).values
ky = chp.split("/")[-1].split("_")[-1].replace(".txt", "")
hot_chains.update({ky: ch})
except:
print(chp, "cant be loaded.")
if not isinstance(burn, int):
if burn is None:
burn = int(0.25 * chain.shape[0])
else:
burn = int(burn)
chain_list.append(chain[burn:])
def get_chain_tmparam_dict(chaindir_list, **kwargs):
"""separates list of chains by priors, puts corresponding chains and timing params
in a list in a dictionary.
:chaindir_list:
list, list of chain locations
"""
chain_dict = {}
for chaindir in chaindir_list:
sep = chaindir.split("/")
dataset = sep[8]
name = sep[-1]
splt_name = name.split("_")
tmp_tmparam = []
tmp_tmparam.extend(np.loadtxt(chaindir + "/pars.txt", dtype="S").astype("U"))
tmp_tmparam.extend(("lnlike", "lnprior", "chain accept", "pt chain accept"))
chainpaths = sorted(glob.glob(chaindir + "/chain*.txt"))
chain = pd.read_csv(chainpaths[0], sep="\t", dtype=float, header=None).values
hot_chains = {}
if len(chainpaths) > 1:
for chp in chainpaths[1:]:
try:
ch = pd.read_csv(chp, sep="\t", dtype=float, header=None).values
ky = chp.split("/")[-1].split("_")[-1].replace(".txt", "")
hot_chains.update({ky: ch})
except:
print(chp, "cant be loaded.")
burn = kwargs.get("burn", int(0.25 * chain.shape[0]))
if not isinstance(burn, int):
burn = int(burn)
if "uniform" in splt_name and not "timing" in splt_name:
if "uniform" not in chain_dict.keys():
chain_dict["uniform"] = {}
if "PX" in splt_name and not any(
ephem in ["DE405", "DE421", "DE436", "DE438"] for ephem in splt_name
):
if "vlbi_priors" not in chain_dict["uniform"].keys():
chain_dict["uniform"]["vlbi_priors"] = {}
if dataset not in chain_dict["uniform"]["vlbi_priors"].keys():
chain_dict["uniform"]["vlbi_priors"][dataset] = defaultdict(list)
chain_dict["uniform"]["vlbi_priors"][dataset]["chains"].append(
chain[burn:]
)
chain_dict["uniform"]["vlbi_priors"][dataset]["tmparams"].append(
tmp_tmparam
)
if hot_chains:
chain_dict["uniform"]["vlbi_priors"][dataset][
"hot_chains"
] = hot_chains
elif "PX" in splt_name and any(
ephem in ["DE405", "DE421", "DE436", "DE438"] for ephem in splt_name
):
for item in splt_name:
if item in ["DE405", "DE421", "DE436", "DE438"]:
new_item = "vlbi_priors_" + item
if new_item not in chain_dict["uniform"].keys():
chain_dict["uniform"][new_item] = {}
if dataset not in chain_dict["uniform"][new_item].keys():
chain_dict["uniform"][new_item][dataset] = defaultdict(list)
chain_dict["uniform"][new_item][dataset]["chains"].append(
chain[burn:]
)
chain_dict["uniform"][new_item][dataset]["tmparams"].append(
tmp_tmparam
)
if hot_chains:
chain_dict["uniform"][new_item][dataset][
"hot_chains"
] = hot_chains
elif "PX" not in splt_name and any(
ephem in ["DE405", "DE421", "DE436", "DE438"] for ephem in splt_name
):
for item in splt_name:
if item in ["DE405", "DE421", "DE436", "DE438"]:
new_item = "parfile_priors_" + item
if new_item not in chain_dict["uniform"].keys():
chain_dict["uniform"][new_item] = {}
if dataset not in chain_dict["uniform"][new_item].keys():
chain_dict["uniform"][new_item][dataset] = defaultdict(list)
chain_dict["uniform"][new_item][dataset]["chains"].append(
chain[burn:]
)
chain_dict["uniform"][new_item][dataset]["tmparams"].append(
tmp_tmparam
)
if hot_chains:
chain_dict["uniform"][new_item][dataset][
"hot_chains"
] = hot_chains
else:
if "parfile_priors" not in chain_dict["uniform"].keys():
chain_dict["uniform"]["parfile_priors"] = {}
if dataset not in chain_dict["uniform"]["parfile_priors"].keys():
chain_dict["uniform"]["parfile_priors"][dataset] = defaultdict(list)
chain_dict["uniform"]["parfile_priors"][dataset]["chains"].append(
chain[burn:]
)
chain_dict["uniform"]["parfile_priors"][dataset]["tmparams"].append(
tmp_tmparam
)
if hot_chains:
chain_dict["uniform"]["parfile_priors"][dataset][
"hot_chains"
] = hot_chains
elif "bounded" in splt_name:
if "bounded" not in chain_dict.keys():
chain_dict["bounded"] = {}
if "PX" in splt_name and not any(
ephem in ["DE405", "DE421", "DE436", "DE438"] for ephem in splt_name
):
if "vlbi_priors" not in chain_dict["bounded"].keys():
chain_dict["bounded"]["vlbi_priors"] = {}
if dataset not in chain_dict["bounded"]["vlbi_priors"].keys():
chain_dict["bounded"]["vlbi_priors"][dataset] = defaultdict(list)
chain_dict["bounded"]["vlbi_priors"][dataset]["chains"].append(
chain[burn:]
)
chain_dict["bounded"]["vlbi_priors"][dataset]["tmparams"].append(
tmp_tmparam
)
if hot_chains:
chain_dict["bounded"]["vlbi_priors"][dataset][
"hot_chains"
] = hot_chains
elif "PX" in splt_name and any(
ephem in ["DE405", "DE421", "DE436", "DE438"] for ephem in splt_name
):
for item in splt_name:
if item in ["DE405", "DE421", "DE436", "DE438"]:
new_item = "vlbi_priors_" + item
if new_item not in chain_dict["bounded"].keys():
chain_dict["bounded"][new_item] = {}
if dataset not in chain_dict["bounded"][new_item].keys():
chain_dict["bounded"][new_item][dataset] = defaultdict(list)
chain_dict["bounded"][new_item][dataset]["chains"].append(
chain[burn:]
)
chain_dict["bounded"][new_item][dataset]["tmparams"].append(
tmp_tmparam
)
if hot_chains:
chain_dict["bounded"][new_item][dataset][
"hot_chains"
] = hot_chains
elif "PX" not in splt_name and any(
ephem in ["DE405", "DE421", "DE436", "DE438"] for ephem in splt_name
):
for item in splt_name:
if item in ["DE405", "DE421", "DE436", "DE438"]:
new_item = "parfile_priors_" + item
if new_item not in chain_dict["bounded"].keys():
chain_dict["bounded"][new_item] = {}
if dataset not in chain_dict["bounded"][new_item].keys():
chain_dict["bounded"][new_item][dataset] = defaultdict(list)
chain_dict["bounded"][new_item][dataset]["chains"].append(
chain[burn:]
)
chain_dict["bounded"][new_item][dataset]["tmparams"].append(
tmp_tmparam
)
if hot_chains:
chain_dict["bounded"][new_item][dataset][
"hot_chains"
] = hot_chains
else:
if "parfile_priors" not in chain_dict["bounded"].keys():
chain_dict["bounded"]["parfile_priors"] = {}
if dataset not in chain_dict["bounded"]["parfile_priors"].keys():
chain_dict["bounded"]["parfile_priors"][dataset] = defaultdict(list)
chain_dict["bounded"]["parfile_priors"][dataset]["chains"].append(
chain[burn:]
)
chain_dict["bounded"]["parfile_priors"][dataset]["tmparams"].append(
tmp_tmparam
)
if hot_chains:
chain_dict["bounded"]["parfile_priors"][dataset][
"hot_chains"
] = hot_chains
else:
if dataset not in chain_dict["misc"].keys():
chain_dict["misc"][dataset] = defaultdict(list)
chain_dict["misc"][dataset]["chains"].append(chain[burn:])
chain_dict["misc"][dataset]["tmparams"].append(tmp_tmparam)
if hot_chains:
chain_dict["misc"][dataset]["hot_chains"] = hot_chains
print("\r" + dataset + " " + name + " Loaded. ")
return chain_dict
def get_trimmed_chain_tmparam_dict(chain_dict):
"""Makes all chains in chain_dict the same length"""
trimmed_chain_dict = {}
i = 0
for prior, prior_dict in chain_dict.items():
for px_prior, px_prior_dict in prior_dict.items():
for dataset in px_prior_dict.keys():
for chain in chain_dict[prior][px_prior][dataset]["chains"]:
if i == 0:
min_chain_len_idx = (np.shape(chain)[0], i)
else:
if np.shape(chain)[0] < min_chain_len_idx[0]:
min_chain_len_idx = (np.shape(chain)[0], i)
i += 1
i = 0
for prior, prior_dict in chain_dict.items():
trimmed_chain_dict[prior] = {}
for px_prior, px_prior_dict in prior_dict.items():
trimmed_chain_dict[prior][px_prior] = {}
for dataset in chain_dict[prior][px_prior].keys():
if dataset not in trimmed_chain_dict[prior][px_prior].keys():
trimmed_chain_dict[prior][px_prior][dataset] = defaultdict(list)
for chain, tmparam in zip(
chain_dict[prior][px_prior][dataset]["chains"],
chain_dict[prior][px_prior][dataset]["tmparams"],
):
if i == min_chain_len_idx[1]:
trimmed_chain_dict[prior][px_prior][dataset]["chains"].append(
chain
)
else:
trim = np.shape(chain)[0] - min_chain_len_idx[0]
trimmed_chain_dict[prior][px_prior][dataset]["chains"].append(
chain[trim:]
)
trimmed_chain_dict[prior][px_prior][dataset]["tmparams"].append(
tmparam
)
i += 1
return trimmed_chain_dict
def get_combined_arviz_obj_from_list(chain_list):
comb_chain_dict = {}
for i, chain in enumerate(chain_list):
print(np.shape(chain))
for j, par in enumerate(tmparam_list[i]):
if par.split("_")[0] in [psr.name for psr in psrs]:
if list(par)[0] != "J":
par = "J" + par
if par in comb_chain_dict.keys():
comb_chain_dict[par] = np.concatenate(
(comb_chain_dict[par], [chain[:, j]]), axis=0
)
else:
comb_chain_dict[par] = [chain[:, j]]
return az.convert_to_inference_data(comb_chain_dict)
def get_combined_arviz_obj_from_dict(chain_dict, psrs, return_dict=False):
comb_chain_dict = {}
for prior, prior_dict in chain_dict.items():
for px_prior, px_prior_dict in prior_dict.items():
for dataset, dataset_dict in px_prior_dict.items():
for i, chain in enumerate(
chain_dict[prior][px_prior][dataset]["chains"]
):
print(np.shape(chain))
for j, par in enumerate(
chain_dict[prior][px_prior][dataset]["tmparams"][i]
):
if par.split("_")[0] in [psr.name for psr in psrs]:
if list(par)[0] != "J":
par = "J" + par
if par in comb_chain_dict.keys():
try:
comb_chain_dict[par] = np.concatenate(
(comb_chain_dict[par], [chain[:, j]]), axis=0
)
except:
print(
"Exluding ",
prior,
px_prior,
dataset,
par,
"because its chain length {} doesnt match {}".format(
np.shape(chain[:, j]),
np.shape(comb_chain_dict[par][0]),
),
)
else:
comb_chain_dict[par] = [chain[:, j]]
if return_dict:
return az.convert_to_inference_data(comb_chain_dict), comb_chain_dict
else:
return az.convert_to_inference_data(comb_chain_dict)
def get_rescaled_chain_dict(
chains, tmparams, pardict, dataset, old_dict=None, px_priors=False
):
if not isinstance(chains, np.ndarray):
chains = np.asarray(chains)
if not isinstance(tmparams, np.ndarray):
tmparams = np.array(tmparams)
rescaled_chain_dict = {}
for i in range(np.shape(tmparams)[0]):
for j, par in enumerate(tmparams[i]):
split_string = par.split("_")
psr_name = split_string[0]
if "timing" in split_string:
if "DMX" in split_string:
og_par = ("_").join(split_string[-2:])
else:
og_par = split_string[-1]
if px_priors and og_par == "PX":
rescaled_chain = chains[i][:, j]
else:
rescaled_chain = (
chains[i][:, j] * pardict[dataset][psr_name][og_par]["err"]
+ pardict[dataset][psr_name][og_par]["val"]
)
else:
if psr_name in ["lnlike", "lnprior", "chain accept", "pt chain accept"]:
og_par = par
rescaled_chain = chains[i][:, j]
else:
if list(par)[0] != "J":
og_par = "J" + par
else:
og_par = par
rescaled_chain = chains[i][:, j]
if og_par in rescaled_chain_dict.keys():
rescaled_chain_dict[og_par] = np.concatenate(
(rescaled_chain_dict[og_par], [rescaled_chain]), axis=0
)
else:
rescaled_chain_dict[og_par] = [rescaled_chain]
if old_dict is not None:
for old_key in old_dict.keys():
if old_key in rescaled_chain_dict.keys():
rescaled_chain_dict[old_key] = np.concatenate(
(rescaled_chain_dict[old_key], old_dict[old_key]), axis=0
)
else:
rescaled_chain_dict[old_key] = old_dict[old_key]
return rescaled_chain_dict
def get_combined_rescaled_chain_dict(chain_dict, pardict, dataset, **kwargs):
px_priors = kwargs.get("px_priors", [])
if "priors" not in kwargs.keys():
priors = []
if len(px_priors) == 0:
for p in chain_dict.keys():
for px_p in chain_dict[p].keys():
if dataset in chain_dict[p][px_p].keys():
if p not in priors:
priors.append(p)
if px_p not in px_priors:
px_priors.append(px_p)
else:
print(dataset, "not in", p, px_p, chain_dict[p][px_p].keys())
else:
for p in chain_dict.keys():
for px_p in px_priors:
if px_p in chain_dict[p].keys():
if dataset in chain_dict[p][px_p].keys():
if p not in priors:
priors.append(p)
else:
print(
dataset, "not in", p, px_p, chain_dict[p][px_p].keys()
)
else:
print(px_p, "not in", p, chain_dict[p].keys())
else:
priors = kwargs["priors"]
rescaled_chain_dict = {}
for prior in priors:
for px_prior in px_priors:
if px_prior in chain_dict[prior].keys():
for tmparams, chains in zip(
chain_dict[prior][px_prior][dataset]["tmparams"],
chain_dict[prior][px_prior][dataset]["chains"],
):
if "vlbi" in px_prior.split("_"):
if not rescaled_chain_dict:
rescaled_chain_dict = get_rescaled_chain_dict(
[chains], [tmparams], pardict, dataset, px_priors=True
)
else:
rescaled_chain_dict_comb = get_rescaled_chain_dict(
[chains],
[tmparams],
pardict,
dataset,
px_priors=True,
old_dict=rescaled_chain_dict,
)
del rescaled_chain_dict
rescaled_chain_dict = rescaled_chain_dict_comb
else:
if not rescaled_chain_dict:
rescaled_chain_dict = get_rescaled_chain_dict(
[chains], [tmparams], pardict, dataset
)
else:
rescaled_chain_dict_comb = get_rescaled_chain_dict(
[chains],
[tmparams],
pardict,
dataset,
old_dict=rescaled_chain_dict,
)
del rescaled_chain_dict
rescaled_chain_dict = rescaled_chain_dict_comb
else:
print(
px_prior,
"not in",
prior,
"dict:",
chain_dict[prior].keys(),
)
return rescaled_chain_dict_comb
def get_rescaled_chains_as_core_list(chain_dict, label, burn=0):
common_dict = {}
max_size = (0, 0)
for val in chain_dict.values():
size = np.shape(val)
if np.shape(val)[0] > max_size[0]:
max_size = size
for key, val in chain_dict.items():
if np.shape(val) == max_size:
common_dict[key] = val
else:
print(
"Exluding ",
key,
" because its shape {} doesnt match others".format(np.shape(val)),
)
param_list = list(common_dict.keys())
[num_chains, len_chains] = np.shape(common_dict[param_list[0]])
core_list = []
for i in range(num_chains):
if i != 0:
del stacked_chains
for chains in common_dict.values():
if "stacked_chains" not in locals():
stacked_chains = [chains[i]]
else:
stacked_chains = np.concatenate((stacked_chains, [chains[i]]), axis=0)
core_list.append(
co.Core(
label="{}".format(label),
chain=stacked_chains.T,
params=param_list,
burn=burn,
)
)
return core_list
def plot_common_chains(
core_list, chaindir_list, priors, px_priors, dataset, plot_kwargs={}, misc_kwargs={}
):
"""Uses la_forge to plot chains
:prior:
str, {'uniform','bounded'}
:px_prior:
str, {'px_priors','other'}
:dataset:
str, {'5yr','9yr','11yr'}
"""
if "hist" not in plot_kwargs.keys():
plot_kwargs["hist"] = True
if "ncols" not in plot_kwargs.keys():
plot_kwargs["ncols"] = 4
if "bins" not in plot_kwargs.keys():
plot_kwargs["bins"] = 10
if "hist_kwargs" not in plot_kwargs.keys():
plot_kwargs["hist_kwargs"] = dict(fill=False)
legend_labels = misc_kwargs.get("legend_labels", [])
if legend_labels:
user_legend = True
else:
user_legend = False
legend_loc = misc_kwargs.get("legend_loc", None)
if isinstance(priors, str):
priors = [priors]
if isinstance(px_priors, str):
px_priors = [px_priors]
if isinstance(dataset, str):
datasets = [dataset]
else:
datasets = dataset
chaindir_indices = get_chaindir_indices(chaindir_list)
if len(priors) == 1 and len(px_priors) == 1:
if np.shape(chaindir_indices[priors[0]][px_priors[0]][datasets[0]])[0] > 1:
common_pars = []
for i in range(len(chaindir_indices[priors[0]][px_priors[0]][datasets[0]])):
if i == 0:
for param in core_list[
chaindir_indices[priors[0]][px_priors[0]][datasets[0]][i]
].params:
if param not in [
"lnlike",
"lnprior",
"chain_accept",
"pt_chain_accept",
"chain accept",
"pt chain accept",
]:
common_pars.append(param)
else:
noncommon_pars = []
for com_par in common_pars:
if (
com_par
not in core_list[
chaindir_indices[priors[0]][px_priors[0]][datasets[0]][
i
]
].params
):
noncommon_pars.append(com_par)
for ncom_par in noncommon_pars:
del common_pars[common_pars.index(ncom_par)]
if not user_legend:
legend_labels.append(
datasets[0]
+ ": "
+ priors[0]
+ " prior, "
+ px_priors[0]
+ " v{}".format(i + 1)
)
titles = []
for cp in common_pars:
if "timing" in cp.split("_"):
if "DMX" in cp.split("_"):
titles.append(("_").join(cp.split("_")[-2:]))
else:
titles.append(cp.split("_")[-1])
else:
if "J" in cp.split("_")[0]:
titles.append((" ").join(cp.split("_")[1:]))
else:
titles.append(cp)
dg.plot_chains(
[
core_list[x]
for x in chaindir_indices[priors[0]][px_priors[0]][datasets[0]]
],
pars=common_pars,
titles=titles,
legend_labels=legend_labels,
legend_loc=legend_loc,
**plot_kwargs
)
else:
if not user_legend:
legend_labels.append(
datasets[0] + ": " + priors[0] + " prior, " + px_priors[0]
)
titles = []
for param in core_list[0].params:
if "timing" in param.split("_"):
if "DMX" in param.split("_"):
titles.append(("_").join(param.split("_")[-2:]))
else:
titles.append(param.split("_")[-1])
else:
if "J" in param.split("_")[0]:
titles.append((" ").join(param.split("_")[1:]))
else:
titles.append(param)
dg.plot_chains(
core_list[0],
titles=titles,
legend_labels=legend_labels,
legend_loc=legend_loc,
**plot_kwargs
)
else:
common_pars = []
for prior in priors:
if prior in chaindir_indices.keys():
for px_prior in px_priors:
if px_prior in chaindir_indices[prior].keys():
for dataset in datasets:
if dataset in chaindir_indices[prior][px_prior].keys():
if len(common_pars) == 0:
comb_indices = chaindir_indices[prior][px_prior][
dataset
]
for idx1 in comb_indices:
if len(common_pars) == 0:
for core_param in core_list[idx1].params:
if core_param not in [
"lnlike",
"lnprior",
"chain_accept",
"pt_chain_accept",
"chain accept",
"pt chain accept",
]:
common_pars.append(core_param)
else:
noncommon_pars = []
for com_par in common_pars:
if (
com_par
not in core_list[idx1].params
):
noncommon_pars.append(com_par)
for ncom_par in noncommon_pars:
del common_pars[
common_pars.index(ncom_par)
]
else:
comb_indices = sorted(
np.concatenate(
(
comb_indices,
chaindir_indices[prior][px_prior][
dataset
],
),
axis=0,
)
)
for idx2 in comb_indices:
noncommon_pars = []
for com_par in common_pars:
if com_par not in core_list[idx2].params:
noncommon_pars.append(com_par)
for ncom_par in noncommon_pars:
del common_pars[common_pars.index(ncom_par)]
else:
print(
dataset,
"not in",
prior,
"[",
px_prior,
"] dict:",
chaindir_indices[prior][px_prior].keys(),
)
else:
print(
px_prior,
"not in",
prior,
"dict:",
chaindir_indices[prior].keys(),
)
else:
print(prior, "not in", chaindir_indices.keys())
if not user_legend:
for i in comb_indices:
for prior in chaindir_indices.keys():
for px_prior in chaindir_indices[prior].keys():
for dataset in chaindir_indices[prior][px_prior].keys():
if i in chaindir_indices[prior][px_prior][dataset]:
j = 1
label = (
dataset
+ ": "
+ prior
+ " prior, "
+ px_prior
+ " v{}".format(j)
)
if label in legend_labels:
versioning = True
j += 1
while versioning is True:
new_label = (
dataset
+ ": "
+ prior
+ " prior, "
+ px_prior
+ " v{}".format(j)
)
if new_label not in legend_labels:
legend_labels.append(new_label)
versioning = False
j += 1
else:
legend_labels.append(label)
titles = []
for cp in common_pars:
if "timing" in cp.split("_"):
if "DMX" in cp.split("_"):
titles.append(("_").join(cp.split("_")[-2:]))
else:
titles.append(cp.split("_")[-1])
else:
if "J" in cp.split("_")[0]:
titles.append((" ").join(cp.split("_")[1:]))
else:
titles.append(cp)
dg.plot_chains(
[core_list[x] for x in comb_indices],
titles=titles,
pars=common_pars,
legend_labels=legend_labels,
legend_loc=legend_loc,
**plot_kwargs
)