Skip to content

Commit c144f77

Browse files
committed
Add support for NDMeta
1 parent 81677e9 commit c144f77

File tree

7 files changed

+63
-5
lines changed

7 files changed

+63
-5
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import numpy as np
2+
3+
from asdf.extension import Converter
4+
5+
6+
class NDMetaConverter(Converter):
7+
tags = ["tag:sunpy.org:ndcube/meta/ndmeta-*"]
8+
types = ["ndcube.meta.NDMeta"]
9+
10+
def from_yaml_tree(self, node, tag, ctx):
11+
from ndcube.meta import NDMeta
12+
axes = {k: np.array(v) for k, v in node["axes"].items()}
13+
meta = NDMeta(node["meta"], node["key_comments"], axes, node["data_shape"])
14+
meta._original_meta = node["original_meta"]
15+
return meta
16+
17+
def to_yaml_tree(self, meta, tag, ctx):
18+
node = {}
19+
node["meta"] = dict(meta)
20+
node["key_comments"] = meta.key_comments
21+
node["axes"] = meta.axes
22+
node["data_shape"] = meta.data_shape
23+
node["original_meta"] = meta._original_meta # not the MappingProxy object
24+
return node

ndcube/asdf/entry_points.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def get_extensions():
3737
from ndcube.asdf.converters.ndcollection_converter import NDCollectionConverter
3838
from ndcube.asdf.converters.ndcube_converter import NDCubeConverter
3939
from ndcube.asdf.converters.ndcubesequence_converter import NDCubeSequenceConverter
40+
from ndcube.asdf.converters.ndmeta_converter import NDMetaConverter
4041
from ndcube.asdf.converters.reorderedwcs_converter import ReorderedConverter
4142
from ndcube.asdf.converters.resampled_converter import ResampledConverter
4243
from ndcube.asdf.converters.tablecoord_converter import (
@@ -58,6 +59,7 @@ def get_extensions():
5859
CompoundConverter(),
5960
NDCubeSequenceConverter(),
6061
NDCollectionConverter(),
62+
NDMetaConverter(),
6163
]
6264
_manifest_uri = "asdf://sunpy.org/ndcube/manifests/ndcube-1.0.0"
6365

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,6 @@ tags:
4141

4242
- tag_uri: "tag:sunpy.org:ndcube/ndcube/ndcollection-1.0.0"
4343
schema_uri: "asdf://sunpy.org/ndcube/schemas/ndcollection-1.0.0"
44+
45+
- tag_uri: "tag:sunpy.org:ndcube/meta/ndmeta-1.0.0"
46+
schema_uri: "asdf://sunpy.org/ndcube/schemas/ndmeta-1.0.0"

ndcube/asdf/resources/schemas/ndcube-1.0.0.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
%YAML 1.1
22
---
33
$schema: "http://stsci.edu/schemas/yaml-schema/draft-01"
4-
id: "asdf://sunpy.org/ndcube/schemas/NDCube-1.0.0"
4+
id: "asdf://sunpy.org/ndcube/schemas/ndcube-1.0.0"
55

66
title:
77
Represents the ndcube NDCube object
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
%YAML 1.1
2+
---
3+
$schema: "http://stsci.edu/schemas/yaml-schema/draft-01"
4+
id: "asdf://sunpy.org/ndcube/schemas/ndmeta-1.0.0"
5+
6+
title:
7+
Represents the ndcube NDMeta object
8+
9+
description:
10+
Represents the ndcube NDMeta object
11+
12+
type: object
13+
properties:
14+
meta:
15+
type: object
16+
key_comments:
17+
type: object
18+
axes:
19+
type: object
20+
data_shape:
21+
tag: "tag:stsci.edu:asdf/core/ndarray-1.*"
22+
original_meta:
23+
type: object
24+
25+
required: [meta, key_comments, axes, data_shape, original_meta]
26+
additionalProperties: true
27+
...

ndcube/meta.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,11 @@ def _sanitize_axis_value(self, axis, value, key):
184184
if isinstance(axis, numbers.Integral):
185185
axis = (axis,)
186186
if len(axis) == 0:
187-
return ValueError(axis_err_msg)
187+
raise ValueError(axis_err_msg)
188188
# Verify each entry in axes is an iterable of ints or a scalar.
189189
if not (isinstance(axis, collections.abc.Iterable)
190190
and all(isinstance(i, numbers.Integral) for i in axis)):
191-
return ValueError(axis_err_msg)
191+
raise ValueError(axis_err_msg)
192192
# If metadata's axis/axes include axis beyond current data shape, extend it.
193193
data_shape = self.data_shape
194194
if max(axis) >= len(data_shape):

ndcube/tests/helpers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,12 @@ def assert_metas_equal(test_input, expected_output):
107107
else:
108108
assert np.allclose(test_input.data_shape, expected_output.data_shape)
109109

110-
for test_value, expected_value in zip(test_input.values(), expected_output.values()):
110+
for key in test_input.keys():
111+
test_value = test_input[key]
112+
expected_value = expected_output[key]
111113
try:
112114
assert test_value == expected_value
113-
except ValueError as err: # noqa: PERF203
115+
except ValueError as err:
114116
if multi_element_msg in err.args[0]:
115117
assert np.allclose(test_value, expected_value)
116118
for key in test_input.axes.keys():

0 commit comments

Comments
 (0)