Skip to content
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: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Unreleased
in them (@nkorinek, #182)
- added contributors file and updated README to remove that information
(@nkorinek, #121)
- Changed tolerance functionality from relative tolerance to absolute
tolerance. (@ryla5068, #234)

0.1.2
-----
Expand Down
5 changes: 3 additions & 2 deletions examples/plot_test_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,10 @@

###############################################################################
# However, if you set a tolerance, the assertion can pass. Here you will test
# it with ``tolerance=0.2``.
# it with ``tolerance=6``, as that is the maximum difference between the two
# datasets.

plot_tester_testing_2.assert_bin_values(bins_expected_1, tolerance=0.2)
plot_tester_testing_2.assert_bin_values(bins_expected_1, tolerance=6)

###############################################################################
# Because no ``AssertionError`` is raised, you know that the test passed with
Expand Down
38 changes: 19 additions & 19 deletions matplotcheck/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -833,11 +833,12 @@ def assert_xydata(
points_only=False,
xtime=False,
xlabels=False,
tolerence=0,
tolerance=0,
message="Incorrect data values",
):
"""Asserts that the x and y data of Axes `ax` matches `xy_expected`
with error message `m`. If ``xy_expected = None``, assertion is passed.
with error message `message`. If ``xy_expected = None``,
assertion is passed.

Parameters
----------
Expand All @@ -864,12 +865,11 @@ def assert_xydata(
Set ``True`` if using x axis labels rather than x data. Instead of
comparing numbers in the x-column to expected, compares numbers or
text in x labels to expected.
tolerence : float
Measure of relative error allowed.
For example: Given a tolerance ``tolerence=0.1``, an expected value
``e``, and an actual value ``a``, this asserts
``abs(a - e) < (e * 0.1)``. (This uses `np.testing.assert_allclose`
with ``rtol=tolerence`` and ``atol=inf``.)
tolerance : float
A non-zero value of tol_rel allows an absolute tolerance when
checking the data. For example, a tolerance of 0.1 would
check that the actual data is within 0.1 units of the actual data.
Note that the units for datetime data is always days.
message : string
The error message to be displayed if the xy-data does not match
`xy_expected`
Expand All @@ -878,7 +878,8 @@ def assert_xydata(
Raises
-------
AssertionError
with message `m` if legends overlap
with message `message`, if x and y data of Axes `ax` does not match
`xy_expected`
"""
if xy_expected is None:
return
Expand Down Expand Up @@ -913,19 +914,19 @@ def assert_xydata(
xy_expected.sort_values(by=xcol),
)

if tolerence > 0:
if tolerance > 0:
if xtime:
raise ValueError("tolerance must be 0 with datetime on x-axis")
np.testing.assert_allclose(
xy_data["x"],
xy_expected[xcol],
rtol=tolerence,
atol=tolerance,
err_msg=message,
)
np.testing.assert_allclose(
xy_data["y"],
xy_expected[ycol],
rtol=tolerence,
atol=tolerance,
err_msg=message,
)

Expand Down Expand Up @@ -1239,12 +1240,11 @@ def assert_bin_values(
bin_values : list
A list of numbers representing the expected values of each
consecutive bin (i.e. the heights of the bars in the histogram).
tolerence : float
Measure of relative error allowed.
For example: Given a tolerance ``tolerence=0.1``, an expected value
``e``, and an actual value ``a``, this asserts
``abs(a - e) < (e * 0.1)``. (This uses `np.testing.assert_allclose`
with ``rtol=tolerence`` and ``atol=inf``.)
tolerance : float
A non-zero value of tol_abs allows an absolute tolerance when
checking the bin values. For example, an absolute tolerance of 1
checks that the actual bin values do not differ from the expected
bin values by more than 1.
message : string
The error message to be displayed if the bin values do not match
`bin_values`
Expand All @@ -1271,7 +1271,7 @@ def assert_bin_values(
np.testing.assert_allclose(
plot_bin_values,
expected_bin_values,
rtol=tolerance,
atol=tolerance,
err_msg=message,
)
except AssertionError:
Expand Down
31 changes: 15 additions & 16 deletions matplotcheck/tests/test_base_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,20 @@ def test_assert_xydata_scatter(pt_scatter_plt, pd_df):

def test_assert_xydata_tolerance(pt_scatter_plt, pd_df):
"""Checks that slightly altered data still passes with an appropriate
tolerance"""
tolerence"""
pd_df = pd_df.astype(np.float)
for i in range(len(pd_df["A"])):
pd_df["A"][i] = pd_df["A"][i] + (np.floor(pd_df["A"][i] * 0.25))
pd_df["B"][i] = pd_df["B"][i] + (np.floor(pd_df["B"][i] * 0.25))
pt_scatter_plt.assert_xydata(pd_df, xcol="A", ycol="B", tolerence=0.5)
pd_df["A"][i] = pd_df["A"][i] + np.random.choice([-0.1, 0.1])
pd_df["B"][i] = pd_df["B"][i] + np.random.choice([-0.1, 0.1])
pt_scatter_plt.assert_xydata(pd_df, xcol="A", ycol="B", tolerance=0.2)
plt.close()


def test_assert_xydata_tolerance_fail(pt_scatter_plt, pd_df):
"""Checks that data altered beyond the tolerance throws an assertion."""
pd_df["A"][1] = pd_df["A"][1] * 2
def test_assert_xydata_abs_tolerance_fails(pt_scatter_plt, pd_df):
"""Checks that data altered beyond the tolerence correctly fails."""
pd_df["A"][1] = pd_df["A"][1] + 1
with pytest.raises(AssertionError, match="Incorrect data values"):
pt_scatter_plt.assert_xydata(pd_df, xcol="A", ycol="B", tolerence=0.1)
pt_scatter_plt.assert_xydata(pd_df, xcol="A", ycol="B", tolerance=0.1)
plt.close()


Expand Down Expand Up @@ -351,28 +352,26 @@ def test_assert_bin_values_incorrect(pt_hist_overlapping):


def test_assert_bin_values_tolerance(pt_hist_overlapping):
"""Test that assert_bin_values correctly passes when using tolerance
flag."""
"""Test that assert_bin_values correctly passes when using tolerance."""
bin_values = pt_hist_overlapping.get_bin_values()
for i in range(len(bin_values)):
bin_values[i] = bin_values[i] * 1.1
bin_values[i] = bin_values[i] + 1

pt_hist_overlapping.assert_bin_values(bin_values, tolerance=0.11)
pt_hist_overlapping.assert_bin_values(bin_values, tolerance=2)

plt.close()


def test_assert_bin_values_tolerance_fails(pt_hist_overlapping):
"""Test that assert_bin_values correctly fails when using tolerance
flag."""
"""Test that assert_bin_values correctly fails when using tolerance."""
bin_values = pt_hist_overlapping.get_bin_values()
for i in range(len(bin_values)):
bin_values[i] = bin_values[i] * 1.1
bin_values[i] = bin_values[i] + 1

with pytest.raises(
AssertionError, match="Did not find expected bin values in plot"
):
pt_hist_overlapping.assert_bin_values(bin_values, tolerance=0.09)
pt_hist_overlapping.assert_bin_values(bin_values, tolerance=0.5)

plt.close()

Expand Down