#! /usr/bin/env python # ========================================================================== # Performs unit tests for the ctools example scripts # # Copyright (C) 2016-2019 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 gammalib # =============================================== # # 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_make_pointings, 'Test make_pointings.py') self.append(self.test_show_butterfly, 'Test show_butterfly.py') self.append(self.test_show_irf, 'Test show_irf.py') self.append(self.test_show_lightcurve, 'Test show_lightcurve.py') self.append(self.test_show_model, 'Test show_model.py') self.append(self.test_show_obs, 'Test show_obs.py') self.append(self.test_show_pha, 'Test show_pha.py') self.append(self.test_show_phases, 'Test show_phases.py') self.append(self.test_show_pointings, 'Test show_pointings.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_residuals, 'Test show_residuals.py') self.append(self.test_show_response, 'Test show_response.py') self.append(self.test_show_rmf, 'Test show_rmf.py') self.append(self.test_show_sensitivity, 'Test show_sensitivity.py') self.append(self.test_show_significance_distribution, 'Test show_significance_distribution.py') self.append(self.test_show_spectrum, 'Test show_spectrum.py') self.append(self.test_show_ts_distribution, 'Test show_ts_distribution.py') self.append(self.test_show_variability_evolution, 'Test show_variability_evolution.py') self.append(self.test_show_viscube, 'Test show_viscube.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 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_irf def test_show_irf(self): """ Test show_irf """ # Set arguments args = '-p example_show_irf.eps prod2 South_0.5h' # Execute script self._execute_python('show_irf', args=args) # Return return # Test show_lightcurve def test_show_lightcurve(self): """ Test show_lightcurve """ # Set arguments args = '-p example_show_lightcurve.eps %s/lightcurve.fits' % self._datadir # Execute script self._execute_python('show_lightcurve', 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_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 # 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_phases def test_show_phases(self): """ Test show_phases """ # Set arguments args = '-p example_show_phases.eps %s/phased_events.fits' % self._datadir # Execute script self._execute_python('show_phases', args=args) # Return return # Test show_pointings def test_show_pointings(self): """ Test show_pointings """ # Set arguments args = '-p example_show_pointings.eps %s/obs_unbinned.xml' % self._datadir # Execute script self._execute_python('show_pointings', 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_residuals def test_show_residuals(self): """ Test show_residuals """ # Set arguments args = '-p example_show_residuals.eps %s/residual.fits' % self._datadir # Execute script self._execute_python('show_residuals', args=args) # Set arguments args = ('-p example_show_residuals_onoff.eps %s/residual_onoff.fits' % self._datadir) # Execute script self._execute_python('show_residuals', 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_rmf def test_show_rmf(self): """ Test show_rmf """ # Set arguments args = ('-p example_show_rmf.eps %s/onoff_rmf.fits' % self._datadir) # Execute script self._execute_python('show_rmf', 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_significance_distribution def test_show_significance_distribution(self): """ Test show_significance_distribution """ # Set arguments args = '-p example_show_significance_distribution.eps '+ \ '%s/sigdist_map.fits --exclude %s/sigdist_map.fits[EXCLUSION]' % \ (self._datadir, self._datadir) # Execute script self._execute_python('show_significance_distribution', 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) # Set arguments args = '-p example_show_spectrum_castro.eps %s/spectrum_castro.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_variability_evolution def test_show_variability_evolution(self): """ Test show_variability_evolution """ # Set arguments args = ('-p example_show_variability_evolution.eps %s/variability.fits' % self._datadir) # Execute script self._execute_python('show_variability_evolution', args=args) # Return return # Test show_viscube def test_show_viscube(self): """ Test show_viscube """ # Set arguments args = ('-p example_show_viscube.eps %s/viscube.fits' % self._datadir) # Execute script self._execute_python('show_viscube', 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') # Raise an exception in case of failure if not success: raise RuntimeError('At least one error occured during the test.')