How to use the asdf.asdftypes.AsdfType function in asdf

To help you get started, we’ve selected a few asdf 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 / asdf / asdf / tags / unit / unit.py View on Github external
# Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-

from __future__ import absolute_import, division, unicode_literals, print_function


import six

from ...asdftypes import AsdfType


class UnitType(AsdfType):
    name = 'unit/unit'
    types = ['astropy.units.UnitBase']
    requires = ['astropy']

    @classmethod
    def to_tree(cls, node, ctx):
        from astropy.units import Unit, UnitBase

        if isinstance(node, six.string_types):
            node = Unit(node, format='vounit', parse_strict='warn')
        if isinstance(node, UnitBase):
            return node.to_string(format='vounit')
        raise TypeError("'{0}' is not a valid unit".format(node))

    @classmethod
    def from_tree(cls, node, ctx):
github spacetelescope / asdf / asdf / tags / core / constant.py View on Github external
# -*- coding: utf-8 -*-


from ...asdftypes import AsdfType


class Constant(object):
    def __init__(self, value):
        self._value = value

    @property
    def value(self):
        return self._value


class ConstantType(AsdfType):
    name = 'core/constant'
    types = [Constant]

    @classmethod
    def from_tree(self, node, ctx):
        return Constant(node)

    @classmethod
    def to_tree(self, data, ctx):
        return data.value
github spacetelescope / asdf / asdf / tags / wcs / wcs.py View on Github external
return {'name': gwcs.name,
                'steps': yamlutil.custom_tree_to_tagged_tree(steps, ctx)}

    @classmethod
    def assert_equal(cls, old, new):
        from ...tests import helpers

        assert old.name == new.name
        assert len(old.available_frames) == len(new.available_frames)
        for (old_frame, old_transform), (new_frame, new_transform) in zip(
                old.pipeline, new.pipeline):
            helpers.assert_tree_match(old_frame, new_frame)
            helpers.assert_tree_match(old_transform, new_transform)


class StepType(dict, AsdfType):
    name = "wcs/step"
    requires = _REQUIRES


class FrameType(AsdfType):
    name = "wcs/frame"
    version = '1.1.0'
    # We require a specific version of astropy in order to make use of
    # CartesianDifferential
    requires = ['gwcs', 'astropy-1.3.3']
    types = ['gwcs.Frame2D']

    @classmethod
    def _get_reference_frame_mapping(cls):
        if hasattr(cls, '_reference_frame_mapping'):
            return cls._reference_frame_mapping
github spacetelescope / asdf / asdf / tags / transform / basic.py View on Github external
HAS_ASTROPY = False
else:
    HAS_ASTROPY = True
    from astropy.modeling import mappings
    from astropy.utils import minversion
    ASTROPY_12 = minversion(astropy, "1.2")

from ...asdftypes import AsdfType
from ... import tagged
from ... import yamlutil

__all__ = ['TransformType', 'IdentityType', 'ConstantType',
           'DomainType']


class TransformType(AsdfType):
    version = '1.1.0'
    requires = ['astropy']

    @classmethod
    def _from_tree_base_transform_members(cls, model, node, ctx):
        if 'inverse' in node:
            model.inverse = yamlutil.tagged_tree_to_custom_tree(
                node['inverse'], ctx)

        if 'name' in node:
            model = model.rename(node['name'])

        # TODO: Remove domain in a later version.
        if 'domain' in node:
            model.bounding_box = cls._domain_to_bounding_box(node['domain'])
        elif 'bounding_box' in node:
github spacetelescope / asdf / asdf / tags / core / table.py View on Github external
# Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-

from __future__ import absolute_import, division, unicode_literals, print_function

import numpy as np

from ...asdftypes import AsdfType
from ... import yamlutil


class TableType(AsdfType):
    name = 'core/table'
    types = ['astropy.table.Table']
    requires = ['astropy']

    @classmethod
    def from_tree(cls, node, ctx):
        from astropy import table

        columns = [
            yamlutil.tagged_tree_to_custom_tree(c, ctx)
            for c in node['columns']
        ]

        return table.Table(columns, meta=node.get('meta', {}))

    @classmethod
github spacetelescope / asdf / asdf / tags / fits / fits.py View on Github external
# Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-

from __future__ import absolute_import, division, unicode_literals, print_function

import numpy as np

from ...asdftypes import AsdfType
from ... import yamlutil


class FitsType(AsdfType):
    name = 'fits/fits'
    types = ['astropy.io.fits.HDUList']
    requires = ['astropy']

    @classmethod
    def from_tree(cls, data, ctx):
        from astropy.io import fits

        hdus = []
        first = True
        for hdu_entry in data:
            header = fits.Header([fits.Card(*x) for x in hdu_entry['header']])
            data = hdu_entry.get('data')
            if data is not None:
                try:
                    data = data.__array__()
github spacetelescope / asdf / asdf / tags / wcs / wcs.py View on Github external
from ...tests import helpers

        assert old.name == new.name
        assert len(old.available_frames) == len(new.available_frames)
        for (old_frame, old_transform), (new_frame, new_transform) in zip(
                old.pipeline, new.pipeline):
            helpers.assert_tree_match(old_frame, new_frame)
            helpers.assert_tree_match(old_transform, new_transform)


class StepType(dict, AsdfType):
    name = "wcs/step"
    requires = _REQUIRES


class FrameType(AsdfType):
    name = "wcs/frame"
    version = '1.1.0'
    # We require a specific version of astropy in order to make use of
    # CartesianDifferential
    requires = ['gwcs', 'astropy-1.3.3']
    types = ['gwcs.Frame2D']

    @classmethod
    def _get_reference_frame_mapping(cls):
        if hasattr(cls, '_reference_frame_mapping'):
            return cls._reference_frame_mapping

        from astropy.coordinates import builtin_frames

        cls._reference_frame_mapping = {
            'ICRS': builtin_frames.ICRS,
github spacetelescope / asdf / asdf / tags / transform / basic.py View on Github external
types = ['astropy.modeling.functional_models.Const1D']

    @classmethod
    def from_tree_transform(cls, node, ctx):
        from astropy.modeling import functional_models

        return functional_models.Const1D(node['value'])

    @classmethod
    def to_tree_transform(cls, data, ctx):
        return {
            'value': data.amplitude.value
        }


class DomainType(AsdfType):
    name = "transform/domain"

    @classmethod
    def from_tree(cls, node, ctx):
        return node

    @classmethod
    def to_tree(cls, data, ctx):
        return data


# TODO: This is just here for bootstrapping and will go away eventually
if HAS_ASTROPY:
    class GenericModel(mappings.Mapping):
        def __init__(self, n_inputs, n_outputs):
            mapping = tuple(range(n_inputs))
github spacetelescope / asdf / asdf / tags / time / time.py View on Github external
from ...asdftypes import AsdfType
from ... import yamlutil


_guessable_formats = set(['iso', 'byear', 'jyear', 'yday'])


_astropy_format_to_asdf_format = {
    'isot': 'iso',
    'byear_str': 'byear',
    'jyear_str': 'jyear'
}


class TimeType(AsdfType):
    name = 'time/time'
    types = ['astropy.time.core.Time']
    requires = ['astropy']

    @classmethod
    def to_tree(cls, node, ctx):
        from astropy import time

        format = node.format

        if format == 'byear':
            node = time.Time(node, format='byear_str')

        elif format == 'jyear':
            node = time.Time(node, format='jyear_str')
github spacetelescope / asdf / asdf / tags / core / table.py View on Github external
node = {'columns': columns}
        if data.meta:
            node['meta'] = data.meta

        return node

    @classmethod
    def assert_equal(cls, old, new):
        from .ndarray import NDArrayType

        assert old.meta == new.meta
        NDArrayType.assert_equal(np.array(old), np.array(new))


class ColumnType(AsdfType):
    name = 'core/column'
    types = ['astropy.table.Column', 'astropy.table.MaskedColumn']
    requires = ['astropy']
    handle_dynamic_subclasses = True

    @classmethod
    def from_tree(cls, node, ctx):
        from astropy import table

        data = yamlutil.tagged_tree_to_custom_tree(
            node['data'], ctx)
        name = node['name']
        description = node.get('description')
        unit = node.get('unit')
        meta = node.get('meta', None)