Commit 54899f4e08b2a8dcff6ede4354c972806c1b43a9

Authored by L. Tibaldo
1 parent 3836359d

test unit

Showing 1 changed file with 275 additions and 0 deletions
test/test_csresspec.py 0 → 100755
  1 +#! /usr/bin/env python
  2 +# ==========================================================================
  3 +# This scripts performs unit tests for the csresspec script.
  4 +#
  5 +# Copyright (C) 2017- Luigi Tibaldo
  6 +#
  7 +# This program is free software: you can redistribute it and/or modify
  8 +# it under the terms of the GNU General Public License as published by
  9 +# the Free Software Foundation, either version 3 of the License, or
  10 +# (at your option) any later version.
  11 +#
  12 +# This program is distributed in the hope that it will be useful,
  13 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15 +# GNU General Public License for more details.
  16 +#
  17 +# You should have received a copy of the GNU General Public License
  18 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  19 +#
  20 +# ==========================================================================
  21 +import gammalib
  22 +import cscripts
  23 +from testing import test
  24 +
  25 +
  26 +# =============================== #
  27 +# Test class for csresspec script #
  28 +# =============================== #
  29 +class Test(test):
  30 + """
  31 + Test class for csresspec script
  32 +
  33 + This test class makes unit tests for the csresspec script by using it
  34 + from the command line and from Python.
  35 + """
  36 +
  37 + # Constructor
  38 + def __init__(self):
  39 + """
  40 + Constructor
  41 + """
  42 + # Call base class constructor
  43 + test.__init__(self)
  44 +
  45 + # Set members
  46 + self._inonoff = self._datadir + '/onoff_obs.xml'
  47 + self._onoff_model = self._datadir + '/onoff_model.xml'
  48 +
  49 + # Return
  50 + return
  51 +
  52 + # Set test functions
  53 + def set(self):
  54 + """
  55 + Set all test functions
  56 + """
  57 + # Set test name
  58 + self.name('csresspec')
  59 +
  60 + # Append tests
  61 + self.append(self._test_cmd, 'Test csresspec on command line')
  62 + self.append(self._test_python, 'Test csresspec from Python')
  63 +
  64 + # Return
  65 + return
  66 +
  67 + # Test csresspec on command line
  68 + def _test_cmd(self):
  69 + """
  70 + Test csresspec on the command line.
  71 + """
  72 + # Set script name
  73 + csresspec = self._script('csresspec')
  74 +
  75 + # Setup csresspec command
  76 + cmd = csresspec + ' inobs="' + self._events + '"' + \
  77 + ' outfile="csresspec_cmd1.fits"' + \
  78 + ' inmodel="' + self._model + '"' + \
  79 + ' caldb="' + self._caldb + '" irf="' + self._irf + '"' + \
  80 + ' emin=0.1 emax=100.0 enumbins=20' + \
  81 + ' mask=no algorithm="SUB"' + \
  82 + ' logfile="csresspec_cmd1.log" chatter=4'
  83 +
  84 + # Check if execution of wrong command fails
  85 + self.test_assert(self._execute('command_that_does_not_exist') != 0,
  86 + 'Self test of test script')
  87 +
  88 + # Check if execution was successful
  89 + self.test_assert(self._execute(cmd) == 0,
  90 + 'Check successful execution from command line')
  91 +
  92 + # Check result file
  93 + self._check_result_file('csresspec_cmd1.fits', 20, 5)
  94 +
  95 + # Setup csresspec command
  96 + cmd = csresspec + ' inobs="event_file_that_does_not_exist.fits"' + \
  97 + ' outfile="csresspec_cmd2.fits"' + \
  98 + ' inmodel="' + self._model + '"' + \
  99 + ' caldb="' + self._caldb + '" irf="' + self._irf + '"' + \
  100 + ' emin=0.1 emax=100.0 enumbins=20' + \
  101 + ' mask=no algorithm="SUBDIVSQRT"' + \
  102 + ' logfile="csresspec_cmd2.log" debug=yes chatter=2'
  103 +
  104 + # Check if execution failed
  105 + self.test_assert(self._execute(cmd) != 0,
  106 + 'Check invalid input file when executed from command line')
  107 +
  108 + # Check csresspec --help
  109 + self._check_help(csresspec)
  110 +
  111 + # Return
  112 + return
  113 +
  114 + # Test csresspec from Python
  115 + def _test_python(self):
  116 + """
  117 + Test csresspec from Python
  118 + """
  119 + # Set-up csresspec for event list
  120 + resspec = cscripts.csresspec()
  121 + resspec['inobs'] = self._events
  122 + resspec['outfile'] = 'csresspec_py1.fits'
  123 + resspec['inmodel'] = self._model
  124 + resspec['caldb'] = self._caldb
  125 + resspec['irf'] = self._irf
  126 + resspec['emin'] = 0.1
  127 + resspec['emax'] = 100.0
  128 + resspec['enumbins'] = 20
  129 + resspec['mask'] = False
  130 + resspec['algorithm'] = 'SUB'
  131 + resspec['logfile'] = 'csresspec_py1.log'
  132 + resspec['chatter'] = 2
  133 +
  134 + # Run csresspec script
  135 + resspec.logFileOpen() # Make sure we get a log file
  136 + resspec.run()
  137 + resspec.save()
  138 +
  139 + # Check result file
  140 + self._check_result_file('csresspec_py1.fits', 20, 5)
  141 +
  142 + # Set-up csresspec for event list
  143 + # with computation of individual model components
  144 + resspec = cscripts.csresspec()
  145 + resspec['inobs'] = self._events
  146 + resspec['outfile'] = 'csresspec_py2.fits'
  147 + resspec['inmodel'] = self._model
  148 + resspec['caldb'] = self._caldb
  149 + resspec['irf'] = self._irf
  150 + resspec['emin'] = 0.1
  151 + resspec['emax'] = 100.0
  152 + resspec['enumbins'] = 20
  153 + resspec['mask'] = False
  154 + resspec['algorithm'] = 'SUBDIV'
  155 + resspec['components'] = True
  156 + resspec['logfile'] = 'csresspec_py2.log'
  157 + resspec['chatter'] = 2
  158 +
  159 + # Run csresspec script
  160 + resspec.execute()
  161 +
  162 + # Check result file
  163 + self._check_result_file('csresspec_py2.fits', 20, 7)
  164 +
  165 + # Set-up csresspec for counts cube
  166 + resspec = cscripts.csresspec()
  167 + resspec['inobs'] = self._cntcube
  168 + resspec['modcube'] = 'NONE'
  169 + resspec['expcube'] = 'NONE'
  170 + resspec['psfcube'] = 'NONE'
  171 + resspec['edispcube'] = 'NONE'
  172 + resspec['bkgcube'] = 'NONE'
  173 + resspec['caldb'] = self._caldb
  174 + resspec['irf'] = self._irf
  175 + resspec['inmodel'] = self._model
  176 + resspec['outfile'] = 'csresspec_py3.fits'
  177 + resspec['mask'] = False
  178 + resspec['algorithm'] = 'SUBDIVSQRT'
  179 + resspec['logfile'] = 'csresspec_py3.log'
  180 + resspec['chatter'] = 3
  181 +
  182 + # Run csresspec script
  183 + resspec.execute()
  184 +
  185 + # Check result file
  186 + self._check_result_file('csresspec_py3.fits', 20, 5)
  187 +
  188 + # Set-up csresspec for counts cube
  189 + # with source mask
  190 + resspec = cscripts.csresspec()
  191 + resspec['inobs'] = self._cntcube
  192 + resspec['modcube'] = 'NONE'
  193 + resspec['expcube'] = 'NONE'
  194 + resspec['psfcube'] = 'NONE'
  195 + resspec['edispcube'] = 'NONE'
  196 + resspec['bkgcube'] = 'NONE'
  197 + resspec['caldb'] = self._caldb
  198 + resspec['irf'] = self._irf
  199 + resspec['inmodel'] = self._model
  200 + resspec['outfile'] = 'csresspec_py4.fits'
  201 + resspec['mask'] = True
  202 + resspec['ra'] = 83.63
  203 + resspec['dec'] = 22.01
  204 + resspec['rad'] = 0.2
  205 + resspec['regfile'] = 'NONE'
  206 + resspec['algorithm'] = 'SUBDIVSQRT'
  207 + resspec['logfile'] = 'csresspec_py4.log'
  208 + resspec['chatter'] = 3
  209 +
  210 + # Run csresspec script
  211 + resspec.execute()
  212 +
  213 + # Check result file
  214 + self._check_result_file('csresspec_py4.fits', 20, 5)
  215 +
  216 + # Set-up csresspec for On/Off observations
  217 + resspec = cscripts.csresspec()
  218 + resspec['inobs'] = self._inonoff
  219 + resspec['inmodel'] = self._onoff_model
  220 + resspec['outfile'] = 'csresspec_py5.fits'
  221 + resspec['algorithm'] = 'SIGNIFICANCE'
  222 + resspec['stack'] = False
  223 + resspec['logfile'] = 'csresspec_py5.log'
  224 + resspec['chatter'] = 3
  225 +
  226 + # Run csresspec script
  227 + resspec.execute()
  228 +
  229 + # Check result file
  230 + self._check_result_file('csresspec_py5.fits', 20, 8, nobs=2)
  231 +
  232 + # Set-up csresspec for On/Off observations
  233 + # in stacked mode and with WSTAT background
  234 + resspec = cscripts.csresspec()
  235 + resspec['inobs'] = self._inonoff
  236 + resspec['inmodel'] = self._onoff_model
  237 + resspec['outfile'] = 'csresspec_py6.fits'
  238 + resspec['algorithm'] = 'SIGNIFICANCE'
  239 + resspec['statistic'] = 'WSTAT'
  240 + resspec['stack'] = True
  241 + resspec['logfile'] = 'csresspec_py6.log'
  242 + resspec['chatter'] = 3
  243 +
  244 + # Run csresspec script
  245 + resspec.execute()
  246 +
  247 + # Check result file
  248 + self._check_result_file('csresspec_py6.fits', 20, 8)
  249 +
  250 + # Return
  251 + return
  252 +
  253 + # Check result file
  254 + def _check_result_file(self, filename, enumbins, ncols, nobs=1):
  255 + """
  256 + Check result file
  257 + """
  258 + # Load residual file
  259 + fits = gammalib.GFits(filename)
  260 +
  261 + # Check number of HDUs = observations (+ first empty image)
  262 + self.test_value(fits.size(), 1 + nobs, 'Number of observations')
  263 +
  264 + # Check first hdu with residuals
  265 + table = fits[1]
  266 + # Check number of rows = number of energy bins
  267 + self.test_value(table.nrows(), enumbins,
  268 + 'Check for %d rows in residuals' % enumbins)
  269 + # Check number of columns, 2 for energy bounds + 3 for 3D or 6 for
  270 + # On/Off + 1 for each model component if components = yes
  271 + self.test_value(table.ncols(), ncols,
  272 + 'Check for %d columns in spectrum' % ncols)
  273 +
  274 + # Return
  275 + return
... ...