From ecd46cb4a92db3f2f17e1bb643b1c4e7214ddff6 Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Sat, 25 Jun 2016 00:36:55 +0200 Subject: [PATCH] Add make_ts_distributions.py script to example unit tests. --- examples/Makefile.am | 2 +- examples/make_ts_distributions.py | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------- test/test_examples.py | 23 ++++++++++++++++++----- 3 files changed, 70 insertions(+), 59 deletions(-) diff --git a/examples/Makefile.am b/examples/Makefile.am index 7da370e..6bb953d 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -1,4 +1,4 @@ -..## Process this file with automake to produce Makefile.in +## Process this file with automake to produce Makefile.in # Scripts to be distributed scripts = README.md \ diff --git a/examples/make_ts_distributions.py b/examples/make_ts_distributions.py index 7386266..c362279 100755 --- a/examples/make_ts_distributions.py +++ b/examples/make_ts_distributions.py @@ -1,18 +1,8 @@ #! /usr/bin/env python # ========================================================================== -# This script generates TS distributions for the standard energy bands that -# are also used for sensitivity computation. The script makes use of the -# "processing" Python module, if available, that can be used for parallel -# computing on multiple cores/CPUs. +# Generate TS distribution # -# This script provides an illustration of how a ctools or cscript can be -# used from within Python. It further illustrates the possibility of -# parallel computing using the "processing" Python module. -# -# Required 3rd party modules: -# - processing (optional) -# -# Copyright (C) 2011-2015 Jurgen Knodlseder +# Copyright (C) 2011-2016 Jurgen Knodlseder # # 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 @@ -30,15 +20,14 @@ # ========================================================================== import sys import time -from cscripts import cstsdist +import cscripts -# Try importing processing module -has_processing = False +# Optionally import processing module try: import processing has_processing = True except: - pass + has_processing = False # ====================== # @@ -49,35 +38,45 @@ def create_ts(loge, emin, emax, ntrials=100, duration=180000.0, """ Create TS distribution. - Parameters: - loge - Logarithm of mean energy in TeV - emin - Minimum energy (TeV) - emax - Maximum energy (TeV) - Keywords: - log - Create log file(s) + Parameters + ---------- + loge : float + Logarithm of mean energy (TeV) + emin : float + Minimum energy (TeV) + emax : float + Maximum energy (TeV) + ntrials : int, optional + Number of MC samples + durations : float, optional + Observation duration (s) + enumbins : int, optional + Number of energy bins + log : bool, optional + Create log file(s) """ # Generate output filename - outfile = "ts_"+str(loge)+".dat" + outfile = 'ts_'+str(loge)+'.dat' # Setup cstsdist tool - tsdist = cstsdist() - tsdist["inmodel"] = "$CTOOLS/share/models/crab.xml" - tsdist["srcname"] = "Crab" - tsdist["outfile"] = outfile - tsdist["ntrials"] = ntrials - tsdist["caldb"] = "prod2" - tsdist["irf"] = "South_50h" - tsdist["ra"] = 83.63 - tsdist["dec"] = 22.01 - tsdist["emin"] = emin - tsdist["emax"] = emax - tsdist["enumbins"] = enumbins - tsdist["tmin"] = 0 - tsdist["tmax"] = duration - tsdist["rad"] = 5.0 - tsdist["npix"] = 200 - tsdist["binsz"] = 0.05 - #tsdist["debug"].boolean(True) + tsdist = cscripts.cstsdist() + tsdist['inmodel'] = 'data/crab.xml' + tsdist['srcname'] = 'Crab' + tsdist['outfile'] = outfile + tsdist['ntrials'] = ntrials + tsdist['caldb'] = 'prod2' + tsdist['irf'] = 'South_0.5h' + tsdist['ra'] = 83.63 + tsdist['dec'] = 22.01 + tsdist['emin'] = emin + tsdist['emax'] = emax + tsdist['enumbins'] = enumbins + tsdist['tmin'] = 0 + tsdist['tmax'] = duration + tsdist['rad'] = 5.0 + tsdist['npix'] = 200 + tsdist['binsz'] = 0.05 + #tsdist['debug'] = True # Optionally open the log file if log: @@ -96,7 +95,7 @@ def create_ts(loge, emin, emax, ntrials=100, duration=180000.0, if __name__ == '__main__': # Get input arguments - usage = "make_ts_distributions [-n ntrials] [-e enumbins] [-m max_threads]" + usage = 'make_ts_distributions [-n ntrials] [-e enumbins] [-m max_threads]' if len(sys.argv) < 1: print(usage) sys.exit() @@ -111,7 +110,7 @@ if __name__ == '__main__': pars = [{'option': '-n', 'value': ntrials}, {'option': '-e', 'value': enumbins}, {'option': '-d', 'value': duration}, - {'option': '-m', 'value': max_threads}] + {'option': '-m', 'value': max_threads}] # Gather parameters from command line i = 1 @@ -121,7 +120,7 @@ if __name__ == '__main__': for par in pars: if sys.argv[i] == par['option']: if len(sys.argv) > i+1: - i += 1 + i += 1 try: par['value'] = int(sys.argv[i]) except: @@ -150,11 +149,12 @@ if __name__ == '__main__': emin = pow(10.0, loge-0.1) emax = pow(10.0, loge+0.1) if loge < 0: - loge = "m"+str(abs(loge)) + loge = 'm'+str(abs(loge)) else: - loge = "p"+str(abs(loge)) + loge = 'p'+str(abs(loge)) - # Processing support? + # If processing support is available then start max_threads processes + # in parallel if has_processing: # Wait until one thread has finished @@ -169,19 +169,17 @@ if __name__ == '__main__': # Generate pull distribution p = processing.Process(target=create_ts, args=args, kwargs=kwargs) p.start() - print("Process emin=%.4f emax=%.4f started." % (emin, emax)) + print('Process emin=%.4f emax=%.4f started.' % (emin, emax)) # Wait a short time to allow process to start time.sleep(1) - # ... no + # ... otherwise simply start a single process else: create_ts(loge, emin, emax, ntrials=ntrials, enumbins=enumbins, - duration=duration) + duration=duration) - # Processing support + # If processing support is available then wait until all threads finished if has_processing: - - # Wait until all threads finished while len(processing.activeChildren()) > 0: time.sleep(10) diff --git a/test/test_examples.py b/test/test_examples.py index c37a98c..9273648 100755 --- a/test/test_examples.py +++ b/test/test_examples.py @@ -71,7 +71,6 @@ class Test(gammalib.GPythonTestSuite): # Execute Python script, make sure we catch any exception try: rc = os.system(cmd+' > example_'+logname+'.log 2>&1') - #rc = os.system(cmd) except: pass @@ -93,6 +92,7 @@ class Test(gammalib.GPythonTestSuite): self.append(self.test_generate_prod3_irfs, 'Test generate_prod3_irfs') self.append(self.test_make_pointings, 'Test make_pointings') self.append(self.test_make_spectrum, 'Test make_spectrum') + self.append(self.test_make_ts_distributions, 'Test make_ts_distributions') self.append(self.test_pipeline_binned_disk, 'Test pipeline_binned_disk') self.append(self.test_pipeline_binned_mem, 'Test pipeline_binned_mem') self.append(self.test_pipeline_stacked_disk, 'Test pipeline_stacked_disk') @@ -124,6 +124,19 @@ class Test(gammalib.GPythonTestSuite): # Return return + # Test generate_prod3_irfs + def test_generate_prod3_irfs(self): + """ + Test generate_prod3_irfs + """ + # Execute script + self._execute_python('generate_prod3_irfs') + + #TODO: Do any testing + + # Return + return + # Test make_spectrum def test_make_spectrum(self): """ @@ -137,13 +150,13 @@ class Test(gammalib.GPythonTestSuite): # Return return - # Test generate_prod3_irfs - def test_generate_prod3_irfs(self): + # Test make_ts_distributions + def test_make_ts_distributions(self): """ - Test generate_prod3_irfs + Test make_ts_distributions """ # Execute script - self._execute_python('generate_prod3_irfs') + self._execute_python('make_ts_distributions', args='-n 2 -e 0 -d 1800') #TODO: Do any testing -- libgit2 0.21.4