Skip to content

Add support for ASDF serialisation and deserialisation #776

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
wants to merge 76 commits into
base: main
Choose a base branch
from

Conversation

Cadair
Copy link
Member

@Cadair Cadair commented Nov 6, 2024

This PR adds support for saving and loading the vast majority of ndcube objects to/from ASDF files.

Thanks to @ViciousEagle03 who did the majority of this work.


[asdf] Add some documentation
@Cadair Cadair marked this pull request as ready for review May 26, 2025 11:45
Copy link
Member

@DanRyanIrish DanRyanIrish left a comment

Choose a reason for hiding this comment

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

Initial partial review

key_value_pairs = list(zip(node["keys"], node["value"]))
aligned_axes = list(node.get("aligned_axes").values())
aligned_axes = tuple(tuple(lst) for lst in aligned_axes)
return NDCollection(key_value_pairs, meta=node.get("meta"), aligned_axes=aligned_axes)
Copy link
Member

Choose a reason for hiding this comment

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

Does this mean that every future metadata object needs its own schema to be saved and read?

Copy link
Member Author

Choose a reason for hiding this comment

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

Well yes and no, anything custom will need one.

Copy link
Member Author

Choose a reason for hiding this comment

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

This also goes for unsaveable values in meta. i.e. if you put a dask array in your meta dict it will break.


return node


Copy link
Member

Choose a reason for hiding this comment

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

Got this far.

anyOf:
- tag: "tag:stsci.edu:asdf/core/ndarray-1.*"
- type: object
- type: boolean
Copy link
Member Author

Choose a reason for hiding this comment

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

@DanRyanIrish Is having a single boolean as a mask something that's actually logical? We have some test fixtures which do this so I added this here.

@@ -184,11 +184,11 @@ def _sanitize_axis_value(self, axis, value, key):
if isinstance(axis, numbers.Integral):
axis = (axis,)
if len(axis) == 0:
return ValueError(axis_err_msg)
raise ValueError(axis_err_msg)
Copy link
Member Author

Choose a reason for hiding this comment

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

This was a fun bug to discover. I ended up with a dict with ValueErrors as the values.

Copy link
Member

Choose a reason for hiding this comment

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

In that case, this probably deserves a bugfix changelog file.

type: object
patternProperties:
".*":
oneOf:
Copy link
Member

@DanRyanIrish DanRyanIrish May 27, 2025

Choose a reason for hiding this comment

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

Strictly speaking, I think NDCollection can allow any type of item, not just NDCube and NDCubeSequence, more like dict. Items included in aligned_axes must be sliceable and have enough axes for them to include the designated aligned one. But I think other items can be any object...?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes it can, but the question is do we want it to? We are more strict in a bunch of these schemas than the code strictly requires.

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess from a personal perspective keeping it looser here would let me put dkist Dataset objects in, and maybe the same logic for some other subclasses.

Copy link
Member

Choose a reason for hiding this comment

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

Yes it can, but the question is do we want it to?

Saving aside, I don't see why we would restrict it.

We are more strict in a bunch of these schemas than the code strictly requires.

Which would mean that NDCollection can only be saved for the standard use cases, but not all possible?

Copy link
Member Author

Choose a reason for hiding this comment

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

@braingram you got thoughts on how lax or strict we should be in the schemas? ndcube is a little different from dkist for example where we are often subclassed so I'm hesitant to be too strict about things in these schemas?

Copy link
Member

@DanRyanIrish DanRyanIrish left a comment

Choose a reason for hiding this comment

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

Left a couple small comments that may require changes. Didn't look through the tests. Not an ASDF expert, but didn't see anything obvious that I didn't comment on.

Copy link

@braingram braingram left a comment

Choose a reason for hiding this comment

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

A few comments on the schemas.

"astropy>=5.3.0",
"gwcs>=0.20",
"numpy>=1.25.0",
"scipy>=1.11.1",

Choose a reason for hiding this comment

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

Should this have asdf and asdf-astropy?

Copy link
Member Author

Choose a reason for hiding this comment

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

We should probably add an asdf extra.

unit:
anyOf:
- tag: "tag:stsci.edu:asdf/unit/unit-*"
- tag: "tag:astropy.org:astropy/units/unit-1.*"
Copy link
Member Author

Choose a reason for hiding this comment

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

Suggested change
- tag: "tag:astropy.org:astropy/units/unit-1.*"
- tag: "tag:astropy.org:astropy/units/unit-*"

type: object
patternProperties:
".*":
oneOf:
Copy link
Member Author

Choose a reason for hiding this comment

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

Suggested change
oneOf:
anyOf:

items:
- type: string
- type: object
oneOf:
Copy link
Member Author

Choose a reason for hiding this comment

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

Suggested change
oneOf:
anyOf:

items:
type: array
items:
- oneOf:
Copy link
Member Author

Choose a reason for hiding this comment

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

Suggested change
- oneOf:
- anyOf:

- oneOf:
- type: number
- type: array
- oneOf:
Copy link
Member Author

Choose a reason for hiding this comment

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

Suggested change
- oneOf:
- anyOf:

".*":
oneOf:
- tag: "tag:sunpy.org:ndcube/ndcube/ndcube-*"
- tag: "tag:sunpy.org:ndcube/ndcube/ndcube_sequence-*"
Copy link
Member Author

@Cadair Cadair May 27, 2025

Choose a reason for hiding this comment

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

Suggested change
- tag: "tag:sunpy.org:ndcube/ndcube/ndcube_sequence-*"
- tag: "tag:sunpy.org:ndcube/ndcube/ndcube_sequence-*"
# Allow any other objects here as we don't want to restrict what people can save
- {}

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.

6 participants