| 1 |
""" |
| 2 |
Helpers for testing astropy objects in ASDF files. |
| 3 |
""" |
| 4 |
|
| 5 |
import numpy as np |
| 6 |
|
| 7 |
|
| 8 |
def assert_earth_location_equal(a, b): |
| 9 |
""" |
| 10 |
Assert earth location objects are equal. |
| 11 |
""" |
| 12 |
__tracebackhide__ = True |
| 13 |
|
| 14 |
assert (a == b).all() |
| 15 |
|
| 16 |
|
| 17 |
def assert_representation_equal(a, b): |
| 18 |
""" |
| 19 |
Assert representation objects are equal. |
| 20 |
""" |
| 21 |
from astropy import units |
| 22 |
|
| 23 |
__tracebackhide__ = True |
| 24 |
|
| 25 |
assert type(a) is type(b) |
| 26 |
assert a.components == b.components |
| 27 |
for component in a.components: |
| 28 |
assert units.allclose(getattr(a, component), getattr(b, component)) |
| 29 |
|
| 30 |
|
| 31 |
def assert_sky_coord_equal(a, b): |
| 32 |
""" |
| 33 |
Assert sky coordinate objects are equal. |
| 34 |
""" |
| 35 |
__tracebackhide__ = True |
| 36 |
|
| 37 |
assert a.is_equivalent_frame(b) |
| 38 |
assert a.representation_type is b.representation_type |
| 39 |
assert a.shape == b.shape |
| 40 |
|
| 41 |
assert_representation_equal(a.data, b.data) |
| 42 |
|
| 43 |
|
| 44 |
def assert_frame_equal(a, b): |
| 45 |
""" |
| 46 |
Assert frame objects are equal. |
| 47 |
""" |
| 48 |
__tracebackhide__ = True |
| 49 |
|
| 50 |
assert type(a) is type(b) |
| 51 |
|
| 52 |
if a is None: |
| 53 |
return |
| 54 |
|
| 55 |
if a.has_data: |
| 56 |
assert b.has_data |
| 57 |
assert_representation_equal(a.data, b.data) |
| 58 |
else: |
| 59 |
return |
| 60 |
|
| 61 |
|
| 62 |
def assert_spectral_coord_equal(a, b): |
| 63 |
""" |
| 64 |
Assert spectral coordinate objects are equal. |
| 65 |
""" |
| 66 |
from astropy.tests.helper import assert_quantity_allclose |
| 67 |
|
| 68 |
__tracebackhide__ = True |
| 69 |
|
| 70 |
assert type(a) is type(b) |
| 71 |
assert_quantity_allclose(a.quantity, b.quantity) |
| 72 |
assert_frame_equal(a.observer, b.observer) |
| 73 |
assert_frame_equal(a.target, b.target) |
| 74 |
|
| 75 |
|
| 76 |
def assert_time_equal(a, b): |
| 77 |
""" |
| 78 |
Assert time objects are equal |
| 79 |
""" |
| 80 |
from astropy.coordinates import EarthLocation |
| 81 |
|
| 82 |
assert a.format == b.format |
| 83 |
assert a.scale == b.scale |
| 84 |
|
| 85 |
assert isinstance(a.location, type(b.location)) |
| 86 |
if isinstance(a.location, EarthLocation): |
| 87 |
assert_earth_location_equal(a.location, b.location) |
| 88 |
else: |
| 89 |
assert a.location == b.location |
| 90 |
|
| 91 |
if a.format == "plot_date": |
| 92 |
np.testing.assert_array_almost_equal(a.value, b.value) |
| 93 |
else: |
| 94 |
np.testing.assert_array_equal(a, b) |
| 95 |
|
| 96 |
|
| 97 |
def assert_time_delta_equal(a, b): |
| 98 |
""" |
| 99 |
Assert time delta objects are equal |
| 100 |
""" |
| 101 |
np.testing.assert_array_equal(a.jd, b.jd) |
| 102 |
np.testing.assert_array_equal(a.jd2, b.jd2) |
| 103 |
np.testing.assert_array_equal(a.sec, b.sec) |
| 104 |
|
| 105 |
|
| 106 |
def assert_hdu_list_equal(a, b): |
| 107 |
""" |
| 108 |
Assert fits hdulists are equal. |
| 109 |
""" |
| 110 |
assert len(a) == len(b) |
| 111 |
for hdu_a, hdu_b in zip(a, b): |
| 112 |
np.testing.assert_array_equal(hdu_a.data, hdu_b.data) |
| 113 |
assert len(hdu_a.header.cards) == len(hdu_b.header.cards) |
| 114 |
for card_a, card_b in zip(hdu_a.header.cards, hdu_b.header.cards): |
| 115 |
assert tuple(card_a) == tuple(card_b) |
| 116 |
|
| 117 |
|
| 118 |
def assert_description_equal(a, b): |
| 119 |
""" |
| 120 |
Assert table descriptions are equal. |
| 121 |
""" |
| 122 |
if a in ("", None) and b in ("", None): |
| 123 |
return |
| 124 |
|
| 125 |
assert a == b |
| 126 |
|
| 127 |
|
| 128 |
def assert_table_equal(a, b): |
| 129 |
""" |
| 130 |
Assert astropy tables are equal. |
| 131 |
""" |
| 132 |
from astropy.table import Column, MaskedColumn |
| 133 |
|
| 134 |
assert type(a) is type(b) |
| 135 |
assert a.meta == b.meta |
| 136 |
|
| 137 |
assert len(a) == len(b) |
| 138 |
for row_a, row_b in zip(a, b): |
| 139 |
np.testing.assert_array_equal(row_a, row_b) |
| 140 |
|
| 141 |
assert a.colnames == b.colnames |
| 142 |
for column_name in a.colnames: |
| 143 |
col_a = a[column_name] |
| 144 |
col_b = b[column_name] |
| 145 |
if isinstance(col_a, (Column, MaskedColumn)) and isinstance(col_b, (Column, MaskedColumn)): |
| 146 |
assert_description_equal(col_a.description, col_b.description) |
| 147 |
assert col_a.unit == col_b.unit |
| 148 |
assert col_a.meta == col_b.meta |
| 149 |
np.testing.assert_array_equal(col_a.data, col_b.data) |
| 150 |
np.testing.assert_array_equal( |
| 151 |
getattr(col_a, "mask", [False] * len(col_a)), |
| 152 |
getattr(col_b, "mask", [False] * len(col_b)), |
| 153 |
) |
| 154 |
|
| 155 |
|
| 156 |
def assert_wcs_equal(a, b): |
| 157 |
from asdf.tags.core.ndarray import NDArrayType |
| 158 |
|
| 159 |
from asdf_astropy.converters.wcs.wcs import _WCS_ATTRS |
| 160 |
|
| 161 |
assert type(a) is type(b) |
| 162 |
assert_hdu_list_equal(a.to_fits(relax=True), b.to_fits(relax=True)) |
| 163 |
for attr in _WCS_ATTRS: |
| 164 |
in_a = hasattr(a, attr) |
| 165 |
in_b = hasattr(b, attr) |
| 166 |
assert in_a == in_b |
| 167 |
if not in_a: |
| 168 |
continue |
| 169 |
a_val = getattr(a, attr) |
| 170 |
b_val = getattr(b, attr) |
| 171 |
if isinstance(a_val, (np.ndarray, NDArrayType)) or isinstance(b_val, (np.ndarray, NDArrayType)): |
| 172 |
np.testing.assert_array_equal(a_val, b_val) |
| 173 |
else: |
| 174 |
assert a_val == b_val |
| 175 |
|
| 176 |
|
| 177 |
def assert_table_roundtrip(table, tmp_path): |
| 178 |
""" |
| 179 |
Assert that a table can be written to an ASDF file and read back |
| 180 |
in without losing any of its essential properties. |
| 181 |
""" |
| 182 |
import asdf |
| 183 |
|
| 184 |
file_path = tmp_path / "testable.asdf" |
| 185 |
|
| 186 |
with asdf.AsdfFile({"table": table}) as af: |
| 187 |
af.write_to(file_path) |
| 188 |
|
| 189 |
with asdf.open(file_path) as af: |
| 190 |
assert_table_equal(table, af["table"]) |
| 191 |
return af["table"] |
| 192 |
|
| 193 |
|
| 194 |
def assert_model_equal(a, b): |
| 195 |
""" |
| 196 |
Assert that two model instances are equivalent. |
| 197 |
""" |
| 198 |
if a is None and b is None: |
| 199 |
return |
| 200 |
|
| 201 |
assert a.__class__ == b.__class__ |
| 202 |
|
| 203 |
assert a.name == b.name |
| 204 |
assert a.inputs == b.inputs |
| 205 |
assert a.input_units == b.input_units |
| 206 |
assert a.outputs == b.outputs |
| 207 |
assert a.input_units_allow_dimensionless == b.input_units_allow_dimensionless |
| 208 |
assert a.input_units_equivalencies == b.input_units_equivalencies |
| 209 |
|
| 210 |
np.testing.assert_array_equal(a.parameters, b.parameters) |
| 211 |
|
| 212 |
assert a._user_bounding_box == b._user_bounding_box |
| 213 |
try: |
| 214 |
a_bounding_box = a.bounding_box |
| 215 |
except NotImplementedError: |
| 216 |
a_bounding_box = None |
| 217 |
|
| 218 |
try: |
| 219 |
b_bounding_box = b.bounding_box |
| 220 |
except NotImplementedError: |
| 221 |
b_bounding_box = None |
| 222 |
|
| 223 |
assert a_bounding_box == b_bounding_box |
| 224 |
|
| 225 |
assert a.fixed == b.fixed |
| 226 |
assert a.bounds == b.bounds |
| 227 |
|
| 228 |
assert_model_equal(a._user_inverse, b._user_inverse) |
| 229 |
|
| 230 |
|
| 231 |
def assert_bounding_box_roundtrip(bounding_box, tmp_path, version=None): |
| 232 |
""" |
| 233 |
Assert that a bounding_box can be written to an ASDF file and read back |
| 234 |
in without losing any of its essential properties. |
| 235 |
""" |
| 236 |
import asdf |
| 237 |
|
| 238 |
path = str(tmp_path / "test.asdf") |
| 239 |
|
| 240 |
with asdf.AsdfFile({"bounding_box": bounding_box}, version=version) as af: |
| 241 |
af.write_to(path) |
| 242 |
|
| 243 |
with asdf.open(path) as af: |
| 244 |
assert bounding_box == af["bounding_box"](bounding_box._model) |
| 245 |
|
| 246 |
|
| 247 |
def assert_model_roundtrip(model, tmp_path, version=None): |
| 248 |
""" |
| 249 |
Assert that a model can be written to an ASDF file and read back |
| 250 |
in without losing any of its essential properties. |
| 251 |
""" |
| 252 |
import asdf |
| 253 |
|
| 254 |
path = str(tmp_path / "test.asdf") |
| 255 |
|
| 256 |
with asdf.AsdfFile({"model": model}, version=version) as af: |
| 257 |
af.write_to(path) |
| 258 |
|
| 259 |
with asdf.open(path) as af: |
| 260 |
assert_model_equal(model, af["model"]) |
| 261 |
return af["model"] |
| 262 |
|
| 263 |
|
| 264 |
def assert_wcs_roundtrip(wcs, tmp_path, version=None): |
| 265 |
import asdf |
| 266 |
|
| 267 |
path = str(tmp_path / "test.asdf") |
| 268 |
|
| 269 |
with asdf.AsdfFile({"wcs": wcs}, version=version) as af: |
| 270 |
af.write_to(path) |
| 271 |
|
| 272 |
with asdf.open(path) as af: |
| 273 |
assert_wcs_equal(wcs, af["wcs"]) |
| 274 |
|
| 275 |
|
| 276 |
def _assert_gwcs_frame_equal(a, b): |
| 277 |
__tracebackhide__ = True |
| 278 |
|
| 279 |
import gwcs.coordinate_frames as cf |
| 280 |
|
| 281 |
assert type(a) is type(b) |
| 282 |
|
| 283 |
if a is None: |
| 284 |
return None |
| 285 |
|
| 286 |
if not isinstance(a, cf.CoordinateFrame): |
| 287 |
return a == b |
| 288 |
|
| 289 |
assert a.name == b.name # nosec |
| 290 |
if not isinstance(a, cf.EmptyFrame): |
| 291 |
assert a.axes_order == b.axes_order # nosec |
| 292 |
assert a.axes_names == b.axes_names # nosec |
| 293 |
assert a.unit == b.unit # nosec |
| 294 |
assert a.reference_frame == b.reference_frame # nosec |
| 295 |
return None |
| 296 |
|
| 297 |
|
| 298 |
def assert_gwcs_equal(a, b): |
| 299 |
assert a.name == b.name # nosec |
| 300 |
assert a.pixel_shape == b.pixel_shape |
| 301 |
assert len(a.available_frames) == len(b.available_frames) # nosec |
| 302 |
for a_step, b_step in zip(a.pipeline, b.pipeline, strict=False): |
| 303 |
_assert_gwcs_frame_equal(a_step.frame, b_step.frame) |
| 304 |
assert_model_equal(a_step.transform, b_step.transform) |