Skip to content

view for numpy arrays #987

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 42 commits into from
Aug 26, 2021
Merged

view for numpy arrays #987

merged 42 commits into from
Aug 26, 2021

Conversation

ncullen93
Copy link
Contributor

@ncullen93 ncullen93 commented Aug 6, 2017

this function is useful when you have numpy array buffers that need to be reinterpreted as different memory types.

For example, I often get a numpy array buffer from an ITK image that is e.g. size 256x256x4 as uint8 and it needs to be "viewed" as 256x256 float... it'd be nice to do this directly with pybind11.

From numpy:

a.view(some_dtype) or a.view(dtype=some_dtype) constructs a view of the array’s memory with a different data-type. This can cause a reinterpretation of the bytes of memory.

Not sure if you like the std:string arg input or would rather have py::dtype or something..

Suggested changelog entry:

* Added ``view`` to view arrays with a different datatype.

@Skylion007 Skylion007 requested review from rwgk and henryiii July 28, 2021 18:48
Skylion007 and others added 6 commits August 26, 2021 11:34
* Comment: matching style in file (///), responsibility sentence, consistent punctuation.
* Replacing `unsigned char` with `uint8_t` for max consistency.
* Removing `1` from `array_view1` because there is only one.
@rwgk
Copy link
Collaborator

rwgk commented Aug 26, 2021

I also ran a quick manual leak check to ensure that the steal and release work as expected.

It was simply temporarily adding while True: in the two new test functions and looking at the top output.

Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

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

@henryiii, @Skylion007: Looking for a 2nd set of eyes / approval.

array view(const std::string& dtype) {
auto& api = detail::npy_api::get();
auto new_view = reinterpret_steal<array>(api.PyArray_View_(
m_ptr, dtype::from_args(pybind11::str(dtype)).release().ptr(), NULL));
if (!new_view) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

personally prefer leaving braces in,

Copy link
Collaborator

Choose a reason for hiding this comment

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

Me too, but I think our clang-format config will take them out, that's why I removed them. Let me double check ...

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry it turns out I was wrong (I'm still really confused why I got to believe so strongly that clang-format takes them out).

They are back now. While I was at it, I applied clang-format-diff, except to line 202 in numpy.h, to not make it outstanding there.

(I really hope we'll get to finish establishing pre-commit clang-format sometime soon...)

@rwgk
Copy link
Collaborator

rwgk commented Aug 26, 2021

All green, merging! Thanks again @ncullen93 and @Skylion007!

@rwgk rwgk merged commit 503ff2a into pybind:master Aug 26, 2021
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Aug 26, 2021
@henryiii henryiii removed the needs changelog Possibly needs a changelog entry label Sep 16, 2021
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.

4 participants