-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLikeRun.py
More file actions
1419 lines (1248 loc) · 48.3 KB
/
LikeRun.py
File metadata and controls
1419 lines (1248 loc) · 48.3 KB
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
#!/home/abrahams/epd_free-7.3-2-rh5-x86/bin/python
import sys, os
import gt_apps as gaps
import numpy as np
from astLib import astCoords
from astLib import astWCS
import pyLikelihood
from BinnedAnalysis import *
import pyfits
from scipy import ndimage
from UpperLimits import UpperLimits
from scipy import integrate
#########################################
##
## Feb. 18, 2016:
## From WeakSrc, cannot delete diffuse sources
## anymore.
##
#########################################
##
## Feb 24, 2016
## Trying: remove upper limits calculations, and
## writing Xml(#1) BEFORE writing results in
## "Properties.txt"
##
#########################################
#
# Likelihood module:
# This module will define all the likelihood analysis routines
# deemed necessary including binned analysis, upper limit calculations,
# and probably others such as freezing weak sources (or removing them
# frmo the model file...). It is a work in progress and utilizes the
# standard python Fermi tools, not the quickAnalysis routines.
## ##############################################
## ##############################################
def Bexpmap_mk(Input,fl):
""" Create the binned exposure map.
Input: input data, ROI position, energy range, etc.
fl : list of file names from SelectLoad.FileNames """
# need gaps.gtexpcube2 and it's parameters
if (fl['bexpmp'] in os.listdir('.')):
return "Binned exposure map made. Continue"
(ra,de) = astCoords.convertCoords("GALACTIC","J2000",
Input['glon'][1],Input['glat'][1],2000)
if ( fl['time'] == False):
gaps.gtexpcube2['infile'] = fl['ltcube']
elif (fl['time'] == True):
gaps.gtexpcube2['infile'] = fl['ltcube_t']
else:
print "Error in binned exposure map input."
gaps.gtexpcube2['cmap'] = 'none'
gaps.gtexpcube2['outfile'] = fl['bexpmp']
gaps.gtexpcube2['irfs'] = fl['IRFS']
gaps.gtexpcube2['nxpix'] = int(500)
gaps.gtexpcube2['nypix'] = int(500)
gaps.gtexpcube2['binsz'] = Input['binsz'][1]
gaps.gtexpcube2['ebinalg'] = 'LOG'
gaps.gtexpcube2['emin'] = Input['emin'][1]
gaps.gtexpcube2['emax'] = Input['emax'][1]
gaps.gtexpcube2['enumbins'] = int(Input['enumbins'][1])
if (Input['glat'][1] > 70):
gaps.gtexpcube2['coordsys'] = 'CEL'
gaps.gtexpcube2['xref'] = ra
gaps.gtexpcube2['yref'] = de
gaps.gtexpcube2['proj'] = 'AIT'
gaps.gtexpcube2.run()
return 0
else:
gaps.gtexpcube2['coordsys'] = 'GAL'
gaps.gtexpcube2['xref'] = Input['glon'][1]
gaps.gtexpcube2['yref'] = Input['glat'][1]
gaps.gtexpcube2['proj'] = 'CAR'
gaps.gtexpcube2.run()
return 0
## ##############################################
def Srcmap_mk(Input,fl):
""" Make the source map for the given model XML file: convolves PSF and
instrument responses and exposure maps together with the model
Input: input data, ROI position, energy range, etc.
fl : list of file names from SelectLoad.FileNames
key : all/NoCODG/Ptsrc -- CO/dark gas templates present or not, and
a point source replacing CO/DG templates """
if ( fl['srcmp_%s'%fl['mode'].lower()] in os.listdir('.')):
return "source map made for mode %s"%fl['mode']
elif ( (fl['mode'] == 'noco') or (fl['mode'] == 'nodg') or (fl['mode'] == 'nocodg') or (fl['mode'] == 'ptsrc') or (fl['mode'] == 'all_plus') ):
os.system('cp %s %s'%(fl['srcmp_all'],fl['srcmp_%s'%fl['mode'].lower()]))
return "Copied original source map into directory for %s"%fl['mode']
gaps.srcMaps['scfile'] = fl['SC_fl']
if (fl['time'] == False):
gaps.srcMaps['expcube']= fl['ltcube']
elif (fl['time'] == True):
gaps.srcMaps['expcube']= fl['ltcube_t']
else:
print "Error in source map input."
gaps.srcMaps['cmap'] = fl['CCUBE']
# gaps.srcMaps['ptsrc'] = 'no'
gaps.srcMaps['evtype']= 3
gaps.srcMaps['bexpmap'] = fl['bexpmp']
gaps.srcMaps['irfs'] = fl['IRFS']
gaps.srcMaps['srcmdl'] = fl['MODEL_%s'%fl['mode'].lower()]
gaps.srcMaps['outfile'] = fl['srcmp_%s'%fl['mode'].lower()]
gaps.srcMaps.run()
return "Finished source map"
## ##############################################
def angsep(ra1,de1,ra2,de2):
""" Find the angular separation between two (RA,dec) positions.
Returns separation in degrees """
d2r = np.pi/180.
ra1 *= d2r
ra2 *= d2r
de1 *= d2r
de2 *= d2r
diffCosine=np.cos(de1)*np.cos(de2)*np.cos(ra1-ra2)+np.sin(de1)*np.sin(de2)
dC=np.round(diffCosine,10)
return np.arccos(dC)/d2r
## ##############################################
def FndSrcProp(Input,fl,name,cat):
""" Find the radius from center of ROI and TS and returns
the position in the list of sources from the model
Input: input data, ROI position, energy range, etc.
fl : list of file names from SelectLoad.FileNames
name : name of source we want the properties of
cat : Fermi 2-year catalog file name """
ind= np.where( cat[1].data['Source_Name'] == name )[0][0]
TS = (cat[1].data['Sqrt_TS100_300'][ind]**2 + cat[1].data['Sqrt_TS300_1000'][ind]**2
+ cat[1].data['Sqrt_TS1000_3000'][ind]**2 + cat[1].data['Sqrt_TS3000_10000'][ind]**2)
rad= ( angsep(Input['glon'][1],Input['glat'][1],
cat[1].data['GLON'][ind],cat[1].data['GLAT'][ind]) )
return (ind,rad,TS)
## ##############################################
def FreezeSrc(Input,fl,like,cat,src):
""" Freeze parameters of sources > 8.5 degrees from middle as well as
weak sources (TS < 50). Remove sources > 15 degrees from middle
and sources with TS < 25. Freezes shape if > 5 degrees from middle
Look for free parameters. If they exist check distance and TS.
Freeze or remove as necessary
Input: input data, ROI position, energy range, etc.
fl : list of file names from SelectLoad.FileNames
like : likelihood object for analysis
cat : Fermi 2-year catalog file name
src : name of source to be frozen"""
wrt = open('Freezing.dat','a')
try:
# len( like.freePars(src)[0] )
(ind, center_dist, TS) = FndSrcProp(Input,fl,src,cat)
if ( TS > 75 ): ## Strong sources are free
like = ThawParams(Input,fl,like,cat,src,ind)
wrt.write('%s kept, TS = %s\n'%(src,TS))
pass
elif ( (center_dist > 4.5) ):
like = FreezeSrc2(Input,fl,like,cat,src,ind)
wrt.write('%s totally frozen......\n'%src)
elif ( (center_dist > 2) and (TS > 250 ) ):
like = FreezeShape(Input,fl,like,cat,src,ind)
wrt.write('%s shape frozen......\n'%src)
elif ( (center_dist > 2) and (TS < 250 ) ):
like = FreezeSrc2(Input,fl,like,cat,src,ind)
wrt.write('%s totally frozen......\n'%src)
elif ( (center_dist < 1) and (TS < 500 ) ):
if ( (src[-1] != 'c') ):
like.deleteSource( src )
wrt.write('\t %s killed: too close,' +
'CO dist = %s, TS = %s.. confused'+
' source\n'%(src,CO_peak_dist,TS))
elif ( (fl['mode'].lower() != 'all_plus') and
(fl['mode'].lower() != 'ptsrc') ):
like.deleteSource( src )
wrt.write('\t %s killed: too close, CO dist ' +
'= %s, TS = %s......\n'%(src,CO_peak_dist,TS))
elif ( (fl['mode'].lower() != 'all_plus') and (fl['mode'].lower() != 'ptsrc') ):
wrt.write('\t %s kept: confused source at dist = %s\n'%(src,TS))
else:
wrt.write('\t %s not touched.\n'%src)
except:
wrt.write('%s error\n'%src)
pass
wrt.close()
return like
## ##############################################
def FreezeShape(Input,fl,like,cat,src,ind):
""" Freeze spectral shape.
Input: input data, ROI position, energy range, etc.
fl : list of file names from SelectLoad.FileNames
like : likelihood object for analysis
cat : Fermi 2-year catalog file name
src : name of source to be frozen"""
wrt = open('Freezing.dat','a')
try:
if (cat[1].data['SpectrumType'][ind] == 'PowerLaw'):
like.freeze(like.par_index(src,'Index'))
elif (cat[1].data['SpectrumType'][ind] == 'PowerLaw2'):
like.freeze(like.par_index(src,'Index'))
elif (cat[1].data['SpectrumType'][ind] == 'LogParabola'):
like.freeze(like.par_index(src,'alpha'))
like.freeze(like.par_index(src,'beta'))
elif (cat[1].data['SpectrumType'][ind] == 'BrokenPowerLaw'):
like.freeze(like.par_index(src,'Index1'))
like.freeze(like.par_index(src,'BreakValue'))
like.freeze(like.par_index(src,'Index2'))
elif (cat[1].data['SpectrumType'][ind] == 'BrokenPowerLaw2'):
like.freeze(like.par_index(src,'Index1'))
like.freeze(like.par_index(src,'BreakValue'))
like.freeze(like.par_index(src,'Index2'))
elif (cat[1].data['SpectrumType'][ind] == 'SmoothBrokenPowerLaw'):
like.freeze(like.par_index(src,'Index1'))
like.freeze(like.par_index(src,'BreakValue'))
like.freeze(like.par_index(src,'Index2'))
like.freeze(like.par_index(src,'Beta'))
elif (cat[1].data['SpectrumType'][ind] == 'ExpCutoff'):
like.freeze(like.par_index(src,'Index'))
like.freeze(like.par_index(src,'Ebreak'))
like.freeze(like.par_index(src,'P1'))
elif (cat[1].data['SpectrumType'][ind] == 'BPLExpCutoff'):
like.freeze(like.par_index(src,'Index1'))
like.freeze(like.par_index(src,'Ebreak'))
like.freeze(like.par_index(src,'P1'))
like.freeze(like.par_index(src,'Index2'))
like.freeze(like.par_index(src,'Eabs'))
elif (cat[1].data['SpectrumType'][ind] == 'PLSuperExpCutoff'):
like.freeze(like.par_index(src,'Index1'))
like.freeze(like.par_index(src,'Cutoff'))
like.freeze(like.par_index(src,'Index2'))
elif (cat[1].data['SpectrumType'][ind] == 'Gaussian'):
like.freeze(like.par_index(src,'Mean'))
like.freeze(like.par_index(src,'Sigma'))
except:
wrt.write('%s error\n'%src)
pass
wrt.close()
return like
## ##############################################
def FreezeSrc2(Input,fl,like,cat,src,ind):
""" For the 2nd round through the likelihood analysis, we want to
freeze all sources except those we care about.
Input: input data, ROI position, energy range, etc.
fl : list of file names from SelectLoad.FileNames
like : likelihood object for analysis
cat : Fermi 2-year catalog file name
src : name of source to be frozen"""
params = like.model[src].funcs['Spectrum'].paramNames
num_params = len(params)
for par in params:
like.freeze(like.par_index(src,par))
return like
## ##############################################
def ThawParams(Input,fl,like,cat,src,ind):
""" For the 2nd round through the likelihood analysis, we want to
thaw some sources.
Input: input data, ROI position, energy range, etc.
fl : list of file names from SelectLoad.FileNames
like : likelihood object for analysis
cat : Fermi 2-year catalog file name
src : name of source to be frozen"""
if ('Add' in src):
like.thaw(like.par_index(src,'Integral'))
like.thaw(like.par_index(src,'Index'))
elif (cat[1].data['SpectrumType'][ind] == 'PowerLaw'):
like.thaw(like.par_index(src,'Prefactor'))
# like.thaw(like.par_index(src,'Index'))
elif (cat[1].data['SpectrumType'][ind] == 'PowerLaw2'):
like.thaw(like.par_index(src,'Integral'))
like.thaw(like.par_index(src,'Index'))
elif (cat[1].data['SpectrumType'][ind] == 'LogParabola'):
like.thaw(like.par_index(src,'norm'))
# like.thaw(like.par_index(src,'alpha'))
# like.freeze(like.par_index(src,'beta'))
elif (cat[1].data['SpectrumType'][ind] == 'BrokenPowerLaw'):
like.thaw(like.par_index(src,'Prefactor'))
like.thaw(like.par_index(src,'Index1'))
like.thaw(like.par_index(src,'BreakValue'))
like.thaw(like.par_index(src,'Index2'))
elif (cat[1].data['SpectrumType'][ind] == 'BrokenPowerLaw2'):
like.thaw(like.par_index(src,'Integral'))
like.thaw(like.par_index(src,'Index1'))
like.thaw(like.par_index(src,'BreakValue'))
like.thaw(like.par_index(src,'Index2'))
elif (cat[1].data['SpectrumType'][ind] == 'SmoothBrokenPowerLaw'):
like.thaw(like.par_index(src,'Prefactor'))
like.thaw(like.par_index(src,'Index1'))
like.thaw(like.par_index(src,'BreakValue'))
like.thaw(like.par_index(src,'Index2'))
like.thaw(like.par_index(src,'Beta'))
elif (cat[1].data['SpectrumType'][ind] == 'ExpCutoff'):
like.thaw(like.par_index(src,'Prefactor'))
# like.thaw(like.par_index(src,'Index'))
# like.thaw(like.par_index(src,'Ebreak'))
# like.thaw(like.par_index(src,'P1'))
elif (cat[1].data['SpectrumType'][ind] == 'BPLExpCutoff'):
like.thaw(like.par_index(src,'Prefactor'))
like.thaw(like.par_index(src,'Index1'))
like.thaw(like.par_index(src,'Ebreak'))
like.thaw(like.par_index(src,'P1'))
like.thaw(like.par_index(src,'Index2'))
like.thaw(like.par_index(src,'Eabs'))
elif (cat[1].data['SpectrumType'][ind] == 'PLSuperExpCutoff'):
like.thaw(like.par_index(src,'Prefactor'))
# like.thaw(like.par_index(src,'Index1'))
# like.thaw(like.par_index(src,'Cutoff'))
# like.thaw(like.par_index(src,'Index2'))
elif (cat[1].data['SpectrumType'][ind] == 'Gaussian'):
like.thaw(like.par_index(src,'Prefactor'))
like.thaw(like.par_index(src,'Mean'))
like.thaw(like.par_index(src,'Sigma'))
return like
## ##############################################
def DiffFreeThaw(Input,fl,like,src,act):
params = like.model[src].funcs['Spectrum'].paramNames
num_params = len(params)
if (act.lower() == 'freeze'):
for par in params:
like.freeze(like.par_index(src,par))
elif (act.lower() == 'thaw'):
for par in params:
like.thaw(like.par_index(src,par))
return like
## ##############################################
def ThawSrc(Input,fl,like,src):
(ind, center_dist, TS) = FndSrcProp(Input,fl,src,fl['cat_var'])
like = ThawParams(Input,fl,like,fl['cat_var'],src,ind)
return like
## ##############################################
def MdlMap(Input,fl,lk_iter,flxrun):
""" Create model map, because the likelihood routine is giving
me a seg fault """
gaps.model_map['srcmaps'] = fl['srcmp_%s'%fl['mode'].lower()]
if (flxrun == 0):
gaps.model_map['srcmdl'] = fl['outmdl%s_%s'%(str(lk_iter),fl['mode'].lower())]
gaps.model_map['outfile'] = fl['outmap%s_%s'%(str(lk_iter),fl['mode'].lower())]
elif(flxrun == 1):
pass
gaps.model_map['irfs'] = fl['IRFS']
gaps.model_map['bexpmap'] = fl['bexpmp']
if ( fl['time'] == False ):
gaps.model_map['expcube'] = fl['ltcube']
else:
gaps.model_map['expcube'] = fl['ltcube_t']
gaps.model_map.run()
## ##############################################
## ##############################################
def PrepAll(Input,fl,like,likerun):
""" Do the likelihood analysis, here I will do the model prep.
Prepare model: freeze point sources & fit diffuse
then freeze diffuse & thaw point sources & fit
then unfreeze diffuse and do standard NewMinuit """
if (fl['mode'] != 'all'):
return (0,like)
g = open('LogLike.dat','a')
# Here: freeze point sources
name = like.sourceNames()
print "\n1st name = %s\n"%name[0]
for nm in name:
if ('3FGL' in nm):
(ind, center_dist, TS) = FndSrcProp(Input,fl,nm,fl['cat_var'])
like = FreezeSrc2(Input,fl,like,fl['cat_var'],nm,ind)
print ("Freeeeeeeze %s"%nm)
# elif ( (nm in fl['gal']) or (nm in fl['iso']) ):
# like.deleteSource(nm)
elif ( (fl['mode'].lower() == 'noco') and (nm == 'CO') ):
like.deleteSource(nm)
elif ( (fl['mode'].lower() == 'nodg') and (nm == 'E(B-V)') ):
like.deleteSource(nm)
elif ('Add' in nm):
like = FreezeSrc2(Input,fl,like,fl['cat_var'],nm,ind)
try:
# if (like.optimizer == 'DRMNFB'):
# lglike = like.fit()
# elif (like.optimizer == 'Minuit'):
# like.writeXml('MDL.xml')
# like = BinnedAnalysis(obs,srcModel='MDL.xml',optimizer='Minuit')
# likeobj = pyLike.Minuit(like.logLike)
# lglike = like.fit(covar=True,optObject=likeobj)
# g.write('Fit quality %s\t'%like1obj.getQuality())
like1obj = pyLike.Minuit(like.logLike)
lglike = like.fit(covar=True,optObject=like1obj,verbosity=0)
g.write('Fit quality %s\t'%like1obj.getQuality())
print ('Fit quality %s\t'%like1obj.getQuality())
except:
print "Boop boop"
like.optimizer='minuit'
print "Optimizer = 'minuit'"
for nm in name:
if ( ((nm == 'CO') or (nm == 'E(B-V)') or (nm == 'HI_far'))
and (like.Ts(nm)< 3) ):
like.deleteSource(nm)
elif ( (nm == 'HI') and ( fl['gas_spec'].lower() != 'spec') ):
like.model['HI'].funcs['Spectrum'].params['Index1'].setValue(-1.8)
like.model['HI'].funcs['Spectrum'].params['Index1'].setScale(1)
like.model['HI'].funcs['Spectrum'].params['Integral'].setValue(2)
likeobj = pyLike.Minuit(like.logLike)
lglike = like.fit(covar=True,optObject=likeobj,verbosity=0)
# Delete weak diffuse sources and freeze strong diffuse sources and thaw good point sources
print "Delete weak diffuse sources, freeze rest, thaw 'good' point sources."
name = like.sourceNames()
for nm in name:
if ( ('3FGL' not in nm) and ('Add' not in nm) and ('iso' not in nm) ):
# if ( like.Ts(nm) < 3 ):
# like.deleteSource(nm)
# else:
like = DiffFreeThaw(Input,fl,like,nm,'freeze')
print ("Frozen %s!"%nm)
elif ( (('IC' in nm) or ( 'galprop' in nm )) and (like.Ts(nm) < 3) ):
like.deleteSource(nm)
elif ( '3FGL' in nm ):
like = ThawSrc(Input,fl,like,nm)
print ("Frozen Sources !! :). Fit point sources.\n")
lgng = open('Logging.txt','a')
lgng.write("Frozen Diffuse Sources !! :). Fit point sources.\n")
lgng.close()
# freeze some sources: fit only important ones
like = MdlPrep(Input,fl,like,likerun)
try:
if (like.optimizer.lower() == 'drmnfb'):
print "Starting 1nd fit of point sources with DRMNFB"
lglike = like.fit(verbosity=0)
elif (like.optimizer.lower() == 'minuit'):
print "Starting 1nd fit of point sources with Minuit"
likeobj2 = pyLike.Minuit(like.logLike)
lglike2 = like.fit(covar=True,optObject=likeobj2,verbosity=0)
print "Fit Quality = %s\n"%likeobj2.getQuality()
except:
print 'poop'
lgng = open('Logging.txt','a')
lgng.write("Poop, can't even accomplish step 1...\n")
lgng.close()
return ("poop")
lgng = open('Logging.txt','a')
lgng.write("Finished 1st fit of ponit sources\n")
lgng.close()
for nm in name:
if ('3FGL' in nm):
try:
aa = len(like.freePars(nm))
if ( like.Ts(nm) < 3 ):
print "Deleting %s"%nm
like.deleteSource(nm)
except:
pass
# Need to freeze point sources and unfreeze diffuse sources...
name = like.sourceNames()
print "Freezing point sources, unfreezing diffuse"
for nm in name:
if ( ('3FGL' not in nm) and ('Add' not in nm) ):
like = DiffFreeThaw(Input,fl,like,nm,'thaw')
elif ( ('3FGL' in nm) ):
like = DiffFreeThaw(Input,fl,like,nm,'freeze')
elif ( 'Add' in nm ):
like.freeze(like.par_index(nm,'Index'))
elif ( (nm == 'CO') or (nm == 'E(B-V)') or (nm == 'HI') or (nm == 'HI_far') ):
l_bnd=like.model[nm].funcs['Spectrum'].params['Normalization'].getBound()[0]
nm_val=like.model[nm].funcs['Spectrum'].params['Normalization'].getValue()
if ( ((nm_val-l_bnd)/l_bnd) < 1 ):
like.deleteSource(nm)
print "Deleted %s because normalization at lower limit"%nm
try:
print "\n\n2nd diffuse source fitting\n\n"
lgng = open('Logging.txt','a')
lgng.write("2nd diffuse source fitting.\n")
lgng.close()
likeobj = pyLike.Minuit(like.logLike)
lglike = like.fit(covar=True,optObject=likeobj,verbosity=0)
g.write('Fit quality %s\t'%like1obj.getQuality())
print ('Fit quality %s\t'%like1obj.getQuality())
except:
lgng = open('Logging.txt','a')
lgng.write("2nd diffuse fitting in 1st round = BAD\n")
lgng.close()
pass
try:
if ( like.model['IC'].funcs['Spectrum'].params['Value'].getValue() < 1e-4 ):
like.deleteSource('IC')
print "IC deleted, too weak"
except:
print "IC not found"
pass
try:
if ( like.model['HI_far'].funcs['Spectrum'].params['Normalization'].getValue() < 0.1 ):
like.deleteSource('HI_far')
print "HI_far deleted, too weak"
except:
print "HI_far gone already"
pass
lgng = open('Logging.txt','a')
lgng.write("Done round 1, writing model.\n")
lgng.close()
like.writeXml('mdl1_all.xml')
print "sources fit, finding new sources.\n"
return (lglike2,like)
# Find new sources here
#
# MdlMap(Input,fl,1,0)
# nsrc_ = NewSrc_Detect(Input,fl)
#
# if ("none" in nsrc_.lower()):
# print "no new sources\n"
# os.system('rm %s'%fl['outmap1_%s'%fl['mode'].lower()])
# print "removed model map...\n"
# lglike2 = lglike
# else:
# os.system('mv mdl1_all.xml mdl1_int1.xml') # Not needed...?
# print "new sources found!"
# fl['mode'] = 'int1'
# No need to make new sourcemap, the likelihood procedure
# make one for us!
#
# obs = BinnedObs(binnedExpMap=fl['bexpmp'],
# srcMaps=fl['srcmp_all'],expCube=fl['ltcube'],irfs=fl['IRFS'])
# like = BinnedAnalysis(obs, srcModel='mdl1_int1.xml',optimizer='minuit')
# try:
# if (like.optimizer.lower() == 'drmnfb'):
# lglike2 = like.fit()
# elif (like.optimizer.lower() == 'minuit'):
# likeobj2 = pyLike.Minuit(like.logLike)
# lglike2 = like.fit(covar=True,optObject=likeobj2)
# except:
# return ("poop")
#
# fl['mode'] = 'all'
# return (lglike,like)
## ##############################################
def BinLike(Input,fl):
""" Perform the binned likelihood analysis using the standard
gt_apps: the python wrapper for the ballistic commands
Freeze sources completely if too far away and freeze the
shapes of sources at intermediate distances from ROI center
Start likelihood with DRMNFB optimizer at coarse tolerance.
Write resulting fit to XML file. Then freeze everything
except gas templates and possibly added sources and do a
second likelihood analysis with NewMinuit optimizer."""
srcmdl = fl['MODEL_%s'%fl['mode'].lower()]
srcmap = fl['srcmp_%s'%fl['mode'].lower()]
outmdl1 = fl['outmdl1_%s'%fl['mode'].lower()]
outmdl2 = fl['outmdl2_%s'%fl['mode'].lower()]
likerun = 1
print "Start 1!"
if ( fl['time'] == False):
obs = BinnedObs(binnedExpMap=fl['bexpmp'],
srcMaps=srcmap,expCube=fl['ltcube'],irfs=fl['IRFS'])
elif ( fl['time'] == True):
obs = BinnedObs(binnedExpMap=fl['bexpmp'],
srcMaps=srcmap,expCube=fl['ltcube_t'],irfs=fl['IRFS'])
else:
print "Error in binned analysis input."
print "Observations set up, creating likelihood object"
# like = BinnedAnalysis(obs, srcModel=srcmdl,optimizer='DRMNFB')
like = BinnedAnalysis(obs, srcModel=srcmdl,optimizer='Minuit')
likeobj = pyLike.Minuit(like.logLike)
g = open('LogLike.dat','a')
print "Setting up likelihood analysis."
optim = 1
try:
if (fl['mode'].lower() == 'all'):
print "Fitting first round."
lgng = open('Logging.txt','a')
lgng.write("Starting round 1 ...\n")
lgng.close()
(lglike,like) = PrepAll(Input,fl,like,likerun)
print "-log(likelihood) = %s"%lglike
print "Done first round."
else:
print "mode =/= 'all', fitting..."
lgng = open('Logging.txt','a')
lgng.write("mode =/= all, fitting 1st round.\n")
lgng.close()
lglike = like.fit(covar=True,optObject=likeobj,verbosity=0)
except:
return "poops. Can't finish fitting for some reason. Check the 'PrepAll' routine, probably.\n\n\n Done \n\n\n :("
print "Writing likelihoods.\n"
g.write('-log(likelihood) of %s = %s\n'%(fl['mode'],lglike))
# Check TS's for diffuse sources
name = like.sourceNames()
# for nm in name:
# if ( ('3FGL' not in nm) and ('Add' not in nm) and ('iso' not in nm) ):
# if ( like.Ts(nm) < 3 ):
# like.deleteSource(nm)
# print ("Deleted %s!"%nm)
#
print "Weak diffuse sources gone, writing results\n."
lgng = open('Logging.txt','a')
lgng.write("Weak diffuse sources gone, writing results.\n")
lgng.close()
like.writeXml(outmdl1)
try:
WriteResults(Input,fl,like,optim)
except:
print "Results won't write, do it by hand."
lgng = open('Logging.txt','a')
lgng.write("Results won't write, do it by hand.\n")
lgng.close()
try:
like = WeakSrc(Input,fl,like) # Get rid of weak sources, including diffuse
except:
lgng = open('Logging.txt','a')
lgng.write("Can't kill weak sources ... let's soldier on.\n")
lgng.close()
# 2nd fitting ... setting up
like = BinnedAnalysis(obs,srcModel=outmdl1,optimizer='NewMinuit')
like2obj = pyLike.NewMinuit(like.logLike)
MdlMap(Input,fl,1,0)
# 2nd likelihood anlaysis to fine tune.
like.tol=1e-4
like = MdlPrep(Input,fl,like,2)
# lging = open('Logging.txt','a')
# lging.write('%s'%like.model)
# lging.close()
lgng = open('Logging.txt','a')
lgng.write("2nd round set up, fitting.\n")
lgng.close()
try:
lglike2 = like.fit(covar=True,optObject=like2obj,verbosity=0)
g.write('likelihood return code = %s\t'%like2obj.getRetCode())
g.write('Final -log(likelihood) of %s = %s\n'%(fl['mode'],lglike2))
like.writeXml(outmdl2)
MdlMap(Input,fl,2,0)
error = 0
except:
g.write('ERROR IN NEWMINUIT FIT\n\n')
lgng = open('Logging.txt','a')
lgng.write("Problem in step 2 (New Minuit)...\n")
lgng.close()
error = 1
pass
g.close()
try:
if ( fl['mode'].lower() == 'all' ):
covs= np.array(like.covariance)
cov = open('Covariance.txt','a')
cov.write('%s:'%len(covs))
for i in np.arange( len(covs) ):
cov.write('\n')
for j in np.arange( len(covs) ):
cov.write('%s,'%covs[i,j])
cov.close()
except:
print "Error writing covariance matrix\n\n"
WriteResults(Input,fl,like,error)
obs = None
like = None
convs = None
cov = None
srcmdl = None
srcmap = None
outmdl1 = None
outmdl2 = None
likeobj = None
like2obj = None
del obs, like, convs, cov, srcmdl, srcmap, outmdl1, outmdl2, likeobj, like2obj
lgng = open('Logging.txt','a')
lgng.write("Finished fitting mode = %s\n\n\n\n\n\n\n\n\n\n\n\n\n\n"%fl['mode'])
lgng.close()
return "Done Likelihood. Run checks."
## ##############################################
def MdlPrep(Input,fl,like,likerun):
""" We prepare the model in "like" for fitting. This means that
we freeze sources deemed unworthy """
like = ConfuSearch(Input,fl,like)
nm = like.sourceNames()
name = nm
wrt = open('Freezing.dat','a')
wrt.write('\n\n')
try:
for src in name:
if ( (fl['mode'].lower() == 'noco') and (src == 'CO') ):
like.deleteSource(src)
wrt.write('Deleting %s'%src)
elif ( (fl['mode'].lower() == 'nodg') and (src == 'E(B-V)') ):
like.deleteSource(src)
wrt.write('Deleting %s'%src)
except:
pass
name = like.sourceNames()
if ( likerun == 1 ):
wrt.write('DRMNFB or Minuit step:\n')
else:
wrt.write('New Minuit step:\n')
# If we want to fix all the point sources after fitting to the
# Fermi diffuse maps, then set likerun = 2, else, set
# likerun=1
if ( fl['mode'].lower() == 'fx_src' ):
for src in name:
if ( (src == 'CO') or (src == 'HI') or (src == 'E(B-V)') or (src == 'HI_far') ):
like.freeze(like.par_index(src,'Integral'))
like.freeze(like.par_index(src,'Index'))
return like
if ( likerun == 1 ):
for src in name:
if ('iso' in src ):
#like.freeze(like.par_index(src,'Normalization'))
print "Keeping %s\n"%src
elif ( ('3FGL' in src) and ((src != 'SMC') or (src != 'LMC') or (src != " ")) ):
try:
like = FreezeSrc(Input,fl,like,fl['cat_var'],src)
print ("%s frozen."%src)
except:
print "Passing; no freeze"
pass
elif( likerun == 2 ):
for src in name:
if ( 'iso' in src ):
# like.freeze(like.par_index(src,'Normalization'))
print "Keeping %s\n"%src
elif ( 'Add' in src ):
like.freeze(like.par_index(src,'Index'))
elif ( ('3FGL' in src) and ((src != 'SMC') or
(src != 'LMC') or (src != " ") or
(src[-1] != 'c')) ):
try:
(ind, center_dist, TS) = FndSrcProp(Input,fl,src,fl['cat_var'])
like = FreezeSrc2(Input,fl,like,fl['cat_var'],src,ind)
wrt.write('%s frozen.\n'%src)
except:
wrt.write('%s NOT FROZEN. ERROR!!!\n'%src)
pass
elif ( ('3FGL' not in src) ):
like = DiffFreeThaw(Input,fl,like,src,'thaw')
wrt.close()
return like
## ##############################################
def ConfuSearch(Input,fl,like):
""" I am searching through the model in order to find confused
sources: source names that end in the character "c". I
should do something similar for extended sources sometime,
those source names ending in "e".
"""
check_cut = 18
check = []
for src in like.sourceNames():
if ( len(src) == check_cut ):
check.append( src )
for name in check:
if ( (fl['mode'].lower() != 'ptsrc') and (name[check_cut-1] == 'c') ):
like.deleteSource( name )
print "Deleted %s."%name
elif ( fl['mode'].lower() == 'ptsrc' ):
like.deleteSource( 'Add_0' )
return like
## ##############################################
def WriteResults(Input,fl,like,error):
""" Write the fluxes and model files (for spectral indices).
"""
if ( fl['mode'].lower() == 'all' ):
mm = open('Properties.txt','a')
like.writeCountsSpectra('CountSpec_%s.fits'%fl['mode'])
else:
mm = open('%s/Properties.txt'%fl['mode'],'a')
like.writeCountsSpectra('%s/CountSpec_%s.fits'%(fl['mode'],fl['mode']))
mdls = ['HI','HI_far','CO','E(B-V)','IC','iso_P8R2_SOURCE_V6_v06']
for i in np.arange( len(like.sourceNames())-1 ):
mdls.append( 'Add_%s'%int(i) )
nm = like.sourceNames()
for name in nm:
if ( name[len(name)-1] == 'c' ):
mdls.append( name )
# (ulim == 0) means the UpperLimits routine failed. If so, then
# upper limit is set to 3 times the found flux. (error==1) means
# that the uncertainties could not be calculated.
for name in nm:
if ( name in mdls ):
try:
mm.write(' %s:\n'%name)
if ( name == 'HI' ):
mm.write(' Tspin = %s\n'%fl['Tspin'])
mm.write(' TS = %s\n'%like.Ts( name ) )
except:
mm.write('Error in writing TS for %s\n'%name)
# try:
# if (like.Ts( name ) < 20):
# ulim = UpperLim(Input,like, name)
# if ((ulim != 0) and (error != 1)):
# lim = float(str(ulim).split(' ph')[0])
# mm.write(' Upper Limit = %s ph/cm^2/s \n'%lim)
# elif ( (ulim == 0) and (error != 1) ):
# lim = 3*like.flux(name,emin=Input['emin'][1],emax=Input['emax'][1])
# limer=3*like.fluxError(name,emin=Input['emin'][1],emax=Input['emax'][1])
# mm.write(' Upper Limit about (3*flux) %s +/- %s\n'%(lim,limer))
# elif ( (ulim == 0) and (error == 1) ):
# lim = 3*like.flux(name,emin=Input['emin'][1],emax=Input['emax'][1])
# mm.write(' Upper Limit about (3*flux) %s \n'%lim)
# except:
# mm.write("Error in writing upper limit for source %s\n"%name)
try:
if ( error != 1 ):
mm.write('energy flux = %s +/- %s \n'%(like.energyFlux(name,
emin=Input['emin'][1],emax=Input['emax'][1]),
like.energyFluxError('HI',emin=Input['emin'][1],
emax=Input['emax'][1])))
mm.write('flux = %s +/- %s'%(like.flux(name,
emin=Input['emin'][1],emax=Input['emax'][1]),
like.fluxError('HI',emin=Input['emin'][1],
emax=Input['emax'][1])))
else:
mm.write('energy flux = %s \n'%(like.energyFlux(name,
emin=Input['emin'][1],emax=Input['emax'][1])))
mm.write('flux = %s'%(like.flux(name,
emin=Input['emin'][1],emax=Input['emax'][1])))
except:
mm.write('Error in writing %s\n'%name)
try:
mm.write('\n')
mm.write('%s'%like.model[name])
mm.write('\n')
except:
mm.write('No model for %s\n'%name)
mm.write('\n\n\n')
mm.close()
return "Done writing."
## ##############################################
def NewSrc_Detect(Input,fl):
""" Rummage through the latest model map created too see if
there are any missed sources (large positive residuals)
or if the model serverely overpredicted the emission,
usually the result of fixing a strong source.
"""
cmap = pyfits.open(fl['CMAP'])
(nx,ny) = (cmap[0].header['NAXIS1'],cmap[0].header['NAXIS2'])
cmap[0].data = np.transpose( cmap[0].data )
try:
mdl = pyfits.open(fl['outmap1_%s'%fl['mode'].lower()])
mdl[0].data = np.transpose( mdl[0].data )
except:
# print "Error loading model map. Refit."
return "None"
resi = cmap[0].data - mdl[0].data
res = pyfits.PrimaryHDU( resi )
res.header = cmap[0].header
res.writeto('resid_%s.fits'%fl['mode'])
resi_smooth = ndimage.gaussian_filter(resi, sigma=2)
# Choose a 4-sigma cutoff. The smoothed residual map should be
# distributed like a normal curve if everything is modeled
# well... What about holes? Those should fit out?
resi_smooth[ resi_smooth < 5*resi_smooth.std() ] = 0
if ( resi_smooth.max() == 0 ): # If nothing stands out, exit
return "None"
f = open('mdl1_%s.xml'%fl['mode'],'r')
dat = f.readlines()
f.close()
del dat[-1]
cnt = 1
WCS = astWCS.WCS(fl['CMAP'])
while (resi_smooth.std() > 0.0):
maxx = np.where( resi_smooth == resi_smooth.max() )[0][0]
maxy = np.where( resi_smooth == resi_smooth.max() )[1][0]
(glon,glat) = WCS.pix2wcs( maxx, maxy )
dat = Diffuse_ptsrc(Input,fl,dat,glon,glat,cnt)
for ind in np.arange( nx*ny ):
if ( np.linalg.norm( np.array( (maxx,maxy) ) - np.array( (ind%nx,ind/nx) ) ) < 10 ):
resi_smooth[ ind%nx,ind/nx ] = 0
cnt += 1
dat.append('</source_library>')
# os.system('mv mdl1_int1.xml MODEL_int1.xml')
g = open('mdl1_int1.xml','w')
g.writelines(dat)
g.close()
return "Done"
## ##############################################
def CatSrc_Fix(Input,fl):
""" Here we perform a basic DRMNFB fit with the Fermi diffuse
galactic emission: 'gal_2yearp7v6_v0'. We then write the
model file and then *fix* all of the point sources while
fitting the gas templates.
"""
os.mkdir('%s'%fl['mode'])
Srcmap_mk(Input,fl)
obs = BinnedObs(binnedExpMap=fl['bexpmp'],
srcMaps=fl['srcmp_orig'],
expCube=fl['ltcube'],irfs=fl['IRFS'] )
like = BinnedAnalysis(obs,srcModel=fl['MODEL_%s'%fl['mode'].lower()],
optimizer='DRMNFB')
like.setEnergyRange(emin=Input['emin'][1],emax=Input['emax'][1])
g = open('LogLike.dat','a')
g.write('\n\n')
for src in like.sourceNames():
if ('3FGL' in src):
try:
(ind,rad,TS) = FndSrcProp(Input,fl,src,fl['cat_var'])
if (rad < Input['rad'][1]+2):
like.deleteSource(src)
else:
FreezeShape(Input,fl,like,cat,src,ind)
except:
pass
try:
lglike = like.fit()
except:
g.write('Error in DRMNFB, going to Minuit.\n')
like = BinnedAnalysis(obs, srcModel=fl['MODEL_orig'],optimizer='Minuit')
like.setEnergyRange(emin=Input['emin'][1],emax=Input['emax'][1])
like1obj = pyLike.Minuit(like.logLike)
lglike = like.fit(covar=True,optObject=like1obj)
g.write('-log(likelihood) of %s = %s\n'%(fl['mode'],lglike))
g.write('\n\n')