GeezerVison is showing github.com — fixing a bunch of stuff around report repetition and multi dimensional · FOSSAware/deepdiff@56b98ab
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .gitignore +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -65,3 +65,4 @@ target/ | 65 | 65 |
| 66 | 66 | .python-version* | 67 | 67 |
| | 68 | +temp* |
Collapse file Copy file name to clipboardExpand all lines: deepdiff/base.py +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 |
| 4 | 4 |
| 5 | 5 | DEFAULT_SIGNIFICANT_DIGITS_WHEN_IGNORE_NUMERIC_TYPES = 12 | | 6 | +TYPE_STABILIZATION_MSG = 'Unable to stabilize the Numpy array {} due to {}. Please set ignore_order=False.' | 6 | 7 |
| 7 | 8 |
| 8 | 9 | class Base: | |
Collapse file Copy file name to clipboardExpand all lines: deepdiff/deephash.py +5-3Lines changed: 5 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | convert_item_or_items_into_set_else_none, get_doc, | 9 | 9 | convert_item_or_items_into_compiled_regexes_else_none, | 10 | 10 | get_id, type_is_subclass_of_type_group, type_in_type_group, | 11 | | - number_to_string, KEY_TO_VAL_STR, short_repr, np) | | 11 | + number_to_string, KEY_TO_VAL_STR, short_repr) | 12 | 12 | from deepdiff.base import Base | 13 | 13 | logger = logging.getLogger(__name__) | 14 | 14 |
| | 26 | 26 | INDEX_VS_ATTRIBUTE = ('[%s]', '.%s') | 27 | 27 |
| 28 | 28 |
| | 29 | +HASH_LOOKUP_ERR_MSG = '{} is not one of the hashed items.' | | 30 | + | | 31 | + | 29 | 32 | class BoolObj(Enum): | 30 | 33 | TRUE = 1 | 31 | 34 | FALSE = 0 | @@ -86,7 +89,6 @@ def __init__(self, | 86 | 89 | self.hashes = hashes.hashes | 87 | 90 | else: | 88 | 91 | self.hashes = {} | 89 | | - self.obj = obj | 90 | 92 | exclude_types = set() if exclude_types is None else set(exclude_types) | 91 | 93 | self.exclude_types_tuple = tuple(exclude_types) # we need tuple for checking isinstance | 92 | 94 | self.ignore_repetition = ignore_repetition | @@ -178,7 +180,7 @@ def _getitem(hashes, obj, extract_index=0): | 178 | 180 | try: | 179 | 181 | result_n_count = hashes[key] | 180 | 182 | except KeyError: | 181 | | - raise KeyError('{} is not one of the hashed items.'.format(obj)) from None | | 183 | + raise KeyError(HASH_LOOKUP_ERR_MSG.format(obj)) from None | 182 | 184 |
| 183 | 185 | if isinstance(obj, strings) and obj in RESERVED_DICT_KEYS: | 184 | 186 | extract_index = None | |
Collapse file Copy file name to clipboardExpand all lines: deepdiff/delta.py +1-17Lines changed: 1 addition & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | from deepdiff import DeepDiff | 5 | 5 | from deepdiff.serialization import pickle_load | 6 | 6 | from deepdiff.helper import ( | 7 | | - DICT_IS_SORTED, MINIMUM_PY_DICT_TYPE_SORTED, strings, short_repr, numbers, np_ndarray) | | 7 | + DICT_IS_SORTED, MINIMUM_PY_DICT_TYPE_SORTED, strings, short_repr, numbers, np_ndarray, not_found) | 8 | 8 | from deepdiff.path import _path_to_elements, _get_nested_obj, GET, GETATTR | 9 | 9 |
| 10 | 10 | DISABLE_DELTA = not DICT_IS_SORTED | | 27 | 27 | 'and NOT ndarray + delta') | 28 | 28 |
| 29 | 29 |
| 30 | | -class _NotFound: | 31 | | - | 32 | | - def __eq__(self, other): | 33 | | - return False | 34 | | - | 35 | | - __req__ = __eq__ | 36 | | - | 37 | | - def __repr__(self): | 38 | | - return 'not found' | 39 | | - | 40 | | - __str__ = __repr__ | 41 | | - | 42 | | - | 43 | | -not_found = _NotFound() | 44 | | - | 45 | | - | 46 | 30 | class DeltaError(ValueError): | 47 | 31 | """ | 48 | 32 | Delta specific errors | |
Collapse file Copy file name to clipboardExpand all lines: deepdiff/diff.py +44-21Lines changed: 44 additions & 21 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -126,6 +126,7 @@ def __init__(self, | 126 | 126 | self.tree = TreeResult() | 127 | 127 | self.t1 = t1 | 128 | 128 | self.t2 = t2 | | 129 | + | 129 | 130 | self.numpy_used = False | 130 | 131 |
| 131 | 132 | root = DiffLevel(t1, t2, verbose_level=self.verbose_level) | @@ -353,8 +354,8 @@ def __diff_dict(self, | 353 | 354 |
| 354 | 355 | def __diff_set(self, level): | 355 | 356 | """Difference of sets""" | 356 | | - t1_hashtable = self.__create_hashtable(level.t1, level) | 357 | | - t2_hashtable = self.__create_hashtable(level.t2, level) | | 357 | + t1_hashtable = self.__create_hashtable(level, 't1') | | 358 | + t2_hashtable = self.__create_hashtable(level, 't2') | 358 | 359 |
| 359 | 360 | t1_hashes = set(t1_hashtable.keys()) | 360 | 361 | t2_hashes = set(t2_hashtable.keys()) | @@ -389,8 +390,10 @@ def __diff_iterable(self, level, parents_ids=frozenset({})): | 389 | 390 | """Difference of iterables""" | 390 | 391 | if self.ignore_order: | 391 | 392 | self.__diff_iterable_with_deephash(level, parents_ids) | 392 | | - return | | 393 | + else: | | 394 | + self.__diff_iterable_in_order(level, parents_ids) | 393 | 395 |
| | 396 | + def __diff_iterable_in_order(self, level, parents_ids=frozenset({})): | 394 | 397 | # We're handling both subscriptable and non-subscriptable iterables. Which one is it? | 395 | 398 | subscriptable = self.__iterables_subscriptable(level.t1, level.t2) | 396 | 399 | if subscriptable: | @@ -488,11 +491,12 @@ def _add_hash(self, hashes, item_hash, item, i): | 488 | 491 | else: | 489 | 492 | hashes[item_hash] = IndexedHash(indexes=[i], item=item) | 490 | 493 |
| 491 | | - def __create_hashtable(self, t, level): | | 494 | + def __create_hashtable(self, level, t): | 492 | 495 | """Create hashtable of {item_hash: (indexes, item)}""" | | 496 | + obj = getattr(level, t) | 493 | 497 |
| 494 | 498 | local_hashes = {} | 495 | | - for (i, item) in enumerate(t): | | 499 | + for (i, item) in enumerate(obj): | 496 | 500 | try: | 497 | 501 | parent = "{}[{}]".format(level.path(), i) | 498 | 502 | # Note: in the DeepDiff we only calculate the hash of items when we have to. | @@ -552,6 +556,13 @@ def __get_most_in_common_pairs_in_iterables(self, hashes_added, hashes_removed, | 552 | 556 | # We need the rough distance between the 2 objects to see if they qualify to be pairs or not | 553 | 557 | parameters = deepcopy(self.parameters) | 554 | 558 | parameters['view'] = DELTA_VIEW | | 559 | + # Having report_repetition as True can increase | | 560 | + # the number of operations to convert one object to the other dramatically | | 561 | + # and can easily cause the objects that could have been otherwise close in distance | | 562 | + # to be discarded as pairs. | | 563 | + # TODO: The rough distance calculator perhaps can use the repetitions report with a low weight | | 564 | + # in the future so that it can still be counted. | | 565 | + parameters['report_repetition'] = False | 555 | 566 | diff = DeepDiff(removed_hash_obj.item, added_hash_obj.item, parameters=parameters, hashes=self.hashes) | 556 | 567 | rough_distance = diff.get_rough_distance() | 557 | 568 | # Discard potential pairs that are too far. | @@ -585,8 +596,9 @@ def __get_most_in_common_pairs_in_iterables(self, hashes_added, hashes_removed, | 585 | 596 |
| 586 | 597 | def __diff_iterable_with_deephash(self, level, parents_ids): | 587 | 598 | """Diff of unhashable iterables. Only used when ignoring the order.""" | 588 | | - t1_hashtable = self.__create_hashtable(level.t1, level) | 589 | | - t2_hashtable = self.__create_hashtable(level.t2, level) | | 599 | + print(f'__diff_iterable_with_deephash for {level}') | | 600 | + t1_hashtable = self.__create_hashtable(level, 't1') | | 601 | + t2_hashtable = self.__create_hashtable(level, 't2') | 590 | 602 |
| 591 | 603 | t1_hashes = set(t1_hashtable.keys()) | 592 | 604 | t2_hashes = set(t2_hashtable.keys()) | @@ -596,6 +608,7 @@ def __diff_iterable_with_deephash(self, level, parents_ids): | 596 | 608 |
| 597 | 609 | pairs = self.__get_most_in_common_pairs_in_iterables( | 598 | 610 | hashes_added, hashes_removed, t1_hashtable, t2_hashtable) | | 611 | + print(f'found pairs: {pairs}') | 599 | 612 | inverse_pairs = {v: k for k, v in pairs.items()} | 600 | 613 | pairs.update(inverse_pairs) | 601 | 614 |
| @@ -752,16 +765,17 @@ def __diff_numpy_array(self, level, parents_ids=frozenset({})): | 752 | 765 | if np is None: | 753 | 766 | raise ImportError('Unable to import numpy. Please make sure it is installed.') | 754 | 767 |
| 755 | | - # fast checks | 756 | | - if self.significant_digits is None: | 757 | | - if np.array_equal(level.t1, level.t2): | 758 | | - return # all good | 759 | | - else: | 760 | | - try: | 761 | | - np.testing.assert_almost_equal(level.t1, level.t2, decimal=self.significant_digits) | 762 | | - return # all good | 763 | | - except AssertionError: | 764 | | - pass # do detailed checking below | | 768 | + if not self.ignore_order: | | 769 | + # fast checks | | 770 | + if self.significant_digits is None: | | 771 | + if np.array_equal(level.t1, level.t2): | | 772 | + return # all good | | 773 | + else: | | 774 | + try: | | 775 | + np.testing.assert_almost_equal(level.t1, level.t2, decimal=self.significant_digits) | | 776 | + return # all good | | 777 | + except AssertionError: | | 778 | + pass # do detailed checking below | 765 | 779 |
| 766 | 780 | # compare array meta-data | 767 | 781 | if level.t1.shape != level.t2.shape: | @@ -772,13 +786,19 @@ def __diff_numpy_array(self, level, parents_ids=frozenset({})): | 772 | 786 | self.__diff(next_level, parents_ids) | 773 | 787 | else: | 774 | 788 | # metadata same -- the difference is in the content | 775 | | - dimensions = len(level.t1.shape) | | 789 | + shape = level.t1.shape | | 790 | + dimensions = len(shape) | 776 | 791 | if dimensions == 1: | 777 | 792 | self.__diff_iterable(level, parents_ids) | | 793 | + elif self.ignore_order: | | 794 | + # convert to list | | 795 | + level.t1 = level.t1.tolist() | | 796 | + level.t2 = level.t2.tolist() | | 797 | + self.__diff_iterable(level, parents_ids) | 778 | 798 | else: | 779 | 799 | for (t1_path, t1_row), (t2_path, t2_row) in zip( | 780 | | - get_numpy_ndarray_rows(level.t1), | 781 | | - get_numpy_ndarray_rows(level.t2)): | | 800 | + get_numpy_ndarray_rows(level.t1, shape), | | 801 | + get_numpy_ndarray_rows(level.t2, shape)): | 782 | 802 |
| 783 | 803 | new_level = level.branch_deeper( | 784 | 804 | t1_row, | @@ -954,7 +974,10 @@ def to_delta_dict(self, directed=True, report_repetition_needed=True): | 954 | 974 |
| 955 | 975 | return result | 956 | 976 |
| 957 | | - def pretty_form(self): | | 977 | + def pretty(self): | | 978 | + """ | | 979 | + The pretty human readable string output for the diff object. | | 980 | + """ | 958 | 981 | result = [] | 959 | 982 | keys = sorted(self.tree.keys()) # sorting keys to guarantee constant order across python versions. | 960 | 983 | for key in keys: | |
Collapse file Copy file name to clipboardExpand all lines: deepdiff/helper.py +24-5Lines changed: 24 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -371,16 +371,35 @@ def cartesian_product_of_shape(dimentions, result=None): | 371 | 371 | return result | 372 | 372 |
| 373 | 373 |
| 374 | | -def get_numpy_ndarray_rows(obj): | | 374 | +def get_numpy_ndarray_rows(obj, shape=None): | 375 | 375 | """ | 376 | 376 | Convert a multi dimensional numpy array to list of rows | 377 | 377 | """ | 378 | | - dimentions = obj.shape[:-1] | 379 | | - for path in cartesian_product_of_shape(dimentions): | | 378 | + if shape is None: | | 379 | + shape = obj.shape | | 380 | + | | 381 | + dimentions = shape[:-1] | | 382 | + for path_tuple in cartesian_product_of_shape(dimentions): | 380 | 383 | result = obj | 381 | | - for index in path: | | 384 | + for index in path_tuple: | 382 | 385 | result = result[index] | 383 | | - yield path, result | | 386 | + yield path_tuple, result | | 387 | + | | 388 | + | | 389 | +class _NotFound: | | 390 | + | | 391 | + def __eq__(self, other): | | 392 | + return False | | 393 | + | | 394 | + __req__ = __eq__ | | 395 | + | | 396 | + def __repr__(self): | | 397 | + return 'not found' | | 398 | + | | 399 | + __str__ = __repr__ | | 400 | + | | 401 | + | | 402 | +not_found = _NotFound() | 384 | 403 |
| 385 | 404 |
| 386 | 405 | warnings.simplefilter('once', DeepDiffDeprecationWarning) |
Collapse file Copy file name to clipboardExpand all lines: deepdiff/model.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | from ast import literal_eval | 4 | 4 | from copy import copy | 5 | 5 | from ordered_set import OrderedSet | 6 | | -from deepdiff.helper import RemapDict, strings, short_repr, notpresent, get_type, numpy_numbers | | 6 | +from deepdiff.helper import RemapDict, strings, short_repr, notpresent, get_type, numpy_numbers, not_found | 7 | 7 |
| 8 | 8 | FORCE_DEFAULT = 'fake' | 9 | 9 | UP_DOWN = {'up': 'down', 'down': 'up'} | |
Collapse file Copy file name to clipboardExpand all lines: tests/test_delta.py +32-7Lines changed: 32 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -545,6 +545,31 @@ def test_delta_cases(self, t1, t2, deepdiff_kwargs, to_delta_kwargs, expected_de | 545 | 545 | }, | 546 | 546 | 'expected_t1_plus_delta': [{1, 2, 3}, {'hello', 'right!', 5}, {(2,), 4, 5, 6}], | 547 | 547 | }, | | 548 | + 'delta_ignore_order_case8_multi_dimensional_list': { | | 549 | + 't1': [[1, 2, 3, 4], [4, 2, 2, 1]], | | 550 | + 't2': [[4, 1, 1, 1], [1, 3, 2, 4]], | | 551 | + 'deepdiff_kwargs': { | | 552 | + 'ignore_order': True, | | 553 | + 'report_repetition': True | | 554 | + }, | | 555 | + 'to_delta_kwargs': {}, | | 556 | + 'expected_delta_dict': { | | 557 | + 'iterable_items_added_at_indexes': { | | 558 | + 'root[1]': { | | 559 | + 1: 1, | | 560 | + 2: 1, | | 561 | + 3: 1 | | 562 | + } | | 563 | + }, | | 564 | + 'iterable_items_removed_at_indexes': { | | 565 | + 'root[1]': { | | 566 | + 1: 2, | | 567 | + 2: 2 | | 568 | + } | | 569 | + } | | 570 | + }, | | 571 | + 'expected_t1_plus_delta': [[1, 2, 3, 4], [4, 1, 1, 1]], | | 572 | + }, | 548 | 573 | } | 549 | 574 |
| 550 | 575 |
| @@ -655,24 +680,20 @@ def test_ignore_order_delta_cases( | 655 | 680 | 'expected_delta_dict': { | 656 | 681 | 'iterable_items_added_at_indexes': { | 657 | 682 | 'root[1]': { | 658 | | - 1: 3, | 659 | | - 2: 2 | 660 | | - }, | 661 | | - 'root[0]': { | 662 | 683 | 1: 1, | 663 | 684 | 2: 1, | 664 | 685 | 3: 1 | 665 | 686 | } | 666 | 687 | }, | 667 | 688 | 'iterable_items_removed_at_indexes': { | 668 | | - 'root[0]': { | | 689 | + 'root[1]': { | 669 | 690 | 1: 2, | 670 | | - 2: 3 | | 691 | + 2: 2 | 671 | 692 | } | 672 | 693 | }, | 673 | 694 | 'numpy_used': True | 674 | 695 | }, | 675 | | - 'expected_result': 't2' | | 696 | + 'expected_result': 't2_via_deepdiff' | 676 | 697 | }, | 677 | 698 | } | 678 | 699 |
| @@ -693,6 +714,10 @@ def test_numpy_delta_cases(self, t1, t2, deepdiff_kwargs, to_delta_kwargs, expec | 693 | 714 | if expected_result == 't2': | 694 | 715 | result = delta + t1 | 695 | 716 | assert np.array_equal(result, t2) | | 717 | + elif expected_result == 't2_via_deepdiff': | | 718 | + result = delta + t1 | | 719 | + diff = DeepDiff(result, t2, ignore_order=True, report_repetition=True) | | 720 | + assert not diff | 696 | 721 | elif expected_result is DeltaNumpyOperatorOverrideError: | 697 | 722 | with pytest.raises(DeltaNumpyOperatorOverrideError): | 698 | 723 | assert t1 + delta | |
Collapse file Copy file name to clipboardExpand all lines: tests/test_diff_numpy.py +43Lines changed: 43 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 62 | 62 | 'repetition_change': {'root[2]': {'old_repeat': 2, 'new_repeat': 1, | 63 | 63 | 'old_indexes': [2, 3], 'new_indexes': [0], 'value': 3}}}, | 64 | 64 | }, | | 65 | + 'numpy_array7_ignore_order_multi_dimensional_array': { | | 66 | + 't1': np.array([[1, 2, 3, 4], [4, 2, 2, 1]], np.int8), | | 67 | + 't2': np.array([[4, 1, 1, 1], [1, 3, 2, 4]], np.int8), | | 68 | + 'deepdiff_kwargs': {'report_repetition': True, 'ignore_order': True}, | | 69 | + 'expected_result': { | | 70 | + 'iterable_item_removed': { | | 71 | + 'root[1][1]': 2, | | 72 | + 'root[1][2]': 2 | | 73 | + }, | | 74 | + 'repetition_change': { | | 75 | + 'root[1][3]': { | | 76 | + 'old_repeat': 1, | | 77 | + 'new_repeat': 3, | | 78 | + 'old_indexes': [3], | | 79 | + 'new_indexes': [1, 2, 3], | | 80 | + 'value': 1 | | 81 | + } | | 82 | + } | | 83 | + }, | | 84 | + }, | | 85 | + 'numpy_array8_ignore_order_multi_dimensional_array_converted_to_list': { | | 86 | + 't1': np.array([[1, 2, 3, 4], [4, 2, 2, 1]], np.int8).tolist(), | | 87 | + 't2': np.array([[4, 1, 1, 1], [1, 3, 2, 4]], np.int8).tolist(), | | 88 | + 'deepdiff_kwargs': { | | 89 | + 'report_repetition': True, | | 90 | + 'ignore_order': True | | 91 | + }, | | 92 | + 'expected_result': { | | 93 | + 'iterable_item_removed': { | | 94 | + 'root[1][1]': 2, | | 95 | + 'root[1][2]': 2 | | 96 | + }, | | 97 | + 'repetition_change': { | | 98 | + 'root[1][3]': { | | 99 | + 'old_repeat': 1, | | 100 | + 'new_repeat': 3, | | 101 | + 'old_indexes': [3], | | 102 | + 'new_indexes': [1, 2, 3], | | 103 | + 'value': 1 | | 104 | + } | | 105 | + } | | 106 | + }, | | 107 | + }, | 65 | 108 | } | 66 | 109 |
| 67 | 110 |
| |
|