Skip to content

Commit

Permalink
Consolidate all uncertainties into one
Browse files Browse the repository at this point in the history
  • Loading branch information
braingram committed Nov 8, 2024
1 parent 09df40a commit 34da76e
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 125 deletions.
6 changes: 2 additions & 4 deletions asdf_astropy/converters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
"AstropyTableConverter",
"AsdfTableConverter",
"NdarrayMixinConverter",
"StdDevUncertaintyConverter",
"UnknownUncertaintyConverter",
"VarianceUncertaintyConverter",
"UncertaintyConverter",
"TimeDeltaConverter",
"TimeConverter",
"CompoundConverter",
Expand Down Expand Up @@ -55,7 +53,7 @@
SpectralCoordConverter,
)
from .fits import AsdfFitsConverter, AstropyFitsConverter, FitsConverter
from .nddata import StdDevUncertaintyConverter, UnknownUncertaintyConverter, VarianceUncertaintyConverter
from .nddata import UncertaintyConverter
from .table import AsdfTableConverter, AstropyTableConverter, ColumnConverter, NdarrayMixinConverter
from .time import TimeConverter, TimeDeltaConverter
from .transform import (
Expand Down
6 changes: 2 additions & 4 deletions asdf_astropy/converters/nddata/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
__all__ = [
"StdDevUncertaintyConverter",
"UnknownUncertaintyConverter",
"VarianceUncertaintyConverter",
"UncertaintyConverter",
]

from .uncertainty import StdDevUncertaintyConverter, UnknownUncertaintyConverter, VarianceUncertaintyConverter
from .uncertainty import UncertaintyConverter
66 changes: 28 additions & 38 deletions asdf_astropy/converters/nddata/uncertainty.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,35 @@
class _UncertaintyBaseConverter:
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(_UncertaintyBaseConverter):
tags = ("tag:astropy.org:astropy/nddata/stddevuncertainty-*",)
types = ("astropy.nddata.nduncertainty.StdDevUncertainty",)
class UncertaintyConverter:
tags = ("tag:astropy.org:astropy/nddata/uncertainty-*",)
types = (
"astropy.nddata.nduncertainty.StdDevUncertainty",
"astropy.nddata.nduncertainty.UnknownUncertainty",
"astropy.nddata.nduncertainty.VarianceUncertainty",
)

# Mapping of uncertainty class name (attribute of astropy.nddata)
# and code "name" stored in the data file.
# This will need to be separately versioned if the schema is updated.
_class_name_to_code = {
"StdDevUncertainty": "stddev",
"UnknownUncertainty": "unknown",
"VarianceUncertainty": "variance",
}
_class_code_to_name = {v: k for k, v in _class_name_to_code.items()}

def from_yaml_tree(self, node, tag, ctx):
from astropy.nddata import StdDevUncertainty
import astropy.nddata

array, unit = super().from_yaml_tree(node, tag, ctx)
return StdDevUncertainty(array=array, unit=unit)
class_name = self._class_code_to_name[node["name"]]

return getattr(astropy.nddata, class_name)(node["array"], unit=node.get("unit"))

class UnknownUncertaintyConverter(_UncertaintyBaseConverter):
tags = ("tag:astropy.org:astropy/nddata/unknownuncertainty-*",)
types = ("astropy.nddata.nduncertainty.UnknownUncertainty",)
def to_yaml_tree(self, obj, tag, ctx):
node = {
"name": self._class_name_to_code[obj.__class__.__name__],
"array": obj.array,
}

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)


class VarianceUncertaintyConverter(_UncertaintyBaseConverter):
tags = ("tag:astropy.org:astropy/nddata/varianceuncertainty-*",)
types = ("astropy.nddata.nduncertainty.VarianceUncertainty",)
if obj.unit is not None:
node["unit"] = obj.unit

def from_yaml_tree(self, node, tag, ctx):
from astropy.nddata import VarianceUncertainty

array, unit = super().from_yaml_tree(node, tag, ctx)
return VarianceUncertainty(array=array, unit=unit)
return node
10 changes: 2 additions & 8 deletions asdf_astropy/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@
from .converters.coordinates.sky_coord import SkyCoordConverter
from .converters.coordinates.spectral_coord import SpectralCoordConverter
from .converters.fits.fits import AsdfFitsConverter, AstropyFitsConverter
from .converters.nddata.uncertainty import (
StdDevUncertaintyConverter,
UnknownUncertaintyConverter,
VarianceUncertaintyConverter,
)
from .converters.nddata.uncertainty import UncertaintyConverter
from .converters.table.table import AsdfTableConverter, AstropyTableConverter, ColumnConverter, NdarrayMixinConverter
from .converters.time.time import TimeConverter
from .converters.time.time_delta import TimeDeltaConverter
Expand Down Expand Up @@ -481,9 +477,7 @@
AstropyTableConverter(),
AstropyFitsConverter(),
NdarrayMixinConverter(),
StdDevUncertaintyConverter(),
UnknownUncertaintyConverter(),
VarianceUncertaintyConverter(),
UncertaintyConverter(),
]

_COORDINATES_MANIFEST_URIS = [
Expand Down
18 changes: 4 additions & 14 deletions asdf_astropy/resources/manifests/astropy-1.2.0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,8 @@ tags:
title: NdarrayMixin column.
description: |-
Represents an astropy.table.NdarrayMixin instance.
- tag_uri: tag:astropy.org:astropy/nddata/unknownuncertainty-1.0.0
schema_uri: http://astropy.org/schemas/astropy/nddata/unknownuncertainty-1.0.0
title: Represent the nddata.uncertainty.UnknownUncertainty object
- tag_uri: tag:astropy.org:astropy/nddata/uncertainty-1.0.0
schema_uri: http://astropy.org/schemas/astropy/nddata/uncertainty-1.0.0
title: Represent an astropy.nddata uncertainty
description: |-
Represents an instance of the nddata.uncertainty.UnknownUncertainty object
- tag_uri: tag:astropy.org:astropy/nddata/stddevuncertainty-1.0.0
schema_uri: http://astropy.org/schemas/astropy/nddata/stddevuncertainty-1.0.0
title: Represent the nddata.uncertainty.StdDevUncertainty object
description: |-
Represents an instance of the nddata.uncertainty.StdDevUncertainty object
- tag_uri: tag:astropy.org:astropy/nddata/varianceuncertainty-1.0.0
schema_uri: http://astropy.org/schemas/astropy/nddata/varianceuncertainty-1.0.0
title: Represent the nddata.uncertainty.VarianceUncertainty object
description: |-
Represents an instance of the nddata.uncertainty.VarianceUncertainty object
Represents an instance of an astropy.nddata uncertainty
18 changes: 4 additions & 14 deletions asdf_astropy/resources/manifests/astropy-1.3.0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,8 @@ tags:
title: NdarrayMixin column.
description: |-
Represents an astropy.table.NdarrayMixin instance.
- tag_uri: tag:astropy.org:astropy/nddata/unknownuncertainty-1.0.0
schema_uri: http://astropy.org/schemas/astropy/nddata/unknownuncertainty-1.0.0
title: Represent the nddata.uncertainty.UnknownUncertainty object
- tag_uri: tag:astropy.org:astropy/nddata/uncertainty-1.0.0
schema_uri: http://astropy.org/schemas/astropy/nddata/uncertainty-1.0.0
title: Represent an astropy.nddata uncertainty
description: |-
Represents an instance of the nddata.uncertainty.UnknownUncertainty object
- tag_uri: tag:astropy.org:astropy/nddata/stddevuncertainty-1.0.0
schema_uri: http://astropy.org/schemas/astropy/nddata/stddevuncertainty-1.0.0
title: Represent the nddata.uncertainty.StdDevUncertainty object
description: |-
Represents an instance of the nddata.uncertainty.StdDevUncertainty object
- tag_uri: tag:astropy.org:astropy/nddata/varianceuncertainty-1.0.0
schema_uri: http://astropy.org/schemas/astropy/nddata/varianceuncertainty-1.0.0
title: Represent the nddata.uncertainty.VarianceUncertainty object
description: |-
Represents an instance of the nddata.uncertainty.VarianceUncertainty object
Represents an instance of an astropy.nddata uncertainty
20 changes: 0 additions & 20 deletions asdf_astropy/resources/schemas/nddata/stddevuncertainty-1.0.0.yaml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
$schema: "http://stsci.edu/schemas/yaml-schema/draft-01"
id: "http://astropy.org/schemas/astropy/nddata/unknownuncertainty-1.0.0"

title: Represents the astropy.nddata.UnknownUncertainty class
title: Represents an astropy.nddata uncertainty class

description: >-
This object represents the `UnknownUncertainty` class, which is a subclass
of the `NDUncertainty` class from `astropy.nddata`
This object represents an uncertainty.
allOf:
- type: object
properties:
name:
enum: ["stddev", "unknown", "variance"]
array:
type: object
unit:
anyOf:
- tag: "tag:stsci.edu:asdf/unit/unit-*"
- tag: "tag:astropy.org:astropy/units/unit-1.*"
required: [name, array]

This file was deleted.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ extend-ignore = [
# Individually ignored checks
"SLF001", # private-member-access
"FBT001", # boolean positional arguments in function definition
"RUF012", # mutable class attributes should be annotated with typing.ClassVar
]
extend-exclude = ["docs/*"]

Expand Down

0 comments on commit 34da76e

Please sign in to comment.