Commit 99ab76debda844c7af027efc3cc046a67746fc96

Authored by Jürgen Knödlseder
1 parent 9b24f41f

Increase code coverage

pyext/Makefile.am
@@ -72,7 +72,9 @@ testdata = $(top_srcdir)/test/data/crab.xml \ @@ -72,7 +72,9 @@ testdata = $(top_srcdir)/test/data/crab.xml \
72 $(top_srcdir)/test/data/model_temporal_phasecurve.fits \ 72 $(top_srcdir)/test/data/model_temporal_phasecurve.fits \
73 $(top_srcdir)/test/data/obs_unbinned.xml \ 73 $(top_srcdir)/test/data/obs_unbinned.xml \
74 $(top_srcdir)/test/data/exclusion.reg \ 74 $(top_srcdir)/test/data/exclusion.reg \
75 - $(top_srcdir)/test/data/irf_file.fits.gz 75 + $(top_srcdir)/test/data/exclusion.fits \
  76 + $(top_srcdir)/test/data/irf_file.fits.gz \
  77 + $(top_srcdir)/test/data/skymap_ring.fits
76 78
77 # Install test scripts and test data. Note that we don't need to prepend 79 # Install test scripts and test data. Note that we don't need to prepend
78 # $(DESTDIR) here (not fully clear why, but that's the way it is) 80 # $(DESTDIR) here (not fully clear why, but that's the way it is)
test/data/exclusion.fits 0 → 100644
No preview for this file type
test/data/skymap_ring.fits 0 → 100644
No preview for this file type
test/test_csspec.py
@@ -118,7 +118,7 @@ class Test(test): @@ -118,7 +118,7 @@ class Test(test):
118 """ 118 """
119 Test csspec from Python 119 Test csspec from Python
120 """ 120 """
121 - # Set-up unbinned csspec 121 + # Set-up unbinned csspec (no multi-processing)
122 spec = cscripts.csspec() 122 spec = cscripts.csspec()
123 spec['inobs'] = self._events 123 spec['inobs'] = self._events
124 spec['inmodel'] = self._model 124 spec['inmodel'] = self._model
@@ -130,6 +130,7 @@ class Test(test): @@ -130,6 +130,7 @@ class Test(test):
130 spec['enumbins'] = 2 130 spec['enumbins'] = 2
131 spec['emin'] = 1.0 131 spec['emin'] = 1.0
132 spec['emax'] = 100.0 132 spec['emax'] = 100.0
  133 + spec['nthreads'] = 1
133 spec['outfile'] = 'csspec_py1.fits' 134 spec['outfile'] = 'csspec_py1.fits'
134 spec['logfile'] = 'csspec_py1.log' 135 spec['logfile'] = 'csspec_py1.log'
135 spec['chatter'] = 2 136 spec['chatter'] = 2
@@ -143,7 +144,7 @@ class Test(test): @@ -143,7 +144,7 @@ class Test(test):
143 # Check result file 144 # Check result file
144 self._check_result_file('csspec_py1.fits', 2) 145 self._check_result_file('csspec_py1.fits', 2)
145 146
146 - # Set-up stacked csspec 147 + # Set-up stacked csspec (no multi-processing)
147 spec = cscripts.csspec() 148 spec = cscripts.csspec()
148 spec['inobs'] = self._cntcube 149 spec['inobs'] = self._cntcube
149 spec['expcube'] = self._expcube 150 spec['expcube'] = self._expcube
@@ -157,6 +158,7 @@ class Test(test): @@ -157,6 +158,7 @@ class Test(test):
157 spec['enumbins'] = 2 158 spec['enumbins'] = 2
158 spec['emin'] = 0.1 159 spec['emin'] = 0.1
159 spec['emax'] = 100.0 160 spec['emax'] = 100.0
  161 + spec['nthreads'] = 1
160 spec['outfile'] = 'csspec_py2.fits' 162 spec['outfile'] = 'csspec_py2.fits'
161 spec['logfile'] = 'csspec_py2.log' 163 spec['logfile'] = 'csspec_py2.log'
162 spec['chatter'] = 3 164 spec['chatter'] = 3
@@ -168,7 +170,7 @@ class Test(test): @@ -168,7 +170,7 @@ class Test(test):
168 # Check result file 170 # Check result file
169 self._check_result_file('csspec_py2.fits', 2) 171 self._check_result_file('csspec_py2.fits', 2)
170 172
171 - # Set-up On/Off csspec 173 + # Set-up On/Off csspec (no multi-processing)
172 spec = cscripts.csspec() 174 spec = cscripts.csspec()
173 spec['inobs'] = self._inonoff 175 spec['inobs'] = self._inonoff
174 spec['inmodel'] = self._onoff_model 176 spec['inmodel'] = self._onoff_model
@@ -178,6 +180,7 @@ class Test(test): @@ -178,6 +180,7 @@ class Test(test):
178 spec['enumbins'] = 2 180 spec['enumbins'] = 2
179 spec['emin'] = 1.0 181 spec['emin'] = 1.0
180 spec['emax'] = 10.0 182 spec['emax'] = 10.0
  183 + spec['nthreads'] = 1
181 spec['outfile'] = 'csspec_py3.fits' 184 spec['outfile'] = 'csspec_py3.fits'
182 spec['logfile'] = 'csspec_py3.log' 185 spec['logfile'] = 'csspec_py3.log'
183 spec['chatter'] = 4 186 spec['chatter'] = 4
@@ -231,32 +234,6 @@ class Test(test): @@ -231,32 +234,6 @@ class Test(test):
231 # Check result file 234 # Check result file
232 self._check_result_file('csspec_py5.fits', 2) 235 self._check_result_file('csspec_py5.fits', 2)
233 236
234 - # Set-up csspec without multiprocessing  
235 - spec = cscripts.csspec()  
236 - spec['inobs'] = self._events  
237 - spec['inmodel'] = self._model  
238 - spec['srcname'] = 'Crab'  
239 - spec['caldb'] = self._caldb  
240 - spec['irf'] = self._irf  
241 - spec['method'] = 'AUTO'  
242 - spec['ebinalg'] = 'LOG'  
243 - spec['enumbins'] = 2  
244 - spec['emin'] = 1.0  
245 - spec['emax'] = 100.0  
246 - spec['outfile'] = 'csspec_py6.fits'  
247 - spec['logfile'] = 'csspec_py6.log'  
248 - spec['chatter'] = 2  
249 - spec['publish'] = True  
250 - spec['nthreads'] = 1  
251 -  
252 - # Run csspec script  
253 - spec.logFileOpen() # Make sure we get a log file  
254 - spec.run()  
255 - spec.save()  
256 -  
257 - # Check result file  
258 - self._check_result_file('csspec_py6.fits', 2)  
259 -  
260 # Return 237 # Return
261 return 238 return
262 239
test/test_ctskymap.py
@@ -39,6 +39,11 @@ class Test(test): @@ -39,6 +39,11 @@ class Test(test):
39 # Call base class constructor 39 # Call base class constructor
40 test.__init__(self) 40 test.__init__(self)
41 41
  42 + # Set test data
  43 + self._skymap_ring = self._datadir + '/skymap_ring.fits'
  44 + self._exclusion_region = self._datadir + '/exclusion.reg'
  45 + self._exclusion_map = self._datadir + '/exclusion.fits'
  46 +
42 # Return 47 # Return
43 return 48 return
44 49
@@ -53,7 +58,11 @@ class Test(test): @@ -53,7 +58,11 @@ class Test(test):
53 # Append tests 58 # Append tests
54 self.append(self._test_cmd, 'Test ctskymap on command line') 59 self.append(self._test_cmd, 'Test ctskymap on command line')
55 self.append(self._test_python, 'Test ctskymap from Python') 60 self.append(self._test_python, 'Test ctskymap from Python')
56 - 61 + self.append(self._test_python_irf,
  62 + 'Test ctskymap from Python for IRF background')
  63 + self.append(self._test_python_ring,
  64 + 'Test ctskymap from Python for RING background')
  65 +
57 # Return 66 # Return
58 return 67 return
59 68
@@ -202,12 +211,17 @@ class Test(test): @@ -202,12 +211,17 @@ class Test(test):
202 # Publish with name 211 # Publish with name
203 skymap.publish('My sky map') 212 skymap.publish('My sky map')
204 213
205 - # ================================  
206 - # TEST IRF BACKGROUND SUBTRACTION  
207 - # ================================  
208 -  
209 - # Allocate ctskymap tool from observation container  
210 - skymap = ctools.ctskymap(obs) 214 + # Return
  215 + return
  216 +
  217 + # Test ctskymap from Python for IRF background method
  218 + def _test_python_irf(self):
  219 + """
  220 + Test ctskymap from Python for IRF background method
  221 + """
  222 + # Test IRF background subtraction
  223 + skymap = ctools.ctskymap()
  224 + skymap['inobs'] = self._events
211 skymap['emin'] = 0.1 225 skymap['emin'] = 0.1
212 skymap['emax'] = 100 226 skymap['emax'] = 100
213 skymap['nxpix'] = 20 227 skymap['nxpix'] = 20
@@ -220,25 +234,34 @@ class Test(test): @@ -220,25 +234,34 @@ class Test(test):
220 skymap['bkgsubtract'] = 'IRF' 234 skymap['bkgsubtract'] = 'IRF'
221 skymap['caldb'] = self._caldb 235 skymap['caldb'] = self._caldb
222 skymap['irf'] = self._irf 236 skymap['irf'] = self._irf
223 - skymap['outmap'] = 'ctskymap_py4.fits'  
224 - skymap['logfile'] = 'ctskymap_py4.log'  
225 - skymap['chatter'] = 4 237 + skymap['outmap'] = 'ctskymap_irf_py1.fits'
  238 + skymap['logfile'] = 'ctskymap_irf_py1.log'
  239 + skymap['chatter'] = 2
226 240
227 # Execute tool 241 # Execute tool
228 skymap.logFileOpen() # Needed to get a new log file 242 skymap.logFileOpen() # Needed to get a new log file
229 skymap.execute() 243 skymap.execute()
230 244
231 # Check result file 245 # Check result file
232 - self._check_result_file('ctskymap_py4.fits')  
233 - self._check_result_file('ctskymap_py4.fits[BACKGROUND]')  
234 - self._check_result_file('ctskymap_py4.fits[SIGNIFICANCE]') 246 + self._check_result_file('ctskymap_irf_py1.fits')
  247 + self._check_result_file('ctskymap_irf_py1.fits[BACKGROUND]')
  248 + self._check_result_file('ctskymap_irf_py1.fits[SIGNIFICANCE]')
235 249
236 - # Test RING background subtraction  
237 - skymap = ctools.ctskymap(obs) 250 + # Return
  251 + return
  252 +
  253 + # Test ctskymap from Python for RING background method
  254 + def _test_python_ring(self):
  255 + """
  256 + Test ctskymap from Python for RING background method
  257 + """
  258 + # Test basic RING background subtraction
  259 + skymap = ctools.ctskymap()
  260 + skymap['inobs'] = self._events
238 skymap['emin'] = 0.1 261 skymap['emin'] = 0.1
239 skymap['emax'] = 100 262 skymap['emax'] = 100
240 - skymap['nxpix'] = 10 # Make region smaller since the 'RING'  
241 - skymap['nypix'] = 10 # method takes longer to run than 'IRF' 263 + skymap['nxpix'] = 10
  264 + skymap['nypix'] = 10
242 skymap['binsz'] = 0.2 265 skymap['binsz'] = 0.2
243 skymap['coordsys'] = 'CEL' 266 skymap['coordsys'] = 'CEL'
244 skymap['proj'] = 'CAR' 267 skymap['proj'] = 'CAR'
@@ -252,18 +275,145 @@ class Test(test): @@ -252,18 +275,145 @@ class Test(test):
252 skymap['inexclusion'] = 'NONE' 275 skymap['inexclusion'] = 'NONE'
253 skymap['caldb'] = self._caldb 276 skymap['caldb'] = self._caldb
254 skymap['irf'] = self._irf 277 skymap['irf'] = self._irf
255 - skymap['outmap'] = 'ctskymap_py5.fits'  
256 - skymap['logfile'] = 'ctskymap_py5.log'  
257 - skymap['chatter'] = 4 278 + skymap['outmap'] = 'ctskymap_ring_py1.fits'
  279 + skymap['logfile'] = 'ctskymap_ring_py1.log'
  280 + skymap['chatter'] = 2
  281 +
  282 + # Execute tool
  283 + skymap.logFileOpen() # Needed to get a new log file
  284 + skymap.execute()
  285 +
  286 + # Check result file
  287 + self._check_result_file('ctskymap_ring_py1.fits', 10, 10)
  288 + self._check_result_file('ctskymap_ring_py1.fits[BACKGROUND]', 10, 10)
  289 + self._check_result_file('ctskymap_ring_py1.fits[SIGNIFICANCE]', 10, 10)
  290 +
  291 + # Test iterative RING background subtraction
  292 + skymap = ctools.ctskymap()
  293 + skymap['inobs'] = self._events
  294 + skymap['emin'] = 0.1
  295 + skymap['emax'] = 100
  296 + skymap['nxpix'] = 10
  297 + skymap['nypix'] = 10
  298 + skymap['binsz'] = 0.2
  299 + skymap['coordsys'] = 'CEL'
  300 + skymap['proj'] = 'CAR'
  301 + skymap['xref'] = 83.63
  302 + skymap['yref'] = 22.01
  303 + skymap['bkgsubtract'] = 'RING'
  304 + skymap['roiradius'] = 0.1
  305 + skymap['inradius'] = 0.6
  306 + skymap['outradius'] = 0.8
  307 + skymap['iterations'] = 2
  308 + skymap['threshold'] = 5.0
  309 + skymap['inexclusion'] = 'NONE'
  310 + skymap['caldb'] = self._caldb
  311 + skymap['irf'] = self._irf
  312 + skymap['outmap'] = 'ctskymap_ring_py2.fits'
  313 + skymap['logfile'] = 'ctskymap_ring_py2.log'
  314 + skymap['chatter'] = 2
  315 +
  316 + # Execute tool
  317 + skymap.logFileOpen() # Needed to get a new log file
  318 + skymap.execute()
  319 +
  320 + # Check result file
  321 + self._check_result_file('ctskymap_ring_py2.fits', 10, 10)
  322 + self._check_result_file('ctskymap_ring_py2.fits[BACKGROUND]', 10, 10)
  323 + self._check_result_file('ctskymap_ring_py2.fits[SIGNIFICANCE]', 10, 10)
  324 +
  325 + # Test iterative RING background subtraction, starting from a sky map
  326 + skymap = ctools.ctskymap()
  327 + skymap['inmap'] = self._skymap_ring
  328 + skymap['bkgsubtract'] = 'RING'
  329 + skymap['roiradius'] = 0.1
  330 + skymap['inradius'] = 0.6
  331 + skymap['outradius'] = 0.8
  332 + skymap['iterations'] = 2
  333 + skymap['threshold'] = 5.0
  334 + skymap['inexclusion'] = 'NONE'
  335 + skymap['outmap'] = 'ctskymap_ring_py3.fits'
  336 + skymap['logfile'] = 'ctskymap_ring_py3.log'
  337 + skymap['chatter'] = 2
  338 +
  339 + # Execute tool
  340 + skymap.logFileOpen() # Needed to get a new log file
  341 + skymap.execute()
  342 +
  343 + # Check result file
  344 + self._check_result_file('ctskymap_ring_py3.fits', 10, 10)
  345 + self._check_result_file('ctskymap_ring_py3.fits[BACKGROUND]', 10, 10)
  346 + self._check_result_file('ctskymap_ring_py3.fits[SIGNIFICANCE]', 10, 10)
  347 +
  348 + # Test iterative RING background subtraction with an exclusion region
  349 + skymap = ctools.ctskymap()
  350 + skymap['inmap'] = self._skymap_ring
  351 + skymap['bkgsubtract'] = 'RING'
  352 + skymap['roiradius'] = 0.1
  353 + skymap['inradius'] = 0.6
  354 + skymap['outradius'] = 0.8
  355 + skymap['iterations'] = 2
  356 + skymap['threshold'] = 5.0
  357 + skymap['inexclusion'] = self._exclusion_region
  358 + skymap['outmap'] = 'ctskymap_ring_py4.fits'
  359 + skymap['logfile'] = 'ctskymap_ring_py4.log'
  360 + skymap['chatter'] = 2
  361 +
  362 + # Execute tool
  363 + skymap.logFileOpen() # Needed to get a new log file
  364 + skymap.execute()
  365 +
  366 + # Check result file
  367 + self._check_result_file('ctskymap_ring_py4.fits', 10, 10)
  368 + self._check_result_file('ctskymap_ring_py4.fits[BACKGROUND]', 10, 10)
  369 + self._check_result_file('ctskymap_ring_py4.fits[SIGNIFICANCE]', 10, 10)
  370 +
  371 + # Test iterative RING background subtraction with an exclusion map
  372 + skymap = ctools.ctskymap()
  373 + skymap['inmap'] = self._skymap_ring
  374 + skymap['bkgsubtract'] = 'RING'
  375 + skymap['roiradius'] = 0.1
  376 + skymap['inradius'] = 0.6
  377 + skymap['outradius'] = 0.8
  378 + skymap['iterations'] = 2
  379 + skymap['threshold'] = 5.0
  380 + skymap['inexclusion'] = self._exclusion_map
  381 + skymap['outmap'] = 'ctskymap_ring_py5.fits'
  382 + skymap['logfile'] = 'ctskymap_ring_py5.log'
  383 + skymap['chatter'] = 2
  384 +
  385 + # Execute tool
  386 + skymap.logFileOpen() # Needed to get a new log file
  387 + skymap.execute()
  388 +
  389 + # Check result file
  390 + self._check_result_file('ctskymap_ring_py5.fits', 10, 10)
  391 + self._check_result_file('ctskymap_ring_py5.fits[BACKGROUND]', 10, 10)
  392 + self._check_result_file('ctskymap_ring_py5.fits[SIGNIFICANCE]', 10, 10)
  393 +
  394 + # Test RING background subtraction with direct significance computation
  395 + skymap = ctools.ctskymap()
  396 + skymap['inmap'] = self._skymap_ring
  397 + skymap['bkgsubtract'] = 'RING'
  398 + skymap['roiradius'] = 0.1
  399 + skymap['inradius'] = 0.6
  400 + skymap['outradius'] = 0.8
  401 + skymap['iterations'] = 0
  402 + skymap['threshold'] = 5.0
  403 + skymap['inexclusion'] = 'NONE'
  404 + skymap['usefft'] = False # Direct computation, no FFT
  405 + skymap['outmap'] = 'ctskymap_ring_py6.fits'
  406 + skymap['logfile'] = 'ctskymap_ring_py6.log'
  407 + skymap['chatter'] = 2
258 408
259 # Execute tool 409 # Execute tool
260 skymap.logFileOpen() # Needed to get a new log file 410 skymap.logFileOpen() # Needed to get a new log file
261 skymap.execute() 411 skymap.execute()
262 412
263 # Check result file 413 # Check result file
264 - self._check_result_file('ctskymap_py5.fits', 10, 10)  
265 - self._check_result_file('ctskymap_py5.fits[BACKGROUND]', 10, 10)  
266 - self._check_result_file('ctskymap_py5.fits[SIGNIFICANCE]', 10, 10) 414 + self._check_result_file('ctskymap_ring_py6.fits', 10, 10)
  415 + self._check_result_file('ctskymap_ring_py6.fits[BACKGROUND]', 10, 10)
  416 + self._check_result_file('ctskymap_ring_py6.fits[SIGNIFICANCE]', 10, 10)
267 417
268 # Return 418 # Return
269 return 419 return