LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
GaussianProcess.h
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 #ifndef LSST_AFW_MATH_GAUSSIAN_PROCESS_H
26 #define LSST_AFW_MATH_GAUSSIAN_PROCESS_H
27 
28 #include <Eigen/Dense>
29 #include <stdexcept>
30 
31 #include "ndarray/eigen.h"
32 #include <memory>
33 
34 #include "lsst/daf/base/DateTime.h"
35 #include "lsst/pex/exceptions.h"
36 
37 namespace lsst {
38 namespace afw {
39 namespace math {
40 
61 public:
63 
68  ~GaussianProcessTimer() = default;
69 
74  void reset();
75 
79  void start();
80 
84  void addToEigen();
85 
89  void addToVariance();
90 
94  void addToSearch();
95 
99  void addToIteration();
100 
106  void addToTotal(int i);
107 
111  void display();
112 
113 private:
114  double _before, _beginning;
115  double _eigenTime, _iterationTime, _searchTime, _varianceTime, _totalTime;
116  int _interpolationCount;
117 };
118 
125 template <typename T>
126 class Covariogram {
127 public:
128  virtual ~Covariogram();
129 
130  // No copying
131  Covariogram(const Covariogram &) = delete;
132  Covariogram &operator=(const Covariogram &) = delete;
133 
134  // No moving
135  Covariogram(Covariogram &&) = delete;
137 
141  explicit Covariogram(){};
142 
150  virtual T operator()(ndarray::Array<const T, 1, 1> const &p1,
151  ndarray::Array<const T, 1, 1> const &p2) const;
152 };
153 
163 template <typename T>
165 public:
167 
168  explicit SquaredExpCovariogram();
169 
174  void setEllSquared(double ellSquared);
175 
176  T operator()(ndarray::Array<const T, 1, 1> const &, ndarray::Array<const T, 1, 1> const &) const override;
177 
178 private:
179  double _ellSquared;
180 };
181 
192 template <typename T>
194 public:
196 
197  explicit NeuralNetCovariogram();
198 
202  void setSigma0(double sigma0);
203 
207  void setSigma1(double sigma1);
208 
209  T operator()(ndarray::Array<const T, 1, 1> const &, ndarray::Array<const T, 1, 1> const &) const override;
210 
211 private:
212  double _sigma0, _sigma1;
213 };
214 
223 template <typename T>
224 class KdTree {
225 public:
226  // No copying
227  KdTree(const KdTree &) = delete;
228  KdTree &operator=(const KdTree &) = delete;
229 
230  // No moving
231  KdTree(KdTree &&) = delete;
232  KdTree &operator=(KdTree &&) = delete;
233 
234  // Add default constructor (which is no longer generated)
235  KdTree() = default;
236 
245  void Initialize(ndarray::Array<T, 2, 2> const &dt);
246 
262  void findNeighbors(ndarray::Array<int, 1, 1> neighdex, ndarray::Array<double, 1, 1> dd,
263  ndarray::Array<const T, 1, 1> const &v, int n_nn) const;
264 
272  T getData(int ipt, int idim) const;
273 
295  ndarray::Array<T, 1, 1> getData(int ipt) const;
296 
304  void addPoint(ndarray::Array<const T, 1, 1> const &v);
305 
314  void removePoint(int dex);
315 
319  int getNPoints() const;
320 
328  void getTreeNode(ndarray::Array<int, 1, 1> const &v, int dex) const;
329 
330 private:
331  ndarray::Array<int, 2, 2> _tree;
332  ndarray::Array<int, 1, 1> _inn;
333  ndarray::Array<T, 2, 2> _data;
334 
335  enum { DIMENSION, LT, GEQ, PARENT };
336 
337  //_tree stores the relationships between data points
338  //_tree[i][DIMENSION] is the dimension on which the ith node segregates its daughters
339  //
340  //_tree[i][LT] is the branch of the tree down which the daughters' DIMENSIONth component
341  // is less than the parent's
342  //
343  //_tree[i][GEQ] is the branch of the tree down which the daughters' DIMENSIONth component is
344  // greather than or equal to the parent's
345  //
346  //_tree[i][PARENT] is the parent node of the ith node
347 
348  //_data actually stores the data points
349 
350  int _npts, _dimensions, _room, _roomStep, _masterParent;
351  mutable int _neighborsFound, _neighborsWanted;
352 
353  //_room denotes the capacity of _data and _tree. It will usually be larger
354  // than _npts so that we do not have to reallocate
355  //_tree and _data every time we add a new point to the tree
356 
357  mutable ndarray::Array<double, 1, 1> _neighborDistances;
358  mutable ndarray::Array<int, 1, 1> _neighborCandidates;
359 
372  void _organize(ndarray::Array<int, 1, 1> const &use, int ct, int parent, int dir);
373 
379  int _findNode(ndarray::Array<const T, 1, 1> const &v) const;
380 
397  void _lookForNeighbors(ndarray::Array<const T, 1, 1> const &v, int consider, int from) const;
398 
402  int _testTree() const;
403 
417  int _walkUpTree(int target, int dir, int root) const;
418 
427  void _count(int where, int *ct) const;
428 
434  void _descend(int root);
435 
441  void _reassign(int target);
442 
446  double _distance(ndarray::Array<const T, 1, 1> const &p1, ndarray::Array<const T, 1, 1> const &p2) const;
447 };
448 
470 template <typename T>
472 public:
473  // No copying
474  GaussianProcess(const GaussianProcess &) = delete;
476 
477  // No moving
480 
493  GaussianProcess(ndarray::Array<T, 2, 2> const &dataIn, ndarray::Array<T, 1, 1> const &ff,
494  std::shared_ptr<Covariogram<T> > const &covarIn);
495 
515  GaussianProcess(ndarray::Array<T, 2, 2> const &dataIn, ndarray::Array<T, 1, 1> const &mn,
516  ndarray::Array<T, 1, 1> const &mx, ndarray::Array<T, 1, 1> const &ff,
517  std::shared_ptr<Covariogram<T> > const &covarIn);
530  GaussianProcess(ndarray::Array<T, 2, 2> const &dataIn, ndarray::Array<T, 2, 2> const &ff,
531  std::shared_ptr<Covariogram<T> > const &covarIn);
548  GaussianProcess(ndarray::Array<T, 2, 2> const &dataIn, ndarray::Array<T, 1, 1> const &mn,
549  ndarray::Array<T, 1, 1> const &mx, ndarray::Array<T, 2, 2> const &ff,
550  std::shared_ptr<Covariogram<T> > const &covarIn);
551 
555  int getNPoints() const;
556 
561  int getDim() const;
562 
572  void getData(ndarray::Array<T, 2, 2> pts, ndarray::Array<T, 1, 1> fn,
573  ndarray::Array<int, 1, 1> indices) const;
574 
584  void getData(ndarray::Array<T, 2, 2> pts, ndarray::Array<T, 2, 2> fn,
585  ndarray::Array<int, 1, 1> indices) const;
586 
604  T interpolate(ndarray::Array<T, 1, 1> variance, ndarray::Array<T, 1, 1> const &vin,
605  int numberOfNeighbors) const;
621  void interpolate(ndarray::Array<T, 1, 1> mu, ndarray::Array<T, 1, 1> variance,
622  ndarray::Array<T, 1, 1> const &vin, int numberOfNeighbors) const;
623 
643  T selfInterpolate(ndarray::Array<T, 1, 1> variance, int dex, int numberOfNeighbors) const;
644 
659  void selfInterpolate(ndarray::Array<T, 1, 1> mu, ndarray::Array<T, 1, 1> variance, int dex,
660  int numberOfNeighbors) const;
661 
683  void batchInterpolate(ndarray::Array<T, 1, 1> mu, ndarray::Array<T, 1, 1> variance,
684  ndarray::Array<T, 2, 2> const &queries) const;
685 
703  void batchInterpolate(ndarray::Array<T, 1, 1> mu, ndarray::Array<T, 2, 2> const &queries) const;
704 
709  void batchInterpolate(ndarray::Array<T, 2, 2> mu, ndarray::Array<T, 2, 2> variance,
710  ndarray::Array<T, 2, 2> const &queries) const;
711 
716  void batchInterpolate(ndarray::Array<T, 2, 2> mu, ndarray::Array<T, 2, 2> const &queries) const;
717 
734  void addPoint(ndarray::Array<T, 1, 1> const &vin, T f);
735 
745  void addPoint(ndarray::Array<T, 1, 1> const &vin, ndarray::Array<T, 1, 1> const &f);
746 
758  void removePoint(int dex);
759 
766  void setKrigingParameter(T kk);
767 
774  void setCovariogram(std::shared_ptr<Covariogram<T> > const &covar);
775 
787  void setLambda(T lambda);
788 
804 
805 private:
806  int _npts, _useMaxMin, _dimensions, _room, _roomStep, _nFunctions;
807 
808  T _krigingParameter, _lambda;
809 
810  ndarray::Array<T, 1, 1> _max, _min;
811  ndarray::Array<T, 2, 2> _function;
812 
813  KdTree<T> _kdTree;
814 
815  std::shared_ptr<Covariogram<T> > _covariogram;
816  mutable GaussianProcessTimer _timer;
817 };
818 } // namespace math
819 } // namespace afw
820 } // namespace lsst
821 
822 #endif //#ifndef LSST_AFW_MATH_GAUSSIAN_PROCESS_H
Key< Flag > const & target
Interface for DateTime class.
table::Key< double > sigma1
afw::table::Key< afw::table::Array< VariancePixelT > > variance
table::Key< int > from
The parent class of covariogram functions for use in Gaussian Process interpolation.
Covariogram()
construct a Covariogram assigning default values to the hyper parameters
Covariogram & operator=(Covariogram &&)=delete
Covariogram(const Covariogram &)=delete
virtual T operator()(ndarray::Array< const T, 1, 1 > const &p1, ndarray::Array< const T, 1, 1 > const &p2) const
Actually evaluate the covariogram function relating two points you want to interpolate from.
Covariogram(Covariogram &&)=delete
Covariogram & operator=(const Covariogram &)=delete
Stores values of a function sampled on an image and allows you to interpolate the function to unsampl...
GaussianProcessTimer & getTimes() const
Give the user acces to _timer, an object keeping track of the time spent on various processes within ...
void setLambda(T lambda)
set the value of the hyperparameter _lambda
int getDim() const
return the dimensionality of data points stored in the GaussianProcess
GaussianProcess & operator=(GaussianProcess &&)=delete
void setKrigingParameter(T kk)
Assign a value to the Kriging paramter.
void addPoint(ndarray::Array< T, 1, 1 > const &vin, T f)
Add a point to the pool of data used by GaussianProcess for interpolation.
void batchInterpolate(ndarray::Array< T, 1, 1 > mu, ndarray::Array< T, 1, 1 > variance, ndarray::Array< T, 2, 2 > const &queries) const
Interpolate a list of query points using all of the input data (rather than nearest neighbors)
GaussianProcess(const GaussianProcess &)=delete
T interpolate(ndarray::Array< T, 1, 1 > variance, ndarray::Array< T, 1, 1 > const &vin, int numberOfNeighbors) const
Interpolate the function value at one point using a specified number of nearest neighbors.
T selfInterpolate(ndarray::Array< T, 1, 1 > variance, int dex, int numberOfNeighbors) const
This method will interpolate the function on a data point for purposes of optimizing hyper parameters...
void removePoint(int dex)
This will remove a point from the data set.
void setCovariogram(std::shared_ptr< Covariogram< T > > const &covar)
Assign a different covariogram to this GaussianProcess.
void getData(ndarray::Array< T, 2, 2 > pts, ndarray::Array< T, 1, 1 > fn, ndarray::Array< int, 1, 1 > indices) const
Return a sub-sample the data underlying the Gaussian Process.
GaussianProcess & operator=(const GaussianProcess &)=delete
GaussianProcess(GaussianProcess &&)=delete
int getNPoints() const
return the number of data points stored in the GaussianProcess
This is a structure for keeping track of how long the interpolation methods spend on different parts ...
void addToSearch()
Adds time to _searchTime.
void addToVariance()
Adds time to _varianceTime.
void addToIteration()
Adds time to _iterationTime.
void start()
Starts the timer for an individual call to an interpolation routine.
void display()
Displays the current values of all times and _interpolationCount.
GaussianProcessTimer(GaussianProcessTimer const &)=default
GaussianProcessTimer & operator=(GaussianProcessTimer &&)=default
void reset()
Resets all of the data members of GaussianProcessTimer to zero.
void addToEigen()
Adds time to _eigenTime.
GaussianProcessTimer & operator=(GaussianProcessTimer const &)=default
void addToTotal(int i)
Adds time to _totalTime and adds counts to _interpolationCount.
GaussianProcessTimer(GaussianProcessTimer &&)=default
The data for GaussianProcess is stored in a KD tree to facilitate nearest-neighbor searches.
KdTree(const KdTree &)=delete
KdTree & operator=(const KdTree &)=delete
void removePoint(int dex)
Remove a point from the tree.
void findNeighbors(ndarray::Array< int, 1, 1 > neighdex, ndarray::Array< double, 1, 1 > dd, ndarray::Array< const T, 1, 1 > const &v, int n_nn) const
Find the nearest neighbors of a point.
void addPoint(ndarray::Array< const T, 1, 1 > const &v)
Add a point to the tree.
void Initialize(ndarray::Array< T, 2, 2 > const &dt)
Build a KD Tree to store the data for GaussianProcess.
void getTreeNode(ndarray::Array< int, 1, 1 > const &v, int dex) const
Return the _tree information for a given data point.
int getNPoints() const
return the number of data points stored in the tree
KdTree & operator=(KdTree &&)=delete
T getData(int ipt, int idim) const
Return one element of one node on the tree.
KdTree(KdTree &&)=delete
a Covariogram that recreates a neural network with one hidden layer and infinite units in that layer
void setSigma0(double sigma0)
set the _sigma0 hyper parameter
T operator()(ndarray::Array< const T, 1, 1 > const &, ndarray::Array< const T, 1, 1 > const &) const override
Actually evaluate the covariogram function relating two points you want to interpolate from.
void setSigma1(double sigma1)
set the _sigma1 hyper parameter
void setEllSquared(double ellSquared)
set the _ellSquared hyper parameter (the square of the characteristic length scale of the covariogram...
T operator()(ndarray::Array< const T, 1, 1 > const &, ndarray::Array< const T, 1, 1 > const &) const override
Actually evaluate the covariogram function relating two points you want to interpolate from.
A base class for image defects.