Commit e77f96b37c593cb9ae4ce9299191dd99e60d586e

Authored by Jürgen Knödlseder
1 parent 816acf9e

Add cspull script unit test.

cscripts/Makefile.am
... ... @@ -33,6 +33,7 @@ testscripts = tests/__init__.py \
33 33 ../test/test_csobs2caldb.py \
34 34 ../test/test_csobsdef.py \
35 35 ../test/test_csobsinfo.py \
  36 + ../test/test_cspull.py \
36 37 ../test/test_csfindobs.py \
37 38 ../test/test_csiactcopy.py \
38 39 ../test/test_csiactdata.py \
... ...
cscripts/cspull.py
... ... @@ -82,10 +82,8 @@ class cspull(ctools.cscript):
82 82 """
83 83 Get parameters from parfile.
84 84 """
85   - # Get parameters
86   -
87   - # Set observation if not done before
88   - if self._obs == None or self._obs.size() == 0:
  85 + # If there are no observations in container then get some ...
  86 + if self._obs.size() == 0:
89 87 self._obs = self._get_observations()
90 88  
91 89 # Check for requested pattern and use above
... ... @@ -94,6 +92,11 @@ class cspull(ctools.cscript):
94 92 if self._pattern == "four":
95 93 self._obs = self._set_obs()
96 94  
  95 + # ... otherwise add response information and energy boundaries
  96 + # in case they are missing.
  97 + else:
  98 + self._setup_observations(self._obs)
  99 +
97 100 # Get number of energy bins
98 101 self._enumbins = self["enumbins"].integer()
99 102  
... ...
test/data/obs_stacked.xml 0 → 100644
  1 +<?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2 +<observation_list title="observation list">
  3 + <observation name="Crab" id="00001" instrument="CTA">
  4 + <parameter name="CountsCube" file="data/crab_cntmap.fits"/>
  5 + <parameter name="ExposureCube" file="data/crab_expcube.fits"/>
  6 + <parameter name="PsfCube" file="data/crab_psfcube.fits"/>
  7 + <parameter name="BkgCube" file="data/crab_bkgcube.fits"/>
  8 + </observation>
  9 +</observation_list>
... ...
test/test_cspull.py 0 → 100755
  1 +#! /usr/bin/env python
  2 +# ==========================================================================
  3 +# This scripts performs unit tests for the cspull script.
  4 +#
  5 +# Copyright (C) 2016 Juergen Knoedlseder
  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 os
  22 +import gammalib
  23 +import cscripts
  24 +
  25 +
  26 +# ============================ #
  27 +# Test class for cspull script #
  28 +# ============================ #
  29 +class Test(gammalib.GPythonTestSuite):
  30 + """
  31 + Test class for cspull script.
  32 +
  33 + This test class makes unit tests for the cspull 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 + gammalib.GPythonTestSuite.__init__(self)
  44 +
  45 + # Return
  46 + return
  47 +
  48 + # Set test functions
  49 + def set(self):
  50 + """
  51 + Set all test functions.
  52 + """
  53 + # Set test name
  54 + self.name("cspull")
  55 +
  56 + # Append tests
  57 + self.append(self._test_cmd, "Test cspull on command line")
  58 + self.append(self._test_python, "Test cspull from Python")
  59 +
  60 + # Return
  61 + return
  62 +
  63 + # Test cspull on command line
  64 + def _test_cmd(self):
  65 + """
  66 + Test cspull on the command line.
  67 + """
  68 + # Kluge to set the command (installed version has no README file)
  69 + if os.path.isfile("README"):
  70 + cspull = "../cscripts/cspull.py"
  71 + else:
  72 + cspull = "cspull"
  73 +
  74 + # Setup cspull command
  75 + cmd = cspull+' inmodel="data/crab.xml"'+ \
  76 + ' outfile="cspull_cmd1.dat"'+ \
  77 + ' ntrials=3 caldb="prod2" irf="South_0.5h"'+ \
  78 + ' ra=83.6331 dec=22.0145 emin=0.1 emax=100.0'+ \
  79 + ' enumbins=0 tmax=1800.0 deadc=0.95 rad=5.0'+ \
  80 + ' npix=200 binsz=0.05'+ \
  81 + ' logfile="cspull_cmd1.log" chatter=1'
  82 +
  83 + # Execute cspull, make sure we catch any exception
  84 + try:
  85 + rc = os.system(cmd+" >/dev/null 2>&1")
  86 + except:
  87 + pass
  88 +
  89 + # Check if execution was successful
  90 + self.test_assert(rc == 0,
  91 + "Successful cspull execution on command line")
  92 +
  93 + # Check pull distribution file
  94 + self._check_pull_file("cspull_cmd1.dat")
  95 +
  96 + # Setup cspull command
  97 + cmd = cspull+' inmodel="model_that_does_not_exist.xml"'+ \
  98 + ' outfile="cspull_cmd1.dat"'+ \
  99 + ' ntrials=3 caldb="prod2" irf="South_0.5h"'+ \
  100 + ' ra=83.6331 dec=22.0145 emin=0.1 emax=100.0'+ \
  101 + ' enumbins=0 tmax=1800.0 deadc=0.95 rad=5.0'+ \
  102 + ' npix=200 binsz=0.05'+ \
  103 + ' logfile="cspull_cmd2.log"'
  104 +
  105 + # Execute cspull, make sure we catch any exception
  106 + try:
  107 + rc = os.system(cmd+" >/dev/null 2>&1")
  108 + except:
  109 + pass
  110 +
  111 + # Check if execution failed
  112 + self.test_assert(rc != 0,
  113 + "Failure of cspull execution on command line")
  114 +
  115 + # Return
  116 + return
  117 +
  118 + # Test cspull from Python
  119 + def _test_python(self):
  120 + """
  121 + Test cspull from Python.
  122 + """
  123 + # Set-up unbinned cspull
  124 + pull = cscripts.cspull()
  125 + pull["inmodel"] = "data/crab.xml"
  126 + pull["outfile"] = "cspull_py1.dat"
  127 + pull["ntrials"] = 3
  128 + pull["caldb"] = "prod2"
  129 + pull["irf"] = "South_0.5h"
  130 + pull["ra"] = 83.6331
  131 + pull["dec"] = 22.0145
  132 + pull["emin"] = 0.1
  133 + pull["emax"] = 100.0
  134 + pull["enumbins"] = 0
  135 + pull["tmax"] = 1800.0
  136 + pull["deadc"] = 0.95
  137 + pull["rad"] = 5.0
  138 + pull["logfile"] = "cspull_py1.log"
  139 + pull["chatter"] = 2
  140 +
  141 + # Run cspull script
  142 + pull.logFileOpen() # Make sure we get a log file
  143 + pull.run()
  144 + #pull.save()
  145 +
  146 + # Check pull distribution file
  147 + self._check_pull_file("cspull_py1.dat")
  148 +
  149 + # Set-up binned cspull
  150 + pull = cscripts.cspull()
  151 + pull["inmodel"] = "data/crab.xml"
  152 + pull["outfile"] = "cspull_py2.dat"
  153 + pull["ntrials"] = 3
  154 + pull["caldb"] = "prod2"
  155 + pull["irf"] = "South_0.5h"
  156 + pull["ra"] = 83.6331
  157 + pull["dec"] = 22.0145
  158 + pull["emin"] = 0.1
  159 + pull["emax"] = 100.0
  160 + pull["enumbins"] = 10
  161 + pull["tmax"] = 1800.0
  162 + pull["deadc"] = 0.95
  163 + pull["rad"] = 5.0
  164 + pull["npix"] = 100
  165 + pull["binsz"] = 0.02
  166 + pull["coordsys"] = "CEL"
  167 + pull["proj"] = "TAN"
  168 + pull["logfile"] = "cspull_py2.log"
  169 + pull["chatter"] = 3
  170 +
  171 + # Execute cspull script
  172 + pull.execute()
  173 +
  174 + # Check pull distribution file
  175 + self._check_pull_file("cspull_py2.dat")
  176 +
  177 + # Set-up cspull from event list
  178 + pull = cscripts.cspull()
  179 + pull["inobs"] = "data/crab_events.fits"
  180 + pull["inmodel"] = "data/crab.xml"
  181 + pull["outfile"] = "cspull_py3.dat"
  182 + pull["ntrials"] = 3
  183 + pull["caldb"] = "prod2"
  184 + pull["irf"] = "South_0.5h"
  185 + pull["enumbins"] = 0
  186 + pull["logfile"] = "cspull_py3.log"
  187 + pull["chatter"] = 4
  188 +
  189 + # Execute cspull script
  190 + pull.execute()
  191 +
  192 + # Check pull distribution file
  193 + self._check_pull_file("cspull_py3.dat")
  194 +
  195 + # Build observation container with unbinned observation
  196 + cta = gammalib.GCTAObservation("data/crab_events.fits")
  197 + obs = gammalib.GObservations()
  198 + obs.append(cta)
  199 +
  200 + # Set-up cspull from observation container with unbinned observation
  201 + pull = cscripts.cspull(obs)
  202 + pull["inmodel"] = "data/crab.xml"
  203 + pull["outfile"] = "cspull_py4.dat"
  204 + pull["ntrials"] = 3
  205 + pull["caldb"] = "prod2"
  206 + pull["irf"] = "South_0.5h"
  207 + pull["enumbins"] = 0
  208 + pull["logfile"] = "cspull_py4.log"
  209 + pull["chatter"] = 4
  210 +
  211 + # Execute cspull script
  212 + pull.execute()
  213 +
  214 + # Check pull distribution file
  215 + self._check_pull_file("cspull_py4.dat")
  216 +
  217 + # Set-up stacked cspull
  218 + pull = cscripts.cspull()
  219 + pull["inobs"] = "data/obs_stacked.xml"
  220 + pull["inmodel"] = "data/crab_bkgcube.xml"
  221 + pull["outfile"] = "cspull_py5.dat"
  222 + pull["ntrials"] = 3
  223 + pull["caldb"] = "prod2"
  224 + pull["irf"] = "South_0.5h"
  225 + pull["enumbins"] = 0
  226 + pull["logfile"] = "cspull_py5.log"
  227 + pull["chatter"] = 4
  228 +
  229 + # Execute cspull script
  230 + pull.execute()
  231 +
  232 + # Check pull distribution file
  233 + self._check_pull_file("cspull_py5.dat")
  234 +
  235 + # Return
  236 + return
  237 +
  238 + # Check pull file
  239 + def _check_pull_file(self, filename):
  240 + """
  241 + Check pull file.
  242 + """
  243 + # Open pull file as CSV file
  244 + pulls = gammalib.GCsv(filename)
  245 +
  246 + # Check dimensions
  247 + self.test_value(pulls.nrows(), 4,
  248 + 'Check for 4 rows in pull file')
  249 +
  250 + # Return
  251 + return
... ...
test/test_python_cscripts.py
... ... @@ -30,6 +30,7 @@ import test_csmodelmerge
30 30 import test_csobs2caldb
31 31 import test_csobsdef
32 32 import test_csobsinfo
  33 +import test_cspull
33 34 import test_csfindobs
34 35 import test_csiactcopy
35 36 import test_csiactdata
... ... @@ -89,6 +90,7 @@ def test(installed=False, debug=False):
89 90 suite_csobs2caldb = test_csobs2caldb.Test()
90 91 suite_csobsdef = test_csobsdef.Test()
91 92 suite_csobsinfo = test_csobsinfo.Test()
  93 + suite_cspull = test_cspull.Test()
92 94  
93 95 # Setup unit tests
94 96 suite_cscaldb.set()
... ... @@ -98,6 +100,7 @@ def test(installed=False, debug=False):
98 100 suite_csobs2caldb.set()
99 101 suite_csobsdef.set()
100 102 suite_csobsinfo.set()
  103 + suite_cspull.set()
101 104  
102 105 # Append tests to container
103 106 suites.append(suite_cscaldb)
... ... @@ -107,6 +110,7 @@ def test(installed=False, debug=False):
107 110 suites.append(suite_csobs2caldb)
108 111 suites.append(suite_csobsdef)
109 112 suites.append(suite_csobsinfo)
  113 + suites.append(suite_cspull)
110 114  
111 115 # Append tests for Python 2.6+ (the IACT cscripts depend on the json
112 116 # module which is only available since Python 2.6+
... ...