Skip to content
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 1 commit into from Nov 17, 2021
Merged

Conversation

morriscb
Copy link
Contributor

No description provided.

@morriscb morriscb changed the title DM-32008: Wrap test_spoke in C++/pybind1 DM-32008: Wrap test_spoke in C++/pybind11 Oct 22, 2021
Copy link
Contributor

@parejkoj parejkoj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please pull testSpoke out into a separate src/X.cc file and switch to using ndarray instead of py::array (which I don't believe we use anywhere). I'll look at it again after that. The array access should be simpler after this.


namespace {

int testSpoke(double cos_theta_src, double sin_theta_src, py::array_t<double> ref_ctr, int ref_ctr_id,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this function should be implemented in a file in src/, with a header in include/ (which then gets the docstrings that this definitely needs!); the only thing in this pybind file should be the PYBIND11_MODULE below.

int testSpoke(double cos_theta_src, double sin_theta_src, py::array_t<double> ref_ctr, int ref_ctr_id,
py::array_t<double> proj_ref_ctr_delta, double proj_ref_ctr_dist_sq,
py::array_t<unsigned int> ref_dist_idx_array, py::array_t<unsigned int> ref_id_array,
py::array_t<double> reference_array, double src_sin_tol) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These py::array_t should be ndarray::Array<type, 1> when you move this to src/. Then I think you won't need the unchecked iterators below, you can just do ref_ctr[0], etc.

python/lsst/meas/astrom/pessimisticPatternMatcherUtils.cc Outdated Show resolved Hide resolved
@morriscb morriscb force-pushed the tickets/DM-32008 branch 2 times, most recently from c91751b to 7f637bc Compare November 5, 2021 00:22
@morriscb
Copy link
Contributor Author

morriscb commented Nov 5, 2021

Finished things up mostly except for one compiler warning.

@parejkoj
Copy link
Contributor

parejkoj commented Nov 5, 2021

Please squash your commits.

* no match is found.
*/
int check_spoke(double cos_theta_src, double sin_theta_src, ndarray::Array<double, 1, 1> const& ref_ctr,
uint16_t ref_ctr_id, ndarray::Array<double, 1, 1> const& proj_ref_ctr_delta,
Copy link
Contributor

@parejkoj parejkoj Nov 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is ref_ctr_id really a uint16? That means the refcat can only have 65k entries. I'd expect this, ref_id_array, and ref_dist_idx_array to all be std::size_t, since they are array indexes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is. I'll change the other two types and see if it compiles.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did these not work as size_t? The uint16 makes me uncomfortable.


PYBIND11_MODULE(pessimisticPatternMatcherUtils, mod) {
mod.def("check_spoke", &check_spoke,
"Test the opening angle between the spokes of our source pattern against the reference.");
Copy link
Contributor

Choose a reason for hiding this comment

The 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!

Copy link
Contributor

@parejkoj parejkoj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A number of array cleanups: Eigen and ndarray can do a bunch of things to clean this up.

Just to check: you ran clang-format on this?

src/pessimisticPatternMatcherUtils.cc Show resolved Hide resolved
for (auto idx = ref_dist_idx_array.begin(); idx != ref_dist_idx_array.end(); idx++) {
/// Compute the delta vector from the pattern center.
unsigned int ref_id = ref_id_array[*idx];
double ref_delta[3];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this an ndarray, so you can directly assign it:

Array<double, 1, 2> ref_delta = copy(reference_array[ref_id] - ref_ctr);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do. However I will point out that the last number of is the number of contiguous dimensions. So you can't have 1, 2. This would be 1, 1.

src/pessimisticPatternMatcherUtils.cc Outdated Show resolved Hide resolved
src/pessimisticPatternMatcherUtils.cc Outdated Show resolved Hide resolved
(proj_ref_delta[0] * proj_ref_ctr_delta[1] - proj_ref_delta[1] * proj_ref_ctr_delta[0]) /
geom_dist_ref;
double sin_theta_ref =
cross_ref[0] * ref_ctr[0] + cross_ref[1] * ref_ctr[1] + cross_ref[2] * ref_ctr[2];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And again, with Eign, something like:

double sin_theta_ref = cross_ref.asEigenMatrix().dot(ref_ctr.asEigenMatrix());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll give these a shot on one or two of them and see if I can get it to compile/run. If I can't though, I'm not too uncomfortable with just leaving it for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

src/pessimisticPatternMatcherUtils.cc Outdated Show resolved Hide resolved
@morriscb
Copy link
Contributor Author

morriscb commented Nov 5, 2021

Clang format is running every time I save the file, yes.

@morriscb morriscb force-pushed the tickets/DM-32008 branch 3 times, most recently from c2f5f27 to 1dc498a Compare November 17, 2021 18:01
namespace astrom {

int check_spoke(double cos_theta_src, double sin_theta_src, ndarray::Array<double, 1, 1> const& ref_ctr,
uint16_t ref_ctr_id, ndarray::Array<double, 1, 1> const& proj_ref_ctr_delta,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hang on, I don't see ref_ctr_id used in the C++ code below at all?

* 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.
Copy link
Contributor

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.

Copy link
Contributor

@parejkoj parejkoj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove ref_ctr_id, add a note about why ref_id_array is uint16, and you're good to go.

@morriscb
Copy link
Contributor Author

Done! Take a look at the comment for yourself if you want.

Add pybind layer.

Add pessimistic utils to SConscript and import.

Add testSpokeCC to pessimitic matcher.

Remove call to old _test_spoke method.

Create header and source file.

Move code to source file.

Change to using ndarray.

Add doxygen.

Change function name.

Change input arrays to const&

Switch to eigen operations for cross and dot products.

Update comment around for loop.

Change to double slash.

Change cross product to eigen.

Remove unused ref_ctr_id variable.

Add comment.

Clang fortmating.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants