LSST Applications g0f08755f38+9c285cab97,g1635faa6d4+13f3999e92,g1653933729+a8ce1bb630,g1a0ca8cf93+bf6eb00ceb,g28da252d5a+0829b12dee,g29321ee8c0+5700dc9eac,g2bbee38e9b+9634bc57db,g2bc492864f+9634bc57db,g2cdde0e794+c2c89b37c4,g3156d2b45e+41e33cbcdc,g347aa1857d+9634bc57db,g35bb328faa+a8ce1bb630,g3a166c0a6a+9634bc57db,g3e281a1b8c+9f2c4e2fc3,g414038480c+077ccc18e7,g41af890bb2+fde0dd39b6,g5fbc88fb19+17cd334064,g781aacb6e4+a8ce1bb630,g80478fca09+55a9465950,g82479be7b0+d730eedb7d,g858d7b2824+9c285cab97,g9125e01d80+a8ce1bb630,g9726552aa6+10f999ec6a,ga5288a1d22+2a84bb7594,gacf8899fa4+c69c5206e8,gae0086650b+a8ce1bb630,gb58c049af0+d64f4d3760,gc28159a63d+9634bc57db,gcf0d15dbbd+4b7d09cae4,gda3e153d99+9c285cab97,gda6a2b7d83+4b7d09cae4,gdaeeff99f8+1711a396fd,ge2409df99d+5e831397f4,ge79ae78c31+9634bc57db,gf0baf85859+147a0692ba,gf3967379c6+41c94011de,gf3fb38a9a8+8f07a9901b,gfb92a5be7c+9c285cab97,w.2024.46
LSST Data Management Base Package
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | List of all members
lsst::afw::table::CovarianceMatrixKey< T, N > Class Template Referenceabstract

#include <aggregates.h>

Inheritance diagram for lsst::afw::table::CovarianceMatrixKey< T, N >:
lsst::afw::table::FunctorKey< Eigen::Matrix< T, N, N > > lsst::afw::table::OutputFunctorKey< T > lsst::afw::table::InputFunctorKey< T >

Public Types

using ErrKeyArray = std::vector<Key<T>>
 
using CovarianceKeyArray = std::vector<Key<T>>
 
using NameArray = std::vector<std::string>
 
using Value
 The data type for get and set.
 

Public Member Functions

 CovarianceMatrixKey ()
 Construct an invalid instance; must assign before subsequent use.
 
 CovarianceMatrixKey (ErrKeyArray const &err, CovarianceKeyArray const &cov=CovarianceKeyArray())
 Construct a from arrays of per-element Keys.
 
 CovarianceMatrixKey (SubSchema const &s, NameArray const &names)
 Construct from a subschema and an array of names for each parameter of the matrix.
 
 CovarianceMatrixKey (CovarianceMatrixKey const &)
 
 CovarianceMatrixKey (CovarianceMatrixKey &&)
 
CovarianceMatrixKeyoperator= (CovarianceMatrixKey const &)
 
CovarianceMatrixKeyoperator= (CovarianceMatrixKey &&)
 
 ~CovarianceMatrixKey () noexcept override
 
Eigen::Matrix< T, N, Nget (BaseRecord const &record) const override
 Get a covariance matrix from the given record.
 
void set (BaseRecord &record, Eigen::Matrix< T, N, N > const &value) const override
 Set a covariance matrix in the given record (uses only the lower triangle of the given matrix)
 
getElement (BaseRecord const &record, int i, int j) const
 Return the element in row i and column j.
 
void setElement (BaseRecord &record, int i, int j, T value) const
 Set the element in row i and column j.
 
bool isValid () const noexcept
 Return True if all the constituent error Keys are valid.
 
bool operator== (CovarianceMatrixKey const &other) const noexcept
 Compare the FunctorKey for equality with another, using its constituent Keys.
 
bool operator!= (CovarianceMatrixKey const &other) const noexcept
 
std::size_t hash_value () const noexcept
 Return a hash of this object.
 
virtual void set (BaseRecord &record, T const &value) const =0
 

Static Public Member Functions

static CovarianceMatrixKey addFields (Schema &schema, std::string const &prefix, NameArray const &names, std::string const &unit, bool diagonalOnly=false)
 Add covariance matrix fields to a Schema, and return a CovarianceMatrixKey to manage them.
 
static CovarianceMatrixKey addFields (Schema &schema, std::string const &prefix, NameArray const &names, NameArray const &units, bool diagonalOnly=false)
 Add covariance matrix fields to a Schema, and return a CovarianceMatrixKey to manage them.
 

Detailed Description

template<typename T, int N>
class lsst::afw::table::CovarianceMatrixKey< T, N >

Definition at line 519 of file aggregates.h.

Member Typedef Documentation

◆ CovarianceKeyArray

template<typename T , int N>
using lsst::afw::table::CovarianceMatrixKey< T, N >::CovarianceKeyArray = std::vector<Key<T>>

Definition at line 522 of file aggregates.h.

◆ ErrKeyArray

template<typename T , int N>
using lsst::afw::table::CovarianceMatrixKey< T, N >::ErrKeyArray = std::vector<Key<T>>

Definition at line 521 of file aggregates.h.

◆ NameArray

template<typename T , int N>
using lsst::afw::table::CovarianceMatrixKey< T, N >::NameArray = std::vector<std::string>

Definition at line 523 of file aggregates.h.

◆ Value

using lsst::afw::table::FunctorKey< T >::Value
inherited

The data type for get and set.

Definition at line 77 of file FunctorKey.h.

Constructor & Destructor Documentation

◆ CovarianceMatrixKey() [1/5]

template<typename T , int N>
lsst::afw::table::CovarianceMatrixKey< T, N >::CovarianceMatrixKey ( )
default

Construct an invalid instance; must assign before subsequent use.

◆ CovarianceMatrixKey() [2/5]

template<typename T , int N>
lsst::afw::table::CovarianceMatrixKey< T, N >::CovarianceMatrixKey ( ErrKeyArray const & err,
CovarianceKeyArray const & cov = CovarianceKeyArray() )
explicit

Construct a from arrays of per-element Keys.

The err array Keys should point to the square root of the diagonal of the covariance matrix. The cov array Keys should point to the off-diagonal elements of the lower-triangle, packed first in rows, then in columns (or equivalently, in the upper-triangle, packed first in columns, then in rows). For a 4x4 matrix, the order is is:

err[0]^2   cov[0]     cov[1]     cov[3]
cov[0]     err[1]^2   cov[2]     cov[4]
cov[1]     cov[2]     err[2]^2   cov[5]
cov[3]     cov[4]     cov[5]     err[3]^2

The cov array may also be empty, to indicate that no off-diagonal elements are stored, and should be set to zero. If not empty, the size of the cov matrix must be exactly n*(n-1)/2, where n is the size of the err matrix.

Definition at line 222 of file aggregates.cc.

223 : _err(err), _cov(cov) {
224 if (N != Eigen::Dynamic) {
225 LSST_THROW_IF_NE(err.size(), std::size_t(N), pex::exceptions::LengthError,
226 "Size of err array (%d) does not match template argument (%d)");
227 }
228 if (!cov.empty()) {
229 LSST_THROW_IF_NE(cov.size(), err.size() * (err.size() - 1) / 2, pex::exceptions::LengthError,
230 "Size of cov array (%d) is does not match with size inferred from err array (%d)");
231 bool haveCov = false;
232 for (typename CovarianceKeyArray::const_iterator i = _cov.begin(); i != _cov.end(); ++i) {
233 if (i->isValid()) haveCov = true;
234 }
235 if (!haveCov) _cov.resize(0);
236 }
237}
#define LSST_THROW_IF_NE(N1, N2, EXC_CLASS, MSG)
Check whether the given values are equal, and throw an LSST Exception if they are not.
Definition asserts.h:38
T begin(T... args)
T end(T... args)
T resize(T... args)

◆ CovarianceMatrixKey() [3/5]

template<typename T , int N>
lsst::afw::table::CovarianceMatrixKey< T, N >::CovarianceMatrixKey ( SubSchema const & s,
NameArray const & names )

Construct from a subschema and an array of names for each parameter of the matrix.

The field names should match the following convention:

  • diagonal elements should have names like "p1Err", where "p1" is the name of the parameter, and should contain the square root of the variance in that parameter.
  • off-diagonal elements hould have names like "p1_p2_Cov", where "p1" and "p2" are names of parameters. For example, for the covariance matrix of a position, we'd look for "xErr", "yErr", and "x_y_Cov".

Definition at line 240 of file aggregates.cc.

241 : _err(names.size()), _cov(names.size() * (names.size() - 1) / 2) {
242 int const n = names.size();
243 int k = 0;
244 bool haveCov = false;
245 for (int i = 0; i < n; ++i) {
246 _err[i] = s[names[i] + "Err"];
247 for (int j = 0; j < i; ++j, ++k) {
248 try {
249 _cov[k] = s[names[i] + "_" + names[j] + "_Cov"];
250 haveCov = true;
251 } catch (pex::exceptions::NotFoundError &) {
252 try {
253 _cov[k] = s[names[j] + "_" + names[i] + "_Cov"];
254 haveCov = true;
255 } catch (pex::exceptions::NotFoundError &) {
256 }
257 }
258 }
259 }
260 if (!haveCov) _cov.resize(0);
261}
T size(T... args)

◆ CovarianceMatrixKey() [4/5]

template<typename T , int N>
lsst::afw::table::CovarianceMatrixKey< T, N >::CovarianceMatrixKey ( CovarianceMatrixKey< T, N > const & )
default

◆ CovarianceMatrixKey() [5/5]

template<typename T , int N>
lsst::afw::table::CovarianceMatrixKey< T, N >::CovarianceMatrixKey ( CovarianceMatrixKey< T, N > && )
default

◆ ~CovarianceMatrixKey()

template<typename T , int N>
lsst::afw::table::CovarianceMatrixKey< T, N >::~CovarianceMatrixKey ( )
overridedefaultnoexcept

Member Function Documentation

◆ addFields() [1/2]

template<typename T , int N>
CovarianceMatrixKey< T, N > lsst::afw::table::CovarianceMatrixKey< T, N >::addFields ( Schema & schema,
std::string const & prefix,
NameArray const & names,
NameArray const & units,
bool diagonalOnly = false )
static

Add covariance matrix fields to a Schema, and return a CovarianceMatrixKey to manage them.

Parameters
[out]schemaSchema to add fields to.
[in]prefixString used to form the first part of all field names. Suffixes of the form '_xErr' and '_x_y_Cov' will be added to form the full field names (using names={'x', 'y'} as an example).
[in]unitsVector of units for for error (standard deviation) values; covariance matrix elements will have "{units[i]} {units[j]}" or "{units[i]}^2", depending on whether units[i] == units[j].
[in]namesVector of strings containing the names of the quantities the covariance matrix represents the uncertainty of.
[in]diagonalOnlyIf true, only create fields for the error values.

Definition at line 183 of file aggregates.cc.

185 {
186 if (N != Eigen::Dynamic) {
187 LSST_THROW_IF_NE(names.size(), std::size_t(N), pex::exceptions::LengthError,
188 "Size of names array (%d) does not match template argument (%d)");
189 LSST_THROW_IF_NE(units.size(), std::size_t(N), pex::exceptions::LengthError,
190 "Size of units array (%d) does not match template argument (%d)");
191 }
192 ErrKeyArray err;
194 err.reserve(names.size());
195 for (std::size_t i = 0; i < names.size(); ++i) {
196 err.push_back(schema.addField<T>(schema.join(prefix, names[i] + "Err"),
197 "1-sigma uncertainty on " + names[i], units[i]));
198 }
199 if (!diagonalOnly) {
200 cov.reserve((names.size() * (names.size() - 1)) / 2);
201 for (std::size_t i = 0; i < names.size(); ++i) {
202 for (std::size_t j = 0; j < i; ++j) {
203 // We iterate over the lower-triangular part of the matrix in row-major order,
204 // but we use the upper-triangular names (i.e. we switch the order of i and j, below).
205 // That puts the elements in the order expected by the constructor we call below,
206 // while creating the field names users would expect from the ordering of their name
207 // vector (i.e. _a_b_Cov instead of _b_a_Cov if names=[a, b]).
208 cov.push_back(schema.addField<T>(
209 schema.join(prefix, names[j], names[i], "Cov"),
210 "uncertainty covariance between " + names[j] + " and " + names[i],
211 units[j] + (units[j].empty() || units[i].empty() ? "" : " ") + units[i]));
212 }
213 }
214 }
215 return CovarianceMatrixKey<T, N>(err, cov);
216}
std::string prefix
std::vector< Key< T > > CovarianceKeyArray
Definition aggregates.h:522
std::vector< Key< T > > ErrKeyArray
Definition aggregates.h:521
T reserve(T... args)

◆ addFields() [2/2]

template<typename T , int N>
CovarianceMatrixKey< T, N > lsst::afw::table::CovarianceMatrixKey< T, N >::addFields ( Schema & schema,
std::string const & prefix,
NameArray const & names,
std::string const & unit,
bool diagonalOnly = false )
static

Add covariance matrix fields to a Schema, and return a CovarianceMatrixKey to manage them.

Parameters
[out]schemaSchema to add fields to.
[in]prefixString used to form the first part of all field names. Suffixes of the form '_xErr' and '_x_y_Cov' will be added to form the full field names (using names={'x', 'y'} as an example).
[in]unitUnit for for error (standard deviation) values; covariance matrix elements will be unit^2.
[in]namesVector of strings containing the names of the quantities the covariance matrix represents the uncertainty of.
[in]diagonalOnlyIf true, only create fields for the error values.

Definition at line 175 of file aggregates.cc.

177 {
178 NameArray units(names.size(), unit);
179 return addFields(schema, prefix, names, units, diagonalOnly);
180}
std::vector< std::string > NameArray
Definition aggregates.h:523
static CovarianceMatrixKey addFields(Schema &schema, std::string const &prefix, NameArray const &names, std::string const &unit, bool diagonalOnly=false)
Add covariance matrix fields to a Schema, and return a CovarianceMatrixKey to manage them.

◆ get()

template<typename T , int N>
Eigen::Matrix< T, N, N > lsst::afw::table::CovarianceMatrixKey< T, N >::get ( BaseRecord const & record) const
overridevirtual

Get a covariance matrix from the given record.

Implements lsst::afw::table::OutputFunctorKey< T >.

Definition at line 293 of file aggregates.cc.

293 {
294 Eigen::Matrix<T, N, N> value = makeZeroMatrix(_err.size(), this);
295 int const n = _err.size();
296 int k = 0;
297 for (int i = 0; i < n; ++i) {
298 T err = record.get(_err[i]);
299 value(i, i) = err * err;
300 if (!_cov.empty()) {
301 for (int j = 0; j < i; ++j, ++k) {
302 if (_cov[k].isValid()) {
303 value(i, j) = value(j, i) = record.get(_cov[k]);
304 }
305 }
306 }
307 }
308 return value;
309}
bool isValid() const noexcept
Return True if all the constituent error Keys are valid.
T empty(T... args)

◆ getElement()

template<typename T , int N>
T lsst::afw::table::CovarianceMatrixKey< T, N >::getElement ( BaseRecord const & record,
int i,
int j ) const

Return the element in row i and column j.

Definition at line 370 of file aggregates.cc.

370 {
371 if (i == j) {
372 T err = record.get(_err[i]);
373 return err * err;
374 }
375 if (_cov.empty()) {
376 return 0.0;
377 }
378 Key<T> key = (i < j) ? _cov[j * (j - 1) / 2 + i] : _cov[i * (i - 1) / 2 + j];
379 return key.isValid() ? record.get(key) : 0.0;
380}

◆ hash_value()

template<typename T , int N>
std::size_t lsst::afw::table::CovarianceMatrixKey< T, N >::hash_value ( ) const
noexcept

Return a hash of this object.

Definition at line 363 of file aggregates.cc.

363 {
364 // Completely arbitrary seeds, different to avoid any weird degeneracies/interactions
365 return cpputils::hashCombine(17, cpputils::hashIterable(19, _err.begin(), _err.end()),
366 cpputils::hashIterable(23, _cov.begin(), _cov.end()));
367}
std::size_t hashCombine(std::size_t seed) noexcept
Combine hashes.
Definition hashCombine.h:35
std::size_t hashIterable(std::size_t seed, InputIterator begin, InputIterator end) noexcept
Combine hashes in an iterable.
Definition hashCombine.h:93

◆ isValid()

template<typename T , int N>
bool lsst::afw::table::CovarianceMatrixKey< T, N >::isValid ( ) const
noexcept

Return True if all the constituent error Keys are valid.

Note that if the only one or more off-diagonal keys are invalid, we assume that means those terms are zero, not that the whole FunctorKey is invalid.

Definition at line 328 of file aggregates.cc.

328 {
329 int const n = _err.size();
330 if (n < 1) return false;
331 for (int i = 0; i < n; ++i) {
332 if (!_err[i].isValid()) return false;
333 }
334 return true;
335}

◆ operator!=()

template<typename T , int N>
bool lsst::afw::table::CovarianceMatrixKey< T, N >::operator!= ( CovarianceMatrixKey< T, N > const & other) const
inlinenoexcept

Definition at line 624 of file aggregates.h.

624{ return !(*this == other); }

◆ operator=() [1/2]

template<typename T , int N>
CovarianceMatrixKey< T, N > & lsst::afw::table::CovarianceMatrixKey< T, N >::operator= ( CovarianceMatrixKey< T, N > && )
default

◆ operator=() [2/2]

template<typename T , int N>
CovarianceMatrixKey< T, N > & lsst::afw::table::CovarianceMatrixKey< T, N >::operator= ( CovarianceMatrixKey< T, N > const & )
default

◆ operator==()

template<typename T , int N>
bool lsst::afw::table::CovarianceMatrixKey< T, N >::operator== ( CovarianceMatrixKey< T, N > const & other) const
noexcept

Compare the FunctorKey for equality with another, using its constituent Keys.

Definition at line 338 of file aggregates.cc.

338 {
339 if (_err.size() != other._err.size()) {
340 return false;
341 }
342 if (_cov.size() != other._cov.size()) {
343 return false;
344 }
345 int const n = _err.size();
346 int k = 0;
347 for (int i = 0; i < n; ++i) {
348 if (_err[i] != other._err[i]) {
349 return false;
350 }
351 if (!_cov.empty()) {
352 for (int j = 0; j < i; ++j, ++k) {
353 if (_cov[k] != other._cov[k]) {
354 return false;
355 }
356 }
357 }
358 }
359 return true;
360}

◆ set() [1/2]

template<typename T , int N>
void lsst::afw::table::CovarianceMatrixKey< T, N >::set ( BaseRecord & record,
Eigen::Matrix< T, N, N > const & value ) const
override

Set a covariance matrix in the given record (uses only the lower triangle of the given matrix)

Definition at line 312 of file aggregates.cc.

312 {
313 int const n = _err.size();
314 int k = 0;
315 for (int i = 0; i < n; ++i) {
316 record.set(_err[i], std::sqrt(value(i, i)));
317 if (!_cov.empty()) {
318 for (int j = 0; j < i; ++j, ++k) {
319 if (_cov[k].isValid()) {
320 record.set(_cov[k], value(i, j));
321 }
322 }
323 }
324 }
325}
T sqrt(T... args)

◆ set() [2/2]

template<typename T >
virtual void lsst::afw::table::InputFunctorKey< T >::set ( BaseRecord & record,
T const & value ) const
pure virtualinherited

◆ setElement()

template<typename T , int N>
void lsst::afw::table::CovarianceMatrixKey< T, N >::setElement ( BaseRecord & record,
int i,
int j,
T value ) const

Set the element in row i and column j.

Definition at line 383 of file aggregates.cc.

383 {
384 if (i == j) {
385 record.set(_err[i], std::sqrt(value));
386 } else {
387 if (_cov.empty()) {
388 throw LSST_EXCEPT(
389 pex::exceptions::LogicError,
390 (boost::format("Cannot set covariance element %d,%d; no fields for covariance") % i % j)
391 .str());
392 }
393 Key<T> key = (i < j) ? _cov[j * (j - 1) / 2 + i] : _cov[i * (i - 1) / 2 + j];
394 if (!key.isValid()) {
395 throw LSST_EXCEPT(
396 pex::exceptions::LogicError,
397 (boost::format("Cannot set covariance element %d,%d; no field for this element") % i % j)
398 .str());
399 }
400 record.set(key, value);
401 }
402}
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition Exception.h:48

The documentation for this class was generated from the following files: