Skip to content

Commit

Permalink
Don't import astropy stuff at module scope
Browse files Browse the repository at this point in the history
  • Loading branch information
lpsinger committed Jan 28, 2025
1 parent 1898456 commit 4cd79ee
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
13 changes: 3 additions & 10 deletions asdf_astropy/converters/coordinates/angle.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
from astropy.coordinates.angles import Angle, Latitude, Longitude
from astropy.utils.masked import Masked

from asdf_astropy.converters.unit.quantity import QuantityConverter

MaskedAngle = Masked(Angle)
MaskedLatitude = Masked(Latitude)
MaskedLongitude = Masked(Longitude)


class AngleConverter(QuantityConverter):
tags = ("tag:astropy.org:astropy/coordinates/angle-*",)
types = (
"astropy.coordinates.angles.Angle",
"astropy.coordinates.angles.core.Angle",
MaskedAngle,
"astropy.utils.masked.core.MaskedAngle",
)

def from_yaml_tree(self, node, tag, ctx):
Expand All @@ -27,7 +20,7 @@ class LatitudeConverter(QuantityConverter):
types = (
"astropy.coordinates.angles.Latitude",
"astropy.coordinates.angles.core.Latitude",
MaskedLatitude,
"astropy.utils.masked.core.MaskedLatitude",
)

def from_yaml_tree(self, node, tag, ctx):
Expand All @@ -41,7 +34,7 @@ class LongitudeConverter(QuantityConverter):
types = (
"astropy.coordinates.angles.Longitude",
"astropy.coordinates.angles.core.Longitude",
MaskedLongitude,
"astropy.utils.masked.core.MaskedLongitude",
)

def to_yaml_tree(self, obj, tag, ctx):
Expand Down
11 changes: 5 additions & 6 deletions asdf_astropy/converters/unit/quantity.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import numpy as np
from asdf.extension import Converter
from asdf.tags.core.ndarray import NDArrayType
from astropy.units import Quantity
from astropy.utils.masked import Masked

MaskedQuantity = Masked(Quantity)


class QuantityConverter(Converter):
Expand All @@ -14,10 +9,13 @@ class QuantityConverter(Converter):
# The Distance class has no tag of its own, so we
# just serialize it as a quantity.
"astropy.coordinates.distances.Distance",
MaskedQuantity,
"astropy.utils.masked.core.MaskedQuantity",
)

def to_yaml_tree(self, obj, tag, ctx):
import numpy as np
from astropy.utils.masked import Masked

node = {
"value": np.ma.asarray(obj.value) if isinstance(obj, Masked) else obj.value,
"unit": obj.unit,
Expand All @@ -34,6 +32,7 @@ def from_yaml_tree(self, node, tag, ctx):
# astropy 6.1 changed Quantity in a similar way
import numpy as np
from astropy.units import Quantity
from astropy.utils.masked.core import MaskedQuantity

copy = None if np.lib.NumpyVersion(np.__version__) >= "2.0.0b1" else False

Expand Down

0 comments on commit 4cd79ee

Please sign in to comment.