-
Notifications
You must be signed in to change notification settings - Fork 81
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
Convert floats representing integers to int in sample set serialization #1354
Merged
arcondello
merged 2 commits into
dwavesystems:main
from
arcondello:feature/sampleset-integer-serialization
Aug 17, 2023
Merged
Changes from 1 commit
Commits
Show all changes
2 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 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 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 |
---|---|---|
|
@@ -13,11 +13,47 @@ | |
# limitations under the License. | ||
|
||
import collections.abc as abc | ||
|
||
from numbers import Integral, Number | ||
from typing import List, Union | ||
|
||
import numpy as np | ||
|
||
|
||
__all__ = ["serialize_ndarray", "deserialize_ndarray", | ||
"serialize_ndarrays", "deserialize_ndarrays", | ||
"pack_samples", "unpack_samples", | ||
] | ||
|
||
|
||
def _replace_float_with_int(arr: Union[List[float], List[List]]): | ||
"""Replace floats representing integers with ints in a list representing an array. | ||
|
||
Take a list of floats, as produced by :meth:`numpy.ndarray.tolist` from an array | ||
of floating types, and convert any ``float`` representing an integer value into | ||
``int``. | ||
|
||
This function assumes some uniformity of the list structure. For instance giving it | ||
a list like ``[0.0, 0]`` or ``[0.0, [0.0]`` will cause it to fail. | ||
|
||
Acts on the list(s) in-place. | ||
""" | ||
if not len(arr): | ||
# nothing to do when the list is empty | ||
pass | ||
|
||
elif isinstance(arr[0], List): | ||
for subarr in arr: | ||
_replace_float_with_int(subarr) | ||
|
||
elif hasattr(arr[0], "is_integer"): | ||
arr[:] = (int(a) if a.is_integer() else a for a in arr) | ||
|
||
else: | ||
raise ValueError("expected a (possibly nested) list of floats, " | ||
f"recieved a (possible nested) list of {type(arr[0])}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typos: received, possibly |
||
|
||
|
||
def serialize_ndarray(arr, use_bytes=False, bytes_type=bytes): | ||
"""Serialize a NumPy array. | ||
|
||
|
@@ -43,6 +79,10 @@ def serialize_ndarray(arr, use_bytes=False, bytes_type=bytes): | |
data = bytes_type(arr.tobytes(order='C')) | ||
else: | ||
data = arr.tolist() | ||
|
||
if np.issubdtype(arr.dtype, np.floating): | ||
_replace_float_with_int(data) | ||
|
||
return dict(type='array', | ||
data=data, | ||
data_type=arr.dtype.name, | ||
|
12 changes: 12 additions & 0 deletions
12
releasenotes/notes/sampleset-serialization-replace-float-with-int-5a3133c66480b8f4.yaml
This file contains 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,12 @@ | ||
--- | ||
features: | ||
- | | ||
Implement ``SampleSet`` serialization schema version 3.2.0. | ||
|
||
Version 3.2.0 replaces ``float`` values that represent integers with ``int`` | ||
in the ``"data"`` field of any arrays returned by ``SampleSet.to_serializable()``. | ||
In some pathological cases this can result in a much smaller representation | ||
when the data dictionaries are json-serialized by avoiding the redundant | ||
``.0`` appended to every value. | ||
|
||
This is a backwards-compatible change. |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: missing ]