Skip to content

Commit 7e75454

Browse files
Add serialization logic for the NDCubeSequence and NDCollection
1 parent e8268fc commit 7e75454

File tree

8 files changed

+191
-1
lines changed

8 files changed

+191
-1
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from asdf.extension import Converter
2+
3+
4+
class NDCollectionConverter(Converter):
5+
tags = ["tag:sunpy.org:ndcube/ndcube/ndcollection-*"]
6+
types = ["ndcube.ndcollection.NDCollection"]
7+
8+
def from_yaml_tree(self, node, tag, ctx):
9+
from ndcube.ndcollection import NDCollection
10+
11+
key_value_pairs = list(zip(node["keys"], node["value"]))
12+
aligned_axes = list(node.get("aligned_axes").values())
13+
aligned_axes = tuple(tuple(lst) for lst in aligned_axes)
14+
ndcollection = NDCollection(key_value_pairs,
15+
meta=node.get("meta"),
16+
aligned_axes = aligned_axes)
17+
return ndcollection
18+
19+
20+
def to_yaml_tree(self, ndcollection, tag, ctx):
21+
22+
node = {}
23+
node["keys"] = tuple(ndcollection.keys())
24+
node["value"] = tuple(ndcollection.values())
25+
if ndcollection.meta is not None:
26+
node["meta"] = ndcollection.meta
27+
if ndcollection._aligned_axes is not None:
28+
node["aligned_axes"] = ndcollection._aligned_axes
29+
30+
return node
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from asdf.extension import Converter
2+
3+
4+
class NDCubeSequenceConverter(Converter):
5+
tags = ["tag:sunpy.org:ndcube/ndcube/ndcube_sequence-*"]
6+
types = ["ndcube.ndcube_sequence.NDCubeSequence"]
7+
8+
def from_yaml_tree(self, node, tag, ctx):
9+
from ndcube.ndcube_sequence import NDCubeSequence
10+
11+
return NDCubeSequence(node["data"],
12+
meta=node.get("meta"),
13+
common_axis=node.get("common_axis"))
14+
15+
16+
def to_yaml_tree(self, ndcseq, tag, ctx):
17+
18+
node = {}
19+
node["data"] = ndcseq.data
20+
if ndcseq.meta is not None:
21+
node["meta"] = ndcseq.meta
22+
if ndcseq._common_axis is not None:
23+
node["common_axis"] = ndcseq._common_axis
24+
25+
return node
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import pytest
2+
from gwcs import __version__ as gwcs_version
3+
from packaging.version import Version
4+
5+
import asdf
6+
7+
from ndcube.ndcollection import NDCollection
8+
from ndcube.ndcube_sequence import NDCubeSequence
9+
from ndcube.tests.helpers import assert_collections_equal
10+
11+
12+
@pytest.fixture
13+
def create_ndcollection_cube(ndcube_gwcs_3d_ln_lt_l, ndcube_gwcs_3d_ln_lt_l_ec_q_t_gc, ndcube_gwcs_3d_ln_lt_l_ec_dropped_dim):
14+
aligned_axes = ((1, 2), (1, 2), (1, 2))
15+
cube_collection = NDCollection([("cube0", ndcube_gwcs_3d_ln_lt_l),
16+
("cube1", ndcube_gwcs_3d_ln_lt_l_ec_q_t_gc),
17+
("cube2", ndcube_gwcs_3d_ln_lt_l_ec_dropped_dim)],
18+
aligned_axes=aligned_axes)
19+
20+
return cube_collection
21+
22+
23+
@pytest.mark.skipif(Version(gwcs_version) < Version("0.20"), reason="Requires gwcs>=0.20")
24+
def test_serialization_cube(create_ndcollection_cube, tmp_path):
25+
ndcollection = create_ndcollection_cube
26+
file_path = tmp_path / "test.asdf"
27+
with asdf.AsdfFile() as af:
28+
af["ndcube_gwcs"] = ndcollection
29+
af.write_to(file_path)
30+
31+
with asdf.open(file_path) as af:
32+
assert_collections_equal(af["ndcube_gwcs"], ndcollection)
33+
@pytest.fixture
34+
def create_ndcollection_sequence(ndcube_gwcs_3d_ln_lt_l, ndcube_gwcs_3d_ln_lt_l_ec_dropped_dim):
35+
36+
sequence02 = NDCubeSequence([ndcube_gwcs_3d_ln_lt_l, ndcube_gwcs_3d_ln_lt_l_ec_dropped_dim])
37+
sequence20 = NDCubeSequence([ndcube_gwcs_3d_ln_lt_l_ec_dropped_dim, ndcube_gwcs_3d_ln_lt_l])
38+
seq_collection = NDCollection([("seq0", sequence02), ("seq1", sequence20)], aligned_axes="all")
39+
return seq_collection
40+
41+
42+
@pytest.mark.skipif(Version(gwcs_version) < Version("0.20"), reason="Requires gwcs>=0.20")
43+
def test_serialization_sequence(create_ndcollection_sequence, tmp_path):
44+
ndcollection = create_ndcollection_sequence
45+
file_path = tmp_path / "test.asdf"
46+
with asdf.AsdfFile() as af:
47+
af["ndcube_gwcs"] = ndcollection
48+
af.write_to(file_path)
49+
50+
with asdf.open(file_path) as af:
51+
assert_collections_equal(af["ndcube_gwcs"], ndcollection)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pytest
2+
from gwcs import __version__ as gwcs_version
3+
from packaging.version import Version
4+
5+
import asdf
6+
7+
from ndcube.ndcube_sequence import NDCubeSequence
8+
from ndcube.tests.helpers import assert_cubesequences_equal
9+
10+
11+
@pytest.mark.skipif(Version(gwcs_version) < Version("0.20"), reason="Requires gwcs>=0.20")
12+
def test_serialization(ndcube_gwcs_3d_ln_lt_l, ndcube_gwcs_3d_ln_lt_l_ec_q_t_gc, tmp_path):
13+
file_path = tmp_path / "test.asdf"
14+
ndcseq = NDCubeSequence([ndcube_gwcs_3d_ln_lt_l, ndcube_gwcs_3d_ln_lt_l_ec_q_t_gc], common_axis=1)
15+
with asdf.AsdfFile() as af:
16+
af["ndcube_gwcs"] = ndcseq
17+
af.write_to(file_path)
18+
19+
with asdf.open(file_path) as af:
20+
assert_cubesequences_equal(af["ndcube_gwcs"], ndcseq)

ndcube/asdf/entry_points.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,23 @@ def get_extensions():
3333
"""
3434
from ndcube.asdf.converters.extracoords_converter import ExtraCoordsConverter
3535
from ndcube.asdf.converters.globalcoords_converter import GlobalCoordsConverter
36+
from ndcube.asdf.converters.ndcollection_converter import NDCollectionConverter
3637
from ndcube.asdf.converters.ndcube_converter import NDCubeConverter
38+
from ndcube.asdf.converters.ndcubesequence_converter import NDCubeSequenceConverter
3739
from ndcube.asdf.converters.tablecoord_converter import (
3840
QuantityTableCoordinateConverter,
3941
SkyCoordTableCoordinateConverter,
4042
TimeTableCoordConverter,
4143
)
42-
4344
ndcube_converters = [
4445
NDCubeConverter(),
4546
ExtraCoordsConverter(),
4647
TimeTableCoordConverter(),
4748
QuantityTableCoordinateConverter(),
4849
SkyCoordTableCoordinateConverter(),
4950
GlobalCoordsConverter(),
51+
NDCubeSequenceConverter(),
52+
NDCollectionConverter(),
5053
]
5154
_manifest_uri = "asdf://sunpy.org/ndcube/manifests/ndcube-0.1.0"
5255

ndcube/asdf/resources/manifests/ndcube-0.1.0.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@ tags:
2323

2424
- tag_uri: "tag:sunpy.org:ndcube/global_coords/globalcoords-0.1.0"
2525
schema_uri: "asdf://sunpy.org/ndcube/schemas/global_coords-0.1.0"
26+
27+
- tag_uri: "tag:sunpy.org:ndcube/ndcube/ndcube_sequence-0.1.0"
28+
schema_uri: "asdf://sunpy.org/ndcube/schemas/ndcube_sequence-0.1.0"
29+
30+
- tag_uri: "tag:sunpy.org:ndcube/ndcube/ndcollection-0.1.0"
31+
schema_uri: "asdf://sunpy.org/ndcube/schemas/ndcollection-0.1.0"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
%YAML 1.1
2+
---
3+
$schema: "http://stsci.edu/schemas/yaml-schema/draft-01"
4+
id: "asdf://sunpy.org/ndcube/schemas/ndcollection-0.1.0"
5+
6+
title:
7+
Represents the ndcube.ndcollection.NDCollection object
8+
9+
description:
10+
Represents the ndcube ndcube.ndcollection.NDCollection object
11+
12+
type: object
13+
properties:
14+
keys:
15+
type: array
16+
value:
17+
type: array
18+
items:
19+
- type: object
20+
oneOf:
21+
- tag: "tag:sunpy.org:ndcube/ndcube/ndcube-0.*"
22+
- tag: "tag:sunpy.org:ndcube/ndcube/ndcube_sequence-0.*"
23+
aligned_axes:
24+
anyOf:
25+
- type: object
26+
- type: string
27+
28+
required: [keys, value]
29+
additionalProperties: true
30+
...
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
%YAML 1.1
2+
---
3+
$schema: "http://stsci.edu/schemas/yaml-schema/draft-01"
4+
id: "asdf://sunpy.org/ndcube/schemas/ndcube_sequence-0.1.0"
5+
6+
title:
7+
Represents the ndcube.ndcube_sequence.NDCubeSequence object
8+
9+
description:
10+
Represents the ndcube.ndcube_sequence.NDCubeSequence object
11+
12+
type: object
13+
properties:
14+
data:
15+
type: array
16+
items:
17+
tag: "tag:sunpy.org:ndcube/ndcube/ndcube-0.*"
18+
meta:
19+
type: object
20+
common_axis:
21+
type: integer
22+
23+
required: [data]
24+
additionalProperties: true
25+
...

0 commit comments

Comments
 (0)