Commit a9c1de331646e47964e72ce43eeb1b1ac69b2608
Merge branch 'ctools-test-function' into integration
Conflicts: ChangeLog
Showing
6 changed files
with
183 additions
and
15 deletions
ChangeLog
| ... | ... | @@ -4,6 +4,7 @@ |
| 4 | 4 | ========================= |
| 5 | 5 | |
| 6 | 6 | Extend cspull to stacked analysis (#1503) |
| 7 | + Add tests for installed ctools version (#1486) | |
| 7 | 8 | Create cscripts Python module, move obsutils in that module (#1470) |
| 8 | 9 | Add csinfo script (#1489) |
| 9 | 10 | Removed obsutils.spectrum() function (replaced by csspec) | ... | ... |
NEWS
| ... | ... | @@ -22,13 +22,18 @@ None |
| 22 | 22 | A specific cscripts Python module has been created for cscripts. This module |
| 23 | 23 | can be imported using |
| 24 | 24 | |
| 25 | - import cscripts | |
| 25 | + >>> import cscripts | |
| 26 | 26 | |
| 27 | 27 | The obsutils utility functions have also been moved into the scripts folder. |
| 28 | 28 | |
| 29 | 29 | The obsutils.spectrum() function has been removed as it is now replaced by the |
| 30 | 30 | csspec script. |
| 31 | 31 | |
| 32 | +Some tests for the installed ctools package have been added. They can be | |
| 33 | +executed by typing the following under Python: | |
| 34 | + | |
| 35 | + >>> cscripts.test() | |
| 36 | + | |
| 32 | 37 | |
| 33 | 38 | 4. Support model |
| 34 | 39 | ---------------- | ... | ... |
cscripts/Makefile.am
| ... | ... | @@ -5,24 +5,30 @@ |
| 5 | 5 | # tsmerge.py |
| 6 | 6 | binscripts = tsmerge.py |
| 7 | 7 | |
| 8 | -# Scripts to be distributed in pythondir | |
| 8 | +# Scripts to be distributed in $(pythondir)/cscripts | |
| 9 | 9 | pythonscripts = __init__.py \ |
| 10 | 10 | cspull.py cssens.py cstsdist.py cscaldb.py csresmap.py \ |
| 11 | 11 | cshessobs.py csobsdef.py csspec.py cslightcrv.py \ |
| 12 | 12 | csinfo.py \ |
| 13 | 13 | obsutils.py |
| 14 | 14 | |
| 15 | +# Test scripts to be distributed in $(pythondir)/cscripts/tests | |
| 16 | +testscripts = tests/__init__.py \ | |
| 17 | + tests/test_unbinned.py | |
| 18 | + | |
| 15 | 19 | # Scripts for which symbolic links will be created in bindir |
| 16 | 20 | linkscripts = cspull cssens cstsdist cscaldb csresmap \ |
| 17 | 21 | cshessobs csobsdef csspec cslightcrv \ |
| 18 | 22 | csinfo |
| 19 | 23 | |
| 20 | 24 | # Distribute scripts |
| 21 | -EXTRA_DIST = $(binscripts) $(pythonscripts) | |
| 25 | +EXTRA_DIST = $(binscripts) $(pythonscripts) $(testscripts) | |
| 22 | 26 | binscriptsdir = $(bindir) |
| 23 | 27 | binscripts_DATA = $(binscripts) |
| 24 | 28 | pythonscriptsdir = $(pythondir)/cscripts |
| 25 | 29 | pythonscripts_DATA = $(pythonscripts) |
| 30 | +testscriptsdir = $(pythondir)/cscripts/tests | |
| 31 | +testscripts_DATA = $(testscripts) | |
| 26 | 32 | |
| 27 | 33 | # Add hook rule to create symbolic links for cscripts |
| 28 | 34 | install-data-hook: | ... | ... |
cscripts/__init__.py
| 1 | +"""cscripts --- Python scripts from the ctools package. | |
| 2 | + | |
| 3 | +http://cta.irap.omp.eu/ctools/ | |
| 4 | +""" | |
| 5 | + | |
| 6 | + | |
| 1 | 7 | # List all scripts in this module |
| 2 | -__all__ = ["cscaldb", \ | |
| 3 | - "cshessobs", \ | |
| 4 | - "csinfo", \ | |
| 5 | - "cslightcrv", \ | |
| 6 | - "csobsdef", \ | |
| 7 | - "cspull", \ | |
| 8 | - "csresmap", \ | |
| 9 | - "cssens", \ | |
| 10 | - "csspec", \ | |
| 11 | - "cstsdist", \ | |
| 12 | - "obsutils"] | |
| 8 | +__all__ = [ | |
| 9 | + "cscaldb", | |
| 10 | + "cshessobs", | |
| 11 | + "csinfo", | |
| 12 | + "cslightcrv", | |
| 13 | + "csobsdef", | |
| 14 | + "cspull", | |
| 15 | + "csresmap", | |
| 16 | + "cssens", | |
| 17 | + "csspec", | |
| 18 | + "cstsdist", | |
| 19 | + "obsutils", | |
| 20 | + "test", | |
| 21 | +] | |
| 13 | 22 | |
| 14 | 23 | # Import |
| 15 | 24 | from .cscaldb import cscaldb |
| ... | ... | @@ -22,4 +31,20 @@ from .csresmap import csresmap |
| 22 | 31 | from .cssens import cssens |
| 23 | 32 | from .csspec import csspec |
| 24 | 33 | from .cstsdist import cstsdist |
| 25 | -import obsutils | |
| 26 | 34 | \ No newline at end of file |
| 35 | +from . import obsutils | |
| 36 | + | |
| 37 | + | |
| 38 | +def test(verbosity=2): | |
| 39 | + """Run ctools tests. | |
| 40 | + """ | |
| 41 | + import unittest | |
| 42 | + from .tests.test_unbinned import TestUnbinnedAnalysis | |
| 43 | + | |
| 44 | + test_suite = unittest.TestSuite() | |
| 45 | + | |
| 46 | + # Test cases must be listed manually here ... | |
| 47 | + # we currently don't use unittest for test collection | |
| 48 | + test_suite.addTest(unittest.makeSuite(TestUnbinnedAnalysis)) | |
| 49 | + | |
| 50 | + test_runner = unittest.TextTestRunner(verbosity=verbosity) | |
| 51 | + test_runner.run(test_suite) | ... | ... |
cscripts/tests/__init__.py
0 → 100644
cscripts/tests/test_unbinned.py
0 → 100644
| 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 | ... | ... |