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

Convert numpy scalars to native python objects before sending to yaml dump #101

Merged
merged 4 commits into from
Apr 24, 2023

Conversation

nvaytet
Copy link
Contributor

@nvaytet nvaytet commented Apr 13, 2023

This came up when trying to save values from Scipp variables to an Orso file.
In recent versions of Scipp, var.value() now returns numpy types, e.g. np.float64 instead ofpython float.
We use the item() method to convert them to a python type before sending to yaml which cannot handle the numpy scalars.

@arm61 arm61 requested review from andyfaff and aglavic April 18, 2023 15:09
@@ -431,6 +431,9 @@ def represent_data(self, data):
elif isinstance(data, datetime.datetime):
value = data.isoformat("T")
return super().represent_scalar("tag:yaml.org,2002:timestamp", value)
elif (not np.shape(data)) and hasattr(data, "item"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about elif (np.isscalar(data) and hasattr(data, "item"):?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@andyfaff andyfaff merged commit 4623a87 into reflectivity:main Apr 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants