Commit 918c92ae6c93ad293322af4b0185aa9fea89cd04

Authored by Jürgen Knödlseder
1 parent 8ceb1537

Use test observation class for unit testing and make compliant to coding conventions.

The test used initially the LAT observation class, and therefore was not really executed. Now the new observation test class is used, and tests are performed for binned and unbinned analysis.
test/test_GOptimizer.cpp
1 1 /***************************************************************************
2 2 * test_GOptimizer.cpp - test GOptimizer class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2009 by Jurgen Knodlseder *
  4 + * copyright (C) 2009-2012 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -18,149 +18,171 @@
18 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19 19 * *
20 20 ***************************************************************************/
  21 +/**
  22 + * @file test_GOptimizer.cpp
  23 + * @brief Implementation of unit tests for optimizer module
  24 + * @author Juergen Knoedlseder
  25 + */
21 26  
22 27 /* __ Includes ___________________________________________________________ */
23   -#include <stdlib.h>
  28 +#ifdef HAVE_CONFIG_H
  29 +#include <config.h>
  30 +#endif
  31 +//#include <stdlib.h>
24 32 #include "test_GOptimizer.hpp"
  33 +#include "testinst/GTestLib.hpp"
25 34  
26   -/***************************************************************************
27   - * Set *
28   - ***************************************************************************/
  35 +/* __ Coding definitions _________________________________________________ */
  36 +#define RATE 13.0 //!< Events per seconde. For events generation.
  37 +#define UN_BINNED 0
  38 +#define BINNED 1
  39 +
  40 +
  41 +/***********************************************************************//**
  42 + * @brief Set parameters and tests
  43 + **************************************************************************/
29 44 void TestGOptimizer::set(void){
  45 +
30 46 // Test name
31   - name("GOptimizer");
  47 + name("Optimizer module");
32 48  
33   - //add tests
34   - add_test(static_cast<pfunction>(&TestGOptimizer::test_optimizer),"Test LAT Response");
  49 + // Append tests
  50 + append(static_cast<pfunction>(&TestGOptimizer::test_unbinned_optimizer), "Test unbinned optimization");
  51 + append(static_cast<pfunction>(&TestGOptimizer::test_binned_optimizer), "Test binned optimization");
35 52  
  53 + // Return
36 54 return;
37 55 }
38 56  
39   -/***************************************************************************
40   - * Test: Optimizer *
  57 +
  58 +/***********************************************************************//**
  59 + * @brief Test optimizer
  60 + *
  61 + * @param[in] mode Testing mode.
  62 + *
  63 + * This method supports two testing modes: 0 = unbinned and 1 = binned.
41 64 ***************************************************************************/
42   -void TestGOptimizer::test_optimizer(void)
  65 +GModelPar& TestGOptimizer::test_optimizer(int mode)
43 66 {
  67 + // Create Test Model
  68 + GTestModelData model;
44 69  
45   - // Number of observations in data
46   - int nobs = 1;
  70 + // Create Models conteners
  71 + GModels models;
  72 + models.append(model);
47 73  
48   - // Setup GData for optimizing
49   - GData data;
50   - GLATObservation obs;
51   - test_try("Setup GData for optimizing");
52   - try {
53   - obs.load_binned("data/lat/cntmap.fits.gz", "", "");
54   - for (int i = 0; i < nobs; ++i)
55   - data.add(obs);
  74 + // Time iterval
  75 + GTime tmin(0,0, "sec");
  76 + GTime tmax(1800,0,"sec");
56 77  
57   - test_try_success();
58   - }
59   - catch (std::exception &e) {
60   - test_try_failure(e);
61   - }
  78 + // Rate : events/sec
  79 + double rate = RATE;
62 80  
63   - //std::cout << data << std::endl;
64   -
65   - // Setup GModels for optimizing
66   - GModelSpatialPtsrc point_source;
67   - GModelSpectralPlaw power_law;
68   - GModel crab;
69   - GModels models;
70   - test_try("Setup GModels for optimizing");
71   - try {
72   - GSkyDir dir;
73   - dir.radec_deg(83.6331, +22.0145);
74   - point_source = GModelSpatialPtsrc(dir);
75   - power_law = GModelSpectralPlaw(1.0e-7, -2.1);
76   - crab = GModel(point_source, power_law);
77   - crab.name("Crab");
78   - models.add(crab);
79   - data.models(models);
80   -
81   - test_try_success();
82   - }
83   - catch (std::exception &e) {
84   - test_try_failure(e);
85   - }
  81 + // Create observations
  82 + GObservations obs;
86 83  
87   - //std::cout << models << std::endl;
  84 + // Add some observation
  85 + for (int i=0; i<6; ++i) {
88 86  
89   - // Setup parameters for optimizing
90   - test_try("Setup parameters for optimizing");
91   - GOptimizerPars pars;
92   - try {
93   - pars = GOptimizerPars(models);
  87 + // Random Generator
  88 + GRan ran;
  89 + ran.seed(i);
94 90  
95   - test_try_success();
96   - }
97   - catch (std::exception &e) {
98   - test_try_failure(e);
99   - }
  91 + // Allocate events pointer
  92 + GEvents *events;
100 93  
101   - //std::cout << pars << std::endl;
102   -
103   - // Time simple GData iterator
104   - double t_elapse;
105   - test_try("Time simple GData iterator");
106   - try {
107   - clock_t t_start = clock();
108   - int num = 0;
109   - int sum = 0;
110   - for (GData::iterator event = data.begin(); event != data.end(); ++event) {
111   - num++;
112   - sum += (int)event->counts();
  94 + // Create either a event list or an event cube
  95 + if (mode == UN_BINNED) {
  96 + events = model.generateList(rate,tmin,tmax,ran);
  97 + }
  98 + else {
  99 + events = model.generateCube(rate,tmin,tmax,ran);
113 100 }
114   - t_elapse = double(clock() - t_start)/double(CLOCKS_PER_SEC);
115 101  
116   - test_try_success();
117   - }
118   - catch (std::exception &e) {
119   - test_try_failure(e);
  102 + // Create an observation
  103 + GTestObservation ob;
  104 +
  105 + // Add events to the observation
  106 + ob.events(events);
  107 + ob.ontime(tmax.met()-tmin.met());
  108 + obs.append(ob);
120 109 }
121 110  
122   - //std::cout << " - Reference time for GData::iterator: " << t_elapse << std::endl;
  111 + // Add the model to the observation
  112 + obs.models(models);
123 113  
124   - // Setup LM optimizer
125   - GOptimizerLM opt;
126   - test_try("Setup LM optimizer");
127   - try {
128   - data.optimize(opt);
129   - }
130   - catch (std::exception &e) {
131   - test_try_failure(e);
132   - }
  114 + // Create a GLog for show the interations of optimizer.
  115 + GLog log;
  116 +
  117 + // Create an optimizer.
  118 + GOptimizerLM opt(log);
  119 +
  120 + opt.max_stalls(50);
133 121  
134   - //std::cout << opt << std::endl;
135   - //std::cout << data << std::endl;
  122 + // Optimize
  123 + obs.optimize(opt);
136 124  
137   - // Exit test
  125 + // Get the result
  126 + GModelPar& result = ((obs.models())[0])[0];
  127 +
  128 + //check if converged
  129 + test_assert(opt.status()==0, "Check if converged", "Optimizer did not convered");
  130 +
  131 + //check if value is correct
  132 + test_value(result.value(),RATE,result.error()*3);
  133 +
  134 + // Return
  135 + return (((obs.models())[0])[0]);
  136 +}
  137 +
  138 +
  139 +/***********************************************************************//**
  140 + * @brief Test unbinned optimizer
  141 + ***************************************************************************/
  142 +void TestGOptimizer::test_unbinned_optimizer(void)
  143 +{
  144 + // Test
  145 + test_optimizer(UN_BINNED);
  146 +
  147 + // Return
138 148 return;
  149 +}
  150 +
  151 +
  152 +/***********************************************************************//**
  153 + * @brief Test binned optimizer
  154 + ***************************************************************************/
  155 +void TestGOptimizer::test_binned_optimizer(void)
  156 +{
  157 + // Test
  158 + test_optimizer(BINNED);
139 159  
  160 + // Return
  161 + return;
140 162 }
141 163  
142 164  
143 165 /***************************************************************************
144   - * Main test function *
  166 + * @brief Main entry point for test executable
145 167 ***************************************************************************/
146 168 int main(void)
147 169 {
148   - GTestSuites testsuites("GOptimizer");
  170 + // Allocate test suit container
  171 + GTestSuites testsuites("Optimizer module");
149 172  
150   - bool was_successful=true;
  173 + // Initially assume that we pass all tests
  174 + bool success = true;
151 175  
152   - //Create a test suite
153   - TestGOptimizer test;
  176 + // Create and append test suite
  177 + TestGOptimizer openmp;
  178 + testsuites.append(openmp);
154 179  
155   - //Append to the container
156   - testsuites.append(test);
  180 + // Run the testsuites
  181 + success = testsuites.run();
157 182  
158   - //Run
159   - was_successful=testsuites.run();
160   -
161   - //save xml report
  183 + // Save test report
162 184 testsuites.save("reports/GOptimizer.xml");
163 185  
164   - // Return
165   - return was_successful ? 0:1;
  186 + // Return success status
  187 + return (success ? 0 : 1);
166 188 }
... ...
test/test_GOptimizer.hpp
1 1 /***************************************************************************
2   - * test_GOptimizer.hpp - test GOptimizer class *
  2 + * test_GOptimizer.hpp - test optimizer module *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2009-2012 by Jurgen Knodlseder *
  4 + * copyright (C) 2009-2012 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -18,29 +18,35 @@
18 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19 19 * *
20 20 ***************************************************************************/
  21 +/**
  22 + * @file test_GOptimizer.hpp
  23 + * @brief Definition of unit tests for optimizer module
  24 + * @author Juergen Knoedlseder
  25 + */
21 26  
22 27 #ifndef TEST_GOPTIMIZER_HPP
23 28 #define TEST_GOPTIMIZER_HPP
24 29  
25 30 /* __ Includes ___________________________________________________________ */
26 31 #include "GammaLib.hpp"
27   -#include <iostream> // cout, cerr
28   -#include <stdexcept> // std::exception
29 32  
30   -class TestGOptimizer : public GTestSuite
31   -{
32   - public:
33   - // Constructors and destructors
34   - TestGOptimizer(void) : GTestSuite(){ return; }
35   - virtual ~TestGOptimizer(void){ return; }
36 33  
37   - // Methods
38   - virtual void set(void);
39   - void test_optimizer(void);
40   -
41   - // Private members
42   - private:
43   -};
  34 +/***********************************************************************//**
  35 + * @class TestGOptimizer
  36 + *
  37 + * @brief Test suite for optimizer module testing
  38 + ***************************************************************************/
  39 +class TestGOptimizer : public GTestSuite {
44 40  
  41 +public:
  42 + // Constructors and destructors
  43 + TestGOptimizer(void) : GTestSuite() {}
  44 + virtual ~TestGOptimizer(void){}
45 45  
  46 + // Methods
  47 + virtual void set(void);
  48 + void test_unbinned_optimizer(void);
  49 + void test_binned_optimizer(void);
  50 + GModelPar& test_optimizer(int mode);
  51 +};
46 52 #endif /* TEST_GOPTIMIZER_HPP */
... ...
test/test_GOptimizer.py
1 1 # ==========================================================================
2   -# This module performs unit tests for the GammaLib opt module.
  2 +# This module performs unit tests for the GammaLib optimizer module.
3 3 #
4 4 # Copyright (C) 2012 Juergen Knoedlseder
5 5 #
... ... @@ -23,11 +23,11 @@ import os
23 23  
24 24  
25 25 # ================================== #
26   -# Test class for GammaLib opt module #
  26 +# Test class for GammaLib optimizer module #
27 27 # ================================== #
28 28 class Test(GPythonTestSuite):
29 29 """
30   - Test class for GammaLib opt module.
  30 + Test class for GammaLib optimizer module.
31 31 """
32 32 # Constructor
33 33 def __init__(self):
... ... @@ -50,7 +50,7 @@ class Test(GPythonTestSuite):
50 50 self.name("opt")
51 51  
52 52 # Append tests
53   - self.append(self.test, "Dummy test")
  53 + self.append(self.test, "Optimizer module dummy test")
54 54  
55 55 # Return
56 56 return
... ...