Debian Codegraph @

asdf_astropy/converters/transform/tabular.py

raw
1 from .core import TransformConverterBase
2
3 __all__ = ["TabularConverter"]
4
5
6 class TabularConverter(TransformConverterBase):
7 """
8 ASDF support for serializing tabular models.
9 """
10
11 tags = ("tag:stsci.edu:asdf/transform/tabular-*",)
12 types = (
13 "astropy.modeling.tabular.Tabular1D",
14 "astropy.modeling.tabular.Tabular2D",
15 )
16
17 def to_yaml_tree_transform(self, model, tag, ctx):
18 node = {}
19 if model.fill_value is not None:
20 node["fill_value"] = model.fill_value
21 node["lookup_table"] = model.lookup_table
22 node["points"] = list(model.points)
23 node["method"] = str(model.method)
24 node["bounds_error"] = model.bounds_error
25
26 return node
27
28 def from_yaml_tree_transform(self, node, tag, ctx):
29 from astropy.modeling import tabular
30
31 lookup_table = node.pop("lookup_table")
32 dim = lookup_table.ndim
33 fill_value = node.pop("fill_value", None)
34 if dim == 1:
35 points = (node["points"][0],)
36 model = tabular.Tabular1D(
37 points=points,
38 lookup_table=lookup_table,
39 method=node["method"],
40 bounds_error=node["bounds_error"],
41 fill_value=fill_value,
42 )
43 elif dim == 2: # noqa: PLR2004
44 points = tuple(node["points"])
45 model = tabular.Tabular2D(
46 points=points,
47 lookup_table=lookup_table,
48 method=node["method"],
49 bounds_error=node["bounds_error"],
50 fill_value=fill_value,
51 )
52 else:
53 msg = "tabular models with ndim > 2 are not supported "
54 raise NotImplementedError(msg)
55
56 return model