lsst.afw  15.0-29-ga12a2b06e
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 #include "lsst/pex/policy.h"
38 #include "lsst/pex/policy/Policy.h"
39 
40 namespace lsst {
41 namespace afw {
42 namespace math {
43 
64 public:
66 
67  GaussianProcessTimer(GaussianProcessTimer const &) = default;
71  ~GaussianProcessTimer() = default;
72 
77  void reset();
78 
82  void start();
83 
87  void addToEigen();
88 
92  void addToVariance();
93 
97  void addToSearch();
98 
102  void addToIteration();
103 
109  void addToTotal(int i);
110 
114  void display();
115 
116 private:
117  double _before, _beginning;
118  double _eigenTime, _iterationTime, _searchTime, _varianceTime, _totalTime;
119  int _interpolationCount;
120 };
121 
128 template <typename T>
130 public:
131  virtual ~Covariogram();
132 
133  // No copying
134  Covariogram(const Covariogram &) = delete;
135  Covariogram &operator=(const Covariogram &) = delete;
136 
137  // No moving
138  Covariogram(Covariogram &&) = delete;
139  Covariogram &operator=(Covariogram &&) = delete;
140 
144  explicit Covariogram() : lsst::daf::base::Citizen(typeid(this)){};
145 
153  virtual T operator()(ndarray::Array<const T, 1, 1> const &p1,
154  ndarray::Array<const T, 1, 1> const &p2) const;
155 };
156 
166 template <typename T>
168 public:
169  virtual ~SquaredExpCovariogram();
170 
171  explicit SquaredExpCovariogram();
172 
177  void setEllSquared(double ellSquared);
178 
179  virtual T operator()(ndarray::Array<const T, 1, 1> const &, ndarray::Array<const T, 1, 1> const &) const;
180 
181 private:
182  double _ellSquared;
183 };
184 
195 template <typename T>
197 public:
198  virtual ~NeuralNetCovariogram();
199 
200  explicit NeuralNetCovariogram();
201 
205  void setSigma0(double sigma0);
206 
210  void setSigma1(double sigma1);
211 
212  virtual T operator()(ndarray::Array<const T, 1, 1> const &, ndarray::Array<const T, 1, 1> const &) const;
213 
214 private:
215  double _sigma0, _sigma1;
216 };
217 
226 template <typename T>
227 class KdTree {
228 public:
229  // No copying
230  KdTree(const KdTree &) = delete;
231  KdTree &operator=(const KdTree &) = delete;
232 
233  // No moving
234  KdTree(KdTree &&) = delete;
235  KdTree &operator=(KdTree &&) = delete;
236 
237  // Add default constructor (which is no longer generated)
238  KdTree() = default;
239 
248  void Initialize(ndarray::Array<T, 2, 2> const &dt);
249 
265  void findNeighbors(ndarray::Array<int, 1, 1> neighdex, ndarray::Array<double, 1, 1> dd,
266  ndarray::Array<const T, 1, 1> const &v, int n_nn) const;
267 
275  T getData(int ipt, int idim) const;
276 
298  ndarray::Array<T, 1, 1> getData(int ipt) const;
299 
307  void addPoint(ndarray::Array<const T, 1, 1> const &v);
308 
317  void removePoint(int dex);
318 
322  int getNPoints() const;
323 
331  void getTreeNode(ndarray::Array<int, 1, 1> const &v, int dex) const;
332 
333 private:
334  ndarray::Array<int, 2, 2> _tree;
335  ndarray::Array<int, 1, 1> _inn;
336  ndarray::Array<T, 2, 2> _data;
337 
338  enum { DIMENSION, LT, GEQ, PARENT };
339 
340  //_tree stores the relationships between data points
341  //_tree[i][DIMENSION] is the dimension on which the ith node segregates its daughters
342  //
343  //_tree[i][LT] is the branch of the tree down which the daughters' DIMENSIONth component
344  // is less than the parent's
345  //
346  //_tree[i][GEQ] is the branch of the tree down which the daughters' DIMENSIONth component is
347  // greather than or equal to the parent's
348  //
349  //_tree[i][PARENT] is the parent node of the ith node
350 
351  //_data actually stores the data points
352 
353  int _npts, _dimensions, _room, _roomStep, _masterParent;
354  mutable int _neighborsFound, _neighborsWanted;
355 
356  //_room denotes the capacity of _data and _tree. It will usually be larger
357  // than _npts so that we do not have to reallocate
358  //_tree and _data every time we add a new point to the tree
359 
360  mutable ndarray::Array<double, 1, 1> _neighborDistances;
361  mutable ndarray::Array<int, 1, 1> _neighborCandidates;
362 
375  void _organize(ndarray::Array<int, 1, 1> const &use, int ct, int parent, int dir);
376 
382  int _findNode(ndarray::Array<const T, 1, 1> const &v) const;
383 
400  void _lookForNeighbors(ndarray::Array<const T, 1, 1> const &v, int consider, int from) const;
401 
405  int _testTree() const;
406 
420  int _walkUpTree(int target, int dir, int root) const;
421 
430  void _count(int where, int *ct) const;
431 
437  void _descend(int root);
438 
444  void _reassign(int target);
445 
449  double _distance(ndarray::Array<const T, 1, 1> const &p1, ndarray::Array<const T, 1, 1> const &p2) const;
450 };
451 
473 template <typename T>
475 public:
476  // No copying
477  GaussianProcess(const GaussianProcess &) = delete;
478  GaussianProcess &operator=(const GaussianProcess &) = delete;
479 
480  // No moving
481  GaussianProcess(GaussianProcess &&) = delete;
483 
496  GaussianProcess(ndarray::Array<T, 2, 2> const &dataIn, ndarray::Array<T, 1, 1> const &ff,
497  std::shared_ptr<Covariogram<T> > const &covarIn);
498 
518  GaussianProcess(ndarray::Array<T, 2, 2> const &dataIn, ndarray::Array<T, 1, 1> const &mn,
519  ndarray::Array<T, 1, 1> const &mx, ndarray::Array<T, 1, 1> const &ff,
520  std::shared_ptr<Covariogram<T> > const &covarIn);
533  GaussianProcess(ndarray::Array<T, 2, 2> const &dataIn, ndarray::Array<T, 2, 2> const &ff,
534  std::shared_ptr<Covariogram<T> > const &covarIn);
551  GaussianProcess(ndarray::Array<T, 2, 2> const &dataIn, ndarray::Array<T, 1, 1> const &mn,
552  ndarray::Array<T, 1, 1> const &mx, ndarray::Array<T, 2, 2> const &ff,
553  std::shared_ptr<Covariogram<T> > const &covarIn);
554 
558  int getNPoints() const;
559 
564  int getDim() const;
565 
575  void getData(ndarray::Array<T, 2, 2> pts, ndarray::Array<T, 1, 1> fn,
576  ndarray::Array<int, 1, 1> indices) const;
577 
587  void getData(ndarray::Array<T, 2, 2> pts, ndarray::Array<T, 2, 2> fn,
588  ndarray::Array<int, 1, 1> indices) const;
589 
607  T interpolate(ndarray::Array<T, 1, 1> variance, ndarray::Array<T, 1, 1> const &vin,
608  int numberOfNeighbors) const;
624  void interpolate(ndarray::Array<T, 1, 1> mu, ndarray::Array<T, 1, 1> variance,
625  ndarray::Array<T, 1, 1> const &vin, int numberOfNeighbors) const;
626 
646  T selfInterpolate(ndarray::Array<T, 1, 1> variance, int dex, int numberOfNeighbors) const;
647 
662  void selfInterpolate(ndarray::Array<T, 1, 1> mu, ndarray::Array<T, 1, 1> variance, int dex,
663  int numberOfNeighbors) const;
664 
686  void batchInterpolate(ndarray::Array<T, 1, 1> mu, ndarray::Array<T, 1, 1> variance,
687  ndarray::Array<T, 2, 2> const &queries) const;
688 
706  void batchInterpolate(ndarray::Array<T, 1, 1> mu, ndarray::Array<T, 2, 2> const &queries) const;
707 
712  void batchInterpolate(ndarray::Array<T, 2, 2> mu, ndarray::Array<T, 2, 2> variance,
713  ndarray::Array<T, 2, 2> const &queries) const;
714 
719  void batchInterpolate(ndarray::Array<T, 2, 2> mu, ndarray::Array<T, 2, 2> const &queries) const;
720 
737  void addPoint(ndarray::Array<T, 1, 1> const &vin, T f);
738 
748  void addPoint(ndarray::Array<T, 1, 1> const &vin, ndarray::Array<T, 1, 1> const &f);
749 
761  void removePoint(int dex);
762 
769  void setKrigingParameter(T kk);
770 
777  void setCovariogram(std::shared_ptr<Covariogram<T> > const &covar);
778 
790  void setLambda(T lambda);
791 
806  GaussianProcessTimer &getTimes() const;
807 
808 private:
809  int _npts, _useMaxMin, _dimensions, _room, _roomStep, _nFunctions;
810 
811  T _krigingParameter, _lambda;
812 
813  ndarray::Array<T, 1, 1> _max, _min;
814  ndarray::Array<T, 2, 2> _function;
815 
816  KdTree<T> _kdTree;
817 
818  std::shared_ptr<Covariogram<T> > _covariogram;
819  mutable GaussianProcessTimer _timer;
820 };
821 }
822 }
823 }
824 
825 #endif //#ifndef LSST_AFW_MATH_GAUSSIAN_PROCESS_H
GaussianProcessTimer & operator=(GaussianProcessTimer const &)=default
afw::table::Key< afw::table::Array< VariancePixelT > > variance
Key< Flag > const & target
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.
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...
a Covariogram that recreates a neural network with one hidden layer and infinite units in that layer ...
Covariogram()
construct a Covariogram assigning default values to the hyper parameters
void addToSearch()
Adds time to _searchTime.
void display()
Displays the current values of all times and _interpolationCount.
void reset()
Resets all of the data members of GaussianProcessTimer to zero.
void addToEigen()
Adds time to _eigenTime.
table::Key< double > sigma1