Skip to content

Commit

Permalink
Merge pull request #1605 from braingram/nep51
Browse files Browse the repository at this point in the history
convert numpy scalars to python types before yaml encoding
  • Loading branch information
braingram authored Jul 27, 2023
2 parents 8d1770c + 5cce445 commit b43b2c5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ The ASDF Standard is at v1.6.0
- Fix issue opening files that don't support ``fileno`` [#1557]
- Allow Converters to defer conversion to other Converters
by returning ``None`` in ``Converter.select_tag`` [#1561]
- Convert numpy scalars to python types during yaml encoding
to handle NEP51 changes for numpy 2.0 [#1605]

2.15.0 (2023-03-28)
-------------------
Expand Down
8 changes: 7 additions & 1 deletion asdf/_tests/test_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,10 @@ def test_numpy_scalar(numpy_value, expected_value):
yamlutil.dump_tree(tree, buffer, ctx)
buffer.seek(0)

assert yamlutil.load_tree(buffer)["value"] == expected_value
loaded_value = yamlutil.load_tree(buffer)["value"]
if isinstance(numpy_value, np.floating):
abs_diff = abs(expected_value - loaded_value)
eps = np.finfo(numpy_value.dtype).eps
assert abs_diff < eps, abs_diff
else:
assert loaded_value == expected_value
5 changes: 3 additions & 2 deletions asdf/yamlutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,12 @@ def represent_ordereddict(dumper, data):
# ----------------------------------------------------------------------
# Handle numpy scalars


for scalar_type in util.iter_subclasses(np.floating):
AsdfDumper.add_representer(scalar_type, AsdfDumper.represent_float)
AsdfDumper.add_representer(scalar_type, lambda dumper, data: dumper.represent_float(float(data)))

for scalar_type in util.iter_subclasses(np.integer):
AsdfDumper.add_representer(scalar_type, AsdfDumper.represent_int)
AsdfDumper.add_representer(scalar_type, lambda dumper, data: dumper.represent_int(int(data)))


def represent_numpy_str(dumper, data):
Expand Down
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ git+https://github.com/asdf-format/asdf-unit-schemas.git
git+https://github.com/asdf-format/asdf-wcs-schemas
git+https://github.com/astropy/astropy
git+https://github.com/spacetelescope/gwcs
git+https://github.com/yaml/pyyaml.git
#git+https://github.com/yaml/pyyaml.git
# jsonschema 4.18 contains incompatible changes: https://github.com/asdf-format/asdf/issues/1485
#git+https://github.com/python-jsonschema/jsonschema

numpy>=0.0.dev0
scipy>=0.0.dev0

0 comments on commit b43b2c5

Please sign in to comment.