lsst.afw  14.0-38-g278b05818
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 
71  void reset();
72 
76  void start();
77 
81  void addToEigen();
82 
86  void addToVariance();
87 
91  void addToSearch();
92 
96  void addToIteration();
97 
103  void addToTotal(int i);
104 
108  void display();
109 
110 private:
111  double _before, _beginning;
112  double _eigenTime, _iterationTime, _searchTime, _varianceTime, _totalTime;
113  int _interpolationCount;
114 };
115 
122 template <typename T>
124 public:
125  virtual ~Covariogram();
126 
127  // No copying
128  Covariogram(const Covariogram &) = delete;
129  Covariogram &operator=(const Covariogram &) = delete;
130 
131  // No moving
132  Covariogram(Covariogram &&) = delete;
133  Covariogram &operator=(Covariogram &&) = delete;
134 
138  explicit Covariogram() : lsst::daf::base::Citizen(typeid(this)){};
139 
147  virtual T operator()(ndarray::Array<const T, 1, 1> const &p1,
148  ndarray::Array<const T, 1, 1> const &p2) const;
149 };
150 
160 template <typename T>
162 public:
163  virtual ~SquaredExpCovariogram();
164 
165  explicit SquaredExpCovariogram();
166 
171  void setEllSquared(double ellSquared);
172 
173  virtual T operator()(ndarray::Array<const T, 1, 1> const &, ndarray::Array<const T, 1, 1> const &) const;
174 
175 private:
176  double _ellSquared;
177 };
178 
189 template <typename T>
191 public:
192  virtual ~NeuralNetCovariogram();
193 
194  explicit NeuralNetCovariogram();
195 
199  void setSigma0(double sigma0);
200 
204  void setSigma1(double sigma1);
205 
206  virtual T operator()(ndarray::Array<const T, 1, 1> const &, ndarray::Array<const T, 1, 1> const &) const;
207 
208 private:
209  double _sigma0, _sigma1;
210 };
211 
220 template <typename T>
221 class KdTree {
222 public:
223  // No copying
224  KdTree(const KdTree &) = delete;
225  KdTree &operator=(const KdTree &) = delete;
226 
227  // No moving
228  KdTree(KdTree &&) = delete;
229  KdTree &operator=(KdTree &&) = delete;
230 
231  // Add default constructor (which is no longer generated)
232  KdTree() = default;
233 
242  void Initialize(ndarray::Array<T, 2, 2> const &dt);
243 
259  void findNeighbors(ndarray::Array<int, 1, 1> neighdex, ndarray::Array<double, 1, 1> dd,
260  ndarray::Array<const T, 1, 1> const &v, int n_nn) const;
261 
269  T getData(int ipt, int idim) const;
270 
292  ndarray::Array<T, 1, 1> getData(int ipt) const;
293 
301  void addPoint(ndarray::Array<const T, 1, 1> const &v);
302 
311  void removePoint(int dex);
312 
316  int getNPoints() const;
317 
325  void getTreeNode(ndarray::Array<int, 1, 1> const &v, int dex) const;
326 
327 private:
328  ndarray::Array<int, 2, 2> _tree;
329  ndarray::Array<int, 1, 1> _inn;
330  ndarray::Array<T, 2, 2> _data;
331 
332  enum { DIMENSION, LT, GEQ, PARENT };
333 
334  //_tree stores the relationships between data points
335  //_tree[i][DIMENSION] is the dimension on which the ith node segregates its daughters
336  //
337  //_tree[i][LT] is the branch of the tree down which the daughters' DIMENSIONth component
338  // is less than the parent's
339  //
340  //_tree[i][GEQ] is the branch of the tree down which the daughters' DIMENSIONth component is
341  // greather than or equal to the parent's
342  //
343  //_tree[i][PARENT] is the parent node of the ith node
344 
345  //_data actually stores the data points
346 
347  int _npts, _dimensions, _room, _roomStep, _masterParent;
348  mutable int _neighborsFound, _neighborsWanted;
349 
350  //_room denotes the capacity of _data and _tree. It will usually be larger
351  // than _npts so that we do not have to reallocate
352  //_tree and _data every time we add a new point to the tree
353 
354  mutable ndarray::Array<double, 1, 1> _neighborDistances;
355  mutable ndarray::Array<int, 1, 1> _neighborCandidates;
356 
369  void _organize(ndarray::Array<int, 1, 1> const &use, int ct, int parent, int dir);
370 
376  int _findNode(ndarray::Array<const T, 1, 1> const &v) const;
377 
394  void _lookForNeighbors(ndarray::Array<const T, 1, 1> const &v, int consider, int from) const;
395 
399  int _testTree() const;
400 
414  int _walkUpTree(int target, int dir, int root) const;
415 
424  void _count(int where, int *ct) const;
425 
431  void _descend(int root);
432 
438  void _reassign(int target);
439 
443  double _distance(ndarray::Array<const T, 1, 1> const &p1, ndarray::Array<const T, 1, 1> const &p2) const;
444 };
445 
467 template <typename T>
469 public:
470  // No copying
471  GaussianProcess(const GaussianProcess &) = delete;
472  GaussianProcess &operator=(const GaussianProcess &) = delete;
473 
474  // No moving
475  GaussianProcess(GaussianProcess &&) = delete;
476  GaussianProcess &operator=(GaussianProcess &&) = delete;
477 
490  GaussianProcess(ndarray::Array<T, 2, 2> const &dataIn, ndarray::Array<T, 1, 1> const &ff,
491  std::shared_ptr<Covariogram<T> > const &covarIn);
492 
512  GaussianProcess(ndarray::Array<T, 2, 2> const &dataIn, ndarray::Array<T, 1, 1> const &mn,
513  ndarray::Array<T, 1, 1> const &mx, ndarray::Array<T, 1, 1> const &ff,
514  std::shared_ptr<Covariogram<T> > const &covarIn);
527  GaussianProcess(ndarray::Array<T, 2, 2> const &dataIn, ndarray::Array<T, 2, 2> const &ff,
528  std::shared_ptr<Covariogram<T> > const &covarIn);
545  GaussianProcess(ndarray::Array<T, 2, 2> const &dataIn, ndarray::Array<T, 1, 1> const &mn,
546  ndarray::Array<T, 1, 1> const &mx, ndarray::Array<T, 2, 2> const &ff,
547  std::shared_ptr<Covariogram<T> > const &covarIn);
548 
552  int getNPoints() const;
553 
558  int getDim() const;
559 
569  void getData(ndarray::Array<T, 2, 2> pts, ndarray::Array<T, 1, 1> fn,
570  ndarray::Array<int, 1, 1> indices) const;
571 
581  void getData(ndarray::Array<T, 2, 2> pts, ndarray::Array<T, 2, 2> fn,
582  ndarray::Array<int, 1, 1> indices) const;
583 
601  T interpolate(ndarray::Array<T, 1, 1> variance, ndarray::Array<T, 1, 1> const &vin,
602  int numberOfNeighbors) const;
618  void interpolate(ndarray::Array<T, 1, 1> mu, ndarray::Array<T, 1, 1> variance,
619  ndarray::Array<T, 1, 1> const &vin, int numberOfNeighbors) const;
620 
640  T selfInterpolate(ndarray::Array<T, 1, 1> variance, int dex, int numberOfNeighbors) const;
641 
656  void selfInterpolate(ndarray::Array<T, 1, 1> mu, ndarray::Array<T, 1, 1> variance, int dex,
657  int numberOfNeighbors) const;
658 
680  void batchInterpolate(ndarray::Array<T, 1, 1> mu, ndarray::Array<T, 1, 1> variance,
681  ndarray::Array<T, 2, 2> const &queries) const;
682 
700  void batchInterpolate(ndarray::Array<T, 1, 1> mu, ndarray::Array<T, 2, 2> const &queries) const;
701 
706  void batchInterpolate(ndarray::Array<T, 2, 2> mu, ndarray::Array<T, 2, 2> variance,
707  ndarray::Array<T, 2, 2> const &queries) const;
708 
713  void batchInterpolate(ndarray::Array<T, 2, 2> mu, ndarray::Array<T, 2, 2> const &queries) const;
714 
731  void addPoint(ndarray::Array<T, 1, 1> const &vin, T f);
732 
742  void addPoint(ndarray::Array<T, 1, 1> const &vin, ndarray::Array<T, 1, 1> const &f);
743 
755  void removePoint(int dex);
756 
763  void setKrigingParameter(T kk);
764 
771  void setCovariogram(std::shared_ptr<Covariogram<T> > const &covar);
772 
784  void setLambda(T lambda);
785 
800  GaussianProcessTimer &getTimes() const;
801 
802 private:
803  int _npts, _useMaxMin, _dimensions, _room, _roomStep, _nFunctions;
804 
805  T _krigingParameter, _lambda;
806 
807  ndarray::Array<T, 1, 1> _max, _min;
808  ndarray::Array<T, 2, 2> _function;
809 
810  KdTree<T> _kdTree;
811 
812  std::shared_ptr<Covariogram<T> > _covariogram;
813  mutable GaussianProcessTimer _timer;
814 };
815 }
816 }
817 }
818 
819 #endif //#ifndef LSST_AFW_MATH_GAUSSIAN_PROCESS_H
afw::table::Key< afw::table::Array< VariancePixelT > > variance
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.