LSST Applications g063fba187b+cac8b7c890,g0f08755f38+6aee506743,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g1a2382251a+b4475c5878,g1dcb35cd9c+8f9bc1652e,g20f6ffc8e0+6aee506743,g217e2c1bcf+73dee94bd0,g28da252d5a+1f19c529b9,g2bbee38e9b+3f2625acfc,g2bc492864f+3f2625acfc,g3156d2b45e+6e55a43351,g32e5bea42b+1bb94961c2,g347aa1857d+3f2625acfc,g35bb328faa+a8ce1bb630,g3a166c0a6a+3f2625acfc,g3e281a1b8c+c5dd892a6c,g3e8969e208+a8ce1bb630,g414038480c+5927e1bc1e,g41af890bb2+8a9e676b2a,g7af13505b9+809c143d88,g80478fca09+6ef8b1810f,g82479be7b0+f568feb641,g858d7b2824+6aee506743,g89c8672015+f4add4ffd5,g9125e01d80+a8ce1bb630,ga5288a1d22+2903d499ea,gb58c049af0+d64f4d3760,gc28159a63d+3f2625acfc,gcab2d0539d+b12535109e,gcf0d15dbbd+46a3f46ba9,gda6a2b7d83+46a3f46ba9,gdaeeff99f8+1711a396fd,ge79ae78c31+3f2625acfc,gef2f8181fd+0a71e47438,gf0baf85859+c1f95f4921,gfa517265be+6aee506743,gfa999e8aa5+17cd334064,w.2024.51
LSST Data Management Base Package
Loading...
Searching...
No Matches
gaussiancomponent.cc
Go to the documentation of this file.
1#include <stdexcept>
2
6
13
14namespace lsst::gauss2d::fit {
15// This is the gauss2d convention; see evaluator.h
16static const std::array<size_t, N_PARAMS_GAUSS2D> IDX_ORDER = {0, 1, 3, 4, 5, 2};
17static const size_t N_PARAMS_INTEGRAL_MAX = 2;
18static const size_t N_PARAMS_EXTRA_INTEGRAL_MAX = N_PARAMS_INTEGRAL_MAX - 1;
19
25
26ParamCRefs GaussianComponent::_get_parameters_grad(const Channel& channel) const {
27 ParamCRefs params;
28 ParamFilter filter{};
29 filter.channel = channel;
30 _centroid->get_parameters_const(params, &filter);
31 _ellipse->get_parameters_const(params, &filter);
32
33 const size_t n_params_nonint = params.size();
34 size_t n_params_expect = N_PARAMS_GAUSS2D - 1;
35
36 if (n_params_nonint != n_params_expect) {
37 throw std::runtime_error(this->str() + " (centroid + ellipse).get_parameters_const(channel="
38 + channel.str() + ").size())=" + std::to_string(params.size())
39 + "!=(N_PARAMS_GAUSS2D - 1)=" + std::to_string(n_params_expect));
40 }
41
42 ParamCRefs params_int;
43 this->_integralmodel->get_parameters_const(params_int, &filter);
44
45 size_t n_params_int = params_int.size();
46 if (!(n_params_int <= N_PARAMS_INTEGRAL_MAX)) {
47 throw std::runtime_error(this->str() + "->_integralmodel->get_parameters_const(channel="
48 + channel.str() + ").size())=" + std::to_string(params_int.size())
49 + "!<=" + std::to_string(N_PARAMS_INTEGRAL_MAX));
50 }
51 for (auto param : params_int) params.push_back(param);
52 return params;
53}
54
56 const GradParamMap& map_grad, ParameterMap& offsets) const {
57 auto factors_int = _integralmodel->get_integral_derivative_factors(channel);
58 const size_t n_factors = factors_int.size();
59 if (!(n_factors <= N_PARAMS_EXTRA_INTEGRAL_MAX)) {
60 throw std::runtime_error(_integralmodel->str() + ".get_integral_derivative_factors(" + channel.str()
61 + ").size()=" + std::to_string(n_factors)
62 + "!<=" + std::to_string(N_PARAMS_EXTRA_INTEGRAL_MAX));
63 }
64 size_t idx = 0;
65 size_t offset = 0;
66
67 for (const auto& factor : factors_int) {
68 const auto& param = factor.first.get();
69 if (!param.get_fixed()) {
70 auto found = offsets.find(param);
71 if (found == offsets.end()) {
72 throw std::runtime_error("param=" + param.str() + " not found in offsets;"
73 " was add_grad_param_map called first?");
74 }
75 offset = (*found).second;
76 idx = map_grad.size() - 1;
77 }
78 }
79 map_extra.push_back({idx, offset});
80}
81
83 factors.push_back({0, 0, 0});
84}
85
87 ParameterMap& offsets) const {
88 auto params = nonconsecutive_unique(this->_get_parameters_grad(channel));
89 map.push_back({0, 0, 0, 0, 0, 0});
90 auto& values = map.back();
91
92 size_t size_map = offsets.size();
93 size_t index_param_map;
94 for (size_t idx_param = 0; idx_param < N_PARAMS_GAUSS2D; ++idx_param) {
95 const size_t& order_param = IDX_ORDER[idx_param];
96 // The parameters must be in the same order as returned by get_parameters(_const)
97 const auto& param = params.at(idx_param).get();
98 if (!param.get_fixed()) {
99 if (offsets.find(param) == offsets.end()) {
100 index_param_map = ++size_map;
101 offsets[param] = index_param_map;
102 } else {
103 index_param_map = offsets[param];
104 }
105 values[order_param] = index_param_map;
106 }
107 }
108 const size_t n_params = params.size();
109 // Add offset for any extra integral parameters
110 for (size_t idx_param = N_PARAMS_GAUSS2D; idx_param < n_params; ++idx_param) {
111 const auto& param = params.at(idx_param).get();
112 if (!param.get_fixed() && (offsets.find(param) == offsets.end())) {
113 offsets[param] = ++size_map;
114 }
115 }
116}
117
119 factors.push_back({1, 1, 1, 1, 1, 1});
120}
121
123 const Channel& channel) const {
124 lsst::gauss2d::Gaussians::Data gaussians = {std::make_shared<Gaussian>(
125 std::make_shared<Centroid>(this->_centroid), std::make_shared<Ellipse>(this->_ellipsedata),
126 std::make_shared<GaussianModelIntegral>(channel, this->_integralmodel))};
127 return std::make_unique<const lsst::gauss2d::Gaussians>(gaussians);
128}
129
130size_t GaussianComponent::get_n_gaussians(const Channel& channel) const { return 1; };
131
134 return params;
135}
136
139 return params;
140}
141
143 size_t index) const {
144 auto factors_int = _integralmodel->get_integral_derivative_factors(channel);
145 const size_t n_factors = factors_int.size();
146 if (!(n_factors <= 1)) {
147 throw std::runtime_error(this->str() + ".set_extra_param_factors(" + channel.str()
148 + ", index=" + std::to_string(index) + " has " + std::to_string(n_factors)
149 + " parameter factors" + "; only a maximum of one is supported.");
150 }
151 auto& row = factors[index];
152
153 if (n_factors > 0) {
154 row[0] = factors_int[0].second[0] / factors_int[0].first.get().get_transform_derivative();
155 } else {
156 row[0] = 0;
157 }
158 row[1] = 0;
159 row[2] = 0;
160}
161
163 size_t index) const {
164 auto params = this->_get_parameters_grad(channel);
165 auto& values = factors.at(index);
166
167 for (size_t idx_param = 0; idx_param < N_PARAMS_GAUSS2D; ++idx_param) {
168 const size_t& order_param = IDX_ORDER[idx_param];
169 // The parameters must be in the same order as returned by get_parameters(_const)
170 const auto& param = params[idx_param].get();
171 if (param.get_fixed()) {
172 values[order_param] = 0;
173 } else {
174 auto deriv = param.get_transform_derivative();
175 if (deriv == 0) {
176 throw std::runtime_error("Param[idx=" + std::to_string(idx_param) + "]=" + param.str()
177 + " get_transform_derivative=0 (will result in divide by 0)");
178 }
179 values[order_param] = 1.0 / deriv;
180 }
181 }
182}
183
184std::string GaussianComponent::repr(bool name_keywords, std::string_view namespace_separator) const {
185 return type_name_str<GaussianComponent>(false, namespace_separator) + "("
186 + EllipticalComponent::repr(name_keywords, namespace_separator) + ")";
187}
188
190 return type_name_str<GaussianComponent>(true) + "(" + EllipticalComponent::str() + ")";
191}
192
193} // namespace lsst::gauss2d::fit
T at(T... args)
T back(T... args)
An observational channel, usually representing some range of wavelengths of light.
Definition channel.h:29
std::string str() const override
Return a brief, human-readable string representation of this.
Definition channel.cc:104
A Component with an elliptically-symmetric intensity profile.
std::string repr(bool name_keywords=false, std::string_view namespace_separator=Object::CC_NAMESPACE_SEPARATOR) const override
Return a full, callable string representation of this.
std::string str() const override
Return a brief, human-readable string representation of this.
std::shared_ptr< CentroidParameters > _centroid
ParamCRefs & get_parameters_const(ParamCRefs &params, ParamFilter *filter=nullptr) const override
Same as get_parameters(), but for const refs.
std::shared_ptr< IntegralModel > _integralmodel
ParamRefs & get_parameters(ParamRefs &params, ParamFilter *filter=nullptr) const override
Add Parameter refs matching the filter to a vector, in order.
std::shared_ptr< ParametricEllipse > _ellipse
size_t get_n_gaussians(const Channel &channel) const override
Return the number of Gaussian sub-components controlled by this model.
ParamCRefs & get_parameters_const(ParamCRefs &params, ParamFilter *filter=nullptr) const override
Same as get_parameters(), but for const refs.
void add_extra_param_factors(const Channel &channel, ExtraParamFactors &factors) const override
Add extra Parameter gradient factors to an existing vector.
void set_extra_param_factors(const Channel &channel, ExtraParamFactors &factors, size_t index) const override
Set extra Parameter gradient factors in an existing map.
GaussianComponent(std::shared_ptr< GaussianParametricEllipse > ellipse=nullptr, std::shared_ptr< CentroidParameters > centroid=nullptr, std::shared_ptr< IntegralModel > integralmodel=nullptr)
Construct a GaussianComponent from ellipse, centroid and integral parameters.
std::string repr(bool name_keywords=false, std::string_view namespace_separator=Object::CC_NAMESPACE_SEPARATOR) const override
Return a full, callable string representation of this.
ParamRefs & get_parameters(ParamRefs &params, ParamFilter *filter=nullptr) const override
Add Parameter refs matching the filter to a vector, in order.
void add_grad_param_factors(const Channel &channel, GradParamFactors &factor) const override
Add Parameter gradient factors to an existing map.
void add_grad_param_map(const Channel &channel, GradParamMap &map, ParameterMap &offsets) const override
Add Parameter gradient indices to an existing map.
void set_grad_param_factors(const Channel &channel, GradParamFactors &factors, size_t index) const override
Set Parameter gradient factors in an existing map.
void add_extra_param_map(const Channel &channel, ExtraParamMap &map_extra, const GradParamMap &map_grad, ParameterMap &offsets) const override
Add extra Parameter indices to a map.
std::string str() const override
Return a brief, human-readable string representation of this.
std::unique_ptr< const lsst::gauss2d::Gaussians > get_gaussians(const Channel &channel) const override
Return the vector of Gaussian sub-components controlled by this model.
std::shared_ptr< GaussianParametricEllipse > _ellipsedata
std::vector< T > nonconsecutive_unique(const std::vector< T > &vec)
Definition util.h:92
std::vector< ParamBaseCRef > ParamCRefs
Definition param_defs.h:11
const size_t N_PARAMS_GAUSS2D
Definition evaluate.h:61
STL namespace.
T push_back(T... args)
T size(T... args)
int row
Definition CR.cc:145
Options for filtering Parameter instances.
std::optional< std::reference_wrapper< const Channel > > channel
T to_string(T... args)