diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..67662f5 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,3 @@ +[run] + +omit = tests/* diff --git a/.gitignore b/.gitignore index b830d6f..4688cab 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ __pycache__ /build *.swp tests/*-out-* -/.venv \ No newline at end of file +/.venv +/.coverage \ No newline at end of file diff --git a/README.rst b/README.rst index c640c8d..6eeb423 100644 --- a/README.rst +++ b/README.rst @@ -15,3 +15,5 @@ Galaxy Image Analysis: https://github.com/BMCV/galaxy-image-analysis Use ``python -m unittest`` in the root directory of the repository to run the test suite. + +Use ``coverage run -m unittest && coverage html`` to generate a coverage report. diff --git a/giatools/io.py b/giatools/io.py index caab622..4af40fa 100644 --- a/giatools/io.py +++ b/giatools/io.py @@ -25,7 +25,7 @@ def imread(*args, **kwargs): tool has failed: https://docs.galaxyproject.org/en/latest/dev/schema.html#error-detection To prevent this, this wrapper around ``skimage.io.imread`` will mute all non-fatal errors. - Image loading is first attempted using ``tifffile`` (if available, more reliable for loading TIFF files), and if + Image loading is first attempted using `tifffile` (if available, more reliable for loading TIFF files), and if that fails (e.g., because the file is not a TIFF file), falls back to ``skimage.io.imread``. """ diff --git a/tests/data/input4.png b/tests/data/input4.png new file mode 100644 index 0000000..5b7b472 Binary files /dev/null and b/tests/data/input4.png differ diff --git a/tests/test_io.py b/tests/test_io.py index 56a208d..f5faebd 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -1,8 +1,13 @@ import unittest +import unittest.mock import giatools.io +# This tests require that the `tifffile` package is installed. +assert giatools.io.tifffile is not None + + class imread(unittest.TestCase): def test_input1(self): @@ -16,10 +21,28 @@ def test_input2(self): def test_input3(self): """ This is a multi-page TIFF, that sometimes fails to load properly with ``skimage.io.imread``, but works with - ``tifffile``. + `tifffile`. For details see: https://github.com/BMCV/galaxy-image-analysis/pull/132#issuecomment-2371561435 """ img = giatools.io.imread('tests/data/input3.tif') self.assertEqual(img.shape, (5, 198, 356)) self.assertEqual(img.mean(), 1259.6755334241288) + + def test_input4(self): + """ + This is an RGB PNG file, that cannot be loaded with `tifffile`, but works with ``skimage.io.imread``. + """ + img = giatools.io.imread('tests/data/input4.png') + self.assertEqual(img.shape, (10, 10, 3)) + self.assertEqual(img.mean(), 130.04) + + @unittest.mock.patch('skimage.io.imread') + @unittest.mock.patch('giatools.io.tifffile', None) + def test_without_tifffile(self, mock_skimage_io_imread): + """ + Test that loading an image without `tifffile` installed falls back to ``skimage.io.imread``. + """ + img = giatools.io.imread('tests/data/input1.tif') + mock_skimage_io_imread.assert_called_once_with('tests/data/input1.tif') + self.assertIs(img, mock_skimage_io_imread.return_value)