Skip to content

Commit

Permalink
make import lazy for asdf extension
Browse files Browse the repository at this point in the history
  • Loading branch information
braingram committed Jul 5, 2024
1 parent 53025a9 commit 6b5c599
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
14 changes: 9 additions & 5 deletions gwcs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,12 @@
# package is not installed
pass # pragma: no cover


from .wcs import * # noqa
from .wcstools import * # noqa
from .coordinate_frames import * # noqa
from .selector import * # noqa
def __getattr__(name):
for sub_module in ("wcs", "wcstools", "coordinate_frames", "selector"):
module = __import__(f"gwcs.{sub_module}", fromlist=['*'])
globals().update({name: getattr(module, name) for name in module.__all__})

try:
return globals()[name]
except KeyError:
raise AttributeError(f"module 'gwcs' has no attribute '{name}'") from None
13 changes: 8 additions & 5 deletions gwcs/converters/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
# -*- coding: utf-8 -*-

from collections import OrderedDict
import numpy as np
from astropy.modeling import models
from astropy.modeling.core import Model
from astropy.utils.misc import isiterable

from asdf.tags.core.ndarray import NDArrayType
from asdf_astropy.converters.transform.core import TransformConverterBase


Expand All @@ -20,6 +15,12 @@ class LabelMapperConverter(TransformConverterBase):
"gwcs.selector.LabelMapperRange", "gwcs.selector.LabelMapper"]

def from_yaml_tree_transform(self, node, tag, ctx):
from asdf.tags.core.ndarray import NDArrayType
from astropy.modeling import models
from astropy.modeling.core import Model
from astropy.utils.misc import isiterable
import numpy as np

from ..selector import (LabelMapperArray, LabelMapperDict,
LabelMapperRange, LabelMapper)
inputs_mapping = node.get('inputs_mapping', None)
Expand Down Expand Up @@ -52,6 +53,7 @@ def from_yaml_tree_transform(self, node, tag, ctx):
return LabelMapperDict(inputs, dict_mapper, inputs_mapping, atol=atol)

def to_yaml_tree_transform(self, model, tag, ctx):
from astropy.utils.misc import isiterable
from ..selector import (LabelMapperArray, LabelMapperDict,
LabelMapperRange, LabelMapper)
node = OrderedDict()
Expand Down Expand Up @@ -91,6 +93,7 @@ class RegionsSelectorConverter(TransformConverterBase):

def from_yaml_tree_transform(self, node, tag, ctx):
from ..selector import RegionsSelector

inputs = node['inputs']
outputs = node['outputs']
label_mapper = node['label_mapper']
Expand Down
2 changes: 1 addition & 1 deletion gwcs/converters/spectroscopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
ASDF tags for spectroscopy related models.
"""
from astropy import units as u
from asdf_astropy.converters.transform.core import (
TransformConverterBase, parameter_to_value
)
Expand Down Expand Up @@ -84,6 +83,7 @@ def from_yaml_tree_transform(self, node, tag, ctx):
return model

def to_yaml_tree_transform(self, model, tag, ctx):
from astropy import units as u
from ..spectroscopy import (AnglesFromGratingEquation3D,
WavelengthFromGratingEquation)
if model.groove_density.unit is not None:
Expand Down

0 comments on commit 6b5c599

Please sign in to comment.