diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 3c68e212..88057a6d 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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 ----- diff --git a/matplotcheck/base.py b/matplotcheck/base.py index bef8cec2..3f896c47 100644 --- a/matplotcheck/base.py +++ b/matplotcheck/base.py @@ -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 ---------- @@ -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` @@ -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 @@ -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, ) @@ -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` @@ -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: diff --git a/matplotcheck/tests/test_base_data.py b/matplotcheck/tests/test_base_data.py index a9cea54d..2a09729c 100644 --- a/matplotcheck/tests/test_base_data.py +++ b/matplotcheck/tests/test_base_data.py @@ -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() @@ -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 tolerence.""" 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 tolerence.""" 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()