From 3ed80fe98268b3746bc1f3871f5e0258501d200c Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Fri, 12 Jan 2018 12:28:37 +0100 Subject: [PATCH] Implement csphagen On/Off region Python unit test instead of command line test --- cscripts/Makefile.am | 4 +++- test/test_csphagen.py | 90 ++++++++++++++++++++++++++++++------------------------------------------------------------ 2 files changed, 33 insertions(+), 61 deletions(-) diff --git a/cscripts/Makefile.am b/cscripts/Makefile.am index f292d84..6941b18 100644 --- a/cscripts/Makefile.am +++ b/cscripts/Makefile.am @@ -1,7 +1,7 @@ ############################################################################# # Makefile for cscripts scripts # # ------------------------------------------------------------------------- # -# Copyright (C) 2016-2017 Juergen Knoedlseder # +# Copyright (C) 2016-2018 Juergen Knoedlseder # # ------------------------------------------------------------------------- # # # # This program is free software: you can redistribute it and/or modify # @@ -168,6 +168,8 @@ testdata = $(top_srcdir)/test/data/crab.xml \ $(top_srcdir)/test/data/crab_offaxis1.fits.gz \ $(top_srcdir)/test/data/crab_offaxis2.fits.gz \ $(top_srcdir)/test/data/crab_exclusion.fits.gz \ + $(top_srcdir)/test/data/crab_src_reg.reg \ + $(top_srcdir)/test/data/crab_bkg_reg.reg \ $(top_srcdir)/test/data/tsmap_0.fits \ $(top_srcdir)/test/data/tsmap_1.fits \ $(top_srcdir)/test/data/exclusion.reg \ diff --git a/test/test_csphagen.py b/test/test_csphagen.py index 265de68..8c10e60 100644 --- a/test/test_csphagen.py +++ b/test/test_csphagen.py @@ -48,13 +48,11 @@ class Test(test): self._exclusion = self._datadir + '/crab_exclusion.fits' self._nreg_with_excl = 5 self._nreg_wo_excl = 8 + self._nreg_bkg_reg = 5 self._nreg_mul = [self._nreg_with_excl, 6] self._nbins = 10 - # number of expected background regions with/wo exclusion, - # and for two different runs self._regfile_src = self._datadir + '/crab_src_reg.reg' self._regfile_bkg = self._datadir + '/crab_bkg_reg.reg' - self._counts_reflected = 0 # Return return @@ -70,7 +68,6 @@ class Test(test): # Append tests self.append(self._test_cmd, 'Test csphagen on command line') self.append(self._test_python, 'Test csphagen from Python') - self.append(self._test_regfiles_cmd, 'Test csphagen input region files on command line') # Return return @@ -108,9 +105,6 @@ class Test(test): self._check_output('csphagen_cmd1', self._nbins, self._nreg_with_excl) self._check_outobs('csphagen_cmd1', 1) - # store on-counts for later testing bkgmethod CUSTOM - self._counts_reflected = self._read_pha_counts('csphagen_cmd1_pha_on.fits') - # Setup csphagen command cmd = csphagen + ' inobs="events_that_do_not_exist.fits" ' + \ 'caldb="' + self._caldb + '" irf="' + self._irf + \ @@ -162,7 +156,7 @@ class Test(test): phagen['logfile'] = 'csphagen_py1.log' phagen['chatter'] = 1 - # Run script + # Execute script phagen.execute() # Check output @@ -192,7 +186,7 @@ class Test(test): phagen['logfile'] = 'csphagen_py2.log' phagen['chatter'] = 2 - # Run script + # Execute script phagen.execute() # Check output @@ -260,7 +254,7 @@ class Test(test): phagen['logfile'] = 'csphagen_py4.log' phagen['chatter'] = 4 - # Run script + # Execute script phagen.execute() # Check output @@ -269,61 +263,37 @@ class Test(test): 0, check_regions=False) self._check_outobs('csphagen_py4', 1) - # Return - return - - # Test csphagen input region files (command line) - def _test_regfiles_cmd(self): - """ - Test csphagen input region files (command line). - This is done by redoing the test performed in _test_cmd() - but this time bkgmethod is CUSTOM and the regions are read - from ds9 region files. Exactly the same output of csphagen - compared to _test_cmd() is expected. - Hint: The output region files of _test_cmd() could be used - directly, but to make the execution of this test case standalone - the output once created by _test_cmd() was copied and added - staticly to the repository. - """ - # Set script name - csphagen = self._script('csphagen') - - # Setup csphagen command - # TODO: remove rad from cmd line arguments. currently it is needed (ctool::set_obs_bounds needs rad!) - cmd = csphagen + ' inobs="' + self._myevents1 + \ - '" caldb="' + self._caldb + '" irf="' + self._irf + \ - '" ebinalg="LOG" emin=0.1 emax=100. enumbins=' + \ - str(self._nbins) + \ - ' etruemin=0.05 etruemax=150 etruebins=5 '+ \ - 'stack="no" ' + \ - 'inexclusion="' + self._exclusion + '" ' + \ - 'bkgmethod="CUSTOM" ' + \ - 'rad=0.2 ' + \ - 'srcregfile="' + self._regfile_src + '" ' + \ - 'bkgregfile="' + self._regfile_bkg + '" ' + \ - 'outobs="csphagen_regfiles_cmd1.xml" ' + \ - 'prefix="csphagen_regfiles_cmd1" ' + \ - 'logfile="csphagen_regfiles_cmd1.log" chatter=2 ' + \ - 'coordsys="CEL" ra=83.633 dec=22.0145' - - # Check if execution was successful - self.test_assert(self._execute(cmd) == 0, - 'Check successful execution from command line') - - # Check output files - self._check_output('csphagen_regfiles_cmd1', self._nbins, self._nreg_with_excl) - self._check_outobs('csphagen_regfiles_cmd1', 1) - - # Check resulting number of counts, which should be equal to those obtained in _test_cmd() - counts_custom = self._read_pha_counts('csphagen_regfiles_cmd1_pha_on.fits') + # Setup csphagen for test with custom On and Off regions provided + phagen = cscripts.csphagen() + phagen['inobs'] = self._myevents1 + phagen['caldb'] = self._caldb + phagen['irf'] = self._irf + phagen['ebinalg'] = 'LOG' + phagen['emin'] = 0.1 + phagen['emax'] = 100.0 + phagen['enumbins'] = self._nbins + phagen['bkgmethod'] = 'CUSTOM' + phagen['srcregfile'] = self._regfile_src + phagen['bkgregfile'] = self._regfile_bkg + phagen['etruemin'] = 0.05 + phagen['etruemax'] = 150.0 + phagen['etruebins'] = 5 + phagen['stack'] = False + phagen['outobs'] = 'csphagen_py5.xml' + phagen['prefix'] = 'csphagen_py5' + phagen['logfile'] = 'csphagen_py5.log' + phagen['chatter'] = 2 + + # Execute script + phagen.execute() - self.test_assert(counts_custom == self._counts_reflected, - 'Check bkgmethods CUSTOM and REFLECTED: number of on-counts should be equal') + # Check output + self._check_output('csphagen_py5', self._nbins, self._nreg_bkg_reg) + self._check_outobs('csphagen_py5', 1) # Return return - def _check_ebounds(self, table, bins): """ Check EBOUNDS table -- libgit2 0.21.4