Commit fca0cb574d3df6594c49faaa8aeecc46fab42d99

Authored by Jürgen Knödlseder
1 parent 3067e5a0

Add unit test for spectral table model (#599)

include/GModelSpectralTable.hpp
1 1 /***************************************************************************
2 2 * GModelSpectralTable.hpp - Spectral table model class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2019 by Juergen Knoedlseder *
  4 + * copyright (C) 2019-2020 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ...
include/GModelSpectralTablePar.hpp
1 1 /***************************************************************************
2 2 * GModelSpectralTablePar.hpp - Spectral table model parameter class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2019 by Juergen Knoedlseder *
  4 + * copyright (C) 2019-2020 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ...
include/GModelSpectralTablePars.hpp
1 1 /***************************************************************************
2 2 * GModelSpectralTablePars.hpp - Spectral table model par container class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2019 by Juergen Knoedlseder *
  4 + * copyright (C) 2019-2020 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ...
pyext/GModelSpectralTable.i
1 1 /***************************************************************************
2 2 * GModelSpectralTable.i - Spectral table model class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2019 by Juergen Knoedlseder *
  4 + * copyright (C) 2019-2020 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ...
pyext/GModelSpectralTablePar.i
1 1 /***************************************************************************
2 2 * GModelSpectralTablePar.i - Spectral table model parameter class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2019 by Juergen Knoedlseder *
  4 + * copyright (C) 2019-2020 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ...
pyext/GModelSpectralTablePars.i
1 1 /***************************************************************************
2 2 * GModelSpectralTablePars.i - Spectral table model par container class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2019 by Juergen Knoedlseder *
  4 + * copyright (C) 2019-2020 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ...
pyext/Makefile.am
... ... @@ -129,6 +129,7 @@ testdata = $(top_srcdir)/test/data/initial_coords.txt \
129 129 $(top_srcdir)/test/data/model_point_plaw.xml \
130 130 $(top_srcdir)/test/data/test_cube.fits \
131 131 $(top_srcdir)/test/data/filefunction.txt \
  132 + $(top_srcdir)/test/data/model_point_table.fits \
132 133 $(top_srcdir)/test/data/model_temporal_lightcurve.fits \
133 134 $(top_srcdir)/test/data/model_temporal_phasecurve.fits
134 135  
... ...
src/model/GModelSpectralTable.cpp
1 1 /***************************************************************************
2 2 * GModelSpectralTable.cpp - Spectral table model class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2019 by Juergen Knoedlseder *
  4 + * copyright (C) 2019-2020 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -494,6 +494,9 @@ void GModelSpectralTable::read(const GXmlElement& xml)
494 494 "Spectral function requires exactly 1 parameter.");
495 495 }
496 496  
  497 + // Load table model from file (do this first since method calls clear())
  498 + load(gammalib::xml_file_expand(xml, xml.attribute("file")));
  499 +
497 500 // Get parameter element
498 501 const GXmlElement* par = xml.element("parameter", 0);
499 502  
... ... @@ -506,9 +509,6 @@ void GModelSpectralTable::read(const GXmlElement& xml)
506 509 "Require \"Normalization\" parameter.");
507 510 }
508 511  
509   - // Load table model from file
510   - load(gammalib::xml_file_expand(xml, xml.attribute("file")));
511   -
512 512 // Return
513 513 return;
514 514 }
... ...
src/model/GModelSpectralTablePar.cpp
1 1 /***************************************************************************
2 2 * GModelSpectralTablePar.cpp - Spectral table model parameter class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2019 by Juergen Knoedlseder *
  4 + * copyright (C) 2019-2020 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ...
src/model/GModelSpectralTablePars.cpp
1 1 /***************************************************************************
2 2 * GModelSpectralTablePars.cpp - Spectral table model par container class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2019 by Juergen Knoedlseder *
  4 + * copyright (C) 2019-2020 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ...
test/data/model_point_filefct.xml
1 1 <?xml version="1.0" standalone="no"?>
2 2 <source_library title="source library">
3   - <source name="Crab" type="PointSource">
4   - <spectrum type="FileFunction" file="filefunction.txt">
5   - <parameter scale="1.0" name="Normalization" min="0.0" max="1000.0" value="1.0" free="1"/>
6   - </spectrum>
7   - <spatialModel type="PointSource">
8   - <parameter free="0" max="360" min="-360" name="RA" scale="1" value="83.6331" />
9   - <parameter free="0" max="90" min="-90" name="DEC" scale="1" value="22.0145" />
10   - </spatialModel>
11   - </source>
  3 + <source name="Crab" type="PointSource">
  4 + <spectrum type="FileFunction" file="filefunction.txt">
  5 + <parameter scale="1.0" name="Normalization" min="0.0" max="1000.0" value="1.0" free="1"/>
  6 + </spectrum>
  7 + <spatialModel type="PointSource">
  8 + <parameter free="0" max="360" min="-360" name="RA" scale="1" value="83.6331" />
  9 + <parameter free="0" max="90" min="-90" name="DEC" scale="1" value="22.0145" />
  10 + </spatialModel>
  11 + </source>
12 12 </source_library>
... ...
test/data/model_point_table.fits 0 → 100644
No preview for this file type
test/data/model_point_table.xml 0 → 100644
  1 +<?xml version="1.0" standalone="no"?>
  2 +<source_library title="source library">
  3 + <source name="Crab" type="PointSource">
  4 + <spectrum type="TableModel" file="model_point_table.fits">
  5 + <parameter name="Normalization" scale="1.0" value="1.0" min="1e-07" max="1000.0" free="1"/>
  6 + </spectrum>
  7 + <spatialModel type="PointSource">
  8 + <parameter free="0" max="360" min="-360" name="RA" scale="1" value="83.6331" />
  9 + <parameter free="0" max="90" min="-90" name="DEC" scale="1" value="22.0145" />
  10 + </spatialModel>
  11 + </source>
  12 +</source_library>
... ...
test/test_GModel.cpp
... ... @@ -51,6 +51,7 @@ void TestGModel::set(void)
51 51 m_map_file = datadir + "/cena_lobes_parkes.fits";
52 52 m_cube_file = datadir + "/test_cube.fits";
53 53 m_filefct = datadir + "/filefunction.txt";
  54 + m_table = datadir + "/model_point_table.fits";
54 55 m_temp_lightcurve = datadir + "/model_temporal_lightcurve.fits";
55 56 m_temp_phasecurve = datadir + "/model_temporal_phasecurve.fits";
56 57 m_xml_file = datadir + "/crab.xml";
... ... @@ -67,10 +68,11 @@ void TestGModel::set(void)
67 68 m_xml_model_point_smoothbplaw = datadir + "/model_point_smoothbplaw.xml";
68 69 m_xml_model_point_supeplaw = datadir + "/model_point_supeplaw.xml";
69 70 m_xml_model_point_logparabola = datadir + "/model_point_logparabola.xml";
70   - m_xml_point_multiplicative = datadir + "/model_point_multiplicative.xml";
71   - m_xml_point_exponential = datadir + "/model_point_exponential.xml";
72 71 m_xml_model_point_nodes = datadir + "/model_point_nodes.xml";
73 72 m_xml_model_point_filefct = datadir + "/model_point_filefct.xml";
  73 + m_xml_model_point_table = datadir + "/model_point_table.xml";
  74 + m_xml_point_multiplicative = datadir + "/model_point_multiplicative.xml";
  75 + m_xml_point_exponential = datadir + "/model_point_exponential.xml";
74 76 m_xml_model_spectral_composite = datadir + "/model_spectral_composite.xml";
75 77 m_xml_model_diffuse_const = datadir + "/model_diffuse_const.xml";
76 78 m_xml_model_diffuse_cube = datadir + "/model_diffuse_cube.xml";
... ... @@ -170,6 +172,8 @@ void TestGModel::set(void)
170 172 "Test GModelSpectralNodes");
171 173 append(static_cast<pfunction>(&TestGModel::test_filefct),
172 174 "Test GModelSpectralFunc");
  175 + append(static_cast<pfunction>(&TestGModel::test_table),
  176 + "Test GModelSpectralTable");
173 177 append(static_cast<pfunction>(&TestGModel::test_spectral_composite),
174 178 "Test GModelSpectralComposite");
175 179 append(static_cast<pfunction>(&TestGModel::test_spectral_model),
... ... @@ -2291,6 +2295,61 @@ void TestGModel::test_filefct(void)
2291 2295 return;
2292 2296 }
2293 2297  
  2298 +
  2299 +/***********************************************************************//**
  2300 + * @brief Test GModelSpectralTable class
  2301 + ***************************************************************************/
  2302 +void TestGModel::test_table(void)
  2303 +{
  2304 + // Test void constructor
  2305 + GModelSpectralTable model1;
  2306 + test_value(model1.type(), "TableModel", "Check void model type");
  2307 +
  2308 + // Test value constructor
  2309 + GModelSpectralTable model2(m_table, 2.0);
  2310 + test_value(model2.filename().url(), m_table,
  2311 + "Check file function data file name");
  2312 + test_value(model2.norm(), 2.0);
  2313 +
  2314 + // Test XML constructor
  2315 + GXml xml(m_xml_model_point_table);
  2316 + GXmlElement* element = xml.element(0)->element(0)->element("spectrum", 0);
  2317 + GModelSpectralTable model3(*element);
  2318 + test_value(model3.size(), 3);
  2319 + test_value(model3.type(), "TableModel", "Check model type");
  2320 + test_value(model3.filename().url(), m_table,
  2321 + "Check table model data file name");
  2322 + test_value(model3.norm(), 1.0);
  2323 +
  2324 + // Test filename method
  2325 + //model3.filename(m_table);
  2326 + //test_value(model3.filename().url(), m_table,
  2327 + // "Check table model data file name");
  2328 +
  2329 + // Test norm method
  2330 + model3.norm(3.0);
  2331 + test_value(model3.norm(), 3.0);
  2332 +
  2333 + // Test operator access
  2334 + /*
  2335 + const char* strarray[] = {"Normalization"};
  2336 + for (int i = 0; i < 1; ++i) {
  2337 + std::string keyname(strarray[i]);
  2338 + model3[keyname].remove_range(); // To allow setting of any value
  2339 + model3[keyname].value(2.1);
  2340 + model3[keyname].error(1.9);
  2341 + model3[keyname].gradient(0.8);
  2342 + test_value(model3[keyname].value(), 2.1);
  2343 + test_value(model3[keyname].error(), 1.9);
  2344 + test_value(model3[keyname].gradient(), 0.8);
  2345 + }
  2346 + */
  2347 +
  2348 + // Exit test
  2349 + return;
  2350 +}
  2351 +
  2352 +
2294 2353 /***********************************************************************//**
2295 2354 * @brief Test GModelSpectralComposite class
2296 2355 ***************************************************************************/
... ...
test/test_GModel.hpp
... ... @@ -78,6 +78,7 @@ public:
78 78 void test_filefct(void);
79 79 void test_spectral_composite(void);
80 80 void test_spectral_model(void);
  81 + void test_table(void);
81 82 void test_temp_const(void);
82 83 void test_temp_lightcurve(void);
83 84 void test_temp_phasecurve(void);
... ... @@ -109,6 +110,7 @@ private:
109 110 std::string m_map_file;
110 111 std::string m_cube_file;
111 112 std::string m_filefct;
  113 + std::string m_table;
112 114 std::string m_temp_lightcurve;
113 115 std::string m_temp_phasecurve;
114 116 std::string m_xml_file;
... ... @@ -125,10 +127,11 @@ private:
125 127 std::string m_xml_model_point_smoothbplaw;
126 128 std::string m_xml_model_point_supeplaw;
127 129 std::string m_xml_model_point_logparabola;
128   - std::string m_xml_point_multiplicative;
129   - std::string m_xml_point_exponential;
130 130 std::string m_xml_model_point_nodes;
131 131 std::string m_xml_model_point_filefct;
  132 + std::string m_xml_model_point_table;
  133 + std::string m_xml_point_multiplicative;
  134 + std::string m_xml_point_exponential;
132 135 std::string m_xml_model_spectral_composite;
133 136 std::string m_xml_model_diffuse_const;
134 137 std::string m_xml_model_diffuse_cube;
... ...
test/test_GModel.py
... ... @@ -147,6 +147,7 @@ class Test(gammalib.GPythonTestSuite):
147 147 test_support.pickeling(self, gammalib.GModelSpectralPlawPhotonFlux())
148 148 test_support.pickeling(self, gammalib.GModelSpectralSmoothBrokenPlaw())
149 149 test_support.pickeling(self, gammalib.GModelSpectralSuperExpPlaw())
  150 + test_support.pickeling(self, gammalib.GModelSpectralTable())
150 151 test_support.pickeling(self, gammalib.GModelTemporalConst())
151 152 test_support.pickeling(self, gammalib.GModelTemporalLightCurve())
152 153 test_support.pickeling(self, gammalib.GModelTemporalPhaseCurve())
... ... @@ -175,6 +176,7 @@ class Test(gammalib.GPythonTestSuite):
175 176 lcrv = gammalib.GFilename(os.environ['TEST_DATA']+'/model_temporal_lightcurve.fits')
176 177 pcrv = gammalib.GFilename(os.environ['TEST_DATA']+'/model_temporal_phasecurve.fits')
177 178 cube = gammalib.GFilename(os.environ['TEST_DATA']+'/test_cube.fits')
  179 + table = gammalib.GFilename(os.environ['TEST_DATA']+'/model_point_table.fits')
178 180 dir = gammalib.GSkyDir()
179 181  
180 182 # Perform pickeling tests of filled classes
... ... @@ -212,6 +214,7 @@ class Test(gammalib.GPythonTestSuite):
212 214 test_support.pickeling(self, gammalib.GModelSpectralPlawPhotonFlux(1.0,-2.0,pivot,pivot))
213 215 test_support.pickeling(self, gammalib.GModelSpectralSmoothBrokenPlaw(1.0,-2.0,pivot,-2.1,pivot,0.9))
214 216 test_support.pickeling(self, gammalib.GModelSpectralSuperExpPlaw(1.0,-2.0,pivot,pivot,0.9))
  217 + test_support.pickeling(self, gammalib.GModelSpectralTable(table, 2.0))
215 218 test_support.pickeling(self, gammalib.GModelTemporalConst(cnst))
216 219 test_support.pickeling(self, gammalib.GModelTemporalLightCurve(lcrv, 2.0))
217 220 test_support.pickeling(self, gammalib.GModelTemporalPhaseCurve(pcrv, time, 0.5, 1.0, 1.0, 1.0, 2.0))
... ...