Commit aa5dede98cc11e8676371cb357fe917232b35cae
1 parent
a3e46f27
Add csiactobs script unit test.
Showing
4 changed files
with
222 additions
and
7 deletions
cscripts/Makefile.am
| @@ -31,7 +31,8 @@ testscripts = tests/__init__.py \ | @@ -31,7 +31,8 @@ testscripts = tests/__init__.py \ | ||
| 31 | ../test/test_cslightcrv.py \ | 31 | ../test/test_cslightcrv.py \ |
| 32 | ../test/test_csfindobs.py \ | 32 | ../test/test_csfindobs.py \ |
| 33 | ../test/test_csiactcopy.py \ | 33 | ../test/test_csiactcopy.py \ |
| 34 | - ../test/test_csiactdata.py | 34 | + ../test/test_csiactdata.py \ |
| 35 | + ../test/test_csiactobs.py | ||
| 35 | 36 | ||
| 36 | # Test data to be distributed in $(pyexecdir)/cscripts/tests/data | 37 | # Test data to be distributed in $(pyexecdir)/cscripts/tests/data |
| 37 | testdata = ../test/data/crab.xml \ | 38 | testdata = ../test/data/crab.xml \ |
cscripts/csiactobs.py
| @@ -201,6 +201,11 @@ class csiactobs(ctools.cscript): | @@ -201,6 +201,11 @@ class csiactobs(ctools.cscript): | ||
| 201 | self._subdir = os.path.dirname(self._hdu_index) | 201 | self._subdir = os.path.dirname(self._hdu_index) |
| 202 | self._debug = False # Debugging in client tools | 202 | self._debug = False # Debugging in client tools |
| 203 | 203 | ||
| 204 | + # Write input parameters into logger | ||
| 205 | + if self._logTerse(): | ||
| 206 | + self._log_parameters() | ||
| 207 | + self._log("\n") | ||
| 208 | + | ||
| 204 | # Return | 209 | # Return |
| 205 | return | 210 | return |
| 206 | 211 | ||
| @@ -345,11 +350,6 @@ class csiactobs(ctools.cscript): | @@ -345,11 +350,6 @@ class csiactobs(ctools.cscript): | ||
| 345 | # Get parameters | 350 | # Get parameters |
| 346 | self._get_parameters() | 351 | self._get_parameters() |
| 347 | 352 | ||
| 348 | - # Write input parameters into logger | ||
| 349 | - if self._logTerse(): | ||
| 350 | - self._log_parameters() | ||
| 351 | - self._log("\n") | ||
| 352 | - | ||
| 353 | # Clear models | 353 | # Clear models |
| 354 | self._models.clear() | 354 | self._models.clear() |
| 355 | 355 | ||
| @@ -618,7 +618,7 @@ class csiactobs(ctools.cscript): | @@ -618,7 +618,7 @@ class csiactobs(ctools.cscript): | ||
| 618 | self._xml.save(self._outobs) | 618 | self._xml.save(self._outobs) |
| 619 | 619 | ||
| 620 | # Save model XML file | 620 | # Save model XML file |
| 621 | - self._models.save(self._outmodel, self._clobber()) | 621 | + self._models.save(self._outmodel) |
| 622 | 622 | ||
| 623 | # Return | 623 | # Return |
| 624 | return | 624 | return |
test/test_csiactobs.py
0 → 100755
| 1 | +#! /usr/bin/env python | ||
| 2 | +# ========================================================================== | ||
| 3 | +# This scripts performs unit tests for the csiactobs 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 csiactobs script # | ||
| 28 | +# =============================== # | ||
| 29 | +class Test(gammalib.GPythonTestSuite): | ||
| 30 | + """ | ||
| 31 | + Test class for csiactobs script. | ||
| 32 | + | ||
| 33 | + This test class makes unit tests for the csiactobs 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("csiactobs") | ||
| 55 | + | ||
| 56 | + # Append tests | ||
| 57 | + self.append(self._test_cmd, "Test csiactobs on command line") | ||
| 58 | + self.append(self._test_python, "Test csiactobs from Python") | ||
| 59 | + | ||
| 60 | + # Return | ||
| 61 | + return | ||
| 62 | + | ||
| 63 | + # Test csiactobs on command line | ||
| 64 | + def _test_cmd(self): | ||
| 65 | + """ | ||
| 66 | + Test csiactobs on the command line. | ||
| 67 | + """ | ||
| 68 | + # Kluge to set the command (installed version has no README file) | ||
| 69 | + if os.path.isfile("README"): | ||
| 70 | + csiactobs = "../cscripts/csiactobs.py" | ||
| 71 | + else: | ||
| 72 | + csiactobs = "csiactobs" | ||
| 73 | + | ||
| 74 | + # Setup csiactobs command | ||
| 75 | + cmd = csiactobs+' datapath="iactdata"'+ \ | ||
| 76 | + ' prodname="unit-test"'+ \ | ||
| 77 | + ' infile="runlist_cmd1.dat"'+ \ | ||
| 78 | + ' bkgpars=1'+\ | ||
| 79 | + ' outobs="obs_iactobs_cmd1.xml"'+ \ | ||
| 80 | + ' outmodel="bgd_iactobs_cmd1.xml"'+ \ | ||
| 81 | + ' logfile="csiactobs_cmd1.log" chatter=1' | ||
| 82 | + | ||
| 83 | + # Execute csiactobs, 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 csiactobs execution on command line") | ||
| 92 | + | ||
| 93 | + # Check observation definition XML file | ||
| 94 | + self._check_obsdef("obs_iactobs_cmd1.xml") | ||
| 95 | + | ||
| 96 | + # Check model definition XML file | ||
| 97 | + self._check_moddef("bgd_iactobs_cmd1.xml") | ||
| 98 | + | ||
| 99 | + # Setup csiactobs command | ||
| 100 | + cmd = csiactobs+' datapath="data_path_that_does_not_exist"'+ \ | ||
| 101 | + ' prodname="unit-test"'+ \ | ||
| 102 | + ' infile="runlist_cmd1.dat"'+ \ | ||
| 103 | + ' bkgpars=1'+\ | ||
| 104 | + ' outobs="obs_iactobs_cmd2.xml"'+ \ | ||
| 105 | + ' outmodel="bgd_iactobs_cmd2.xml"'+ \ | ||
| 106 | + ' logfile="csiactobs_cmd2.log" chatter=1' | ||
| 107 | + | ||
| 108 | + # Execute csiactobs, make sure we catch any exception | ||
| 109 | + try: | ||
| 110 | + rc = os.system(cmd+" >/dev/null 2>&1") | ||
| 111 | + except: | ||
| 112 | + pass | ||
| 113 | + | ||
| 114 | + # Check if execution failed | ||
| 115 | + self.test_assert(rc != 0, | ||
| 116 | + "Failure of csiactobs execution on command line") | ||
| 117 | + | ||
| 118 | + # Return | ||
| 119 | + return | ||
| 120 | + | ||
| 121 | + # Test csiactobs from Python | ||
| 122 | + def _test_python(self): | ||
| 123 | + """ | ||
| 124 | + Test csiactobs from Python. | ||
| 125 | + """ | ||
| 126 | + # Set-up csiactobs | ||
| 127 | + iactobs = cscripts.csiactobs() | ||
| 128 | + iactobs["datapath"] = "iactdata" | ||
| 129 | + iactobs["prodname"] = "unit-test" | ||
| 130 | + iactobs["infile"] = "runlist_py1.dat" | ||
| 131 | + iactobs["bkgpars"] = 1 | ||
| 132 | + iactobs["outobs"] = "obs_iactobs_py1.xml" | ||
| 133 | + iactobs["outmodel"] = "bgd_iactobs_py1.xml" | ||
| 134 | + iactobs["logfile"] = "csiactobs_py1.log" | ||
| 135 | + iactobs["chatter"] = 2 | ||
| 136 | + | ||
| 137 | + # Run csiactobs script and save run list | ||
| 138 | + iactobs.logFileOpen() # Make sure we get a log file | ||
| 139 | + iactobs.run() | ||
| 140 | + iactobs.save() | ||
| 141 | + | ||
| 142 | + # Check observation definition XML file | ||
| 143 | + self._check_obsdef("obs_iactobs_py1.xml") | ||
| 144 | + | ||
| 145 | + # Check model definition XML file | ||
| 146 | + self._check_moddef("bgd_iactobs_py1.xml") | ||
| 147 | + | ||
| 148 | + # Return | ||
| 149 | + return | ||
| 150 | + | ||
| 151 | + # Check observation definition XML file | ||
| 152 | + def _check_obsdef(self, filename): | ||
| 153 | + """ | ||
| 154 | + Check observation definition XML file. | ||
| 155 | + """ | ||
| 156 | + # Load observation definition XML file | ||
| 157 | + obs = gammalib.GObservations(filename) | ||
| 158 | + | ||
| 159 | + # Get response | ||
| 160 | + rsp = obs[0].response() | ||
| 161 | + | ||
| 162 | + # Check number of observations | ||
| 163 | + self.test_value(obs.size(), 1, | ||
| 164 | + 'Check for single observation in XML file') | ||
| 165 | + self.test_assert(obs[0].eventfile().file() == 'crab_events.fits.gz', | ||
| 166 | + 'Check event file name') | ||
| 167 | + self.test_assert(obs[0].eventfile().extname() == 'EVENTS', | ||
| 168 | + 'Check event extension name') | ||
| 169 | + self.test_assert(rsp.aeff().filename().file() == | ||
| 170 | + 'irf_file.fits.gz', | ||
| 171 | + 'Check effective area name') | ||
| 172 | + self.test_assert(rsp.aeff().filename().extname() == | ||
| 173 | + 'EFFECTIVE AREA', | ||
| 174 | + 'Check effective area extension name') | ||
| 175 | + self.test_assert(rsp.psf().filename().file() == | ||
| 176 | + 'irf_file.fits.gz', | ||
| 177 | + 'Check point spread function name') | ||
| 178 | + self.test_assert(rsp.psf().filename().extname() == | ||
| 179 | + 'POINT SPREAD FUNCTION', | ||
| 180 | + 'Check point spread function extension name') | ||
| 181 | + self.test_assert(rsp.edisp().filename().file() == | ||
| 182 | + 'irf_file.fits.gz', | ||
| 183 | + 'Check energy dispersion name') | ||
| 184 | + self.test_assert(rsp.edisp().filename().extname() == | ||
| 185 | + 'ENERGY DISPERSION', | ||
| 186 | + 'Check energy dispersion extension name') | ||
| 187 | + self.test_assert(rsp.background().filename().file() == | ||
| 188 | + 'irf_file.fits.gz', | ||
| 189 | + 'Check background name') | ||
| 190 | + self.test_assert(rsp.background().filename().extname() == | ||
| 191 | + 'BACKGROUND', | ||
| 192 | + 'Check background extension name') | ||
| 193 | + | ||
| 194 | + # Return | ||
| 195 | + return | ||
| 196 | + | ||
| 197 | + # Check model XML file | ||
| 198 | + def _check_moddef(self, filename): | ||
| 199 | + """ | ||
| 200 | + Check model definition XML file. | ||
| 201 | + """ | ||
| 202 | + # Load model definition XML file | ||
| 203 | + models = gammalib.GModels(filename) | ||
| 204 | + | ||
| 205 | + # Check number of models | ||
| 206 | + self.test_value(models.size(), 1, | ||
| 207 | + 'Check for single model in XML file') | ||
| 208 | + | ||
| 209 | + # Return | ||
| 210 | + return |
test/test_python_cscripts.py
| @@ -29,6 +29,7 @@ import test_cslightcrv | @@ -29,6 +29,7 @@ import test_cslightcrv | ||
| 29 | import test_csfindobs | 29 | import test_csfindobs |
| 30 | import test_csiactcopy | 30 | import test_csiactcopy |
| 31 | import test_csiactdata | 31 | import test_csiactdata |
| 32 | +import test_csiactobs | ||
| 32 | 33 | ||
| 33 | 34 | ||
| 34 | # ================== # | 35 | # ================== # |
| @@ -100,16 +101,19 @@ def test(installed=False, debug=False): | @@ -100,16 +101,19 @@ def test(installed=False, debug=False): | ||
| 100 | suite_csfindobs = test_csfindobs.Test() | 101 | suite_csfindobs = test_csfindobs.Test() |
| 101 | suite_csiactcopy = test_csiactcopy.Test() | 102 | suite_csiactcopy = test_csiactcopy.Test() |
| 102 | suite_csiactdata = test_csiactdata.Test() | 103 | suite_csiactdata = test_csiactdata.Test() |
| 104 | + suite_csiactobs = test_csiactobs.Test() | ||
| 103 | 105 | ||
| 104 | # Setup unit tests | 106 | # Setup unit tests |
| 105 | suite_csfindobs.set() | 107 | suite_csfindobs.set() |
| 106 | suite_csiactcopy.set() | 108 | suite_csiactcopy.set() |
| 107 | suite_csiactdata.set() | 109 | suite_csiactdata.set() |
| 110 | + suite_csiactobs.set() | ||
| 108 | 111 | ||
| 109 | # Append tests to container | 112 | # Append tests to container |
| 110 | suites.append(suite_csfindobs) | 113 | suites.append(suite_csfindobs) |
| 111 | suites.append(suite_csiactcopy) | 114 | suites.append(suite_csiactcopy) |
| 112 | suites.append(suite_csiactdata) | 115 | suites.append(suite_csiactdata) |
| 116 | + suites.append(suite_csiactobs) | ||
| 113 | 117 | ||
| 114 | # Run test suite | 118 | # Run test suite |
| 115 | success = suites.run() | 119 | success = suites.run() |