Commit 29ed2bd15157c7d3a4e3db5b3d37e5f27f20049e

Authored by Jürgen Knödlseder
1 parent cd5a8ca5

Add GModel::associations() setter and getter methods and implement reading from …

…and writing to the XML file of the associations (#3469)
... ... @@ -33,6 +33,7 @@ The following methods have been added:
33 33 - GObservation::model_grad(GModel&, GModelPar&)
34 34 - GModels::eval(GObservation&, GMatrixSparse*)
35 35 - GModel::eval(GObservation&, GMatrixSparse*)
  36 +- GModel::associations()
36 37 - GModelSky::eval(GObservation&, GMatrixSparse*)
37 38 - GModelData::eval(GObservation&, GMatrixSparse*)
38 39 - GModelSky::flux(GEnergy&, GEnergy&)
... ... @@ -102,8 +103,12 @@ Implemented GMatrixSparse::row() and GMatrixSparse::add_to_row() methods.
102 103 8. Model module
103 104 ---------------
104 105 Added GModelAssociation class to implement a model association and the
105   -GModelAssociations container class to hold a list of model associations
106   -(#3469).
  106 +GModelAssociations container class to hold a list of model associations.
  107 +The GModelAssociations class was added as a member of GModel, and
  108 +GModel::associations() getter and setter methods have been implemented to
  109 +access the associations. The associations are also automatically written in
  110 +the GModel::read_attributes() and GModel::write_attributes() methods, and
  111 +the association names are displayed by GModel::print_attributes() (#3469).
107 112  
108 113 Added GModels::eval() vector evaluation method to compute the model values for
109 114 all events within a given observation (#3455).
... ...
include/GModel.hpp
... ... @@ -32,6 +32,7 @@
32 32 #include <string>
33 33 #include "GBase.hpp"
34 34 #include "GModelPar.hpp"
  35 +#include "GModelAssociations.hpp"
35 36 #include "GXmlElement.hpp"
36 37 #include "GEnergy.hpp"
37 38 #include "GTime.hpp"
... ... @@ -130,31 +131,33 @@ public:
130 131 virtual std::string print(const GChatter& chatter = NORMAL) const = 0;
131 132  
132 133 // Implemented methods
133   - int size(void) const;
134   - int scales(void) const;
135   - GModelPar& at(const int& index);
136   - const GModelPar& at(const int& index) const;
137   - bool has_par(const std::string& name) const;
138   - bool has_scales(void) const;
139   - const std::string& name(void) const;
140   - void name(const std::string& name);
141   - const double& ts(void) const;
142   - void ts(const double& ts);
143   - const bool& tscalc(void) const;
144   - void tscalc(const bool& tscalc);
145   - const bool& has_ts(void) const;
146   - std::string instruments(void) const;
147   - void instruments(const std::string& instruments);
148   - GModelPar& scale(const int& index);
149   - const GModelPar& scale(const int& index) const;
150   - GModelPar scale(const std::string& instrument) const;
151   - void scale(const GModelPar& par);
152   - std::string ids(void) const;
153   - void ids(const std::string& ids);
154   - bool is_valid(const std::string& instruments,
155   - const std::string& ids) const;
156   - const bool& has_eval_indices(void) const;
157   - const std::vector<int>& eval_indices(void) const;
  134 + int size(void) const;
  135 + int scales(void) const;
  136 + GModelPar& at(const int& index);
  137 + const GModelPar& at(const int& index) const;
  138 + bool has_par(const std::string& name) const;
  139 + bool has_scales(void) const;
  140 + const std::string& name(void) const;
  141 + void name(const std::string& name);
  142 + const double& ts(void) const;
  143 + void ts(const double& ts);
  144 + const bool& tscalc(void) const;
  145 + void tscalc(const bool& tscalc);
  146 + const bool& has_ts(void) const;
  147 + std::string instruments(void) const;
  148 + void instruments(const std::string& instruments);
  149 + GModelPar& scale(const int& index);
  150 + const GModelPar& scale(const int& index) const;
  151 + GModelPar scale(const std::string& instrument) const;
  152 + void scale(const GModelPar& par);
  153 + std::string ids(void) const;
  154 + void ids(const std::string& ids);
  155 + bool is_valid(const std::string& instruments,
  156 + const std::string& ids) const;
  157 + const GModelAssociations& associations(void) const;
  158 + void associations(const GModelAssociations& associations);
  159 + const bool& has_eval_indices(void) const;
  160 + const std::vector<int>& eval_indices(void) const;
158 161  
159 162 protected:
160 163 // Protected methods
... ... @@ -173,6 +176,7 @@ protected:
173 176 std::vector<GModelPar> m_scales; //!< Model instrument scale factors
174 177 std::vector<std::string> m_ids; //!< Identifiers to which model applies
175 178 std::vector<GModelPar*> m_pars; //!< Pointers to all model parameters
  179 + GModelAssociations m_associations; //!< Model associations
176 180 bool m_has_ts; //!< Signals if TS is available
177 181 bool m_has_tscalc; //!< Signals if tscalc attribute is available
178 182 bool m_tscalc; //!< Signals if TS should be computed
... ... @@ -274,6 +278,7 @@ void GModel::name(const std::string&amp; name)
274 278 return;
275 279 }
276 280  
  281 +
277 282 /***********************************************************************//**
278 283 * @brief Return Test Statistic value
279 284 *
... ... @@ -366,6 +371,35 @@ const bool&amp; GModel::has_ts(void) const
366 371  
367 372  
368 373 /***********************************************************************//**
  374 + * @brief Return model associations
  375 + *
  376 + * @return Model associations.
  377 + *
  378 + * Returns model associations.
  379 + ***************************************************************************/
  380 +inline
  381 +const GModelAssociations& GModel::associations(void) const
  382 +{
  383 + return m_associations;
  384 +}
  385 +
  386 +
  387 +/***********************************************************************//**
  388 + * @brief Set model associations
  389 + *
  390 + * @param[in] associations Model associations.
  391 + *
  392 + * Set the model associations.
  393 + ***************************************************************************/
  394 +inline
  395 +void GModel::associations(const GModelAssociations& associations)
  396 +{
  397 + m_associations = associations;
  398 + return;
  399 +}
  400 +
  401 +
  402 +/***********************************************************************//**
369 403 * @brief Signals that parameter indices updated by eval() method were set
370 404 *
371 405 * @return True if parameter indices updated by eval() method were set.
... ...
include/GModelAssociations.hpp
... ... @@ -31,9 +31,9 @@
31 31 #include <string>
32 32 #include <vector>
33 33 #include "GContainer.hpp"
  34 +#include "GModelAssociation.hpp"
34 35  
35 36 /* __ Forward declarations _______________________________________________ */
36   -class GModelAssociation;
37 37 class GXmlElement;
38 38  
39 39  
... ...
pyext/GModel.i
... ... @@ -61,28 +61,30 @@ public:
61 61 virtual void write(GXmlElement& xml) const = 0;
62 62  
63 63 // Implemented methods
64   - int size(void) const;
65   - int scales(void) const;
66   - bool has_par(const std::string& name) const;
67   - bool has_scales(void) const;
68   - const std::string& name(void) const;
69   - void name(const std::string& name);
70   - const double& ts(void) const;
71   - void ts(const double& ts);
72   - const bool& tscalc(void) const;
73   - void tscalc(const bool& tscalc);
74   - const bool& has_ts(void) const;
75   - std::string instruments(void) const;
76   - void instruments(const std::string& instruments);
77   - GModelPar& scale(const int& index);
78   - GModelPar scale(const std::string& instrument) const;
79   - void scale(const GModelPar& par);
80   - std::string ids(void) const;
81   - void ids(const std::string& ids);
82   - bool is_valid(const std::string& instruments,
83   - const std::string& ids) const;
84   - const bool& has_eval_indices(void) const;
85   - const std::vector<int>& eval_indices(void) const;
  64 + int size(void) const;
  65 + int scales(void) const;
  66 + bool has_par(const std::string& name) const;
  67 + bool has_scales(void) const;
  68 + const std::string& name(void) const;
  69 + void name(const std::string& name);
  70 + const double& ts(void) const;
  71 + void ts(const double& ts);
  72 + const bool& tscalc(void) const;
  73 + void tscalc(const bool& tscalc);
  74 + const bool& has_ts(void) const;
  75 + std::string instruments(void) const;
  76 + void instruments(const std::string& instruments);
  77 + GModelPar& scale(const int& index);
  78 + GModelPar scale(const std::string& instrument) const;
  79 + void scale(const GModelPar& par);
  80 + std::string ids(void) const;
  81 + void ids(const std::string& ids);
  82 + bool is_valid(const std::string& instruments,
  83 + const std::string& ids) const;
  84 + const GModelAssociations& associations(void) const;
  85 + void associations(const GModelAssociations& associations);
  86 + const bool& has_eval_indices(void) const;
  87 + const std::vector<int>& eval_indices(void) const;
86 88 };
87 89  
88 90  
... ...
src/model/GModel.cpp
... ... @@ -621,6 +621,7 @@ void GModel::init_members(void)
621 621 m_scales.clear();
622 622 m_ids.clear();
623 623 m_pars.clear();
  624 + m_associations.clear();
624 625 m_ts = 0.0;
625 626 m_has_ts = false;
626 627 m_has_tscalc = false;
... ... @@ -646,6 +647,7 @@ void GModel::copy_members(const GModel&amp; model)
646 647 m_scales = model.m_scales;
647 648 m_ids = model.m_ids;
648 649 m_pars = model.m_pars;
  650 + m_associations = model.m_associations;
649 651 m_ts = model.m_ts;
650 652 m_has_ts = model.m_has_ts;
651 653 m_has_tscalc = model.m_has_tscalc;
... ... @@ -699,6 +701,9 @@ void GModel::read_attributes(const GXmlElement&amp; xml)
699 701 // Read instrument scales
700 702 read_scales(xml);
701 703  
  704 + // Read model associations
  705 + m_associations.read(xml);
  706 +
702 707 // Return
703 708 return;
704 709 }
... ... @@ -743,6 +748,9 @@ void GModel::write_attributes(GXmlElement&amp; xml) const
743 748 // Write instrument scales
744 749 write_scales(xml);
745 750  
  751 + // Write model associations
  752 + m_associations.write(xml);
  753 +
746 754 // Return
747 755 return;
748 756 }
... ... @@ -799,6 +807,17 @@ std::string GModel::print_attributes(void) const
799 807 result.append("all");
800 808 }
801 809  
  810 + // Append associations
  811 + if (!m_associations.is_empty()) {
  812 + result.append("\n"+gammalib::parformat("Associations"));
  813 + for (int i = 0; i < m_associations.size(); ++i) {
  814 + if (i > 0) {
  815 + result.append(", ");
  816 + }
  817 + result.append(m_associations[i].name());
  818 + }
  819 + }
  820 +
802 821 // Return result
803 822 return result;
804 823 }
... ...
test/data/crab_associations.xml 0 → 100644
  1 +<?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2 +<source_library title="source library">
  3 + <source name="1FGL J0005.7+3815" type="PointSource" instrument="LAT,CTA" id="000001" ts="100.100" tscalc="1">
  4 + <spectrum type="PowerLaw">
  5 + <parameter name="Prefactor" value="1.73" error="0" scale="1e-07" min="1e-07" max="1000" free="1" />
  6 + <parameter name="Index" value="-2.1" error="0" scale="1" min="-5" max="5" free="1" />
  7 + <parameter name="PivotEnergy" value="100" scale="1" min="10" max="1000000" free="0" />
  8 + </spectrum>
  9 + <spatialModel type="PointSource">
  10 + <parameter name="RA" value="83.6331" scale="1" min="-360" max="360" free="0" />
  11 + <parameter name="DEC" value="22.0145" scale="1" min="-90" max="90" free="0" />
  12 + </spatialModel>
  13 + <scaling>
  14 + <instrument name="LAT" value="1.1" scale="1" min="0.1" max="10" free="0" />
  15 + <instrument name="CTA" value="0.5" scale="1" min="0.1" max="10" free="0" />
  16 + </scaling>
  17 + <associations>
  18 + <association name="Crab nebula">
  19 + <property name="RA" value="83.6331" />
  20 + <property name="DEC" value="22.0145" />
  21 + </association>
  22 + <association name="Crab pulsar">
  23 + <property name="RA" value="83.6331" />
  24 + <property name="DEC" value="22.0145" />
  25 + </association>
  26 + </associations>
  27 + </source>
  28 +</source_library>
... ...
test/test_GModel.cpp
... ... @@ -55,6 +55,7 @@ void TestGModel::set(void)
55 55 m_temp_lightcurve = datadir + "/model_temporal_lightcurve.fits";
56 56 m_temp_phasecurve = datadir + "/model_temporal_phasecurve.fits";
57 57 m_xml_file = datadir + "/crab.xml";
  58 + m_assoc_file = datadir + "/crab_associations.xml";
58 59  
59 60 // Set model definiton XML files
60 61 m_xml_model_point_const = datadir + "/model_point_const.xml";
... ... @@ -788,6 +789,7 @@ void TestGModel::test_sky_model(void)
788 789 GXml xml2(m_xml_file);
789 790 GXmlElement* element2 = xml2.element(0)->element(0);
790 791 GModelSky sky6(*element2);
  792 + test_value(sky6.associations().size(), 0, "Check absence of associations");
791 793  
792 794 // Test has_par methods
793 795 test_assert(sky6.spatial()->has_par("RA"),
... ... @@ -814,6 +816,14 @@ void TestGModel::test_sky_model(void)
814 816 test_value(flux, flux_spectrum, "Test flux() method");
815 817 test_value(eflux, eflux_spectrum, "Test eflux() method");
816 818  
  819 + // Test associations
  820 + GXml xml3(m_assoc_file);
  821 + GXmlElement* element3 = xml3.element(0)->element(0);
  822 + GModelSky sky8(*element3);
  823 + test_value(sky8.associations().size(), 2, "Check presence of two associations");
  824 + test_assert(sky8.associations().contains("Crab nebula"), "Check presence of \"Crab nebula\" association");
  825 + test_assert(sky8.associations().contains("Crab pulsar"), "Check presence of \"Crab pulsar\" association");
  826 +
817 827 // Exit test
818 828 return;
819 829 }
... ...
test/test_GModel.hpp
... ... @@ -116,6 +116,7 @@ private:
116 116 std::string m_temp_lightcurve;
117 117 std::string m_temp_phasecurve;
118 118 std::string m_xml_file;
  119 + std::string m_assoc_file;
119 120  
120 121 // Model definiton XML files
121 122 std::string m_xml_model_point_const;
... ...