-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathquickUtils.py
505 lines (417 loc) · 17.9 KB
/
quickUtils.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
#!/usr/bin/env python
"""Various common functions and utilities for the quick modules
"""
__author__ = 'Jeremy S. Perkins (FSSC)'
__version__ = '0.2.0'
import os
import logging
import math
import ConfigParser
import numpy as np
from gt_apps import *
class FileNotFound: pass
class CommandNotFound: pass
def log_array(npts, xmin, xmax):
'''This function creates an array with npts-1 logarithmically spaced
bins borrowed from macro Jim sent to do profile likelihood.'''
xstep = np.log(xmax/xmin)/(npts - 1)
return xmin*np.exp(np.arange(npts, dtype=np.float)*xstep)
def checkForFiles(quickLogger, fileList):
"""Checks for the existence of needed files in the list."""
for filename in fileList:
if(not os.path.exists(filename)):
quickLogger.critical(filename+" doesn't exist.")
raise FileNotFound
def checkForCommand(quickLogger, commandList):
"""Checks for the existence of a certain command."""
for command in commandList:
cmd = "which -s " + command + " > " + os.devnull + " 2>&1"
retcode = os.system(cmd)
if(retcode):
quickLogger.critical("unix command "+command+" not found.")
raise CommandNotFound
def writeConfig(quickLogger, commonDictionary, analysisDictionary = {}, likelihoodDictionary = {}, plotDictionary = {}, curveDictionary = {}):
"""Writes all of the needed information to the config file called
<basename>.cfg"""
basename = commonDictionary['base']
config = ConfigParser.RawConfigParser()
config.read(basename+'.cfg')
if(not config.has_section('common')):
config.add_section('common')
for variable, value in commonDictionary.iteritems():
config.set('common', variable, value)
quickLogger.info("wrote common config to "+basename+".cfg.")
if(analysisDictionary):
if(config.has_section('quickAnalysis')):
quickLogger.info("quickAnalysis config exists, overwriting...")
else:
config.add_section('quickAnalysis')
for variable, value in analysisDictionary.iteritems():
config.set('quickAnalysis', variable, value)
quickLogger.info("wrote quickAnalysis config to "+basename+".cfg.")
if(likelihoodDictionary):
if(config.has_section('quickLike')):
quickLogger.info("quickLike config exists, overwriting...")
else:
config.add_section('quickLike')
for variable, value in likelihoodDictionary.iteritems():
config.set('quickLike', variable, value)
quickLogger.info("wrote quickLikeconfig to "+basename+".cfg.")
if(plotDictionary):
if(config.has_section('quickPlot')):
quickLogger.info("quickPlot config exists, overwriting...")
else:
config.add_section('quickPlot')
for variable, value in plotDictionary.iteritems():
config.set('quickPlot', variable, value)
quickLogger.info("wrote quickPlot config to "+basename+".cfg.")
if(curveDictionary):
if(config.has_section('quickCurve')):
quickLogger.info("quickCurve config exists, overwriting...")
else:
config.add_section('quickCurve')
for variable, value in curveDictionary.iteritems():
config.set('quickCurve', variable, value)
quickLogger.info("wrote quickCurve config to "+basename+".cfg.")
with open(basename+'.cfg', 'wb') as configfile:
config.write(configfile)
def readConfig(quickLogger,basename):
"""Returns all of the needed information from the config file
called <basename>.cfg. Also checks to make sure all of the
config parameters are set based on the configure dictionaries
given in the configDictionaryList."""
commonDictionary = {}
analysisDictionary = {}
likelihoodDictionary = {}
plotDictionary = {}
curveDictionary = {}
try:
checkForFiles(quickLogger,[basename+".cfg"])
quickLogger.info('Reading from config file ('+basename+'.cfg)')
config = ConfigParser.RawConfigParser()
config.read(basename+'.cfg')
if(config.has_section('common')):
quickLogger.info('Reading common variables...')
commonDictionary = dict(config.items('common'))
if( commonDictionary['binned'] in ['True', 'true', '1', 'yes']):
commonDictionary['binned'] = True
else:
commonDictionary['binned'] = False
if(config.has_section('quickAnalysis')):
quickLogger.info('Reading quickAnalysis variables...')
analysisDictionary = dict(config.items('quickAnalysis'))
if(config.has_section('quickLike')):
quickLogger.info('Reading quickLike variables...')
likelihoodDictionary = dict(config.items('quickLike'))
if(config.has_section('quickPlot')):
quickLogger.info('Reading quickPlot variables...')
plotDictionary = dict(config.items('quickPlot'))
if(config.has_section('quickCurve')):
quickLogger.info('Reading quickCurve variables...')
curveDictionary = dict(config.items('quickCurve'))
if( curveDictionary['sliding'] in ['True', 'true', '1', 'yes']):
curveDictionary['sliding'] = True
else:
curveDictionary['sliding'] = False
return commonDictionary,analysisDictionary,likelihoodDictionary,plotDictionary,curveDictionary
except(FileNotFound):
raise FileNotFound
return
def checkConfig(quickLogger, referenceDictionary,testDictionary):
"""Checks a dictionary against a refernece to make sure that all
of the parameters are there. If all is good, it'll returen the
checked dictionary. If not, it'll return the reference dictionary
and raise an exception."""
try:
for key in referenceDictionary:
item = testDictionary[key]
return testDictionary
except KeyError as inst:
quickLogger.critical("Cannot find "+inst.args[0]+" in the config file.")
raise KeyError
return referenceDictionary
def initLogger(base, name):
"""Sets up and returns a properly configured logging object."""
quickLogger = logging.getLogger(name)
quickLogger.setLevel(logging.DEBUG)
#Prevents duuplicate log entries after reinitialization.
if(not quickLogger.handlers):
fh = logging.FileHandler(base+'_'+name+'.log')
fh.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
quickLogger.addHandler(fh)
quickLogger.addHandler(ch)
return quickLogger
def NumberOfPixels(radius, bin_size):
"""Returns the number of pixels needed to fill in the largest
possible square subtended by the circle with the given radius.
The size of the bins is also needed."""
return int((radius/math.sqrt(2.0))*2.0 / bin_size)
def generateXMLmodel(quickLogger,
base,
galactic_file="gll_iem_v05.fit",
galactic_name="gll_iem_v05",
isotropic_file="iso_source_v05.txt",
isotropic_name="iso_source_v05",
catalog_file="gll_psc_v08.fit"):
"""Checks to see if <basename>_model.xml exists and creates one
using the make2FGLXML module if it doesn't. make2FGLXml.py must
be in the working directory or accessable to python for this
function to work. The galactic and isotropic models plus the
Fermi LAT catalog must also be in the working directory.
Additionally, if any extended sources are in the ROI, the diffuse
templates for those sources should be in the working directory."""
try:
checkForFiles(quickLogger,[base+"_model.xml"])
quickLogger.info(base+"_model.xml exists, won't create a new one.")
except(FileNotFound):
quickLogger.info(base+"_model.xml doesn't exist, will create a new one.")
try:
checkForFiles(quickLogger,[base+"_filtered_gti.fits",galactic_file,isotropic_file,catalog_file])
import make2FGLxml
mymodel = make2FGLxml.srcList(catalog_file,base+"_filtered_gti.fits",base+"_model.xml")
mymodel.makeModel(galactic_file, galactic_name, isotropic_file, isotropic_name)
quickLogger.info("NOTE: if there are extended sources in your ROI, make sure the "\
+"correspoinding diffuse template is in the working directory.")
except(FileNotFound):
raise FileNotFound
def runCommand(AppCommand,quickLogger,run=True,printCmd=False):
"""Runs a giving command if run is True. If run is False,
prints out what the function would run."""
if(run):
AppCommand.run(print_command=printCmd)
quickLogger.info(AppCommand.command())
else:
print AppCommand.command()
def runModel(quickLogger,
base,
modelFile="",
irfs="P7REP_SOURCE_V15",
run=True):
"""Generates a model map. You need to have already run
the general quickAnlysis tool and then fit your model with
quickLike so that all of the needed files exsist."""
if(modelFile):
model = modelFile
else:
model = base+"_likeMinuit.xml"
try:
checkForFiles(quickLogger,
[base+"_srcMaps.fits",
model,
base+"_ltcube.fits",
base+"_BinnedExpMap.fits"])
except(FileNotFound):
quickLogger.critical("One or more needed files do not exist.")
return
model_map['srcmaps'] = base+"_srcMaps.fits"
model_map['srcmdl'] = model
model_map['outfile'] = base+"_modelMap.fits"
model_map['expcube'] = base+"_ltcube.fits"
model_map['irfs'] = irfs
model_map['bexpmap'] = base+"_BinnedExpMap.fits"
runCommand(model_map,quickLogger,run)
def runCMAP(quickLogger,
base,
rad,
binsize,
ra,
dec,
nxpix,
nypix,
run=True):
"""Generates a counts map. The dimensions of which are the
largest square subtended by the ROI. Note that if the ROI is
exceptionally small or the bin size exceptionally large, the
square might not be the largest posible since the npix
calculation floors the calculated value."""
if nxpix < 0 or nypix < 0:
nxpix = NumberOfPixels(float(rad),float(binsize))
nypix = NumberOfPixels(float(rad),float(binsize))
evtbin['evfile'] = base+'_filtered_gti.fits'
evtbin['outfile'] = base+'_CMAP.fits'
evtbin['scfile'] = base+"_SC.fits"
evtbin['algorithm'] = 'CMAP'
evtbin['nxpix'] = nxpix
evtbin['nypix'] = nypix
evtbin['binsz'] = binsize
evtbin['coordsys'] = 'CEL'
evtbin['xref'] = ra
evtbin['yref'] = dec
evtbin['axisrot'] = 0
evtbin['proj'] = 'AIT'
runCommand(evtbin,quickLogger,run)
class quickMath:
'''This class is used in the quickCurve script and are various
statistical functions developed by Stephen Fegan
<[email protected]> based on Numerical recipes in C.'''
_def_itmax = 100
_def_reltol = 1e-15
@staticmethod
def _gamma_ser(x, a, gammalna = None):
'''See Numerical recipes in C - eq 6.2.5.'''
if x<=0.0:
if x==0.0: return 0.0
else: raise ValueError("Argument x is negative: x=%f"%x)
if a<=0.0:
raise ValueError("Argument a is zero or negative: a=%f"%a)
if gammalna == None:
gammalna = math.lgamma(a)
actr = a
dsum = 1.0/a
sum = dsum
for i in range(1,quickMath._def_itmax):
actr += 1.0
dsum *= x/actr
# print i,dsum,sum,sum+dsum
sum += dsum
if math.fabs(dsum) < quickMath._def_reltol*math.fabs(sum):
return sum*math.exp(a*math.log(x) - x - math.lgamma(a))
raise RuntimeError("Maximum number of iterations exceeded")
@staticmethod
def _gamma_cfrac(x, a, gammalna = None):
# See Numerical recipes in C - eq 6.2.7 and section 5.2
if x<=0.0:
if x==0.0: return 0.0
else: raise ValueError("Argument x is negative: x=%f"%x)
if a<=0.0:
raise ValueError("Argument a is zero or negative: a=%f"%a)
if gammalna == None:
gammalna = math.lgamma(a)
tiny = 1e-30
A = 0.0
B = x+1.0-a
F = B
if F == 0.0: F = tiny
C = F
D = 0.0
for i in range(1,quickMath._def_itmax):
A += a + 1.0 - 2.0*i
B += 2.0
D = B + A*D
if D == 0.0: D = tiny
C = B + A/C
if C == 0.0: C = tiny
D = 1.0/D
delta = C*D
F *= delta
# print i,F
if math.fabs(delta-1.0) < quickMath._def_reltol:
return math.exp(a*math.log(x) - x - gammalna)/F
raise RuntimeError("Maximum number of iterations exceeded")
@staticmethod
def gammainc(x, a):
if x<=0.0:
if x==0.0: return 0.0
else: raise ValueError("Argument x is negative: x=%f"%x)
if a<=0.0:
raise ValueError("Argument a is zero or negative: a=%f"%a)
if x<a+1.0:
return quickMath._gamma_ser(x, a)
else:
return 1.0-quickMath._gamma_cfrac(x, a)
@staticmethod
def gammaincc(x, a):
if x<=0.0:
if x==0.0: return 0.0
else: raise ValueError("Argument x is negative: x=%f"%x)
if a<=0.0:
raise ValueError("Argument a is zero or negative: a=%f"%a)
if x<a+1.0:
return 1.0-quickMath._gamma_ser(x, a)
else:
return quickMath._gamma_cfrac(x, a)
@staticmethod
def gammainv(p, a):
if p<=0.0:
if p==0.0: return 0.0
else: raise ValueError("Argument p is negative: p=%f"%p)
elif p>=1:
if p==1: return float('infinity')
else: raise ValueError("Argument p is greater than unity: p=%f"%p)
if a<=0:
raise ValueError("Argument a is zero or negative: a=%f"%a)
gammalna = math.lgamma(a)
xtest = a+1.0
ftest = quickMath._gamma_ser(xtest, a, gammalna)
ffind = p
if(ffind<ftest):
f = lambda x: quickMath._gamma_ser(x, a, gammalna)
dfdx = lambda x,f: math.exp((a-1.0)*math.log(x) - x - gammalna)
ffind = p
if ffind<0.75:
xtest = math.exp((math.log(ffind) + gammalna + math.log(a))/a)
if ffind<0.1*quickMath._def_reltol:
return xtest
else:
f = lambda x: math.log(quickMath._gamma_cfrac(x, a, gammalna))
dfdx = lambda x,f: -math.exp((a-1.0)*math.log(x) - x - gammalna - f)
ffind = math.log(1-p)
ftest = f(xtest)
dfdxtest = dfdx(xtest, ftest)
for i in range(1,quickMath._def_itmax):
xnext = xtest - (ftest-ffind)/dfdxtest
if xnext <= 0.0:
xnext = 0.5*(xtest+0.0)
#print i, xtest, ftest, dfdxtest, xnext, ffind, math.fabs(xnext-xtest), 10*quickMath._def_reltol*(xnext+xtest)
if math.fabs(xnext-xtest) < 10*quickMath._def_reltol*(xnext+xtest):
return xnext
xtest = xnext
ftest = f(xtest)
dfdxtest = dfdx(xtest, ftest)
raise RuntimeError("Maximum number of iterations exceeded")
@staticmethod
def gammainvc(p, a):
if p<=0.0:
if p==0.0: return 0.0
else: raise ValueError("Argument p is negative: p=%f"%p)
elif p>=1:
if p==1: return float('infinity')
else: raise ValueError("Argument p is greater than unity: p=%f"%p)
if a<=0:
raise ValueError("Argument a is zero or negative: a=%f"%a)
gammalna = math.lgamma(a)
xtest = a+1.0
ftest = quickMath._gamma_cfrac(xtest, a, gammalna)
ffind = p
if(ffind<ftest):
f = lambda x: math.log(quickMath._gamma_cfrac(x, a, gammalna))
dfdx = lambda x,f: -math.exp((a-1.0)*math.log(x) - x - gammalna - f)
ffind = math.log(p)
else:
f = lambda x: quickMath._gamma_ser(x, a, gammalna)
dfdx = lambda x,f: math.exp((a-1.0)*math.log(x) - x - gammalna)
ffind = 1-p
if ffind<0.75:
xtest = math.exp((math.log(ffind) + gammalna + math.log(a))/a)
if ffind<0.1*quickMath._def_reltol:
return xtest
ftest = f(xtest)
dfdxtest = dfdx(xtest, ftest)
for i in range(1,quickMath._def_itmax):
xnext = xtest - (ftest-ffind)/dfdxtest
if xnext <= 0.0:
xnext = 0.5*(xtest+0.0)
# print i, xtest, ftest, dfdxtest, xnext, ffind, math.fabs(xnext-xtest), 10*quickMath._def_reltol*(xnext+xtest)
if math.fabs(xnext-xtest) < 0.5*quickMath._def_reltol*(xnext+xtest):
return xnext
xtest = xnext
ftest = f(xtest)
dfdxtest = dfdx(xtest, ftest)
raise RuntimeError("Maximum number of iterations exceeded")
@staticmethod
def chi2cdf(x, a):
return quickMath.gammainc(0.5*x, 0.5*a)
@staticmethod
def chi2cdfc(x, a):
return quickMath.gammaincc(0.5*x, 0.5*a)
@staticmethod
def chi2inv(p, a):
return 2.0*quickMath.gammainv(p, 0.5*a)
@staticmethod
def chi2invc(p, a):
return 2.0*quickMath.gammainvc(p, 0.5*a)