Commit 6543a1568ffdcbebbcf12c2d79c296212a107691
1 parent
b6620a4a
Add cscript unit tests
Showing
13 changed files
with
1020 additions
and
84 deletions
ChangeLog
| 1 | -2016-04-25 Juergen Knoedlseder <jurgen.knodlseder@irap.omp.eu> | |
| 1 | +2016-05-27 Juergen Knoedlseder <jurgen.knodlseder@irap.omp.eu> | |
| 2 | 2 | |
| 3 | 3 | * Version 1.1.0 released |
| 4 | 4 | ======================== |
| 5 | 5 | |
| 6 | + Add cscript unit tests. | |
| 6 | 7 | Add "bgdoversample" and "bgdinfill" parameters to csroot2caldb |
| 7 | 8 | Add csobsdef.pntdef() method (#1771) |
| 8 | 9 | Add ctmapcube tool (#1760) | ... | ... |
NEWS
| 1 | 1 | New Features and Important Changes in ctools 1.1.0 |
| 2 | 2 | |
| 3 | -25 April 2016 | |
| 3 | +27 May 2016 | |
| 4 | 4 | |
| 5 | 5 | |
| 6 | 6 | Introduction |
| ... | ... | @@ -208,6 +208,8 @@ sampling than the spatial sampling in the ROOT file. |
| 208 | 208 | Add csobsdef.pntdef() method to set directly a pointing definition from a |
| 209 | 209 | gammalib.GCsv object (#1771). |
| 210 | 210 | |
| 211 | +Unit tests have been added for all cscripts. | |
| 212 | + | |
| 211 | 213 | |
| 212 | 214 | Examples |
| 213 | 215 | -------- | ... | ... |
cscripts/Makefile.am
| ... | ... | @@ -71,14 +71,19 @@ linkscripts = cscaldb \ |
| 71 | 71 | testscripts = tests/__init__.py \ |
| 72 | 72 | ../test/test_python_cscripts.py \ |
| 73 | 73 | ../test/test_cscaldb.py \ |
| 74 | - ../test/test_cslightcrv.py \ | |
| 75 | 74 | ../test/test_csmodelinfo.py \ |
| 76 | 75 | ../test/test_csmodelmerge.py \ |
| 77 | 76 | ../test/test_csobs2caldb.py \ |
| 78 | 77 | ../test/test_csobsdef.py \ |
| 79 | 78 | ../test/test_csobsinfo.py \ |
| 79 | + ../test/test_cslightcrv.py \ | |
| 80 | 80 | ../test/test_cspull.py \ |
| 81 | + ../test/test_csresmap.py \ | |
| 81 | 82 | ../test/test_csroot2caldb.py \ |
| 83 | + ../test/test_cssens.py \ | |
| 84 | + ../test/test_csspec.py \ | |
| 85 | + ../test/test_cstsdist.py \ | |
| 86 | + ../test/test_cstsmapmerge.py \ | |
| 82 | 87 | ../test/test_csworkflow.py \ |
| 83 | 88 | ../test/test_csfindobs.py \ |
| 84 | 89 | ../test/test_csiactcopy.py \ |
| ... | ... | @@ -91,12 +96,17 @@ testdata = ../test/data/crab.xml \ |
| 91 | 96 | ../test/data/obs_stacked.xml \ |
| 92 | 97 | ../test/data/crab_events.fits.gz \ |
| 93 | 98 | ../test/data/crab_cntmap.fits.gz \ |
| 99 | + ../test/data/crab_cntmap_small.fits.gz \ | |
| 94 | 100 | ../test/data/crab_expcube.fits.gz \ |
| 95 | 101 | ../test/data/crab_psfcube.fits.gz \ |
| 102 | + ../test/data/crab_edispcube.fits.gz \ | |
| 96 | 103 | ../test/data/crab_bkgcube.fits.gz \ |
| 104 | + ../test/data/tsmap_0.fits \ | |
| 105 | + ../test/data/tsmap_1.fits \ | |
| 97 | 106 | ../test/data/exclusion.reg \ |
| 98 | 107 | ../test/data/pntdef.dat \ |
| 99 | 108 | ../test/data/models.txt \ |
| 109 | + ../test/data/tsmaps.txt \ | |
| 100 | 110 | ../test/data/model_cube_background.xml \ |
| 101 | 111 | ../test/data/model_cube_background2.xml \ |
| 102 | 112 | ../test/data/crab_bkgcube.xml \ | ... | ... |
cscripts/csspec.py
| ... | ... | @@ -42,8 +42,8 @@ class csspec(ctools.cscript): |
| 42 | 42 | Constructor. |
| 43 | 43 | """ |
| 44 | 44 | # Set name |
| 45 | - self._name = "csspec" | |
| 46 | - self._version = "1.1.0" | |
| 45 | + self._name = 'csspec' | |
| 46 | + self._version = '1.1.0' | |
| 47 | 47 | |
| 48 | 48 | # Initialise some members |
| 49 | 49 | self._ebounds = gammalib.GEbounds() |
| ... | ... | @@ -136,56 +136,58 @@ class csspec(ctools.cscript): |
| 136 | 136 | """ |
| 137 | 137 | # Get ebounds |
| 138 | 138 | if self._binned_mode: |
| 139 | - | |
| 139 | + | |
| 140 | + # Initialise energy boundaries for spectrum | |
| 141 | + self._ebounds = gammalib.GEbounds() | |
| 142 | + | |
| 143 | + # Create energy boundaries according to user parameters | |
| 144 | + ebounds = self._create_ebounds() | |
| 145 | + | |
| 140 | 146 | # Extract cube ebounds |
| 141 | 147 | cube_ebounds = self._obs[0].events().ebounds() |
| 142 | - | |
| 143 | - # Read user parameters | |
| 144 | - enumbins = self["enumbins"].integer() | |
| 145 | - emin_val = self["emin"].real() - 1e-6 # Rounding tolerance | |
| 146 | - emax_val = self["emax"].real() + 1e-6 # Rounding tolerance | |
| 147 | - | |
| 148 | - # Set energy range | |
| 149 | - emin = gammalib.GEnergy(emin_val, "TeV") | |
| 150 | - emax = gammalib.GEnergy(emax_val, "TeV") | |
| 151 | - | |
| 152 | - # Determine all cube layers that overlap with the [emin,emax] | |
| 153 | - # energy range. | |
| 154 | - use_layers = [] | |
| 155 | - for i in range(cube_ebounds.size()): | |
| 156 | - if cube_ebounds.emin(i) < emax and cube_ebounds.emax(i) > emin: | |
| 157 | - use_layers.append(i) | |
| 158 | - | |
| 159 | - # If no layers overlap then throw an exception | |
| 160 | - if (len(use_layers) == 0): | |
| 161 | - msg = "Energy range ["+str(cube_ebounds.emin())+ \ | |
| 162 | - ", "+str(cube_ebounds.emax())+"] of counts "+ \ | |
| 163 | - "cube does not overlap with specified energy "+ \ | |
| 164 | - "range ["+ \ | |
| 165 | - str(emin)+", "+str(emax)+"]. Specify "+ \ | |
| 166 | - "overlapping energy range." | |
| 167 | - raise RuntimeError(msg) | |
| 168 | - | |
| 169 | - # Determine number of layers to use for each spectral bin | |
| 170 | - n_layers = int(len(use_layers) / enumbins) | |
| 171 | 148 | |
| 172 | - # Don't allow zero layers | |
| 173 | - if n_layers == 0: | |
| 174 | - n_layers = 1 | |
| 149 | + # Loop over user energy boundaries and collect all layers | |
| 150 | + # that overlap | |
| 151 | + for i in range(ebounds.size()): | |
| 152 | + | |
| 153 | + # Extract minimum and maximum energy of user energy bin, | |
| 154 | + # including some rounding tolerance | |
| 155 | + emin = ebounds.emin(i).TeV() - 1e-6 # Rounding tolerance | |
| 156 | + emax = ebounds.emax(i).TeV() + 1e-6 # Rounding tolerance | |
| 157 | + | |
| 158 | + # Set number of overlapping energy bins | |
| 159 | + nbins = 0 | |
| 160 | + | |
| 161 | + # Search first cube bin that is comprised within user energy bin | |
| 162 | + for k in range(cube_ebounds.size()): | |
| 163 | + if cube_ebounds.emin(k).TeV() >= emin and \ | |
| 164 | + cube_ebounds.emax(k).TeV() <= emax: | |
| 165 | + emin_value = cube_ebounds.emin(k).TeV() | |
| 166 | + break | |
| 167 | + | |
| 168 | + # Search last cube bin that is comprised within user energy bin | |
| 169 | + for k in range(cube_ebounds.size()): | |
| 170 | + if cube_ebounds.emin(k).TeV() >= emin and \ | |
| 171 | + cube_ebounds.emax(k).TeV() <= emax: | |
| 172 | + emax_value = cube_ebounds.emax(k).TeV() | |
| 173 | + nbins += 1 | |
| 174 | + | |
| 175 | + # Append energy bin if there are overlapping bins in the | |
| 176 | + # counts cube | |
| 177 | + if nbins > 0: | |
| 178 | + self._ebounds.append(gammalib.GEnergy(emin_value, 'TeV'), | |
| 179 | + gammalib.GEnergy(emax_value, 'TeV')) | |
| 180 | + | |
| 181 | + # Raise an exception if there are no overlapping layers | |
| 182 | + if (len(self._ebounds) == 0): | |
| 183 | + msg = 'Energy range ['+str(cube_ebounds.emin())+ \ | |
| 184 | + ', '+str(cube_ebounds.emax())+'] of counts '+ \ | |
| 185 | + 'cube does not overlap with specified energy '+ \ | |
| 186 | + 'range ['+ \ | |
| 187 | + str(ebounds.emin())+', '+str(ebounds.emax())+'].'+ \ | |
| 188 | + ' Specify overlapping energy range.' | |
| 189 | + raise RuntimeError(msg) | |
| 175 | 190 | |
| 176 | - # Create energy boundaries | |
| 177 | - self._ebounds = gammalib.GEbounds() | |
| 178 | - i_start = 0 | |
| 179 | - i_stop = n_layers - 1 | |
| 180 | - while i_start < len(use_layers): | |
| 181 | - if i_stop >= len(use_layers): | |
| 182 | - i_stop = len(use_layers) - 1 | |
| 183 | - ebinmin = cube_ebounds.emin(use_layers[i_start]) | |
| 184 | - ebinmax = cube_ebounds.emax(use_layers[i_stop]) | |
| 185 | - self._ebounds.append(ebinmin, ebinmax) | |
| 186 | - i_start += n_layers | |
| 187 | - i_stop += n_layers | |
| 188 | - | |
| 189 | 191 | # Unbinned mode |
| 190 | 192 | else: |
| 191 | 193 | self._ebounds = self._create_ebounds() | ... | ... |
cscripts/cstsmapmerge.py
| ... | ... | @@ -39,12 +39,12 @@ class cstsmapmerge(ctools.cscript): |
| 39 | 39 | Constructor. |
| 40 | 40 | """ |
| 41 | 41 | # Set name |
| 42 | - self._name = "cstsmapmerge" | |
| 43 | - self._version = "1.1.0" | |
| 42 | + self._name = 'cstsmapmerge' | |
| 43 | + self._version = '1.1.0' | |
| 44 | 44 | |
| 45 | 45 | # Initialise class members |
| 46 | 46 | self._files = None |
| 47 | - self._in_filename = "" | |
| 47 | + self._in_filename = '' | |
| 48 | 48 | self._tsmap = gammalib.GSkyMap() # Empty sky map |
| 49 | 49 | self._statusmap = gammalib.GSkyMap() # Empty sky map |
| 50 | 50 | self._maps = [] |
| ... | ... | @@ -67,49 +67,49 @@ class cstsmapmerge(ctools.cscript): |
| 67 | 67 | Get parameters from parfile. |
| 68 | 68 | """ |
| 69 | 69 | # Get input maps string |
| 70 | - inmaps = self["inmaps"].string() | |
| 70 | + inmaps = self['inmaps'].string() | |
| 71 | 71 | |
| 72 | 72 | # Handle ascii files |
| 73 | - if "@" == inmaps[0]: | |
| 74 | - self._files = open(inmaps.replace("@","")).read().splitlines() | |
| 73 | + if '@' == inmaps[0]: | |
| 74 | + self._files = open(inmaps.replace('@','')).read().splitlines() | |
| 75 | 75 | |
| 76 | 76 | # Handle wild card strings |
| 77 | - elif "*" in inmaps: | |
| 77 | + elif '*' in inmaps: | |
| 78 | 78 | self._files = glob.glob(inmaps) |
| 79 | 79 | |
| 80 | 80 | # Handle space-separated list |
| 81 | - elif " " in inmaps: | |
| 82 | - self._files = inmaps.split(" ") | |
| 81 | + elif ' ' in inmaps: | |
| 82 | + self._files = inmaps.split(' ') | |
| 83 | 83 | |
| 84 | 84 | # Handle semi-colon separated list |
| 85 | - elif ";" in inmaps: | |
| 86 | - self._files = inmaps.split(";") | |
| 85 | + elif ';' in inmaps: | |
| 86 | + self._files = inmaps.split(';') | |
| 87 | 87 | |
| 88 | 88 | # Throw exception if input models cannot be decoded |
| 89 | 89 | else: |
| 90 | - msg = "Parameter \"inmaps\" must contain either an @ASCII "\ | |
| 91 | - "file, a semi-colon-separated or whitespace-separated "\ | |
| 92 | - "list of files or a wildcard string." | |
| 90 | + msg = 'Parameter "inmaps" must contain either an @ASCII '\ | |
| 91 | + 'file, a semi-colon-separated or whitespace-separated '\ | |
| 92 | + 'list of files or a wildcard string.' | |
| 93 | 93 | raise RuntimeError(msg) |
| 94 | 94 | |
| 95 | 95 | # Check number of files. We need at least two files. |
| 96 | 96 | if len(self._files) <= 1: |
| 97 | - msg = "Need at least two files to start merging, "+\ | |
| 98 | - str(len(self._files))+" file(s) given." | |
| 97 | + msg = 'Need at least two files to start merging, '+\ | |
| 98 | + str(len(self._files))+' file(s) given.' | |
| 99 | 99 | raise RuntimeError(msg) |
| 100 | 100 | |
| 101 | 101 | # Get other parameters |
| 102 | - self._overwrite = self["overwrite"].boolean() | |
| 103 | - self._delete = self["delete"].boolean() | |
| 102 | + self._overwrite = self['overwrite'].boolean() | |
| 103 | + self._delete = self['delete'].boolean() | |
| 104 | 104 | |
| 105 | 105 | # Read ahead output filename |
| 106 | 106 | if self._read_ahead(): |
| 107 | - self["outmap"].filename() | |
| 107 | + self['outmap'].filename() | |
| 108 | 108 | |
| 109 | 109 | # Write input parameters into logger |
| 110 | 110 | if self._logTerse(): |
| 111 | 111 | self._log_parameters() |
| 112 | - self._log("\n") | |
| 112 | + self._log('\n') | |
| 113 | 113 | |
| 114 | 114 | # Return |
| 115 | 115 | return | ... | ... |
test/data/tsmaps.txt
0 → 100644
test/test_cscripts.sh.in
| ... | ... | @@ -257,9 +257,9 @@ else |
| 257 | 257 | exit 1 |
| 258 | 258 | fi |
| 259 | 259 | # === ascii-file === |
| 260 | -ls data/tsmap_*.fits > tsmaps.txt | |
| 260 | +#ls data/tsmap_*.fits > tsmaps.txt | |
| 261 | 261 | @PYTHON_COVERAGE@@abs_top_srcdir@/cscripts/cstsmapmerge.py \ |
| 262 | - inmaps="@tsmaps.txt" \ | |
| 262 | + inmaps="@data/tsmaps.txt" \ | |
| 263 | 263 | outmap="merged_tsmap4.fits" > cstsmapmerge.log |
| 264 | 264 | $ECHO -n "." |
| 265 | 265 | if [ -s "merged_tsmap4.fits" ] | ... | ... |
test/test_csresmap.py
0 → 100755
| 1 | +#! /usr/bin/env python | |
| 2 | +# ========================================================================== | |
| 3 | +# This scripts performs unit tests for the csresmap 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 csresmap script # | |
| 28 | +# ============================== # | |
| 29 | +class Test(gammalib.GPythonTestSuite): | |
| 30 | + """ | |
| 31 | + Test class for csresmap script. | |
| 32 | + | |
| 33 | + This test class makes unit tests for the csresmap 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('csresmap') | |
| 55 | + | |
| 56 | + # Append tests | |
| 57 | + self.append(self._test_cmd, 'Test csresmap on command line') | |
| 58 | + self.append(self._test_python, 'Test csresmap from Python') | |
| 59 | + | |
| 60 | + # Return | |
| 61 | + return | |
| 62 | + | |
| 63 | + # Test csresmap on command line | |
| 64 | + def _test_cmd(self): | |
| 65 | + """ | |
| 66 | + Test csresmap on the command line. | |
| 67 | + """ | |
| 68 | + # Kluge to set the command (installed version has no README file) | |
| 69 | + if os.path.isfile('README'): | |
| 70 | + csresmap = '../cscripts/csresmap.py' | |
| 71 | + else: | |
| 72 | + csresmap = 'csresmap' | |
| 73 | + | |
| 74 | + # Setup csresmap command | |
| 75 | + cmd = csresmap+' inobs="data/crab_events.fits"'+ \ | |
| 76 | + ' outmap="csresmap_cmd1.fits"'+ \ | |
| 77 | + ' inmodel="data/crab.xml"'+ \ | |
| 78 | + ' caldb="prod2" irf="South_0.5h"' + \ | |
| 79 | + ' nxpix=50 nypix=50 binsz=0.02'+ \ | |
| 80 | + ' coordsys="CEL" proj="CAR" xref=83.63 yref=22.01'+ \ | |
| 81 | + ' algorithm="SUBDIV"'+ \ | |
| 82 | + ' logfile="csresmap_cmd1.log" chatter=1' | |
| 83 | + | |
| 84 | + # Execute csresmap, make sure we catch any exception | |
| 85 | + try: | |
| 86 | + rc = os.system(cmd+' >/dev/null 2>&1') | |
| 87 | + except: | |
| 88 | + pass | |
| 89 | + | |
| 90 | + # Check if execution was successful | |
| 91 | + self.test_assert(rc == 0, | |
| 92 | + 'Successful csresmap execution on command line') | |
| 93 | + | |
| 94 | + # Check result file | |
| 95 | + self._check_result_file('csresmap_cmd1.fits') | |
| 96 | + | |
| 97 | + # Setup csresmap command | |
| 98 | + cmd = csresmap+' inobs="event_file_that_does_not_exist.fits"'+ \ | |
| 99 | + ' outmap="csresmap_cmd1.fits"'+ \ | |
| 100 | + ' inmodel="data/crab.xml"'+ \ | |
| 101 | + ' caldb="prod2" irf="South_0.5h"' + \ | |
| 102 | + ' nxpix=50 nypix=50 binsz=0.02'+ \ | |
| 103 | + ' coordsys="CEL" proj="CAR" xref=83.63 yref=22.01'+ \ | |
| 104 | + ' algorithm="SUBDIV"'+ \ | |
| 105 | + ' logfile="csresmap_cmd2.log" chatter=2' | |
| 106 | + | |
| 107 | + # Execute csresmap, make sure we catch any exception | |
| 108 | + try: | |
| 109 | + rc = os.system(cmd+' >/dev/null 2>&1') | |
| 110 | + except: | |
| 111 | + pass | |
| 112 | + | |
| 113 | + # Check if execution failed | |
| 114 | + self.test_assert(rc != 0, | |
| 115 | + 'Failure of csresmap execution on command line') | |
| 116 | + | |
| 117 | + # Return | |
| 118 | + return | |
| 119 | + | |
| 120 | + # Test csresmap from Python | |
| 121 | + def _test_python(self): | |
| 122 | + """ | |
| 123 | + Test csresmap from Python. | |
| 124 | + """ | |
| 125 | + # Set-up csresmap for event list | |
| 126 | + resmap = cscripts.csresmap() | |
| 127 | + resmap['inobs'] = 'data/crab_events.fits' | |
| 128 | + resmap['outmap'] = 'csresmap_py1.fits' | |
| 129 | + resmap['inmodel'] = 'data/crab.xml' | |
| 130 | + resmap['caldb'] = 'prod2' | |
| 131 | + resmap['irf'] = 'South_0.5h' | |
| 132 | + resmap['nxpix'] = 50 | |
| 133 | + resmap['nypix'] = 50 | |
| 134 | + resmap['binsz'] = 0.02 | |
| 135 | + resmap['coordsys'] = 'CEL' | |
| 136 | + resmap['proj'] = 'CAR' | |
| 137 | + resmap['xref'] = 83.63 | |
| 138 | + resmap['yref'] = 22.01 | |
| 139 | + resmap['algorithm'] = 'SUBDIV' | |
| 140 | + resmap['logfile'] = 'csresmap_py1.log' | |
| 141 | + resmap['chatter'] = 2 | |
| 142 | + | |
| 143 | + # Run csresmap script | |
| 144 | + resmap.logFileOpen() # Make sure we get a log file | |
| 145 | + resmap.run() | |
| 146 | + resmap.save() | |
| 147 | + | |
| 148 | + # Check pull distribution file | |
| 149 | + self._check_result_file('csresmap_py1.fits') | |
| 150 | + | |
| 151 | + # Set-up csresmap for counts cube | |
| 152 | + resmap = cscripts.csresmap() | |
| 153 | + resmap['inobs'] = 'data/crab_cntmap.fits' | |
| 154 | + resmap['modcube'] = 'NONE' | |
| 155 | + resmap['expcube'] = 'NONE' | |
| 156 | + resmap['psfcube'] = 'NONE' | |
| 157 | + resmap['edispcube'] = 'NONE' | |
| 158 | + resmap['bkgcube'] = 'NONE' | |
| 159 | + resmap['caldb'] = 'prod2' | |
| 160 | + resmap['irf'] = 'South_0.5h' | |
| 161 | + resmap['inmodel'] = 'data/crab.xml' | |
| 162 | + resmap['outmap'] = 'csresmap_py2.fits' | |
| 163 | + resmap['algorithm'] = 'SUBDIV' | |
| 164 | + resmap['logfile'] = 'csresmap_py2.log' | |
| 165 | + resmap['chatter'] = 3 | |
| 166 | + | |
| 167 | + # Run csresmap script | |
| 168 | + resmap.execute() | |
| 169 | + | |
| 170 | + # Check pull distribution file | |
| 171 | + self._check_result_file('csresmap_py2.fits', nx=200, ny=200) | |
| 172 | + | |
| 173 | + # Return | |
| 174 | + return | |
| 175 | + | |
| 176 | + # Check result file | |
| 177 | + def _check_result_file(self, filename, nx=50, ny=50): | |
| 178 | + """ | |
| 179 | + Check result file. | |
| 180 | + """ | |
| 181 | + # Load residual map | |
| 182 | + residual = gammalib.GSkyMap(filename) | |
| 183 | + | |
| 184 | + # Check residual map | |
| 185 | + self.test_value(residual.nmaps(), 1, 'One map') | |
| 186 | + self.test_value(residual.nx(), nx, '%d pixels in X' % nx) | |
| 187 | + self.test_value(residual.ny(), ny, '%d pixels in Y' % ny) | |
| 188 | + | |
| 189 | + # Return | |
| 190 | + return | ... | ... |
test/test_cssens.py
0 → 100755
| 1 | +#! /usr/bin/env python | |
| 2 | +# ========================================================================== | |
| 3 | +# This scripts performs unit tests for the cssens 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 cssens script # | |
| 28 | +# ============================ # | |
| 29 | +class Test(gammalib.GPythonTestSuite): | |
| 30 | + """ | |
| 31 | + Test class for cssens script. | |
| 32 | + | |
| 33 | + This test class makes unit tests for the cssens 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('cssens') | |
| 55 | + | |
| 56 | + # Append tests | |
| 57 | + self.append(self._test_cmd, 'Test cssens on command line') | |
| 58 | + self.append(self._test_python, 'Test cssens from Python') | |
| 59 | + | |
| 60 | + # Return | |
| 61 | + return | |
| 62 | + | |
| 63 | + # Test cssens on command line | |
| 64 | + def _test_cmd(self): | |
| 65 | + """ | |
| 66 | + Test cssens on the command line. | |
| 67 | + """ | |
| 68 | + # Kluge to set the command (installed version has no README file) | |
| 69 | + if os.path.isfile('README'): | |
| 70 | + cssens = '../cscripts/cssens.py' | |
| 71 | + else: | |
| 72 | + cssens = 'cssens' | |
| 73 | + | |
| 74 | + # Setup cssens command | |
| 75 | + cmd = cssens+' inmodel="data/crab.xml"'+ \ | |
| 76 | + ' srcname="Crab" caldb="prod2" irf="South_0.5h"' + \ | |
| 77 | + ' duration=1800.0 rad=3.0 emin=1.0 emax=10.0 bins=1'+ \ | |
| 78 | + ' outfile="cssens_cmd1.dat"'+ \ | |
| 79 | + ' logfile="cssens_cmd1.log" chatter=1' | |
| 80 | + | |
| 81 | + # Execute cssens, make sure we catch any exception | |
| 82 | + try: | |
| 83 | + rc = os.system(cmd+' >/dev/null 2>&1') | |
| 84 | + except: | |
| 85 | + pass | |
| 86 | + | |
| 87 | + # Check if execution was successful | |
| 88 | + self.test_assert(rc == 0, | |
| 89 | + 'Successful cssens execution on command line') | |
| 90 | + | |
| 91 | + # Check result file | |
| 92 | + self._check_result_file('cssens_cmd1.dat') | |
| 93 | + | |
| 94 | + # Setup cssens command | |
| 95 | + cmd = cssens+' inmodel="model_that_does_not_exist.xml"'+ \ | |
| 96 | + ' srcname="Crab" caldb="prod2" irf="South_0.5h"' + \ | |
| 97 | + ' duration=1800.0 rad=3.0 emin=1.0 emax=10.0 bins=1'+ \ | |
| 98 | + ' outfile="cssens_cmd2.dat"'+ \ | |
| 99 | + ' logfile="cssens_cmd2.log" chatter=2' | |
| 100 | + | |
| 101 | + # Execute cssens, make sure we catch any exception | |
| 102 | + try: | |
| 103 | + rc = os.system(cmd+' >/dev/null 2>&1') | |
| 104 | + except: | |
| 105 | + pass | |
| 106 | + | |
| 107 | + # Check if execution failed | |
| 108 | + self.test_assert(rc != 0, | |
| 109 | + 'Failure of cssens execution on command line') | |
| 110 | + | |
| 111 | + # Return | |
| 112 | + return | |
| 113 | + | |
| 114 | + # Test cssens from Python | |
| 115 | + def _test_python(self): | |
| 116 | + """ | |
| 117 | + Test cssens from Python. | |
| 118 | + """ | |
| 119 | + # Set-up cssens | |
| 120 | + sens = cscripts.cssens() | |
| 121 | + sens['inobs'] = 'NONE' | |
| 122 | + sens['inmodel'] = 'data/crab.xml' | |
| 123 | + sens['srcname'] = 'Crab' | |
| 124 | + sens['caldb'] = 'prod2' | |
| 125 | + sens['irf'] = 'South_0.5h' | |
| 126 | + sens['outfile'] = 'cssens_py1.dat' | |
| 127 | + sens['duration'] = 1800.0 | |
| 128 | + sens['rad'] = 3.0 | |
| 129 | + sens['emin'] = 1.0 | |
| 130 | + sens['emax'] = 10.0 | |
| 131 | + sens['bins'] = 1 | |
| 132 | + sens['logfile'] = 'cssens_py1.log' | |
| 133 | + sens['chatter'] = 2 | |
| 134 | + | |
| 135 | + # Run cssens script | |
| 136 | + sens.logFileOpen() # Make sure we get a log file | |
| 137 | + sens.run() | |
| 138 | + sens.save() | |
| 139 | + | |
| 140 | + # Check pull distribution file | |
| 141 | + self._check_result_file('cssens_py1.dat') | |
| 142 | + | |
| 143 | + # Return | |
| 144 | + return | |
| 145 | + | |
| 146 | + # Check result file | |
| 147 | + def _check_result_file(self, filename): | |
| 148 | + """ | |
| 149 | + Check result file. | |
| 150 | + """ | |
| 151 | + # Open result file as CSV file | |
| 152 | + results = gammalib.GCsv(filename, ',') | |
| 153 | + | |
| 154 | + # Check dimensions | |
| 155 | + self.test_value(results.nrows(), 2, 'Check for 2 rows in sensitivity file') | |
| 156 | + self.test_value(results.ncols(), 7, 'Check for 7 columns in sensitivity file') | |
| 157 | + | |
| 158 | + # Return | |
| 159 | + return | ... | ... |
test/test_csspec.py
0 → 100755
| 1 | +#! /usr/bin/env python | |
| 2 | +# ========================================================================== | |
| 3 | +# This scripts performs unit tests for the csspec 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 csspec script # | |
| 28 | +# ============================ # | |
| 29 | +class Test(gammalib.GPythonTestSuite): | |
| 30 | + """ | |
| 31 | + Test class for csspec script. | |
| 32 | + | |
| 33 | + This test class makes unit tests for the csspec 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('csspec') | |
| 55 | + | |
| 56 | + # Append tests | |
| 57 | + self.append(self._test_cmd, 'Test csspec on command line') | |
| 58 | + self.append(self._test_python, 'Test csspec from Python') | |
| 59 | + | |
| 60 | + # Return | |
| 61 | + return | |
| 62 | + | |
| 63 | + # Test csspec on command line | |
| 64 | + def _test_cmd(self): | |
| 65 | + """ | |
| 66 | + Test csspec on the command line. | |
| 67 | + """ | |
| 68 | + # Kluge to set the command (installed version has no README file) | |
| 69 | + if os.path.isfile('README'): | |
| 70 | + csspec = '../cscripts/csspec.py' | |
| 71 | + else: | |
| 72 | + csspec = 'csspec' | |
| 73 | + | |
| 74 | + # Setup csspec command | |
| 75 | + cmd = csspec+' inobs="data/crab_events.fits"'+ \ | |
| 76 | + ' inmodel="data/crab.xml"'+ \ | |
| 77 | + ' srcname="Crab" caldb="prod2" irf="South_0.5h"' + \ | |
| 78 | + ' ebinalg="LOG" enumbins=5 emin=0.1 emax=100.0'+ \ | |
| 79 | + ' outfile="csspec_cmd1.fits"'+ \ | |
| 80 | + ' logfile="csspec_cmd1.log" chatter=1' | |
| 81 | + | |
| 82 | + # Execute csspec, make sure we catch any exception | |
| 83 | + try: | |
| 84 | + rc = os.system(cmd+' >/dev/null 2>&1') | |
| 85 | + except: | |
| 86 | + pass | |
| 87 | + | |
| 88 | + # Check if execution was successful | |
| 89 | + self.test_assert(rc == 0, | |
| 90 | + 'Successful csspec execution on command line') | |
| 91 | + | |
| 92 | + # Check result file | |
| 93 | + self._check_result_file('csspec_cmd1.fits', 5) | |
| 94 | + | |
| 95 | + # Setup csspec command | |
| 96 | + cmd = csspec+' inobs="input_file_that_does_not_exist.fits"'+ \ | |
| 97 | + ' inmodel="data/crab.xml"'+ \ | |
| 98 | + ' srcname="Crab" caldb="prod2" irf="South_0.5h"' + \ | |
| 99 | + ' ebinalg="LOG" enumbins=5 emin=0.1 emax=100.0'+ \ | |
| 100 | + ' outfile="csspec_cmd2.fits"'+ \ | |
| 101 | + ' logfile="csspec_cmd2.log" chatter=1' | |
| 102 | + | |
| 103 | + # Execute csspec, make sure we catch any exception | |
| 104 | + try: | |
| 105 | + rc = os.system(cmd+' >/dev/null 2>&1') | |
| 106 | + except: | |
| 107 | + pass | |
| 108 | + | |
| 109 | + # Check if execution failed | |
| 110 | + self.test_assert(rc != 0, | |
| 111 | + 'Failure of csspec execution on command line') | |
| 112 | + | |
| 113 | + # Return | |
| 114 | + return | |
| 115 | + | |
| 116 | + # Test csspec from Python | |
| 117 | + def _test_python(self): | |
| 118 | + """ | |
| 119 | + Test csspec from Python. | |
| 120 | + """ | |
| 121 | + # Set-up unbinned csspec | |
| 122 | + spec = cscripts.csspec() | |
| 123 | + spec['inobs'] = 'data/crab_events.fits' | |
| 124 | + spec['inmodel'] = 'data/crab.xml' | |
| 125 | + spec['srcname'] = 'Crab' | |
| 126 | + spec['caldb'] = 'prod2' | |
| 127 | + spec['irf'] = 'South_0.5h' | |
| 128 | + spec['outfile'] = 'csspec_py1.fits' | |
| 129 | + spec['ebinalg'] = 'LOG' | |
| 130 | + spec['enumbins'] = 5 | |
| 131 | + spec['emin'] = 0.1 | |
| 132 | + spec['emax'] = 100.0 | |
| 133 | + spec['logfile'] = 'csspec_py1.log' | |
| 134 | + spec['chatter'] = 2 | |
| 135 | + | |
| 136 | + # Run csspec script | |
| 137 | + spec.logFileOpen() # Make sure we get a log file | |
| 138 | + spec.run() | |
| 139 | + spec.save() | |
| 140 | + | |
| 141 | + # Check pull distribution file | |
| 142 | + self._check_result_file('csspec_py1.fits', 5) | |
| 143 | + | |
| 144 | + # Set-up binned csspec | |
| 145 | + spec = cscripts.csspec() | |
| 146 | + spec['inobs'] = 'data/crab_cntmap_small.fits' | |
| 147 | + spec['expcube'] = 'data/crab_expcube.fits' | |
| 148 | + spec['psfcube'] = 'data/crab_psfcube.fits' | |
| 149 | + spec['edispcube'] = 'data/crab_edispcube.fits' | |
| 150 | + spec['bkgcube'] = 'data/crab_bkgcube.fits' | |
| 151 | + spec['inmodel'] = 'data/crab.xml' | |
| 152 | + spec['srcname'] = 'Crab' | |
| 153 | + spec['outfile'] = 'csspec_py2.fits' | |
| 154 | + spec['ebinalg'] = 'LOG' | |
| 155 | + spec['enumbins'] = 2 | |
| 156 | + spec['emin'] = 0.1 | |
| 157 | + spec['emax'] = 100.0 | |
| 158 | + spec['logfile'] = 'csspec_py2.log' | |
| 159 | + spec['chatter'] = 3 | |
| 160 | + | |
| 161 | + # Execute csspec script | |
| 162 | + spec.execute() | |
| 163 | + | |
| 164 | + # Check pull distribution file | |
| 165 | + self._check_result_file('csspec_py2.fits', 2) | |
| 166 | + | |
| 167 | + # Return | |
| 168 | + return | |
| 169 | + | |
| 170 | + # Check result file | |
| 171 | + def _check_result_file(self, filename, bins): | |
| 172 | + """ | |
| 173 | + Check result file. | |
| 174 | + """ | |
| 175 | + # Open result file | |
| 176 | + fits = gammalib.GFits(filename) | |
| 177 | + | |
| 178 | + # Get spectrum table | |
| 179 | + spectrum = fits['SPECTRUM'] | |
| 180 | + | |
| 181 | + # Check dimensions | |
| 182 | + self.test_value(spectrum.nrows(), bins, 'Check for %d rows in spectrum' % bins) | |
| 183 | + self.test_value(spectrum.ncols(), 8, 'Check for 8 columns in spectrum') | |
| 184 | + | |
| 185 | + # Return | |
| 186 | + return | ... | ... |
test/test_cstsdist.py
0 → 100755
| 1 | +#! /usr/bin/env python | |
| 2 | +# ========================================================================== | |
| 3 | +# This scripts performs unit tests for the cstsdist 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 cstsdist script # | |
| 28 | +# ============================== # | |
| 29 | +class Test(gammalib.GPythonTestSuite): | |
| 30 | + """ | |
| 31 | + Test class for cstsdist script. | |
| 32 | + | |
| 33 | + This test class makes unit tests for the cstsdist 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('cstsdist') | |
| 55 | + | |
| 56 | + # Append tests | |
| 57 | + self.append(self._test_cmd, 'Test cstsdist on command line') | |
| 58 | + self.append(self._test_python, 'Test cstsdist from Python') | |
| 59 | + | |
| 60 | + # Return | |
| 61 | + return | |
| 62 | + | |
| 63 | + # Test cstsdist on command line | |
| 64 | + def _test_cmd(self): | |
| 65 | + """ | |
| 66 | + Test cstsdist on the command line. | |
| 67 | + """ | |
| 68 | + # Kluge to set the command (installed version has no README file) | |
| 69 | + if os.path.isfile('README'): | |
| 70 | + cstsdist = '../cscripts/cstsdist.py' | |
| 71 | + else: | |
| 72 | + cstsdist = 'cstsdist' | |
| 73 | + | |
| 74 | + # Setup cstsdist command | |
| 75 | + cmd = cstsdist+' inmodel="data/crab.xml"'+ \ | |
| 76 | + ' srcname="Crab" caldb="prod2" irf="South_0.5h"' + \ | |
| 77 | + ' ntrials=1 ra=83.63 dec=22.01 emin=0.1 emax=100.0'+ \ | |
| 78 | + ' enumbins=0 tmax=1800.0 rad=5.0 npix=200 npix=200'+ \ | |
| 79 | + ' binsz=0.05'+ \ | |
| 80 | + ' outfile="cstsdist_cmd1.dat"'+ \ | |
| 81 | + ' logfile="cstsdist_cmd1.log" chatter=1' | |
| 82 | + | |
| 83 | + # Execute cstsdist, 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 cstsdist execution on command line') | |
| 92 | + | |
| 93 | + # Check result file | |
| 94 | + self._check_result_file('cstsdist_cmd1.dat') | |
| 95 | + | |
| 96 | + # Setup cstsdist command | |
| 97 | + cmd = cstsdist+' inmodel="model_that_does_not_exist.xml"'+ \ | |
| 98 | + ' srcname="Crab" caldb="prod2" irf="South_0.5h"' + \ | |
| 99 | + ' ntrials=1 ra=83.63 dec=22.01 emin=0.1 emax=100.0'+ \ | |
| 100 | + ' enumbins=0 tmax=1800.0 rad=5.0 npix=200 npix=200'+ \ | |
| 101 | + ' binsz=0.05'+ \ | |
| 102 | + ' outfile="cstsdist_cmd2.dat"'+ \ | |
| 103 | + ' logfile="cstsdist_cmd2.log" chatter=2' | |
| 104 | + | |
| 105 | + # Execute cstsdist, 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 cstsdist execution on command line') | |
| 114 | + | |
| 115 | + # Return | |
| 116 | + return | |
| 117 | + | |
| 118 | + # Test cstsdist from Python | |
| 119 | + def _test_python(self): | |
| 120 | + """ | |
| 121 | + Test cstsdist from Python. | |
| 122 | + """ | |
| 123 | + # Set-up cstsdist | |
| 124 | + tsdist = cscripts.cstsdist() | |
| 125 | + tsdist['inmodel'] = 'data/crab.xml' | |
| 126 | + tsdist['srcname'] = 'Crab' | |
| 127 | + tsdist['caldb'] = 'prod2' | |
| 128 | + tsdist['irf'] = 'South_0.5h' | |
| 129 | + tsdist['outfile'] = 'cstsdist_py1.dat' | |
| 130 | + tsdist['ntrials'] = 1 | |
| 131 | + tsdist['ra'] = 83.63 | |
| 132 | + tsdist['dec'] = 22.01 | |
| 133 | + tsdist['emin'] = 0.1 | |
| 134 | + tsdist['emax'] = 100.0 | |
| 135 | + tsdist['enumbins'] = 0 | |
| 136 | + tsdist['tmax'] = 1800.0 | |
| 137 | + tsdist['rad'] = 5.0 | |
| 138 | + tsdist['npix'] = 200 | |
| 139 | + tsdist['binsz'] = 0.05 | |
| 140 | + tsdist['logfile'] = 'cstsdist_py1.log' | |
| 141 | + tsdist['chatter'] = 2 | |
| 142 | + | |
| 143 | + # Run cstsdist script | |
| 144 | + tsdist.logFileOpen() # Make sure we get a log file | |
| 145 | + tsdist.run() | |
| 146 | + tsdist.save() | |
| 147 | + | |
| 148 | + # Check pull distribution file | |
| 149 | + self._check_result_file('cstsdist_py1.dat') | |
| 150 | + | |
| 151 | + # Return | |
| 152 | + return | |
| 153 | + | |
| 154 | + # Check result file | |
| 155 | + def _check_result_file(self, filename): | |
| 156 | + """ | |
| 157 | + Check result file. | |
| 158 | + """ | |
| 159 | + # Open result file as CSV file | |
| 160 | + results = gammalib.GCsv(filename, ',') | |
| 161 | + | |
| 162 | + # Check dimensions | |
| 163 | + self.test_value(results.nrows(), 2, 'Check for 2 rows in TS file') | |
| 164 | + self.test_value(results.ncols(), 14, 'Check for 14 columns in TS file') | |
| 165 | + | |
| 166 | + # Return | |
| 167 | + return | ... | ... |
test/test_cstsmapmerge.py
0 → 100755
| 1 | +#! /usr/bin/env python | |
| 2 | +# ========================================================================== | |
| 3 | +# This scripts performs unit tests for the cstsmapmerge 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 cstsmapmerge script # | |
| 28 | +# ================================== # | |
| 29 | +class Test(gammalib.GPythonTestSuite): | |
| 30 | + """ | |
| 31 | + Test class for cstsmapmerge script. | |
| 32 | + | |
| 33 | + This test class makes unit tests for the cstsmapmerge 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('cstsmapmerge') | |
| 55 | + | |
| 56 | + # Append tests | |
| 57 | + self.append(self._test_cmd, 'Test cstsmapmerge on command line') | |
| 58 | + self.append(self._test_python, 'Test cstsmapmerge from Python') | |
| 59 | + | |
| 60 | + # Return | |
| 61 | + return | |
| 62 | + | |
| 63 | + # Test cstsmapmerge on command line | |
| 64 | + def _test_cmd(self): | |
| 65 | + """ | |
| 66 | + Test cstsmapmerge on the command line. | |
| 67 | + """ | |
| 68 | + # Kluge to set the command (installed version has no README file) | |
| 69 | + if os.path.isfile('README'): | |
| 70 | + cstsmapmerge = '../cscripts/cstsmapmerge.py' | |
| 71 | + else: | |
| 72 | + cstsmapmerge = 'cstsmapmerge' | |
| 73 | + | |
| 74 | + # Setup cstsmapmerge command | |
| 75 | + cmd = cstsmapmerge+' inmaps="data/tsmap_0.fits;data/tsmap_1.fits"'+ \ | |
| 76 | + ' outmap="cstsmapmerge_cmd1.fits"'+ \ | |
| 77 | + ' logfile="cstsmapmerge_cmd1.log" chatter=1' | |
| 78 | + | |
| 79 | + # Execute cstsmapmerge, make sure we catch any exception | |
| 80 | + try: | |
| 81 | + rc = os.system(cmd+' >/dev/null 2>&1') | |
| 82 | + except: | |
| 83 | + pass | |
| 84 | + | |
| 85 | + # Check if execution was successful | |
| 86 | + self.test_assert(rc == 0, | |
| 87 | + 'Successful cstsmapmerge execution on command line') | |
| 88 | + | |
| 89 | + # Check result file | |
| 90 | + self._check_result_file('cstsmapmerge_cmd1.fits') | |
| 91 | + | |
| 92 | + # Setup cstsmapmerge command | |
| 93 | + cmd = cstsmapmerge+' inmaps="map_that_does_not_exist.fits"'+ \ | |
| 94 | + ' outmap="cstsmapmerge_cmd2.fits"'+ \ | |
| 95 | + ' logfile="cstsmapmerge_cmd2.log" chatter=2' | |
| 96 | + | |
| 97 | + # Execute cstsmapmerge, make sure we catch any exception | |
| 98 | + try: | |
| 99 | + rc = os.system(cmd+' >/dev/null 2>&1') | |
| 100 | + except: | |
| 101 | + pass | |
| 102 | + | |
| 103 | + # Check if execution failed | |
| 104 | + self.test_assert(rc != 0, | |
| 105 | + 'Failure of cstsmapmerge execution on command line') | |
| 106 | + | |
| 107 | + # Return | |
| 108 | + return | |
| 109 | + | |
| 110 | + # Test cstsmapmerge from Python | |
| 111 | + def _test_python(self): | |
| 112 | + """ | |
| 113 | + Test cstsmapmerge from Python. | |
| 114 | + """ | |
| 115 | + # Set-up semi-colon separated cstsmapmerge | |
| 116 | + merge = cscripts.cstsmapmerge() | |
| 117 | + merge['inmaps'] = 'data/tsmap_0.fits;data/tsmap_1.fits' | |
| 118 | + merge['outmap'] = 'cstsmapmerge_py1.fits' | |
| 119 | + merge['logfile'] = 'cstsmapmerge_py1.log' | |
| 120 | + merge['chatter'] = 2 | |
| 121 | + | |
| 122 | + # Run cstsmapmerge script | |
| 123 | + merge.logFileOpen() # Make sure we get a log file | |
| 124 | + merge.run() | |
| 125 | + merge.save() | |
| 126 | + | |
| 127 | + # Check pull distribution file | |
| 128 | + self._check_result_file('cstsmapmerge_py1.fits') | |
| 129 | + | |
| 130 | + # Set-up space-separated cstsmapmerge | |
| 131 | + merge = cscripts.cstsmapmerge() | |
| 132 | + merge['inmaps'] = 'data/tsmap_0.fits data/tsmap_1.fits' | |
| 133 | + merge['outmap'] = 'cstsmapmerge_py2.fits' | |
| 134 | + merge['logfile'] = 'cstsmapmerge_py2.log' | |
| 135 | + merge['chatter'] = 3 | |
| 136 | + | |
| 137 | + # Run cstsmapmerge script | |
| 138 | + merge.execute() | |
| 139 | + | |
| 140 | + # Check pull distribution file | |
| 141 | + self._check_result_file('cstsmapmerge_py2.fits') | |
| 142 | + | |
| 143 | + # Set-up wildcard string cstsmapmerge | |
| 144 | + merge = cscripts.cstsmapmerge() | |
| 145 | + merge['inmaps'] = 'data/tsmap_*.fits' | |
| 146 | + merge['outmap'] = 'cstsmapmerge_py3.fits' | |
| 147 | + merge['logfile'] = 'cstsmapmerge_py3.log' | |
| 148 | + merge['chatter'] = 4 | |
| 149 | + | |
| 150 | + # Run cstsmapmerge script | |
| 151 | + merge.execute() | |
| 152 | + | |
| 153 | + # Check pull distribution file | |
| 154 | + self._check_result_file('cstsmapmerge_py3.fits') | |
| 155 | + | |
| 156 | + # Set-up ASCII file cstsmapmerge | |
| 157 | + merge = cscripts.cstsmapmerge() | |
| 158 | + merge['inmaps'] = '@data/tsmaps.txt' | |
| 159 | + merge['outmap'] = 'cstsmapmerge_py4.fits' | |
| 160 | + merge['logfile'] = 'cstsmapmerge_py4.log' | |
| 161 | + merge['chatter'] = 4 | |
| 162 | + | |
| 163 | + # Run cstsmapmerge script | |
| 164 | + merge.execute() | |
| 165 | + | |
| 166 | + # Check pull distribution file | |
| 167 | + self._check_result_file('cstsmapmerge_py4.fits') | |
| 168 | + | |
| 169 | + # Return | |
| 170 | + return | |
| 171 | + | |
| 172 | + # Check result file | |
| 173 | + def _check_result_file(self, filename): | |
| 174 | + """ | |
| 175 | + Check result file. | |
| 176 | + """ | |
| 177 | + # Open result file | |
| 178 | + fits = gammalib.GFits(filename) | |
| 179 | + | |
| 180 | + # Get HDUs | |
| 181 | + ts = fits['Primary'] | |
| 182 | + prefactor = fits['Prefactor'] | |
| 183 | + index = fits['Index'] | |
| 184 | + | |
| 185 | + # Check dimensions | |
| 186 | + self.test_value(ts.naxis(), 2, 'Check for 2 dimensions') | |
| 187 | + self.test_value(ts.naxes(0), 2, 'Check for 2 pixels in X') | |
| 188 | + self.test_value(ts.naxes(1), 1, 'Check for 1 pixel in Y') | |
| 189 | + self.test_value(prefactor.naxis(), 2, 'Check for 2 dimensions') | |
| 190 | + self.test_value(prefactor.naxes(0), 2, 'Check for 2 pixels in X') | |
| 191 | + self.test_value(prefactor.naxes(1), 1, 'Check for 1 pixel in Y') | |
| 192 | + self.test_value(index.naxis(), 2, 'Check for 2 dimensions') | |
| 193 | + self.test_value(index.naxes(0), 2, 'Check for 2 pixels in X') | |
| 194 | + self.test_value(index.naxes(1), 1, 'Check for 1 pixel in Y') | |
| 195 | + | |
| 196 | + # Return | |
| 197 | + return | ... | ... |
test/test_python_cscripts.py
| ... | ... | @@ -32,11 +32,16 @@ import test_csobs2caldb |
| 32 | 32 | import test_csobsdef |
| 33 | 33 | import test_csobsinfo |
| 34 | 34 | import test_cspull |
| 35 | +import test_csresmap | |
| 35 | 36 | import test_csroot2caldb |
| 37 | +import test_cssens | |
| 38 | +import test_csspec | |
| 39 | +import test_cstsdist | |
| 40 | +import test_cstsmapmerge | |
| 41 | +import test_csworkflow | |
| 36 | 42 | import test_csiactcopy |
| 37 | 43 | import test_csiactdata |
| 38 | 44 | import test_csiactobs |
| 39 | -import test_csworkflow | |
| 40 | 45 | |
| 41 | 46 | |
| 42 | 47 | # ================== # |
| ... | ... | @@ -61,29 +66,29 @@ def test(installed=False, debug=False): |
| 61 | 66 | dirname = os.path.dirname(testdir) |
| 62 | 67 | |
| 63 | 68 | # Copy over test data and irf |
| 64 | - os.system("cp -r %s %s" % (dirname+"/data", "data")) | |
| 65 | - os.system("cp -r %s %s" % (dirname+"/iactdata", "iactdata")) | |
| 66 | - os.system("cp -r %s %s" % (dirname+"/irf", "irf")) | |
| 69 | + os.system('cp -r %s %s' % (dirname+'/data', 'data')) | |
| 70 | + os.system('cp -r %s %s' % (dirname+'/iactdata', 'iactdata')) | |
| 71 | + os.system('cp -r %s %s' % (dirname+'/irf', 'irf')) | |
| 67 | 72 | |
| 68 | 73 | # ... otherwise set the calibration database |
| 69 | 74 | else: |
| 70 | - os.environ['CALDB'] = "../caldb" | |
| 75 | + os.environ['CALDB'] = '../caldb' | |
| 71 | 76 | |
| 72 | 77 | # Set PFILES environment variable |
| 73 | 78 | try: |
| 74 | - os.mkdir("pfiles") | |
| 79 | + os.mkdir('pfiles') | |
| 75 | 80 | except: |
| 76 | 81 | pass |
| 77 | - os.environ['PFILES'] = "pfiles" | |
| 82 | + os.environ['PFILES'] = 'pfiles' | |
| 78 | 83 | |
| 79 | 84 | # Copy over pfiles |
| 80 | 85 | if not installed: |
| 81 | - os.system("cp -r ../cscripts/*.par pfiles/") | |
| 86 | + os.system('cp -r ../cscripts/*.par pfiles/') | |
| 82 | 87 | else: |
| 83 | - os.system("cp -r %s/syspfiles/*.par pfiles/" % (os.environ['CTOOLS'])) | |
| 88 | + os.system('cp -r %s/syspfiles/*.par pfiles/' % (os.environ['CTOOLS'])) | |
| 84 | 89 | |
| 85 | 90 | # Allocate test suite container |
| 86 | - suites = gammalib.GTestSuites("cscripts unit testing") | |
| 91 | + suites = gammalib.GTestSuites('cscripts unit testing') | |
| 87 | 92 | |
| 88 | 93 | # Allocate test suites |
| 89 | 94 | suite_cscaldb = test_cscaldb.Test() |
| ... | ... | @@ -94,7 +99,12 @@ def test(installed=False, debug=False): |
| 94 | 99 | suite_csobsdef = test_csobsdef.Test() |
| 95 | 100 | suite_csobsinfo = test_csobsinfo.Test() |
| 96 | 101 | suite_cspull = test_cspull.Test() |
| 102 | + suite_csresmap = test_csresmap.Test() | |
| 97 | 103 | suite_csroot2caldb = test_csroot2caldb.Test() |
| 104 | + suite_cssens = test_cssens.Test() | |
| 105 | + suite_csspec = test_csspec.Test() | |
| 106 | + suite_cstsdist = test_cstsdist.Test() | |
| 107 | + suite_cstsmapmerge = test_cstsmapmerge.Test() | |
| 98 | 108 | suite_csworkflow = test_csworkflow.Test() |
| 99 | 109 | |
| 100 | 110 | # Setup unit tests |
| ... | ... | @@ -106,7 +116,12 @@ def test(installed=False, debug=False): |
| 106 | 116 | suite_csobsdef.set() |
| 107 | 117 | suite_csobsinfo.set() |
| 108 | 118 | suite_cspull.set() |
| 119 | + suite_csresmap.set() | |
| 109 | 120 | suite_csroot2caldb.set() |
| 121 | + suite_cssens.set() | |
| 122 | + suite_csspec.set() | |
| 123 | + suite_cstsdist.set() | |
| 124 | + suite_cstsmapmerge.set() | |
| 110 | 125 | suite_csworkflow.set() |
| 111 | 126 | |
| 112 | 127 | # Append tests to container |
| ... | ... | @@ -118,7 +133,12 @@ def test(installed=False, debug=False): |
| 118 | 133 | suites.append(suite_csobsdef) |
| 119 | 134 | suites.append(suite_csobsinfo) |
| 120 | 135 | suites.append(suite_cspull) |
| 136 | + suites.append(suite_csresmap) | |
| 121 | 137 | suites.append(suite_csroot2caldb) |
| 138 | + suites.append(suite_cssens) | |
| 139 | + suites.append(suite_csspec) | |
| 140 | + suites.append(suite_cstsdist) | |
| 141 | + suites.append(suite_cstsmapmerge) | |
| 122 | 142 | suites.append(suite_csworkflow) |
| 123 | 143 | |
| 124 | 144 | # Append tests for Python 2.6+ (the IACT cscripts depend on the json |
| ... | ... | @@ -149,7 +169,7 @@ def test(installed=False, debug=False): |
| 149 | 169 | |
| 150 | 170 | # If we have a non-installed version then save test results |
| 151 | 171 | if not installed: |
| 152 | - suites.save("reports/cscripts.xml") | |
| 172 | + suites.save('reports/cscripts.xml') | |
| 153 | 173 | |
| 154 | 174 | # If debuging is requested then print test suites |
| 155 | 175 | if debug: | ... | ... |