diff --git a/ChangeLog b/ChangeLog index b81d059..2b22b60 100644 --- a/ChangeLog +++ b/ChangeLog @@ -4,6 +4,7 @@ ========================= Extend cspull to stacked analysis (#1503) + Add tests for installed ctools version (#1486) Create cscripts Python module, move obsutils in that module (#1470) Add csinfo script (#1489) Removed obsutils.spectrum() function (replaced by csspec) diff --git a/NEWS b/NEWS index 87a32da..865874a 100644 --- a/NEWS +++ b/NEWS @@ -22,13 +22,18 @@ None A specific cscripts Python module has been created for cscripts. This module can be imported using - import cscripts + >>> import cscripts The obsutils utility functions have also been moved into the scripts folder. The obsutils.spectrum() function has been removed as it is now replaced by the csspec script. +Some tests for the installed ctools package have been added. They can be +executed by typing the following under Python: + + >>> cscripts.test() + 4. Support model ---------------- diff --git a/cscripts/Makefile.am b/cscripts/Makefile.am index 0a456ec..4aa0fda 100644 --- a/cscripts/Makefile.am +++ b/cscripts/Makefile.am @@ -5,24 +5,30 @@ # tsmerge.py binscripts = tsmerge.py -# Scripts to be distributed in pythondir +# Scripts to be distributed in $(pythondir)/cscripts pythonscripts = __init__.py \ cspull.py cssens.py cstsdist.py cscaldb.py csresmap.py \ cshessobs.py csobsdef.py csspec.py cslightcrv.py \ csinfo.py \ obsutils.py +# Test scripts to be distributed in $(pythondir)/cscripts/tests +testscripts = tests/__init__.py \ + tests/test_unbinned.py + # Scripts for which symbolic links will be created in bindir linkscripts = cspull cssens cstsdist cscaldb csresmap \ cshessobs csobsdef csspec cslightcrv \ csinfo # Distribute scripts -EXTRA_DIST = $(binscripts) $(pythonscripts) +EXTRA_DIST = $(binscripts) $(pythonscripts) $(testscripts) binscriptsdir = $(bindir) binscripts_DATA = $(binscripts) pythonscriptsdir = $(pythondir)/cscripts pythonscripts_DATA = $(pythonscripts) +testscriptsdir = $(pythondir)/cscripts/tests +testscripts_DATA = $(testscripts) # Add hook rule to create symbolic links for cscripts install-data-hook: diff --git a/cscripts/__init__.py b/cscripts/__init__.py index 56aa7ae..e84c830 100644 --- a/cscripts/__init__.py +++ b/cscripts/__init__.py @@ -1,15 +1,24 @@ +"""cscripts --- Python scripts from the ctools package. + +http://cta.irap.omp.eu/ctools/ +""" + + # List all scripts in this module -__all__ = ["cscaldb", \ - "cshessobs", \ - "csinfo", \ - "cslightcrv", \ - "csobsdef", \ - "cspull", \ - "csresmap", \ - "cssens", \ - "csspec", \ - "cstsdist", \ - "obsutils"] +__all__ = [ + "cscaldb", + "cshessobs", + "csinfo", + "cslightcrv", + "csobsdef", + "cspull", + "csresmap", + "cssens", + "csspec", + "cstsdist", + "obsutils", + "test", +] # Import from .cscaldb import cscaldb @@ -22,4 +31,20 @@ from .csresmap import csresmap from .cssens import cssens from .csspec import csspec from .cstsdist import cstsdist -import obsutils \ No newline at end of file +from . import obsutils + + +def test(verbosity=2): + """Run ctools tests. + """ + import unittest + from .tests.test_unbinned import TestUnbinnedAnalysis + + 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)) + + test_runner = unittest.TextTestRunner(verbosity=verbosity) + test_runner.run(test_suite) diff --git a/cscripts/tests/__init__.py b/cscripts/tests/__init__.py new file mode 100644 index 0000000..24b20ee --- /dev/null +++ b/cscripts/tests/__init__.py @@ -0,0 +1,2 @@ +"""Tests that can be run for an installed ctools package. +""" diff --git a/cscripts/tests/test_unbinned.py b/cscripts/tests/test_unbinned.py new file mode 100644 index 0000000..99ae35c --- /dev/null +++ b/cscripts/tests/test_unbinned.py @@ -0,0 +1,129 @@ +"""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