diff --git a/CHANGES.md b/CHANGES.md index eee5e7f..2dacf1d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,6 +3,9 @@ - Add ability to compare to Pandas DataFrames and store them as HDF5 files [#23] +- Fix ``array_compare`` so that the ``atol`` parameter is correctly used with + FITS files. [#33] + 0.5 (2022-01-12) ---------------- diff --git a/README.rst b/README.rst index 39dd16b..48136ea 100644 --- a/README.rst +++ b/README.rst @@ -111,12 +111,12 @@ The supported formats at this time are ``text`` and ``fits``, and contributions for other formats are welcome. The default format is ``text``. -Another argument is the relative tolerance for floating point values -(which defaults to 1e-7): +Additional arguments are the relative and absolute tolerances for floating +point values (which default to 1e-7 and 0, respectively): .. code:: python - @pytest.mark.array_compare(rtol=20) + @pytest.mark.array_compare(rtol=20, atol=0.1) def test_array(): ... diff --git a/pytest_arraydiff/plugin.py b/pytest_arraydiff/plugin.py index c80d008..da78dfb 100755 --- a/pytest_arraydiff/plugin.py +++ b/pytest_arraydiff/plugin.py @@ -116,8 +116,9 @@ def compare(cls, reference_file, test_file, atol=None, rtol=None): from astropy.io.fits.diff import FITSDiff from astropy.utils.introspection import minversion if minversion(astropy, '2.0'): - diff = FITSDiff(reference_file, test_file, rtol=rtol) + diff = FITSDiff(reference_file, test_file, rtol=rtol, atol=atol) else: + # `atol` is not supported prior to Astropy 2.0 diff = FITSDiff(reference_file, test_file, tolerance=rtol) return diff.identical, diff.report() diff --git a/tests/baseline/test_tolerance.fits b/tests/baseline/test_absolute_tolerance.fits similarity index 100% rename from tests/baseline/test_tolerance.fits rename to tests/baseline/test_absolute_tolerance.fits diff --git a/tests/baseline/test_relative_tolerance.fits b/tests/baseline/test_relative_tolerance.fits new file mode 100644 index 0000000..9fab1ae Binary files /dev/null and b/tests/baseline/test_relative_tolerance.fits differ diff --git a/tests/test_pytest_arraydiff.py b/tests/test_pytest_arraydiff.py index 12f133f..e974706 100644 --- a/tests/test_pytest_arraydiff.py +++ b/tests/test_pytest_arraydiff.py @@ -132,9 +132,20 @@ def test_default_format(file_format): assert os.path.exists(os.path.join(gen_dir, 'test_default.' + ('fits' if file_format == 'fits' else 'txt'))) -@pytest.mark.array_compare(reference_dir=reference_dir, rtol=0.5, file_format='fits') -def test_tolerance(): - return np.ones((3, 4)) * 1.6 +@pytest.mark.array_compare(reference_dir=reference_dir, rtol=0.5, + file_format='fits') +def test_relative_tolerance(): + # Scale up the output values by 1.5 to ensure the large `rtol` value is + # needed. (The comparison file contains all 1.6.) + return np.ones((3, 4)) * 1.6 * 1.5 + + +@pytest.mark.array_compare(reference_dir=reference_dir, atol=1.5, + file_format='fits') +def test_absolute_tolerance(): + # Increase the output values by 1.4 to ensure the large `atol` value is + # needed. (The comparison file contains all 1.6.) + return np.ones((3, 4)) * 1.6 + 1.4 def test_nofile():