Debian Codegraph @

asdf_astropy/converters/table/tests/test_table.py

raw
1 import warnings
2
3 import asdf
4 import astropy.units as u
5 import numpy as np
6 import pytest
7 from asdf.testing.helpers import yaml_to_asdf
8 from astropy.coordinates import EarthLocation, SkyCoord
9 from astropy.table import NdarrayMixin, QTable, Table
10 from astropy.time import Time, TimeDelta
11 from numpy.testing import assert_array_equal
12
13 from asdf_astropy.testing import helpers
14
15
16 def assert_description_equal(a, b):
17 message = (
18 "asdf_astropy.converters.table.tests.test_table.assert_description_equal is deprecated."
19 "Use asdf_astropy.testing.helpers.assert_description_equal instead."
20 )
21 warnings.warn(message, DeprecationWarning, stacklevel=2)
22
23 return helpers.assert_description_equal(a, b)
24
25
26 def assert_table_equal(a, b):
27 message = (
28 "asdf_astropy.converters.table.tests.test_table.assert_table_equal is deprecated."
29 "Use asdf_astropy.testing.helpers.assert_table_equal instead."
30 )
31 warnings.warn(message, DeprecationWarning, stacklevel=2)
32
33 return helpers.assert_table_equal(a, b)
34
35
36 def assert_table_roundtrip(table, tmp_path):
37 message = (
38 "asdf_astropy.converters.table.tests.test_table.assert_table_roundtrip is deprecated."
39 "Use asdf_astropy.testing.helpers.assert_table_roundtrip instead."
40 )
41 warnings.warn(message, DeprecationWarning, stacklevel=2)
42
43 return helpers.assert_table_roundtrip(table, tmp_path)
44
45
46 def test_deprecations(tmp_path):
47 rows = [(1, 2.0, "x"), (4, 5.0, "y"), (5, 8.2, "z")]
48
49 table = Table(rows=rows, names=("a", "b", "c"), dtype=("i4", "f8", "S1"))
50 table.columns["a"].description = "RA"
51 table.columns["a"].unit = "degree"
52 table.columns["a"].meta = {"foo": "bar"}
53 table.columns["c"].description = "Some description of some sort"
54
55 # Test assert_description_equal deprecation
56 with pytest.warns(DeprecationWarning, match=".*assert_description_equal.*"):
57 assert_description_equal("a", "a")
58
59 # Test assert_table_equal deprecation
60 with pytest.warns(DeprecationWarning, match=".*assert_table_equal.*"):
61 assert_table_equal(table, table)
62
63 # Test assert_table_roundtrip deprecation
64 with pytest.warns(DeprecationWarning, match=".*assert_table_roundtrip.*"):
65 assert_table_roundtrip(table, tmp_path)
66
67
68 def test_table(tmp_path):
69 rows = [(1, 2.0, "x"), (4, 5.0, "y"), (5, 8.2, "z")]
70
71 table = Table(rows=rows, names=("a", "b", "c"), dtype=("i4", "f8", "S1"))
72 table.columns["a"].description = "RA"
73 table.columns["a"].unit = "degree"
74 table.columns["a"].meta = {"foo": "bar"}
75 table.columns["c"].description = "Some description of some sort"
76
77 helpers.assert_table_roundtrip(table, tmp_path)
78
79
80 def test_array_columns(tmp_path):
81 data = np.array(
82 [([[1, 2], [3, 4]], 2.0, "x"), ([[5, 6], [7, 8]], 5.0, "y"), ([[9, 10], [11, 12]], 8.2, "z")],
83 dtype=[("a", "<i4", (2, 2)), ("b", "<f8"), ("c", "|S1")],
84 )
85
86 table = Table(data, copy=False)
87
88 helpers.assert_table_roundtrip(table, tmp_path)
89
90
91 def test_structured_array_columns(tmp_path):
92 rows = np.array(
93 [((1, "a"), 2.0, "x"), ((4, "b"), 5.0, "y"), ((5, "c"), 8.2, "z")],
94 dtype=[("a", [("a0", "<i4"), ("a1", "|S1")]), ("b", "<f8"), ("c", "|S1")],
95 )
96
97 table = Table(rows, copy=False)
98
99 helpers.assert_table_roundtrip(table, tmp_path)
100
101
102 def test_table_row_order(tmp_path):
103 data = np.array([(1, 2.0, "x"), (4, 5.0, "y"), (5, 8.2, "z")], dtype=[("a", "<i4"), ("b", "<f8"), ("c", "|S1")])
104
105 table = Table(data, copy=False)
106 table.columns["a"].description = "RA"
107 table.columns["a"].unit = "degree"
108 table.columns["a"].meta = {"foo": "bar"}
109 table.columns["c"].description = "Some description of some sort"
110
111 helpers.assert_table_roundtrip(table, tmp_path)
112
113
114 # once asdf 2.14.x can be dropped and the minimum updated 2.15.0 this warning
115 # filter can be removed
116 @pytest.mark.filterwarnings(
117 "ignore:`product` is deprecated as of NumPy 1.25.0, and will be removed in "
118 "NumPy 2.0. Please use `prod` instead.:DeprecationWarning",
119 )
120 def test_table_inline(tmp_path):
121 rows = [(1, 2.0, "x"), (4, 5.0, "y"), (5, 8.2, "z")]
122 table = Table(rows=rows, names=("a", "b", "c"), dtype=("i4", "f8", "S1"))
123 table.columns["a"].description = "RA"
124 table.columns["a"].unit = "degree"
125 table.columns["a"].meta = {"foo": "bar"}
126 table.columns["c"].description = "Some description of some sort"
127
128 with asdf.config_context() as config:
129 config.array_inline_threshold = 64
130 helpers.assert_table_roundtrip(table, tmp_path)
131
132
133 def test_mismatched_columns():
134 yaml = """
135 table: !<tag:astropy.org:astropy/table/table-1.0.0>
136 columns:
137 - !core/column-1.0.0
138 data: !core/ndarray-1.0.0
139 data: [0, 1, 2]
140 name: a
141 - !core/column-1.0.0
142 data: !core/ndarray-1.0.0
143 data: [0, 1, 2, 3]
144 name: b
145 colnames: [a, b]
146 """
147
148 buff = yaml_to_asdf(yaml, version="1.5.0")
149
150 with pytest.raises(ValueError, match="Inconsistent data column lengths"), asdf.open(buff):
151 pass
152
153
154 def test_masked_table(tmp_path):
155 rows = [(1, 2.0, "x"), (4, 5.0, "y"), (5, 8.2, "z")]
156 table = Table(rows=rows, names=("a", "b", "c"), dtype=("i4", "f8", "S1"), masked=True)
157 table.columns["a"].description = "RA"
158 table.columns["a"].unit = "degree"
159 table.columns["a"].meta = {"foo": "bar"}
160 table.columns["a"].mask = [True, False, True]
161 table.columns["c"].description = "Some description of some sort"
162
163 helpers.assert_table_roundtrip(table, tmp_path)
164
165
166 def test_quantity_mixin(tmp_path):
167 table = QTable()
168 table["a"] = [1, 2, 3]
169 table["b"] = ["x", "y", "z"]
170 table["c"] = [2.0, 5.0, 8.2] * u.m
171
172 helpers.assert_table_roundtrip(table, tmp_path)
173
174
175 def test_time_mixin(tmp_path):
176 table = Table()
177 table["a"] = [1, 2]
178 table["b"] = ["x", "y"]
179 table["c"] = Time(["2001-01-02T12:34:56", "2001-02-03T00:01:02"])
180
181 result = helpers.assert_table_roundtrip(table, tmp_path)
182 helpers.assert_time_equal(result["c"], table["c"])
183
184
185 def test_timedelta_mixin(tmp_path):
186 table = Table()
187 table["a"] = [1, 2]
188 table["b"] = ["x", "y"]
189 table["c"] = TimeDelta([1, 2] * u.day)
190
191 result = helpers.assert_table_roundtrip(table, tmp_path)
192 helpers.assert_time_delta_equal(result["c"], table["c"])
193
194
195 def test_skycoord_mixin(tmp_path):
196 table = Table()
197 table["a"] = [1, 2]
198 table["b"] = ["x", "y"]
199 table["c"] = SkyCoord([1, 2], [3, 4], unit="deg,deg", frame="fk4", obstime="J1990.5")
200
201 # We can't use our assert_table_roundtrip helper because
202 # astropy 4.0.x does not include an __eq__ method for SKyCoord.
203 file_path = tmp_path / "test.asdf"
204 with asdf.AsdfFile() as af:
205 af["table"] = table
206 af.write_to(file_path)
207
208 with asdf.open(file_path) as af:
209 helpers.assert_sky_coord_equal(af["table"]["c"], table["c"])
210
211
212 def test_earthlocation_mixin(tmp_path):
213 table = Table()
214 table["a"] = [1, 2]
215 table["b"] = ["x", "y"]
216 table["c"] = EarthLocation(x=[1, 2] * u.km, y=[3, 4] * u.km, z=[5, 6] * u.km)
217
218 result = helpers.assert_table_roundtrip(table, tmp_path)
219 helpers.assert_earth_location_equal(result["c"], table["c"])
220
221
222 def test_ndarray_mixin(tmp_path):
223 table = Table()
224 table["a"] = [1, 2]
225 table["b"] = ["x", "y"]
226 table["c"] = NdarrayMixin([5, 6])
227
228 result = helpers.assert_table_roundtrip(table, tmp_path)
229 assert isinstance(result["c"], NdarrayMixin)
230
231
232 def test_asdf_table():
233 yaml = """
234 table: !core/table-1.0.0
235 columns:
236 - !core/column-1.0.0
237 data: !core/ndarray-1.0.0
238 data: [1, 2, 3]
239 datatype: float64
240 shape: [3]
241 description: RA
242 meta: {foo: bar}
243 name: a
244 unit: !unit/unit-1.0.0 deg
245 - !core/column-1.0.0
246 data: !core/ndarray-1.0.0
247 data: [4, 5, 6]
248 datatype: float64
249 shape: [3]
250 description: DEC
251 name: b
252 - !core/column-1.0.0
253 data: !core/ndarray-1.0.0
254 data: [d, e, f]
255 datatype: [ascii, 1]
256 shape: [3]
257 description: The target name
258 name: c
259 """
260 buff = yaml_to_asdf(yaml, version="1.5.0")
261
262 with asdf.open(buff) as af:
263 table = af["table"]
264 assert isinstance(table, Table)
265
266 assert table["a"].name == "a"
267 assert table["a"].description == "RA"
268 assert table["a"].meta == {"foo": "bar"}
269 assert table["a"].unit == u.deg
270 assert_array_equal(table["a"].data, np.array([1, 2, 3], dtype=np.float64))
271
272 assert table["b"].name == "b"
273 assert table["b"].description == "DEC"
274 assert_array_equal(table["b"].data, np.array([4, 5, 6], dtype=np.float64))
275
276 assert table["c"].name == "c"
277 assert table["c"].description == "The target name"
278 assert_array_equal(table["c"].data, np.array([b"d", b"e", b"f"]))