Commit 1d60a87b2a08f7b420ef52f38b068aadd61bc143

Authored by Jürgen Knödlseder
1 parent 9ba34ae9

Update cscripts unit test.

For the moment, no test is done (I cannot get the cscript import working correctly).
cscripts/Makefile.am
... ... @@ -14,7 +14,7 @@ pythonscripts = __init__.py \
14 14  
15 15 # Test scripts to be distributed in $(pythondir)/cscripts/tests
16 16 testscripts = tests/__init__.py \
17   - tests/test_unbinned.py
  17 + tests/scripts.py
18 18  
19 19 # Scripts for which symbolic links will be created in bindir
20 20 linkscripts = cspull cssens cstsdist cscaldb csresmap \
... ...
cscripts/__init__.py
1   -"""cscripts --- Python scripts from the ctools package.
  1 +"""
  2 +cscripts --- Python scripts from the ctools package.
2 3  
3 4 http://cta.irap.omp.eu/ctools/
4 5 """
... ... @@ -33,18 +34,22 @@ from .csspec import csspec
33 34 from .cstsdist import cstsdist
34 35 from . import obsutils
35 36  
36   -
  37 +# Add test function
37 38 def test(verbosity=2):
38   - """Run ctools tests.
39 39 """
  40 + Run cscripts tests.
  41 + """
  42 + # Imports
40 43 import unittest
41   - from .tests.test_unbinned import TestUnbinnedAnalysis
  44 + from .tests.scripts import scripts
42 45  
  46 + # Create a test suite
43 47 test_suite = unittest.TestSuite()
44 48  
45 49 # Test cases must be listed manually here ...
46   - # we currently don't use unittest for test collection
47   - test_suite.addTest(unittest.makeSuite(TestUnbinnedAnalysis))
  50 + # We currently don't use unittest for test collection
  51 + test_suite.addTest(unittest.makeSuite(scripts))
48 52  
  53 + # Create a test runner and run the test
49 54 test_runner = unittest.TextTestRunner(verbosity=verbosity)
50 55 test_runner.run(test_suite)
... ...
cscripts/tests/__init__.py
1   -"""Tests that can be run for an installed ctools package.
  1 +"""
  2 +Tests that can be run for an installed cscripts package.
2 3 """
... ...
cscripts/tests/scripts.py 0 → 100644
  1 +# ==========================================================================
  2 +# This script tests the cscripts.
  3 +#
  4 +# Copyright (C) 2015 Juergen Knoedlseder
  5 +#
  6 +# This program is free software: you can redistribute it and/or modify
  7 +# it under the terms of the GNU General Public License as published by
  8 +# the Free Software Foundation, either version 3 of the License, or
  9 +# (at your option) any later version.
  10 +#
  11 +# This program is distributed in the hope that it will be useful,
  12 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 +# GNU General Public License for more details.
  15 +#
  16 +# You should have received a copy of the GNU General Public License
  17 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  18 +#
  19 +# ==========================================================================
  20 +import os
  21 +from tempfile import mkdtemp
  22 +import unittest
  23 +
  24 +
  25 +# ================== #
  26 +# Analysis pipelines #
  27 +# ================== #
  28 +class scripts(unittest.TestCase):
  29 + """
  30 + Test the cscripts.
  31 + """
  32 + # Setup of test class
  33 + def setUp(self):
  34 + gammalib_dir = os.environ['GAMMALIB']
  35 + self.model_name = os.path.join(gammalib_dir, "share/models/crab.xml")
  36 + self.workdir = mkdtemp()
  37 +
  38 + # Test cscaldb
  39 + def test_cscaldb(self):
  40 + """
  41 + Test cscaldb.
  42 + """
  43 + # Test cscaldb
  44 + #caldb = cscaldb()
  45 + #caldb["debug"] = True
  46 + #caldb.execute()
  47 +
  48 + # TODO: add asserts
  49 + assert True
... ...
cscripts/tests/test_unbinned.py deleted
1   -"""Test unbinned analysis.
2   -
3   -TODO: This is basically a duplicate of `test/test_pipelines.py`
4   - The duplication should be avoided somehow.
5   -
6   -"""
7   -import os
8   -from tempfile import mkdtemp
9   -import unittest
10   -import ctools
11   -
12   -
13   -class TestUnbinnedAnalysis(unittest.TestCase):
14   - """Test a simple unbinned analysis pipeline.
15   - """
16   -
17   - def setUp(self):
18   - gammalib_dir = os.environ['GAMMALIB']
19   - self.model_name = os.path.join(gammalib_dir, "share/models/crab.xml")
20   - self.workdir = mkdtemp()
21   -
22   - def test_unbinned_fits(self):
23   - """
24   - Test unbinned pipeline with FITS file saving.
25   - """
26   - # Set script parameters
27   - model_name = self.model_name
28   - events_name = os.path.join(self.workdir, "events.fits")
29   - selected_events_name = os.path.join(self.workdir, "selected_events.fits")
30   - result_name = os.path.join(self.workdir, "results.xml")
31   - caldb = "prod2"
32   - irf = "South_50h"
33   - ra = 83.63
34   - dec = 22.01
35   - rad_sim = 10.0
36   - tstart = 0.0
37   - tstop = 1800.0
38   - emin = 0.1
39   - emax = 100.0
40   - rad_select = 3.0
41   -
42   - # Simulate events
43   - sim = ctools.ctobssim()
44   - sim["inmodel"] = model_name
45   - sim["outevents"] = events_name
46   - sim["caldb"] = caldb
47   - sim["irf"] = irf
48   - sim["ra"] = ra
49   - sim["dec"] = dec
50   - sim["rad"] = rad_sim
51   - sim["tmin"] = tstart
52   - sim["tmax"] = tstop
53   - sim["emin"] = emin
54   - sim["emax"] = emax
55   - sim.execute()
56   -
57   - # Select events
58   - select = ctools.ctselect()
59   - select["inobs"] = events_name
60   - select["outobs"] = selected_events_name
61   - select["ra"] = ra
62   - select["dec"] = dec
63   - select["rad"] = rad_select
64   - select["tmin"] = tstart
65   - select["tmax"] = tstop
66   - select["emin"] = emin
67   - select["emax"] = emax
68   - select.execute()
69   -
70   - # Perform maximum likelihood fitting
71   - like = ctools.ctlike()
72   - like["inobs"] = selected_events_name
73   - like["inmodel"] = model_name
74   - like["outmodel"] = result_name
75   - like["caldb"] = caldb
76   - like["irf"] = irf
77   - like.execute()
78   -
79   - # TODO: add asserts
80   - assert True
81   -
82   - def test_unbinned_mem(self):
83   - """
84   - Test unbinned in-memory pipeline.
85   - """
86   - # Set script parameters
87   - model_name = self.model_name
88   - caldb = "prod2"
89   - irf = "South_50h"
90   - ra = 83.63
91   - dec = 22.01
92   - rad_sim = 10.0
93   - tstart = 0.0
94   - tstop = 1800.0
95   - emin = 0.1
96   - emax = 100.0
97   - rad_select = 3.0
98   -
99   - # Simulate events
100   - sim = ctools.ctobssim()
101   - sim["inmodel"] = model_name
102   - sim["caldb"] = caldb
103   - sim["irf"] = irf
104   - sim["ra"] = ra
105   - sim["dec"] = dec
106   - sim["rad"] = rad_sim
107   - sim["tmin"] = tstart
108   - sim["tmax"] = tstop
109   - sim["emin"] = emin
110   - sim["emax"] = emax
111   - sim.run()
112   -
113   - # Select events
114   - select = ctools.ctselect(sim.obs())
115   - select["ra"] = ra
116   - select["dec"] = dec
117   - select["rad"] = rad_select
118   - select["tmin"] = tstart
119   - select["tmax"] = tstop
120   - select["emin"] = emin
121   - select["emax"] = emax
122   - select.run()
123   -
124   - # Perform maximum likelihood fitting
125   - like = ctools.ctlike(select.obs())
126   - like.run()
127   -
128   - # TODO: add asserts
129   - assert True