From 1d60a87b2a08f7b420ef52f38b068aadd61bc143 Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Sat, 17 Oct 2015 01:18:39 +0200 Subject: [PATCH] Update cscripts unit test. --- cscripts/Makefile.am | 2 +- cscripts/__init__.py | 17 +++++++++++------ cscripts/tests/__init__.py | 3 ++- cscripts/tests/scripts.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ cscripts/tests/test_unbinned.py | 129 --------------------------------------------------------------------------------------------------------------------------------- 5 files changed, 63 insertions(+), 137 deletions(-) create mode 100644 cscripts/tests/scripts.py delete mode 100644 cscripts/tests/test_unbinned.py diff --git a/cscripts/Makefile.am b/cscripts/Makefile.am index 4aa0fda..593409f 100644 --- a/cscripts/Makefile.am +++ b/cscripts/Makefile.am @@ -14,7 +14,7 @@ pythonscripts = __init__.py \ # Test scripts to be distributed in $(pythondir)/cscripts/tests testscripts = tests/__init__.py \ - tests/test_unbinned.py + tests/scripts.py # Scripts for which symbolic links will be created in bindir linkscripts = cspull cssens cstsdist cscaldb csresmap \ diff --git a/cscripts/__init__.py b/cscripts/__init__.py index e84c830..12a516c 100644 --- a/cscripts/__init__.py +++ b/cscripts/__init__.py @@ -1,4 +1,5 @@ -"""cscripts --- Python scripts from the ctools package. +""" +cscripts --- Python scripts from the ctools package. http://cta.irap.omp.eu/ctools/ """ @@ -33,18 +34,22 @@ from .csspec import csspec from .cstsdist import cstsdist from . import obsutils - +# Add test function def test(verbosity=2): - """Run ctools tests. """ + Run cscripts tests. + """ + # Imports import unittest - from .tests.test_unbinned import TestUnbinnedAnalysis + from .tests.scripts import scripts + # Create a test suite test_suite = unittest.TestSuite() # Test cases must be listed manually here ... - # we currently don't use unittest for test collection - test_suite.addTest(unittest.makeSuite(TestUnbinnedAnalysis)) + # We currently don't use unittest for test collection + test_suite.addTest(unittest.makeSuite(scripts)) + # Create a test runner and run the test test_runner = unittest.TextTestRunner(verbosity=verbosity) test_runner.run(test_suite) diff --git a/cscripts/tests/__init__.py b/cscripts/tests/__init__.py index 24b20ee..6347c4f 100644 --- a/cscripts/tests/__init__.py +++ b/cscripts/tests/__init__.py @@ -1,2 +1,3 @@ -"""Tests that can be run for an installed ctools package. +""" +Tests that can be run for an installed cscripts package. """ diff --git a/cscripts/tests/scripts.py b/cscripts/tests/scripts.py new file mode 100644 index 0000000..4f029a0 --- /dev/null +++ b/cscripts/tests/scripts.py @@ -0,0 +1,49 @@ +# ========================================================================== +# This script tests the cscripts. +# +# Copyright (C) 2015 Juergen Knoedlseder +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# ========================================================================== +import os +from tempfile import mkdtemp +import unittest + + +# ================== # +# Analysis pipelines # +# ================== # +class scripts(unittest.TestCase): + """ + Test the cscripts. + """ + # Setup of test class + def setUp(self): + gammalib_dir = os.environ['GAMMALIB'] + self.model_name = os.path.join(gammalib_dir, "share/models/crab.xml") + self.workdir = mkdtemp() + + # Test cscaldb + def test_cscaldb(self): + """ + Test cscaldb. + """ + # Test cscaldb + #caldb = cscaldb() + #caldb["debug"] = True + #caldb.execute() + + # TODO: add asserts + assert True diff --git a/cscripts/tests/test_unbinned.py b/cscripts/tests/test_unbinned.py deleted file mode 100644 index 99ae35c..0000000 --- a/cscripts/tests/test_unbinned.py +++ /dev/null @@ -1,129 +0,0 @@ -"""Test unbinned analysis. - -TODO: This is basically a duplicate of `test/test_pipelines.py` - The duplication should be avoided somehow. - -""" -import os -from tempfile import mkdtemp -import unittest -import ctools - - -class TestUnbinnedAnalysis(unittest.TestCase): - """Test a simple unbinned analysis pipeline. - """ - - def setUp(self): - gammalib_dir = os.environ['GAMMALIB'] - self.model_name = os.path.join(gammalib_dir, "share/models/crab.xml") - self.workdir = mkdtemp() - - def test_unbinned_fits(self): - """ - Test unbinned pipeline with FITS file saving. - """ - # Set script parameters - model_name = self.model_name - events_name = os.path.join(self.workdir, "events.fits") - selected_events_name = os.path.join(self.workdir, "selected_events.fits") - result_name = os.path.join(self.workdir, "results.xml") - caldb = "prod2" - irf = "South_50h" - ra = 83.63 - dec = 22.01 - rad_sim = 10.0 - tstart = 0.0 - tstop = 1800.0 - emin = 0.1 - emax = 100.0 - rad_select = 3.0 - - # Simulate events - sim = ctools.ctobssim() - sim["inmodel"] = model_name - sim["outevents"] = events_name - sim["caldb"] = caldb - sim["irf"] = irf - sim["ra"] = ra - sim["dec"] = dec - sim["rad"] = rad_sim - sim["tmin"] = tstart - sim["tmax"] = tstop - sim["emin"] = emin - sim["emax"] = emax - sim.execute() - - # Select events - select = ctools.ctselect() - select["inobs"] = events_name - select["outobs"] = selected_events_name - select["ra"] = ra - select["dec"] = dec - select["rad"] = rad_select - select["tmin"] = tstart - select["tmax"] = tstop - select["emin"] = emin - select["emax"] = emax - select.execute() - - # Perform maximum likelihood fitting - like = ctools.ctlike() - like["inobs"] = selected_events_name - like["inmodel"] = model_name - like["outmodel"] = result_name - like["caldb"] = caldb - like["irf"] = irf - like.execute() - - # TODO: add asserts - assert True - - def test_unbinned_mem(self): - """ - Test unbinned in-memory pipeline. - """ - # Set script parameters - model_name = self.model_name - caldb = "prod2" - irf = "South_50h" - ra = 83.63 - dec = 22.01 - rad_sim = 10.0 - tstart = 0.0 - tstop = 1800.0 - emin = 0.1 - emax = 100.0 - rad_select = 3.0 - - # Simulate events - sim = ctools.ctobssim() - sim["inmodel"] = model_name - sim["caldb"] = caldb - sim["irf"] = irf - sim["ra"] = ra - sim["dec"] = dec - sim["rad"] = rad_sim - sim["tmin"] = tstart - sim["tmax"] = tstop - sim["emin"] = emin - sim["emax"] = emax - sim.run() - - # Select events - select = ctools.ctselect(sim.obs()) - select["ra"] = ra - select["dec"] = dec - select["rad"] = rad_select - select["tmin"] = tstart - select["tmax"] = tstop - select["emin"] = emin - select["emax"] = emax - select.run() - - # Perform maximum likelihood fitting - like = ctools.ctlike(select.obs()) - like.run() - - # TODO: add asserts - assert True -- libgit2 0.21.4