#! /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 . # # ========================================================================== 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 """ # Set arguments args = '-d %s -p example_spectrum.eps' % self._datadir # Execute script self._execute_python('make_spectrum', args=args) # Return return # Test make_ts_distributions def test_make_ts_distributions(self): """ Test make_ts_distributions """ # Set arguments args = '-n 1 -e 0 -t 100 -d %s' % self._datadir # Execute script self._execute_python('make_ts_distributions', args=args) # 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='-d '+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='-d '+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='-d '+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='-d '+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='-d '+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='-d '+self._datadir) # Return return # Test show_butterfly def test_show_butterfly(self): """ Test show_butterfly """ # Set arguments args = '-p example_show_butterfly.eps %s/butterfly.txt' % self._datadir # Execute script self._execute_python('show_butterfly', args=args) # Return return # Test show_model def test_show_model(self): """ Test show_model """ # Set arguments args = '-n Crab -p example_show_model1.eps %s/crab.xml' % self._datadir # Execute script self._execute_python('show_model', args=args) # Set new arguments args = '-p example_show_model2.eps %s/crab.xml' % self._datadir # Execute script self._execute_python('show_model', args=args) # Return return # Test show_pha def test_show_pha(self): """ Test show_pha """ # Set arguments args = '-p example_show_pha1.eps %s/pha.fits' % self._datadir # Execute script self._execute_python('show_pha', args=args) # Set arguments args = '-p example_show_pha2.eps %s/pha_channels.fits' % self._datadir # Execute script self._execute_python('show_pha', args=args) # Return return # Test show_pull_evolution def test_show_pull_evolution(self): """ Test show_pull_evolution """ # Set arguments args = ('-p example_show_pull_evolution.eps %s/pull.dat ' 'Pull_Crab_Prefactor' % self._datadir) # Execute script self._execute_python('show_pull_evolution', args=args) # Return return # Test show_pull_histogram def test_show_pull_histogram(self): """ Test show_pull_histogram """ # Set arguments args = ('-p example_show_pull_histogram1.eps %s/pull.dat ' 'Pull_Crab_Prefactor' % self._datadir) # Execute script self._execute_python('show_pull_histogram', args=args) # Set arguments args = ('-n 20 -p example_show_pull_histogram2.eps %s/pull.dat ' 'Pull_Crab_Prefactor' % self._datadir) # Execute script self._execute_python('show_pull_histogram', args=args) # Return return # Test show_response def test_show_response(self): """ Test show_response """ # Set arguments args = '-p example_show_response.eps' # Execute script self._execute_python('show_response', args=args) # Return return # Test show_sensitivity def test_show_sensitivity(self): """ Test show_sensitivity """ # Set arguments args = ('-p example_show_sensitivity1.eps %s/sensitivity.dat' % self._datadir) # Execute script self._execute_python('show_sensitivity', args=args) # Set arguments args = ('-p example_show_sensitivity2.eps %s/sensitivity_integral.dat' % self._datadir) # Execute script self._execute_python('show_sensitivity', args=args) # Return return # Test show_spectrum def test_show_spectrum(self): """ Test show_spectrum """ # Set arguments args = '-p example_show_spectrum.eps %s/spectrum.fits' % self._datadir # Execute script self._execute_python('show_spectrum', args=args) # Return return # Test show_ts_distribution def test_show_ts_distribution(self): """ Test show_ts_distribution """ # Set arguments args = ('-n 100 -p example_show_ts_distribution1.eps %s/ts.dat' % self._datadir) # Execute script self._execute_python('show_ts_distribution', args=args) # Set arguments args = ('-n 100 -t cdf -p example_show_ts_distribution2.eps %s/ts.dat' % self._datadir) # Execute script self._execute_python('show_ts_distribution', args=args) # Return return # Test show_obs def test_show_obs(self): """ Test show_obs """ # Set arguments args = '-p example_show_obs1.eps %s/obs_unbinned.xml' % self._datadir # Execute script self._execute_python('show_obs', args=args) # Set arguments args = ('-ra 83.63 -dec 22.01 -p example_show_obs2.eps ' '%s/obs_unbinned.xml' % self._datadir) # Execute script self._execute_python('show_obs', args=args) # 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)