Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add astropy Table support to compare_asdf #915

Merged
merged 2 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ general
``asdf.commands.diff`` with ``deepdiff`` and add ``deepdiff`` as
a test dependency [#868]

- Add ``astropy.table.Table`` support to ``compare_asdf`` [#915]

ramp_fitting
------------

Expand Down
21 changes: 21 additions & 0 deletions romancal/regtest/regtestdata.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import io
import os
import os.path as op
import pprint
Expand All @@ -8,7 +9,9 @@
from pathlib import Path

import asdf
import astropy.table
import astropy.time
import astropy.utils.diff
import deepdiff
import gwcs
import numpy as np
Expand Down Expand Up @@ -578,6 +581,23 @@ def give_up_diffing(self, level, diff_instance):
return True


class TableOperator(BaseOperator):
def give_up_diffing(self, level, diff_instance):
sio = io.StringIO()
identical = astropy.utils.diff.report_diff_values(
level.t1, level.t2, fileobj=sio
)
difference = {}
if not identical:
difference["values_differ"] = sio.read()
# also compare meta (which report_diff_values does not)
if level.t1.meta != level.t2.meta:
difference["metas_differ"] = deepdiff.DeepDiff(level.t1.meta, level.t2.meta)
if difference:
diff_instance.custom_report_result("tables_differ", level, difference)
return True


class TimeOperator(BaseOperator):
def give_up_diffing(self, level, diff_instance):
if level.t1 != level.t2:
Expand Down Expand Up @@ -673,6 +693,7 @@ def compare_asdf(result, truth, ignore=None, rtol=1e-05, atol=1e-08, equal_nan=T
rtol, atol, equal_nan, types=[asdf.tags.core.NDArrayType, np.ndarray]
),
TimeOperator(types=[astropy.time.Time]),
TableOperator(types=[astropy.table.Table]),
WCSOperator(types=[gwcs.WCS]),
]
# warnings can be seen in regtest runs which indicate
Expand Down
31 changes: 31 additions & 0 deletions romancal/regtest/test_regtestdata.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import asdf
import astropy.table
import numpy as np
import pytest
from roman_datamodels import datamodels as rdm
from roman_datamodels import maker_utils

Expand Down Expand Up @@ -25,3 +29,30 @@ def test_compare_asdf_differ(tmp_path):
assert not diff.identical, diff.report()
assert "arrays_differ" in diff.diff
assert "root['roman']['data']" in diff.diff["arrays_differ"]


@pytest.mark.parametrize("modification", [None, "dtype", "names", "values", "meta"])
def test_compare_asdf_tables(tmp_path, modification):
fn0 = tmp_path / "test0.asdf"
fn1 = tmp_path / "test1.asdf"
names = ("a",)
values = [1, 2, 3]
dtype = np.uint8
t0 = astropy.table.Table([np.array(values, dtype=dtype)], names=names)
if modification == "names":
names = ("b",)
if modification == "values":
values = [4, 5, 6]
if modification == "dtype":
dtype = np.float64
t1 = astropy.table.Table([np.array(values, dtype=dtype)], names=names)
if modification == "meta":
t1.meta["modified"] = True
asdf.AsdfFile({"t": t0}).write_to(fn0)
asdf.AsdfFile({"t": t1}).write_to(fn1)
diff = compare_asdf(fn0, fn1)
if modification is None:
assert diff.identical, diff.report()
else:
assert not diff.identical, diff.report()
assert "tables_differ" in diff.diff