Commit 3ed80fe98268b3746bc1f3871f5e0258501d200c
1 parent
6473def5
Implement csphagen On/Off region Python unit test instead of command line test
Showing
2 changed files
with
33 additions
and
61 deletions
cscripts/Makefile.am
| 1 | 1 | ############################################################################# |
| 2 | 2 | # Makefile for cscripts scripts # |
| 3 | 3 | # ------------------------------------------------------------------------- # |
| 4 | -# Copyright (C) 2016-2017 Juergen Knoedlseder # | |
| 4 | +# Copyright (C) 2016-2018 Juergen Knoedlseder # | |
| 5 | 5 | # ------------------------------------------------------------------------- # |
| 6 | 6 | # # |
| 7 | 7 | # This program is free software: you can redistribute it and/or modify # |
| ... | ... | @@ -168,6 +168,8 @@ testdata = $(top_srcdir)/test/data/crab.xml \ |
| 168 | 168 | $(top_srcdir)/test/data/crab_offaxis1.fits.gz \ |
| 169 | 169 | $(top_srcdir)/test/data/crab_offaxis2.fits.gz \ |
| 170 | 170 | $(top_srcdir)/test/data/crab_exclusion.fits.gz \ |
| 171 | + $(top_srcdir)/test/data/crab_src_reg.reg \ | |
| 172 | + $(top_srcdir)/test/data/crab_bkg_reg.reg \ | |
| 171 | 173 | $(top_srcdir)/test/data/tsmap_0.fits \ |
| 172 | 174 | $(top_srcdir)/test/data/tsmap_1.fits \ |
| 173 | 175 | $(top_srcdir)/test/data/exclusion.reg \ | ... | ... |
test/test_csphagen.py
| ... | ... | @@ -48,13 +48,11 @@ class Test(test): |
| 48 | 48 | self._exclusion = self._datadir + '/crab_exclusion.fits' |
| 49 | 49 | self._nreg_with_excl = 5 |
| 50 | 50 | self._nreg_wo_excl = 8 |
| 51 | + self._nreg_bkg_reg = 5 | |
| 51 | 52 | self._nreg_mul = [self._nreg_with_excl, 6] |
| 52 | 53 | self._nbins = 10 |
| 53 | - # number of expected background regions with/wo exclusion, | |
| 54 | - # and for two different runs | |
| 55 | 54 | self._regfile_src = self._datadir + '/crab_src_reg.reg' |
| 56 | 55 | self._regfile_bkg = self._datadir + '/crab_bkg_reg.reg' |
| 57 | - self._counts_reflected = 0 | |
| 58 | 56 | |
| 59 | 57 | # Return |
| 60 | 58 | return |
| ... | ... | @@ -70,7 +68,6 @@ class Test(test): |
| 70 | 68 | # Append tests |
| 71 | 69 | self.append(self._test_cmd, 'Test csphagen on command line') |
| 72 | 70 | self.append(self._test_python, 'Test csphagen from Python') |
| 73 | - self.append(self._test_regfiles_cmd, 'Test csphagen input region files on command line') | |
| 74 | 71 | |
| 75 | 72 | # Return |
| 76 | 73 | return |
| ... | ... | @@ -108,9 +105,6 @@ class Test(test): |
| 108 | 105 | self._check_output('csphagen_cmd1', self._nbins, self._nreg_with_excl) |
| 109 | 106 | self._check_outobs('csphagen_cmd1', 1) |
| 110 | 107 | |
| 111 | - # store on-counts for later testing bkgmethod CUSTOM | |
| 112 | - self._counts_reflected = self._read_pha_counts('csphagen_cmd1_pha_on.fits') | |
| 113 | - | |
| 114 | 108 | # Setup csphagen command |
| 115 | 109 | cmd = csphagen + ' inobs="events_that_do_not_exist.fits" ' + \ |
| 116 | 110 | 'caldb="' + self._caldb + '" irf="' + self._irf + \ |
| ... | ... | @@ -162,7 +156,7 @@ class Test(test): |
| 162 | 156 | phagen['logfile'] = 'csphagen_py1.log' |
| 163 | 157 | phagen['chatter'] = 1 |
| 164 | 158 | |
| 165 | - # Run script | |
| 159 | + # Execute script | |
| 166 | 160 | phagen.execute() |
| 167 | 161 | |
| 168 | 162 | # Check output |
| ... | ... | @@ -192,7 +186,7 @@ class Test(test): |
| 192 | 186 | phagen['logfile'] = 'csphagen_py2.log' |
| 193 | 187 | phagen['chatter'] = 2 |
| 194 | 188 | |
| 195 | - # Run script | |
| 189 | + # Execute script | |
| 196 | 190 | phagen.execute() |
| 197 | 191 | |
| 198 | 192 | # Check output |
| ... | ... | @@ -260,7 +254,7 @@ class Test(test): |
| 260 | 254 | phagen['logfile'] = 'csphagen_py4.log' |
| 261 | 255 | phagen['chatter'] = 4 |
| 262 | 256 | |
| 263 | - # Run script | |
| 257 | + # Execute script | |
| 264 | 258 | phagen.execute() |
| 265 | 259 | |
| 266 | 260 | # Check output |
| ... | ... | @@ -269,61 +263,37 @@ class Test(test): |
| 269 | 263 | 0, check_regions=False) |
| 270 | 264 | self._check_outobs('csphagen_py4', 1) |
| 271 | 265 | |
| 272 | - # Return | |
| 273 | - return | |
| 274 | - | |
| 275 | - # Test csphagen input region files (command line) | |
| 276 | - def _test_regfiles_cmd(self): | |
| 277 | - """ | |
| 278 | - Test csphagen input region files (command line). | |
| 279 | - This is done by redoing the test performed in _test_cmd() | |
| 280 | - but this time bkgmethod is CUSTOM and the regions are read | |
| 281 | - from ds9 region files. Exactly the same output of csphagen | |
| 282 | - compared to _test_cmd() is expected. | |
| 283 | - Hint: The output region files of _test_cmd() could be used | |
| 284 | - directly, but to make the execution of this test case standalone | |
| 285 | - the output once created by _test_cmd() was copied and added | |
| 286 | - staticly to the repository. | |
| 287 | - """ | |
| 288 | - # Set script name | |
| 289 | - csphagen = self._script('csphagen') | |
| 290 | - | |
| 291 | - # Setup csphagen command | |
| 292 | - # TODO: remove rad from cmd line arguments. currently it is needed (ctool::set_obs_bounds needs rad!) | |
| 293 | - cmd = csphagen + ' inobs="' + self._myevents1 + \ | |
| 294 | - '" caldb="' + self._caldb + '" irf="' + self._irf + \ | |
| 295 | - '" ebinalg="LOG" emin=0.1 emax=100. enumbins=' + \ | |
| 296 | - str(self._nbins) + \ | |
| 297 | - ' etruemin=0.05 etruemax=150 etruebins=5 '+ \ | |
| 298 | - 'stack="no" ' + \ | |
| 299 | - 'inexclusion="' + self._exclusion + '" ' + \ | |
| 300 | - 'bkgmethod="CUSTOM" ' + \ | |
| 301 | - 'rad=0.2 ' + \ | |
| 302 | - 'srcregfile="' + self._regfile_src + '" ' + \ | |
| 303 | - 'bkgregfile="' + self._regfile_bkg + '" ' + \ | |
| 304 | - 'outobs="csphagen_regfiles_cmd1.xml" ' + \ | |
| 305 | - 'prefix="csphagen_regfiles_cmd1" ' + \ | |
| 306 | - 'logfile="csphagen_regfiles_cmd1.log" chatter=2 ' + \ | |
| 307 | - 'coordsys="CEL" ra=83.633 dec=22.0145' | |
| 308 | - | |
| 309 | - # Check if execution was successful | |
| 310 | - self.test_assert(self._execute(cmd) == 0, | |
| 311 | - 'Check successful execution from command line') | |
| 312 | - | |
| 313 | - # Check output files | |
| 314 | - self._check_output('csphagen_regfiles_cmd1', self._nbins, self._nreg_with_excl) | |
| 315 | - self._check_outobs('csphagen_regfiles_cmd1', 1) | |
| 316 | - | |
| 317 | - # Check resulting number of counts, which should be equal to those obtained in _test_cmd() | |
| 318 | - counts_custom = self._read_pha_counts('csphagen_regfiles_cmd1_pha_on.fits') | |
| 266 | + # Setup csphagen for test with custom On and Off regions provided | |
| 267 | + phagen = cscripts.csphagen() | |
| 268 | + phagen['inobs'] = self._myevents1 | |
| 269 | + phagen['caldb'] = self._caldb | |
| 270 | + phagen['irf'] = self._irf | |
| 271 | + phagen['ebinalg'] = 'LOG' | |
| 272 | + phagen['emin'] = 0.1 | |
| 273 | + phagen['emax'] = 100.0 | |
| 274 | + phagen['enumbins'] = self._nbins | |
| 275 | + phagen['bkgmethod'] = 'CUSTOM' | |
| 276 | + phagen['srcregfile'] = self._regfile_src | |
| 277 | + phagen['bkgregfile'] = self._regfile_bkg | |
| 278 | + phagen['etruemin'] = 0.05 | |
| 279 | + phagen['etruemax'] = 150.0 | |
| 280 | + phagen['etruebins'] = 5 | |
| 281 | + phagen['stack'] = False | |
| 282 | + phagen['outobs'] = 'csphagen_py5.xml' | |
| 283 | + phagen['prefix'] = 'csphagen_py5' | |
| 284 | + phagen['logfile'] = 'csphagen_py5.log' | |
| 285 | + phagen['chatter'] = 2 | |
| 286 | + | |
| 287 | + # Execute script | |
| 288 | + phagen.execute() | |
| 319 | 289 | |
| 320 | - self.test_assert(counts_custom == self._counts_reflected, | |
| 321 | - 'Check bkgmethods CUSTOM and REFLECTED: number of on-counts should be equal') | |
| 290 | + # Check output | |
| 291 | + self._check_output('csphagen_py5', self._nbins, self._nreg_bkg_reg) | |
| 292 | + self._check_outobs('csphagen_py5', 1) | |
| 322 | 293 | |
| 323 | 294 | # Return |
| 324 | 295 | return |
| 325 | 296 | |
| 326 | - | |
| 327 | 297 | def _check_ebounds(self, table, bins): |
| 328 | 298 | """ |
| 329 | 299 | Check EBOUNDS table | ... | ... |