Skip to content

Commit

Permalink
Add serialization logic for uncertainty objects
Browse files Browse the repository at this point in the history
  • Loading branch information
ViciousEagle03 committed Aug 21, 2024
1 parent fa5f1f4 commit 7175aae
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 0 deletions.
4 changes: 4 additions & 0 deletions asdf_astropy/converters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"AstropyTableConverter",
"AsdfTableConverter",
"NdarrayMixinConverter",
"UncertaintyConverter",
"StdDevUncertaintyConverter",
"UnknownUncertaintyConverter",
"TimeDeltaConverter",
"TimeConverter",
"CompoundConverter",
Expand Down Expand Up @@ -73,4 +76,5 @@
TransformConverterBase,
UnitsMappingConverter,
)
from .uncertainty import StdDevUncertaintyConverter, UncertaintyConverter, UnknownUncertaintyConverter
from .unit import EquivalencyConverter, MagUnitConverter, QuantityConverter, UnitConverter
7 changes: 7 additions & 0 deletions asdf_astropy/converters/uncertainty/__init__.py
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 asdf_astropy/converters/uncertainty/tests/test_uncertainty.py
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
40 changes: 40 additions & 0 deletions asdf_astropy/converters/uncertainty/uncertainty.py
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)
8 changes: 8 additions & 0 deletions asdf_astropy/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
from .converters.transform.rotations import Rotate3DConverter, RotationSequenceConverter
from .converters.transform.spline import SplineConverter
from .converters.transform.tabular import TabularConverter
from .converters.uncertainty.uncertainty import (
StdDevUncertaintyConverter,
UncertaintyConverter,
UnknownUncertaintyConverter,
)
from .converters.unit.equivalency import EquivalencyConverter
from .converters.unit.magunit import MagUnitConverter
from .converters.unit.quantity import QuantityConverter
Expand Down Expand Up @@ -482,6 +487,9 @@
AstropyTableConverter(),
AstropyFitsConverter(),
NdarrayMixinConverter(),
UncertaintyConverter(),
StdDevUncertaintyConverter(),
UnknownUncertaintyConverter(),
]

_COORDINATES_MANIFEST_URIS = [
Expand Down
15 changes: 15 additions & 0 deletions asdf_astropy/resources/manifests/astropy-1.0.0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,18 @@ tags:
This transform operates on the units of the input, first converting to
the expected input units, then assigning replacement output units without
further conversion.
- tag_uri: tag:astropy.org:astropy/uncertainty/uncertainty-1.0.0
schema_uri: http://astropy.org/schemas/astropy/uncertainty/uncertainty-1.0.0
title: Represent the nddata.uncertainty.NDUncertainty object
description: |-
Represents an instance of the nddata.uncertainty.NDUncertainty object
- tag_uri: tag:astropy.org:astropy/uncertainty/unknownuncertainty-1.0.0
schema_uri: http://astropy.org/schemas/astropy/uncertainty/unknownuncertainty-1.0.0
title: Represent the nddata.uncertainty.UnknownUncertainty object
description: |-
Represents an instance of the nddata.uncertainty.UnknownUncertainty object
- tag_uri: tag:astropy.org:astropy/uncertainty/stddevuncertainty-1.0.0
schema_uri: http://astropy.org/schemas/astropy/uncertainty/stddevuncertainty-1.0.0
title: Represent the nddata.uncertainty.StdDevUncertainty object
description: |-
Represents an instance of the nddata.uncertainty.StdDevUncertainty object
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 asdf_astropy/resources/schemas/uncertainty/uncertainty-1.0.0.yaml
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.*"
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.*"

0 comments on commit 7175aae

Please sign in to comment.