test_examples.py.in 14.9 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 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
#! /usr/bin/env python
# ==========================================================================
# Performs unit tests for the ctools example scripts
#
# Copyright (C) 2016-2017 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_irf,
                    'Test show_irf.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_phases,
                    'Test show_phases.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_lightcurve,
                    'Test show_lightcurve.py')
        self.append(self.test_show_ts_distribution,
                    'Test ts_distribution.py')
        self.append(self.test_show_obs,
                    'Test show_obs.py')
        self.append(self.test_show_pointings,
                    'Test show_pointings.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_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_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_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_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_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_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

    # 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


# ======================== #
# 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.')