Skip to content

Commit

Permalink
Merge pull request asdf-format#1652 from braingram/diff
Browse files Browse the repository at this point in the history
Fix issue with asdftool diff
  • Loading branch information
braingram authored Sep 29, 2023
2 parents 0800176 + f1b3acb commit 79356ab
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 12 deletions.
6 changes: 4 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ The ASDF Standard is at v1.6.0
``asdf.tags.core.Stream``, update block storage support for
Converter and update internal block API [#1537]
- Remove deprecated resolve_local_refs argument to load_schema [#1623]
- Move IntegerType to converter and drop cache of converted values. [#1527]
- Remove legacy extension API [#1637]
- Fix bug that left out the name of the arrays that differed
for ``asdftool diff`` comparisons [#1652]

2.15.2 (2023-09-29)
-------------------
Expand All @@ -33,8 +37,6 @@ The ASDF Standard is at v1.6.0
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

- Add support for python 3.12 [#1641]
- Move IntegerType to converter and drop cache of converted values. [#1527]
- Remove legacy extension API [#1637]

2.15.1 (2023-08-07)
-------------------
Expand Down
2 changes: 1 addition & 1 deletion asdf/_tests/commands/tests/data/blocks.diff
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ tree:
-
> 9000
< 10000
[31mndarrays differ by shape, datatype and contents[0m
[31mndarrays at "foobar" differ by shape, datatype and contents[0m
Binary file added asdf/_tests/commands/tests/data/ndarray0.asdf
Binary file not shown.
Binary file added asdf/_tests/commands/tests/data/ndarray1.asdf
Binary file not shown.
1 change: 1 addition & 0 deletions asdf/_tests/commands/tests/data/ndarrays.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ndarrays at "a" differ by contents
2 changes: 1 addition & 1 deletion asdf/_tests/commands/tests/data/simple_inline_array.diff
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[31mndarrays differ by contents[0m
[31mndarrays at "array" differ by contents[0m
6 changes: 6 additions & 0 deletions asdf/_tests/commands/tests/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ def test_diff_ignore(result_file, ignore):
_assert_diffs_equal(filenames, result_file, minimal=False, ignore=ignore)


def test_diff_ndarray():
filenames = ["ndarray0.asdf", "ndarray1.asdf"]
result_file = "ndarrays.diff"
_assert_diffs_equal(filenames, result_file, minimal=False)


def test_diff_block():
filenames = ["block0.asdf", "block1.asdf"]
result_file = "blocks.diff"
Expand Down
24 changes: 16 additions & 8 deletions asdf/commands/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys

import jmespath
import numpy as np
from numpy import array_equal

try:
Expand All @@ -30,6 +31,7 @@
RESET = ""

import asdf
from asdf.extension._serialization_context import BlockAccess
from asdf.tagged import Tagged
from asdf.util import human_list

Expand Down Expand Up @@ -243,6 +245,16 @@ def print_dict_diff(diff_ctx, tree, node_list, keys, other):
print_in_tree(diff_ctx, nodes, key_, other, use_marker=use_marker)


def _load_array(asdf_file, array_dict):
# the array_dict may not be tagged if the array is inline
# in this case just use what's in "data"
if not hasattr(array_dict, "_tag"):
return array_dict["data"]
conv = asdf_file.extension_manager.get_converter_for_type(np.ndarray)
sctx = asdf_file._create_serialization_context(BlockAccess.READ)
return conv.from_yaml_tree(array_dict, array_dict._tag, sctx)


def compare_ndarrays(diff_ctx, array0, array1, keys):
"""Compares two ndarray objects"""
if isinstance(array0, list):
Expand All @@ -258,19 +270,15 @@ def compare_ndarrays(diff_ctx, array0, array1, keys):
if array0.get(field) != array1.get(field):
differences.append(field)

def get_flat(af, keys):
for k in keys:
af = af[k]
return af
value0 = _load_array(diff_ctx.asdf0, array0)
value1 = _load_array(diff_ctx.asdf1, array1)

array0 = get_flat(diff_ctx.asdf0, keys)
array1 = get_flat(diff_ctx.asdf1, keys)
if not array_equal(array0, array1):
if not array_equal(value0, value1):
differences.append("contents")

if differences:
prefix = " " * (len(keys) + 1)
msg = f"ndarrays differ by {human_list(differences)}"
msg = f"ndarrays at \"{'.'.join(keys)}\" differ by {human_list(differences)}"
diff_ctx.iostream.write(prefix + RED + msg + RESET_NEWLINE)


Expand Down

0 comments on commit 79356ab

Please sign in to comment.