Skip to content

Commit

Permalink
minor channels updates
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzocerrone committed Nov 12, 2024
1 parent 51dabac commit 5462bdc
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 78 deletions.
12 changes: 10 additions & 2 deletions src/ngio/core/image_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,20 @@ def channel_labels(self) -> list[str]:
"""Return the names of the channels in the image."""
return self.metadata.channel_labels

@property
def num_channels(self) -> int:
"""Return the number of channels in the image."""
return self.dimensions.get("c", 1)

def get_channel_idx(
self,
label: str | None = None,
wavelength_id: str | None = None,
) -> int:
"""Return the index of the channel."""
) -> int | None:
"""Return the index of the channel.
If the channels are not labelled, the index returned is none
"""
return self.metadata.get_channel_idx(label=label, wavelength_id=wavelength_id)

def get_array_from_roi(
Expand Down
99 changes: 90 additions & 9 deletions src/ngio/core/ngff_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ def get_image(
ngio_logger.info(f"- {image.pixel_size}")
return image

def update_omero_window(
self, start_percentile: int = 5, end_percentile: int = 95
) -> None:
"""Update the OMERO window.
def _compute_percentiles(
self, start_percentile: float, end_percentile: float
) -> tuple[list[float], list[float]]:
"""Compute the percentiles for the window.
This will setup percentiles based values for the window of each channel.
Expand All @@ -112,7 +112,6 @@ def update_omero_window(
lowest_res_shape = image.shape
lowest_res_image = image

max_dtype = np.iinfo(image.on_disk_array.dtype).max
num_c = lowest_res_image.dimensions.get("c", 1)

if meta.omero is None:
Expand All @@ -124,17 +123,99 @@ def update_omero_window(
if len(channel_list) != num_c:
raise ValueError("The number of channels does not match the image.")

for c, channel in enumerate(channel_list):
data = image.get_array(c=c, mode="dask").ravel()
starts, ends = [], []
for c in range(num_c):
data = lowest_res_image.get_array(c=c, mode="dask").ravel()
_start_percentile = da.percentile(
data, start_percentile, method="nearest"
).compute()
_end_percentile = da.percentile(
data, end_percentile, method="nearest"
).compute()

channel.channel_visualisation.start = _start_percentile
channel.channel_visualisation.end = _end_percentile
starts.append(_start_percentile)
ends.append(_end_percentile)

return starts, ends

def set_omero(
self,
labels: list[str],
wavelengths: list[str] | None = None,
colors: list[str] | None = None,
adjust_window: bool = True,
start_percentile: int = 5,
end_percentile: int = 95,
active: list[bool] | None = None,
write: bool = True,
) -> None:
"""Set the OMERO metadata for the image.
Args:
labels (list[str]): The labels of the channels.
wavelengths (list[str], optional): The wavelengths of the channels.
colors (list[str], optional): The colors of the channels.
adjust_window (bool, optional): Whether to adjust the window.
start_percentile (int, optional): The start percentile.
end_percentile (int, optional): The end percentile.
active (list[bool], optional): Whether the channel is active.
write (bool, optional): Whether to write the metadata to the image.
"""
image_ref = self.get_image()

if adjust_window:
start, end = self._compute_percentiles(
start_percentile=start_percentile, end_percentile=end_percentile
)

self.image_meta.omero_lazy_init(
labels=labels,
wavelength_ids=wavelengths,
colors=colors,
active=active,
start=start,
end=end,
data_type=image_ref.on_disk_array.dtype,
)

def update_omero_window(
self, start_percentile: int = 5, end_percentile: int = 95
) -> None:
"""Update the OMERO window.
This will setup percentiles based values for the window of each channel.
Args:
start_percentile (int): The start percentile.
end_percentile (int): The end percentile
"""
start, ends = self._compute_percentiles(
start_percentile=start_percentile, end_percentile=end_percentile
)
meta = self.image_meta
ref_image = self.get_image()

max_dtype = np.iinfo(ref_image.on_disk_array.dtype).max
num_c = ref_image.dimensions.get("c", 1)

if meta.omero is None:
raise NotImplementedError(
"OMERO metadata not found. " " Please add OMERO metadata to the image."
)

channel_list = meta.omero.channels
if len(channel_list) != num_c:
raise ValueError("The number of channels does not match the image.")

if len(channel_list) != len(start):
raise ValueError("The number of channels does not match the image.")

for c, (channel, s, e) in enumerate(
zip(channel_list, start, ends, strict=True)
):
channel.channel_visualisation.start = s
channel.channel_visualisation.end = e
channel.channel_visualisation.min = 0
channel.channel_visualisation.max = max_dtype

Expand Down
Loading

0 comments on commit 5462bdc

Please sign in to comment.