-
-
Notifications
You must be signed in to change notification settings - Fork 47
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
Add support for the serialization
of the ndcube
WCS wrappers.
#751
Open
ViciousEagle03
wants to merge
6
commits into
sunpy:asdf-support
Choose a base branch
from
ViciousEagle03:wcs_wrapper_asdf_support
base: asdf-support
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
da6c639
Add the serialization support for the ResampledLowlevelWCS ReorderedL…
ViciousEagle03 c370c07
Merge branch 'asdf-support' into wcs_wrapper_asdf_support
Cadair 90ecc7d
Merge branch 'asdf-support' into wcs_wrapper_asdf_support
ViciousEagle03 1203516
Apply suggestions from code review
ViciousEagle03 a349d20
Use Tabular1D, Tabular2D instead of tabular_model
ViciousEagle03 ddebe6b
Add comment and update logic
ViciousEagle03 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from asdf.extension import Converter | ||
|
||
|
||
class CompoundConverter(Converter): | ||
tags = ["tag:sunpy.org:ndcube/compoundwcs-0.1.0"] | ||
types = ["ndcube.wcs.wrappers.compound_wcs.CompoundLowLevelWCS"] | ||
|
||
def from_yaml_tree(self, node, tag, ctx): | ||
from ndcube.wcs.wrappers import CompoundLowLevelWCS | ||
|
||
return(CompoundLowLevelWCS(*node["wcs"], mapping = node.get("mapping"), pixel_atol = node.get("atol"))) | ||
|
||
def to_yaml_tree(self, compoundwcs, tag, ctx): | ||
node={} | ||
node["wcs"] = compoundwcs._wcs | ||
node["mapping"] = compoundwcs.mapping.mapping | ||
node["atol"] = compoundwcs.atol | ||
return node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from asdf.extension import Converter | ||
|
||
|
||
class ReorderedConverter(Converter): | ||
tags = ["tag:sunpy.org:ndcube/reorderedwcs-0.1.0"] | ||
types = ["ndcube.wcs.wrappers.reordered_wcs.ReorderedLowLevelWCS"] | ||
|
||
def from_yaml_tree(self, node, tag, ctx): | ||
from ndcube.wcs.wrappers import ReorderedLowLevelWCS | ||
|
||
reorderedwcs = ReorderedLowLevelWCS(wcs=node["wcs"], | ||
pixel_order = node.get("pixel_order"), | ||
world_order = node.get("world_order") | ||
) | ||
return reorderedwcs | ||
def to_yaml_tree(self, reorderedwcs, tag, ctx): | ||
node={} | ||
node["wcs"] = reorderedwcs._wcs | ||
node["pixel_order"] = reorderedwcs._pixel_order | ||
node["world_order"] = reorderedwcs._world_order | ||
return node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from asdf.extension import Converter | ||
|
||
|
||
class ResampledConverter(Converter): | ||
tags = ["tag:sunpy.org:ndcube/resampledwcs-0.1.0"] | ||
types = ["ndcube.wcs.wrappers.resampled_wcs.ResampledLowLevelWCS"] | ||
|
||
def from_yaml_tree(self, node, tag, ctx): | ||
from ndcube.wcs.wrappers import ResampledLowLevelWCS | ||
|
||
resampledwcs = ResampledLowLevelWCS(wcs=node["wcs"], | ||
offset = node.get("offset"), | ||
factor = node.get("factor"), | ||
) | ||
return resampledwcs | ||
def to_yaml_tree(self, resampledwcs, tag, ctx): | ||
node={} | ||
node["wcs"] = resampledwcs._wcs | ||
node["factor"] = resampledwcs._factor | ||
node["offset"] = resampledwcs._offset | ||
|
||
return node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
""" | ||
Tests for roundtrip serialization of NDCube with various GWCS types. | ||
|
||
TODO: Add tests for the roundtrip serialization of NDCube with ResampledLowLevelWCS, ReorderedLowLevelWCS, and CompoundLowLevelWCS when using astropy.wcs.WCS. | ||
""" | ||
|
||
import pytest | ||
from gwcs import __version__ as gwcs_version | ||
from packaging.version import Version | ||
|
||
import asdf | ||
|
||
from ndcube import NDCube | ||
from ndcube.conftest import data_nd | ||
from ndcube.tests.helpers import assert_cubes_equal | ||
from ndcube.wcs.wrappers import CompoundLowLevelWCS, ReorderedLowLevelWCS, ResampledLowLevelWCS | ||
|
||
|
||
@pytest.fixture | ||
def create_ndcube_resampledwcs(gwcs_3d_lt_ln_l): | ||
shape = (2, 3, 4) | ||
new_wcs = ResampledLowLevelWCS(wcs = gwcs_3d_lt_ln_l, factor=2 ,offset = 1) | ||
data = data_nd(shape) | ||
return NDCube(data = data, wcs =new_wcs) | ||
|
||
|
||
@pytest.mark.skipif(Version(gwcs_version) < Version("0.20"), reason="Requires gwcs>=0.20") | ||
def test_serialization_resampled(create_ndcube_resampledwcs, tmp_path): | ||
ndc = create_ndcube_resampledwcs | ||
file_path = tmp_path / "test.asdf" | ||
with asdf.AsdfFile() as af: | ||
af["ndcube"] = ndc | ||
af.write_to(file_path) | ||
|
||
with asdf.open(file_path) as af: | ||
loaded_ndcube = af["ndcube"] | ||
|
||
loaded_resampledwcs = loaded_ndcube.wcs.low_level_wcs | ||
resampledwcs = ndc.wcs.low_level_wcs | ||
assert (loaded_resampledwcs._factor == resampledwcs._factor).all() | ||
assert (loaded_resampledwcs._offset == resampledwcs._offset).all() | ||
|
||
assert_cubes_equal(loaded_ndcube, ndc) | ||
|
||
@pytest.fixture | ||
def create_ndcube_reorderedwcs(gwcs_3d_lt_ln_l): | ||
shape = (2, 3, 4) | ||
new_wcs = ReorderedLowLevelWCS(wcs = gwcs_3d_lt_ln_l, pixel_order=[1, 2, 0] ,world_order=[2, 0, 1]) | ||
data = data_nd(shape) | ||
return NDCube(data = data, wcs =new_wcs) | ||
|
||
|
||
|
||
@pytest.mark.skipif(Version(gwcs_version) < Version("0.20"), reason="Requires gwcs>=0.20") | ||
def test_serialization_reordered(create_ndcube_reorderedwcs, tmp_path): | ||
ndc = create_ndcube_reorderedwcs | ||
file_path = tmp_path / "test.asdf" | ||
with asdf.AsdfFile() as af: | ||
af["ndcube"] = ndc | ||
af.write_to(file_path) | ||
|
||
with asdf.open(file_path) as af: | ||
loaded_ndcube = af["ndcube"] | ||
|
||
loaded_reorderedwcs = loaded_ndcube.wcs.low_level_wcs | ||
reorderedwcs = ndc.wcs.low_level_wcs | ||
assert (loaded_reorderedwcs._pixel_order == reorderedwcs._pixel_order) | ||
assert (loaded_reorderedwcs._world_order == reorderedwcs._world_order) | ||
|
||
assert_cubes_equal(loaded_ndcube, ndc) | ||
|
||
@pytest.fixture | ||
def create_ndcube_compoundwcs(gwcs_2d_lt_ln, time_and_simple_extra_coords_2d): | ||
|
||
shape = (1, 2, 3, 4) | ||
new_wcs = CompoundLowLevelWCS(gwcs_2d_lt_ln, time_and_simple_extra_coords_2d.wcs, mapping = [0, 1, 2, 3]) | ||
data = data_nd(shape) | ||
return NDCube(data = data, wcs = new_wcs) | ||
|
||
@pytest.mark.skipif(Version(gwcs_version) < Version("0.20"), reason="Requires gwcs>=0.20") | ||
def test_serialization_compoundwcs(create_ndcube_compoundwcs, tmp_path): | ||
ndc = create_ndcube_compoundwcs | ||
file_path = tmp_path / "test.asdf" | ||
with asdf.AsdfFile() as af: | ||
af["ndcube"] = ndc | ||
af.write_to(file_path) | ||
|
||
with asdf.open(file_path) as af: | ||
loaded_ndcube = af["ndcube"] | ||
assert_cubes_equal(loaded_ndcube, ndc) | ||
assert (loaded_ndcube.wcs.low_level_wcs.mapping.mapping == ndc.wcs.low_level_wcs.mapping.mapping) | ||
assert (loaded_ndcube.wcs.low_level_wcs.atol == ndc.wcs.low_level_wcs.atol) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
%YAML 1.1 | ||
--- | ||
$schema: "http://stsci.edu/schemas/yaml-schema/draft-01" | ||
id: "asdf://sunpy.org/ndcube/schemas/Compoundwcs-0.1.0" | ||
|
||
title: | ||
Represents the ndcube CompoundLowLevelWCS object | ||
|
||
description: | ||
Represents the ndcube CompoundLowLevelWCS object | ||
|
||
type: object | ||
properties: | ||
wcs: | ||
type: array | ||
items: | ||
tag: "tag:stsci.edu:gwcs/wcs-1.*" | ||
mapping: | ||
type: array | ||
atol: | ||
type: number | ||
|
||
required: [wcs] | ||
additionalProperties: true | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
%YAML 1.1 | ||
--- | ||
$schema: "http://stsci.edu/schemas/yaml-schema/draft-01" | ||
id: "asdf://sunpy.org/ndcube/schemas/resampledwcs-0.1.0" | ||
|
||
title: | ||
Represents the ndcube ReorderedLowLevelWCS object | ||
|
||
description: | ||
Represents the ndcube ReorderedLowLevelWCS object | ||
|
||
type: object | ||
properties: | ||
wcs: | ||
tag: "tag:stsci.edu:gwcs/wcs-1.*" | ||
pixel_order: | ||
type: array | ||
world_order: | ||
type: array | ||
|
||
required: [wcs] | ||
additionalProperties: true | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
%YAML 1.1 | ||
--- | ||
$schema: "http://stsci.edu/schemas/yaml-schema/draft-01" | ||
id: "asdf://sunpy.org/ndcube/schemas/resampledwcs-0.1.0" | ||
|
||
title: | ||
Represents the ndcube ResampledLowLevelWCS object | ||
|
||
description: | ||
Represents the ndcube ResampledLowLevelWCS object | ||
|
||
type: object | ||
properties: | ||
wcs: | ||
tag: "tag:stsci.edu:gwcs/wcs-1.*" | ||
factor: | ||
tag: "tag:stsci.edu:asdf/core/ndarray-1.0.0" | ||
offset: | ||
tag: "tag:stsci.edu:asdf/core/ndarray-1.0.0" | ||
|
||
required: [wcs] | ||
additionalProperties: true | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should have an else for >2D. Also a comment saying that this is for asdf would be good for future reference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure