Skip to content

Commit

Permalink
convert numpy scalars to python types before yaml encoding
Browse files Browse the repository at this point in the history
nep51 changes numpy scalars modifying the repr.

this change conflicts with represent_float/int (which uses
repr) and with numpy 2.0 (which implements nep51) scalars
are being encoded as, for example, 'np.float64(3.14)'
instead of '3.14'. To work around this change, convert the
values to builtin python types before passing them to
pyyaml for encoding.
  • Loading branch information
braingram committed Jul 26, 2023
1 parent 1e9a049 commit ccc1d31
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 ccc1d31

Please sign in to comment.