| 1 |
from asdf.extension import Converter |
| 2 |
from asdf.tags.core.ndarray import NDArrayType |
| 3 |
|
| 4 |
|
| 5 |
class ColumnConverter(Converter): |
| 6 |
tags = ( |
| 7 |
"tag:stsci.edu:asdf/core/column-*", |
| 8 |
"tag:stsci.edu:asdf/table/column-*", |
| 9 |
) |
| 10 |
types = ( |
| 11 |
"astropy.table.column.Column", |
| 12 |
"astropy.table.column.MaskedColumn", |
| 13 |
) |
| 14 |
|
| 15 |
def to_yaml_tree(self, obj, tag, ctx): |
| 16 |
node = {"data": obj.data, "name": obj.name} |
| 17 |
|
| 18 |
if obj.description: |
| 19 |
node["description"] = obj.description |
| 20 |
|
| 21 |
if obj.unit: |
| 22 |
node["unit"] = obj.unit |
| 23 |
|
| 24 |
if obj.meta: |
| 25 |
node["meta"] = obj.meta |
| 26 |
|
| 27 |
return node |
| 28 |
|
| 29 |
def from_yaml_tree(self, node, tag, ctx): |
| 30 |
from astropy.table import Column, MaskedColumn |
| 31 |
from numpy.ma.core import MaskedArray |
| 32 |
|
| 33 |
data = node["data"] |
| 34 |
if isinstance(data, NDArrayType): |
| 35 |
# TODO: Why doesn't NDArrayType work? This needs some research |
| 36 |
# and documentation. |
| 37 |
data = data._make_array() |
| 38 |
|
| 39 |
column_class = MaskedColumn if isinstance(data, MaskedArray) else Column |
| 40 |
|
| 41 |
return column_class( |
| 42 |
data=data, |
| 43 |
name=node["name"], |
| 44 |
description=node.get("description"), |
| 45 |
unit=node.get("unit"), |
| 46 |
meta=node.get("meta"), |
| 47 |
) |
| 48 |
|
| 49 |
|
| 50 |
class AsdfTableConverter(Converter): |
| 51 |
tags = ( |
| 52 |
"tag:stsci.edu:asdf/core/table-*", |
| 53 |
"tag:stsci.edu:asdf/table/table-*", |
| 54 |
) |
| 55 |
types = () |
| 56 |
|
| 57 |
def to_yaml_tree(self, obj, tag, ctx): |
| 58 |
msg = "astropy does not support writing astropy.table.Table with the ASDF table-1.0.0 tag" |
| 59 |
raise NotImplementedError(msg) |
| 60 |
|
| 61 |
def from_yaml_tree(self, node, tag, ctx): |
| 62 |
from astropy.table import Table |
| 63 |
|
| 64 |
return Table(node["columns"], meta=node.get("meta")) |
| 65 |
|
| 66 |
|
| 67 |
class AstropyTableConverter(Converter): |
| 68 |
tags = ("tag:astropy.org:astropy/table/table-*",) |
| 69 |
types = ( |
| 70 |
"astropy.table.table.Table", |
| 71 |
"astropy.table.table.QTable", |
| 72 |
) |
| 73 |
|
| 74 |
def to_yaml_tree(self, obj, tag, ctx): |
| 75 |
from astropy.table import QTable |
| 76 |
|
| 77 |
node = { |
| 78 |
"columns": [obj[name] for name in obj.colnames], |
| 79 |
"colnames": obj.colnames, |
| 80 |
"qtable": isinstance(obj, QTable), |
| 81 |
} |
| 82 |
|
| 83 |
if obj.meta: |
| 84 |
node["meta"] = obj.meta |
| 85 |
|
| 86 |
return node |
| 87 |
|
| 88 |
def from_yaml_tree(self, node, tag, ctx): |
| 89 |
from astropy.table import QTable, Table |
| 90 |
|
| 91 |
table = QTable(meta=node.get("meta")) if node.get("qtable", False) else Table(meta=node.get("meta")) |
| 92 |
|
| 93 |
for name, column in zip(node["colnames"], node["columns"]): |
| 94 |
table[name] = column |
| 95 |
|
| 96 |
return table |
| 97 |
|
| 98 |
|
| 99 |
class NdarrayMixinConverter(Converter): |
| 100 |
tags = ("tag:astropy.org:astropy/table/ndarraymixin-*",) |
| 101 |
types = ("astropy.table.ndarray_mixin.NdarrayMixin",) |
| 102 |
|
| 103 |
def to_yaml_tree(self, obj, tag, ctx): |
| 104 |
import numpy as np |
| 105 |
|
| 106 |
return {"array": np.asarray(obj)} |
| 107 |
|
| 108 |
def from_yaml_tree(self, node, tag, ctx): |
| 109 |
from astropy.table import NdarrayMixin |
| 110 |
|
| 111 |
arr = node["array"] |
| 112 |
|
| 113 |
# this will trigger reading the ASDF block that contains the array data |
| 114 |
return arr.view(NdarrayMixin) |