Exemplo n.º 1
0
    def from_tree_tagged(cls, node, ctx):
        warnings.warn(create_asdf_deprecation_warning())

        frame = cls._tag_to_frame(node._tag)

        data = node.get('data', None)
        if data is not None:
            return frame(node['data'], **node['frame_attributes'])

        return frame(**node['frame_attributes'])
Exemplo n.º 2
0
def read_table(filename, data_key=None, find_table=None, **kwargs):
    """
    Read a `~astropy.table.Table` object from an ASDF file

    This requires `asdf <https://pypi.org/project/asdf/>`_ to be installed.
    By default, this function will look for a Table object with the key of
    ``data`` in the top-level ASDF tree. The parameters ``data_key`` and
    ``find_key`` can be used to override the default behavior.

    This function is registered as the Table reader for ASDF files with the
    unified I/O interface.

    Parameters
    ----------
    filename : str or :class:`py.lath:local`
        Name of the file to be read
    data_key : str
        Optional top-level key to use for finding the Table in the tree. If not
        provided, uses ``data`` by default. Use of this parameter is not
        compatible with ``find_table``.
    find_table : function
        Optional function to be used for locating the Table in the tree. The
        function takes a single parameter, which is a dictionary representing
        the top of the ASDF tree. The function must return a
        `~astropy.table.Table` instance.

    Returns
    -------
    table : `~astropy.table.Table`
        `~astropy.table.Table` instance
    """
    warnings.warn(create_asdf_deprecation_warning())

    try:
        import asdf
    except ImportError:
        raise Exception(
            "The asdf module is required to read and write ASDF files")

    if data_key and find_table:
        raise ValueError(
            "Options 'data_key' and 'find_table' are not compatible")

    with asdf.open(filename, **kwargs) as af:
        if find_table:
            return find_table(af.tree)
        else:
            return af[data_key or 'data']
Exemplo n.º 3
0
def write_table(table, filename, data_key=None, make_tree=None, **kwargs):
    """
    Write a `~astropy.table.Table` object to an ASDF file.

    This requires `asdf <https://pypi.org/project/asdf/>`_ to be installed.
    By default, this function will write a Table object in the top-level ASDF
    tree using the key of ``data``. The parameters ``data_key`` and
    ``make_tree`` can be used to override the default behavior.

    This function is registered as the Table writer for ASDF files with the
    unified I/O interface.

    Parameters
    ----------
    table : `~astropy.table.Table`
        `~astropy.table.Table` instance to be written
    filename : str or :class:`py.path:local`
        Name of the new ASDF file to be created
    data_key : str
        Optional top-level key in the ASDF tree to use when writing the Table.
        If not provided, uses ``data`` by default. Use of this parameter is not
        compatible with ``make_tree``.
    make_tree : function
        Optional function to be used for creating the ASDF tree. The function
        takes a single parameter, which is the `~astropy.table.Table` instance
        to be written. The function must return a `dict` representing the ASDF
        tree to be created.
    """
    warnings.warn(create_asdf_deprecation_warning())

    try:
        import asdf
    except ImportError:
        raise Exception(
            "The asdf module is required to read and write ASDF files")

    if data_key and make_tree:
        raise ValueError(
            "Options 'data_key' and 'make_tree' are not compatible")

    if make_tree:
        tree = make_tree(table)
    else:
        tree = {data_key or 'data': table}

    with asdf.AsdfFile(tree) as af:
        af.write_to(filename, **kwargs)
Exemplo n.º 4
0
    def to_tree_tagged(cls, frame, ctx):
        warnings.warn(create_asdf_deprecation_warning())

        if type(frame) not in frame_transform_graph.frame_set:
            raise ValueError("Can only save frames that are registered with the "
                             "transformation graph.")

        node = {}
        if frame.has_data:
            node['data'] = frame.data
        frame_attributes = {}
        for attr in frame.frame_attributes.keys():
            value = getattr(frame, attr, None)
            if value is not None:
                frame_attributes[attr] = value
        node['frame_attributes'] = frame_attributes

        return tagged.tag_object(cls._frame_name_to_tag(frame.name), node, ctx=ctx)
Exemplo n.º 5
0
    def from_tree_tagged(cls, node, ctx):
        warnings.warn(create_asdf_deprecation_warning())

        tag = node._tag[node._tag.rfind('/') + 1:]
        tag = tag[:tag.rfind('-')]
        oper = _tag_to_method_mapping[tag]
        left = node['forward'][0]
        if not isinstance(left, Model):
            raise TypeError(f"Unknown model type '{node['forward'][0]._tag}'")
        right = node['forward'][1]
        if (not isinstance(right, Model)
                and not (oper == 'fix_inputs' and isinstance(right, dict))):
            raise TypeError(f"Unknown model type '{node['forward'][1]._tag}'")
        if oper == 'fix_inputs':
            right = dict(zip(right['keys'], right['values']))
            model = CompoundModel('fix_inputs', left, right)
        else:
            model = getattr(left, oper)(right)

        return cls._from_tree_base_transform_members(model, node, ctx)
Exemplo n.º 6
0
    def to_tree_tagged(cls, model, ctx):
        warnings.warn(create_asdf_deprecation_warning())

        left = model.left

        if isinstance(model.right, dict):
            right = {
                'keys': list(model.right.keys()),
                'values': list(model.right.values())
            }
        else:
            right = model.right

        node = {'forward': [left, right]}

        try:
            tag_name = 'transform/' + _operator_to_tag_mapping[model.op]
        except KeyError:
            raise ValueError(f"Unknown operator '{model.op}'")

        node = tagged.tag_object(cls.make_yaml_tag(tag_name), node, ctx=ctx)

        return cls._to_tree_base_transform_members(model, node, ctx)
Exemplo n.º 7
0
 def from_tree_tagged(cls, tree, ctx):
     warnings.warn(create_asdf_deprecation_warning())
     return super().from_tree_tagged(tree, ctx)
Exemplo n.º 8
0
 def to_tree_tagged(cls, node, ctx):
     warnings.warn(create_asdf_deprecation_warning())
     return super().to_tree_tagged(node, ctx)