| 1 |
from asdf.extension import Converter |
| 2 |
|
| 3 |
from .core import TransformConverterBase |
| 4 |
|
| 5 |
|
| 6 |
class IdentityConverter(TransformConverterBase): |
| 7 |
""" |
| 8 |
ASDF support for serializing the Identity model. |
| 9 |
""" |
| 10 |
|
| 11 |
tags = ("tag:stsci.edu:asdf/transform/identity-*",) |
| 12 |
types = ("astropy.modeling.mappings.Identity",) |
| 13 |
|
| 14 |
def to_yaml_tree_transform(self, model, tag, ctx): |
| 15 |
node = {} |
| 16 |
if model.n_inputs != 1: |
| 17 |
node["n_dims"] = model.n_inputs |
| 18 |
return node |
| 19 |
|
| 20 |
def from_yaml_tree_transform(self, node, tag, ctx): |
| 21 |
from astropy.modeling.mappings import Identity |
| 22 |
|
| 23 |
return Identity(node.get("n_dims", 1)) |
| 24 |
|
| 25 |
|
| 26 |
class RemapAxesConverter(TransformConverterBase): |
| 27 |
""" |
| 28 |
ASDF support for serializing the Mapping model |
| 29 |
""" |
| 30 |
|
| 31 |
tags = ("tag:stsci.edu:asdf/transform/remap_axes-*",) |
| 32 |
types = ("astropy.modeling.mappings.Mapping",) |
| 33 |
|
| 34 |
def to_yaml_tree_transform(self, model, tag, ctx): |
| 35 |
node = {"mapping": list(model.mapping)} |
| 36 |
if model.n_inputs > max(model.mapping) + 1: |
| 37 |
node["n_inputs"] = model.n_inputs |
| 38 |
return node |
| 39 |
|
| 40 |
def from_yaml_tree_transform(self, node, tag, ctx): |
| 41 |
from astropy.modeling.models import Mapping |
| 42 |
|
| 43 |
return Mapping(tuple(node["mapping"]), node.get("n_inputs")) |
| 44 |
|
| 45 |
|
| 46 |
class UnitsMappingConverter(Converter): |
| 47 |
""" |
| 48 |
ASDF support for serializing the UnitsMapping model. |
| 49 |
Note that this converter does not inherit from TransformConverterBase, |
| 50 |
because the inputs and outputs are written differently |
| 51 |
from other models. |
| 52 |
""" |
| 53 |
|
| 54 |
tags = ("tag:astropy.org:astropy/transform/units_mapping-*",) |
| 55 |
types = ("astropy.modeling.mappings.UnitsMapping",) |
| 56 |
|
| 57 |
def to_yaml_tree(self, model, tag, ctx): |
| 58 |
node = {} |
| 59 |
|
| 60 |
if model.name is not None: |
| 61 |
node["name"] = model.name |
| 62 |
|
| 63 |
inputs = [] |
| 64 |
outputs = [] |
| 65 |
for i, o, m in zip(model.inputs, model.outputs, model.mapping): |
| 66 |
input_ = { |
| 67 |
"name": i, |
| 68 |
"allow_dimensionless": model.input_units_allow_dimensionless[i], |
| 69 |
} |
| 70 |
if m[0] is not None: |
| 71 |
input_["unit"] = m[0] |
| 72 |
if model.input_units_equivalencies is not None and i in model.input_units_equivalencies: |
| 73 |
input_["equivalencies"] = model.input_units_equivalencies[i] |
| 74 |
inputs.append(input_) |
| 75 |
|
| 76 |
output = { |
| 77 |
"name": o, |
| 78 |
} |
| 79 |
if m[-1] is not None: |
| 80 |
output["unit"] = m[-1] |
| 81 |
outputs.append(output) |
| 82 |
|
| 83 |
node["unit_inputs"] = inputs |
| 84 |
node["unit_outputs"] = outputs |
| 85 |
|
| 86 |
return node |
| 87 |
|
| 88 |
def from_yaml_tree(self, node, tag, ctx): |
| 89 |
from astropy.modeling.mappings import UnitsMapping |
| 90 |
|
| 91 |
mapping = tuple((i.get("unit"), o.get("unit")) for i, o in zip(node["unit_inputs"], node["unit_outputs"])) |
| 92 |
|
| 93 |
equivalencies = None |
| 94 |
for i in node["unit_inputs"]: |
| 95 |
if "equivalencies" in i: |
| 96 |
if equivalencies is None: |
| 97 |
equivalencies = {} |
| 98 |
equivalencies[i["name"]] = i["equivalencies"] |
| 99 |
|
| 100 |
kwargs = { |
| 101 |
"input_units_equivalencies": equivalencies, |
| 102 |
"input_units_allow_dimensionless": { |
| 103 |
i["name"]: i.get("allow_dimensionless", False) for i in node["unit_inputs"] |
| 104 |
}, |
| 105 |
} |
| 106 |
|
| 107 |
if "name" in node: |
| 108 |
kwargs["name"] = node["name"] |
| 109 |
|
| 110 |
return UnitsMapping(mapping, **kwargs) |