Skip to content

Commit

Permalink
rename and integration of diff in agg base
Browse files Browse the repository at this point in the history
  • Loading branch information
juanbc committed Jan 16, 2024
1 parent 4d5e570 commit 59cf66f
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
``skcriteria.utils.npdict_cmp`` module
``skcriteria.utils.dict_cmp`` module
========================================

.. automodule:: skcriteria.utils.npdict_cmp
.. automodule:: skcriteria.utils.dict_cmp
:members:
:undoc-members:
:show-inheritance:
4 changes: 2 additions & 2 deletions skcriteria/agg/_agg_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
deprecated,
diff,
doc_inherit,
npdict_all_equals,
dict_allclose,
)

# =============================================================================
Expand Down Expand Up @@ -204,7 +204,7 @@ def array_allclose(left_value, right_value):
"method": np.array_equal,
"alternatives": np.array_equal,
"values": array_allclose,
"extra_": npdict_all_equals,
"extra_": dict_allclose,
}

the_diff = diff(self, other, **members)
Expand Down
2 changes: 1 addition & 1 deletion skcriteria/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from .cmanagers import df_temporal_header, hidden
from .deprecate import deprecated, will_change
from .doctools import doc_inherit
from .npdict_cmp import npdict_all_equals
from .dict_cmp import dict_allclose
from .object_diff import DiffEqualityMixin, diff
from .unames import unique_names

Expand Down
90 changes: 90 additions & 0 deletions skcriteria/utils/dict_cmp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# License: BSD-3 (https://tldrlegal.com/license/bsd-3-clause-license-(revised))
# Copyright (c) 2016-2021, Cabral, Juan; Luczywo, Nadia
# Copyright (c) 2022, 2023, 2024 QuatroPe
# All rights reserved.

# =============================================================================
# DOCS
# =============================================================================

"""Utilities to compare two dictionaries with numpy arrays."""

# =============================================================================
# IMPORTS
# =============================================================================

import numpy as np

# =============================================================================
# CLASSES
# =============================================================================

import numpy as np

def dict_allclose(left, right, rtol=1e-05, atol=1e-08, equal_nan=False):
"""Compares two dictionaries. If values of type "numpy.array" are \
encountered, the function utilizes "numpy.allclose" for comparison.
Parameters
----------
left : dict
The left dictionary.
right : dict
The right dictionary.
rtol : float, optional
The relative tolerance parameter for `np.allclose`.
atol : float, optional
The absolute tolerance parameter for `np.allclose`.
equal_nan : bool, optional
Whether to consider NaN values as equal.
Returns
-------
bool
True if the dictionaries are equal, False otherwise.
Notes
-----
This function iteratively compares the values of corresponding keys in the
input dictionaries `left` and `right`. It handles various data types,
including NumPy arrays, and uses the `np.allclose` function for numeric
array comparisons with customizable tolerance levels. The comparison is
performed iteratively, and the function returns True if all values are
equal based on the specified criteria. If the dictionaries have different
lengths or keys, or if the types of corresponding values differ, the
function returns False.
"""
if left is right: # if they are the same object, return True
return True

# Extra keys
keys = set(left).union(right)

# If the keys are not the same on both sides, return False
if not (len(keys) == len(left) == len(right)):
return False

is_equal = True # Flag to check if all keys are equal, optimist
while is_equal and keys: # Loop until all keys are equal
key = keys.pop()
left_value, right_value = left[key], right[key]

if type(left_value) is not type(right_value):
is_equal = False

elif isinstance(left_value, np.ndarray):
is_equal = np.allclose(
left_value,
right_value,
rtol=rtol,
atol=atol,
equal_nan=equal_nan,
)

else:
is_equal = left_value == right_value

return is_equal
58 changes: 0 additions & 58 deletions skcriteria/utils/npdict_cmp.py

This file was deleted.

21 changes: 11 additions & 10 deletions tests/utils/test_npdict_cmp.py → tests/utils/test_dict_cmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# DOCS
# =============================================================================

"""test for skcriteria.utils.npdict_cmp
"""test for skcriteria.utils.dict_cmp
"""

Expand All @@ -21,32 +21,33 @@
import numpy as np


from skcriteria.utils import npdict_cmp
from skcriteria.utils import dict_cmp


# =============================================================================
# The tests
# =============================================================================


def test_npdict_all_equals_():
def test_dict_allclose():
left = {"a": np.array([1, 2, 3]), "b": np.array([4, 5, 6]), "c": 1}
right = {"a": np.array([1, 2, 3]), "b": np.array([4, 5, 6]), "c": 1}
assert npdict_cmp.npdict_all_equals(left, right)
assert dict_cmp.dict_allclose(left, right)


def test_npdict_all_equals_same_obj():
def test_dict_allclose_same_obj():
dict0 = {"a": np.array([1, 2, 3]), "b": np.array([4, 5, 6]), "c": 1}
assert npdict_cmp.npdict_all_equals(dict0, dict0)
assert dict_cmp.dict_allclose(dict0, dict0)


def test_npdict_all_equals_different_keys():
def test_dict_allclose_different_keys():
left = {"a": np.array([1, 2, 3]), "b": np.array([4, 5, 6]), "c": 1}
right = {"a": np.array([1, 2, 3]), "b": np.array([4, 5, 6]), "d": 1}
assert npdict_cmp.npdict_all_equals(left, right) is False
assert dict_cmp.dict_allclose(left, right) is False


def test_npdict_all_equals_different_types():
def test_dict_allclose_different_types():
left = {"a": np.array([1, 2, 3]), "b": np.array([4, 5, 6]), "c": 1}
right = {"a": np.array([1, 2, 3]), "b": np.array([4, 5, 6]), "c": 1.0}
assert npdict_cmp.npdict_all_equals(left, right) is False
assert dict_cmp.dict_allclose(left, right) is False

0 comments on commit 59cf66f

Please sign in to comment.