Commit 1e11230eeeb386ceed8b4e30984cf0992e2356fb

Authored by Jürgen Knödlseder
1 parent a3e660e3

Improve cstsmapmerge.py unit tests (#1828)

cscripts/cstsmapmerge.py
@@ -30,17 +30,17 @@ import ctools @@ -30,17 +30,17 @@ import ctools
30 # ================== # 30 # ================== #
31 class cstsmapmerge(ctools.cscript): 31 class cstsmapmerge(ctools.cscript):
32 """ 32 """
33 - Merge Test Statistic maps. 33 + Merge Test Statistic maps
34 """ 34 """
35 35
36 # Constructor 36 # Constructor
37 def __init__(self, *argv): 37 def __init__(self, *argv):
38 """ 38 """
39 - Constructor. 39 + Constructor
40 """ 40 """
41 # Set name 41 # Set name
42 self._name = 'cstsmapmerge' 42 self._name = 'cstsmapmerge'
43 - self._version = '1.1.0' 43 + self._version = '1.2.0'
44 44
45 # Initialise class members 45 # Initialise class members
46 self._files = None 46 self._files = None
@@ -64,7 +64,7 @@ class cstsmapmerge(ctools.cscript): @@ -64,7 +64,7 @@ class cstsmapmerge(ctools.cscript):
64 # Private methods 64 # Private methods
65 def _get_parameters(self): 65 def _get_parameters(self):
66 """ 66 """
67 - Get parameters from parfile. 67 + Get parameters from parfile
68 """ 68 """
69 # Get input maps string 69 # Get input maps string
70 inmaps = self['inmaps'].string() 70 inmaps = self['inmaps'].string()
@@ -128,7 +128,12 @@ class cstsmapmerge(ctools.cscript): @@ -128,7 +128,12 @@ class cstsmapmerge(ctools.cscript):
128 128
129 def _init_ts_map(self, fitsfile): 129 def _init_ts_map(self, fitsfile):
130 """ 130 """
131 - Initialise Test Statistic map. 131 + Initialise Test Statistic map
  132 +
  133 + Parameters
  134 + ----------
  135 + fitsfile : str
  136 + FITS file to be merged
132 """ 137 """
133 # Set filename 138 # Set filename
134 self._in_filename = fitsfile 139 self._in_filename = fitsfile
@@ -140,7 +145,7 @@ class cstsmapmerge(ctools.cscript): @@ -140,7 +145,7 @@ class cstsmapmerge(ctools.cscript):
140 self._tsmap = gammalib.GSkyMap() 145 self._tsmap = gammalib.GSkyMap()
141 self._tsmap.read(fits[0]) 146 self._tsmap.read(fits[0])
142 self._statusmap = gammalib.GSkyMap() 147 self._statusmap = gammalib.GSkyMap()
143 - self._statusmap.read(fits["STATUS MAP"]) 148 + self._statusmap.read(fits['STATUS MAP'])
144 149
145 # Get other maps 150 # Get other maps
146 self._maps = [] 151 self._maps = []
@@ -150,7 +155,7 @@ class cstsmapmerge(ctools.cscript): @@ -150,7 +155,7 @@ class cstsmapmerge(ctools.cscript):
150 for hdu in fits: 155 for hdu in fits:
151 156
152 # Leave out primary and status extension 157 # Leave out primary and status extension
153 - if hdu.extname() != "IMAGE" and hdu.extname() != "STATUS MAP": 158 + if hdu.extname() != 'IMAGE' and hdu.extname() != 'STATUS MAP':
154 159
155 # Add present maps 160 # Add present maps
156 skymap = gammalib.GSkyMap() 161 skymap = gammalib.GSkyMap()
@@ -166,10 +171,12 @@ class cstsmapmerge(ctools.cscript): @@ -166,10 +171,12 @@ class cstsmapmerge(ctools.cscript):
166 171
167 def _merge_ts_map(self, fitsfile): 172 def _merge_ts_map(self, fitsfile):
168 """ 173 """
169 - Merge TS map from FITS file into output TS map. 174 + Merge TS map from FITS file into output TS map
170 175
171 - Args:  
172 - fitsfile: FITS file to be merged. 176 + Parameters
  177 + ----------
  178 + fitsfile : str
  179 + FITS file to be merged
173 """ 180 """
174 # Open FITS file 181 # Open FITS file
175 fits = gammalib.GFits(fitsfile) 182 fits = gammalib.GFits(fitsfile)
@@ -178,7 +185,7 @@ class cstsmapmerge(ctools.cscript): @@ -178,7 +185,7 @@ class cstsmapmerge(ctools.cscript):
178 add_tsmap = gammalib.GSkyMap() 185 add_tsmap = gammalib.GSkyMap()
179 add_tsmap.read(fits[0]) 186 add_tsmap.read(fits[0])
180 add_statusmap = gammalib.GSkyMap() 187 add_statusmap = gammalib.GSkyMap()
181 - add_statusmap.read(fits["STATUS MAP"]) 188 + add_statusmap.read(fits['STATUS MAP'])
182 189
183 # Get other maps 190 # Get other maps
184 add_maps = [] 191 add_maps = []
@@ -187,7 +194,7 @@ class cstsmapmerge(ctools.cscript): @@ -187,7 +194,7 @@ class cstsmapmerge(ctools.cscript):
187 for hdu in fits: 194 for hdu in fits:
188 195
189 # Leave out primary and status extension 196 # Leave out primary and status extension
190 - if hdu.extname() != "IMAGE" and hdu.extname() != "STATUS MAP": 197 + if hdu.extname() != 'IMAGE' and hdu.extname() != 'STATUS MAP':
191 198
192 # Add present maps 199 # Add present maps
193 skymap = gammalib.GSkyMap() 200 skymap = gammalib.GSkyMap()
@@ -199,9 +206,9 @@ class cstsmapmerge(ctools.cscript): @@ -199,9 +206,9 @@ class cstsmapmerge(ctools.cscript):
199 206
200 # Compare size of maps 207 # Compare size of maps
201 if not len(add_maps) == len(self._maps): 208 if not len(add_maps) == len(self._maps):
202 - msg = "Cannot merge map \""+fitsfile+"\" into map \""+\  
203 - self._in_filename+"\" since the number of parameters "\  
204 - "between both maps is different." 209 + msg = ('Cannot merge map "%s" into map "%s" since the number of '
  210 + 'parameters between both maps is different.' %
  211 + (fitsfile, self._in_filename))
205 raise RuntimeError(msg) 212 raise RuntimeError(msg)
206 213
207 # Loop over bins 214 # Loop over bins
@@ -212,10 +219,10 @@ class cstsmapmerge(ctools.cscript): @@ -212,10 +219,10 @@ class cstsmapmerge(ctools.cscript):
212 219
213 # Raise exception if this bin has already been computed 220 # Raise exception if this bin has already been computed
214 if self._statusmap[i] > 0.5 and not self._overwrite: 221 if self._statusmap[i] > 0.5 and not self._overwrite:
215 - msg = "Attempt to merge bin which apparently has "+\  
216 - "already been merged. File \""+fitsfile+"\" "+\  
217 - "contains already merged bins. Set hidden "+\  
218 - "parameter \"overwrite=yes\" to avoid this error." 222 + msg = ('Attempt to merge bin which apparently has already '
  223 + 'been merged. File "%s" contains already merged '
  224 + 'bins. Set hidden parameter "overwrite=yes" to '
  225 + 'avoid this error.' % fitsfile)
219 raise RuntimeError(msg) 226 raise RuntimeError(msg)
220 227
221 # Copy TS values 228 # Copy TS values
@@ -233,16 +240,20 @@ class cstsmapmerge(ctools.cscript): @@ -233,16 +240,20 @@ class cstsmapmerge(ctools.cscript):
233 240
234 def _get_number_of_ts_pixels(self, fitsfile): 241 def _get_number_of_ts_pixels(self, fitsfile):
235 """ 242 """
236 - Return number of pixels with TS values. 243 + Return number of pixels with TS values
237 244
238 - Args:  
239 - fitsfile: FITS file to be merged. 245 + Parameters
  246 + ----------
  247 + fitsfile : str
  248 + FITS file to be merged
240 249
241 - Returns:  
242 - Number of pixels for which TS has been computed. 250 + Returns
  251 + -------
  252 + counts : int
  253 + Number of pixels for which TS has been computed
243 """ 254 """
244 # Get status map for this file 255 # Get status map for this file
245 - status = gammalib.GSkyMap(fitsfile+"[STATUS MAP]") 256 + status = gammalib.GSkyMap(fitsfile+'[STATUS MAP]')
246 257
247 # Count number of pixels with TS status set 258 # Count number of pixels with TS status set
248 count = 0 259 count = 0
@@ -257,7 +268,7 @@ class cstsmapmerge(ctools.cscript): @@ -257,7 +268,7 @@ class cstsmapmerge(ctools.cscript):
257 # Public methods 268 # Public methods
258 def run(self): 269 def run(self):
259 """ 270 """
260 - Run the script. 271 + Run the script
261 """ 272 """
262 # Switch screen logging on in debug mode 273 # Switch screen logging on in debug mode
263 if self._logDebug(): 274 if self._logDebug():
@@ -268,11 +279,11 @@ class cstsmapmerge(ctools.cscript): @@ -268,11 +279,11 @@ class cstsmapmerge(ctools.cscript):
268 279
269 # Write header into logger 280 # Write header into logger
270 if self._logTerse(): 281 if self._logTerse():
271 - self._log("\n")  
272 - self._log.header1("Merge TS maps") 282 + self._log('\n')
  283 + self._log.header1('Merge TS maps')
273 284
274 # Initialise file to start with 285 # Initialise file to start with
275 - file0 = "" 286 + file0 = ''
276 self._merged_files = [] 287 self._merged_files = []
277 288
278 # Test files for the entry status map. Use the first one to appear 289 # Test files for the entry status map. Use the first one to appear
@@ -282,41 +293,40 @@ class cstsmapmerge(ctools.cscript): @@ -282,41 +293,40 @@ class cstsmapmerge(ctools.cscript):
282 # Skip file if it's not a FITS file 293 # Skip file if it's not a FITS file
283 if not gammalib.GFilename(fitsfile).is_fits(): 294 if not gammalib.GFilename(fitsfile).is_fits():
284 if self._logExplicit(): 295 if self._logExplicit():
285 - self._log(gammalib.parformat("Skip file")) 296 + self._log(gammalib.parformat('Skip file'))
286 self._log(fitsfile) 297 self._log(fitsfile)
287 - self._log(" (not a FITS file)\n") 298 + self._log(' (not a FITS file)\n')
288 continue 299 continue
289 300
290 # Open FITS file 301 # Open FITS file
291 fits = gammalib.GFits(fitsfile) 302 fits = gammalib.GFits(fitsfile)
292 303
293 # If file contains a status map then use it 304 # If file contains a status map then use it
294 - if fits.contains("STATUS MAP"): 305 + if fits.contains('STATUS MAP'):
295 fits.close() 306 fits.close()
296 file0 = fitsfile 307 file0 = fitsfile
297 if self._logTerse(): 308 if self._logTerse():
298 - self._log(gammalib.parformat("Initial TS map file")) 309 + self._log(gammalib.parformat('Initial TS map file'))
299 self._log(fitsfile) 310 self._log(fitsfile)
300 count = self._get_number_of_ts_pixels(fitsfile) 311 count = self._get_number_of_ts_pixels(fitsfile)
301 - self._log(" (%d TS pixels computed)" % count)  
302 - self._log("\n") 312 + self._log(' (%d TS pixels computed)' % count)
  313 + self._log('\n')
303 break 314 break
304 315
305 # ... otherwise signal that file is useless 316 # ... otherwise signal that file is useless
306 else: 317 else:
307 fits.close() 318 fits.close()
308 if self._logExplicit(): 319 if self._logExplicit():
309 - self._log(gammalib.parformat("Skip file")) 320 + self._log(gammalib.parformat('Skip file'))
310 self._log(fitsfile) 321 self._log(fitsfile)
311 - self._log(" (no \"STATUS MAP\" extension)\n") 322 + self._log(' (no "STATUS MAP" extension)\n')
312 continue 323 continue
313 324
314 # Signal if no suitable file was found 325 # Signal if no suitable file was found
315 - if file0 == "": 326 + if file0 == '':
316 if self._logTerse(): 327 if self._logTerse():
317 - self._log("None of the provided files seems to be a sliced "  
318 - "TS map file (none has a \"STATUS MAP\" "  
319 - "extension).\n") 328 + self._log('None of the provided files seems to be a sliced '
  329 + 'TS map file (none has a "STATUS MAP" extension).\n')
320 330
321 # ... otherwise merge files 331 # ... otherwise merge files
322 else: 332 else:
@@ -339,21 +349,21 @@ class cstsmapmerge(ctools.cscript): @@ -339,21 +349,21 @@ class cstsmapmerge(ctools.cscript):
339 # Skip if file is not FITS 349 # Skip if file is not FITS
340 if not gammalib.GFilename(fitsfile).is_fits(): 350 if not gammalib.GFilename(fitsfile).is_fits():
341 if self._logExplicit(): 351 if self._logExplicit():
342 - self._log(gammalib.parformat("Skip file")) 352 + self._log(gammalib.parformat('Skip file'))
343 self._log(fitsfile) 353 self._log(fitsfile)
344 - self._log(" (not a FITS file)\n") 354 + self._log(' (not a FITS file)\n')
345 continue 355 continue
346 356
347 # Open FITS file 357 # Open FITS file
348 fits = gammalib.GFits(fitsfile) 358 fits = gammalib.GFits(fitsfile)
349 359
350 # Skip if file does not contain status map 360 # Skip if file does not contain status map
351 - if not fits.contains("STATUS MAP"): 361 + if not fits.contains('STATUS MAP'):
352 fits.close() 362 fits.close()
353 if self._logExplicit(): 363 if self._logExplicit():
354 - self._log(gammalib.parformat("Skip file")) 364 + self._log(gammalib.parformat('Skip file'))
355 self._log(fitsfile) 365 self._log(fitsfile)
356 - self._log(" (no \"STATUS MAP\" extension)\n") 366 + self._log(' (no "STATUS MAP" extension)\n')
357 continue 367 continue
358 368
359 # Close FITS file 369 # Close FITS file
@@ -361,11 +371,11 @@ class cstsmapmerge(ctools.cscript): @@ -361,11 +371,11 @@ class cstsmapmerge(ctools.cscript):
361 371
362 # Logging 372 # Logging
363 if self._logTerse(): 373 if self._logTerse():
364 - self._log(gammalib.parformat("Merge TS map file")) 374 + self._log(gammalib.parformat('Merge TS map file'))
365 self._log(fitsfile) 375 self._log(fitsfile)
366 count = self._get_number_of_ts_pixels(fitsfile) 376 count = self._get_number_of_ts_pixels(fitsfile)
367 - self._log(" (%d TS pixels computed)" % count)  
368 - self._log("\n") 377 + self._log(' (%d TS pixels computed)' % count)
  378 + self._log('\n')
369 379
370 # Merge TS map 380 # Merge TS map
371 self._merge_ts_map(fitsfile) 381 self._merge_ts_map(fitsfile)
@@ -378,21 +388,21 @@ class cstsmapmerge(ctools.cscript): @@ -378,21 +388,21 @@ class cstsmapmerge(ctools.cscript):
378 388
379 def save(self): 389 def save(self):
380 """ 390 """
381 - Save TS map and remove slices if requested. 391 + Save TS map and remove slices if requested
382 """ 392 """
383 # Write header 393 # Write header
384 if self._logTerse(): 394 if self._logTerse():
385 - self._log("\n")  
386 - self._log.header1("Save TS map") 395 + self._log('\n')
  396 + self._log.header1('Save TS map')
387 397
388 # Get output filename in case it was not read ahead 398 # Get output filename in case it was not read ahead
389 - outmap = self["outmap"].filename() 399 + outmap = self['outmap'].filename()
390 400
391 # Log filename 401 # Log filename
392 if self._logTerse(): 402 if self._logTerse():
393 - self._log(gammalib.parformat("TS map file")) 403 + self._log(gammalib.parformat('TS map file'))
394 self._log(outmap.url()) 404 self._log(outmap.url())
395 - self._log("\n") 405 + self._log('\n')
396 406
397 # Create FITS file 407 # Create FITS file
398 fits = gammalib.GFits() 408 fits = gammalib.GFits()
@@ -418,7 +428,7 @@ class cstsmapmerge(ctools.cscript): @@ -418,7 +428,7 @@ class cstsmapmerge(ctools.cscript):
418 # Write status map if we are not done yet 428 # Write status map if we are not done yet
419 if not done: 429 if not done:
420 self._statusmap.write(fits) 430 self._statusmap.write(fits)
421 - fits[fits.size()-1].extname("STATUS MAP") 431 + fits[fits.size()-1].extname('STATUS MAP')
422 432
423 # Save FITS file 433 # Save FITS file
424 fits.saveto(outmap, self._clobber()) 434 fits.saveto(outmap, self._clobber())
@@ -428,16 +438,16 @@ class cstsmapmerge(ctools.cscript): @@ -428,16 +438,16 @@ class cstsmapmerge(ctools.cscript):
428 for filename in self._merged_files: 438 for filename in self._merged_files:
429 os.remove(filename) 439 os.remove(filename)
430 if self._logTerse(): 440 if self._logTerse():
431 - self._log(gammalib.parformat("Deleted input file")) 441 + self._log(gammalib.parformat('Deleted input file'))
432 self._log(filename) 442 self._log(filename)
433 - self._log("\n") 443 + self._log('\n')
434 444
435 # Return 445 # Return
436 return 446 return
437 447
438 def execute(self): 448 def execute(self):
439 """ 449 """
440 - Execute the script. 450 + Execute the script
441 """ 451 """
442 # Open logfile 452 # Open logfile
443 self.logFileOpen() 453 self.logFileOpen()
test/test_cstsmapmerge.py
1 #! /usr/bin/env python 1 #! /usr/bin/env python
2 # ========================================================================== 2 # ==========================================================================
3 -# This scripts performs unit tests for the cstsmapmerge script. 3 +# This scripts performs unit tests for the cstsmapmerge script
4 # 4 #
5 # Copyright (C) 2016 Juergen Knoedlseder 5 # Copyright (C) 2016 Juergen Knoedlseder
6 # 6 #
@@ -119,7 +119,7 @@ class Test(test): @@ -119,7 +119,7 @@ class Test(test):
119 merge.run() 119 merge.run()
120 merge.save() 120 merge.save()
121 121
122 - # Check pull distribution file 122 + # Check merged TS map
123 self._check_result_file('cstsmapmerge_py1.fits') 123 self._check_result_file('cstsmapmerge_py1.fits')
124 124
125 # Set-up space-separated cstsmapmerge 125 # Set-up space-separated cstsmapmerge
@@ -132,7 +132,7 @@ class Test(test): @@ -132,7 +132,7 @@ class Test(test):
132 # Run cstsmapmerge script 132 # Run cstsmapmerge script
133 merge.execute() 133 merge.execute()
134 134
135 - # Check pull distribution file 135 + # Check merged TS map
136 self._check_result_file('cstsmapmerge_py2.fits') 136 self._check_result_file('cstsmapmerge_py2.fits')
137 137
138 # Set-up wildcard string cstsmapmerge 138 # Set-up wildcard string cstsmapmerge
@@ -145,7 +145,7 @@ class Test(test): @@ -145,7 +145,7 @@ class Test(test):
145 # Run cstsmapmerge script 145 # Run cstsmapmerge script
146 merge.execute() 146 merge.execute()
147 147
148 - # Check pull distribution file 148 + # Check merged TS map
149 self._check_result_file('cstsmapmerge_py3.fits') 149 self._check_result_file('cstsmapmerge_py3.fits')
150 150
151 # Set-up ASCII file cstsmapmerge 151 # Set-up ASCII file cstsmapmerge
@@ -158,9 +158,22 @@ class Test(test): @@ -158,9 +158,22 @@ class Test(test):
158 # Run cstsmapmerge script 158 # Run cstsmapmerge script
159 merge.execute() 159 merge.execute()
160 160
161 - # Check pull distribution file 161 + # Check merged TS map
162 self._check_result_file('cstsmapmerge_py4.fits') 162 self._check_result_file('cstsmapmerge_py4.fits')
163 163
  164 + # Set-up space-separated cstsmapmerge with invalid files
  165 + merge = cscripts.cstsmapmerge()
  166 + merge['inmaps'] = self._events+' '+self._model
  167 + merge['outmap'] = 'cstsmapmerge_py5.fits'
  168 + merge['logfile'] = 'cstsmapmerge_py5.log'
  169 + merge['chatter'] = 4
  170 +
  171 + # Run cstsmapmerge script
  172 + merge.execute()
  173 +
  174 + # Check merged TS map
  175 + #self._check_result_file('cstsmapmerge_py5.fits')
  176 +
164 # Return 177 # Return
165 return 178 return
166 179