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 # Scripts to be distributed 3 # Scripts to be distributed
4 scripts = README.md \ 4 scripts = README.md \
examples/make_ts_distributions.py
1 #! /usr/bin/env python 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 # This program is free software: you can redistribute it and/or modify 7 # This program is free software: you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by 8 # it under the terms of the GNU General Public License as published by
@@ -30,15 +20,14 @@ @@ -30,15 +20,14 @@
30 # ========================================================================== 20 # ==========================================================================
31 import sys 21 import sys
32 import time 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 try: 26 try:
38 import processing 27 import processing
39 has_processing = True 28 has_processing = True
40 except: 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,35 +38,45 @@ def create_ts(loge, emin, emax, ntrials=100, duration=180000.0,
49 """ 38 """
50 Create TS distribution. 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 # Generate output filename 58 # Generate output filename
60 - outfile = "ts_"+str(loge)+".dat" 59 + outfile = 'ts_'+str(loge)+'.dat'
61 60
62 # Setup cstsdist tool 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 # Optionally open the log file 81 # Optionally open the log file
83 if log: 82 if log:
@@ -96,7 +95,7 @@ def create_ts(loge, emin, emax, ntrials=100, duration=180000.0, @@ -96,7 +95,7 @@ def create_ts(loge, emin, emax, ntrials=100, duration=180000.0,
96 if __name__ == '__main__': 95 if __name__ == '__main__':
97 96
98 # Get input arguments 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 if len(sys.argv) < 1: 99 if len(sys.argv) < 1:
101 print(usage) 100 print(usage)
102 sys.exit() 101 sys.exit()
@@ -111,7 +110,7 @@ if __name__ == &#39;__main__&#39;: @@ -111,7 +110,7 @@ if __name__ == &#39;__main__&#39;:
111 pars = [{'option': '-n', 'value': ntrials}, 110 pars = [{'option': '-n', 'value': ntrials},
112 {'option': '-e', 'value': enumbins}, 111 {'option': '-e', 'value': enumbins},
113 {'option': '-d', 'value': duration}, 112 {'option': '-d', 'value': duration},
114 - {'option': '-m', 'value': max_threads}] 113 + {'option': '-m', 'value': max_threads}]
115 114
116 # Gather parameters from command line 115 # Gather parameters from command line
117 i = 1 116 i = 1
@@ -121,7 +120,7 @@ if __name__ == &#39;__main__&#39;: @@ -121,7 +120,7 @@ if __name__ == &#39;__main__&#39;:
121 for par in pars: 120 for par in pars:
122 if sys.argv[i] == par['option']: 121 if sys.argv[i] == par['option']:
123 if len(sys.argv) > i+1: 122 if len(sys.argv) > i+1:
124 - i += 1 123 + i += 1
125 try: 124 try:
126 par['value'] = int(sys.argv[i]) 125 par['value'] = int(sys.argv[i])
127 except: 126 except:
@@ -150,11 +149,12 @@ if __name__ == &#39;__main__&#39;: @@ -150,11 +149,12 @@ if __name__ == &#39;__main__&#39;:
150 emin = pow(10.0, loge-0.1) 149 emin = pow(10.0, loge-0.1)
151 emax = pow(10.0, loge+0.1) 150 emax = pow(10.0, loge+0.1)
152 if loge < 0: 151 if loge < 0:
153 - loge = "m"+str(abs(loge)) 152 + loge = 'm'+str(abs(loge))
154 else: 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 if has_processing: 158 if has_processing:
159 159
160 # Wait until one thread has finished 160 # Wait until one thread has finished
@@ -169,19 +169,17 @@ if __name__ == &#39;__main__&#39;: @@ -169,19 +169,17 @@ if __name__ == &#39;__main__&#39;:
169 # Generate pull distribution 169 # Generate pull distribution
170 p = processing.Process(target=create_ts, args=args, kwargs=kwargs) 170 p = processing.Process(target=create_ts, args=args, kwargs=kwargs)
171 p.start() 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 # Wait a short time to allow process to start 174 # Wait a short time to allow process to start
175 time.sleep(1) 175 time.sleep(1)
176 176
177 - # ... no 177 + # ... otherwise simply start a single process
178 else: 178 else:
179 create_ts(loge, emin, emax, ntrials=ntrials, enumbins=enumbins, 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 if has_processing: 183 if has_processing:
184 -  
185 - # Wait until all threads finished  
186 while len(processing.activeChildren()) > 0: 184 while len(processing.activeChildren()) > 0:
187 time.sleep(10) 185 time.sleep(10)
test/test_examples.py
@@ -71,7 +71,6 @@ class Test(gammalib.GPythonTestSuite): @@ -71,7 +71,6 @@ class Test(gammalib.GPythonTestSuite):
71 # Execute Python script, make sure we catch any exception 71 # Execute Python script, make sure we catch any exception
72 try: 72 try:
73 rc = os.system(cmd+' > example_'+logname+'.log 2>&1') 73 rc = os.system(cmd+' > example_'+logname+'.log 2>&1')
74 - #rc = os.system(cmd)  
75 except: 74 except:
76 pass 75 pass
77 76
@@ -93,6 +92,7 @@ class Test(gammalib.GPythonTestSuite): @@ -93,6 +92,7 @@ class Test(gammalib.GPythonTestSuite):
93 self.append(self.test_generate_prod3_irfs, 'Test generate_prod3_irfs') 92 self.append(self.test_generate_prod3_irfs, 'Test generate_prod3_irfs')
94 self.append(self.test_make_pointings, 'Test make_pointings') 93 self.append(self.test_make_pointings, 'Test make_pointings')
95 self.append(self.test_make_spectrum, 'Test make_spectrum') 94 self.append(self.test_make_spectrum, 'Test make_spectrum')
  95 + self.append(self.test_make_ts_distributions, 'Test make_ts_distributions')
96 self.append(self.test_pipeline_binned_disk, 'Test pipeline_binned_disk') 96 self.append(self.test_pipeline_binned_disk, 'Test pipeline_binned_disk')
97 self.append(self.test_pipeline_binned_mem, 'Test pipeline_binned_mem') 97 self.append(self.test_pipeline_binned_mem, 'Test pipeline_binned_mem')
98 self.append(self.test_pipeline_stacked_disk, 'Test pipeline_stacked_disk') 98 self.append(self.test_pipeline_stacked_disk, 'Test pipeline_stacked_disk')
@@ -124,6 +124,19 @@ class Test(gammalib.GPythonTestSuite): @@ -124,6 +124,19 @@ class Test(gammalib.GPythonTestSuite):
124 # Return 124 # Return
125 return 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 # Test make_spectrum 140 # Test make_spectrum
128 def test_make_spectrum(self): 141 def test_make_spectrum(self):
129 """ 142 """
@@ -137,13 +150,13 @@ class Test(gammalib.GPythonTestSuite): @@ -137,13 +150,13 @@ class Test(gammalib.GPythonTestSuite):
137 # Return 150 # Return
138 return 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 # Execute script 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 #TODO: Do any testing 161 #TODO: Do any testing
149 162