-
Notifications
You must be signed in to change notification settings - Fork 4
/
common.py
1184 lines (1065 loc) · 52.9 KB
/
common.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
import in_out as ino
import ConfigParser
import os as os
import sys as sys
import tracers as trc
import fisher_plot as fsh
import time
fs=16
lw=2
PARS_LCDM={'och2':[0.1197 ,0.001 , 0,'$\\omega_c$'],
'obh2':[0.02222,0.0001, 0,'$\\omega_b$'],
'hh' :[0.67 ,0.01 , 0,'$h$'],
'ns' :[0.96 ,0.01 , 0,'$n_s$'],
'A_s' :[2.19 ,0.01 , 0,'$A_s$'],
'tau' :[0.06 ,0.005 , 0,'$\\tau$'],
'mnu' :[60.0 ,5.0 , 0,'$\\sum m_\\nu$'],
'nnu' :[2.046 ,0.1 , 0,'$\\rm{N_{eff}}$'],
'pan' :[0.00 ,0.5 , 1,'$p_{\\rm ann}$'],
'fnl' :[0.00 ,0.5 , 0,'$f_{\\rm NL}$'],
'rt' :[0.00 ,0.001 , 1,'$r$'],
'lmcb':[14.08 ,0.3 , 0,'$\\log_{10}M_b$'],
'etab':[0.5 ,0.1 , 0,'$\\eta_b$'],
'lksb':[1.7404 ,0.2 , 0,'$\\log_{10}k_s$']}
PARS_WCDM={'w0' :[-1.00 ,0.01 , 1,'$w_0$'],
'wa' :[0.00 ,0.01 , 1,'$w_a$'],
'ok' :[0 ,0.0 , 0,'$\\Omega_k$']}
PARS_JBD ={'obd' :[0.10 ,0.03 , 0,'$10^4/\\omega_{\\rm BD}$']}
PARS_HORN={'bk' :[ 0.00 ,0.05 , 1,'$b_K$'],
'bb' :[ 0.00 ,0.01 ,-1,'$b_B$'],
'bm' :[ 0.00 ,0.007 , 1,'$b_M$'],
'bt' :[ 0.00 ,0.02 ,-1,'$b_T$'],
'ck' :[ 0.10 ,0.05 , 0,'$c_K$'],
'cb' :[ 0.05 ,0.01 , 0,'$c_B$'],
'cm' :[-0.05 ,0.007 , 0,'$c_M$'],
'ct' :[-0.05 ,0.01 , 0,'$c_T$'],
'dk' :[ 0.00 ,0.02 , 0,'$d_K$'],
'db' :[ 0.00 ,0.02 , 0,'$d_B$'],
'dm' :[ 0.00 ,0.02 , 0,'$d_M$'],
'dt' :[ 0.00 ,0.02 , 0,'$d_T$'],
'm2i' :[ 1.0 ,0.05 , 0,'$M^2_*$'],
'lkv' :[ -1. ,0.15 , 0,'$\\log_{10}k_V$']}
LMAX=10000
LMAX_CMB=10000
NLB=1
class GaussSt(object) :
def __init__(self,nmaps) :
self.nmaps=nmaps
self.nvec=(self.nmaps*(self.nmaps+1))/2
self.ind_2d=np.zeros([self.nvec,2],dtype=int)
id1d=0
for i in np.arange(self.nmaps) :
for j in np.arange(self.nmaps-i) :
self.ind_2d[id1d,:]=[j,i+j]
id1d+=1
def gaussian_covariance(self,mat,weight) :
covar=np.zeros([self.nvec,self.nvec])
for iv1 in np.arange(self.nvec) :
i_a=self.ind_2d[iv1,0]; i_b=self.ind_2d[iv1,1];
for iv2 in np.arange(self.nvec) :
i_c=self.ind_2d[iv2,0]; i_d=self.ind_2d[iv2,1];
covar[iv1,iv2]=(mat[i_a,i_c]*mat[i_b,i_d]+mat[i_a,i_d]*mat[i_b,i_c])
covar*=weight
return covar
def unwrap_matrix(self,mat) :
return np.array([mat[ind[0],ind[1]] for ind in self.ind_2d])
class ParamRun:
""" Run parameters """
#Cosmological parameters
params_all=[] #
#Run parameters
lmax=LMAX #
lmax_lss=LMAX #
lmax_cmb=LMAX_CMB
lmin_limber=LMAX #
has_gal_clustering=False
has_gal_shear=False
has_cmb_lensing=False
has_cmb_t=False
has_cmb_p=False
output_dir="test_dir" #
output_spectra="test_prefix" #
output_fisher="test_fisher" #
output_path="test_dir/test_prefix" #
exec_path="./classt" #
fsky=1.0
just_run_cls=False
#Bin parameters
nbins_gal_clustering=0 #
nbins_gal_clustering_read=0 #
nbins_gal_shear=0 #
nbins_gal_shear_read=0 #
nbins_cmb_primary=0 #
nbins_cmb_primary_read=0 #
nbins_cmb_lensing=0 #
nbins_cmb_lensing_read=0 #
nbins_total=1 #
n_tracers=0 #
tracers=[] #
#Behavioral flags
model='LCDM' #
do_pspec_fisher=False #
save_cl_files=True #
save_param_files=True #
save_dbg_files=False
include_alignment=False #
include_rsd=True #
include_magnification=True #
include_gr_vel=False #
include_gr_pot=False #
use_nonlinear=False
use_baryons=False
plot_ext=".pdf"
include_im_fg=False
fit_im_fg=False
#Bias file
bias_file='none'
#BAO
n_bao=0
include_BAO=False
include_DA=False
include_HH=False
include_DV=False
z_nodes_DA=[]
z_nodes_HH=[]
z_nodes_DV=[]
e_nodes_DA=[]
e_nodes_HH=[]
e_nodes_DV=[]
#Terms to include for clustering
terms_gc="density" #
terms_gs="lensing_shear" #
#Power spectra
cl_noise_arr=[]
cl_fid_arr=[]
dcl_arr=[]
#Power spectrum from bias file
cl_mod_arr=[]
#Fisher param structure
params_fshr=[] #
#Number of parameters to vary
npar_vary=0
#Fisher matrix
fshr_l=[]
fshr_cls=[]
fshr_bao=[]
fshr_prior=[]
fshr=[]
fshr_bias=[]
fshr_bias_l=[]
def __init__(self,fname) :
#Read parameter file
# Reset lists
self.tracers = []
self.params_fshr = []
self.read_param_file(fname)
if (self.has_cmb_lensing==False) and (self.has_cmb_t==False) and (self.has_cmb_p==False) :
self.lmax_cmb=0
if (self.has_gal_shear==False) and (self.has_gal_clustering==False) :
self.lmax_lss=0
self.lmax=max(self.lmax_lss,self.lmax_cmb)
self.lmax=NLB*((self.lmax+1)/NLB)-1
#List parameters to vary over
self.npar_vary=0
for p in self.params_all :
if p.isfree==True :
self.params_fshr.append(fsh.ParamFisher(p.val,p.dval,p.prior,p.name,
p.label,p.isfree,p.do_plot,p.onesided))
self.npar_vary+=1
self.params_fshr=np.array(self.params_fshr)
#Read bins information for each tracer
self.nbins_total=0
self.nbins_gal_clustering=0
self.nbins_gal_clustering_read=0
self.nbins_gal_shear=0
self.nbins_gal_shear_read=0
self.nbins_cmb_lensing=0
self.nbins_cmb_lensing_read=0
self.nbins_cmb_primary=0
self.nbins_cmb_primary_read=0
self.include_m_bias = False
self.npar_mbias = 0
for tr in self.tracers :
nbins=tr.get_nbins()
if (tr.tracer_type=="gal_clustering") or (tr.tracer_type=="intensity_mapping") :
self.nbins_gal_clustering_read+=nbins
if tr.consider_tracer :
self.nbins_gal_clustering+=nbins
elif tr.tracer_type=="gal_shear" :
self.nbins_gal_shear_read+=nbins
if tr.consider_tracer :
self.nbins_gal_shear+=nbins
if tr.include_m_bias:
self.include_m_bias = True
self.npar_mbias += nbins
self.m_step = tr.m_step
else:
self.include_m_bias = False
self.npar_mbias = 0
elif tr.tracer_type=="cmb_lensing" :
if self.nbins_cmb_lensing==1 :
sys.exit("You can only have 1 CMB lensing!")
else :
self.nbins_cmb_lensing_read=1
if tr.consider_tracer :
self.nbins_cmb_lensing=1
elif tr.tracer_type=="cmb_primary" :
if self.nbins_cmb_primary>0 :
sys.exit("You can only have 1 CMB primary!")
else :
if tr.has_t :
self.nbins_cmb_primary_read+=1
if tr.has_p :
self.nbins_cmb_primary_read+=2
if tr.consider_tracer :
if tr.has_t :
self.nbins_cmb_primary+=1
if tr.has_p :
self.nbins_cmb_primary+=2
else :
strout="Wrong tracer type "+tr.tracer_type+"\n"
strout+="Allowed tracer types are: gal_clustering, intensity_mapping"
strout+=", gal_shear, cmb_lensing, cmb_primary\n"
sys.exit(strout)
if tr.consider_tracer :
self.nbins_total+=nbins
#Terms to include
if self.include_alignment==True :
self.terms_gs+=", intrinsic_alignment"
if self.include_rsd==True :
self.terms_gc+=", rsd1"
if self.include_magnification==True :
self.terms_gc+=", lensing"
if self.include_gr_vel==True :
self.terms_gc+=", rsd2, rsd3"
if self.include_gr_pot==True :
self.terms_gc+=", gr1, gr2, gr3, gr4, gr5"
#Allocate power spectra
self.cl_fid_arr=np.zeros([(self.lmax+1)/NLB,
self.nbins_total,self.nbins_total])
self.cl_noise_arr=np.zeros([(self.lmax+1)/NLB,
self.nbins_total,self.nbins_total])
self.dcl_arr=np.zeros([self.npar_vary+self.npar_mbias,(self.lmax+1)/NLB,
self.nbins_total,self.nbins_total])
# Allocate power spectrum from additional bias file
self.cl_mod_arr=np.zeros([(self.lmax+1)/NLB,self.nbins_total,self.nbins_total])
#Allocate Fisher matrix
self.fshr_l=np.zeros([self.npar_vary+self.npar_mbias,self.npar_vary+self.npar_mbias,self.lmax+1])
self.fshr_cls=np.zeros([self.npar_vary+self.npar_mbias,self.npar_vary+self.npar_mbias])
self.fshr_bao=np.zeros([self.npar_vary+self.npar_mbias,self.npar_vary+self.npar_mbias])
self.fshr_prior=np.zeros([self.npar_vary+self.npar_mbias,self.npar_vary+self.npar_mbias])
self.fshr=np.zeros([self.npar_vary+self.npar_mbias,self.npar_vary+self.npar_mbias])
self.fshr_bias_l=np.zeros([self.npar_vary+self.npar_mbias,self.lmax+1])
self.fshr_bias =np.zeros([self.npar_vary+self.npar_mbias])
self.print_params()
def print_params(self) :
print " <><> Run parameters"
print " - Overall l_max = %d"%(self.lmax)
if self.has_gal_clustering or self.has_gal_shear :
print " - LSS l_max = %d"%(self.lmax_lss)
if self.has_cmb_lensing or self.has_cmb_t or self.has_cmb_p :
print " - CMB l_max = %d"%(self.lmax_cmb)
print " - Output directory : "+self.output_dir
print " - Output spectra : "+self.output_path
print " - Output Fisher : "+self.output_fisher
if self.has_gal_clustering :
print " - %d"%(self.nbins_gal_clustering)+" (%d) bins used (read) for galaxy clustering"%(self.nbins_gal_clustering_read)
if self.has_gal_shear :
print " - %d"%(self.nbins_gal_shear)+" (%d) bins used (read) for galaxy shear"%(self.nbins_gal_shear_read)
if self.has_cmb_t or self.has_cmb_p :
print " - %d"%(self.nbins_cmb_primary)+" (%d) bins used (read) for CMB primary"%(self.nbins_cmb_primary_read)
if self.has_cmb_lensing :
print " - %d"%(self.nbins_cmb_lensing)+" (%d) bins used (read) for CMB lensing"%(self.nbins_cmb_lensing_read)
print " - %d bins used in total"%(self.nbins_total)
print " - %d tracers read : "%(self.n_tracers)
itr=1
for tr in self.tracers :
print " %d. "%itr+tr.name+". Type: "+tr.tracer_type+", %d bins."%(tr.nbins)
itr+=1
print " - Overlaping sky fraction : %.3lf"%(self.fsky)
if self.has_gal_clustering :
print " - Terms included for galaxy clustering : "+self.terms_gc
if self.has_gal_shear :
print " - Terms included for galaxy shear : "+self.terms_gs
print " - %d free parameters :"%(self.npar_vary+self.npar_mbias)
for par in self.params_fshr :
print " * "+par.name+" : %.3lf"%(par.val)
if self.include_m_bias:
print " * "+str(self.npar_mbias) + " multiplicative bias parameters"
def read_param_file(self,fname) :
config=ConfigParser.SafeConfigParser()
config.read(fname)
#Behaviour parameters
if config.has_option('Behaviour parameters','model') :
self.model=config.get('Behaviour parameters','model')
if config.has_option('Behaviour parameters','do_pspec_fisher') :
self.do_pspec_fisher=config.getboolean('Behaviour parameters','do_pspec_fisher')
if config.has_option('Behaviour parameters','save_cl_files') :
self.save_cl_files=config.getboolean('Behaviour parameters','save_cl_files')
if config.has_option('Behaviour parameters','save_param_files') :
self.save_param_files=config.getboolean('Behaviour parameters',
'save_param_files')
if config.has_option('Behaviour parameters','just_run_cls') :
self.just_run_cls=config.getboolean('Behaviour parameters',
'just_run_cls')
#Bias file
if config.has_option('Bias file','bias_file') :
self.bias_file=config.get('Bias file','bias_file')
#Cosmological parameters
self.params_all=[]
def add_to_params(pars) :
for pname in pars.keys() :
x=pars[pname][0]
dx=pars[pname][1]
prior=0.
isfree=False
onesided=pars[pname][2]
if config.has_section(pname) :
if config.has_option(pname,'x') :
x=config.getfloat(pname,'x')
if config.has_option(pname,'dx') :
dx=config.getfloat(pname,'dx')
if config.has_option(pname,'prior') :
prior=config.getfloat(pname,'prior')
if config.has_option(pname,'is_free') :
isfree=config.getboolean(pname,'is_free')
if config.has_option(pname,'onesided') :
onesided=config.getint(pname,'onesided')
self.params_all.append(fsh.ParamFisher(x,dx,prior,pname,pars[pname][3],isfree,isfree*True,onesided))
add_to_params(PARS_LCDM)
if self.model=='LCDM' :
pass
if self.model=='wCDM' :
add_to_params(PARS_WCDM)
elif self.model=='JBD' :
add_to_params(PARS_JBD)
elif self.model=='Horndeski' :
add_to_params(PARS_WCDM)
add_to_params(PARS_HORN)
#CLASS parameters
if config.has_option('CLASS parameters','lmax_lss') :
self.lmax_lss=config.getint('CLASS parameters','lmax_lss')
if config.has_option('CLASS parameters','lmax_cmb') :
self.lmax_cmb=config.getint('CLASS parameters','lmax_cmb')
if config.has_option('CLASS parameters','lmin_limber') :
self.lmin_limber=config.getfloat('CLASS parameters','lmin_limber')
if config.has_option('CLASS parameters','include_alignment') :
self.include_alignment=config.getboolean('CLASS parameters','include_alignment')
if config.has_option('CLASS parameters','include_rsd') :
self.include_rsd=config.getboolean('CLASS parameters','include_rsd')
if config.has_option('CLASS parameters','include_magnification') :
self.include_magnification=config.getboolean('CLASS parameters','include_magnification')
if config.has_option('CLASS parameters','include_gr_vel') :
self.include_gr_vel=config.getboolean('CLASS parameters','include_gr_vel')
if config.has_option('CLASS parameters','include_gr_pot') :
self.include_gr_pot=config.getboolean('CLASS parameters','include_gr_pot')
if config.has_option('CLASS parameters','exec_path') :
self.exec_path=config.get('CLASS parameters','exec_path')
if config.has_option('CLASS parameters','use_nonlinear') :
self.use_nonlinear=config.getboolean('CLASS parameters','use_nonlinear')
if config.has_option('CLASS parameters','use_baryons') :
self.use_baryons=config.getboolean('CLASS parameters','use_baryons')
if config.has_option('CLASS parameters','f_sky') :
self.fsky=config.getfloat('CLASS parameters','f_sky')
#Output parameters
if config.has_option('Output parameters','output_dir') :
self.output_dir=config.get('Output parameters','output_dir')
if config.has_option('Output parameters','output_spectra') :
self.output_spectra=config.get('Output parameters','output_spectra')
if config.has_option('Output parameters','output_fisher') :
self.output_fisher=config.get('Output parameters','output_fisher')
#Form output prefix and create output directory
self.output_path=self.output_dir+"/"+self.output_spectra
os.system("mkdir -p "+self.output_dir+"/"+self.output_fisher)
#BAO
if config.has_section("BAO 1") :
self.include_BAO=True
else :
self.include_BAO=False
self.n_bao=0
self.relative_bao_errors=False
while config.has_section("BAO %d"%(self.n_bao+1)) :
self.n_bao+=1
sec_title="BAO %d"%(self.n_bao)
if config.has_option(sec_title,'fname_da') :
self.include_DA=True
zn,eda=np.loadtxt(config.get(sec_title,'fname_da'),unpack=True)
self.z_nodes_DA.extend(zn)
self.e_nodes_DA.extend(eda)
if config.has_option(sec_title,'fname_hh') :
self.include_HH=True
zn,ehh=np.loadtxt(config.get(sec_title,'fname_hh'),unpack=True)
self.z_nodes_HH.extend(zn)
self.e_nodes_HH.extend(ehh)
if config.has_option(sec_title,'fname_dv') :
self.include_DV=True
zn,edv=np.loadtxt(config.get(sec_title,'fname_dv'),unpack=True)
self.z_nodes_DV.extend(zn)
self.e_nodes_DV.extend(edv)
if config.has_option(sec_title,'use_relative_errors') :
self.relative_bao_errors=config.getboolean(sec_title,'use_relative_errors')
self.z_nodes_DA=np.array(self.z_nodes_DA)
self.z_nodes_HH=np.array(self.z_nodes_HH)
self.z_nodes_DV=np.array(self.z_nodes_DV)
self.e_nodes_DA=np.array(self.e_nodes_DA)
self.e_nodes_HH=np.array(self.e_nodes_HH)
self.e_nodes_DV=np.array(self.e_nodes_DV)
#Tracers
if not config.has_section("Tracer 1") :
print "The param file contains no Tracers"
# sys.exit("The param file must contain at least one tracer starting with Tracer 1")
n_tracers=0
while config.has_section("Tracer %d"%(n_tracers+1)) :
n_tracers+=1
sec_title="Tracer %d"%n_tracers
#Generic
consider_tracer=True; lmax=self.lmax; lmin=0;
name=config.get(sec_title,'tracer_name')
tr_type=config.get(sec_title,'tracer_type')
if config.has_option(sec_title,'use_tracer') :
consider_tracer=config.getboolean(sec_title,'use_tracer')
if config.has_option(sec_title,'lmin') :
lmin=max(lmin,config.getint(sec_title,'lmin'))
if config.has_option(sec_title,'lmax') :
lmax=min(lmax,config.getint(sec_title,'lmax'))
#CMB primary or lensing
has_t=False; has_p=False; sigma_t=None; sigma_p=None; beam_amin=None; l_transition=None;
if config.has_option(sec_title,'has_t') :
has_t=config.getboolean(sec_title,'has_t')
if config.has_option(sec_title,'has_p') :
has_p=config.getboolean(sec_title,'has_p')
if config.has_option(sec_title,'beam_amin') :
beamstr=config.get(sec_title,'beam_amin')
beam_amin=np.array(map(float,beamstr.split()))
if config.has_option(sec_title,'sigma_t') :
sigmastr=config.get(sec_title,'sigma_t')
sigma_t=np.array(map(float,sigmastr.split()))
if config.has_option(sec_title,'sigma_p') :
sigmastr=config.get(sec_title,'sigma_p')
sigma_p=np.array(map(float,sigmastr.split()))
if config.has_option(sec_title,'l_transition') :
l_transition=config.getint(sec_title,'l_transition')
#Galaxy clustering, lensing or intensity mapping
bins_file=None; nz_file=None;
if config.has_option(sec_title,'bins_file') :
bins_file=config.get(sec_title,'bins_file')
if config.has_option(sec_title,'nz_file') :
nz_file=config.get(sec_title,'nz_file')
#Galaxy clustering or intensity mapping
bias_file=None; sbias_file=None; ebias_file=None;
if config.has_option(sec_title,'bias_file') :
bias_file=config.get(sec_title,'bias_file')
if config.has_option(sec_title,'sbias_file') :
sbias_file=config.get(sec_title,'sbias_file')
if config.has_option(sec_title,'ebias_file') :
ebias_file=config.get(sec_title,'ebias_file')
#Galaxy clustering
is_photometric=True
if config.has_option(sec_title,'is_photometric') :
is_photometric=config.getboolean(sec_title,'is_photometric')
bphz_prior=0 # Prior on photo-zs
if config.has_option(sec_title,'bphz_prior') :
bphz_prior_str=config.get(sec_title,'bphz_prior')
bphz_prior=np.array(map(float,bphz_prior_str.split()))
#Galaxy lensing
sigma_gamma=None; abias_file=None; rfrac_file=None; include_m_bias=None; m_step=None;
if config.has_option(sec_title,'sigma_gamma') :
sigma_gamma=config.getfloat(sec_title,'sigma_gamma')
if config.has_option(sec_title,'abias_file') :
abias_file=config.get(sec_title,'abias_file')
if config.has_option(sec_title,'rfrac_file') :
rfrac_file=config.get(sec_title,'rfrac_file')
if config.has_option(sec_title,'include_m_bias') :
include_m_bias=config.getboolean(sec_title,'include_m_bias')
if config.has_option(sec_title,'m_step') :
m_step_str=config.get(sec_title,'m_step')
m_step=float(m_step_str)
#Intensity mapping
tz_file=None; dish_size=None; t_inst=None; t_total=None; n_dish=None;
area_efficiency=None; fsky_im=None; im_type=None; base_file=None;
baseline_min=None; baseline_max=None;
include_fg=False; fit_fg=False;
a_fg=None; alp_fg=None; bet_fg=None; xi_fg=None; nux_fg=None; lx_fg=None;
if config.has_option(sec_title,'tz_file') :
tz_file=config.get(sec_title,'tz_file')
if config.has_option(sec_title,'dish_size') :
dish_size=config.getfloat(sec_title,'dish_size')
if config.has_option(sec_title,'t_inst') :
t_inst=config.getfloat(sec_title,'t_inst')
if config.has_option(sec_title,'t_total') :
t_total=config.getfloat(sec_title,'t_total')
if config.has_option(sec_title,'n_dish') :
n_dish=config.getint(sec_title,'n_dish')
if config.has_option(sec_title,'area_efficiency') :
area_efficiency=config.getfloat(sec_title,'area_efficiency')
if config.has_option(sec_title,'fsky_im') :
fsky_im=config.getfloat(sec_title,'fsky_im')
if config.has_option(sec_title,'instrument_type') :
im_type=config.get(sec_title,'instrument_type')
if config.has_option(sec_title,'base_file') :
base_file=config.get(sec_title,'base_file')
if config.has_option(sec_title,'baseline_min') :
baseline_min=config.getfloat(sec_title,'baseline_min')
if config.has_option(sec_title,'baseline_max') :
baseline_max=config.getfloat(sec_title,'baseline_max')
if config.has_option(sec_title,'include_foregrounds') :
include_fg=config.getboolean(sec_title,'include_foregrounds')
if include_fg :
if config.has_option(sec_title,'fit_foregrounds') :
fit_fg=config.getboolean(sec_title,'fit_foregrounds')
if config.has_option(sec_title,'A_fg') :
a_fg=config.getfloat(sec_title,'A_fg')
if config.has_option(sec_title,'alpha_fg') :
alp_fg=config.getfloat(sec_title,'alpha_fg')
if config.has_option(sec_title,'beta_fg') :
bet_fg=config.getfloat(sec_title,'beta_fg')
if config.has_option(sec_title,'xi_fg') :
xi_fg=config.getfloat(sec_title,'xi_fg')
if config.has_option(sec_title,'nux_fg') :
nux_fg=config.getfloat(sec_title,'nux_fg')
if config.has_option(sec_title,'lx_fg') :
lx_fg=config.getfloat(sec_title,'lx_fg')
if self.include_im_fg==False :
self.include_im_fg=include_fg
self.fit_im_fg=fit_fg*include_fg
if self.fit_im_fg :
self.params_all.append(fsh.ParamFisher(a_fg,0.1*a_fg,"im_fg_a_fg",
"$A_{\\rm FG}$",True,True,0))
self.params_all.append(fsh.ParamFisher(alp_fg,0.1*alp_fg,"im_fg_alp_fg",
"$\\alpha_{\\rm FG}$",True,True,0))
self.params_all.append(fsh.ParamFisher(bet_fg,0.1*bet_fg,"im_fg_bet_fg",
"$\\beta_{\\rm FG}$",True,True,0))
self.params_all.append(fsh.ParamFisher(xi_fg,0.1*xi_fg,"im_fg_xi_fg",
"$\\xi_{\\rm FG}$",True,True,0))
if (tr_type=="gal_clustering") or (tr_type=="intensity_mapping") :
self.has_gal_clustering=True
lmax=min(lmax,self.lmax_lss)
elif tr_type=="gal_shear" :
self.has_gal_shear=True
lmax=min(lmax,self.lmax_lss)
elif tr_type=="cmb_lensing" :
self.has_cmb_lensing=True
lmax=min(lmax,self.lmax_cmb)
elif tr_type=="cmb_primary" :
if has_t :
self.has_cmb_t=True
if has_p :
self.has_cmb_p=True
lmax=min(lmax,self.lmax_cmb)
else :
strout="Wrong tracer type \""+tr_type+"\"\n"
strout+="Allowed tracer types are: gal_clustering, intensity_mapping, "
strout+="gal_shear, cmb_lensing, cmb_primary\n"
sys.exit(strout)
self.tracers.append(trc.Tracer(self,name,tr_type,bins_file,nz_file,
is_photometric,bias_file,sbias_file,ebias_file,
abias_file,rfrac_file,include_m_bias,m_step,sigma_gamma,
has_t,has_p,sigma_t,sigma_p,beam_amin,l_transition,
tz_file,dish_size,t_inst,t_total,n_dish,
area_efficiency,fsky_im,im_type,base_file,baseline_min,baseline_max,
a_fg,alp_fg,bet_fg,xi_fg,nux_fg,lx_fg,
n_tracers,consider_tracer,lmin,lmax,bphz_prior))
self.n_tracers=n_tracers
if ((self.n_tracers==0) and (self.n_bao==0)) :
sys.exit("You need at least one tracer or some BAO measurements")
#Search for nuisance bias parameters
list_nuisance=[]
def add_nuisance(nu) :
if len(nu.z_arr)>0 :
n_nodes=len(nu.z_arr)
for i in np.arange(n_nodes) :
if nu.i_marg[i]>0 :
nui_name=nu.name+"node%d"%i
new_nuisance=True
for nm in list_nuisance :
if nui_name==nm :
new_nuisance=False
if new_nuisance :
list_nuisance.append(nui_name)
self.params_all.append(fsh.ParamFisher(nu.f_arr[i],nu.df_arr[i],nu.pr_arr[i],
nui_name,nui_name,True,False,0))
for tr in self.tracers :
if tr.consider_tracer :
add_nuisance(tr.nuisance_bias)
add_nuisance(tr.nuisance_sbias)
add_nuisance(tr.nuisance_ebias)
add_nuisance(tr.nuisance_abias)
add_nuisance(tr.nuisance_rfrac)
add_nuisance(tr.nuisance_sphz)
add_nuisance(tr.nuisance_bphz)
def get_param_properties(self,parname) :
for par in self.params_all :
if par.name==parname :
return par.val,par.dval,par.onesided
def get_cls_all(self) :
if self.n_tracers<=0 :
return
#Run CLASS
allfound=True
allfound*=ino.start_running(self,"none",0)
for i in np.arange(self.npar_vary) :
pname=self.params_fshr[i].name
if pname.startswith("im_fg") :
continue
allfound*=ino.start_running(self,pname,1)
allfound*=ino.start_running(self,pname,-1)
if self.just_run_cls :
return
#Wait for CLASS to finish
if not allfound :
while(not allfound) :
allfound=True
allfound*=ino.cls_are_there(self,"none",0,False)
for i in np.arange(self.npar_vary) :
pname=self.params_fshr[i].name
if pname.startswith("im_fg") :
continue
allfound*=ino.cls_are_there(self,pname,1,False)
allfound*=ino.cls_are_there(self,pname,-1,False)
if not allfound :
time.sleep(20)
time.sleep(5)
print "Reading fiducial"
self.cl_fid_arr[:,:,:]=(ino.get_cls(self,"none",0)).reshape((self.lmax+1)/NLB,NLB,self.nbins_total,self.nbins_total).mean(axis=1)
if self.bias_file!="none" :
self.cl_mod_arr[:,:,:]=(ino.get_cls_from_name(self,clf_total=self.bias_file,clf_lensed="none",read_lensed=False,par_vary="none")).reshape((self.lmax+1)/NLB,NLB,self.nbins_total,self.nbins_total).mean(axis=1)
for i in np.arange(self.npar_vary) :
pname=self.params_fshr[i].name
if pname.startswith("im_fg") :
continue
print "Deriv for "+pname
clp=(ino.get_cls(self,pname, 1)).reshape((self.lmax+1)/NLB,NLB,self.nbins_total,self.nbins_total).mean(axis=1)
clm=(ino.get_cls(self,pname,-1)).reshape((self.lmax+1)/NLB,NLB,self.nbins_total,self.nbins_total).mean(axis=1)
if self.params_fshr[i].onesided==0 :
self.dcl_arr[i,:,:,:]=(clp-clm)/(2*self.params_fshr[i].dval)
else :
sig=1
if self.params_fshr[i].onesided<0 :
sig=-1
cl0=self.cl_fid_arr[:,:,:]
self.dcl_arr[i,:,:,:]=(-3*cl0+4*clm-clp)/(2*sig*self.params_fshr[i].dval)
if self.include_im_fg :
print "Adding IM foregrounds"
nbt1=0
for tr1 in self.tracers :
if tr1.consider_tracer==False :
continue
nbt2=0
nb1=tr1.nbins
print " "+tr1.name
for tr2 in self.tracers :
if tr2.consider_tracer==False :
continue
nb2=tr2.nbins
if tr1.tracer_type=='intensity_mapping' :
if tr2.tracer_type=='intensity_mapping' :
cls=(trc.get_foreground_cls(tr1,tr2,self.lmax,"none"))
self.cl_fid_arr[:,nbt1:nbt1+nb1,nbt2:nbt2+nb2]+=cls.reshape((self.lmax+1)/NLB,NLB,nb1,nb2).mean(axis=1)
if self.fit_im_fg :
for ip in np.arange(self.npar_vary) :
pname=self.params_fshr[ip].name
if pname.startswith("im_fg") :
dcls=trc.get_foreground_cls(tr1,tr2,self.lmax,pname)
self.dcl_arr[ip,:,nbt1:nbt1+nb1,nbt2:nbt2+nb2]=dcls.reshape((self.lmax+1)/NLB,NLB,nb1,nb2).mean(axis=1)
nbt2+=nb2
nbt1+=nb1
if self.include_m_bias:
for m_bin in range(self.npar_mbias):
print "Deriv for m"+str(m_bin)
clp=(ino.get_cls(self,"none",0,+self.m_step,m_bin)).reshape((self.lmax+1)/NLB,NLB,self.nbins_total,self.nbins_total).mean(axis=1)
clm=(ino.get_cls(self,"none",0,-self.m_step,m_bin)).reshape((self.lmax+1)/NLB,NLB,self.nbins_total,self.nbins_total).mean(axis=1)
self.dcl_arr[self.npar_vary+m_bin,:,:,:]=(clp-clm)/(2*self.m_step)
# for i in np.arange(self.npar_vary) :
# print "Deriv for "+self.params_fshr[i].name
# toplot=[]
# larr=np.arange(len(self.dcl_arr[i,:,0,0]))
# toplot.append(larr)
# cols=['r','g','b']
# for ib in np.arange(self.nbins_total) :
# arr_toplot=self.dcl_arr[i,:,ib,ib]/self.cl_fid_arr[:,ib,ib]*np.sqrt(larr+0.5)
# plt.plot(larr,arr_toplot,cols[ib%3]+'-')
# toplot.append(arr_toplot)
# toplot=np.array(toplot)
# np.savetxt("dcl.txt",np.transpose(toplot))
# plt.gca().set_xscale('log')
# plt.show()
def get_cls_noise(self) :
if self.n_tracers<=0 :
return
nbt1=0
for i1 in np.arange(self.n_tracers) :
if self.tracers[i1].consider_tracer==False :
continue
nbt2=0
nb1=self.tracers[i1].nbins
print " "+self.tracers[i1].name
for i2 in np.arange(self.n_tracers) :
if self.tracers[i2].consider_tracer==False :
continue
nb2=self.tracers[i2].nbins
cls=trc.get_cross_noise(self.tracers[i1],self.tracers[i2],self.lmax)
self.cl_noise_arr[:,nbt1:nbt1+nb1,nbt2:nbt2+nb2]=cls.reshape((self.lmax+1)/NLB,NLB,nb1,nb2).mean(axis=1)
nbt2+=nb2
nbt1+=nb1
def join_fishers(self) :
self.fshr=self.fshr_cls+self.fshr_bao
names_arr =np.array([p.name for p in self.params_fshr] + ['m'+str(i) for i in range(self.npar_mbias)])
vals_arr =np.array([p.val for p in self.params_fshr] + [0. for i in range(self.npar_mbias)])
labels_arr=np.array([p.label for p in self.params_fshr] + ['$m_1$'+str(i) for i in range(self.npar_mbias)])
np.savez(self.output_dir+"/"+self.output_fisher+"/fisher_raw",
fisher_l=self.fshr_l,fisher_cls=self.fshr_cls,
fisher_bao=self.fshr_bao,fisher_tot=self.fshr,
fisher_bias=self.fshr_bias,fisher_bias_l=self.fshr_bias_l,
fisher_prior=self.fshr_prior,
names=names_arr,values=vals_arr,labels=labels_arr)
def get_fisher_prior(self):
""" Get prior contribution to Fisher """
fname_save=self.output_dir+"/"+self.output_fisher+"/fisher_raw.npz"
if os.path.isfile(fname_save) :
self.fshr_prior[:,:]=np.load(fname_save)['fisher_prior']
else:
for i in np.arange(self.npar_vary):
# TODO: implement prior on multiplicative bias
print "Prior is", self.params_fshr[i].prior, "on", self.params_fshr[i].name
if self.params_fshr[i].prior != 0.:
print "Adding a prior of", self.params_fshr[i].prior, "for", self.params_fshr[i].name
self.fshr_prior[i][i] = 1/self.params_fshr[i].prior**2
def get_fisher_bao(self) :
""" Compute Fisher matrix from numerical derivatives """
if self.n_bao<=0 :
return
fname_save=self.output_dir+"/"+self.output_fisher+"/fisher_raw.npz"
if os.path.isfile(fname_save) :
self.fshr_bao[:,:]=np.load(fname_save)['fisher_bao']
else :
allfound=True
allfound*=ino.start_running(self,"none",0)
for i in np.arange(self.npar_vary) :
allfound*=ino.start_running(self,self.params_fshr[i].name,1)
allfound*=ino.start_running(self,self.params_fshr[i].name,-1)
if not allfound :
while(not allfound) :
allfound=True
allfound*=ino.bao_is_there(self,"none",0,False)
for i in np.arange(self.npar_vary) :
allfound*=ino.bao_is_there(self,self.params_fshr[i].name,1,False)
allfound*=ino.bao_is_there(self,self.params_fshr[i].name,-1,False)
if not allfound :
time.sleep(10)
time.sleep(5)
da_fid_nodes,hh_fid_nodes,dv_fid_nodes=ino.get_bao(self,"none",0)
dda_nodes=np.zeros([self.npar_vary+self.npar_mbias,len(self.z_nodes_DA)])
dhh_nodes=np.zeros([self.npar_vary+self.npar_mbias,len(self.z_nodes_HH)])
ddv_nodes=np.zeros([self.npar_vary+self.npar_mbias,len(self.z_nodes_DV)])
for i in np.arange(self.npar_vary) :
dap,hhp,dvp=ino.get_bao(self,self.params_fshr[i].name, 1)
dam,hhm,dvm=ino.get_bao(self,self.params_fshr[i].name,-1)
if self.params_fshr[i].onesided==0 :
dda_nodes[i,:]=(dap-dam)/(2*self.params_fshr[i].dval)
dhh_nodes[i,:]=(hhp-hhm)/(2*self.params_fshr[i].dval)
ddv_nodes[i,:]=(dvp-dvm)/(2*self.params_fshr[i].dval)
else :
sig=1
if self.params_fshr[i].onesided<0 :
sig=-1
dda_nodes[i,:]=(-3*da_fid_nodes+4*dam-dap)/(2*sig*self.params_fshr[i].dval)
dhh_nodes[i,:]=(-3*hh_fid_nodes+4*hhm-hhp)/(2*sig*self.params_fshr[i].dval)
ddv_nodes[i,:]=(-3*dv_fid_nodes+4*dvm-dvp)/(2*sig*self.params_fshr[i].dval)
if self.relative_bao_errors:
dda_nodes[i,:]/=da_fid_nodes
dhh_nodes[i,:]/=hh_fid_nodes
ddv_nodes[i,:]/=dv_fid_nodes
self.fshr_bao+=np.sum(dda_nodes[:,None,:]*dda_nodes[None,:,:]/self.e_nodes_DA**2,axis=2)
self.fshr_bao+=np.sum(dhh_nodes[:,None,:]*dhh_nodes[None,:,:]/self.e_nodes_HH**2,axis=2)
self.fshr_bao+=np.sum(ddv_nodes[:,None,:]*ddv_nodes[None,:,:]/self.e_nodes_DV**2,axis=2)
def get_fisher_cls_mat(self) :
""" Compute Fisher matrix from numerical derivatives """
if self.n_tracers<=0 :
return
fname_save=self.output_dir+"/"+self.output_fisher+"/fisher_raw.npz"
if os.path.isfile(fname_save) :
self.fshr_l=np.load(fname_save)['fisher_l']
self.fshr_cls=np.load(fname_save)['fisher_cls']
else :
icl_arr=np.zeros_like(self.cl_fid_arr)
lmax_arr=np.zeros(self.nbins_total)
lmin_arr=np.zeros(self.nbins_total)
nbt=0
for tr in self.tracers :
if tr.consider_tracer :
zarr=None
if ((tr.tracer_type=='gal_clustering') or
(tr.tracer_type=='intensity_mapping') or
(tr.tracer_type=='gal_shear')) :
data=np.loadtxt(tr.bins_file,unpack=True)
zarr=(data[0]+data[1])/2
for ib in np.arange(tr.nbins) :
if zarr is not None :
lmn=tr.lmin
else :
lmn=tr.lmin
lmax_arr[nbt]=min(tr.lmax_bins[ib],tr.lmax)
lmin_arr[nbt]=lmn
nbt+=1
for lb in np.arange((self.lmax+1)/NLB) :
for ib in np.arange(NLB) :
l=lb*NLB+ib
if l==0 :
continue
ell=float(l)
indices=np.where((lmin_arr<=l) & (lmax_arr>=l))[0]
cl_fid=self.cl_fid_arr[lb,indices,:][:,indices]
cl_noise=self.cl_noise_arr[lb,indices,:][:,indices]
icl=np.linalg.inv(cl_fid+cl_noise)
for i in np.arange(self.npar_vary+self.npar_mbias) :
dcl1=self.dcl_arr[i,lb,indices,:][:,indices]
for j in np.arange(self.npar_vary-i+self.npar_mbias)+i :
dcl2=self.dcl_arr[j,lb,indices,:][:,indices]
self.fshr_l[i,j,l]=self.fsky*(ell+0.5)*np.trace(np.dot(dcl1,
np.dot(icl,
np.dot(dcl2,
icl))))
if i!=j :
self.fshr_l[j,i,l]=self.fshr_l[i,j,l]
self.fshr_cls[:,:]=np.sum(self.fshr_l,axis=2)
def get_fisher_cls_vec(self) :
""" Compute Fisher matrix from numerical derivatives """
if self.n_tracers<=0 :
return
print "YASSSSS"
fname_save=self.output_dir+"/"+self.output_fisher+"/fisher_raw.npz"
if os.path.isfile(fname_save) :
self.fshr_l=np.load(fname_save)['fisher_l']
self.fshr_cls=np.load(fname_save)['fisher_cls']
else :
icl_arr=np.zeros_like(self.cl_fid_arr)
lmax_arr=np.zeros(self.nbins_total)
lmin_arr=np.zeros(self.nbins_total)
nbt=0
for tr in self.tracers :
if tr.consider_tracer :
zarr=None
if ((tr.tracer_type=='gal_clustering') or
(tr.tracer_type=='intensity_mapping') or
(tr.tracer_type=='gal_shear')) :
data=np.loadtxt(tr.bins_file,unpack=True)
zarr=(data[0]+data[1])/2
for ib in np.arange(tr.nbins) :
if zarr is not None :
lmn=tr.lmin
else :
lmn=tr.lmin
lmax_arr[nbt]=min(tr.lmax_bins[ib],tr.lmax)
lmin_arr[nbt]=lmn
nbt+=1
for lb in np.arange((self.lmax+1)/NLB) :
for ib in np.arange(NLB) :
l=lb*NLB+ib
if l==0 :
continue
weight=1./((2*l+1.)*self.fsky)
indices=np.where((lmin_arr<=l) & (lmax_arr>=l))[0]
gst=GaussSt(len(indices))
cl_fid_mat=(self.cl_fid_arr[lb,indices,:][:,indices]+
self.cl_noise_arr[lb,indices,:][:,indices])
i_covar=np.linalg.inv(gst.gaussian_covariance(cl_fid_mat,weight))
for i in np.arange(self.npar_vary+self.npar_mbias) :
dcl1=gst.unwrap_matrix(self.dcl_arr[i,lb,indices,:][:,indices])
for j in np.arange(self.npar_vary-i+self.npar_mbias)+i :
dcl2=gst.unwrap_matrix(self.dcl_arr[j,lb,indices,:][:,indices])
self.fshr_l[i,j,l]=np.dot(dcl1,np.dot(i_covar,dcl2))
if i!=j :
self.fshr_l[j,i,l]=self.fshr_l[i,j,l]
self.fshr_cls[:,:]=np.sum(self.fshr_l,axis=2)
def get_fisher_cls(self) :
if self.do_pspec_fisher :
self.get_fisher_cls_vec()
else :
self.get_fisher_cls_mat()
def get_bias_mat(self) :
""" Compute the bias for each varied parameter """
if self.bias_file=="none" :
return
if self.n_tracers<=0 :
return
fname_save=self.output_dir+"/"+self.output_fisher+"/fisher_raw.npz"
if os.path.isfile(fname_save) :
self.fshr_bias=np.load(fname_save)['fisher_bias']
else :
icl_arr=np.zeros_like(self.cl_fid_arr)
lmax_arr=np.zeros(self.nbins_total)
lmin_arr=np.zeros(self.nbins_total)
nbt=0
for tr in self.tracers :
if tr.consider_tracer :
zarr=None
if ((tr.tracer_type=='gal_clustering') or
(tr.tracer_type=='intensity_mapping') or
(tr.tracer_type=='gal_shear')) :