Commit 9db125b43fa473a695d2572cde3f0a794dc0a3ad

Authored by Jürgen Knödlseder
1 parent ab3fb1fa

Python unit test script now uses GammaLib Test Suite.

The GammaLib Test Suite is now used by the ctools Python unit test. Reports are written into reports/ctools.xml.
test/test_ctobssim.py 0 → 100755
  1 +#! /usr/bin/env python
  2 +# ==========================================================================
  3 +# This scripts performs unit tests for the ctobssim tool.
  4 +#
  5 +# Copyright (C) 2014 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 gammalib
  22 +import ctools
  23 +
  24 +
  25 +# ============================ #
  26 +# Test class for ctobssim tool #
  27 +# ============================ #
  28 +class Test(gammalib.GPythonTestSuite):
  29 + """
  30 + Test class for ctobssim tool.
  31 + """
  32 + # Constructor
  33 + def __init__(self):
  34 + """
  35 + Constructor.
  36 + """
  37 + # Call base class constructor
  38 + gammalib.GPythonTestSuite.__init__(self)
  39 +
  40 + # Set members
  41 + self.model_name = "data/crab.xml"
  42 + self.events_name = "events.fits"
  43 + self.caldb = "irf"
  44 + self.irf = "cta_dummy_irf"
  45 +
  46 + # Return
  47 + return
  48 +
  49 + # Set test functions
  50 + def set(self):
  51 + """
  52 + Set all test functions.
  53 + """
  54 + # Set test name
  55 + self.name("ctobssim")
  56 +
  57 + # Append tests
  58 + self.append(self.test_functional, "Test ctobssim functionality")
  59 +
  60 + # Return
  61 + return
  62 +
  63 + # Test ctobssim functionnality
  64 + def test_functional(self):
  65 + """
  66 + Test ctobssim functionnality.
  67 + """
  68 + # Set-up ctobssim
  69 + sim = ctools.ctobssim()
  70 + sim["infile"].filename(self.model_name)
  71 + sim["outfile"].filename("events.fits")
  72 + sim["caldb"].string(self.caldb)
  73 + sim["irf"].string(self.irf)
  74 + sim["ra"].real(83.63)
  75 + sim["dec"].real(22.01)
  76 + sim["rad"].real(10.0)
  77 + sim["tmin"].real(0.0)
  78 + sim["tmax"].real(1800.0)
  79 + sim["emin"].real(0.1)
  80 + sim["emax"].real(100.0)
  81 +
  82 + # Run tool
  83 + self.test_try("Run ctobssim")
  84 + try:
  85 + sim.run()
  86 + self.test_try_success()
  87 + except:
  88 + self.test_try_failure("Exception occured in ctobssim.")
  89 +
  90 + # Retrieve observation and check content
  91 + obs = gammalib.GCTAObservation(sim.obs()[0])
  92 + pnt = obs.pointing()
  93 + evt = obs.events()
  94 + self.test_assert(obs.instrument() == "CTA", "Observation not a CTA observation")
  95 + self.test_value(sim.obs().size(), 1, "There is not a single observation")
  96 + self.test_value(obs.ontime(), 1800.0, 1.0e-6, "Ontime is not 1800 sec")
  97 + self.test_value(obs.livetime(), 1710.0, 1.0e-6, "Livetime is not 1710 sec")
  98 + self.test_value(pnt.dir().ra_deg(), 83.63, 1.0e-6, "ROI Right Ascension is not 83.63 deg")
  99 + self.test_value(pnt.dir().dec_deg(), 22.01, 1.0e-6, "ROI Declination is not 22.01 deg")
  100 + self.test_value(evt.size(), 4134, "Number of events is not 4134")
  101 +
  102 + # Save events
  103 + self.test_try("Save events")
  104 + try:
  105 + sim.save()
  106 + self.test_try_success()
  107 + except:
  108 + self.test_try_failure("Exception occured in saving events.")
... ...
test/test_pipelines.py 0 → 100755
  1 +#! /usr/bin/env python
  2 +# ==========================================================================
  3 +# This scripts performs tests processing pipelines.
  4 +#
  5 +# Copyright (C) 2014 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 gammalib
  22 +import ctools
  23 +
  24 +
  25 +# ============================ #
  26 +# Test class for ctobssim tool #
  27 +# ============================ #
  28 +class Test(gammalib.GPythonTestSuite):
  29 + """
  30 + Test class for pipelines.
  31 + """
  32 + # Constructor
  33 + def __init__(self):
  34 + """
  35 + Constructor.
  36 + """
  37 + # Call base class constructor
  38 + gammalib.GPythonTestSuite.__init__(self)
  39 +
  40 + # Return
  41 + return
  42 +
  43 + # Set test functions
  44 + def set(self):
  45 + """
  46 + Set all test functions.
  47 + """
  48 + # Set test name
  49 + self.name("pipelines")
  50 +
  51 + # Append tests
  52 + self.append(self.test_unbinned_fits, "Test unbinned pipeline with FITS file saving")
  53 + self.append(self.test_unbinned_mem, "Test unbinned in-memory pipeline")
  54 +
  55 + # Return
  56 + return
  57 +
  58 + # Test unbinned pipeline with FITS file saving
  59 + def test_unbinned_fits(self):
  60 + """
  61 + Test unbinned pipeline with FITS file saving.
  62 + """
  63 + # Set script parameters
  64 + model_name = "data/crab.xml"
  65 + events_name = "events.fits"
  66 + selected_events_name = "selected_events.fits"
  67 + result_name = "results.xml"
  68 + caldb = "irf"
  69 + irf = "cta_dummy_irf"
  70 + ra = 83.63
  71 + dec = 22.01
  72 + rad_sim = 10.0
  73 + tstart = 0.0
  74 + tstop = 1800.0
  75 + emin = 0.1
  76 + emax = 100.0
  77 + rad_select = 3.0
  78 +
  79 + # Simulate events
  80 + sim = ctools.ctobssim()
  81 + sim["infile"].filename(model_name)
  82 + sim["outfile"].filename(events_name)
  83 + sim["caldb"].string(caldb)
  84 + sim["irf"].string(irf)
  85 + sim["ra"].real(ra)
  86 + sim["dec"].real(dec)
  87 + sim["rad"].real(rad_sim)
  88 + sim["tmin"].real(tstart)
  89 + sim["tmax"].real(tstop)
  90 + sim["emin"].real(emin)
  91 + sim["emax"].real(emax)
  92 + self.test_try("Execute ctobssim")
  93 + try:
  94 + sim.execute()
  95 + self.test_try_success()
  96 + except:
  97 + self.test_try_failure("Exception occured in ctobssim.")
  98 +
  99 + # Select events
  100 + select = ctools.ctselect()
  101 + select["infile"].filename(events_name)
  102 + select["outfile"].filename(selected_events_name)
  103 + select["ra"].real(ra)
  104 + select["dec"].real(dec)
  105 + select["rad"].real(rad_select)
  106 + select["tmin"].real(tstart)
  107 + select["tmax"].real(tstop)
  108 + select["emin"].real(emin)
  109 + select["emax"].real(emax)
  110 + self.test_try("Execute ctselect")
  111 + try:
  112 + select.execute()
  113 + self.test_try_success()
  114 + except:
  115 + self.test_try_failure("Exception occured in ctselect.")
  116 +
  117 + # Perform maximum likelihood fitting
  118 + like = ctools.ctlike()
  119 + like["infile"].filename(selected_events_name)
  120 + like["srcmdl"].filename(model_name)
  121 + like["outmdl"].filename(result_name)
  122 + like["caldb"].string(caldb)
  123 + like["irf"].string(irf)
  124 + self.test_try("Execute ctlike")
  125 + try:
  126 + like.execute()
  127 + self.test_try_success()
  128 + except:
  129 + self.test_try_failure("Exception occured in ctlike.")
  130 +
  131 +
  132 + # Test unbinned in-memory pipeline
  133 + def test_unbinned_mem(self):
  134 + """
  135 + Test unbinned in-memory pipeline.
  136 + """
  137 + # Set script parameters
  138 + model_name = "data/crab.xml"
  139 + caldb = "irf"
  140 + irf = "cta_dummy_irf"
  141 + ra = 83.63
  142 + dec = 22.01
  143 + rad_sim = 10.0
  144 + tstart = 0.0
  145 + tstop = 1800.0
  146 + emin = 0.1
  147 + emax = 100.0
  148 + rad_select = 3.0
  149 +
  150 + # Simulate events
  151 + sim = ctools.ctobssim()
  152 + sim["infile"].filename(model_name)
  153 + sim["caldb"].string(caldb)
  154 + sim["irf"].string(irf)
  155 + sim["ra"].real(ra)
  156 + sim["dec"].real(dec)
  157 + sim["rad"].real(rad_sim)
  158 + sim["tmin"].real(tstart)
  159 + sim["tmax"].real(tstop)
  160 + sim["emin"].real(emin)
  161 + sim["emax"].real(emax)
  162 + self.test_try("Run ctobssim")
  163 + try:
  164 + sim.run()
  165 + self.test_try_success()
  166 + except:
  167 + self.test_try_failure("Exception occured in ctobssim.")
  168 +
  169 + # Select events
  170 + select = ctools.ctselect(sim.obs())
  171 + select["ra"].real(ra)
  172 + select["dec"].real(dec)
  173 + select["rad"].real(rad_select)
  174 + select["tmin"].real(tstart)
  175 + select["tmax"].real(tstop)
  176 + select["emin"].real(emin)
  177 + select["emax"].real(emax)
  178 + self.test_try("Run ctselect")
  179 + try:
  180 + select.run()
  181 + self.test_try_success()
  182 + except:
  183 + self.test_try_failure("Exception occured in ctselect.")
  184 +
  185 + # Perform maximum likelihood fitting
  186 + like = ctools.ctlike(select.obs())
  187 + self.test_try("Run ctlike")
  188 + try:
  189 + like.run()
  190 + self.test_try_success()
  191 + except:
  192 + self.test_try_failure("Exception occured in ctlike.")
... ...
test/test_python.py
1 1 #! /usr/bin/env python
2 2 # ==========================================================================
3   -# This script illustrates how to build an unbinned analysis pipeline using
4   -# the ctools.
  3 +# This scripts performs unit tests for the ctools package.
5 4 #
6   -# Copyright (C) 2011-2014 Juergen Knoedlseder
  5 +# Copyright (C) 2012-2014 Juergen Knoedlseder
7 6 #
8 7 # This program is free software: you can redistribute it and/or modify
9 8 # it under the terms of the GNU General Public License as published by
... ... @@ -19,177 +18,36 @@
19 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 19 #
21 20 # ==========================================================================
22   -from ctools import *
23   -from gammalib import *
24   -from math import *
25   -import os
26   -import glob
  21 +import gammalib
  22 +import ctools
27 23 import sys
28   -
29   -# ============================= #
30   -# Analysis pipeline - version 1 #
31   -# ============================= #
32   -def pipeline_v1():
33   - """
34   - Unbinned analysis pipeline - save intermediate results.
35   -
36   - This function implements an analysis pipeline that successively calls
37   - ctobssim, ctselect and ctlike by saving the intermediate results as FITS
38   - files on disk. This replicates the way how the analysis would be done
39   - in a ftools-like approach.
40   - """
41   - # Set script parameters
42   - model_name = "data/crab.xml"
43   - events_name = "events.fits"
44   - selected_events_name = "selected_events.fits"
45   - result_name = "results.xml"
46   - caldb = "irf"
47   - irf = "cta_dummy_irf"
48   - ra = 83.63
49   - dec = 22.01
50   - rad_sim = 10.0
51   - tstart = 0.0
52   - tstop = 1800.0
53   - emin = 0.1
54   - emax = 100.0
55   - rad_select = 3.0
56   -
57   - # Initialise timing
58   - wall_seconds = 0.0
59   - cpu_seconds = 0.0
60   -
61   - # Simulate events
62   - sim = ctobssim()
63   - sim.logFileOpen()
64   - sim["infile"].filename(model_name)
65   - sim["outfile"].filename(events_name)
66   - sim["caldb"].string(caldb)
67   - sim["irf"].string(irf)
68   - sim["ra"].real(ra)
69   - sim["dec"].real(dec)
70   - sim["rad"].real(rad_sim)
71   - sim["tmin"].real(tstart)
72   - sim["tmax"].real(tstop)
73   - sim["emin"].real(emin)
74   - sim["emax"].real(emax)
75   - sim.execute()
76   - sys.stdout.write(".")
77   -
78   - # Select events
79   - select = ctselect()
80   - select.logFileOpen()
81   - select["infile"].filename(events_name)
82   - select["outfile"].filename(selected_events_name)
83   - select["ra"].real(ra)
84   - select["dec"].real(dec)
85   - select["rad"].real(rad_select)
86   - select["tmin"].real(tstart)
87   - select["tmax"].real(tstop)
88   - select["emin"].real(emin)
89   - select["emax"].real(emax)
90   - select.execute()
91   - sys.stdout.write(".")
92   -
93   - # Perform maximum likelihood fitting
94   - like = ctlike()
95   - like.logFileOpen()
96   - like["infile"].filename(selected_events_name)
97   - like["srcmdl"].filename(model_name)
98   - like["outmdl"].filename(result_name)
99   - like["caldb"].string(caldb)
100   - like["irf"].string(irf)
101   - like.execute()
102   - sys.stdout.write(".")
103   -
104   - # Return
105   - return
  24 +import os
  25 +import test_ctobssim
  26 +import test_pipelines
106 27  
107 28  
108   -# ============================= #
109   -# Analysis pipeline - version 2 #
110   -# ============================= #
111   -def pipeline_v2(model_name="data/crab.xml", \
112   - ra_sim=83.63, dec_sim=22.01, rad_sim=10.0, \
113   - ra_select=83.63, dec_select=22.01, rad_select=3.0, \
114   - tstart=0.0, tstop=1800.0, \
115   - emin=0.1, emax=100.0, \
116   - ref={}):
  29 +# ======================== #
  30 +# Main routine entry point #
  31 +# ======================== #
  32 +if __name__ == '__main__':
117 33 """
118   - Unbinned analysis pipeline - keep intermediate results in memory.
119   -
120   - This function implements an analysis pipeline that successively calls
121   - ctobssim, ctselect and ctlike without saving the intermediate results as
122   - FITS files on disk. All data is only hold in memory.
  34 + Perform unit testing for Python interface.
123 35 """
124   - # Set script parameters
125   - caldb = "irf"
126   - irf = "cta_dummy_irf"
127   -
128   - # Simulate events
129   - sim = ctobssim()
130   - sim["infile"].filename(model_name)
131   - sim["caldb"].string(caldb)
132   - sim["irf"].string(irf)
133   - sim["ra"].real(ra_sim)
134   - sim["dec"].real(dec_sim)
135   - sim["rad"].real(rad_sim)
136   - sim["tmin"].real(tstart)
137   - sim["tmax"].real(tstop)
138   - sim["emin"].real(emin)
139   - sim["emax"].real(emax)
140   - sim.run()
141   - sys.stdout.write(".")
142   -
143   - # Select events
144   - select = ctselect(sim.obs())
145   - select["ra"].real(ra_select)
146   - select["dec"].real(dec_select)
147   - select["rad"].real(rad_select)
148   - select["tmin"].real(tstart)
149   - select["tmax"].real(tstop)
150   - select["emin"].real(emin)
151   - select["emax"].real(emax)
152   - select.run()
153   - sys.stdout.write(".")
154   -
155   - # Perform maximum likelihood fitting
156   - like = ctlike(select.obs())
157   - like.run()
158   - sys.stdout.write(".")
  36 + # Allocate test suite container
  37 + suites = gammalib.GTestSuites("ctools unit testing")
159 38  
160   - # Analyse results
161   - errors = 0
162   - models = like.obs().models()
163   - #print(models)
164   - for model in models:
165   - for key in ref:
166   - if key == model.name():
167   - for par in model:
168   - for value in ref[key]:
169   - if value == par.name():
170   - if abs(float(ref[key][value])-par.value()) < 0.0001:
171   - sys.stdout.write(".")
172   - else:
173   - sys.stdout.write("E")
174   - errors += 1
  39 + # Allocate test suites and append them to the container
  40 + suite_ctobssim = test_ctobssim.Test()
  41 + suite_pipelines = test_pipelines.Test()
175 42  
176   - # Return number of errors
177   - return errors
  43 + # Setup unit tests
  44 + suite_ctobssim.set()
  45 + suite_pipelines.set()
178 46  
  47 + # Append tests to container
  48 + suites.append(suite_ctobssim)
  49 + suites.append(suite_pipelines)
179 50  
180   -#==========================#
181   -# Main routine entry point #
182   -#==========================#
183   -if __name__ == '__main__':
184   - """
185   - Test ctools Python modules.
186   - """
187   - # Dump header
188   - print("")
189   - print("***********************************")
190   - print("* ctools Python interface testing *")
191   - print("***********************************")
192   -
193 51 # Set PFILES environment variable
194 52 try:
195 53 os.mkdir("pfiles")
... ... @@ -197,51 +55,18 @@ if __name__ == &#39;__main__&#39;:
197 55 pass
198 56 os.system("cp -r ../src/*/*.par pfiles/")
199 57 os.environ['PFILES'] = "pfiles"
200   -
201   - # Remove any existing result files
202   - list = [glob.glob("*.fits"), glob.glob("*.xml")]
203   - for files in list:
204   - for file in files:
205   - os.remove(file)
206 58  
207   - # Save intermediate results on disk
208   - sys.stdout.write("Test executable analysis: ")
209   - pipeline_v1()
210   - print(" ok")
  59 + # Run test suite
  60 + success = suites.run()
211 61  
212   - # Keep results in memory
213   - sys.stdout.write("Test in-memory analysis: ")
214   - err = pipeline_v2()
215   - if err == 0:
216   - print(" ok")
217   - else:
218   - print(" NOK")
219   -
220   - # Test diffuse map cube with constant spectrum
221   - sys.stdout.write("Test diffuse map cube with constant analysis: ")
222   - ref = {'CTABackgroundModel': {'Value': 0.242717}}
223   - err = pipeline_v2(model_name="data/model_background_cube_const.xml", \
224   - ra_sim=84.17263, dec_sim=22.01444, rad_sim=2.0, \
225   - ra_select=83.80, dec_select=22.20, rad_select=1.0, \
226   - tstart=0.0, tstop=1.0, \
227   - emin=0.3, emax=8.0, \
228   - ref=ref)
229   - if err == 0:
230   - print(" ok")
231   - else:
232   - print(" NOK")
  62 + # Save test results
  63 + suites.save("reports/ctools.xml")
233 64  
234   - # Test diffuse map cube with power law spectrum
235   - sys.stdout.write("Test diffuse map cube with power law analysis: ")
236   - ref = {'CTABackgroundModel': {'Prefactor': 0.513946, 'Index': -1.22388}}
237   - err = pipeline_v2(model_name="data/model_background_cube.xml", \
238   - ra_sim=84.17263, dec_sim=22.01444, rad_sim=2.0, \
239   - ra_select=84.00, dec_select=22.20, rad_select=1.0, \
240   - tstart=0.0, tstop=1.0, \
241   - emin=0.3, emax=8.0, \
242   - ref=ref)
243   - if err == 0:
244   - print(" ok")
  65 + # Set return code
  66 + if success:
  67 + rc = 0
245 68 else:
246   - print(" NOK")
  69 + rc = 1
247 70  
  71 + # Exit with return code
  72 + sys.exit(rc)
... ...