-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DM-32008: Wrap test_spoke
in C++/pybind11
#152
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// -*- LSST-C++ -*- | ||
|
||
/* | ||
* This file is part of meas_astrom. | ||
* | ||
* Developed for the LSST Data Management System. | ||
* This product includes software developed by the LSST Project | ||
* (https://www.lsst.org). | ||
* See the COPYRIGHT file at the top-level directory of this distribution | ||
* for details of code ownership. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include <ndarray.h> | ||
|
||
namespace lsst { | ||
namespace meas { | ||
namespace astrom { | ||
|
||
/** | ||
* Check the opening angle between the first spoke of our pattern for the | ||
* source object against the reference object. | ||
* | ||
* This method makes heavy use of the small angle approximation to perform | ||
* the comparison. | ||
* | ||
* @param[in] cos_theta_src Cosine of the angle between the current candidate | ||
* source spoke and the first spoke. | ||
* @param[in] sin_theta_src Sine of the angle between the current candidate | ||
* source spoke and the first spoke. | ||
* @param[in] ref_ctr 3 vector of the candidate reference center | ||
* @param[in] ref_ctr_id id lookup of the ref_ctr into the master reference | ||
* array. uint16 type is to limit the amount of memory used and is set | ||
* in the pessimistic_pattern_matcher_3d python class with reference | ||
* catalogs trimmed by the matchPessimisticB runner class. | ||
* @param[in] proj_ref_ctr_delta Plane projected first spoke in the reference | ||
* pattern using the pattern center as normal. | ||
* @param[in] proj_ref_ctr_dist_sq Squared length of the projected vector. | ||
* @param[in] ref_dist_idx_array Indices sorted by the delta distance between | ||
* the source spoke we are trying to test and the candidate reference | ||
* spokes. | ||
* @param[in] ref_id_array Array of id lookups into the master reference array | ||
* that our center id object is paired with. | ||
* @param[in] reference_array Array of three vectors representing the locations | ||
* of all reference objects. | ||
* @param[in] src_sin_tol Sine of tolerance allowed between source and | ||
* reference spoke opening angles. | ||
* @return ID of the candidate reference object successfully matched or -1 if | ||
* no match is found. | ||
*/ | ||
int check_spoke(double cos_theta_src, double sin_theta_src, ndarray::Array<double, 1, 1> const& ref_ctr, | ||
parejkoj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ndarray::Array<double, 1, 1> const& proj_ref_ctr_delta, double proj_ref_ctr_dist_sq, | ||
ndarray::Array<long int, 1, 1> const& ref_dist_idx_array, | ||
ndarray::Array<uint16_t, 1, 1> const& ref_id_array, | ||
ndarray::Array<double, 2, 1> const& reference_array, double src_sin_tol); | ||
|
||
} // namespace astrom | ||
} // namespace meas | ||
} // namespace lsst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// -*- LSST-C++ -*- | ||
|
||
/* | ||
* This file is part of meas_astrom. | ||
* | ||
* Developed for the LSST Data Management System. | ||
* This product includes software developed by the LSST Project | ||
* (https://www.lsst.org). | ||
* See the COPYRIGHT file at the top-level directory of this distribution | ||
* for details of code ownership. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "pybind11/pybind11.h" | ||
#include "pybind11/stl.h" | ||
#include "numpy/arrayobject.h" | ||
#include "ndarray/pybind11.h" | ||
|
||
#include "lsst/meas/astrom/pessimisticPatternMatcherUtils.h" | ||
|
||
namespace py = pybind11; | ||
|
||
namespace lsst { | ||
namespace meas { | ||
namespace astrom { | ||
|
||
PYBIND11_MODULE(pessimisticPatternMatcherUtils, mod) { | ||
mod.def("check_spoke", &check_spoke, | ||
"Test the opening angle between the spokes of our source pattern against the reference."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Huh, I wasn't aware that you could add a python docstring like this. We don't mention it in the dev guide, but we probably should! |
||
} | ||
|
||
} // namespace astrom | ||
} // namespace meas | ||
} // namespace lsst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// -*- LSST-C++ -*- | ||
|
||
/* | ||
* This file is part of meas_astrom. | ||
* | ||
* Developed for the LSST Data Management System. | ||
* This product includes software developed by the LSST Project | ||
* (https://www.lsst.org). | ||
* See the COPYRIGHT file at the top-level directory of this distribution | ||
* for details of code ownership. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include <cmath> | ||
#include <Eigen/Dense> | ||
#include "ndarray/eigen.h" | ||
#include "lsst/meas/astrom/pessimisticPatternMatcherUtils.h" | ||
|
||
namespace lsst { | ||
namespace meas { | ||
namespace astrom { | ||
|
||
int check_spoke(double cos_theta_src, double sin_theta_src, ndarray::Array<double, 1, 1> const& ref_ctr, | ||
ndarray::Array<double, 1, 1> const& proj_ref_ctr_delta, double proj_ref_ctr_dist_sq, | ||
ndarray::Array<long int, 1, 1> const& ref_dist_idx_array, | ||
ndarray::Array<uint16_t, 1, 1> const& ref_id_array, | ||
ndarray::Array<double, 2, 1> const& reference_array, double src_sin_tol) { | ||
// Loop over our candidate reference objects. ref_dist_idx_array is a view into ref_id_array | ||
// and is not the same length as ref_id_array. | ||
for (auto idx = ref_dist_idx_array.begin(); idx != ref_dist_idx_array.end(); idx++) { | ||
parejkoj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Compute the delta vector from the pattern center. | ||
unsigned int ref_id = ref_id_array[*idx]; | ||
ndarray::Array<double, 1, 1> ref_delta = copy(reference_array[ref_id] - ref_ctr); | ||
|
||
double ref_dot = ndarray::asEigenMatrix(ref_delta).dot(ndarray::asEigenMatrix(ref_ctr)); | ||
ndarray::Array<double, 1, 1> proj_ref_delta = copy(ref_delta - ref_dot * ref_ctr); | ||
// Compute the cos between our "center" reference vector and the | ||
// current reference candidate. | ||
double proj_delta_dist_sq = | ||
ndarray::asEigenMatrix(proj_ref_delta).dot(ndarray::asEigenMatrix(proj_ref_delta)); | ||
double geom_dist_ref = sqrt(proj_ref_ctr_dist_sq * proj_delta_dist_sq); | ||
double cos_theta_ref = | ||
ndarray::asEigenMatrix(proj_ref_delta).dot(ndarray::asEigenMatrix(proj_ref_ctr_delta)) / | ||
geom_dist_ref; | ||
|
||
// Make sure we can safely make the comparison in case | ||
// our "center" and candidate vectors are mostly aligned. | ||
double cos_sq_comparison; | ||
if (cos_theta_ref * cos_theta_ref < (1 - src_sin_tol * src_sin_tol)) { | ||
cos_sq_comparison = (cos_theta_src - cos_theta_ref) * (cos_theta_src - cos_theta_ref) / | ||
(1 - cos_theta_ref * cos_theta_ref); | ||
} else { | ||
cos_sq_comparison = (cos_theta_src - cos_theta_ref) * (cos_theta_src - cos_theta_ref) / | ||
(src_sin_tol * src_sin_tol); | ||
} | ||
// Test the difference of the cosine of the reference angle against | ||
// the source angle. Assumes that the delta between the two is | ||
// small. | ||
if (cos_sq_comparison > src_sin_tol * src_sin_tol) { | ||
continue; | ||
} | ||
// The cosine tests the magnitude of the angle but not | ||
// its direction. To do that we need to know the sine as well. | ||
// This cross product calculation does that. | ||
Eigen::Vector3d cross_ref = ndarray::asEigenMatrix(proj_ref_delta) | ||
.head<3>() | ||
.cross(ndarray::asEigenMatrix(proj_ref_ctr_delta).head<3>()) / | ||
geom_dist_ref; | ||
double sin_theta_ref = cross_ref.dot(ndarray::asEigenMatrix(ref_ctr)); | ||
// Check the value of the cos again to make sure that it is not | ||
// near zero. | ||
double sin_comparison; | ||
if (abs(cos_theta_src) < src_sin_tol) { | ||
sin_comparison = (sin_theta_src - sin_theta_ref) / src_sin_tol; | ||
} else { | ||
sin_comparison = (sin_theta_src - sin_theta_ref) / cos_theta_ref; | ||
} | ||
|
||
// Return the correct id of the candidate we found. | ||
if (abs(sin_comparison) < src_sin_tol) { | ||
return ref_id; | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
} // namespace astrom | ||
} // namespace meas | ||
} // namespace lsst |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a note here about why this is uint16 (as you said, the higher level code trims the arrays due to memory limits): it's generally expected that indexes are size_t.