From dc818e8b0ff832420ab86bd59e07966f510197e4 Mon Sep 17 00:00:00 2001 From: Alasdair Wilson Date: Thu, 11 Jul 2024 14:55:13 +0100 Subject: [PATCH] improved docs and tests surrounding roi --- examples/io/creating_rippleimage.py | 8 ++++++++ examples/io/creating_rippleimageseries.py | 7 +++++++ ripplemapper/io.py | 8 ++++---- ripplemapper/tests/test_io.py | 12 ++++++++++++ 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/examples/io/creating_rippleimage.py b/examples/io/creating_rippleimage.py index f3909d0..f8ee06c 100644 --- a/examples/io/creating_rippleimage.py +++ b/examples/io/creating_rippleimage.py @@ -11,3 +11,11 @@ ripple_img = RippleImage(example_data[-1]) print(ripple_img) + +################################################################################ +# We can optionally select a region of interest via roi_x and roi_y keyword arguments, to cut out unnecessary parts of the image. +# These keywords are consistent in the directory loading methods. + +print(ripple_img.image.shape) +ripple_img = RippleImage(example_data[-1], roi_x=[0, 100], roi_y=[0, 100]) +print(ripple_img.image.shape) diff --git a/examples/io/creating_rippleimageseries.py b/examples/io/creating_rippleimageseries.py index 0592906..494f328 100644 --- a/examples/io/creating_rippleimageseries.py +++ b/examples/io/creating_rippleimageseries.py @@ -14,3 +14,10 @@ image_series = RippleImageSeries(imgs) print(image_series) + +################################################################################ +# +# Just like with RippleImages we can select a Region of Interest when loading the image files. + +imgs_with_roi = load_dir_to_obj(example_dir, roi_x=[0, 100], roi_y=[0, 100]) +print(imgs[0].image.shape, imgs_with_roi[0].image.shape) diff --git a/ripplemapper/io.py b/ripplemapper/io.py index b4a1348..017805a 100644 --- a/ripplemapper/io.py +++ b/ripplemapper/io.py @@ -8,7 +8,7 @@ __all__ = ["load_image", "load_tif", "load_dir", "load_dir_to_obj", "load"] -def load(file: str | PosixPath | WindowsPath): +def load(file: str | PosixPath | WindowsPath, **kwargs): """ Load a file into a ripplemapper object based on file extension. @@ -33,11 +33,11 @@ def load(file: str | PosixPath | WindowsPath): if isinstance(file, PosixPath) | isinstance(file, WindowsPath): file = str(file.resolve()) if file.endswith(".txt") | file.endswith(".json"): - return RippleContour(file) + return RippleContour(file, **kwargs) elif file.endswith(".rimg") | file.endswith(".tif") | file.endswith(".tiff"): - return RippleImage(file) + return RippleImage(file, **kwargs) elif file.endswith(".rimgs"): - return RippleImageSeries(file) + return RippleImageSeries(file, **kwargs) else: raise ValueError(f"Unsupported file type: {file}") diff --git a/ripplemapper/tests/test_io.py b/ripplemapper/tests/test_io.py index 0bf00eb..7b54cf0 100644 --- a/ripplemapper/tests/test_io.py +++ b/ripplemapper/tests/test_io.py @@ -20,6 +20,11 @@ def test_load_ripple_image(): assert image.source_file == str(example_data[0].resolve()) assert isinstance(image, RippleImage) +def test_load_with_roi(): + image = load(example_data[0], roi_x=[0,50], roi_y=[0,100]) + assert image.image is not None + assert image.image.shape == (50, 100) + def test_load_ripple_image_series(): series = load(example_rimgs) assert len(series.images) == 4 @@ -48,5 +53,12 @@ def test_load_dir_to_obj(): assert images[0].image is not None assert isinstance(images[0], RippleImage) +def test_load_dir_to_obj_with_roi(): + images = load_dir_to_obj(example_dir, roi_x=[0, 50], roi_y=[0, 100]) + assert len(images) > 0 + assert images[0].image is not None + assert images[0].image.shape == (50, 100) + assert isinstance(images[0], RippleImage) + if __name__ == '__main__': pytest.main()