-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add serialization logic for uncertainty objects
- Loading branch information
1 parent
fa5f1f4
commit 7175aae
Showing
10 changed files
with
166 additions
and
0 deletions.
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
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,7 @@ | ||
__all__ = [ | ||
"UncertaintyConverter", | ||
"StdDevUncertaintyConverter", | ||
"UnknownUncertaintyConverter", | ||
] | ||
|
||
from .uncertainty import StdDevUncertaintyConverter, UncertaintyConverter, UnknownUncertaintyConverter |
Empty file.
27 changes: 27 additions & 0 deletions
27
asdf_astropy/converters/uncertainty/tests/test_uncertainty.py
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,27 @@ | ||
import asdf | ||
import numpy as np | ||
import pytest | ||
from astropy import units as u | ||
from astropy.nddata import StdDevUncertainty, UnknownUncertainty | ||
|
||
|
||
def create_uncertainty(): | ||
uncert = np.arange(100).reshape(10, 10) | ||
uncertainty_stddev_1 = StdDevUncertainty(uncert, unit="m") | ||
uncertainty_stddev_2 = StdDevUncertainty([2], unit="m") | ||
uncertainty_unknown_1 = UnknownUncertainty(uncert, unit="m") | ||
uncertainty_unknown_2 = UnknownUncertainty([0.4], unit=u.adu) | ||
return [uncertainty_stddev_1, uncertainty_stddev_2, uncertainty_unknown_1, uncertainty_unknown_2] | ||
|
||
|
||
@pytest.mark.parametrize("uncertainty", create_uncertainty()) | ||
def test_uncertainty_serialization(uncertainty, tmp_path): | ||
file_path = tmp_path / "test_uncertainty.asdf" | ||
with asdf.AsdfFile() as af: | ||
af["uncertainty"] = uncertainty | ||
af.write_to(file_path) | ||
|
||
with asdf.open(file_path) as af: | ||
loaded_uncert = af["uncertainty"] | ||
assert (loaded_uncert._array == uncertainty._array).all() | ||
assert loaded_uncert.unit == uncertainty.unit |
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,40 @@ | ||
from asdf.extension import Converter | ||
|
||
|
||
class UncertaintyConverter(Converter): | ||
tags = ("tag:astropy.org:astropy/uncertainty/uncertainty-*",) | ||
types = ("astropy.nddata.nduncertainty.Uncertainty",) | ||
|
||
def from_yaml_tree(self, node, tag, ctx): | ||
return (node["array"], node.get("unit")) | ||
|
||
def to_yaml_tree(self, nddata_uncertainty, tag, ctx): | ||
node = {} | ||
|
||
node["array"] = nddata_uncertainty.array | ||
if nddata_uncertainty.unit is not None: | ||
node["unit"] = nddata_uncertainty.unit | ||
|
||
return node | ||
|
||
|
||
class StdDevUncertaintyConverter(UncertaintyConverter): | ||
tags = ("tag:astropy.org:astropy/uncertainty/stddevuncertainty-*",) | ||
types = ("astropy.nddata.nduncertainty.StdDevUncertainty",) | ||
|
||
def from_yaml_tree(self, node, tag, ctx): | ||
from astropy.nddata import StdDevUncertainty | ||
|
||
array, unit = super().from_yaml_tree(node, tag, ctx) | ||
return StdDevUncertainty(array=array, unit=unit) | ||
|
||
|
||
class UnknownUncertaintyConverter(UncertaintyConverter): | ||
tags = ("tag:astropy.org:astropy/uncertainty/unknownuncertainty-*",) | ||
types = ("astropy.nddata.nduncertainty.UnknownUncertainty",) | ||
|
||
def from_yaml_tree(self, node, tag, ctx): | ||
from astropy.nddata import UnknownUncertainty | ||
|
||
array, unit = super().from_yaml_tree(node, tag, ctx) | ||
return UnknownUncertainty(array=array, unit=unit) |
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
22 changes: 22 additions & 0 deletions
22
asdf_astropy/resources/schemas/uncertainty/stddevuncertainty-1.0.0.yaml
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 @@ | ||
%YAML 1.1 | ||
--- | ||
$schema: "http://stsci.edu/schemas/yaml-schema/draft-01" | ||
id: "http://astropy.org/schemas/astropy/uncertainty/stddevuncertainty-1.0.0" | ||
|
||
title: | ||
Represents the astropy.nddata.StdDevUncertainty class | ||
|
||
description: | ||
This object represents the `StdDevUncertainty` class, which is a subclass | ||
of the `NDUncertainty` class from `astropy.nddata` | ||
|
||
allOf: | ||
- tag: "tag:astropy.org:astropy/uncertainty/stddevuncertainty-1.0.0" | ||
- type: object | ||
properties: | ||
array: | ||
type: object | ||
unit: | ||
anyOf: | ||
- tag: "tag:stsci.edu:asdf/unit/unit-*" | ||
- tag: "tag:astropy.org:astropy/units/unit-1.*" |
21 changes: 21 additions & 0 deletions
21
asdf_astropy/resources/schemas/uncertainty/uncertainty-1.0.0.yaml
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 @@ | ||
%YAML 1.1 | ||
--- | ||
$schema: "http://stsci.edu/schemas/yaml-schema/draft-01" | ||
id: "http://astropy.org/schemas/astropy/uncertainty/uncertainty-1.0.0" | ||
|
||
title: | ||
Represents the astropy.nddata.NDUncertainty object | ||
|
||
description: | ||
Represents the astropy.nddata.NDUncertainty object | ||
|
||
allOf: | ||
- tag: "tag:astropy.org:astropy/uncertainty/uncertainty-1.0.0" | ||
- type: object | ||
properties: | ||
array: | ||
type: object | ||
unit: | ||
anyOf: | ||
- tag: "tag:stsci.edu:asdf/unit/unit-*" | ||
- tag: "tag:astropy.org:astropy/units/unit-1.*" |
22 changes: 22 additions & 0 deletions
22
asdf_astropy/resources/schemas/uncertainty/unknownuncertainty-1.0.0.yaml
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 @@ | ||
%YAML 1.1 | ||
--- | ||
$schema: "http://stsci.edu/schemas/yaml-schema/draft-01" | ||
id: "http://astropy.org/schemas/astropy/uncertainty/unknownuncertainty-1.0.0" | ||
|
||
title: | ||
Represents the astropy.nddata.UnknownUncertainty class | ||
|
||
description: | ||
This object represents the `UnknownUncertainty` class, which is a subclass | ||
of the `NDUncertainty` class from `astropy.nddata` | ||
|
||
allOf: | ||
- tag: "tag:astropy.org:astropy/uncertainty/unknownuncertainty-1.0.0" | ||
- type: object | ||
properties: | ||
array: | ||
type: object | ||
unit: | ||
anyOf: | ||
- tag: "tag:stsci.edu:asdf/unit/unit-*" | ||
- tag: "tag:astropy.org:astropy/units/unit-1.*" |