8000 Python more numpy by wrongbad · Pull Request #656 · elalish/manifold · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Python more numpy #656

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

Merged
merged 9 commits into from
Dec 19, 2023
Merged

Python more numpy #656

merged 9 commits into from
Dec 19, 2023

Conversation

wrongbad
Copy link
Contributor

Note: this branch is on top of WarpBatch commit - that will disappear when that other PR merges.

Changed hull_points to accept numpy point arrays. Also removed several redundant numpy shape checks - these shape constraints were already enforced by the shape template parameters.

sdf / level_set could benefit from numpy batching but requires deeper refactoring (#598)

set_properties also could benefit, and I'd like to look into it more here.

That's all that I see. Let me know if I missed anything.

While I was auditing the API, I noticed something else needing cleanup. Many functions need a single vector/point, but the convention is all over the map. Some take 3 floats, some take a tuple, some take a glm::vec3, some take a numpy(3), and several have overloads to support some random subset of those. We should probably converge on 1 way. My preference would be tuple, but I'm curious to hear other opinions.

Copy link
codecov bot commented Dec 14, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (bc0bded) 91.66% compared to head (311cbc7) 91.64%.

❗ Current head 311cbc7 differs from pull request most recent head a4b0e74. Consider uploading reports for the commit a4b0e74 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #656      +/-   ##
==========================================
- Coverage   91.66%   91.64%   -0.03%     
==========================================
  Files          37       36       -1     
  Lines        4737     4737              
==========================================
- Hits         4342     4341       -1     
- Misses        395      396       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@pca006132
Copy link
Collaborator
pca006132 commented Dec 15, 2023

There is a caster for glm::vec3 that can accept anything that is a sequence with 3 elements, e.g. list, tuple and numpy arrays.

Not sure if arguments accepting numpy arrays can accept normal python list.

@pca006132
Copy link
Collaborator

I think there are also some other tiny fixes that can be added into this PR.

Missing \n in line 371:

":param gaussianIdx: The property channel index in which to store "
"the Gaussian curvature. An index < 0 will be ignored (stores "
"nothing). The property set will be automatically expanded to "
"include the channel index specified."
":param meanIdx: The property channel index in which to store the "
"mean curvature. An index < 0 will be ignored (stores nothing). The "
"property set will be automatically expanded to include the channel "
"index specified.")

This should be an int instead of a double:

nb::arg("circular_segments") = 0.0,

@pca006132 pca006132 mentioned this pull request Dec 15, 2023
5 tasks
@wrongbad
Copy link
Contributor Author

Not sure if arguments accepting numpy arrays can accept normal python list.

A quick test suggests the answer is no. I guess the auto casting approach has the best upside. I'll tinker with it some more.

@wrongbad wrongbad marked this pull request as draft December 15, 2023 19:56
@wrongbad wrongbad force-pushed the python_more_numpy branch 3 times, most recently from 7d97aea to d679750 Compare December 15, 2023 23:25
@wrongbad
Copy link
Contributor Author
wrongbad commented Dec 15, 2023

I removed almost all of the lambda functions in the bindings. This is enabled by improved auto casting of glm::X, std::vectorglm::X and VecViewglm::X.

A few remain for overload-resolution, dealing with stray types like Rect or Smoothness, or doing something non-trivial. Also named a few unnamed args.

@wrongbad
Copy link
Contributor Author

I also removed some overloads like cube(float, float, float) when cube(vec3) exists. I think right now a primary obstacle for maintainability and cleanliness is the sheer quantity of code in the manifold3d.cpp, so anything that reduces that code footprint is good.

@pca006132
Copy link
Collaborator

Thanks for the cleanup. I think much of the code comes from docstrings, it would be nice if we can find a way to unify the documentation for the main C++ library and the 3 different bindings, but I guess this is a hard problem (e.g. name inconsistencies, specifics for certain binding).

@elalish
Copy link
Owner
elalish commented Dec 16, 2023

This looks awesome, thanks! I always appreciate cleanups that will ease maintenance going forward. I'll review more closely after the other PR is merged and this comes out of draft.

Also add CrossSection::Bounds()
@wrongbad wrongbad marked this pull request as ready for review December 17, 2023 15:55
@pca006132
Copy link
Collaborator

I think you probably need to push a dummy commit to trigger the CI build. We should fix this CI problem later...

Copy link
Collaborator
@pca006132 pca006132 left a comment

Choose a reason for hiding this comment

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

some nitpicks.

btw, not sure if we want Manifold const &self, I think we typically write const Manifold &self, and order does not matter for references (T& is like T const * anyway).

"of the same shape and type in return. The input array can be "
"modified and returned if desired. "
"\n\n"
":param f: A function that modifies multiple vertex positions.")
Copy link
Collaborator

Choose a reason for hiding this comment

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

thinking about it, we may want documentation about the type signature of the function. same for warp above and set_properties below.

@@ -568,28 +530,24 @@ NB_MODULE(manifold3d, m) {
"as empty. Likewise, empty meshes may still show NoError, for "
"instance if they are small enough relative to their precision to "
"be collapsed to nothing.")
.def_prop_ro(
.def(
Copy link
Collaborator

Choose a reason for hiding this comment

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

we should probably state that this is a breaking change, i.e. property vs method.

@wrongbad
Copy link
Contributor Author

What do you think about bounding_box -> bounding_box() and get_surface_area() -> surface_area() and get_volume() -> volume()?

@pca006132
Copy link
Collaborator

I think every methods on Manifold may require evaluating the underlying CSG graph, so they can be really expensive and should probably be methods instead of attributes. Removing the get_ prefix seems fine to me.

Copy link
Owner
@elalish elalish left a comment

Choose a reason for hiding this comment

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

That's a lot of great clean-up. LGTM; I'll leave it to @pca006132 for final approval since I'm no Python master.

c = c.warp_batch(lambda ps: ps * [1, 0.5] + [1, 0])


def all_manifold():
Copy link
Owner

Choose a reason for hiding this comment

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

Thanks - this big basic test file is a great idea. Nice as an API index too.

@elalish
Copy link
Owner
elalish commented Dec 18, 2023

Looks like a pretty simple error the python test caught, but are we only running Python tests on some of our builds? Is that intentional @pca006132?

@pca006132
Copy link
Collaborator

Forgot if this is intentional or not, we can just enable it for every build. We can also enable all tests even for draft PR, so we will not get skipped tests when we change the state from draft to ready for review.

I think I will wait for @wrongbad to signal this ready for review, not sure if he is done with it now.

@wrongbad
Copy link
Contributor Author

At this point I'm just hoping to get all the tests passing, and resolve any review concerns. I'll look at generating doc-strings in a follow-up.

@pca006132
Copy link
Collaborator

I will have a look at it tmr. We can leave the documentation generation thing later. I feel that it should not be too complicated, but may require parsing the documentation to get a list of parameters for substitution. Can probably be done together with the python stub.

Copy link
Collaborator
@pca006132 pca006132 left a comment

Choose a reason for hiding this comment

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

I think I still see some inconsistencies in parameter name for the docstring, but I guess we can pass this for now and embrace automation :)

}
}
} else {
int num_vec = PyObject_Size(src.ptr()); // negative on failure
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is surprising. Just curious, any reference on this?

Copy link
Contributor Author
@wrongbad wrongbad Dec 19, 2023

Choose a reason for hiding this comment

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

So the caster function this is inside is noexcept and if you call nb::len on a float it throws, and terminates the program. So I looked into source for nb::len and found PyObject_Size which gives a softer fallback mode when user calls a func with the wrong type, we return false and nanobind generates a useful error.

@pca006132
Copy link
Collaborator

So I guess this can be merged?

@pca006132 pca006132 merged commit 7d564cf into elalish:master Dec 19, 2023
@elalish elalish mentioned this pull request Jan 7, 2024
cartesian-theatrics pushed a commit to cartesian-theatrics/manifold that referenced this pull request Mar 11, 2024
* [python] numpy updates and cleanup

Also add CrossSection::Bounds()

* cleanup - const and reference args in py bind

* fix unnamed args

* all_apis.py more coverage

* fixup - more param renaming, docstring fixes

* fixup - run black

* fixup - move const to left side

* fixup extrude.py get_volume

* fix python examples for api update
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0