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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Next Version
* remove unusable C-linkage with std::vector (#1468)
* fixed compatibility issue with Python 3.10 in endf.py (#1472)
* avoid use of deprecated numpy.int alias (#1479)
* avoid use of deprecated numpy.bool alias (#1485)

v0.7.7
======
Expand Down
23 changes: 12 additions & 11 deletions pyne/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
),
}

_INTEGRAL_TYPES = (int, np.integer, np.bool_)
_INTEGRAL_TYPES = (int, np.integer, bool, np.bool_)
_BOOLEAN_TYPES = (bool, np.bool_)
_SEQUENCE_TYPES = (Sequence, np.ndarray)


Expand Down Expand Up @@ -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)

if len(key) != size:
raise KeyError("boolean mask must match the length of the mesh.")
return np.array([getattr(mats[i], name) for i, b in enumerate(key) if b])
Expand All @@ -182,7 +183,7 @@ def __setitem__(self, key, value):
else:
for i in idx:
setattr(mats[i], name, value)
elif isinstance(key, np.ndarray) and key.dtype == np.bool:
elif isinstance(key, np.ndarray) and key.dtype in _BOOLEAN_TYPES:
if len(key) != size:
raise KeyError("boolean mask must match " "the length of the mesh.")
idx = np.where(key)[0]
Expand Down Expand Up @@ -234,7 +235,7 @@ def __getitem__(self, key):
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:
if len(key) != size:
raise KeyError("boolean mask must match the " "length of the mesh.")
return np.array([getattr(mats[i], name)() for i, b in enumerate(key) if b])
Expand Down Expand Up @@ -278,7 +279,7 @@ def __getitem__(self, key):
return mats[key].metadata[name]
elif isinstance(key, slice):
return [mats[i].metadata[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:
if len(key) != size:
raise KeyError("boolean mask must match the length " "of the mesh.")
return [mats[i].metadata[name] for i, b in enumerate(key) if b]
Expand All @@ -305,7 +306,7 @@ def __setitem__(self, key, value):
else:
for i in idx:
mats[i].metadata[name] = value
elif isinstance(key, np.ndarray) and key.dtype == np.bool:
elif isinstance(key, np.ndarray) and key.dtype in _BOOLEAN_TYPES:
if len(key) != size:
raise KeyError("boolean mask must match the length " "of the mesh.")
idx = np.where(key)[0]
Expand Down Expand Up @@ -338,7 +339,7 @@ def __delitem__(self, key):
elif isinstance(key, slice):
for i in range(*key.indices(size)):
del mats[i].metadata[name]
elif isinstance(key, np.ndarray) and key.dtype == np.bool:
elif isinstance(key, np.ndarray) and key.dtype in _BOOLEAN_TYPES:
if len(key) != size:
raise KeyError("boolean mask must match the length " "of the mesh.")
for i, b in enumerate(key):
Expand Down Expand Up @@ -495,7 +496,7 @@ def __getitem__(self, key):
ents = list(miter)[key]
data = self.mesh.mesh.tag_get_data(self.tag, ents, flat=flat)
return data
elif isinstance(key, np.ndarray) and key.dtype == np.bool:
elif isinstance(key, np.ndarray) and key.dtype in _BOOLEAN_TYPES:
if len(key) != size:
raise KeyError("boolean mask must match the length " "of the mesh.")
return self.mesh.mesh.tag_get_data(
Expand Down Expand Up @@ -542,7 +543,7 @@ def __setitem__(self, key, value):
v.shape = (len(key),)
v[...] = value
self.mesh.mesh.tag_set_data(mtag, key, v)
elif isinstance(key, np.ndarray) and key.dtype == np.bool:
elif isinstance(key, np.ndarray) and key.dtype in _BOOLEAN_TYPES:
if len(key) != msize:
raise KeyError("boolean mask must match the length " "of the mesh.")
key = [ve for b, ve in zip(key, miter) if b]
Expand Down Expand Up @@ -584,7 +585,7 @@ def __delitem__(self, key):
self.mesh.mesh.tag_delete_data(mtag, i_ve[1])
elif isinstance(key, slice):
self.mesh.mesh.tag_delete_data(mtag, list(miter)[key])
elif isinstance(key, np.ndarray) and key.dtype == np.bool:
elif isinstance(key, np.ndarray) and key.dtype in _BOOLEAN_TYPES:
if len(key) != size:
raise KeyError("boolean mask must match the " "length of the mesh.")
self.mesh.mesh.tag_delete_data(mtag, [ve for b, ve in zip(key, miter) if b])
Expand Down Expand Up @@ -754,7 +755,7 @@ def __getitem__(self, key):
return f(m, key)
elif isinstance(key, slice):
return [f(m, i) 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:
if len(key) != size:
raise KeyError("boolean mask must match the length " "of the mesh.")
return [f(m, i) for i, b in enumerate(key) if b]
Expand Down