LSSTApplications  17.0+124,17.0+14,17.0+73,18.0.0+37,18.0.0+80,18.0.0-4-g68ffd23+4,18.1.0-1-g0001055+12,18.1.0-1-g03d53ef+5,18.1.0-1-g1349e88+55,18.1.0-1-g2505f39+44,18.1.0-1-g5315e5e+4,18.1.0-1-g5e4b7ea+14,18.1.0-1-g7e8fceb+4,18.1.0-1-g85f8cd4+48,18.1.0-1-g8ff0b9f+4,18.1.0-1-ga2c679d+1,18.1.0-1-gd55f500+35,18.1.0-10-gb58edde+2,18.1.0-11-g0997b02+4,18.1.0-13-gfe4edf0b+12,18.1.0-14-g259bd21+21,18.1.0-19-gdb69f3f+2,18.1.0-2-g5f9922c+24,18.1.0-2-gd3b74e5+11,18.1.0-2-gfbf3545+32,18.1.0-26-g728bddb4+5,18.1.0-27-g6ff7ca9+2,18.1.0-3-g52aa583+25,18.1.0-3-g8ea57af+9,18.1.0-3-gb69f684+42,18.1.0-3-gfcaddf3+6,18.1.0-32-gd8786685a,18.1.0-4-gf3f9b77+6,18.1.0-5-g1dd662b+2,18.1.0-5-g6dbcb01+41,18.1.0-6-gae77429+3,18.1.0-7-g9d75d83+9,18.1.0-7-gae09a6d+30,18.1.0-9-gc381ef5+4,w.2019.45
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/DateTime.h"
35 #include "lsst/pex/exceptions.h"
36 
37 namespace lsst {
38 namespace afw {
39 namespace math {
40 
61 public:
63 
64  GaussianProcessTimer(GaussianProcessTimer const &) = default;
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;
136  Covariogram &operator=(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:
166  ~SquaredExpCovariogram() override;
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:
195  ~NeuralNetCovariogram() override;
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;
475  GaussianProcess &operator=(const GaussianProcess &) = delete;
476 
477  // No moving
478  GaussianProcess(GaussianProcess &&) = delete;
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 
803  GaussianProcessTimer &getTimes() const;
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
GaussianProcessTimer & operator=(GaussianProcessTimer const &)=default
afw::table::Key< afw::table::Array< VariancePixelT > > variance
Key< Flag > const & target
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.
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 ...
Interface for DateTime class.
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.
table::Key< double > sigma1
void addToEigen()
Adds time to _eigenTime.