Commit d2ce573f21f4573cf3785cdbff3b5ce28d173b4b

Authored by Jürgen Knödlseder
2 parents a552305f 77f1091b

Merge branch '1828-enhance-code-quality' into integration

cscripts/cssens.py
... ... @@ -373,7 +373,17 @@ class cssens(ctools.cscript):
373 373 logL = fit.opt().value()
374 374 npred = fit.obs().npred()
375 375 models = fit.obs().models()
376   - ts = models[self._srcname].ts()
  376 + source = models[self._srcname]
  377 + ts = source.ts()
  378 +
  379 + # Get fitted Crab, photon and energy fluxes
  380 + crab_flux = source['Prefactor'].value() / crab_prefactor
  381 + photon_flux = source.spectral().flux(emin, emax)
  382 + energy_flux = source.spectral().eflux(emin, emax)
  383 +
  384 + # Compute differential sensitivity in unit erg/cm2/s
  385 + energy = gammalib.GEnergy(e_mean, 'TeV')
  386 + sensitivity = source.spectral().eval(energy) * e_mean*erg_mean * 1.0e6
377 387  
378 388 # Assess quality based on a comparison between Npred and Nevents
379 389 quality = npred - nevents
... ... @@ -400,27 +410,30 @@ class cssens(ctools.cscript):
400 410 for par in model:
401 411 self._log(str(par)+'\n')
402 412  
403   - # Start over if TS was non-positive
  413 + # If TS was non-positive then increase the test flux and start
  414 + # over
404 415 if ts <= 0.0:
405   - if self._logExplicit():
406   - self._log('Non positive TS. Start over.\n')
407   - continue
408 416  
409   - # Get fitted Crab, photon and energy fluxes
410   - crab_flux = models[self._srcname]['Prefactor'].value() / \
411   - crab_prefactor
412   - photon_flux = models[self._srcname].spectral().flux(emin, emax)
413   - energy_flux = models[self._srcname].spectral().eflux(emin, emax)
  417 + # If the number of iterations was exceeded then stop
  418 + if (iterations >= self._max_iter):
  419 + if self._logTerse():
  420 + self._log(' Test ended after %d iterations.\n' %
  421 + self._max_iter)
  422 + break
414 423  
415   - # Compute differential sensitivity in unit erg/cm2/s
416   - energy = gammalib.GEnergy(e_mean, 'TeV')
417   - sensitivity = models[self._srcname].spectral().eval(energy) * \
418   - e_mean*erg_mean * 1.0e6
  424 + # Increase test flux by a factor of 2
  425 + test_crab_flux = test_crab_flux * 2.0
  426 +
  427 + # Signal start we start over
  428 + if self._logExplicit():
  429 + self._log('Non positive TS, increase test flux and start '
  430 + 'over.\n')
  431 +
  432 + # ... and start over
  433 + continue
419 434  
420 435 # Compute flux correction factor based on average TS
421   - correct = 1.0
422   - if ts > 0:
423   - correct = math.sqrt(self._ts_thres/ts)
  436 + correct = math.sqrt(self._ts_thres/ts)
424 437  
425 438 # Compute extrapolated fluxes
426 439 crab_flux = correct * crab_flux
... ... @@ -432,7 +445,7 @@ class cssens(ctools.cscript):
432 445 energy_flux_value.append(energy_flux)
433 446 sensitivity_value.append(sensitivity)
434 447  
435   - # Write background and test source fit results
  448 + # Write fit results
436 449 if self._logExplicit():
437 450 self._log.parformat('Photon flux')
438 451 self._log(photon_flux)
... ... @@ -453,7 +466,7 @@ class cssens(ctools.cscript):
453 466 for par in model:
454 467 self._log(str(par)+'\n')
455 468 elif self._logTerse():
456   - self._log.parformat('Iteration '+str(iterations))
  469 + self._log.parformat('Iteration %d' % iterations)
457 470 self._log('TS=')
458 471 self._log(ts)
459 472 self._log(' ')
... ... @@ -589,7 +602,19 @@ class cssens(ctools.cscript):
589 602 results = []
590 603  
591 604 # Set test source model for this observation
592   - models = modutils.test_source(self._obs.models(), self._srcname)
  605 + models = modutils.test_source(self._obs.models(), self._srcname,
  606 + ra=self._ra, dec=self._dec)
  607 +
  608 + # Write observations into logger
  609 + if self._logTerse():
  610 + self._log('\n')
  611 + self._log.header1(gammalib.number('Observation',len(self._obs)))
  612 + self._log(str(self._obs))
  613 + self._log('\n')
  614 + if self._logExplicit():
  615 + for obs in self._obs:
  616 + self._log(str(obs))
  617 + self._log('\n')
593 618  
594 619 # Write models into logger
595 620 if self._logTerse():
... ...
examples/pipeline_binned_disk.py
... ... @@ -155,4 +155,5 @@ if __name__ == &#39;__main__&#39;:
155 155 obs.models(gammalib.GModels(datadir+'/crab.xml'))
156 156  
157 157 # Run analysis pipeline
158   - run_pipeline(obs, model=datadir+'/crab.xml')
  158 + run_pipeline(obs, model=datadir+'/crab.xml', enumbins=10, nxpix=40,
  159 + nypix=40, binsz=0.1)
... ...
examples/pipeline_binned_mem.py
... ... @@ -126,4 +126,4 @@ if __name__ == &#39;__main__&#39;:
126 126 obs.models(gammalib.GModels(datadir+'/crab.xml'))
127 127  
128 128 # Run analysis pipeline
129   - run_pipeline(obs)
  129 + run_pipeline(obs, enumbins=10, nxpix=40, nypix=40, binsz=0.1)
... ...
examples/pipeline_stacked_disk.py
... ... @@ -205,4 +205,5 @@ if __name__ == &#39;__main__&#39;:
205 205 obs.models(gammalib.GModels(datadir+'/crab.xml'))
206 206  
207 207 # Run analysis pipeline
208   - run_pipeline(obs, model=datadir+'/crab.xml')
  208 + run_pipeline(obs, model=datadir+'/crab.xml', enumbins=10, nxpix=40,
  209 + nypix=40, binsz=0.1)
... ...
examples/pipeline_stacked_mem.py
... ... @@ -178,4 +178,4 @@ if __name__ == &#39;__main__&#39;:
178 178 obs.models(gammalib.GModels(datadir+'/crab.xml'))
179 179  
180 180 # Run analysis pipeline
181   - run_pipeline(obs)
  181 + run_pipeline(obs, enumbins=10, nxpix=40, nypix=40, binsz=0.1)
... ...
test/test_cspull.py
... ... @@ -146,7 +146,7 @@ class Test(test):
146 146 pull = cscripts.cspull()
147 147 pull['inmodel'] = self._model
148 148 pull['outfile'] = 'cspull_py2.dat'
149   - pull['ntrials'] = 2
  149 + pull['ntrials'] = 1
150 150 pull['caldb'] = self._caldb
151 151 pull['irf'] = self._irf
152 152 pull['ra'] = 83.6331
... ... @@ -157,8 +157,8 @@ class Test(test):
157 157 pull['tmax'] = 100.0
158 158 pull['deadc'] = 0.95
159 159 pull['rad'] = 5.0
160   - pull['npix'] = 100
161   - pull['binsz'] = 0.02
  160 + pull['npix'] = 20
  161 + pull['binsz'] = 0.2
162 162 pull['coordsys'] = 'CEL'
163 163 pull['proj'] = 'TAN'
164 164 pull['logfile'] = 'cspull_py2.log'
... ... @@ -168,14 +168,14 @@ class Test(test):
168 168 pull.execute()
169 169  
170 170 # Check pull distribution file
171   - self._check_pull_file('cspull_py2.dat')
  171 + self._check_pull_file('cspull_py2.dat', rows=2)
172 172  
173 173 # Set-up cspull from event list
174 174 pull = cscripts.cspull()
175 175 pull['inobs'] = self._events
176 176 pull['inmodel'] = self._model
177 177 pull['outfile'] = 'cspull_py3.dat'
178   - pull['ntrials'] = 2
  178 + pull['ntrials'] = 1
179 179 pull['caldb'] = self._caldb
180 180 pull['irf'] = self._irf
181 181 pull['enumbins'] = 0
... ... @@ -186,7 +186,7 @@ class Test(test):
186 186 pull.execute()
187 187  
188 188 # Check pull distribution file
189   - self._check_pull_file('cspull_py3.dat')
  189 + self._check_pull_file('cspull_py3.dat', rows=2)
190 190  
191 191 # Build observation container with unbinned observation
192 192 cta = gammalib.GCTAObservation(self._events)
... ... @@ -197,7 +197,7 @@ class Test(test):
197 197 pull = cscripts.cspull(obs)
198 198 pull['inmodel'] = self._model
199 199 pull['outfile'] = 'cspull_py4.dat'
200   - pull['ntrials'] = 2
  200 + pull['ntrials'] = 1
201 201 pull['caldb'] = self._caldb
202 202 pull['irf'] = self._irf
203 203 pull['enumbins'] = 0
... ... @@ -208,7 +208,7 @@ class Test(test):
208 208 pull.execute()
209 209  
210 210 # Check pull distribution file
211   - self._check_pull_file('cspull_py4.dat')
  211 + self._check_pull_file('cspull_py4.dat', rows=2)
212 212  
213 213 # Set-up stacked cspull with one observation where response cubes
214 214 # are specified in the input observation
... ... @@ -216,7 +216,7 @@ class Test(test):
216 216 pull['inobs'] = self._inobs
217 217 pull['inmodel'] = self._stacked_model
218 218 pull['outfile'] = 'cspull_py5.dat'
219   - pull['ntrials'] = 2
  219 + pull['ntrials'] = 1
220 220 pull['enumbins'] = 0
221 221 pull['logfile'] = 'cspull_py5.log'
222 222 pull['chatter'] = 4
... ... @@ -225,7 +225,7 @@ class Test(test):
225 225 pull.execute()
226 226  
227 227 # Check pull distribution file
228   - self._check_pull_file('cspull_py5.dat')
  228 + self._check_pull_file('cspull_py5.dat', rows=2)
229 229  
230 230 # Set-up stacked cspull with two observations from which response
231 231 # cubes will be computed internally. The IRFs are also specified
... ... @@ -235,7 +235,7 @@ class Test(test):
235 235 pull['inobs'] = self._inobs_two
236 236 pull['inmodel'] = self._model
237 237 pull['outfile'] = 'cspull_py6.dat'
238   - pull['ntrials'] = 2
  238 + pull['ntrials'] = 1
239 239 pull['emin'] = 0.02
240 240 pull['emax'] = 100.0
241 241 pull['enumbins'] = 10
... ... @@ -250,13 +250,13 @@ class Test(test):
250 250 pull.execute()
251 251  
252 252 # Check pull distribution file
253   - self._check_pull_file('cspull_py6.dat')
  253 + self._check_pull_file('cspull_py6.dat', rows=2)
254 254  
255 255 # Return
256 256 return
257 257  
258 258 # Check pull file
259   - def _check_pull_file(self, filename):
  259 + def _check_pull_file(self, filename, rows=3):
260 260 """
261 261 Check pull file
262 262 """
... ... @@ -264,8 +264,8 @@ class Test(test):
264 264 pulls = gammalib.GCsv(filename)
265 265  
266 266 # Check dimensions
267   - self.test_value(pulls.nrows(), 3,
268   - 'Check for 3 rows in pull file')
  267 + self.test_value(pulls.nrows(), rows,
  268 + 'Check for number of rows in pull file')
269 269  
270 270 # Return
271 271 return
... ...
test/test_cssens.py
... ... @@ -72,7 +72,7 @@ class Test(test):
72 72 cmd = cssens+' inmodel="'+self._model+'"'+ \
73 73 ' srcname="Crab"'+ \
74 74 ' caldb="'+self._caldb+'" irf="'+self._irf+'"'+ \
75   - ' duration=500.0 rad=3.0 emin=1.0 emax=10.0 bins=1'+ \
  75 + ' duration=1800.0 rad=3.0 emin=1.0 emax=10.0 bins=1'+ \
76 76 ' outfile="cssens_cmd1.dat"'+ \
77 77 ' logfile="cssens_cmd1.log" chatter=1'
78 78  
... ... @@ -91,7 +91,7 @@ class Test(test):
91 91 cmd = cssens+' inmodel="model_that_does_not_exist.xml"'+ \
92 92 ' srcname="Crab"'+ \
93 93 ' caldb="'+self._caldb+'" irf="'+self._irf+'"'+ \
94   - ' duration=500.0 rad=3.0 emin=1.0 emax=10.0 bins=1'+ \
  94 + ' duration=1800.0 rad=3.0 emin=1.0 emax=10.0 bins=1'+ \
95 95 ' outfile="cssens_cmd2.dat"'+ \
96 96 ' logfile="cssens_cmd2.log" chatter=2'
97 97  
... ... @@ -115,7 +115,7 @@ class Test(test):
115 115 sens['caldb'] = self._caldb
116 116 sens['irf'] = self._irf
117 117 sens['outfile'] = 'cssens_py1.dat'
118   - sens['duration'] = 500.0
  118 + sens['duration'] = 1800.0
119 119 sens['rad'] = 3.0
120 120 sens['emin'] = 1.0
121 121 sens['emax'] = 10.0
... ...
test/test_ctmapcube.py
... ... @@ -71,8 +71,8 @@ class Test(test):
71 71 # Setup ctmapcube command
72 72 cmd = ctmapcube+' inmodel="'+self._model+'"'+ \
73 73 ' outcube="ctmapcube_cmd1.fits"'+\
74   - ' emin=0.1 emax=100.0 enumbins=20 ebinalg=LOG'+ \
75   - ' nxpix=200 nypix=200 binsz=0.02 coordsys=CEL'+ \
  74 + ' emin=0.1 emax=100.0 enumbins=10 ebinalg=LOG'+ \
  75 + ' nxpix=100 nypix=100 binsz=0.04 coordsys=CEL'+ \
76 76 ' xref=83.63 yref=22.01 proj=CAR'+ \
77 77 ' logfile="ctmapcube_cmd1.log" chatter=1'
78 78  
... ... @@ -90,8 +90,8 @@ class Test(test):
90 90 # Setup ctmapcube command
91 91 cmd = ctmapcube+' inmodel="events_that_do_not_exist.fits"'+ \
92 92 ' outcube="ctmapcube_cmd2.fits"'+\
93   - ' emin=0.1 emax=100.0 enumbins=20 ebinalg=LOG'+ \
94   - ' nxpix=200 nypix=200 binsz=0.02 coordsys=CEL'+ \
  93 + ' emin=0.1 emax=100.0 enumbins=10 ebinalg=LOG'+ \
  94 + ' nxpix=100 nypix=100 binsz=0.04 coordsys=CEL'+ \
95 95 ' xref=83.63 yref=22.01 proj=CAR'+ \
96 96 ' logfile="ctmapcube_cmd2.log"'
97 97  
... ... @@ -114,10 +114,10 @@ class Test(test):
114 114 mapcube['ebinalg'] = 'LOG'
115 115 mapcube['emin'] = 0.1
116 116 mapcube['emax'] = 100.0
117   - mapcube['enumbins'] = 20
118   - mapcube['nxpix'] = 200
119   - mapcube['nypix'] = 200
120   - mapcube['binsz'] = 0.02
  117 + mapcube['enumbins'] = 10
  118 + mapcube['nxpix'] = 100
  119 + mapcube['nypix'] = 100
  120 + mapcube['binsz'] = 0.04
121 121 mapcube['coordsys'] = 'CEL'
122 122 mapcube['proj'] = 'CAR'
123 123 mapcube['xref'] = 83.63
... ... @@ -152,12 +152,12 @@ class Test(test):
152 152 cube = gammalib.GModelSpatialDiffuseCube(filename)
153 153  
154 154 # Test map cube
155   - #self.test_value(cube.maps(), 20, "20 maps")
156   - #self.test_value(cube.pixels(), 40000, "40000 map pixels")
  155 + #self.test_value(cube.maps(), 10, "10 maps")
  156 + #self.test_value(cube.pixels(), 10000, "10000 map pixels")
157 157 # The map cube is not loaded by default !!!! We should add the method so that
158 158 # the attributes are known !!!!
159   - self.test_value(cube.maps(), 0, '20 maps')
160   - self.test_value(cube.pixels(), 0, '40000 map pixels')
  159 + self.test_value(cube.maps(), 0, '10 maps')
  160 + self.test_value(cube.pixels(), 0, '10000 map pixels')
161 161  
162 162 # Return
163 163 return
... ...
test/test_cttsmap.py
... ... @@ -69,7 +69,7 @@ class Test(test):
69 69 ' inmodel="'+self._model+'" srcname="Crab"'+ \
70 70 ' caldb="'+self._caldb+'" irf="'+self._irf+'"'+ \
71 71 ' outmap="cttsmap_cmd1.fits"'+ \
72   - ' nxpix=5 nypix=5 binsz=0.02'+ \
  72 + ' nxpix=3 nypix=3 binsz=0.02'+ \
73 73 ' coordsys="CEL" proj="CAR" xref=83.63 yref=22.01'+ \
74 74 ' logfile="cttsmap_cmd1.log" chatter=1'
75 75  
... ... @@ -89,7 +89,7 @@ class Test(test):
89 89 ' inmodel="'+self._model+'" srcname="Crab"'+ \
90 90 ' caldb="'+self._caldb+'" irf="'+self._irf+'"'+ \
91 91 ' outmap="cttsmap_cmd2.fits"'+ \
92   - ' nxpix=5 nypix=5 binsz=0.02'+ \
  92 + ' nxpix=3 nypix=3 binsz=0.02'+ \
93 93 ' coordsys="CEL" proj="CAR" xref=83.63 yref=22.01'+ \
94 94 ' logfile="cttsmap_cmd2.log" chatter=2'
95 95  
... ... @@ -113,8 +113,8 @@ class Test(test):
113 113 tsmap['caldb'] = self._caldb
114 114 tsmap['irf'] = self._irf
115 115 tsmap['outmap'] = 'cttsmap_py1.fits'
116   - tsmap['nxpix'] = 5
117   - tsmap['nypix'] = 5
  116 + tsmap['nxpix'] = 3
  117 + tsmap['nypix'] = 3
118 118 tsmap['binsz'] = 0.02
119 119 tsmap['coordsys'] = 'CEL'
120 120 tsmap['proj'] = 'CAR'
... ... @@ -149,14 +149,14 @@ class Test(test):
149 149  
150 150 # Check dimensions
151 151 self.test_value(ts.naxis(), 2, 'Check for 2 dimensions')
152   - self.test_value(ts.naxes(0), 5, 'Check for 5 pixels in X')
153   - self.test_value(ts.naxes(1), 5, 'Check for 5 pixels in Y')
  152 + self.test_value(ts.naxes(0), 3, 'Check for 3 pixels in X')
  153 + self.test_value(ts.naxes(1), 3, 'Check for 3 pixels in Y')
154 154 self.test_value(prefactor.naxis(), 2, 'Check for 2 dimensions')
155   - self.test_value(prefactor.naxes(0), 5, 'Check for 5 pixels in X')
156   - self.test_value(prefactor.naxes(1), 5, 'Check for 5 pixels in Y')
  155 + self.test_value(prefactor.naxes(0), 3, 'Check for 3 pixels in X')
  156 + self.test_value(prefactor.naxes(1), 3, 'Check for 3 pixels in Y')
157 157 self.test_value(index.naxis(), 2, 'Check for 2 dimensions')
158   - self.test_value(index.naxes(0), 5, 'Check for 5 pixels in X')
159   - self.test_value(index.naxes(1), 5, 'Check for 5 pixels in Y')
  158 + self.test_value(index.naxes(0), 3, 'Check for 3 pixels in X')
  159 + self.test_value(index.naxes(1), 3, 'Check for 3 pixels in Y')
160 160  
161 161 # Return
162 162 return
... ...
test/test_examples.py.in
... ... @@ -186,7 +186,7 @@ class Test(gammalib.GPythonTestSuite):
186 186 Test make_ts_distributions
187 187 """
188 188 # Set arguments
189   - args = '-n 2 -e 0 -t 1800 -d %s' % self._datadir
  189 + args = '-n 1 -e 0 -t 100 -d %s' % self._datadir
190 190  
191 191 # Execute script
192 192 self._execute_python('make_ts_distributions', args=args)
... ...