Skip to content

Commit

Permalink
add pyramid_level_coord to get_image_ROI and get_label_ROI
Browse files Browse the repository at this point in the history
  • Loading branch information
mbstadler committed Feb 8, 2024
1 parent 20b9ca2 commit 37171e9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
33 changes: 33 additions & 0 deletions src/ez_zarr/hcs_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ def get_image_ROI(self,
well: Optional[str]=None,
image_name: str='0',
pyramid_level: Optional[int]=None,
pyramid_level_coord: Optional[int]=None,
as_NumPy: bool=False) -> Union[dask.array.Array, np.ndarray]:
"""
Extract a region of interest from a well image by coordinates.
Expand All @@ -343,6 +344,9 @@ def get_image_ROI(self,
pyramid_level (int): The pyramid level (resolution level), from which the image
should be extracted. If `None`, the lowest-resolution (highest) pyramid level
will be selected.
pyramid_level_coord (int): An optional integer scalar giving the image pyramid level
to which the coordinates (`upper_left_yx`, `lower_right_yx` and `size_yx`)
refer to. By default, this is `None`, which will use `pyramid_level`.
as_NumPy (bool): If `True`, return the image as 4D `numpy.ndarray` object (c,z,y,x).
Otherwise, return the (on-disk) `dask` array of the same dimensions.
Expand Down Expand Up @@ -373,6 +377,19 @@ def get_image_ROI(self,
elif not lower_right_yx:
lower_right_yx = tuple(upper_left_yx[i] + size_yx[i] for i in range(2))
assert all([upper_left_yx[i] < lower_right_yx[i] for i in range(len(upper_left_yx))]), 'upper_left_yx needs to be less than lower_right_yx'

# convert coordinates if needed
if pyramid_level_coord != None and pyramid_level != pyramid_level_coord:
upper_left_yx = self.convert_pixel_to_pixel(zyx=((0,) + upper_left_yx),
pyramid_level_from=pyramid_level_coord,
pyramid_level_to=pyramid_level,
pyramid_ref_from=('image', image_name),
pyramid_ref_to=('image', image_name))[1:]
lower_right_yx = self.convert_pixel_to_pixel(zyx=((0,) + lower_right_yx),
pyramid_level_from=pyramid_level_coord,
pyramid_level_to=pyramid_level,
pyramid_ref_from=('image', image_name),
pyramid_ref_to=('image', image_name))[1:]
img = img[:,
:,
slice(upper_left_yx[0], lower_right_yx[0] + 1),
Expand Down Expand Up @@ -572,6 +589,7 @@ def get_label_ROI(self,
well: Optional[str]=None,
image_name: str='0',
pyramid_level: Optional[int]=None,
pyramid_level_coord: Optional[int]=None,
upper_left_yx: Optional[tuple[int]]=None,
lower_right_yx: Optional[tuple[int]]=None,
size_yx: Optional[tuple[int]]=None,
Expand All @@ -593,6 +611,9 @@ def get_label_ROI(self,
pyramid_level (int): The pyramid level (resolution level), from which the image
should be extracted. If `None`, the lowest-resolution (highest) pyramid level
will be selected.
pyramid_level_coord (int): An optional integer scalar giving the image pyramid level
to which the coordinates (`upper_left_yx`, `lower_right_yx` and `size_yx`)
refer to. By default, this is `None`, which will use `pyramid_level`.
upper_left_yx (tuple): Tuple of (y, x) coordinates for the upper-left (lower) coordinates
defining the region of interest.
lower_right_yx (tuple): Tuple of (y, x) coordinates for the lower-right (higher) coordinates defining the region of interest.
Expand Down Expand Up @@ -628,6 +649,18 @@ def get_label_ROI(self,
elif not lower_right_yx:
lower_right_yx = tuple(upper_left_yx[i] + size_yx[i] for i in range(2))
assert all([upper_left_yx[i] < lower_right_yx[i] for i in range(len(upper_left_yx))]), 'upper_left_yx needs to be less than lower_right_yx'
# convert coordinates if needed
if pyramid_level_coord != None and pyramid_level != pyramid_level_coord:
upper_left_yx = self.convert_pixel_to_pixel(zyx=((0,) + upper_left_yx),
pyramid_level_from=pyramid_level_coord,
pyramid_level_to=pyramid_level,
pyramid_ref_from=('image', image_name),
pyramid_ref_to=('image', image_name))[1:]
lower_right_yx = self.convert_pixel_to_pixel(zyx=((0,) + lower_right_yx),
pyramid_level_from=pyramid_level_coord,
pyramid_level_to=pyramid_level,
pyramid_ref_from=('image', image_name),
pyramid_ref_to=('image', image_name))[1:]
msk = msk[:,
slice(upper_left_yx[0], lower_right_yx[0] + 1),
slice(upper_left_yx[1], lower_right_yx[1] + 1)]
Expand Down
10 changes: 6 additions & 4 deletions tests/test_hcs_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,9 @@ def test_get_image_ROI_3d(plate_3d: hcs_wrappers.FractalZarr):
lower_right_yx=None, size_yx=None)

img1a = plate_3d.get_image_ROI(well='B03', pyramid_level=1,
upper_left_yx=(11, 10),
lower_right_yx=(22, 20),
pyramid_level_coord=0,
upper_left_yx=(22, 20),
lower_right_yx=(44, 40),
size_yx=None,
as_NumPy=True)
img1b = plate_3d.get_image_ROI(well='B03', pyramid_level=1,
Expand Down Expand Up @@ -380,8 +381,9 @@ def test_get_label_ROI_2d(plate_2d: hcs_wrappers.FractalZarr):

msk1a = plate_2d.get_label_ROI(label_name='organoids',
well='B03', pyramid_level=1,
upper_left_yx=(11, 10),
lower_right_yx=(22, 20),
pyramid_level_coord=0,
upper_left_yx=(22, 20),
lower_right_yx=(44, 40),
size_yx=None,
as_NumPy=True)
msk1b = plate_2d.get_label_ROI(label_name='organoids',
Expand Down

0 comments on commit 37171e9

Please sign in to comment.