Skip to content

Commit

Permalink
asset_dmatrix_equals now uses the diff protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
juanbc committed Jan 11, 2024
1 parent ae52fa1 commit 79723b3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 118 deletions.
28 changes: 0 additions & 28 deletions skcriteria/core/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,34 +737,6 @@ def aequals(
:py:func:`numpy.allclose`.
"""

is_aequals = (self is other) or (
isinstance(other, DecisionMatrix)
and np.shape(self) == np.shape(other)
and np.array_equal(self.criteria, other.criteria)
and np.array_equal(self.alternatives, other.alternatives)
and np.array_equal(self.objectives, other.objectives)
and np.allclose(
self.weights,
other.weights,
rtol=rtol,
atol=atol,
equal_nan=equal_nan,
)
and np.allclose(
self.matrix,
other.matrix,
rtol=rtol,
atol=atol,
equal_nan=equal_nan,
)
)

if check_dtype:
is_aequals = is_aequals and np.array_equal(
self.dtypes, other.dtypes
)

the_diff = self.diff(
other,
rtol=rtol,
Expand Down
124 changes: 34 additions & 90 deletions skcriteria/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,110 +40,54 @@
# CONSTANTS
# =============================================================================

def assert_dmatrix_equals(
left,
right,
*,
matrix_kws=None,
objectives_kws=None,
weights_kws=None,
):
"""Asserts that two instances of the DecisionMatrix class are equal.

def _assert(cond, err_msg):
"""Asserts that a condition is true, otherwise raises an AssertionError \
with a specified error message.
This function exists to prevent asserts from being turned off with a
"python -O."
Parameters
----------
left : DecisionMatrix
The first DecisionMatrix instance for comparison.
right : DecisionMatrix
The second DecisionMatrix instance for comparison.
matrix_kws : dict, optional
Keyword arguments for pandas.testing.assert_frame_equal
when comparing the matrix attribute. Default is an empty dict.
objectives_kws : dict, optional
Keyword arguments for pandas.testing.assert_series_equal
when comparing the objectives attribute. Default is an empty dict.
weights_kws : dict, optional
Keyword arguments for pandas.testing.assert_series_equal
when comparing the weights attribute. Default is an empty dict.
cond : bool
The condition to be evaluated.
err_msg : str
The error message to be raised if the condition is false.
Raises
------
AssertionError
If any of the specified attributes of the two DecisionMatrix instances
are not equal.
"""
if not cond:
raise AssertionError(err_msg)

Notes
-----
This function uses NumPy and pandas testing utilities for array and
DataFrame comparisons.
By default, the comparison of the decision matrix (matrix attribute)
does not check the columns dtypes (`check_dtype` is set to False) and
allows for some tolerance (`check_exact` is set to False) when using the
pandas testing function `assert_frame_equal`. Similarly, the comparison of
objectives and weights using the pandas testing function
`assert_series_equal` does not check the data type (`check_dtype` is set to
False) and allows for some tolerance (`check_exact` is set to False) by
default.

Example
-------
>>> assert_dmatrix_equals(
... matrix1, matrix2,
... matrix_kws={'check_dtype': True},
... weights_kws={'check_exact': True})
def assert_dmatrix_equals(left, right, **diff_kws):
"""Asserts that two instances of the DecisionMatrix class are equal."""
_assert(
isinstance(left, DecisionMatrix),
f"'left' is not a DecisionMatrix instance. Found {type(left)!r}",
)

"""
assert isinstance(
left, DecisionMatrix
), f"'left' is not a DecisionMatrix instance. Found {type(left)!r}"
assert isinstance(
right, DecisionMatrix
), f"'right' is not a DecisionMatrix instance. Found {type(right)!r}"
diff = left.diff(right, **diff_kws)

# if the objects are the same, no need to run the test
if left is right:
if not diff.has_differences:
return

# Check equality of alternatives and criteria arrays
npt.assert_array_equal(
np.asarray(left.alternatives),
np.asarray(right.alternatives),
err_msg="Alternatives are not equal",
)
npt.assert_array_equal(
np.asarray(left.criteria),
np.asarray(right.criteria),
err_msg="Criteria are not equal",
_assert(
diff.right_type is DecisionMatrix,
f"'right' is not a DecisionMatrix instance. Found {type(right)!r}",
)

# setup the defaults
matrix_kws = {} if matrix_kws is None else matrix_kws
objectives_kws = {} if objectives_kws is None else objectives_kws
weights_kws = {} if weights_kws is None else weights_kws

# Check equality of decision matrix DataFrame
matrix_kws.setdefault("check_dtype", False)
matrix_kws.setdefault("check_exact", False),
pdt.assert_frame_equal(
left.matrix,
right.matrix,
obj=DecisionMatrix.__name__,
**matrix_kws,
assert("shape" not in diff.members_diff, "'shape' are not equal")
_assert("criteria" not in diff.members_diff, "'criteria' are not equal")
_assert(
"alternatives" not in diff.members_diff, "'alternatives' are not equal"
)

# Check equality of objectives and weights Series
objectives_kws.setdefault("check_exact", False),
objectives_kws.setdefault("check_dtype", False),
pdt.assert_series_equal(
left.objectives, right.objectives, obj="Objectives", **objectives_kws
)

objectives_kws.setdefault("check_dtype", False),
objectives_kws.setdefault("check_exact", False),
pdt.assert_series_equal(
left.weights, right.weights, obj="Weights", **weights_kws
_assert(
"objectives" not in diff.members_diff, "'objectives' are not equal"
)
_assert("weights" not in diff.members_diff, "'weights' are not equal")
_assert("matrix" not in diff.members_diff, "'matrix' are not equal")
_assert("dtypes" not in diff.members_diff, "'dtypes' are not equal")


def assert_result_equals(left, right):
Expand Down

0 comments on commit 79723b3

Please sign in to comment.