Commit 87f0705cf0e4c1d4a55244ce3b79ab0f2b012f90

Authored by Jürgen Knödlseder
1 parent 6bb547d9

Add csmodelmerge script unit test.

cscripts/Makefile.am
... ... @@ -30,6 +30,7 @@ testscripts = tests/__init__.py \
30 30 ../test/test_csobs2caldb.py \
31 31 ../test/test_cslightcrv.py \
32 32 ../test/test_csmodelinfo.py \
  33 + ../test/test_csmodelmerge.py \
33 34 ../test/test_csfindobs.py \
34 35 ../test/test_csiactcopy.py \
35 36 ../test/test_csiactdata.py \
... ... @@ -40,7 +41,10 @@ testdata = ../test/data/crab.xml \
40 41 ../test/data/obs_unbinned.xml \
41 42 ../test/data/crab_events.fits.gz \
42 43 ../test/data/crab_cntmap.fits.gz \
43   - ../test/data/exclusion.reg
  44 + ../test/data/exclusion.reg \
  45 + ../test/data/models.txt \
  46 + ../test/data/model_cube_background.xml \
  47 + ../test/data/model_cube_background2.xml
44 48  
45 49 # Test data to be distributed in $(pyexecdir)/cscripts/tests/iactdata
46 50 testiactdata = ../test/iactdata/hdu-index.fits.gz \
... ...
test/data/models.txt 0 → 100644
  1 +data/model_cube_background.xml
  2 +data/model_cube_background2.xml
... ...
test/test_csmodelinfo.py
... ... @@ -122,7 +122,7 @@ class Test(gammalib.GPythonTestSuite):
122 122 modelinfo["logfile"] = "csmodelinfo_py1.log"
123 123 modelinfo["chatter"] = 2
124 124  
125   - # Run csmodelinfo script and save light curve
  125 + # Run csmodelinfo script and save DS9 file
126 126 modelinfo.logFileOpen() # Make sure we get a log file
127 127 modelinfo.run()
128 128 modelinfo.save()
... ...
test/test_csmodelmerge.py 0 → 100755
  1 +#! /usr/bin/env python
  2 +# ==========================================================================
  3 +# This scripts performs unit tests for the csmodelmerge 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 csmodelmerge script #
  28 +# ================================== #
  29 +class Test(gammalib.GPythonTestSuite):
  30 + """
  31 + Test class for csmodelmerge script.
  32 +
  33 + This test class makes unit tests for the csmodelmerge 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("csmodelmerge")
  55 +
  56 + # Append tests
  57 + self.append(self._test_cmd, "Test csmodelmerge on command line")
  58 + self.append(self._test_python, "Test csmodelmerge from Python")
  59 +
  60 + # Return
  61 + return
  62 +
  63 + # Test csmodelmerge on command line
  64 + def _test_cmd(self):
  65 + """
  66 + Test csmodelmerge on the command line.
  67 + """
  68 + # Kluge to set the command (installed version has no README file)
  69 + if os.path.isfile("README"):
  70 + csmodelmerge = "../cscripts/csmodelmerge.py"
  71 + else:
  72 + csmodelmerge = "csmodelmerge"
  73 +
  74 + # Setup csmodelmerge command
  75 + cmd = csmodelmerge+' inmodels="data/crab.xml data/model_cube_background.xml"'+ \
  76 + ' outmodel="mergedmodel_cmd1.xml"'+ \
  77 + ' logfile="csmodelmerge_cmd1.log" chatter=1'
  78 +
  79 + # Execute csmodelmerge, 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 csmodelmerge execution on command line")
  88 +
  89 + # Check model file
  90 + self._check_model_file("mergedmodel_cmd1.xml", 3)
  91 +
  92 + # Setup csmodelmerge command
  93 + cmd = csmodelmerge+' inmodels="model_that_does_not_exist.xml"'+ \
  94 + ' outmodel="mergedmodel_cmd2.xml"'+ \
  95 + ' logfile="csmodelmerge_cmd2.log"'
  96 +
  97 + # Execute csmodelmerge, 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 csmodelmerge execution on command line")
  106 +
  107 + # Return
  108 + return
  109 +
  110 + # Test csmodelmerge from Python
  111 + def _test_python(self):
  112 + """
  113 + Test csmodelmerge from Python.
  114 + """
  115 + # Set-up csmodelmerge for space-separated input
  116 + modelmerge = cscripts.csmodelmerge()
  117 + modelmerge["inmodels"] = "data/crab.xml data/model_cube_background.xml"
  118 + modelmerge["outmodel"] = "mergedmodel_py1.xml"
  119 + modelmerge["logfile"] = "csmodelmerge_py1.log"
  120 + modelmerge["chatter"] = 2
  121 +
  122 + # Run csmodelmerge script and save models
  123 + modelmerge.logFileOpen() # Make sure we get a log file
  124 + modelmerge.run()
  125 + modelmerge.save()
  126 +
  127 + # Check model file
  128 + self._check_model_file("mergedmodel_py1.xml", 3)
  129 +
  130 + # Set-up csmodelmerge for semi-colon separated input
  131 + modelmerge = cscripts.csmodelmerge()
  132 + modelmerge["inmodels"] = "data/crab.xml;data/model_cube_background.xml"
  133 + modelmerge["outmodel"] = "mergedmodel_py2.xml"
  134 + modelmerge["logfile"] = "csmodelmerge_py2.log"
  135 + modelmerge["chatter"] = 3
  136 +
  137 + # Execute csmodelmerge script
  138 + modelmerge.execute()
  139 +
  140 + # Check model file
  141 + self._check_model_file("mergedmodel_py2.xml", 3)
  142 +
  143 + # Set-up csmodelmerge for wildcard input
  144 + modelmerge = cscripts.csmodelmerge()
  145 + modelmerge["inmodels"] = "data/model_cube_background*.xml"
  146 + modelmerge["outmodel"] = "mergedmodel_py3.xml"
  147 + modelmerge["logfile"] = "csmodelmerge_py3.log"
  148 + modelmerge["chatter"] = 4
  149 +
  150 + # Execute csmodelmerge script
  151 + modelmerge.execute()
  152 +
  153 + # Check model file
  154 + self._check_model_file("mergedmodel_py3.xml", 2)
  155 +
  156 + # Set-up csmodelmerge for ASCII file
  157 + modelmerge = cscripts.csmodelmerge()
  158 + modelmerge["inmodels"] = "@data/models.txt"
  159 + modelmerge["outmodel"] = "mergedmodel_py4.xml"
  160 + modelmerge["logfile"] = "csmodelmerge_py4.log"
  161 + modelmerge["chatter"] = 4
  162 +
  163 + # Execute csmodelmerge script
  164 + modelmerge.execute()
  165 +
  166 + # Check model file
  167 + self._check_model_file("mergedmodel_py4.xml", 2)
  168 +
  169 + # Return
  170 + return
  171 +
  172 + # Check model file
  173 + def _check_model_file(self, filename, number):
  174 + """
  175 + Check model file.
  176 + """
  177 + # Open models
  178 + models = gammalib.GModels(filename)
  179 +
  180 + # Check number of models
  181 + self.test_value(models.size(), number,
  182 + 'Check for '+str(number)+' models in XML file')
  183 +
  184 + # Return
  185 + return
... ...
test/test_python_cscripts.py
... ... @@ -27,6 +27,7 @@ import test_cscaldb
27 27 import test_csobs2caldb
28 28 import test_cslightcrv
29 29 import test_csmodelinfo
  30 +import test_csmodelmerge
30 31 import test_csfindobs
31 32 import test_csiactcopy
32 33 import test_csiactdata
... ... @@ -79,22 +80,25 @@ def test(installed=False, debug=False):
79 80 suites = gammalib.GTestSuites("cscripts unit testing")
80 81  
81 82 # Allocate test suites
82   - suite_cscaldb = test_cscaldb.Test()
83   - suite_csobs2caldb = test_csobs2caldb.Test()
84   - suite_cslightcrv = test_cslightcrv.Test()
85   - suite_csmodelinfo = test_csmodelinfo.Test()
  83 + suite_cscaldb = test_cscaldb.Test()
  84 + suite_csobs2caldb = test_csobs2caldb.Test()
  85 + suite_cslightcrv = test_cslightcrv.Test()
  86 + suite_csmodelinfo = test_csmodelinfo.Test()
  87 + suite_csmodelmerge = test_csmodelmerge.Test()
86 88  
87 89 # Setup unit tests
88 90 suite_cscaldb.set()
89 91 suite_csobs2caldb.set()
90 92 suite_cslightcrv.set()
91 93 suite_csmodelinfo.set()
  94 + suite_csmodelmerge.set()
92 95  
93 96 # Append tests to container
94 97 suites.append(suite_cscaldb)
95 98 suites.append(suite_csobs2caldb)
96 99 suites.append(suite_cslightcrv)
97 100 suites.append(suite_csmodelinfo)
  101 + suites.append(suite_csmodelmerge)
98 102  
99 103 # Append tests for Python 2.6+ (the IACT cscripts depend on the json
100 104 # module which is only available since Python 2.6+
... ...