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 30 # ================== #
31 31 class cstsmapmerge(ctools.cscript):
32 32 """
33   - Merge Test Statistic maps.
  33 + Merge Test Statistic maps
34 34 """
35 35  
36 36 # Constructor
37 37 def __init__(self, *argv):
38 38 """
39   - Constructor.
  39 + Constructor
40 40 """
41 41 # Set name
42 42 self._name = 'cstsmapmerge'
43   - self._version = '1.1.0'
  43 + self._version = '1.2.0'
44 44  
45 45 # Initialise class members
46 46 self._files = None
... ... @@ -64,7 +64,7 @@ class cstsmapmerge(ctools.cscript):
64 64 # Private methods
65 65 def _get_parameters(self):
66 66 """
67   - Get parameters from parfile.
  67 + Get parameters from parfile
68 68 """
69 69 # Get input maps string
70 70 inmaps = self['inmaps'].string()
... ... @@ -128,7 +128,12 @@ class cstsmapmerge(ctools.cscript):
128 128  
129 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 138 # Set filename
134 139 self._in_filename = fitsfile
... ... @@ -140,7 +145,7 @@ class cstsmapmerge(ctools.cscript):
140 145 self._tsmap = gammalib.GSkyMap()
141 146 self._tsmap.read(fits[0])
142 147 self._statusmap = gammalib.GSkyMap()
143   - self._statusmap.read(fits["STATUS MAP"])
  148 + self._statusmap.read(fits['STATUS MAP'])
144 149  
145 150 # Get other maps
146 151 self._maps = []
... ... @@ -150,7 +155,7 @@ class cstsmapmerge(ctools.cscript):
150 155 for hdu in fits:
151 156  
152 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 160 # Add present maps
156 161 skymap = gammalib.GSkyMap()
... ... @@ -166,10 +171,12 @@ class cstsmapmerge(ctools.cscript):
166 171  
167 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 181 # Open FITS file
175 182 fits = gammalib.GFits(fitsfile)
... ... @@ -178,7 +185,7 @@ class cstsmapmerge(ctools.cscript):
178 185 add_tsmap = gammalib.GSkyMap()
179 186 add_tsmap.read(fits[0])
180 187 add_statusmap = gammalib.GSkyMap()
181   - add_statusmap.read(fits["STATUS MAP"])
  188 + add_statusmap.read(fits['STATUS MAP'])
182 189  
183 190 # Get other maps
184 191 add_maps = []
... ... @@ -187,7 +194,7 @@ class cstsmapmerge(ctools.cscript):
187 194 for hdu in fits:
188 195  
189 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 199 # Add present maps
193 200 skymap = gammalib.GSkyMap()
... ... @@ -199,9 +206,9 @@ class cstsmapmerge(ctools.cscript):
199 206  
200 207 # Compare size of maps
201 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 212 raise RuntimeError(msg)
206 213  
207 214 # Loop over bins
... ... @@ -212,10 +219,10 @@ class cstsmapmerge(ctools.cscript):
212 219  
213 220 # Raise exception if this bin has already been computed
214 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 226 raise RuntimeError(msg)
220 227  
221 228 # Copy TS values
... ... @@ -233,16 +240,20 @@ class cstsmapmerge(ctools.cscript):
233 240  
234 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 255 # Get status map for this file
245   - status = gammalib.GSkyMap(fitsfile+"[STATUS MAP]")
  256 + status = gammalib.GSkyMap(fitsfile+'[STATUS MAP]')
246 257  
247 258 # Count number of pixels with TS status set
248 259 count = 0
... ... @@ -257,7 +268,7 @@ class cstsmapmerge(ctools.cscript):
257 268 # Public methods
258 269 def run(self):
259 270 """
260   - Run the script.
  271 + Run the script
261 272 """
262 273 # Switch screen logging on in debug mode
263 274 if self._logDebug():
... ... @@ -268,11 +279,11 @@ class cstsmapmerge(ctools.cscript):
268 279  
269 280 # Write header into logger
270 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 285 # Initialise file to start with
275   - file0 = ""
  286 + file0 = ''
276 287 self._merged_files = []
277 288  
278 289 # Test files for the entry status map. Use the first one to appear
... ... @@ -282,41 +293,40 @@ class cstsmapmerge(ctools.cscript):
282 293 # Skip file if it's not a FITS file
283 294 if not gammalib.GFilename(fitsfile).is_fits():
284 295 if self._logExplicit():
285   - self._log(gammalib.parformat("Skip file"))
  296 + self._log(gammalib.parformat('Skip file'))
286 297 self._log(fitsfile)
287   - self._log(" (not a FITS file)\n")
  298 + self._log(' (not a FITS file)\n')
288 299 continue
289 300  
290 301 # Open FITS file
291 302 fits = gammalib.GFits(fitsfile)
292 303  
293 304 # If file contains a status map then use it
294   - if fits.contains("STATUS MAP"):
  305 + if fits.contains('STATUS MAP'):
295 306 fits.close()
296 307 file0 = fitsfile
297 308 if self._logTerse():
298   - self._log(gammalib.parformat("Initial TS map file"))
  309 + self._log(gammalib.parformat('Initial TS map file'))
299 310 self._log(fitsfile)
300 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 314 break
304 315  
305 316 # ... otherwise signal that file is useless
306 317 else:
307 318 fits.close()
308 319 if self._logExplicit():
309   - self._log(gammalib.parformat("Skip file"))
  320 + self._log(gammalib.parformat('Skip file'))
310 321 self._log(fitsfile)
311   - self._log(" (no \"STATUS MAP\" extension)\n")
  322 + self._log(' (no "STATUS MAP" extension)\n')
312 323 continue
313 324  
314 325 # Signal if no suitable file was found
315   - if file0 == "":
  326 + if file0 == '':
316 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 331 # ... otherwise merge files
322 332 else:
... ... @@ -339,21 +349,21 @@ class cstsmapmerge(ctools.cscript):
339 349 # Skip if file is not FITS
340 350 if not gammalib.GFilename(fitsfile).is_fits():
341 351 if self._logExplicit():
342   - self._log(gammalib.parformat("Skip file"))
  352 + self._log(gammalib.parformat('Skip file'))
343 353 self._log(fitsfile)
344   - self._log(" (not a FITS file)\n")
  354 + self._log(' (not a FITS file)\n')
345 355 continue
346 356  
347 357 # Open FITS file
348 358 fits = gammalib.GFits(fitsfile)
349 359  
350 360 # Skip if file does not contain status map
351   - if not fits.contains("STATUS MAP"):
  361 + if not fits.contains('STATUS MAP'):
352 362 fits.close()
353 363 if self._logExplicit():
354   - self._log(gammalib.parformat("Skip file"))
  364 + self._log(gammalib.parformat('Skip file'))
355 365 self._log(fitsfile)
356   - self._log(" (no \"STATUS MAP\" extension)\n")
  366 + self._log(' (no "STATUS MAP" extension)\n')
357 367 continue
358 368  
359 369 # Close FITS file
... ... @@ -361,11 +371,11 @@ class cstsmapmerge(ctools.cscript):
361 371  
362 372 # Logging
363 373 if self._logTerse():
364   - self._log(gammalib.parformat("Merge TS map file"))
  374 + self._log(gammalib.parformat('Merge TS map file'))
365 375 self._log(fitsfile)
366 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 380 # Merge TS map
371 381 self._merge_ts_map(fitsfile)
... ... @@ -378,21 +388,21 @@ class cstsmapmerge(ctools.cscript):
378 388  
379 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 393 # Write header
384 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 398 # Get output filename in case it was not read ahead
389   - outmap = self["outmap"].filename()
  399 + outmap = self['outmap'].filename()
390 400  
391 401 # Log filename
392 402 if self._logTerse():
393   - self._log(gammalib.parformat("TS map file"))
  403 + self._log(gammalib.parformat('TS map file'))
394 404 self._log(outmap.url())
395   - self._log("\n")
  405 + self._log('\n')
396 406  
397 407 # Create FITS file
398 408 fits = gammalib.GFits()
... ... @@ -418,7 +428,7 @@ class cstsmapmerge(ctools.cscript):
418 428 # Write status map if we are not done yet
419 429 if not done:
420 430 self._statusmap.write(fits)
421   - fits[fits.size()-1].extname("STATUS MAP")
  431 + fits[fits.size()-1].extname('STATUS MAP')
422 432  
423 433 # Save FITS file
424 434 fits.saveto(outmap, self._clobber())
... ... @@ -428,16 +438,16 @@ class cstsmapmerge(ctools.cscript):
428 438 for filename in self._merged_files:
429 439 os.remove(filename)
430 440 if self._logTerse():
431   - self._log(gammalib.parformat("Deleted input file"))
  441 + self._log(gammalib.parformat('Deleted input file'))
432 442 self._log(filename)
433   - self._log("\n")
  443 + self._log('\n')
434 444  
435 445 # Return
436 446 return
437 447  
438 448 def execute(self):
439 449 """
440   - Execute the script.
  450 + Execute the script
441 451 """
442 452 # Open logfile
443 453 self.logFileOpen()
... ...
test/test_cstsmapmerge.py
1 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 5 # Copyright (C) 2016 Juergen Knoedlseder
6 6 #
... ... @@ -119,7 +119,7 @@ class Test(test):
119 119 merge.run()
120 120 merge.save()
121 121  
122   - # Check pull distribution file
  122 + # Check merged TS map
123 123 self._check_result_file('cstsmapmerge_py1.fits')
124 124  
125 125 # Set-up space-separated cstsmapmerge
... ... @@ -132,7 +132,7 @@ class Test(test):
132 132 # Run cstsmapmerge script
133 133 merge.execute()
134 134  
135   - # Check pull distribution file
  135 + # Check merged TS map
136 136 self._check_result_file('cstsmapmerge_py2.fits')
137 137  
138 138 # Set-up wildcard string cstsmapmerge
... ... @@ -145,7 +145,7 @@ class Test(test):
145 145 # Run cstsmapmerge script
146 146 merge.execute()
147 147  
148   - # Check pull distribution file
  148 + # Check merged TS map
149 149 self._check_result_file('cstsmapmerge_py3.fits')
150 150  
151 151 # Set-up ASCII file cstsmapmerge
... ... @@ -158,9 +158,22 @@ class Test(test):
158 158 # Run cstsmapmerge script
159 159 merge.execute()
160 160  
161   - # Check pull distribution file
  161 + # Check merged TS map
162 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 177 # Return
165 178 return
166 179  
... ...