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

fix asdftool diff output for arrays in list #1672

Merged
merged 1 commit into from
Oct 30, 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: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
The ASDF Standard is at v1.6.0
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

-
- Fix bug in ``asdftool diff`` for arrays within a list [#1672]

3.0.0 (2023-10-16)
------------------
Expand Down
3 changes: 2 additions & 1 deletion asdf/_tests/commands/tests/data/blocks.diff
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ tree:
-
> 9000
< 10000
ndarrays at "foobar" differ by shape, datatype and contents
> ndarrays differ by shape, datatype and contents
< ndarrays differ by shape, datatype and contents
9 changes: 9 additions & 0 deletions asdf/_tests/commands/tests/data/ndarray_in_list.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
< tree:
< list:
< - a:
< offset:
< 16
< strides:
< - -8
> ndarrays differ by contents
< ndarrays differ by contents
Binary file not shown.
Binary file not shown.
5 changes: 4 additions & 1 deletion asdf/_tests/commands/tests/data/ndarrays.diff
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
ndarrays at "a" differ by contents
tree:
a:
> ndarrays differ by contents
< ndarrays differ by contents
5 changes: 4 additions & 1 deletion asdf/_tests/commands/tests/data/simple_inline_array.diff
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
ndarrays at "array" differ by contents
tree:
array:
> ndarrays differ by contents
< ndarrays differ by contents
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 @@ -54,6 +54,12 @@ def test_diff_ndarray():
_assert_diffs_equal(filenames, result_file, minimal=False)


def test_diff_ndarray_in_list():
filenames = ["ndarray_in_list0.asdf", "ndarray_in_list1.asdf"]
result_file = "ndarray_in_list.diff"
_assert_diffs_equal(filenames, result_file, minimal=False)


def test_diff_block():
filenames = ["block0.asdf", "block1.asdf"]
result_file = "blocks.diff"
Expand Down
13 changes: 7 additions & 6 deletions asdf/commands/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ class ArrayNode:
of but not necessarily display. This allows the diff output to be
cleaner."""

def __init__(self, name):
def __init__(self, name, index):
self.name = name
self.index = index

def __hash__(self):
return hash(self.name)
Expand Down Expand Up @@ -191,7 +192,7 @@ def print_in_tree(diff_ctx, node_list, thing, other, use_marker=False, last_was_
# If tree element is list, recursively print list contents
if isinstance(thing, list):
for i, subthing in enumerate(thing):
key = ArrayNode(f"{node_list[-1]}_{i}")
key = ArrayNode(f"{node_list[-1]}_{i}", i)
last_was_list = print_in_tree(
diff_ctx,
[*node_list, key],
Expand Down Expand Up @@ -277,9 +278,9 @@ def compare_ndarrays(diff_ctx, array0, array1, keys):
differences.append("contents")

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


def both_are_ndarrays(tree0, tree1):
Expand Down Expand Up @@ -321,7 +322,7 @@ def compare_trees(diff_ctx, tree0, tree1, keys=None):
compare_dicts(diff_ctx, tree0, tree1, keys)
elif isinstance(tree0, list) and isinstance(tree1, list):
for i, (obj0, obj1) in enumerate(zip(tree0, tree1)):
key = ArrayNode(f"item_{i}")
key = ArrayNode(f"item_{i}", i)
compare_trees(diff_ctx, obj0, obj1, [*keys, key])
else:
compare_objects(diff_ctx, tree0, tree1, keys)
Expand Down
Loading