-
Notifications
You must be signed in to change notification settings - Fork 139
Register Boost.Python converters for NumPy 2.0 scalar types #1131
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
Open
dwpaley
wants to merge
4
commits into
master
Choose a base branch
from
fix/numpy2-scalar-converters
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+248
−0
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
scitbx/array_family/boost_python/tst_numpy_scalar_conversions.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| from __future__ import absolute_import, division, print_function | ||
| import sys | ||
|
|
||
| def run(args): | ||
| assert len(args) == 0 | ||
| try: | ||
| import numpy as np | ||
| except ImportError: | ||
| print("numpy not available, skipping") | ||
| print("OK") | ||
| return | ||
| from scitbx.array_family import flex | ||
|
|
||
| exercise_original_reproducer(np, flex) | ||
| exercise_element_setitem(np, flex) | ||
| exercise_element_iadd(np, flex) | ||
| exercise_array_iadd(np, flex) | ||
| exercise_construction_from_numpy_scalars(np, flex) | ||
| exercise_value_preservation(np, flex) | ||
| print("OK") | ||
|
dwpaley marked this conversation as resolved.
|
||
|
|
||
| def exercise_original_reproducer(np, flex): | ||
| """Reproducer from https://github.com/cctbx/cctbx_project/issues/1084""" | ||
| arr = flex.double(10) | ||
| arr[0] += np.array([1,2,3], dtype=np.float32)[0] | ||
| assert arr[0] == 1.0 | ||
|
|
||
| def exercise_element_setitem(np, flex): | ||
| """Test arr[i] = numpy_scalar for various type combinations.""" | ||
| # float scalars -> flex.double | ||
| a = flex.double(1) | ||
| for val in [np.float32(3.5), np.float64(3.5)]: | ||
| a[0] = val | ||
| assert a[0] == 3.5, (a[0], type(val)) | ||
| # integer scalars -> flex.double (implicit widening) | ||
| for val in [np.int32(7), np.int64(7)]: | ||
| a[0] = val | ||
| assert a[0] == 7.0, (a[0], type(val)) | ||
| # float scalars -> flex.float | ||
| b = flex.float(1) | ||
| for val in [np.float32(2.5), np.float64(2.5)]: | ||
| b[0] = val | ||
| assert abs(b[0] - 2.5) < 1e-6, (b[0], type(val)) | ||
| # integer scalars -> flex.int | ||
| c = flex.int(1) | ||
| for val in [np.int32(42), np.int64(42)]: | ||
| c[0] = val | ||
| assert c[0] == 42, (c[0], type(val)) | ||
|
|
||
| def exercise_element_iadd(np, flex): | ||
| """Test arr[i] += numpy_scalar for various type combinations.""" | ||
| a = flex.double([10.0]) | ||
| a[0] += np.float32(2.5) | ||
| assert a[0] == 12.5 | ||
| a[0] += np.float64(1.0) | ||
| assert a[0] == 13.5 | ||
| a[0] += np.int32(1) | ||
| assert a[0] == 14.5 | ||
| a[0] += np.int64(1) | ||
| assert a[0] == 15.5 | ||
|
|
||
| b = flex.int([10]) | ||
| b[0] += np.int32(5) | ||
| assert b[0] == 15 | ||
| b[0] += np.int64(3) | ||
| assert b[0] == 18 | ||
|
|
||
| def exercise_array_iadd(np, flex): | ||
| """Test arr += numpy_scalar (whole-array operations).""" | ||
| a = flex.double([1.0, 2.0, 3.0]) | ||
| a += np.float32(10.0) | ||
| assert list(a) == [11.0, 12.0, 13.0] | ||
| a += np.float64(1.0) | ||
| assert list(a) == [12.0, 13.0, 14.0] | ||
| a += np.int32(1) | ||
| assert list(a) == [13.0, 14.0, 15.0] | ||
|
|
||
| b = flex.int([1, 2, 3]) | ||
| b += np.int32(10) | ||
| assert list(b) == [11, 12, 13] | ||
| b += np.int64(1) | ||
| assert list(b) == [12, 13, 14] | ||
|
|
||
| def exercise_construction_from_numpy_scalars(np, flex): | ||
| """Test constructing flex arrays from lists containing numpy scalars.""" | ||
| a = flex.double([np.float32(1.0), np.float64(2.0), np.float32(3.0)]) | ||
| assert list(a) == [1.0, 2.0, 3.0] | ||
| b = flex.int([np.int32(1), np.int32(2), np.int32(3)]) | ||
| assert list(b) == [1, 2, 3] | ||
|
|
||
| def exercise_value_preservation(np, flex): | ||
| """Test that values are preserved accurately through conversion.""" | ||
| # float32 has ~7 decimal digits of precision | ||
| a = flex.double(1) | ||
| a[0] = np.float32(1.23456789) | ||
| # float32 truncates, so check against float32 precision | ||
| assert abs(a[0] - float(np.float32(1.23456789))) < 1e-10 | ||
|
|
||
| # float64 should be exact for representable values | ||
| a[0] = np.float64(1.234567890123456) | ||
| assert a[0] == 1.234567890123456 | ||
|
|
||
| # Large integers | ||
| b = flex.int(1) | ||
| b[0] = np.int32(2147483647) # INT32_MAX | ||
| assert b[0] == 2147483647 | ||
|
|
||
| if __name__ == "__main__": | ||
| run(args=sys.argv[1:]) | ||
|
dwpaley marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.