Commit 47f5646116cc567b28a6d3a729d19b8725b20848

Authored by Christoph Deil
1 parent b7096783

Implement unbinned pipeline tests

cscripts/__init__.py
... ... @@ -34,19 +34,17 @@ from .cstsdist import cstsdist
34 34 from . import obsutils
35 35  
36 36  
37   -def test():
  37 +def test(verbosity=2):
38 38 """Run ctools tests.
39 39 """
40 40 import unittest
41 41 from .tests.test_unbinned import TestUnbinnedAnalysis
42   - from .tests.test_binned import TestBinnedAnalysis
43   - # suite = unittest.TestLoader().loadTestsFromTestCase(TestUnbinnedAnalysis)
44   - # unittest.TextTestRunner(verbosity=2).run(suite)
  42 +
45 43 test_suite = unittest.TestSuite()
46 44  
47 45 # Test cases must be listed manually here ...
48 46 # we currently don't use unittest for test collection
49 47 test_suite.addTest(unittest.makeSuite(TestUnbinnedAnalysis))
50   - test_suite.addTest(unittest.makeSuite(TestBinnedAnalysis))
51 48  
52   - unittest.TextTestRunner(verbosity=2).run(test_suite)
  49 + test_runner = unittest.TextTestRunner(verbosity=verbosity)
  50 + test_runner.run(test_suite)
... ...
cscripts/tests/test_binned.py deleted
1   -"""Test binned analysis.
2   -"""
3   -import unittest
4   -
5   -class TestBinnedAnalysis(unittest.TestCase):
6   -
7   - def test_it(self):
8   - self.assertEqual('foo'.upper(), 'FOO')
cscripts/tests/test_unbinned.py
1 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 +
2 6 """
  7 +import os
  8 +from tempfile import mkdtemp
3 9 import unittest
  10 +import ctools
  11 +
4 12  
5 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()
6 127  
7   - def test_it(self):
8   - self.assertEqual('foo'.upper(), 'FOO')
  128 + # TODO: add asserts
  129 + assert True
... ...