Skip to content

Commit

Permalink
improved docs and tests surrounding roi
Browse files Browse the repository at this point in the history
  • Loading branch information
alasdairwilson committed Jul 11, 2024
1 parent 1e33db7 commit dc818e8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
8 changes: 8 additions & 0 deletions examples/io/creating_rippleimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
7 changes: 7 additions & 0 deletions examples/io/creating_rippleimageseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
8 changes: 4 additions & 4 deletions ripplemapper/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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}")

Expand Down
12 changes: 12 additions & 0 deletions ripplemapper/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

0 comments on commit dc818e8

Please sign in to comment.