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

avoid use of deprecated numpy.bool alias #1485

Merged
merged 1 commit into from Jul 1, 2023

Conversation

neutrinoceros
Copy link
Contributor

@neutrinoceros neutrinoceros commented Jun 24, 2023

Description

Fix a compatibility issue with numpy>=1.24 (similar to #1479)

Motivation and Context

np.bool was an alias to builtin bool. This alias was deprecated in numpy 1.20 and was removed in numpy 1.24

@welcome
Copy link

welcome bot commented Jun 24, 2023

Hi! Welcome, and thanks for opening this pull request. We ask that you follow our template for new pull requests, and soon you'll hear back about the results of our tests and continuous integration checks. Thank you for your contribution!

@neutrinoceros neutrinoceros changed the title Fix AttributeErrors ( module 'numpy' has no attribute 'bool') avoid use of deprecated numpy.bool alias Jun 24, 2023
@neutrinoceros neutrinoceros marked this pull request as ready for review June 24, 2023 09:05
@@ -155,7 +156,7 @@ def __getitem__(self, key):
return getattr(mats[int(key)], name)
elif isinstance(key, slice):
return np.array([getattr(mats[i], name) for i in range(*key.indices(size))])
elif isinstance(key, np.ndarray) and key.dtype == np.bool:
elif isinstance(key, np.ndarray) and key.dtype in _BOOLEAN_TYPES:

Choose a reason for hiding this comment

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

I believe that setting key.dtype == bool may be more efficient in this case. This comparison checks if the data type (dtype) of key is exactly bool. On the other hand, using key.dtype in (bool, np.bool_) checks if the dtype is either bool or np.bool_. This approach involves iterating over the elements of the tuple to find a match, which could potentially be slower.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right that my proposed change would be technically slower, involving 2 equality checks instead of one, but note that

  • we're talking 10s of nano-seconds here (hopefully this doesn't matter)
  • because bool is the first element in _BOOLEAN_TYPES, the second check is only performed in case key.dtype != bool
  • if performance was important here (again, I'm hoping it's not), key.dtype is bool would be the way to go (identity check is always cheaper than equality check, although here it's 20ns VS 24ns on my machine)

Choose a reason for hiding this comment

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

Considering the potential impact on simulation time when the code is called multiple times (considering large data), it becomes more important to optimize the comparison. In that case, using key.dtype == bool would be more efficient compared to key.dtype in (bool, np.bool_) due to the reduced number of operations involved. However, it's still advisable to measure the actual performance gain before making a decision, as the difference might still be minimal.

Copy link
Contributor

Choose a reason for hiding this comment

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

Does the proposed approach ensure some kind of backward compatibility? Are there configurations that may use one vs the other? Or is bool always available so that any invocation will always work if we restrict to bool? Put another way, what's the benefit to this proposed approach?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My approach is supposed to be 100% backward compatible. Additionally, it survives np.bool_ (current implementation doesn't)

@gonuke
Copy link
Contributor

gonuke commented Jul 1, 2023

Thanks @neutrinoceros

@gonuke gonuke merged commit 948bc2b into pyne:develop Jul 1, 2023
6 checks passed
@welcome
Copy link

welcome bot commented Jul 1, 2023

Hooray! Congratulations on your first merged pull request! We hope we keep seeing you around! If you haven't already, consider joining our User's Group and our Development List. 🎆

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.

None yet

3 participants