How to use the gwcs.gwcs_types.GWCSTransformType function in gwcs

To help you get started, we’ve selected a few gwcs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github spacetelescope / gwcs / gwcs / tags / spectroscopy_models.py View on Github external
from numpy.testing import assert_allclose

from astropy import units as u
from astropy.units import allclose
from asdf import yamlutil

from ..gwcs_types import GWCSTransformType
from .. spectroscopy import *
from . import _parameter_to_value


__all__ = ['GratingEquationType', 'SellmeierGlassType',
           'SellmeierZemaxType', 'Snell3D']


class SellmeierGlassType(GWCSTransformType):
    name = "sellmeier_glass"
    types = [SellmeierGlass]
    version = "1.1.0"

    @classmethod
    def from_tree_transform(cls, node, ctx):
        return SellmeierGlass(node['B_coef'], node['C_coef'])

    @classmethod
    def to_tree_transform(cls, model, ctx):
        node = {'B_coef': _parameter_to_value(model.B_coef),
                'C_coef': _parameter_to_value(model.C_coef)}
        return yamlutil.custom_tree_to_tagged_tree(node, ctx)


class SellmeierZemaxType(GWCSTransformType):
github spacetelescope / gwcs / gwcs / tags / geometry_models.py View on Github external
else:
            raise TypeError(f"Unknown model_type {transform_type}")

    @classmethod
    def to_tree_transform(cls, model, ctx):
        if isinstance(model, FromDirectionCosines):
            transform_type = 'from_direction_cosines'
        elif isinstance(model, ToDirectionCosines):
            transform_type = 'to_direction_cosines'
        else:
            raise TypeError(f"Model of type {model.__class__} is not supported.")
        node = {'transform_type': transform_type}
        return yamlutil.custom_tree_to_tagged_tree(node, ctx)


class SphericalCartesianType(GWCSTransformType):
    name = "spherical_cartesian"
    types = [SphericalToCartesian, CartesianToSpherical]
    version = "1.1.0"

    @classmethod
    def from_tree_transform(cls, node, ctx):
        transform_type = node['transform_type']
        wrap_lon_at = node['wrap_lon_at']
        if transform_type == 'spherical_to_cartesian':
            return SphericalToCartesian(wrap_lon_at=wrap_lon_at)
        elif transform_type == 'cartesian_to_spherical':
            return CartesianToSpherical(wrap_lon_at=wrap_lon_at)
        else:
            raise TypeError(f"Unknown model_type {transform_type}")

    @classmethod
github spacetelescope / gwcs / gwcs / tags / geometry_models.py View on Github external
"""
ASDF tags for geometry related models.
"""
from asdf import yamlutil
from ..gwcs_types import GWCSTransformType
from .. geometry import (ToDirectionCosines, FromDirectionCosines,
                         SphericalToCartesian, CartesianToSpherical)


__all__ = ['DirectionCosinesType', 'SphericalCartesianType']


class DirectionCosinesType(GWCSTransformType):
    name = "direction_cosines"
    types = [ToDirectionCosines, FromDirectionCosines]
    version = "1.1.0"

    @classmethod
    def from_tree_transform(cls, node, ctx):
        transform_type = node['transform_type']
        if transform_type == 'to_direction_cosines':
            return ToDirectionCosines()
        elif transform_type == 'from_direction_cosines':
            return FromDirectionCosines()
        else:
            raise TypeError(f"Unknown model_type {transform_type}")

    @classmethod
    def to_tree_transform(cls, model, ctx):
github spacetelescope / gwcs / gwcs / tags / selectortags.py View on Github external
def assert_equal(cls, a, b):
        # TODO: If models become comparable themselves, remove this.
        assert (a.__class__ == b.__class__) # nosec
        if isinstance(a.mapper, dict):
            assert(a.mapper.__class__ == b.mapper.__class__) # nosec
            assert(all(np.in1d(list(a.mapper), list(b.mapper)))) # nosec
            for k in a.mapper:
                assert (a.mapper[k].__class__ == b.mapper[k].__class__) # nosec
                assert(all(a.mapper[k].parameters == b.mapper[k].parameters))  # nosec
            assert (a.inputs == b.inputs) # nosec
            assert (a.inputs_mapping.mapping == b.inputs_mapping.mapping) # nosec
        else:
            assert_array_equal(a.mapper, b.mapper)


class RegionsSelectorType(GWCSTransformType):
    name = "regions_selector"
    types = [RegionsSelector]
    version = "1.1.0"

    @classmethod
    def from_tree_transform(cls, node, ctx):
        inputs = node['inputs']
        outputs = node['outputs']
        label_mapper = node['label_mapper']
        undefined_transform_value = node['undefined_transform_value']
        sel = node['selector']
        sel = dict(zip(sel['labels'], sel['transforms']))
        return RegionsSelector(inputs, outputs,
                               sel, label_mapper, undefined_transform_value)

    @classmethod
github spacetelescope / gwcs / gwcs / tags / spectroscopy_models.py View on Github external
node['E_coef'])

    @classmethod
    def to_tree_transform(cls, model, ctx):
        node = {'B_coef': _parameter_to_value(model.B_coef),
                'C_coef': _parameter_to_value(model.C_coef),
                'D_coef': _parameter_to_value(model.D_coef),
                'E_coef': _parameter_to_value(model.E_coef),
                'temperature': _parameter_to_value(model.temperature),
                'ref_temperature': _parameter_to_value(model.ref_temperature),
                'pressure': _parameter_to_value(model.pressure),
                'ref_pressure': _parameter_to_value(model.ref_pressure)}
        return yamlutil.custom_tree_to_tagged_tree(node, ctx)


class Snell3DType(GWCSTransformType):
    name = "snell3d"
    types = [Snell3D]
    version = "1.1.0"

    @classmethod
    def from_tree_transform(cls, node, ctx):
        return Snell3D()

    @classmethod
    def to_tree_transform(cls, model, ctx):
        return yamlutil.custom_tree_to_tagged_tree({}, ctx)


class GratingEquationType(GWCSTransformType):
    name = "grating_equation"
    version = '1.1.0'
github spacetelescope / gwcs / gwcs / tags / spectroscopy_models.py View on Github external
name = "sellmeier_glass"
    types = [SellmeierGlass]
    version = "1.1.0"

    @classmethod
    def from_tree_transform(cls, node, ctx):
        return SellmeierGlass(node['B_coef'], node['C_coef'])

    @classmethod
    def to_tree_transform(cls, model, ctx):
        node = {'B_coef': _parameter_to_value(model.B_coef),
                'C_coef': _parameter_to_value(model.C_coef)}
        return yamlutil.custom_tree_to_tagged_tree(node, ctx)


class SellmeierZemaxType(GWCSTransformType):
    name = "sellmeier_zemax"
    types = [SellmeierZemax]
    version = "1.1.0"

    @classmethod
    def from_tree_transform(cls, node, ctx):
        return SellmeierZemax(node['temperature'], node['ref_temperature'],
                              node['ref_pressure'], node['pressure'],
                              node['B_coef'], node['C_coef'], node['D_coef'],
                              node['E_coef'])

    @classmethod
    def to_tree_transform(cls, model, ctx):
        node = {'B_coef': _parameter_to_value(model.B_coef),
                'C_coef': _parameter_to_value(model.C_coef),
                'D_coef': _parameter_to_value(model.D_coef),
github spacetelescope / gwcs / gwcs / tags / selectortags.py View on Github external
from astropy.modeling import models
from astropy.modeling.core import Model
from astropy.utils.misc import isiterable

from asdf import yamlutil
from asdf.tags.core.ndarray import NDArrayType
from ..gwcs_types import GWCSTransformType


from ..selector import *


__all__ = ['LabelMapperType', 'RegionsSelectorType']


class LabelMapperType(GWCSTransformType):
    name = "label_mapper"
    types = [LabelMapperArray, LabelMapperDict, LabelMapperRange, LabelMapper]
    version = "1.1.0"

    @classmethod
    def from_tree_transform(cls, node, ctx):
        inputs_mapping = node.get('inputs_mapping', None)
        if inputs_mapping is not None and not isinstance(inputs_mapping, models.Mapping):
            raise TypeError("inputs_mapping must be an instance"
                            "of astropy.modeling.models.Mapping.")
        mapper = node['mapper']
        atol = node.get('atol', 10**-8)
        no_label = node.get('no_label', np.nan)

        if isinstance(mapper, NDArrayType):
            if mapper.ndim != 2:
github spacetelescope / gwcs / gwcs / tags / spectroscopy_models.py View on Github external
class Snell3DType(GWCSTransformType):
    name = "snell3d"
    types = [Snell3D]
    version = "1.1.0"

    @classmethod
    def from_tree_transform(cls, node, ctx):
        return Snell3D()

    @classmethod
    def to_tree_transform(cls, model, ctx):
        return yamlutil.custom_tree_to_tagged_tree({}, ctx)


class GratingEquationType(GWCSTransformType):
    name = "grating_equation"
    version = '1.1.0'
    types = [AnglesFromGratingEquation3D,
             WavelengthFromGratingEquation]

    @classmethod
    def from_tree_transform(cls, node, ctx):
        groove_density = node['groove_density']
        order = node['order']
        output = node['output']
        if output == "wavelength":
            model = WavelengthFromGratingEquation(groove_density=groove_density,
                                                  spectral_order=order)
        elif output == "angle":
            model = AnglesFromGratingEquation3D(groove_density=groove_density,
                                                spectral_order=order)