From a3fa81512bccf7874b8c052908cc3d36f13d8bf4 Mon Sep 17 00:00:00 2001 From: Brett Date: Fri, 27 Oct 2023 10:49:47 -0400 Subject: [PATCH] fix asdftool diff output for arrays in list --- CHANGES.rst | 2 +- asdf/_tests/commands/tests/data/blocks.diff | 3 ++- .../commands/tests/data/ndarray_in_list.diff | 9 +++++++++ .../commands/tests/data/ndarray_in_list0.asdf | Bin 0 -> 745 bytes .../commands/tests/data/ndarray_in_list1.asdf | Bin 0 -> 778 bytes asdf/_tests/commands/tests/data/ndarrays.diff | 5 ++++- .../commands/tests/data/simple_inline_array.diff | 5 ++++- asdf/_tests/commands/tests/test_diff.py | 6 ++++++ asdf/commands/diff.py | 13 +++++++------ 9 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 asdf/_tests/commands/tests/data/ndarray_in_list.diff create mode 100644 asdf/_tests/commands/tests/data/ndarray_in_list0.asdf create mode 100644 asdf/_tests/commands/tests/data/ndarray_in_list1.asdf diff --git a/CHANGES.rst b/CHANGES.rst index bac63847d..9283e4157 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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) ------------------ diff --git a/asdf/_tests/commands/tests/data/blocks.diff b/asdf/_tests/commands/tests/data/blocks.diff index 9f04a8167..da3edd2df 100644 --- a/asdf/_tests/commands/tests/data/blocks.diff +++ b/asdf/_tests/commands/tests/data/blocks.diff @@ -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 diff --git a/asdf/_tests/commands/tests/data/ndarray_in_list.diff b/asdf/_tests/commands/tests/data/ndarray_in_list.diff new file mode 100644 index 000000000..3a58bce13 --- /dev/null +++ b/asdf/_tests/commands/tests/data/ndarray_in_list.diff @@ -0,0 +1,9 @@ +< tree: +< list: +< - a: +< offset: +< 16 +< strides: +< - -8 +> ndarrays differ by contents +< ndarrays differ by contents diff --git a/asdf/_tests/commands/tests/data/ndarray_in_list0.asdf b/asdf/_tests/commands/tests/data/ndarray_in_list0.asdf new file mode 100644 index 0000000000000000000000000000000000000000..0b2e1925a051bfa175e3d68e2e130f7b0385bf62 GIT binary patch literal 745 zcmb7C&2G~`5H1{j<-&2Rs_KDwozOHAmJqnOMW|^j(HuYsSrdD0uVj0p-Em4pc?^zy zfnNF+y(8X%x2VLdf1*&S2YlI?@i+5*^X={g7vV7*@gW~NJ2s9kX2Dq)daLIoF_9e)rp`6fZ#w zf)FG74mIXV`)rmA)+hC8mm0|&z@Y!CDxbQtMlbF|Y9;>n?N|i1Fn|N>F zFjhfHXiGd`OPV27<+FXF8u3IdCoeLH;njEy=?^ED1{DdTF7XXTWwFL){U+;bR0@Oy z5L(d$*=UL40*q-n`MQNiB~&sM2Ki|h9Q7m*`fDxQZd$J~uic3&oq3I;>!GH}Nrb%? zE21~^F?0XJje^|p9hX5rt4cs?SZ%CqRIOujx8=5e;A%m+tZ*`J^vqW%luiWQXCV;_ z(W=bB!oKW(bvWnF@5AZI)^O|J?|GN5&u2G3-`u~w`gL`EeD~+pd)>pIbivc^Y@RGT aoSwfsVIR)IqfZ;}l^!K+Gz`o6p_}F2fTx;zK@kc5ED7&Vq|D_z=?J0Uhps z3eKk#kDT3E@Q!UWgv>W+V#!5P`(TpPb6nSD+p*HZv$;!XsBx*mBkvH^^>emqf#Iw zfY6F2$VN#N7hp`w$=4-3Dxs38Fv!ol;J7FGtG?E ndarrays differ by contents +< ndarrays differ by contents diff --git a/asdf/_tests/commands/tests/data/simple_inline_array.diff b/asdf/_tests/commands/tests/data/simple_inline_array.diff index 3e30e30c1..8a423c5fd 100644 --- a/asdf/_tests/commands/tests/data/simple_inline_array.diff +++ b/asdf/_tests/commands/tests/data/simple_inline_array.diff @@ -1 +1,4 @@ - ndarrays at "array" differ by contents +tree: + array: +> ndarrays differ by contents +< ndarrays differ by contents diff --git a/asdf/_tests/commands/tests/test_diff.py b/asdf/_tests/commands/tests/test_diff.py index d96eb6849..45df7d036 100644 --- a/asdf/_tests/commands/tests/test_diff.py +++ b/asdf/_tests/commands/tests/test_diff.py @@ -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" diff --git a/asdf/commands/diff.py b/asdf/commands/diff.py index fa8776ad7..7c682fa74 100644 --- a/asdf/commands/diff.py +++ b/asdf/commands/diff.py @@ -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) @@ -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], @@ -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): @@ -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)