Commit ecd46cb4a92db3f2f17e1bb643b1c4e7214ddff6

Authored by Jürgen Knödlseder
1 parent 38a54b73

Add make_ts_distributions.py script to example unit tests.

examples/Makefile.am
1   -..## Process this file with automake to produce Makefile.in
  1 +## Process this file with automake to produce Makefile.in
2 2  
3 3 # Scripts to be distributed
4 4 scripts = README.md \
... ...
examples/make_ts_distributions.py
1 1 #! /usr/bin/env python
2 2 # ==========================================================================
3   -# This script generates TS distributions for the standard energy bands that
4   -# are also used for sensitivity computation. The script makes use of the
5   -# "processing" Python module, if available, that can be used for parallel
6   -# computing on multiple cores/CPUs.
  3 +# Generate TS distribution
7 4 #
8   -# This script provides an illustration of how a ctools or cscript can be
9   -# used from within Python. It further illustrates the possibility of
10   -# parallel computing using the "processing" Python module.
11   -#
12   -# Required 3rd party modules:
13   -# - processing (optional)
14   -#
15   -# Copyright (C) 2011-2015 Jurgen Knodlseder
  5 +# Copyright (C) 2011-2016 Jurgen Knodlseder
16 6 #
17 7 # This program is free software: you can redistribute it and/or modify
18 8 # it under the terms of the GNU General Public License as published by
... ... @@ -30,15 +20,14 @@
30 20 # ==========================================================================
31 21 import sys
32 22 import time
33   -from cscripts import cstsdist
  23 +import cscripts
34 24  
35   -# Try importing processing module
36   -has_processing = False
  25 +# Optionally import processing module
37 26 try:
38 27 import processing
39 28 has_processing = True
40 29 except:
41   - pass
  30 + has_processing = False
42 31  
43 32  
44 33 # ====================== #
... ... @@ -49,35 +38,45 @@ def create_ts(loge, emin, emax, ntrials=100, duration=180000.0,
49 38 """
50 39 Create TS distribution.
51 40  
52   - Parameters:
53   - loge - Logarithm of mean energy in TeV
54   - emin - Minimum energy (TeV)
55   - emax - Maximum energy (TeV)
56   - Keywords:
57   - log - Create log file(s)
  41 + Parameters
  42 + ----------
  43 + loge : float
  44 + Logarithm of mean energy (TeV)
  45 + emin : float
  46 + Minimum energy (TeV)
  47 + emax : float
  48 + Maximum energy (TeV)
  49 + ntrials : int, optional
  50 + Number of MC samples
  51 + durations : float, optional
  52 + Observation duration (s)
  53 + enumbins : int, optional
  54 + Number of energy bins
  55 + log : bool, optional
  56 + Create log file(s)
58 57 """
59 58 # Generate output filename
60   - outfile = "ts_"+str(loge)+".dat"
  59 + outfile = 'ts_'+str(loge)+'.dat'
61 60  
62 61 # Setup cstsdist tool
63   - tsdist = cstsdist()
64   - tsdist["inmodel"] = "$CTOOLS/share/models/crab.xml"
65   - tsdist["srcname"] = "Crab"
66   - tsdist["outfile"] = outfile
67   - tsdist["ntrials"] = ntrials
68   - tsdist["caldb"] = "prod2"
69   - tsdist["irf"] = "South_50h"
70   - tsdist["ra"] = 83.63
71   - tsdist["dec"] = 22.01
72   - tsdist["emin"] = emin
73   - tsdist["emax"] = emax
74   - tsdist["enumbins"] = enumbins
75   - tsdist["tmin"] = 0
76   - tsdist["tmax"] = duration
77   - tsdist["rad"] = 5.0
78   - tsdist["npix"] = 200
79   - tsdist["binsz"] = 0.05
80   - #tsdist["debug"].boolean(True)
  62 + tsdist = cscripts.cstsdist()
  63 + tsdist['inmodel'] = 'data/crab.xml'
  64 + tsdist['srcname'] = 'Crab'
  65 + tsdist['outfile'] = outfile
  66 + tsdist['ntrials'] = ntrials
  67 + tsdist['caldb'] = 'prod2'
  68 + tsdist['irf'] = 'South_0.5h'
  69 + tsdist['ra'] = 83.63
  70 + tsdist['dec'] = 22.01
  71 + tsdist['emin'] = emin
  72 + tsdist['emax'] = emax
  73 + tsdist['enumbins'] = enumbins
  74 + tsdist['tmin'] = 0
  75 + tsdist['tmax'] = duration
  76 + tsdist['rad'] = 5.0
  77 + tsdist['npix'] = 200
  78 + tsdist['binsz'] = 0.05
  79 + #tsdist['debug'] = True
81 80  
82 81 # Optionally open the log file
83 82 if log:
... ... @@ -96,7 +95,7 @@ def create_ts(loge, emin, emax, ntrials=100, duration=180000.0,
96 95 if __name__ == '__main__':
97 96  
98 97 # Get input arguments
99   - usage = "make_ts_distributions [-n ntrials] [-e enumbins] [-m max_threads]"
  98 + usage = 'make_ts_distributions [-n ntrials] [-e enumbins] [-m max_threads]'
100 99 if len(sys.argv) < 1:
101 100 print(usage)
102 101 sys.exit()
... ... @@ -111,7 +110,7 @@ if __name__ == &#39;__main__&#39;:
111 110 pars = [{'option': '-n', 'value': ntrials},
112 111 {'option': '-e', 'value': enumbins},
113 112 {'option': '-d', 'value': duration},
114   - {'option': '-m', 'value': max_threads}]
  113 + {'option': '-m', 'value': max_threads}]
115 114  
116 115 # Gather parameters from command line
117 116 i = 1
... ... @@ -121,7 +120,7 @@ if __name__ == &#39;__main__&#39;:
121 120 for par in pars:
122 121 if sys.argv[i] == par['option']:
123 122 if len(sys.argv) > i+1:
124   - i += 1
  123 + i += 1
125 124 try:
126 125 par['value'] = int(sys.argv[i])
127 126 except:
... ... @@ -150,11 +149,12 @@ if __name__ == &#39;__main__&#39;:
150 149 emin = pow(10.0, loge-0.1)
151 150 emax = pow(10.0, loge+0.1)
152 151 if loge < 0:
153   - loge = "m"+str(abs(loge))
  152 + loge = 'm'+str(abs(loge))
154 153 else:
155   - loge = "p"+str(abs(loge))
  154 + loge = 'p'+str(abs(loge))
156 155  
157   - # Processing support?
  156 + # If processing support is available then start max_threads processes
  157 + # in parallel
158 158 if has_processing:
159 159  
160 160 # Wait until one thread has finished
... ... @@ -169,19 +169,17 @@ if __name__ == &#39;__main__&#39;:
169 169 # Generate pull distribution
170 170 p = processing.Process(target=create_ts, args=args, kwargs=kwargs)
171 171 p.start()
172   - print("Process emin=%.4f emax=%.4f started." % (emin, emax))
  172 + print('Process emin=%.4f emax=%.4f started.' % (emin, emax))
173 173  
174 174 # Wait a short time to allow process to start
175 175 time.sleep(1)
176 176  
177   - # ... no
  177 + # ... otherwise simply start a single process
178 178 else:
179 179 create_ts(loge, emin, emax, ntrials=ntrials, enumbins=enumbins,
180   - duration=duration)
  180 + duration=duration)
181 181  
182   - # Processing support
  182 + # If processing support is available then wait until all threads finished
183 183 if has_processing:
184   -
185   - # Wait until all threads finished
186 184 while len(processing.activeChildren()) > 0:
187 185 time.sleep(10)
... ...
test/test_examples.py
... ... @@ -71,7 +71,6 @@ class Test(gammalib.GPythonTestSuite):
71 71 # Execute Python script, make sure we catch any exception
72 72 try:
73 73 rc = os.system(cmd+' > example_'+logname+'.log 2>&1')
74   - #rc = os.system(cmd)
75 74 except:
76 75 pass
77 76  
... ... @@ -93,6 +92,7 @@ class Test(gammalib.GPythonTestSuite):
93 92 self.append(self.test_generate_prod3_irfs, 'Test generate_prod3_irfs')
94 93 self.append(self.test_make_pointings, 'Test make_pointings')
95 94 self.append(self.test_make_spectrum, 'Test make_spectrum')
  95 + self.append(self.test_make_ts_distributions, 'Test make_ts_distributions')
96 96 self.append(self.test_pipeline_binned_disk, 'Test pipeline_binned_disk')
97 97 self.append(self.test_pipeline_binned_mem, 'Test pipeline_binned_mem')
98 98 self.append(self.test_pipeline_stacked_disk, 'Test pipeline_stacked_disk')
... ... @@ -124,6 +124,19 @@ class Test(gammalib.GPythonTestSuite):
124 124 # Return
125 125 return
126 126  
  127 + # Test generate_prod3_irfs
  128 + def test_generate_prod3_irfs(self):
  129 + """
  130 + Test generate_prod3_irfs
  131 + """
  132 + # Execute script
  133 + self._execute_python('generate_prod3_irfs')
  134 +
  135 + #TODO: Do any testing
  136 +
  137 + # Return
  138 + return
  139 +
127 140 # Test make_spectrum
128 141 def test_make_spectrum(self):
129 142 """
... ... @@ -137,13 +150,13 @@ class Test(gammalib.GPythonTestSuite):
137 150 # Return
138 151 return
139 152  
140   - # Test generate_prod3_irfs
141   - def test_generate_prod3_irfs(self):
  153 + # Test make_ts_distributions
  154 + def test_make_ts_distributions(self):
142 155 """
143   - Test generate_prod3_irfs
  156 + Test make_ts_distributions
144 157 """
145 158 # Execute script
146   - self._execute_python('generate_prod3_irfs')
  159 + self._execute_python('make_ts_distributions', args='-n 2 -e 0 -d 1800')
147 160  
148 161 #TODO: Do any testing
149 162  
... ...