LSSTApplications  17.0+11,17.0+34,17.0+56,17.0+57,17.0+59,17.0+7,17.0-1-g377950a+33,17.0.1-1-g114240f+2,17.0.1-1-g4d4fbc4+28,17.0.1-1-g55520dc+49,17.0.1-1-g5f4ed7e+52,17.0.1-1-g6dd7d69+17,17.0.1-1-g8de6c91+11,17.0.1-1-gb9095d2+7,17.0.1-1-ge9fec5e+5,17.0.1-1-gf4e0155+55,17.0.1-1-gfc65f5f+50,17.0.1-1-gfc6fb1f+20,17.0.1-10-g87f9f3f+1,17.0.1-11-ge9de802+16,17.0.1-16-ga14f7d5c+4,17.0.1-17-gc79d625+1,17.0.1-17-gdae4c4a+8,17.0.1-2-g26618f5+29,17.0.1-2-g54f2ebc+9,17.0.1-2-gf403422+1,17.0.1-20-g2ca2f74+6,17.0.1-23-gf3eadeb7+1,17.0.1-3-g7e86b59+39,17.0.1-3-gb5ca14a,17.0.1-3-gd08d533+40,17.0.1-30-g596af8797,17.0.1-4-g59d126d+4,17.0.1-4-gc69c472+5,17.0.1-6-g5afd9b9+4,17.0.1-7-g35889ee+1,17.0.1-7-gc7c8782+18,17.0.1-9-gc4bbfb2+3,w.2019.22
LSSTDataManagementBasePackage
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/Citizen.h"
35 #include "lsst/daf/base/DateTime.h"
36 #include "lsst/pex/exceptions.h"
37 
38 namespace lsst {
39 namespace afw {
40 namespace math {
41 
62 public:
64 
65  GaussianProcessTimer(GaussianProcessTimer const &) = default;
69  ~GaussianProcessTimer() = default;
70 
75  void reset();
76 
80  void start();
81 
85  void addToEigen();
86 
90  void addToVariance();
91 
95  void addToSearch();
96 
100  void addToIteration();
101 
107  void addToTotal(int i);
108 
112  void display();
113 
114 private:
115  double _before, _beginning;
116  double _eigenTime, _iterationTime, _searchTime, _varianceTime, _totalTime;
117  int _interpolationCount;
118 };
119 
126 template <typename T>
128 public:
129  virtual ~Covariogram();
130 
131  // No copying
132  Covariogram(const Covariogram &) = delete;
133  Covariogram &operator=(const Covariogram &) = delete;
134 
135  // No moving
136  Covariogram(Covariogram &&) = delete;
137  Covariogram &operator=(Covariogram &&) = delete;
138 
142  explicit Covariogram() : lsst::daf::base::Citizen(typeid(this)){};
143 
151  virtual T operator()(ndarray::Array<const T, 1, 1> const &p1,
152  ndarray::Array<const T, 1, 1> const &p2) const;
153 };
154 
164 template <typename T>
166 public:
167  ~SquaredExpCovariogram() override;
168 
169  explicit SquaredExpCovariogram();
170 
175  void setEllSquared(double ellSquared);
176 
177  T operator()(ndarray::Array<const T, 1, 1> const &, ndarray::Array<const T, 1, 1> const &) const override;
178 
179 private:
180  double _ellSquared;
181 };
182 
193 template <typename T>
195 public:
196  ~NeuralNetCovariogram() override;
197 
198  explicit NeuralNetCovariogram();
199 
203  void setSigma0(double sigma0);
204 
208  void setSigma1(double sigma1);
209 
210  T operator()(ndarray::Array<const T, 1, 1> const &, ndarray::Array<const T, 1, 1> const &) const override;
211 
212 private:
213  double _sigma0, _sigma1;
214 };
215 
224 template <typename T>
225 class KdTree {
226 public:
227  // No copying
228  KdTree(const KdTree &) = delete;
229  KdTree &operator=(const KdTree &) = delete;
230 
231  // No moving
232  KdTree(KdTree &&) = delete;
233  KdTree &operator=(KdTree &&) = delete;
234 
235  // Add default constructor (which is no longer generated)
236  KdTree() = default;
237 
246  void Initialize(ndarray::Array<T, 2, 2> const &dt);
247 
263  void findNeighbors(ndarray::Array<int, 1, 1> neighdex, ndarray::Array<double, 1, 1> dd,
264  ndarray::Array<const T, 1, 1> const &v, int n_nn) const;
265 
273  T getData(int ipt, int idim) const;
274 
296  ndarray::Array<T, 1, 1> getData(int ipt) const;
297 
305  void addPoint(ndarray::Array<const T, 1, 1> const &v);
306 
315  void removePoint(int dex);
316 
320  int getNPoints() const;
321 
329  void getTreeNode(ndarray::Array<int, 1, 1> const &v, int dex) const;
330 
331 private:
332  ndarray::Array<int, 2, 2> _tree;
333  ndarray::Array<int, 1, 1> _inn;
334  ndarray::Array<T, 2, 2> _data;
335 
336  enum { DIMENSION, LT, GEQ, PARENT };
337 
338  //_tree stores the relationships between data points
339  //_tree[i][DIMENSION] is the dimension on which the ith node segregates its daughters
340  //
341  //_tree[i][LT] is the branch of the tree down which the daughters' DIMENSIONth component
342  // is less than the parent's
343  //
344  //_tree[i][GEQ] is the branch of the tree down which the daughters' DIMENSIONth component is
345  // greather than or equal to the parent's
346  //
347  //_tree[i][PARENT] is the parent node of the ith node
348 
349  //_data actually stores the data points
350 
351  int _npts, _dimensions, _room, _roomStep, _masterParent;
352  mutable int _neighborsFound, _neighborsWanted;
353 
354  //_room denotes the capacity of _data and _tree. It will usually be larger
355  // than _npts so that we do not have to reallocate
356  //_tree and _data every time we add a new point to the tree
357 
358  mutable ndarray::Array<double, 1, 1> _neighborDistances;
359  mutable ndarray::Array<int, 1, 1> _neighborCandidates;
360 
373  void _organize(ndarray::Array<int, 1, 1> const &use, int ct, int parent, int dir);
374 
380  int _findNode(ndarray::Array<const T, 1, 1> const &v) const;
381 
398  void _lookForNeighbors(ndarray::Array<const T, 1, 1> const &v, int consider, int from) const;
399 
403  int _testTree() const;
404 
418  int _walkUpTree(int target, int dir, int root) const;
419 
428  void _count(int where, int *ct) const;
429 
435  void _descend(int root);
436 
442  void _reassign(int target);
443 
447  double _distance(ndarray::Array<const T, 1, 1> const &p1, ndarray::Array<const T, 1, 1> const &p2) const;
448 };
449 
471 template <typename T>
473 public:
474  // No copying
475  GaussianProcess(const GaussianProcess &) = delete;
476  GaussianProcess &operator=(const GaussianProcess &) = delete;
477 
478  // No moving
479  GaussianProcess(GaussianProcess &&) = delete;
481 
494  GaussianProcess(ndarray::Array<T, 2, 2> const &dataIn, ndarray::Array<T, 1, 1> const &ff,
495  std::shared_ptr<Covariogram<T> > const &covarIn);
496 
516  GaussianProcess(ndarray::Array<T, 2, 2> const &dataIn, ndarray::Array<T, 1, 1> const &mn,
517  ndarray::Array<T, 1, 1> const &mx, ndarray::Array<T, 1, 1> const &ff,
518  std::shared_ptr<Covariogram<T> > const &covarIn);
531  GaussianProcess(ndarray::Array<T, 2, 2> const &dataIn, ndarray::Array<T, 2, 2> const &ff,
532  std::shared_ptr<Covariogram<T> > const &covarIn);
549  GaussianProcess(ndarray::Array<T, 2, 2> const &dataIn, ndarray::Array<T, 1, 1> const &mn,
550  ndarray::Array<T, 1, 1> const &mx, ndarray::Array<T, 2, 2> const &ff,
551  std::shared_ptr<Covariogram<T> > const &covarIn);
552 
556  int getNPoints() const;
557 
562  int getDim() const;
563 
573  void getData(ndarray::Array<T, 2, 2> pts, ndarray::Array<T, 1, 1> fn,
574  ndarray::Array<int, 1, 1> indices) const;
575 
585  void getData(ndarray::Array<T, 2, 2> pts, ndarray::Array<T, 2, 2> fn,
586  ndarray::Array<int, 1, 1> indices) const;
587 
605  T interpolate(ndarray::Array<T, 1, 1> variance, ndarray::Array<T, 1, 1> const &vin,
606  int numberOfNeighbors) const;
622  void interpolate(ndarray::Array<T, 1, 1> mu, ndarray::Array<T, 1, 1> variance,
623  ndarray::Array<T, 1, 1> const &vin, int numberOfNeighbors) const;
624 
644  T selfInterpolate(ndarray::Array<T, 1, 1> variance, int dex, int numberOfNeighbors) const;
645 
660  void selfInterpolate(ndarray::Array<T, 1, 1> mu, ndarray::Array<T, 1, 1> variance, int dex,
661  int numberOfNeighbors) const;
662 
684  void batchInterpolate(ndarray::Array<T, 1, 1> mu, ndarray::Array<T, 1, 1> variance,
685  ndarray::Array<T, 2, 2> const &queries) const;
686 
704  void batchInterpolate(ndarray::Array<T, 1, 1> mu, ndarray::Array<T, 2, 2> const &queries) const;
705 
710  void batchInterpolate(ndarray::Array<T, 2, 2> mu, ndarray::Array<T, 2, 2> variance,
711  ndarray::Array<T, 2, 2> const &queries) const;
712 
717  void batchInterpolate(ndarray::Array<T, 2, 2> mu, ndarray::Array<T, 2, 2> const &queries) const;
718 
735  void addPoint(ndarray::Array<T, 1, 1> const &vin, T f);
736 
746  void addPoint(ndarray::Array<T, 1, 1> const &vin, ndarray::Array<T, 1, 1> const &f);
747 
759  void removePoint(int dex);
760 
767  void setKrigingParameter(T kk);
768 
775  void setCovariogram(std::shared_ptr<Covariogram<T> > const &covar);
776 
788  void setLambda(T lambda);
789 
804  GaussianProcessTimer &getTimes() const;
805 
806 private:
807  int _npts, _useMaxMin, _dimensions, _room, _roomStep, _nFunctions;
808 
809  T _krigingParameter, _lambda;
810 
811  ndarray::Array<T, 1, 1> _max, _min;
812  ndarray::Array<T, 2, 2> _function;
813 
814  KdTree<T> _kdTree;
815 
816  std::shared_ptr<Covariogram<T> > _covariogram;
817  mutable GaussianProcessTimer _timer;
818 };
819 } // namespace math
820 } // namespace afw
821 } // namespace lsst
822 
823 #endif //#ifndef LSST_AFW_MATH_GAUSSIAN_PROCESS_H
GaussianProcessTimer & operator=(GaussianProcessTimer const &)=default
table::Key< int > from
Stores values of a function sampled on an image and allows you to interpolate the function to unsampl...
void addToTotal(int i)
Adds time to _totalTime and adds counts to _interpolationCount.
afw::table::Key< afw::table::Array< VariancePixelT > > variance
This is a structure for keeping track of how long the interpolation methods spend on different parts ...
The parent class of covariogram functions for use in Gaussian Process interpolation.
void start()
Starts the timer for an individual call to an interpolation routine.
void addToVariance()
Adds time to _varianceTime.
A base class for image defects.
void addToIteration()
Adds time to _iterationTime.
The data for GaussianProcess is stored in a KD tree to facilitate nearest-neighbor searches...
Key< Flag > const & target
a Covariogram that recreates a neural network with one hidden layer and infinite units in that layer ...
Interface for DateTime class.
Covariogram()
construct a Covariogram assigning default values to the hyper parameters
void addToSearch()
Adds time to _searchTime.
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:55
void display()
Displays the current values of all times and _interpolationCount.
void reset()
Resets all of the data members of GaussianProcessTimer to zero.
table::Key< double > sigma1
void addToEigen()
Adds time to _eigenTime.