test_examples.py.in
12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#! /usr/bin/env python
# ==========================================================================
# Performs unit tests for the ctools example scripts
#
# Copyright (C) 2016 Juergen Knoedlseder
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ==========================================================================
import os
import sys
import gammalib
import ctools
import cscripts
# =============================================== #
# Test class for example executables unit testing #
# =============================================== #
class Test(gammalib.GPythonTestSuite):
"""
Test class for GammaLib example executables
"""
# Constructor
def __init__(self):
"""
Constructor.
"""
# Call base class constructor
gammalib.GPythonTestSuite.__init__(self)
# Set test data directory
self._datadir = os.environ['TEST_DATA']
# Return
return
# Execute Python script
def _execute_python(self, name, args='', log=''):
"""
Execute Python script
Parameters
----------
name : str
Python script name with .py extension
args : str, optional
String with arguments to be passed to script
log : str, optional
String with name to be used for logging
"""
# Setup command
cmd = '@PYTHON_COVERAGE@@top_srcdir@/examples/' + name + '.py'
if len(args) > 0:
cmd += ' ' + args
# Set logname
if len(log) > 0:
logname = log
else:
logname = name
# Execute Python script, make sure we catch any exception
try:
rc = os.system(cmd+' > example_'+logname+'.log 2>&1')
except:
pass
# Check if execution was successful
self.test_assert(rc == 0, 'Check "'+name+'" script from command line')
# Return
return
# Set test functions
def set(self):
"""
Set all test functions
"""
# Set test name
self.name('examples')
# Append tests
self.append(self.test_generate_prod3_irfs,
'Test generate_prod3_irfs.py')
self.append(self.test_make_pointings,
'Test make_pointings.py')
self.append(self.test_make_spectrum,
'Test make_spectrum.py')
self.append(self.test_make_ts_distributions,
'Test make_ts_distributions.py')
self.append(self.test_pipeline_binned_disk,
'Test pipeline_binned_disk.py')
self.append(self.test_pipeline_binned_mem,
'Test pipeline_binned_mem.py')
self.append(self.test_pipeline_stacked_disk,
'Test pipeline_stacked_disk.py')
self.append(self.test_pipeline_stacked_mem,
'Test pipeline_stacked_mem.py')
self.append(self.test_pipeline_unbinned_disk,
'Test pipeline_unbinned_disk.py')
self.append(self.test_pipeline_unbinned_mem,
'Test pipeline_unbinned_mem.py')
self.append(self.test_show_butterfly,
'Test show_butterfly.py')
self.append(self.test_show_model,
'Test show_model.py')
self.append(self.test_show_pha,
'Test show_pha.py')
self.append(self.test_show_pull_evolution,
'Test show_pull_evolution.py')
self.append(self.test_show_pull_histogram,
'Test show_pull_histogram.py')
self.append(self.test_show_response,
'Test show_response.py')
self.append(self.test_show_sensitivity,
'Test show_sensitivity.py')
self.append(self.test_show_spectrum,
'Test show_spectrum.py')
self.append(self.test_show_ts_distribution,
'Test ts_distribution.py')
self.append(self.test_show_obs,
'Test show_obs.py')
# Return
return
# Test make_pointings
def test_make_pointings(self):
"""
Test make_pointings
"""
# Set script name
script = 'make_pointings'
# Execute script
self._execute_python(script, args='-h', log='make_pointings_h')
self._execute_python(script, args='gps', log='make_pointings_gps')
self._execute_python(script, args='gps3', log='make_pointings_gps3')
self._execute_python(script, args='extgal', log='make_pointings_extgal')
self._execute_python(script, args='gc', log='make_pointings_gc')
self._execute_python(script, args='lmc', log='make_pointings_lmc')
# Return
return
# Test generate_prod3_irfs
def test_generate_prod3_irfs(self):
"""
Test generate_prod3_irfs
"""
# Execute script
self._execute_python('generate_prod3_irfs',
args=self._datadir)
# Return
return
# Test make_spectrum
def test_make_spectrum(self):
"""
Test make_spectrum
"""
# Execute script
self._execute_python('make_spectrum',
args=self._datadir+' eps')
# Return
return
# Test make_ts_distributions
def test_make_ts_distributions(self):
"""
Test make_ts_distributions
"""
# Execute script
self._execute_python('make_ts_distributions',
args='-n 2 -e 0 -d 1800 -p '+self._datadir)
# Return
return
# Test pipeline_binned_disk
def test_pipeline_binned_disk(self):
"""
Test pipeline_binned_disk
"""
# Execute script
self._execute_python('pipeline_binned_disk', args=self._datadir)
# Return
return
# Test pipeline_binned_mem
def test_pipeline_binned_mem(self):
"""
Test pipeline_binned_mem
"""
# Execute script
self._execute_python('pipeline_binned_mem', args=self._datadir)
# Return
return
# Test pipeline_stacked_disk
def test_pipeline_stacked_disk(self):
"""
Test pipeline_stacked_disk
"""
# Execute script
self._execute_python('pipeline_stacked_disk', args=self._datadir)
# Return
return
# Test pipeline_stacked_mem
def test_pipeline_stacked_mem(self):
"""
Test pipeline_stacked_mem
"""
# Execute script
self._execute_python('pipeline_stacked_mem', args=self._datadir)
# Return
return
# Test pipeline_unbinned_disk
def test_pipeline_unbinned_disk(self):
"""
Test pipeline_unbinned_disk
"""
# Execute script
self._execute_python('pipeline_unbinned_disk', args=self._datadir)
# Return
return
# Test pipeline_unbinned_mem
def test_pipeline_unbinned_mem(self):
"""
Test pipeline_unbinned_mem
"""
# Execute script
self._execute_python('pipeline_unbinned_mem', args=self._datadir)
# Return
return
# Test show_butterfly
def test_show_butterfly(self):
"""
Test show_butterfly
"""
# Execute script
self._execute_python('show_butterfly',
args=self._datadir+'/butterfly.txt '
'example_show_butterfly.eps')
# Return
return
# Test show_model
def test_show_model(self):
"""
Test show_model
"""
# Execute script
self._execute_python('show_model',
args=self._datadir+'/crab.xml '
'Crab example_show_model.eps')
# Return
return
# Test show_pha
def test_show_pha(self):
"""
Test show_pha
"""
# Execute script
self._execute_python('show_pha',
args=self._datadir+'/pha.fits '
'example_show_pha.eps')
# Return
return
# Test show_pull_evolution
def test_show_pull_evolution(self):
"""
Test show_pull_evolution
"""
# Execute script
self._execute_python('show_pull_evolution',
args=self._datadir+'/pull.dat '
'Pull_Crab_Prefactor '
'example_show_pull_evolution.eps')
# Return
return
# Test show_pull_histogram
def test_show_pull_histogram(self):
"""
Test show_pull_histogram
"""
# Execute script
self._execute_python('show_pull_histogram',
args=self._datadir+'/pull.dat '
'Pull_Crab_Prefactor 50 '
'example_show_pull_histogram.eps')
# Return
return
# Test show_response
def test_show_response(self):
"""
Test show_response
"""
# Execute script
self._execute_python('show_response', args='example_show_response.eps')
# Return
return
# Test show_sensitivity
def test_show_sensitivity(self):
"""
Test show_sensitivity
"""
# Execute script
self._execute_python('show_sensitivity',
args=self._datadir+'/sensitivity.dat '
'example_show_sensitivity.eps')
# Return
return
# Test show_spectrum
def test_show_spectrum(self):
"""
Test show_spectrum
"""
# Execute script
self._execute_python('show_spectrum',
args=self._datadir+'/spectrum.fits '
'example_show_spectrum.eps')
# Return
return
# Test show_ts_distribution
def test_show_ts_distribution(self):
"""
Test show_ts_distribution
"""
# Execute script
self._execute_python('show_ts_distribution',
args=self._datadir+'/ts.dat -n 100 '
'-f example_show_ts_distribution.eps')
# Return
return
# Test show_obs
def test_show_obs(self):
"""
Test show_obs
"""
# Execute script
self._execute_python('show_obs',
args=self._datadir+'/obs_unbinned.xml '
'example_show_obs.eps')
# Return
return
# ======================== #
# Main routine entry point #
# ======================== #
if __name__ == '__main__':
# Set calibration database
os.environ['CALDB'] = '%s/caldb' % (os.environ['TEST_SRCDIR'])
# Set PFILES environment variable
try:
os.mkdir('pfiles')
except:
pass
os.environ['PFILES'] = 'pfiles'
# Copy over pfiles
os.system('cp -r %s/src/*/*.par pfiles/' % (os.environ['TEST_SRCDIR']))
os.system('cp -r %s/cscripts/*.par pfiles/' % (os.environ['TEST_SRCDIR']))
# Allocate test suites
suites = gammalib.GTestSuites('Examples testing')
# Allocate test suite, setup tests and append them to the container
suite = Test()
suite.set()
suites.append(suite)
# Run test suite
success = suites.run()
# Save test results
suites.save('reports/examples.xml')
# Set return code
if success:
rc = 0
else:
rc = 1
# Exit with return code
sys.exit(rc)