LSST Applications g1653933729+34a971ddd9,g1a997c3884+34a971ddd9,g2160c40384+da0d0eec6b,g28da252d5a+1236b942f7,g2bbee38e9b+e5a1bc5b38,g2bc492864f+e5a1bc5b38,g2ca4be77d2+192fe503f0,g2cdde0e794+704103fe75,g3156d2b45e+6e87dc994a,g347aa1857d+e5a1bc5b38,g35bb328faa+34a971ddd9,g3a166c0a6a+e5a1bc5b38,g3e281a1b8c+8ec26ec694,g4005a62e65+ba0306790b,g414038480c+9f5be647b3,g41af890bb2+c3a10c924f,g5065538af8+e7237db731,g5a0bb5165c+eae055db26,g717e5f8c0f+b65b5c3ae4,g80478fca09+4ce5a07937,g82479be7b0+08790af60f,g858d7b2824+b65b5c3ae4,g9125e01d80+34a971ddd9,ga5288a1d22+5df949a35e,gae0086650b+34a971ddd9,gb58c049af0+ace264a4f2,gbd397ab92a+2141afb137,gc28159a63d+e5a1bc5b38,gc805d3fbd4+b65b5c3ae4,gcf0d15dbbd+97632ccc20,gd6b7c0dfd1+de826e8718,gda6a2b7d83+97632ccc20,gdaeeff99f8+7774323b41,ge2409df99d+e6cadbf968,ge33fd446bb+b65b5c3ae4,ge79ae78c31+e5a1bc5b38,gf0baf85859+890af219f9,gf5289d68f6+a27069ed62,w.2024.37
LSST Data Management Base Package
Loading...
Searching...
No Matches
parameter.h
Go to the documentation of this file.
1// -*- LSST-C++ -*-
2/*
3 * This file is part of modelfit_parameters.
4 *
5 * Developed for the LSST Data Management System.
6 * This product includes software developed by the LSST Project
7 * (https://www.lsst.org).
8 * See the COPYRIGHT file at the top-level directory of this distribution
9 * for details of code ownership.
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
25#ifndef LSST_MODELFIT_PARAMETERS_PARAMETER_H
26#define LSST_MODELFIT_PARAMETERS_PARAMETER_H
27
28#include <cmath>
29#include <iostream>
30#include <limits>
31#include <memory>
32#include <set>
33#include <stdexcept>
34#include <string>
35
36#include "limits.h"
37#include "object.h"
38#include "transform.h"
39#include "type_name.h"
40#include "unit.h"
41
43
57template <typename T>
58class ParameterBase : public Object {
59public:
61 virtual T get_default() const = 0;
63 virtual std::string get_desc() const = 0;
65 virtual bool get_fixed() const = 0;
67 virtual bool get_free() const = 0;
69 virtual std::string get_label() const = 0;
71 virtual const Limits<T>& get_limits() const = 0;
73 virtual const Limits<T>& get_limits_maximal() const = 0;
75 virtual bool get_linear() const = 0;
77 virtual T get_min() const = 0;
79 virtual T get_max() const = 0;
81 virtual std::string get_name() const = 0;
83 virtual const Transform<T>& get_transform() const = 0;
85 virtual T get_transform_derivative() const = 0;
89 virtual T get_value() const = 0;
91 virtual T get_value_transformed() const = 0;
93 virtual const Unit& get_unit() const = 0;
95 virtual void set_fixed(bool fixed) = 0;
97 virtual void set_free(bool free) = 0;
99 virtual void set_label(std::string label) = 0;
101 virtual void set_limits(std::shared_ptr<const Limits<T>> limits) = 0;
103 virtual void set_transform(std::shared_ptr<const Transform<T>> transform) = 0;
105 virtual void set_value(T value) = 0;
107 virtual void set_value_transformed(T value_transformed) = 0;
109 virtual void set_unit(std::shared_ptr<const Unit> unit = nullptr) = 0;
110
112
113 friend bool operator==(const ParameterBase<T>& first, const ParameterBase<T>& second) {
114 return &first == &second;
115 }
116
117 friend bool operator!=(const ParameterBase<T>& first, const ParameterBase<T>& second) {
118 return &first != &second;
119 }
120
121 friend bool operator<(const ParameterBase<T>& first, const ParameterBase<T>& second) {
122 return std::less<const ParameterBase<T>*>{}(&first, &second) == true;
123 }
124
125 virtual ~ParameterBase() = default;
126};
127
149template <typename T, class C>
151private:
152 // These classes can store the default const Limits/Transform refs,
153 // in case the unique_ptr thereof is null.
154
156 struct Limiter {
157 const Limits<T>& limits;
158 Limiter(const Limits<T>& limits_in) : limits(limits_in){};
159 };
160
162 struct Transformer {
163 const Transform<T>& transform;
164 Transformer(const Transform<T>& transform_in) : transform(transform_in){};
165 };
166
168 static constexpr T _default = 0;
170 static constexpr T _min = -std::numeric_limits<T>::infinity();
172 static constexpr T _max = std::numeric_limits<T>::infinity();
174 static constexpr bool _linear = false;
176 bool _free = true;
180 std::unique_ptr<Transformer> _transformer;
182 std::string _label;
189
191 void _set_value(T value) {
192 if (!(get_limits().check(value)))
193 throw std::runtime_error(this->str() + "Value=" + std::to_string(value)
194 + " beyond get_limits()=" + get_limits().str());
195 _value = value;
196 }
197
198protected:
203
204 static const std::string _get_desc() { return C::_desc; }
205 static constexpr bool _get_linear() { return C::_linear; }
206 static constexpr T _get_min() { return C::_min; }
207 static constexpr T _get_max() { return C::_max; }
208 static const std::string _get_name() { return C::_name; }
209
210public:
212 static constexpr T _get_default() { return C::_default; }
213
214 std::string get_desc() const override { return _get_desc(); }
215
216 T get_default() const override { return _get_default(); }
217
218 bool get_fixed() const override { return !_free; }
219
220 bool get_free() const override { return _free; }
221
222 std::string get_label() const override { return _label; }
223
224 const Limits<T>& get_limits_maximal() const override {
225 static const Limits<T> limits_maximal
226 = Limits<T>(_get_min(), _get_max(), std::string(type_name<C>()) + ".limits_maximal");
227 return limits_maximal;
228 }
229
230 const Limits<T>& get_limits() const override { return _limiter->limits; }
231
232 bool get_linear() const override { return _get_linear(); }
233
234 T get_min() const override { return _get_min(); }
235
236 T get_max() const override { return _get_max(); }
237
238 std::string get_name() const override { return _get_name(); }
239
240 const Transform<T>& get_transform() const override { return _transformer->transform; }
241
242 std::shared_ptr<const Transform<T>> get_transform_ptr() const override { return _transform_ptr; }
243
244 T get_transform_derivative() const override {
245 return this->get_transform().derivative(this->get_value());
246 }
248 static const std::string get_type_name(bool strip_namespace_separator = false,
249 const std::string_view& namespace_separator
251 return type_name_str<C>(strip_namespace_separator, namespace_separator);
252 }
253
254 const Unit& get_unit() const override { return *_unit_ptr; }
255
256 T get_value() const override { return _value; }
257
258 T get_value_transformed() const override { return _value_transformed; }
259
262
263 void set_fixed(bool fixed) override { set_free(!fixed); }
264 void set_free(bool free) override { _free = free; }
265 void set_label(std::string label) override { _label = std::move(label); }
266 void set_limits(std::shared_ptr<const Limits<T>> limits) override {
267 // TODO: Fix bad_alloc when calling this without &
268 // Disable copy constructor explicitly maybe?
269 const auto& limits_maximal = this->get_limits_maximal();
270 if (limits == nullptr) {
271 _limiter = std::make_unique<Limiter>(limits_maximal);
272 } else {
273 if (!((limits->get_min() >= this->get_min()) && (limits->get_max() <= this->get_max()))) {
274 std::string error = get_type_name() + ".set_limits(" + limits->str()
275 + ") sets limits that are less restrictive than the minimum="
276 + limits_maximal.str();
277 throw std::runtime_error(error);
278 }
279 _limits_ptr = std::move(limits);
280 _limiter = std::make_unique<Limiter>(*_limits_ptr);
281 }
282 }
283 void set_transform(const std::shared_ptr<const Transform<T>> transform) override {
284 if (transform == nullptr) {
285 // TODO: determine why passing transform_none as arg here returns:
286 // error: modification of '<temporary>' is not a constant expression
287 // whereas get_transform_unit<T>() results in a segfault
288 // (iff Transform has a virtual destructor)
289 _transformer = std::make_unique<Transformer>(this->transform_none());
290 } else {
291 _transform_ptr = std::move(transform);
292 _transformer = std::make_unique<Transformer>(*_transform_ptr);
293 }
294 _value_transformed = _transformer->transform.forward(_value);
295 }
296
297 void set_value(T value) override {
298 _set_value(value);
299 double value_new = this->get_value();
300 _value_transformed = _transformer->transform.forward(value_new);
301 };
302
303 void set_value_transformed(T value_transformed) override {
304 _set_value(_transformer->transform.reverse(value_transformed));
305 _value_transformed = _transformer->transform.forward(this->get_value());
306 }
307
308 void set_unit(std::shared_ptr<const Unit> unit = nullptr) override {
309 _unit_ptr = unit == nullptr ? nullptr : std::move(unit);
310 }
311
312 std::string repr(bool name_keywords = false, const std::string_view& namespace_separator
313 = Object::CC_NAMESPACE_SEPARATOR) const override {
314 return get_type_name(false, namespace_separator) + "(" + (name_keywords ? "value=" : "")
315 + std::to_string(_value) + ", " + (name_keywords ? "limits=" : "") + get_limits().repr() + ", "
316 + (name_keywords ? "transform=" : "") + get_transform().repr() + ", "
317 + (name_keywords ? "fixed=" : "") + std::to_string(0 + get_fixed()) + ", "
318 + (name_keywords ? "label='" : "'") + _label + "')";
319 }
320
321 std::string str() const override {
322 return get_type_name(true) + "(value=" + std::to_string(_value)
323 + ", "
324 // TODO: Implement equality operators for limits/transforms
325 + ((&get_limits() == &get_limits_maximal()) ? "" : ("limits=" + get_limits().repr() + ", "))
326 + ((&get_transform() == &(this->transform_none()))
327 ? ""
328 : ("transform=" + get_transform().repr() + ", "))
329 + (!get_fixed() ? "" : (std::string("fixed=") + std::to_string(0 + get_fixed()) + ", "))
330 + ((get_label() == "") ? "" : ("label='" + get_label() + "'")) + ")";
331 }
332
343 Parameter(T value = _get_default(), std::shared_ptr<const Limits<T>> limits = nullptr,
344 const std::shared_ptr<const Transform<T>> transform = nullptr,
345 std::shared_ptr<const Unit> unit = nullptr, bool fixed = false, std::string label = "")
346 : ParameterBase<T>() {
347 set_limits(limits);
348 _value = value;
349 set_transform(transform == nullptr ? nullptr : std::move(transform));
350 set_unit(unit);
351 set_fixed(fixed);
352 set_label(label);
353 }
355};
356} // namespace lsst::modelfit::parameters
357#endif // LSST_MODELFIT_PARAMETERS_PARAMETER_H
Range-based limits for parameter values.
Definition limits.h:45
A generic object from the parameters library.
Definition object.h:39
static constexpr std::string_view CC_NAMESPACE_SEPARATOR
The C++ namespace separator.
Definition object.h:42
Interface for parameters with values and metadata.
Definition parameter.h:58
virtual T get_max() const =0
Return the maximum value for this parameter instance.
virtual T get_default() const =0
Get the default value.
virtual std::string get_desc() const =0
Get a string description for this parameter class.
virtual void set_unit(std::shared_ptr< const Unit > unit=nullptr)=0
Set the unit for this parameter instance.
virtual T get_min() const =0
Return the minimum value for this parameter instance.
virtual void set_fixed(bool fixed)=0
Set the parameter to be fixed (or not).
static const UnitTransform< T > & transform_none()
Definition parameter.h:111
virtual const Limits< T > & get_limits() const =0
Return the limits for the untransformed value.
virtual const Unit & get_unit() const =0
Return the unit of this parameter instance.
virtual void set_label(std::string label)=0
Set the string label for this parameter instance.
virtual T get_transform_derivative() const =0
Return the derivative of the transform for this parameter instance.
virtual void set_limits(std::shared_ptr< const Limits< T > > limits)=0
Set the limits for this parameter instance.
friend bool operator<(const ParameterBase< T > &first, const ParameterBase< T > &second)
Definition parameter.h:121
virtual void set_value(T value)=0
Set the untransformed value for this parameter instance.
virtual bool get_fixed() const =0
Return whether the parameter is fixed (not free).
virtual const Limits< T > & get_limits_maximal() const =0
Return limits representing the maximum/minimum untransformed value.
virtual void set_free(bool free)=0
Set the parameter to be free (or not).
virtual std::shared_ptr< const Transform< T > > get_transform_ptr() const =0
Return the transform pointer for this parameter instance.
virtual std::string get_name() const =0
Get a string name for this parameter class.
virtual bool get_free() const =0
Return whether the parameter is free (not fixed).
virtual std::string get_label() const =0
Return a string label for this parameter instance.
friend bool operator==(const ParameterBase< T > &first, const ParameterBase< T > &second)
Definition parameter.h:113
virtual const Transform< T > & get_transform() const =0
Return the transforming function for this parameter instance.
virtual bool get_linear() const =0
Return whether the parameter is linear.
virtual void set_value_transformed(T value_transformed)=0
Set the transformed value for this parameter instance.
virtual void set_transform(std::shared_ptr< const Transform< T > > transform)=0
Set the transforming function for this parameter instance.
friend bool operator!=(const ParameterBase< T > &first, const ParameterBase< T > &second)
Definition parameter.h:117
virtual T get_value() const =0
Return the untransformed value of this parameter instance.
virtual T get_value_transformed() const =0
Return the transformed value of this parameter instance.
A parameter with values and metadata.
Definition parameter.h:150
std::string get_label() const override
Return a string label for this parameter instance.
Definition parameter.h:222
std::string get_desc() const override
Get a string description for this parameter class.
Definition parameter.h:214
const Unit & get_unit() const override
Return the unit of this parameter instance.
Definition parameter.h:254
bool get_fixed() const override
Return whether the parameter is fixed (not free).
Definition parameter.h:218
T _value
The untransformed value.
Definition parameter.h:200
static constexpr T _get_default()
Get the default value for the derived type of this.
Definition parameter.h:212
static const std::string get_type_name(bool strip_namespace_separator=false, const std::string_view &namespace_separator=detail::NAMESPACE_SEPARATOR)
Get the name of the derived type of this.
Definition parameter.h:248
T _value_transformed
The cached, transformed value.
Definition parameter.h:202
Parameter(T value=_get_default(), std::shared_ptr< const Limits< T > > limits=nullptr, const std::shared_ptr< const Transform< T > > transform=nullptr, std::shared_ptr< const Unit > unit=nullptr, bool fixed=false, std::string label="")
Initialize a Parameter.
Definition parameter.h:343
std::shared_ptr< C > ptr()
Return a shared pointer to this.
Definition parameter.h:261
T get_value() const override
Return the untransformed value of this parameter instance.
Definition parameter.h:256
const Limits< T > & get_limits_maximal() const override
Return limits representing the maximum/minimum untransformed value.
Definition parameter.h:224
void set_transform(const std::shared_ptr< const Transform< T > > transform) override
Set the transforming function for this parameter instance.
Definition parameter.h:283
const Transform< T > & get_transform() const override
Return the transforming function for this parameter instance.
Definition parameter.h:240
T get_max() const override
Return the maximum value for this parameter instance.
Definition parameter.h:236
void set_label(std::string label) override
Set the string label for this parameter instance.
Definition parameter.h:265
std::string get_name() const override
Get a string name for this parameter class.
Definition parameter.h:238
void set_limits(std::shared_ptr< const Limits< T > > limits) override
Set the limits for this parameter instance.
Definition parameter.h:266
T get_transform_derivative() const override
Return the derivative of the transform for this parameter instance.
Definition parameter.h:244
T get_default() const override
Get the default value.
Definition parameter.h:216
std::shared_ptr< const Transform< T > > get_transform_ptr() const override
Return the transform pointer for this parameter instance.
Definition parameter.h:242
void set_free(bool free) override
Set the parameter to be free (or not).
Definition parameter.h:264
std::string str() const override
Return a brief, human-readable string representation of this.
Definition parameter.h:321
void set_unit(std::shared_ptr< const Unit > unit=nullptr) override
Set the unit for this parameter instance.
Definition parameter.h:308
std::string repr(bool name_keywords=false, const std::string_view &namespace_separator=Object::CC_NAMESPACE_SEPARATOR) const override
Return a full, callable string representation of this.
Definition parameter.h:312
static const std::string _get_desc()
Definition parameter.h:204
bool get_free() const override
Return whether the parameter is free (not fixed).
Definition parameter.h:220
static const std::string _get_name()
Definition parameter.h:208
static constexpr bool _get_linear()
Definition parameter.h:205
bool get_linear() const override
Return whether the parameter is linear.
Definition parameter.h:232
T get_min() const override
Return the minimum value for this parameter instance.
Definition parameter.h:234
void set_value_transformed(T value_transformed) override
Set the transformed value for this parameter instance.
Definition parameter.h:303
void set_fixed(bool fixed) override
Set the parameter to be fixed (or not).
Definition parameter.h:263
const Limits< T > & get_limits() const override
Return the limits for the untransformed value.
Definition parameter.h:230
T get_value_transformed() const override
Return the transformed value of this parameter instance.
Definition parameter.h:258
void set_value(T value) override
Set the untransformed value for this parameter instance.
Definition parameter.h:297
A reversible transformation of a real scalar value.
Definition transform.h:45
A generic interface for physical units described by strings.
Definition unit.h:35
static const UnitTransform< T > & get()
Definition transform.h:64
T infinity(T... args)
T move(T... args)
constexpr std::string_view NAMESPACE_SEPARATOR
Definition type_name.h:67
T to_string(T... args)