37 GaussianProcessTimer::GaussianProcessTimer()
39 _interpolationCount = 0;
48 void GaussianProcessTimer::reset()
50 _interpolationCount = 0;
59 void GaussianProcessTimer::start()
65 void GaussianProcessTimer::addToEigen()
69 _eigenTime += after - _before;
73 void GaussianProcessTimer::addToVariance()
77 _varianceTime += after - _before;
81 void GaussianProcessTimer::addToSearch()
85 _searchTime += after - _before;
89 void GaussianProcessTimer::addToIteration()
93 _iterationTime += after - _before;
97 void GaussianProcessTimer::addToTotal(
int i)
101 _totalTime += after - _beginning;
102 _interpolationCount += i;
106 std::cout <<
"\nSearch time " << _searchTime <<
"\n";
107 std::cout <<
"Eigen time " << _eigenTime <<
"\n";
108 std::cout <<
"Variance time " << _varianceTime <<
"\n";
109 std::cout <<
"Iteration time " << _iterationTime <<
"\n";
110 std::cout <<
"Total time " << _totalTime <<
"\n";
111 std::cout <<
"Number of interpolations " << _interpolationCount <<
"\n";
114 template <
typename T >
119 _pts = dt.template getSize < 0 > ();
120 _dimensions = dt.template getSize < 1 > ();
134 for (i = 0 ; i < _pts ; i++ ) {
138 _organize(_inn,_pts, -1, -1);
143 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
144 "Failed to properly initialize KdTree\n");
149 template <
typename T >
160 _neighborsWanted = n_nn;
162 for (i = 0; i < n_nn; i++ ) _neighborDistances[i] = -1.0;
164 start = _findNode(v);
166 _neighborDistances[0] = _distance(v,_data[start]);
167 _neighborCandidates[0] = start;
171 for (i = 1; i < 4; i++ ) {
172 if(_tree[start][i] >= 0){
173 _lookForNeighbors(v,_tree[start][i], start);
177 for (i = 0 ; i < n_nn ; i++ ) {
178 neighdex[i] = _neighborCandidates[i];
179 dd[i] = _neighborDistances[i];
183 template <
typename T >
186 return _data[ipt][idim];
189 template <
typename T >
195 template <
typename T >
199 int i,j,node,dim,dir;
202 dim = _tree[node][DIMENSION] + 1;
203 if(dim == _dimensions)dim = 0;
210 dbuff.
deep() = _data;
211 tbuff.
deep() = _tree;
219 for (i = 0; i < _pts; i++ ) {
220 for (j = 0; j < _dimensions; j++ ) _data[i][j] = dbuff[i][j];
221 for (j = 0; j < 4; j++ ) _tree[i][j] = tbuff[i][j];
225 _tree[_pts][DIMENSION] = dim;
226 _tree[_pts][
PARENT] = node;
227 i = _tree[node][DIMENSION];
229 if(_data[node][i] > v[i]){
230 if(_tree[node][LT] >= 0){
231 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
232 "Trying to add to KdTree in a node that is already occupied\n");
234 _tree[node][LT] = _pts;
238 if(_tree[node][GEQ] >= 0){
239 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
240 "Trying to add to KdTree in a node that is already occupied\n");
242 _tree[node][GEQ] = _pts;
245 _tree[_pts][LT] = -1;
246 _tree[_pts][GEQ] = -1;
247 for (i = 0; i < _dimensions; i++ ) {
248 _data[_pts][i] = v[i];
254 i = _walkUpTree(_tree[_pts-1][
PARENT], dir, _pts-1);
255 if (i != _masterParent){
256 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
257 "Adding to KdTree failed\n");
262 template <
typename T >
268 template <
typename T >
272 v[0] = _tree[dex][DIMENSION];
273 v[1] = _tree[dex][LT];
274 v[2] = _tree[dex][GEQ];
275 v[3] = _tree[dex][
PARENT];
278 template <
typename T >
282 std::vector < int > isparent;
286 for (i = 0; i < _pts; i++ ) isparent.push_back(0);
289 for (i = 0; i < _pts; i++ ) {
290 if(_tree[i][
PARENT] < 0)j++;
293 std::cout <<
"_tree FAILURE " << j <<
" _masterParents\n";
297 for (i = 0; i < _pts; i++ ) {
298 if(_tree[i][
PARENT] >= 0){
299 isparent[_tree[i][
PARENT]]++;
303 for (i = 0; i < _pts; i++ ) {
305 std::cout <<
"_tree FAILURE " << i <<
" is parent to " << isparent[i] <<
"\n";
311 for (i = 0; i < _pts; i++ ) {
312 if(_tree[i][
PARENT] >= 0) {
313 if(_tree[_tree[i][
PARENT]][LT] == i)j = LT;
315 output = _walkUpTree(_tree[i][
PARENT], j, i);
317 if(output != _masterParent){
323 if(output != _masterParent)
return 0;
327 template <
typename T >
335 int i,j,k,l,idim,daughter;
339 std::vector < std::vector < T > > toSort;
340 std::vector < T > toSortElement;
348 for (i = 0; i < _dimensions; i++ ) {
351 for (j = 0; j < ct; j++ ) {
352 mean += _data[use[j]][i];
353 var += _data[use[j]][i]*_data[use[j]][i];
355 mean = mean/double(ct);
356 var = var/double(ct) - mean*mean;
357 if(i == 0 || var > varbest ||
358 (var == varbest && parent >= 0 && i > _tree[parent][DIMENSION])) {
372 toSortElement.push_back(_data[use[0]][idim]);
373 toSortElement.push_back(T(use[0]));
374 toSort.push_back(toSortElement);
375 for(i = 1; i < ct; i++ ) {
376 toSortElement[0] = _data[use[i]][idim];
377 toSortElement[1] = T(use[i]);
378 toSort.push_back(toSortElement);
381 std::stable_sort(toSort.begin(), toSort.end());
385 while(k > 0 && toSort[k][0] == toSort[k - 1][0]) k--;
387 while(l < ct - 1 && toSort[l][0] == toSort[ct/2][0]) l++;
389 if((ct/2 - k) < (l - ct/2) || l == ct - 1) j = k;
392 for(i = 0; i < ct; i++ ) {
393 use[i] = int(toSort[i][1]);
397 if(parent >= 0)_tree[parent][dir] = daughter;
398 _tree[daughter][DIMENSION] = idim;
399 _tree[daughter][
PARENT] = parent;
404 else _tree[daughter][GEQ] = -1;
407 _organize(use, j, daughter, LT);
409 else _tree[daughter][LT] = -1;
415 if(parent >= 0) _tree[parent][dir] = daughter;
417 idim = _tree[parent][DIMENSION] + 1;
419 if(idim >= _dimensions)idim = 0;
421 _tree[daughter][DIMENSION] = idim;
422 _tree[daughter][LT] = - 1;
423 _tree[daughter][GEQ] = - 1;
424 _tree[daughter][
PARENT] = parent;
429 _masterParent = daughter;
434 template <
typename T >
437 int consider,next,dim;
439 dim = _tree[_masterParent][DIMENSION];
441 if(v[dim] < _data[_masterParent][dim]) consider = _tree[_masterParent][LT];
442 else consider = _tree[_masterParent][GEQ];
450 dim = _tree[consider][DIMENSION];
451 if(v[dim] < _data[consider][dim]) next = _tree[consider][LT];
452 else next = _tree[consider][GEQ];
459 template <
typename T >
468 dd = _distance(v, _data[consider]);
470 if(_neighborsFound < _neighborsWanted || dd < _neighborDistances[_neighborsWanted -1]) {
472 for(j = 0; j < _neighborsFound && _neighborDistances[j] < dd; j++ );
474 for(i = _neighborsWanted - 1; i > j; i-- ){
475 _neighborDistances[i] = _neighborDistances[i - 1];
476 _neighborCandidates[i] = _neighborCandidates[i - 1];
479 _neighborDistances[j] = dd;
480 _neighborCandidates[j] = consider;
482 if(_neighborsFound < _neighborsWanted) _neighborsFound++;
485 if(_tree[consider][
PARENT] == from){
488 i = _tree[consider][DIMENSION];
489 dd = v[i] - _data[consider][i];
490 if((dd <= _neighborDistances[_neighborsFound - 1] || _neighborsFound < _neighborsWanted)
491 && _tree[consider][LT] >= 0){
493 _lookForNeighbors(v, _tree[consider][LT], consider);
496 dd = _data[consider][i] - v[i];
497 if((dd <= _neighborDistances[_neighborsFound - 1] || _neighborsFound < _neighborsWanted)
498 && _tree[consider][GEQ] >= 0){
500 _lookForNeighbors(v, _tree[consider][GEQ], consider);
507 if(_tree[consider][LT] == from){
514 j = _tree[consider][going];
517 i = _tree[consider][DIMENSION];
519 if(going == 1) dd = v[i] - _data[consider][i];
520 else dd = _data[consider][i] - v[i];
522 if(dd <= _neighborDistances[_neighborsFound - 1] || _neighborsFound < _neighborsWanted) {
523 _lookForNeighbors(v, j, consider);
528 if(_tree[consider][
PARENT] >= 0) {
529 _lookForNeighbors(v, _tree[consider][
PARENT], consider);
534 template <
typename T >
548 if(_data[root][_tree[target][DIMENSION]] >= _data[target][_tree[target][DIMENSION]]){
553 if(_data[root][_tree[target][DIMENSION]] < _data[target][_tree[target][DIMENSION]]) {
558 if(_tree[target][
PARENT] >= 0){
559 if(_tree[_tree[target][
PARENT]][LT] == target)i = LT;
561 output = output*_walkUpTree(_tree[target][
PARENT],i,root);
565 output = output*target;
573 template <
typename T >
577 int nl,nr,i,j,k,side;
582 if(_tree[target][LT] >= 0){
584 _count(_tree[target][LT], &nl);
587 if(_tree[target][GEQ] >= 0){
589 _count(_tree[target][GEQ], &nr);
592 if(nl == 0 && nr == 0){
594 k = _tree[target][
PARENT];
596 if(_tree[k][LT] == target) _tree[k][LT] = - 1;
597 else if(_tree[k][GEQ] == target) _tree[k][GEQ] = - 1;
600 else if((nl == 0 && nr > 0) || (nr == 0 && nl > 0)){
602 if(nl == 0) side = GEQ;
605 k = _tree[target][
PARENT];
608 if(_tree[k][LT] == target){
609 _tree[k][LT] = _tree[target][side];
610 _tree[_tree[k][LT]][
PARENT] = k;
614 _tree[k][GEQ] = _tree[target][side];
615 _tree[_tree[k][GEQ]][
PARENT] = k;
619 _masterParent = _tree[target][side];
620 _tree[_tree[target][side]][
PARENT] = - 1;
625 if(nl > nr)side = LT;
628 k = _tree[target][
PARENT];
630 _masterParent = _tree[target][side];
631 _tree[_masterParent][
PARENT] = - 1;
634 if(_tree[k][LT] == target){
635 _tree[k][LT] = _tree[target][side];
636 _tree[_tree[k][LT]][
PARENT] = k;
639 _tree[k][GEQ] = _tree[target][side];
640 _tree[_tree[k][GEQ]][
PARENT] = k;
644 root = _tree[target][3 - side];
649 if(target < _pts - 1){
650 for(i = target + 1;i < _pts;i++ ){
651 for(j = 0; j < 4; j++ ) _tree[i - 1][j] = _tree[i][j];
653 for(j = 0; j < _dimensions; j++ ) _data[i - 1][j] = _data[i][j];
656 for(i = 0; i < _pts; i++ ){
657 for(j = 1; j < 4; j++ ){
658 if(_tree[i][j] > target) _tree[i][j]--;
662 if(_masterParent > target)_masterParent-- ;
668 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
669 "Subtracting from KdTree failed\n");
675 template <
typename T >
680 if(_tree[where][LT] >= 0) {
682 _count(_tree[where][LT], ct);
684 if(_tree[where][GEQ] >= 0) {
686 _count(_tree[where][GEQ], ct);
690 template <
typename T >
696 where = _masterParent;
697 if(_data[target][_tree[where][DIMENSION]] < _data[where][_tree[where][DIMENSION]]) dir = LT;
700 k = _tree[where][dir];
703 if(_data[target][_tree[where][DIMENSION]] < _data[where][_tree[where][DIMENSION]]) dir = LT;
705 k = _tree[where][dir];
708 _tree[where][dir] = target;
709 _tree[target][
PARENT] = where;
710 _tree[target][LT] = - 1;
711 _tree[target][GEQ] = - 1;
712 _tree[target][DIMENSION] = _tree[where][DIMENSION] + 1;
714 if(_tree[target][DIMENSION] == _dimensions) _tree[target][DIMENSION] = 0;
717 template <
typename T >
721 if(_tree[root][LT] >= 0) _descend(_tree[root][LT]);
722 if(_tree[root][GEQ] >= 0) _descend(_tree[root][GEQ]);
727 template <
typename T >
735 dd = p1.template getSize< 0 >();
737 for(i = 0;i < dd;i++ ) ans += (p1[i] - p2[i])*(p1[i] - p2[i]);
744 template <
typename T >
752 _covariogram = covarIn;
754 _pts = dataIn.template getSize < 0 > ();
755 _dimensions = dataIn.template getSize < 1 > ();
763 for(i = 0; i < _pts; i++ ) _function[i][0] = ff[i];
765 _krigingParameter = T(1.0);
771 _kdTree.Initialize(dataIn);
773 _pts = _kdTree.getPoints();
777 template <
typename T >
789 _covariogram = covarIn;
791 _pts = dataIn.template getSize < 0 > ();
792 _dimensions = dataIn.template getSize < 1 > ();
796 _krigingParameter = T(1.0);
799 _krigingParameter = T(1.0);
807 for(i = 0; i < _pts; i++ ) {
808 for(j = 0; j < _dimensions; j++ ) {
809 normalizedData[i][j] = (dataIn[i][j] - _min[j])/(_max[j] - _min[j]);
814 _kdTree.Initialize(normalizedData);
816 _pts = _kdTree.getPoints();
819 for(i = 0; i < _pts; i++ )_function[i][0] = ff[i];
822 template <
typename T >
828 _covariogram = covarIn;
830 _pts = dataIn.template getSize < 0 > ();
831 _dimensions = dataIn.template getSize < 1 > ();
836 _nFunctions = ff.template getSize < 1 > ();
838 _function.deep() = ff;
840 _krigingParameter = T(1.0);
847 _kdTree.Initialize(dataIn);
849 _pts = _kdTree.getPoints();
852 template <
typename T >
864 _covariogram = covarIn;
866 _pts = dataIn.template getSize < 0 > ();
867 _dimensions = dataIn.template getSize < 1 > ();
872 _krigingParameter = T(1.0);
875 _krigingParameter = T(1.0);
883 for(i = 0; i < _pts; i++ ) {
884 for(j = 0; j < _dimensions; j++ ) {
885 normalizedData[i][j] = (dataIn[i][j] - _min[j])/(_max[j] - _min[j]);
890 _kdTree.Initialize(normalizedData);
891 _pts = _kdTree.getPoints();
892 _nFunctions = ff.template getSize < 1 > ();
894 _function.deep() = ff;
898 template <
typename T >
901 int numberOfNeighbors)
const
904 if(numberOfNeighbors <= 0){
905 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
906 "Asked for zero or negative number of neighbors\n");
909 if(numberOfNeighbors > _kdTree.getPoints()){
910 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
911 "Asked for more neighbors than you have data points\n");
921 Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > covariance,bb,xx;
922 Eigen::LDLT < Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > > ldlt;
927 bb.resize(numberOfNeighbors, 1);
928 xx.resize(numberOfNeighbors, 1);
930 covariance.resize(numberOfNeighbors,numberOfNeighbors);
945 for(i = 0; i < _dimensions; i++ ) vv[i] = (vin[i] - _min[i])/(_max[i] - _min[i]);
952 _kdTree.findNeighbors(neighbors, neighborDistances, vv, numberOfNeighbors);
954 _timer.addToSearch();
957 for(i = 0; i < numberOfNeighbors; i++ )fbar += _function[neighbors[i]][0];
958 fbar = fbar/double(numberOfNeighbors);
960 for(i = 0; i < numberOfNeighbors; i++ ){
961 covarianceTestPoint[i] = (*_covariogram)(vv, _kdTree.
getData(neighbors[i]));
963 covariance(i,i) = (*_covariogram)(_kdTree.getData(neighbors[i]), _kdTree.getData(neighbors[i]))
966 for(j = i + 1; j < numberOfNeighbors; j++ ){
967 covariance(i,j) = (*_covariogram)(_kdTree.getData(neighbors[i]),
968 _kdTree.getData(neighbors[j]));
969 covariance(j,i) = covariance(i, j);
973 _timer.addToIteration();
976 ldlt.compute(covariance);
978 for(i = 0; i < numberOfNeighbors; i++) bb(i,0) = _function[neighbors[i]][0] - fbar;
985 for(i = 0; i < numberOfNeighbors; i++ ){
986 mu += covarianceTestPoint[i]*xx(i, 0);
989 _timer.addToIteration();
991 variance(0) = (*_covariogram)(vv, vv) + _lambda;
993 for(i = 0; i < numberOfNeighbors; i++ ) bb(i) = covarianceTestPoint[i];
997 for(i = 0; i < numberOfNeighbors; i++ ){
998 variance(0) -= covarianceTestPoint[i]*xx(i, 0);
1001 variance(0) = variance(0)*_krigingParameter;
1003 _timer.addToVariance();
1004 _timer.addToTotal(1);
1009 template <
typename T >
1013 int numberOfNeighbors)
const
1017 if(numberOfNeighbors <= 0){
1018 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1019 "Asked for zero or negative number of neighbors\n");
1022 if(numberOfNeighbors > _kdTree.getPoints()){
1023 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1024 "Asked for more neighbors than you have data points\n");
1035 Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > covariance,bb,xx;
1036 Eigen::LDLT < Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > > ldlt;
1041 bb.resize(numberOfNeighbors,1);
1042 xx.resize(numberOfNeighbors,1);
1043 covariance.resize(numberOfNeighbors,numberOfNeighbors);
1051 if(_useMaxMin == 1) {
1057 for(i = 0; i < _dimensions; i++ )vv[i] = (vin[i] - _min[i])/(_max[i] - _min[i]);
1064 _kdTree.findNeighbors(neighbors, neighborDistances, vv, numberOfNeighbors);
1066 _timer.addToSearch();
1068 for(i = 0; i < numberOfNeighbors; i++ ) {
1069 covarianceTestPoint[i] = (*_covariogram)(vv, _kdTree.
getData(neighbors[i]));
1071 covariance(i,i) = (*_covariogram)(_kdTree.getData(neighbors[i]), _kdTree.getData(neighbors[i]))
1074 for(j = i + 1; j < numberOfNeighbors; j++ ){
1075 covariance(i,j) = (*_covariogram)(_kdTree.getData(neighbors[i]),
1076 _kdTree.getData(neighbors[j]));
1077 covariance(j,i) = covariance(i, j);
1081 _timer.addToIteration();
1084 ldlt.compute(covariance);
1086 for(ii = 0; ii < _nFunctions; ii++ ) {
1089 for(i = 0; i < numberOfNeighbors; i++ )fbar += _function[neighbors[i]][ii];
1090 fbar = fbar/double(numberOfNeighbors);
1092 for(i = 0; i < numberOfNeighbors; i++ )bb(i,0) = _function[neighbors[i]][ii] - fbar;
1093 xx = ldlt.solve(bb);
1097 for(i = 0; i < numberOfNeighbors; i++ ) {
1098 mu[ii] += covarianceTestPoint[i]*xx(i, 0);
1103 _timer.addToEigen();
1105 variance[0] = (*_covariogram)(vv, vv) + _lambda;
1107 for(i = 0; i < numberOfNeighbors; i++ )bb(i) = covarianceTestPoint[i];
1109 xx = ldlt.solve(bb);
1111 for(i = 0; i < numberOfNeighbors; i++ ) {
1112 variance[0] -= covarianceTestPoint[i]*xx(i, 0);
1114 variance[0] = variance[0]*_krigingParameter;
1117 for(i = 1; i < _nFunctions; i++ )variance[i] = variance[0];
1119 _timer.addToVariance();
1120 _timer.addToTotal(1);
1124 template <
typename T >
1127 int numberOfNeighbors)
const
1130 if(numberOfNeighbors <= 0){
1131 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1132 "Asked for zero or negative number of neighbors\n");
1135 if(numberOfNeighbors > _kdTree.getPoints()){
1136 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1137 "Asked for more neighbors than you have data points\n");
1149 Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > covariance,bb,xx;
1150 Eigen::LDLT < Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > > ldlt;
1154 bb.resize(numberOfNeighbors, 1);
1155 xx.resize(numberOfNeighbors, 1);
1156 covariance.resize(numberOfNeighbors,numberOfNeighbors);
1167 _kdTree.findNeighbors(selfNeighbors, selfDistances, _kdTree.
getData(dex),
1168 numberOfNeighbors + 1);
1170 _timer.addToSearch();
1172 if(selfNeighbors[0]!= dex) {
1173 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1174 "Nearest neighbor search in selfInterpolate did not find self\n");
1182 for(i = 0; i < numberOfNeighbors; i++ ){
1183 neighbors[i] = selfNeighbors[i + 1];
1184 neighborDistances[i] = selfDistances[i + 1];
1188 for(i = 0; i < numberOfNeighbors; i++ ) fbar += _function[neighbors[i]][0];
1189 fbar = fbar/double(numberOfNeighbors);
1191 for(i = 0; i < numberOfNeighbors; i++ ){
1192 covarianceTestPoint[i] = (*_covariogram)(_kdTree.getData(dex),
1193 _kdTree.getData(neighbors[i]));
1195 covariance(i, i) = (*_covariogram)(_kdTree.getData(neighbors[i]),
1196 _kdTree.getData(neighbors[i])) + _lambda;
1198 for(j = i + 1; j < numberOfNeighbors; j++ ) {
1199 covariance(i, j) = (*_covariogram)(_kdTree.getData(neighbors[i]),
1200 _kdTree.getData(neighbors[j]));
1201 covariance(j, i) = covariance(i ,j);
1204 _timer.addToIteration();
1207 ldlt.compute(covariance);
1210 for(i = 0; i < numberOfNeighbors;i++ ) bb(i, 0) = _function[neighbors[i]][0] - fbar;
1211 xx = ldlt.solve(bb);
1212 _timer.addToEigen();
1216 for(i = 0; i < numberOfNeighbors; i++ ) {
1217 mu += covarianceTestPoint[i]*xx(i, 0);
1221 variance(0) = (*_covariogram)(_kdTree.getData(dex), _kdTree.getData(dex)) + _lambda;
1223 for(i = 0; i < numberOfNeighbors; i++ )bb(i) = covarianceTestPoint[i];
1225 xx = ldlt.solve(bb);
1227 for(i = 0; i < numberOfNeighbors; i++ ){
1228 variance(0) -= covarianceTestPoint[i]*xx(i,0);
1231 variance(0) = variance(0)*_krigingParameter;
1232 _timer.addToVariance();
1233 _timer.addToTotal(1);
1238 template <
typename T >
1242 int numberOfNeighbors)
const{
1244 if(numberOfNeighbors <= 0){
1245 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1246 "Asked for zero or negative number of neighbors\n");
1249 if(numberOfNeighbors + 1 > _kdTree.getPoints()){
1250 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1251 "Asked for more neighbors than you have data points\n");
1254 if(dex < 0 || dex >=_kdTree.getPoints()){
1255 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1256 "Asked to self interpolate on a point that does not exist\n");
1268 Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > covariance,bb,xx;
1269 Eigen::LDLT < Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > > ldlt;
1273 bb.resize(numberOfNeighbors, 1);
1274 xx.resize(numberOfNeighbors, 1);
1275 covariance.resize(numberOfNeighbors,numberOfNeighbors);
1286 _kdTree.findNeighbors(selfNeighbors, selfDistances, _kdTree.
getData(dex),
1287 numberOfNeighbors + 1);
1289 _timer.addToSearch();
1291 if(selfNeighbors[0]!= dex) {
1293 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1294 "Nearest neighbor search in selfInterpolate did not find self\n");
1302 for(i = 0; i < numberOfNeighbors; i++ ) {
1303 neighbors[i] = selfNeighbors[i + 1];
1304 neighborDistances[i] = selfDistances[i + 1];
1309 for(i = 0; i < numberOfNeighbors; i++ ) {
1310 covarianceTestPoint[i] = (*_covariogram)(_kdTree.getData(dex),_kdTree.getData(neighbors[i]));
1312 covariance(i, i) = (*_covariogram)(_kdTree.getData(neighbors[i]), _kdTree.getData(neighbors[i]))
1315 for(j = i + 1; j < numberOfNeighbors; j++ ) {
1316 covariance(i, j) = (*_covariogram)(_kdTree.getData(neighbors[i]),
1317 _kdTree.getData(neighbors[j]));
1318 covariance(j, i) = covariance(i, j);
1321 _timer.addToIteration();
1325 ldlt.compute(covariance);
1327 for(ii = 0; ii < _nFunctions; ii++ ) {
1330 for(i = 0; i < numberOfNeighbors; i++ )fbar += _function[neighbors[i]][ii];
1331 fbar = fbar/double(numberOfNeighbors);
1333 for(i = 0; i < numberOfNeighbors; i++ )bb(i,0) = _function[neighbors[i]][ii] - fbar;
1334 xx = ldlt.solve(bb);
1338 for(i = 0; i < numberOfNeighbors; i++ ){
1339 mu[ii] += covarianceTestPoint[i]*xx(i,0);
1343 _timer.addToEigen();
1345 variance[0] = (*_covariogram)(_kdTree.getData(dex), _kdTree.getData(dex)) + _lambda;
1347 for(i = 0; i < numberOfNeighbors; i++ )bb(i) = covarianceTestPoint[i];
1349 xx = ldlt.solve(bb);
1351 for(i = 0; i < numberOfNeighbors; i++ ) {
1352 variance[0] -= covarianceTestPoint[i]*xx(i,0);
1355 variance[0] = variance[0]*_krigingParameter;
1357 for(i = 1; i < _nFunctions; i++ )variance[i] = variance[0];
1359 _timer.addToVariance();
1360 _timer.addToTotal(1);
1364 template <
typename T >
1370 int i,j,ii,nQueries;
1373 Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > batchCovariance,batchbb,batchxx;
1374 Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > queryCovariance;
1375 Eigen::LDLT < Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > > ldlt;
1381 nQueries = queries.template getSize < 0 > ();
1385 batchbb.resize(_pts, 1);
1386 batchxx.resize(_pts, 1);
1387 batchCovariance.resize(_pts, _pts);
1388 queryCovariance.resize(_pts, 1);
1391 for(i = 0; i < _pts; i++ ) {
1393 batchCovariance(i, i) = (*_covariogram)(_kdTree.getData(i), _kdTree.getData(i)) + _lambda;
1394 for(j = i + 1; j < _pts; j++ ) {
1395 batchCovariance(i, j) = (*_covariogram)(_kdTree.getData(i), _kdTree.getData(j));
1396 batchCovariance(j, i) = batchCovariance(i, j);
1399 _timer.addToIteration();
1401 ldlt.compute(batchCovariance);
1404 for(i = 0; i < _pts; i++ ) {
1405 fbar += _function[i][0];
1407 fbar = fbar/T(_pts);
1409 for(i = 0; i < _pts; i++ ){
1410 batchbb(i, 0) = _function[i][0] - fbar;
1412 batchxx = ldlt.solve(batchbb);
1413 _timer.addToEigen();
1416 for(ii = 0; ii < nQueries; ii++ ) {
1417 for(i = 0; i < _dimensions; i++ )v1[i] = queries[ii][i];
1418 if(_useMaxMin == 1) {
1419 for(i = 0; i < _dimensions; i++ )v1[i] = (v1[i] - _min[i])/(_max[i] - _min[i]);
1422 for(i = 0; i < _pts; i++ ){
1423 mu(ii) += batchxx(i)*(*_covariogram)(v1, _kdTree.getData(i));
1426 _timer.addToIteration();
1428 for(ii = 0; ii < nQueries; ii++ ) {
1429 for(i = 0; i < _dimensions; i++ )v1[i] = queries[ii][i];
1430 if(_useMaxMin == 1) {
1431 for(i = 0; i < _dimensions; i++ )v1[i] = (v1[i] - _min[i])/(_max[i] - _min[i]);
1434 for(i = 0; i < _pts; i++ ) {
1435 batchbb(i, 0) = (*_covariogram)(v1, _kdTree.getData(i));
1436 queryCovariance(i, 0) = batchbb(i, 0);
1438 batchxx = ldlt.solve(batchbb);
1440 variance[ii] = (*_covariogram)(v1, v1) + _lambda;
1442 for(i = 0; i < _pts; i++ ){
1443 variance[ii] -= queryCovariance(i, 0)*batchxx(i);
1446 variance[ii] = variance[ii]*_krigingParameter;
1450 _timer.addToVariance();
1451 _timer.addToTotal(nQueries);
1454 template <
typename T >
1460 int i,j,ii,nQueries,ifn;
1463 Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > batchCovariance,batchbb,batchxx;
1464 Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > queryCovariance;
1465 Eigen::LDLT < Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > > ldlt;
1471 nQueries = queries.template getSize < 0 > ();
1476 batchbb.resize(_pts, 1);
1477 batchxx.resize(_pts, 1);
1478 batchCovariance.resize(_pts, _pts);
1479 queryCovariance.resize(_pts, 1);
1481 for(i = 0; i < _pts; i++ ){
1482 batchCovariance(i, i) = (*_covariogram)(_kdTree.getData(i), _kdTree.getData(i)) + _lambda;
1483 for(j = i + 1; j < _pts; j++ ) {
1484 batchCovariance(i, j) = (*_covariogram)(_kdTree.getData(i), _kdTree.getData(j));
1485 batchCovariance(j, i) = batchCovariance(i, j);
1489 _timer.addToIteration();
1491 ldlt.compute(batchCovariance);
1493 _timer.addToEigen();
1495 for(ifn = 0; ifn < _nFunctions; ifn++ ) {
1498 for(i = 0; i < _pts; i++ ){
1499 fbar += _function[i][ifn];
1501 fbar = fbar/T(_pts);
1502 _timer.addToIteration();
1504 for(i = 0; i < _pts; i++ ){
1505 batchbb(i,0) = _function[i][ifn] - fbar;
1507 batchxx = ldlt.solve(batchbb);
1508 _timer.addToEigen();
1511 for(ii = 0; ii < nQueries; ii++ ){
1512 for(i = 0; i < _dimensions; i++ ) v1[i] = queries[ii][i];
1513 if(_useMaxMin == 1) {
1514 for(i = 0; i < _dimensions; i++ ) v1[i] = (v1[i] - _min[i])/(_max[i] - _min[i]);
1517 for(i = 0; i < _pts; i++ ){
1518 mu[ii][ifn] += batchxx(i)*(*_covariogram)(v1, _kdTree.getData(i));
1524 _timer.addToIteration();
1525 for(ii = 0; ii < nQueries; ii++ ){
1526 for(i = 0; i < _dimensions; i++ ) v1[i] = queries[ii][i];
1527 if(_useMaxMin == 1){
1528 for(i = 0; i < _dimensions; i++ ) v1[i] = (v1[i] - _min[i])/(_max[i] - _min[i]);
1531 for(i = 0;i < _pts;i++ ) {
1532 batchbb(i,0) = (*_covariogram)(v1,_kdTree.getData(i));
1533 queryCovariance(i,0) = batchbb(i,0);
1535 batchxx = ldlt.solve(batchbb);
1537 variance[ii][0] = (*_covariogram)(v1, v1) + _lambda;
1539 for(i = 0; i < _pts; i++ ) {
1540 variance[ii][0] -= queryCovariance(i, 0)*batchxx(i);
1543 variance[ii][0] = variance[ii][0]*_krigingParameter;
1544 for(i = 1; i < _nFunctions; i++ ) variance[ii][i] = variance[ii][0];
1547 _timer.addToVariance();
1548 _timer.addToTotal(nQueries);
1551 template <
typename T >
1556 int i,j,ii,nQueries;
1559 Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > batchCovariance,batchbb,batchxx;
1560 Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > queryCovariance;
1561 Eigen::LDLT < Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > > ldlt;
1567 nQueries = queries.template getSize < 0 > ();
1571 batchbb.resize(_pts, 1);
1572 batchxx.resize(_pts, 1);
1573 batchCovariance.resize(_pts, _pts);
1574 queryCovariance.resize(_pts, 1);
1577 for(i = 0; i < _pts; i++ ) {
1578 batchCovariance(i, i) = (*_covariogram)(_kdTree.getData(i), _kdTree.getData(i)) + _lambda;
1579 for(j = i + 1; j < _pts; j++ ) {
1580 batchCovariance(i, j) = (*_covariogram)(_kdTree.getData(i), _kdTree.getData(j));
1581 batchCovariance(j, i) = batchCovariance(i, j);
1584 _timer.addToIteration();
1586 ldlt.compute(batchCovariance);
1589 for(i = 0; i < _pts; i++ ) {
1590 fbar += _function[i][0];
1592 fbar = fbar/T(_pts);
1594 for(i = 0; i < _pts; i++ ) {
1595 batchbb(i, 0) = _function[i][0] - fbar;
1597 batchxx = ldlt.solve(batchbb);
1598 _timer.addToEigen();
1600 for(ii = 0; ii < nQueries; ii++ ) {
1602 for(i = 0; i < _dimensions; i++ ) v1[i] = queries[ii][i];
1603 if(_useMaxMin == 1) {
1604 for(i = 0; i < _dimensions; i++ )v1[i] = (v1[i] - _min[i])/(_max[i] - _min[i]);
1608 for(i = 0; i < _pts; i++ ) {
1609 mu(ii) += batchxx(i)*(*_covariogram)(v1, _kdTree.getData(i));
1612 _timer.addToIteration();
1613 _timer.addToTotal(nQueries);
1617 template <
typename T >
1622 int i,j,ii,nQueries,ifn;
1626 Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > batchCovariance,batchbb,batchxx;
1627 Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > queryCovariance;
1628 Eigen::LDLT < Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > > ldlt;
1634 nQueries = queries.template getSize < 0 > ();
1639 batchbb.resize(_pts, 1);
1640 batchxx.resize(_pts, 1);
1641 batchCovariance.resize(_pts, _pts);
1642 queryCovariance.resize(_pts, 1);
1645 for(i = 0; i < _pts; i++ ){
1646 batchCovariance(i, i) = (*_covariogram)(_kdTree.getData(i), _kdTree.getData(i)) + _lambda;
1647 for(j = i + 1; j < _pts; j++ ){
1648 batchCovariance(i, j) = (*_covariogram)(_kdTree.getData(i), _kdTree.getData(j));
1649 batchCovariance(j, i) = batchCovariance(i, j);
1653 _timer.addToIteration();
1655 ldlt.compute(batchCovariance);
1657 _timer.addToEigen();
1659 for(ifn = 0; ifn < _nFunctions; ifn++ ){
1662 for(i = 0; i < _pts; i++ ){
1663 fbar += _function[i][ifn];
1665 fbar = fbar/T(_pts);
1667 _timer.addToIteration();
1669 for(i = 0; i < _pts; i++ ){
1670 batchbb(i, 0) = _function[i][ifn] - fbar;
1672 batchxx = ldlt.solve(batchbb);
1673 _timer.addToEigen();
1676 for(ii = 0; ii < nQueries; ii++ ){
1677 for(i = 0; i < _dimensions; i++ )v1[i] = queries[ii][i];
1678 if(_useMaxMin == 1){
1679 for(i = 0;i < _dimensions;i++ )v1[i] = (v1[i] - _min[i])/(_max[i] - _min[i]);
1683 for(i = 0; i < _pts; i++ ){
1684 mu[ii][ifn] += batchxx(i)*(*_covariogram)(v1, _kdTree.getData(i));
1691 _timer.addToTotal(nQueries);
1696 template <
typename T >
1702 if(_nFunctions!= 1){
1704 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1705 "You are calling the wrong addPoint; you need a vector of functions\n");
1712 for(i = 0; i < _dimensions; i++ ){
1714 if(_useMaxMin == 1){
1715 v[i] = (v[i] - _min[i])/(_max[i] - _min[i]);
1722 buff.
deep() = _function;
1726 for(i = 0; i < _pts; i++ ) {
1728 for(j = 0; j < _nFunctions; j++ ) {
1729 _function[i][j] = buff[i][j];
1735 _function[_pts][0] = f;
1737 _kdTree.addPoint(v);
1738 _pts = _kdTree.getPoints();
1742 template <
typename T >
1752 for(i = 0; i < _dimensions; i++ ) {
1754 if(_useMaxMin == 1) {
1755 v[i] = (v[i] - _min[i])/(_max[i] - _min[i]);
1762 buff.
deep() = _function;
1766 for(i = 0; i < _pts; i++ ) {
1767 for(j = 0; j < _nFunctions; j++ ) {
1768 _function[i][j] = buff[i][j];
1773 for(i = 0; i < _nFunctions; i++ )_function[_pts][i] = f[i];
1775 _kdTree.addPoint(v);
1776 _pts = _kdTree.getPoints();
1781 template <
typename T >
1789 for(i = dex; i < _pts; i++ ) {
1790 for(j = 0; j < _nFunctions; j++ ) {
1791 _function[i][j] = _function[i + 1][j];
1794 _pts = _kdTree.getPoints();
1797 template <
typename T >
1800 _krigingParameter = kk;
1803 template <
typename T >
1805 _covariogram = covar;
1808 template <
typename T >
1814 template <
typename T >
1820 template <
typename T >
1823 template <
typename T >
1827 std::cout <<
"by the way, you are calling the wrong operator\n";
1832 template <
typename T >
1835 template <
typename T >
1841 template <
typename T >
1844 _ellSquared = ellSquared;
1847 template <
typename T >
1854 for(i = 0; i < p1.template getSize < 0 > (); i++ ){
1855 d += (p1[i] - p2[i])*(p1[i] - p2[i]);
1859 return T(exp( - 0.5*d));
1862 template <
typename T >
1865 template <
typename T >
1872 template <
typename T >
1878 double num,denom1,denom2,arg;
1880 dim = p1.template getSize < 0 > ();
1883 denom1 = 1.0 + 2.0*_sigma0;
1884 denom2 = 1.0 + 2.0*_sigma0;
1886 for(i = 0; i < dim; i++ ) {
1887 num += 2.0*p1[i]*p2[i]*_sigma1;
1888 denom1 += 2.0*p1[i]*p1[i]*_sigma1;
1889 denom2 += 2.0*p2[i]*p2[i]*_sigma1;
1892 arg = num/::sqrt(denom1*denom2);
1893 return T(2.0*(::asin(arg))/3.141592654);
1897 template <
typename T >
1903 template <
typename T >
1911 #define gpn lsst::afw::math
1913 #define INSTANTIATEGP(T) \
1914 template class gpn::KdTree < T > ; \
1915 template class gpn::GaussianProcess < T > ; \
1916 template class gpn::Covariogram < T > ; \
1917 template class gpn::SquaredExpCovariogram < T > ;\
1918 template class gpn::NeuralNetCovariogram < T > ;
static DateTime now(void)
View< boost::fusion::vector1< index::Full > > view()
Start a view definition that includes the entire first dimension.
Stores values of a function sampled on an image and allows you to interpolate the function to unsampl...
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.
int getSize() const
Return the size of a specific dimension.
void removePoint(int dex)
This will remove a point from the data set.
Vector< T, N > makeVector(T v1, T v2,..., T vN)
Variadic constructor for Vector.
The data for GaussianProcess is stored in a KD tree to facilitate nearest-neighbor searches...
a Covariogram that falls off as the negative exponent of the square of the distance between the point...
a Covariogram that recreates a neural network with one hidden layer and infinite units in that layer ...
Element * getData() const
Return a raw pointer to the first element of the array.
detail::SimpleInitializer< N > allocate(Vector< int, N > const &shape)
Create an expression that allocates uninitialized memory for an array.
#define LSST_EXCEPT(type,...)
Deep const deep() const
Return an ArrayRef view to this.
afw::table::Key< double > sigma1