Commit 6f1583ea922ab82d2710e75f78dc3d859f1580d3
1 parent
d0045ac8
Implement cslightcrv unit test.
Showing
6 changed files
with
349 additions
and
52 deletions
cscripts/Makefile.am
| @@ -26,13 +26,17 @@ linkscripts = cspull cssens cstsdist cscaldb csresmap \ | @@ -26,13 +26,17 @@ linkscripts = cspull cssens cstsdist cscaldb csresmap \ | ||
| 26 | # Test scripts to be distributed in $(pyexecdir)/cscripts/tests | 26 | # Test scripts to be distributed in $(pyexecdir)/cscripts/tests |
| 27 | testscripts = tests/__init__.py \ | 27 | testscripts = tests/__init__.py \ |
| 28 | ../test/test_python_cscripts.py \ | 28 | ../test/test_python_cscripts.py \ |
| 29 | - ../test/test_cscaldb.py | 29 | + ../test/test_cscaldb.py \ |
| 30 | + ../test/test_cslightcrv.py | ||
| 30 | 31 | ||
| 31 | # Test data to be distributed in $(pyexecdir)/cscripts/tests/data | 32 | # Test data to be distributed in $(pyexecdir)/cscripts/tests/data |
| 32 | -testdata = | 33 | +testdata = ../test/data/crab.xml \ |
| 34 | + ../test/data/crab_events.fits.gz \ | ||
| 35 | + ../test/data/crab_cntmap.fits.gz \ | ||
| 36 | + ../test/data/exclusion.reg | ||
| 33 | 37 | ||
| 34 | # Test IRFs to be distributed in $(pyexecdir)/cscripts/tests/irf | 38 | # Test IRFs to be distributed in $(pyexecdir)/cscripts/tests/irf |
| 35 | -testirf = | 39 | +#testirf = |
| 36 | 40 | ||
| 37 | # Distribute scripts and parfiles | 41 | # Distribute scripts and parfiles |
| 38 | EXTRA_DIST = $(binscripts) $(pythonscripts) | 42 | EXTRA_DIST = $(binscripts) $(pythonscripts) |
| @@ -46,8 +50,8 @@ testscriptsdir = $(pyexecdir)/cscripts/tests | @@ -46,8 +50,8 @@ testscriptsdir = $(pyexecdir)/cscripts/tests | ||
| 46 | testscripts_DATA = $(testscripts) | 50 | testscripts_DATA = $(testscripts) |
| 47 | testdatadir = $(pyexecdir)/cscripts/tests/data | 51 | testdatadir = $(pyexecdir)/cscripts/tests/data |
| 48 | testdata_DATA = $(testdata) | 52 | testdata_DATA = $(testdata) |
| 49 | -testirfdir = $(pyexecdir)/cscripts/tests/irf | ||
| 50 | -testirf_DATA = $(testirf) | 53 | +#testirfdir = $(pyexecdir)/cscripts/tests/irf |
| 54 | +#testirf_DATA = $(testirf) | ||
| 51 | 55 | ||
| 52 | # Add hook rule to create symbolic links for cscripts | 56 | # Add hook rule to create symbolic links for cscripts |
| 53 | install-data-hook: | 57 | install-data-hook: |
cscripts/cslightcrv.py
| @@ -76,12 +76,15 @@ class cslightcrv(ctools.cscript): | @@ -76,12 +76,15 @@ class cslightcrv(ctools.cscript): | ||
| 76 | self._stacked = False | 76 | self._stacked = False |
| 77 | 77 | ||
| 78 | # Initialise observation container from constructor arguments. | 78 | # Initialise observation container from constructor arguments. |
| 79 | - self._obs = self._set_input_obs(argv) | 79 | + self._obs, argv = self._set_input_obs(argv) |
| 80 | 80 | ||
| 81 | - # Initialise application by calling the appropriate class | ||
| 82 | - # constructor. | 81 | + # Initialise script by calling the appropriate class constructor. |
| 83 | self._init_cscript(argv) | 82 | self._init_cscript(argv) |
| 84 | 83 | ||
| 84 | + # Set logger properties | ||
| 85 | + self._log_header() | ||
| 86 | + self._log.date(True) | ||
| 87 | + | ||
| 85 | # Return | 88 | # Return |
| 86 | return | 89 | return |
| 87 | 90 | ||
| @@ -91,12 +94,10 @@ class cslightcrv(ctools.cscript): | @@ -91,12 +94,10 @@ class cslightcrv(ctools.cscript): | ||
| 91 | """ | 94 | """ |
| 92 | Get parameters from parfile. | 95 | Get parameters from parfile. |
| 93 | """ | 96 | """ |
| 94 | - # Set observation if not done before | ||
| 95 | - if self._obs == None or self._obs.size() == 0: | ||
| 96 | - self._require_inobs("cslightcrv::_get_parameters()") | ||
| 97 | - self._obs = self._get_observations() | 97 | + # Setup observations |
| 98 | + self._setup_observations(self._obs) | ||
| 98 | 99 | ||
| 99 | - # Set models if we have none | 100 | + # Set models if there are none in the container |
| 100 | if self._obs.models().size() == 0: | 101 | if self._obs.models().size() == 0: |
| 101 | self._obs.models(self["inmodel"].filename()) | 102 | self._obs.models(self["inmodel"].filename()) |
| 102 | 103 | ||
| @@ -436,16 +437,26 @@ class cslightcrv(ctools.cscript): | @@ -436,16 +437,26 @@ class cslightcrv(ctools.cscript): | ||
| 436 | if self._logExplicit(): | 437 | if self._logExplicit(): |
| 437 | self._log.header3("Creating point spread function cube") | 438 | self._log.header3("Creating point spread function cube") |
| 438 | 439 | ||
| 439 | - # Create psf cube | 440 | + # Compute spatial binning for point spread function and |
| 441 | + # energy dispersion cubes | ||
| 442 | + binsz = 0.1 * self["binsz"].real() | ||
| 443 | + nxpix = self["nxpix"].integer() / 10 | ||
| 444 | + nypix = self["nypix"].integer() / 10 | ||
| 445 | + if nxpix < 2: | ||
| 446 | + nxpix = 2 | ||
| 447 | + if nypix < 2: | ||
| 448 | + nypix = 2 | ||
| 449 | + | ||
| 450 | + # Create point spread function cube | ||
| 440 | psfcube = ctools.ctpsfcube(obs) | 451 | psfcube = ctools.ctpsfcube(obs) |
| 441 | psfcube["incube"] = "NONE" | 452 | psfcube["incube"] = "NONE" |
| 442 | psfcube["usepnt"] = False | 453 | psfcube["usepnt"] = False |
| 443 | psfcube["ebinalg"] = "LOG" | 454 | psfcube["ebinalg"] = "LOG" |
| 444 | psfcube["xref"] = self["xref"].real() | 455 | psfcube["xref"] = self["xref"].real() |
| 445 | psfcube["yref"] = self["yref"].real() | 456 | psfcube["yref"] = self["yref"].real() |
| 446 | - psfcube["binsz"] = self["binsz"].real() | ||
| 447 | - psfcube["nxpix"] = self["nxpix"].integer() | ||
| 448 | - psfcube["nypix"] = self["nypix"].integer() | 457 | + psfcube["binsz"] = binsz |
| 458 | + psfcube["nxpix"] = nxpix | ||
| 459 | + psfcube["nypix"] = nypix | ||
| 449 | psfcube["enumbins"] = self["enumbins"].integer() | 460 | psfcube["enumbins"] = self["enumbins"].integer() |
| 450 | psfcube["emin"] = self["emin"].real() | 461 | psfcube["emin"] = self["emin"].real() |
| 451 | psfcube["emax"] = self["emax"].real() | 462 | psfcube["emax"] = self["emax"].real() |
| @@ -460,16 +471,16 @@ class cslightcrv(ctools.cscript): | @@ -460,16 +471,16 @@ class cslightcrv(ctools.cscript): | ||
| 460 | if self._logExplicit(): | 471 | if self._logExplicit(): |
| 461 | self._log.header3("Creating energy dispersion cube") | 472 | self._log.header3("Creating energy dispersion cube") |
| 462 | 473 | ||
| 463 | - # Create edisp cube | 474 | + # Create energy dispersion cube |
| 464 | edispcube = ctools.ctedispcube(obs) | 475 | edispcube = ctools.ctedispcube(obs) |
| 465 | edispcube["incube"] = "NONE" | 476 | edispcube["incube"] = "NONE" |
| 466 | edispcube["usepnt"] = False | 477 | edispcube["usepnt"] = False |
| 467 | edispcube["ebinalg"] = "LOG" | 478 | edispcube["ebinalg"] = "LOG" |
| 468 | edispcube["xref"] = self["xref"].real() | 479 | edispcube["xref"] = self["xref"].real() |
| 469 | edispcube["yref"] = self["yref"].real() | 480 | edispcube["yref"] = self["yref"].real() |
| 470 | - edispcube["binsz"] = self["binsz"].real() | ||
| 471 | - edispcube["nxpix"] = self["nxpix"].integer() | ||
| 472 | - edispcube["nypix"] = self["nypix"].integer() | 481 | + edispcube["binsz"] = binsz |
| 482 | + edispcube["nxpix"] = nxpix | ||
| 483 | + edispcube["nypix"] = nypix | ||
| 473 | edispcube["enumbins"] = self["enumbins"].integer() | 484 | edispcube["enumbins"] = self["enumbins"].integer() |
| 474 | edispcube["emin"] = self["emin"].real() | 485 | edispcube["emin"] = self["emin"].real() |
| 475 | edispcube["emax"] = self["emax"].real() | 486 | edispcube["emax"] = self["emax"].real() |
test/test_cscaldb.py
| @@ -18,6 +18,7 @@ | @@ -18,6 +18,7 @@ | ||
| 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | # | 19 | # |
| 20 | # ========================================================================== | 20 | # ========================================================================== |
| 21 | +import os | ||
| 21 | import gammalib | 22 | import gammalib |
| 22 | import cscripts | 23 | import cscripts |
| 23 | 24 | ||
| @@ -28,7 +29,11 @@ import cscripts | @@ -28,7 +29,11 @@ import cscripts | ||
| 28 | class Test(gammalib.GPythonTestSuite): | 29 | class Test(gammalib.GPythonTestSuite): |
| 29 | """ | 30 | """ |
| 30 | Test class for cscaldb script. | 31 | Test class for cscaldb script. |
| 32 | + | ||
| 33 | + This test class makes unit tests for the cslightcrv script by using it | ||
| 34 | + from the command line and from Python. | ||
| 31 | """ | 35 | """ |
| 36 | + | ||
| 32 | # Constructor | 37 | # Constructor |
| 33 | def __init__(self): | 38 | def __init__(self): |
| 34 | """ | 39 | """ |
| @@ -49,15 +54,15 @@ class Test(gammalib.GPythonTestSuite): | @@ -49,15 +54,15 @@ class Test(gammalib.GPythonTestSuite): | ||
| 49 | self.name("cscaldb") | 54 | self.name("cscaldb") |
| 50 | 55 | ||
| 51 | # Append tests | 56 | # Append tests |
| 52 | - self.append(self._test_functional, "Test cscaldb functionality") | 57 | + self.append(self._test_python, "Test cscaldb from Python") |
| 53 | 58 | ||
| 54 | # Return | 59 | # Return |
| 55 | return | 60 | return |
| 56 | 61 | ||
| 57 | - # Test cscaldb functionality | ||
| 58 | - def _test_functional(self): | 62 | + # Test cscaldb from Python |
| 63 | + def _test_python(self): | ||
| 59 | """ | 64 | """ |
| 60 | - Test cscaldb functionality. | 65 | + Test cscaldb from Python. |
| 61 | """ | 66 | """ |
| 62 | # Set-up cscaldb | 67 | # Set-up cscaldb |
| 63 | caldb = cscripts.cscaldb() | 68 | caldb = cscripts.cscaldb() |
test/test_cslightcrv.py
0 → 100755
| 1 | +#! /usr/bin/env python | ||
| 2 | +# ========================================================================== | ||
| 3 | +# This scripts performs unit tests for the cslightcrv 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 cslightcrv script # | ||
| 28 | +# ================================ # | ||
| 29 | +class Test(gammalib.GPythonTestSuite): | ||
| 30 | + """ | ||
| 31 | + Test class for cslightcrv script. | ||
| 32 | + | ||
| 33 | + This test class makes unit tests for the cslightcrv 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 | + # Set members | ||
| 46 | + self._events_name = "data/crab_events.fits" | ||
| 47 | + self._model_name = "data/crab.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("cslightcrv") | ||
| 59 | + | ||
| 60 | + # Append tests | ||
| 61 | + self.append(self._test_cmd, "Test cslightcrv on command line") | ||
| 62 | + self.append(self._test_python, "Test cslightcrv from Python") | ||
| 63 | + | ||
| 64 | + # Return | ||
| 65 | + return | ||
| 66 | + | ||
| 67 | + # Test cslightcrv on command line | ||
| 68 | + def _test_cmd(self): | ||
| 69 | + """ | ||
| 70 | + Test cslightcrv on the command line. | ||
| 71 | + """ | ||
| 72 | + # Kluge to set the command (installed version has no README file) | ||
| 73 | + if os.path.isfile("README"): | ||
| 74 | + cslightcrv = "../cscripts/cslightcrv.py" | ||
| 75 | + else: | ||
| 76 | + cslightcrv = "cslightcrv" | ||
| 77 | + | ||
| 78 | + # Setup cslightcrv command | ||
| 79 | + cmd = cslightcrv+' inobs="'+self._events_name+'"'+ \ | ||
| 80 | + ' inmodel="'+self._model_name+'"'+ \ | ||
| 81 | + ' srcname="Crab" caldb="prod2" irf="South_0.5h"'+ \ | ||
| 82 | + ' tbinalg="LIN" tmin=51544.50 tmax=51544.53'+ \ | ||
| 83 | + ' tbins=3 enumbins=0 emin=0.1 emax=100.0'+ \ | ||
| 84 | + ' outfile="lightcurve_cmd1.fits"' | ||
| 85 | + | ||
| 86 | + # Execute cslightcrv, make sure we catch any exception | ||
| 87 | + try: | ||
| 88 | + rc = os.system(cmd+" &>/dev/null") | ||
| 89 | + except: | ||
| 90 | + pass | ||
| 91 | + | ||
| 92 | + # Check if execution was successful | ||
| 93 | + self.test_assert(rc == 0, | ||
| 94 | + "Successful cslightcrv execution on command line") | ||
| 95 | + | ||
| 96 | + # Check light curve | ||
| 97 | + self._check_light_curve("lightcurve_cmd1.fits", 3) | ||
| 98 | + | ||
| 99 | + # Setup cslightcrv command | ||
| 100 | + cmd = cslightcrv+' inobs="events_that_do_not_exist.fits"'+ \ | ||
| 101 | + ' inmodel="'+self._model_name+'"'+ \ | ||
| 102 | + ' srcname="Crab" caldb="prod2" irf="South_0.5h"'+ \ | ||
| 103 | + ' tbinalg="LIN" tmin=51544.50 tmax=51544.53'+ \ | ||
| 104 | + ' tbins=3 enumbins=0 emin=0.1 emax=100.0'+ \ | ||
| 105 | + ' outfile="lightcurve_cmd1.fits"' | ||
| 106 | + | ||
| 107 | + # Execute cslightcrv, make sure we catch any exception | ||
| 108 | + try: | ||
| 109 | + rc = os.system(cmd+" &>/dev/null") | ||
| 110 | + except: | ||
| 111 | + pass | ||
| 112 | + | ||
| 113 | + # Check if execution failed | ||
| 114 | + self.test_assert(rc != 0, | ||
| 115 | + "Failure of cslightcrv execution on command line") | ||
| 116 | + | ||
| 117 | + # Return | ||
| 118 | + return | ||
| 119 | + | ||
| 120 | + # Test cslightcrv from Python | ||
| 121 | + def _test_python(self): | ||
| 122 | + """ | ||
| 123 | + Test cslightcrv from Python. | ||
| 124 | + """ | ||
| 125 | + # Set-up unbinned cslightcrv | ||
| 126 | + lcrv = cscripts.cslightcrv() | ||
| 127 | + lcrv["inobs"] = self._events_name | ||
| 128 | + lcrv["inmodel"] = self._model_name | ||
| 129 | + lcrv["srcname"] = "Crab" | ||
| 130 | + lcrv["caldb"] = "prod2" | ||
| 131 | + lcrv["irf"] = "South_0.5h" | ||
| 132 | + lcrv["tbinalg"] = "LIN" | ||
| 133 | + lcrv["tmin"] = 51544.50 | ||
| 134 | + lcrv["tmax"] = 51544.53 | ||
| 135 | + lcrv["tbins"] = 3 | ||
| 136 | + lcrv["enumbins"] = 0 | ||
| 137 | + lcrv["emin"] = 0.1 | ||
| 138 | + lcrv["emax"] = 100.0 | ||
| 139 | + lcrv["outfile"] = "lightcurve_py1.fits" | ||
| 140 | + | ||
| 141 | + # Run cslightcrv script and save light curve | ||
| 142 | + lcrv.run() | ||
| 143 | + lcrv.save() | ||
| 144 | + | ||
| 145 | + # Check light curve | ||
| 146 | + self._check_light_curve("lightcurve_py1.fits", 3) | ||
| 147 | + | ||
| 148 | + # Now use FILE as time bin algorithm. For this we need first to | ||
| 149 | + # create an ASCII file. We use now 6 time bins. The ASCII file | ||
| 150 | + # is saved into the file "lightcurve_py2.dat". | ||
| 151 | + csv = gammalib.GCsv(2,2) | ||
| 152 | + tmin = 51544.50 | ||
| 153 | + tdelta = 0.01 | ||
| 154 | + for i in range(csv.nrows()): | ||
| 155 | + csv[i,0] = "%.5f" % (tmin + i * tdelta) | ||
| 156 | + csv[i,1] = "%.5f" % (tmin + (i+1) * tdelta) | ||
| 157 | + csv.save("lightcurve_py2.dat", " ", True) | ||
| 158 | + | ||
| 159 | + # Set-up unbinned cslightcrv | ||
| 160 | + lcrv = cscripts.cslightcrv() | ||
| 161 | + lcrv["inobs"] = self._events_name | ||
| 162 | + lcrv["inmodel"] = self._model_name | ||
| 163 | + lcrv["srcname"] = "Crab" | ||
| 164 | + lcrv["caldb"] = "prod2" | ||
| 165 | + lcrv["irf"] = "South_0.5h" | ||
| 166 | + lcrv["tbinalg"] = "FILE" | ||
| 167 | + lcrv["tbinfile"] = "lightcurve_py2.dat" | ||
| 168 | + lcrv["enumbins"] = 0 | ||
| 169 | + lcrv["emin"] = 0.1 | ||
| 170 | + lcrv["emax"] = 100.0 | ||
| 171 | + lcrv["outfile"] = "lightcurve_py2.fits" | ||
| 172 | + | ||
| 173 | + # Execute cslightcrv script | ||
| 174 | + lcrv.execute() | ||
| 175 | + | ||
| 176 | + # Check light curve | ||
| 177 | + self._check_light_curve("lightcurve_py2.fits", 2) | ||
| 178 | + | ||
| 179 | + # Now we setup an observation container on input. We attached the | ||
| 180 | + # model to the observation container so that cslightcrv should | ||
| 181 | + # no longer query for the parameter. | ||
| 182 | + cta = gammalib.GCTAObservation(self._events_name) | ||
| 183 | + obs = gammalib.GObservations() | ||
| 184 | + obs.append(cta) | ||
| 185 | + obs.models(self._model_name) | ||
| 186 | + | ||
| 187 | + # Set-up unbinned cslightcrv from observation container. Now use | ||
| 188 | + # the GTI algorithm so that we test all timing algorithms. | ||
| 189 | + lcrv = cscripts.cslightcrv(obs) | ||
| 190 | + lcrv["srcname"] = "Crab" | ||
| 191 | + lcrv["caldb"] = "prod2" | ||
| 192 | + lcrv["irf"] = "South_0.5h" | ||
| 193 | + lcrv["tbinalg"] = "GTI" | ||
| 194 | + lcrv["enumbins"] = 0 | ||
| 195 | + lcrv["emin"] = 0.1 | ||
| 196 | + lcrv["emax"] = 100.0 | ||
| 197 | + lcrv["outfile"] = "lightcurve_py3.fits" | ||
| 198 | + | ||
| 199 | + # Execute cslightcrv script | ||
| 200 | + lcrv.execute() | ||
| 201 | + | ||
| 202 | + # Check light curve | ||
| 203 | + self._check_light_curve("lightcurve_py3.fits", 1) | ||
| 204 | + | ||
| 205 | + # Finally we set-up a binned cslightcrv | ||
| 206 | + lcrv = cscripts.cslightcrv() | ||
| 207 | + lcrv["inobs"] = self._events_name | ||
| 208 | + lcrv["inmodel"] = self._model_name | ||
| 209 | + lcrv["srcname"] = "Crab" | ||
| 210 | + lcrv["caldb"] = "prod2" | ||
| 211 | + lcrv["irf"] = "South_0.5h" | ||
| 212 | + lcrv["tbinalg"] = "LIN" | ||
| 213 | + lcrv["tmin"] = 51544.50 | ||
| 214 | + lcrv["tmax"] = 51544.53 | ||
| 215 | + lcrv["tbins"] = 2 | ||
| 216 | + lcrv["emin"] = 0.1 | ||
| 217 | + lcrv["emax"] = 100.0 | ||
| 218 | + lcrv["enumbins"] = 10 | ||
| 219 | + lcrv["coordsys"] = "CEL" | ||
| 220 | + lcrv["proj"] = "TAN" | ||
| 221 | + lcrv["xref"] = 83.63 | ||
| 222 | + lcrv["yref"] = 22.01 | ||
| 223 | + lcrv["nxpix"] = 20 | ||
| 224 | + lcrv["nypix"] = 20 | ||
| 225 | + lcrv["binsz"] = 0.02 | ||
| 226 | + lcrv["outfile"] = "lightcurve_py4.fits" | ||
| 227 | + | ||
| 228 | + # Execute cslightcrv script | ||
| 229 | + lcrv.execute() | ||
| 230 | + | ||
| 231 | + # Check light curve | ||
| 232 | + self._check_light_curve("lightcurve_py4.fits", 2) | ||
| 233 | + | ||
| 234 | + # Return | ||
| 235 | + return | ||
| 236 | + | ||
| 237 | + # Check light curve result file | ||
| 238 | + def _check_light_curve(self, filename, bins): | ||
| 239 | + """ | ||
| 240 | + Check light curve file. | ||
| 241 | + """ | ||
| 242 | + # Expected column names | ||
| 243 | + cols = ["MJD", "e_MJD", "Prefactor", "e_Prefactor", | ||
| 244 | + "Index", "e_Index", "TS", "UpperLimit"] | ||
| 245 | + | ||
| 246 | + # Open FITS file | ||
| 247 | + fits = gammalib.GFits(filename) | ||
| 248 | + | ||
| 249 | + # Check FITS file structure | ||
| 250 | + self.test_value(fits.size(), 2, | ||
| 251 | + 'Check for 2 extensions in light curve FITS file') | ||
| 252 | + self.test_assert(fits.contains("LIGHTCURVE"), | ||
| 253 | + 'FITS file contains "LIGHTCURVE" extension') | ||
| 254 | + | ||
| 255 | + # Get LIGHTCURVE table | ||
| 256 | + table = fits["LIGHTCURVE"] | ||
| 257 | + | ||
| 258 | + # Check FITS table structure | ||
| 259 | + self.test_value(table.ncols(), len(cols), | ||
| 260 | + 'Check for %d columns in light curve FITS table' % len(cols)) | ||
| 261 | + self.test_value(table.nrows(), bins, | ||
| 262 | + 'Check for %d rows in light curve FITS table' % bins) | ||
| 263 | + for col in cols: | ||
| 264 | + self.test_assert(table.contains(col), | ||
| 265 | + 'FITS file contains "'+col+'" column') | ||
| 266 | + | ||
| 267 | + # Close FITS file | ||
| 268 | + fits.close() | ||
| 269 | + | ||
| 270 | + # Return | ||
| 271 | + return |
test/test_ctbin.py
| @@ -57,14 +57,14 @@ class Test(gammalib.GPythonTestSuite): | @@ -57,14 +57,14 @@ class Test(gammalib.GPythonTestSuite): | ||
| 57 | self.name("ctbin") | 57 | self.name("ctbin") |
| 58 | 58 | ||
| 59 | # Append tests | 59 | # Append tests |
| 60 | - self.append(self._test_ctbin_cmd, "Test ctbin on command line") | ||
| 61 | - self.append(self._test_ctbin_python, "Test ctbin from Python") | 60 | + self.append(self._test_cmd, "Test ctbin on command line") |
| 61 | + self.append(self._test_python, "Test ctbin from Python") | ||
| 62 | 62 | ||
| 63 | # Return | 63 | # Return |
| 64 | return | 64 | return |
| 65 | 65 | ||
| 66 | # Test ctbin on command line | 66 | # Test ctbin on command line |
| 67 | - def _test_ctbin_cmd(self): | 67 | + def _test_cmd(self): |
| 68 | """ | 68 | """ |
| 69 | Test ctbin on the command line. | 69 | Test ctbin on the command line. |
| 70 | """ | 70 | """ |
| @@ -93,7 +93,7 @@ class Test(gammalib.GPythonTestSuite): | @@ -93,7 +93,7 @@ class Test(gammalib.GPythonTestSuite): | ||
| 93 | 93 | ||
| 94 | # Load counts cube and check content. | 94 | # Load counts cube and check content. |
| 95 | evt = gammalib.GCTAEventCube("cntmap_cmd1.fits") | 95 | evt = gammalib.GCTAEventCube("cntmap_cmd1.fits") |
| 96 | - self._test_cube(evt, 5542) | 96 | + self._check_cube(evt, 5542) |
| 97 | 97 | ||
| 98 | # Setup ctbin command | 98 | # Setup ctbin command |
| 99 | cmd = ctbin+' inobs="events_that_do_not_exist.fits"'+ \ | 99 | cmd = ctbin+' inobs="events_that_do_not_exist.fits"'+ \ |
| @@ -116,7 +116,7 @@ class Test(gammalib.GPythonTestSuite): | @@ -116,7 +116,7 @@ class Test(gammalib.GPythonTestSuite): | ||
| 116 | return | 116 | return |
| 117 | 117 | ||
| 118 | # Test ctbin from Python | 118 | # Test ctbin from Python |
| 119 | - def _test_ctbin_python(self): | 119 | + def _test_python(self): |
| 120 | """ | 120 | """ |
| 121 | Test ctbin from Python. | 121 | Test ctbin from Python. |
| 122 | """ | 122 | """ |
| @@ -140,15 +140,15 @@ class Test(gammalib.GPythonTestSuite): | @@ -140,15 +140,15 @@ class Test(gammalib.GPythonTestSuite): | ||
| 140 | bin.run() | 140 | bin.run() |
| 141 | 141 | ||
| 142 | # Check content of observation and cube | 142 | # Check content of observation and cube |
| 143 | - self._test_observation(bin, 5542) | ||
| 144 | - self._test_cube(bin.cube(), 5542) | 143 | + self._check_observation(bin, 5542) |
| 144 | + self._check_cube(bin.cube(), 5542) | ||
| 145 | 145 | ||
| 146 | # Test copy constructor | 146 | # Test copy constructor |
| 147 | cpy_bin = bin.copy() | 147 | cpy_bin = bin.copy() |
| 148 | 148 | ||
| 149 | # Check content of observation and cube | 149 | # Check content of observation and cube |
| 150 | - self._test_observation(cpy_bin, 5542) | ||
| 151 | - self._test_cube(cpy_bin.cube(), 5542) | 150 | + self._check_observation(cpy_bin, 5542) |
| 151 | + self._check_cube(cpy_bin.cube(), 5542) | ||
| 152 | 152 | ||
| 153 | # Run copy of ctbin tool again | 153 | # Run copy of ctbin tool again |
| 154 | cpy_bin.run() | 154 | cpy_bin.run() |
| @@ -157,8 +157,8 @@ class Test(gammalib.GPythonTestSuite): | @@ -157,8 +157,8 @@ class Test(gammalib.GPythonTestSuite): | ||
| 157 | # event cube as on input the observation is binned, and any binned | 157 | # event cube as on input the observation is binned, and any binned |
| 158 | # observation will be skipped, hence the counts cube should be | 158 | # observation will be skipped, hence the counts cube should be |
| 159 | # empty. | 159 | # empty. |
| 160 | - self._test_observation(cpy_bin, 0) | ||
| 161 | - self._test_cube(cpy_bin.cube(), 0) | 160 | + self._check_observation(cpy_bin, 0) |
| 161 | + self._check_cube(cpy_bin.cube(), 0) | ||
| 162 | 162 | ||
| 163 | # Clear and run copy of ctbin again | 163 | # Clear and run copy of ctbin again |
| 164 | #cpy_bin.clear() | 164 | #cpy_bin.clear() |
| @@ -172,7 +172,7 @@ class Test(gammalib.GPythonTestSuite): | @@ -172,7 +172,7 @@ class Test(gammalib.GPythonTestSuite): | ||
| 172 | 172 | ||
| 173 | # Load counts cube and check content. | 173 | # Load counts cube and check content. |
| 174 | evt = gammalib.GCTAEventCube("cntmap.fits") | 174 | evt = gammalib.GCTAEventCube("cntmap.fits") |
| 175 | - self._test_cube(evt, 5542) | 175 | + self._check_cube(evt, 5542) |
| 176 | 176 | ||
| 177 | # Prepare observation container for stacked analysis | 177 | # Prepare observation container for stacked analysis |
| 178 | cta = gammalib.GCTAObservation(self._events_name) | 178 | cta = gammalib.GCTAObservation(self._events_name) |
| @@ -204,8 +204,8 @@ class Test(gammalib.GPythonTestSuite): | @@ -204,8 +204,8 @@ class Test(gammalib.GPythonTestSuite): | ||
| 204 | 204 | ||
| 205 | # Check content of observation and cube (need multiplier=3 since | 205 | # Check content of observation and cube (need multiplier=3 since |
| 206 | # three identical observations have been appended) | 206 | # three identical observations have been appended) |
| 207 | - self._test_observation(bin, 5542, multiplier=3) | ||
| 208 | - self._test_cube(bin.cube(), 5542, multiplier=3) | 207 | + self._check_observation(bin, 5542, multiplier=3) |
| 208 | + self._check_cube(bin.cube(), 5542, multiplier=3) | ||
| 209 | 209 | ||
| 210 | # Set-up ctbin using an observation container | 210 | # Set-up ctbin using an observation container |
| 211 | bin = ctools.ctbin(obs) | 211 | bin = ctools.ctbin(obs) |
| @@ -227,7 +227,7 @@ class Test(gammalib.GPythonTestSuite): | @@ -227,7 +227,7 @@ class Test(gammalib.GPythonTestSuite): | ||
| 227 | 227 | ||
| 228 | # Load counts cube and check content. | 228 | # Load counts cube and check content. |
| 229 | evt = gammalib.GCTAEventCube("cntmap2.fits") | 229 | evt = gammalib.GCTAEventCube("cntmap2.fits") |
| 230 | - self._test_cube(evt, 5542, multiplier=3) | 230 | + self._check_cube(evt, 5542, multiplier=3) |
| 231 | 231 | ||
| 232 | # Clear tool | 232 | # Clear tool |
| 233 | #bin.clear() | 233 | #bin.clear() |
| @@ -242,9 +242,9 @@ class Test(gammalib.GPythonTestSuite): | @@ -242,9 +242,9 @@ class Test(gammalib.GPythonTestSuite): | ||
| 242 | return | 242 | return |
| 243 | 243 | ||
| 244 | # Check observation | 244 | # Check observation |
| 245 | - def _test_observation(self, ctbin, nevents, multiplier=1): | 245 | + def _check_observation(self, ctbin, nevents, multiplier=1): |
| 246 | """ | 246 | """ |
| 247 | - Test content of an observation. | 247 | + Check content of an observation. |
| 248 | 248 | ||
| 249 | Args: | 249 | Args: |
| 250 | ctbin: ctbin instance | 250 | ctbin: ctbin instance |
| @@ -270,15 +270,15 @@ class Test(gammalib.GPythonTestSuite): | @@ -270,15 +270,15 @@ class Test(gammalib.GPythonTestSuite): | ||
| 270 | "Pointing Declination is 22.01 deg") | 270 | "Pointing Declination is 22.01 deg") |
| 271 | 271 | ||
| 272 | # Test event cube | 272 | # Test event cube |
| 273 | - self._test_cube(obs.events(), nevents, multiplier=multiplier) | 273 | + self._check_cube(obs.events(), nevents, multiplier=multiplier) |
| 274 | 274 | ||
| 275 | # Return | 275 | # Return |
| 276 | return | 276 | return |
| 277 | 277 | ||
| 278 | # Check event cube | 278 | # Check event cube |
| 279 | - def _test_cube(self, cube, nevents, multiplier=1): | 279 | + def _check_cube(self, cube, nevents, multiplier=1): |
| 280 | """ | 280 | """ |
| 281 | - Test content of event cube." | 281 | + Check content of event cube." |
| 282 | 282 | ||
| 283 | Args: | 283 | Args: |
| 284 | cube: Event cube | 284 | cube: Event cube |
test/test_python_cscripts.py
| @@ -18,12 +18,13 @@ | @@ -18,12 +18,13 @@ | ||
| 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | # | 19 | # |
| 20 | # ========================================================================== | 20 | # ========================================================================== |
| 21 | +import os | ||
| 22 | +import sys | ||
| 21 | import gammalib | 23 | import gammalib |
| 22 | import cscripts | 24 | import cscripts |
| 23 | -import sys | ||
| 24 | -import os | ||
| 25 | sys.path.append(os.path.dirname(os.path.abspath(__file__))) | 25 | sys.path.append(os.path.dirname(os.path.abspath(__file__))) |
| 26 | import test_cscaldb | 26 | import test_cscaldb |
| 27 | +import test_cslightcrv | ||
| 27 | 28 | ||
| 28 | 29 | ||
| 29 | # ================== # | 30 | # ================== # |
| @@ -37,13 +38,16 @@ def test(installed=False): | @@ -37,13 +38,16 @@ def test(installed=False): | ||
| 37 | suites = gammalib.GTestSuites("cscripts Python module unit testing") | 38 | suites = gammalib.GTestSuites("cscripts Python module unit testing") |
| 38 | 39 | ||
| 39 | # Allocate test suites and append them to the container | 40 | # Allocate test suites and append them to the container |
| 40 | - suite_cscaldb = test_cscaldb.Test() | 41 | + suite_cscaldb = test_cscaldb.Test() |
| 42 | + suite_cslightcrv = test_cslightcrv.Test() | ||
| 41 | 43 | ||
| 42 | # Setup unit tests | 44 | # Setup unit tests |
| 43 | suite_cscaldb.set() | 45 | suite_cscaldb.set() |
| 46 | + suite_cslightcrv.set() | ||
| 44 | 47 | ||
| 45 | # Append tests to container | 48 | # Append tests to container |
| 46 | suites.append(suite_cscaldb) | 49 | suites.append(suite_cscaldb) |
| 50 | + suites.append(suite_cslightcrv) | ||
| 47 | 51 | ||
| 48 | # If we have an installed version then create a temporary directory and | 52 | # If we have an installed version then create a temporary directory and |
| 49 | # copy over all information that is needed | 53 | # copy over all information that is needed |
| @@ -60,9 +64,13 @@ def test(installed=False): | @@ -60,9 +64,13 @@ def test(installed=False): | ||
| 60 | dirname = os.path.dirname(testdir) | 64 | dirname = os.path.dirname(testdir) |
| 61 | 65 | ||
| 62 | # Copy over test data and irf | 66 | # Copy over test data and irf |
| 63 | - #os.system("cp -r %s %s" % (dirname+"/data", "data")) | 67 | + os.system("cp -r %s %s" % (dirname+"/data", "data")) |
| 64 | #os.system("cp -r %s %s" % (dirname+"/irf", "irf")) | 68 | #os.system("cp -r %s %s" % (dirname+"/irf", "irf")) |
| 65 | 69 | ||
| 70 | + # ... otherwise set the calibration database | ||
| 71 | + else: | ||
| 72 | + os.environ['CALDB'] = "../caldb" | ||
| 73 | + | ||
| 66 | # Set PFILES environment variable | 74 | # Set PFILES environment variable |
| 67 | try: | 75 | try: |
| 68 | os.mkdir("pfiles") | 76 | os.mkdir("pfiles") |
| @@ -97,8 +105,6 @@ def test(installed=False): | @@ -97,8 +105,6 @@ def test(installed=False): | ||
| 97 | # Main routine entry point # | 105 | # Main routine entry point # |
| 98 | # ======================== # | 106 | # ======================== # |
| 99 | if __name__ == '__main__': | 107 | if __name__ == '__main__': |
| 100 | - """ | ||
| 101 | - Perform unit testing for cscripts used from Python. | ||
| 102 | - """ | 108 | + |
| 103 | # Run tests | 109 | # Run tests |
| 104 | test() | 110 | test() |