Skip to content

Commit

Permalink
Added Dask converter for ome.tiff reader, other small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
folterj committed Jan 26, 2024
1 parent ce28512 commit 9a1229b
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#### Version 0.6.2
- Added Dask converter for ome.tiff reader

#### Version 0.6.1
- Rewritten code base supporting full multi-dimensional images
- Improved (ome) zarr support
Expand Down
14 changes: 9 additions & 5 deletions OmeSliCC/OmeSource.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ def _init_sizes(self):

self.output_dimension_order = 'tczyx'

def as_dask(self):
raise NotImplementedError('Implement method in subclass')

def get_mag(self) -> float:
mag = self.source_mag
# get effective mag at target pixel size
Expand Down Expand Up @@ -214,7 +217,7 @@ def get_thumbnail(self, target_size: tuple, precise: bool = False) -> np.ndarray
thumbnail = self.render(image_resize(image, target_size))
return thumbnail

def get_window_min_max(self, channeli):
def get_channel_window(self, channeli):
min_quantile = 0.001
max_quantile = 0.999

Expand Down Expand Up @@ -266,12 +269,13 @@ def render(self, image: np.ndarray, t: int = 0, z: int = 0, channels: list = [])
else:
channel_values = image[..., channeli]
if do_normalisation:
window = self.get_window_min_max(channeli)
window = self.get_channel_window(channeli)
channel_values = normalise_values(channel_values, window['min'], window['max'])
else:
channel_values = int2float_image(channel_values)
if 'color' in channel and channel['color'] != '':
rgba = channel['color']
color = channel.get('color')
if color:
rgba = color
else:
rgba = [1, 1, 1, 1]
color = rgba[:3]
Expand All @@ -283,7 +287,7 @@ def render(self, image: np.ndarray, t: int = 0, z: int = 0, channels: list = [])
tot_alpha += alpha
new_image = float2int_image(new_image / tot_alpha)
elif do_normalisation:
window = self.get_window_min_max(0)
window = self.get_channel_window(0)
new_image = float2int_image(normalise_values(image, window['min'], window['max']))
else:
new_image = image
Expand Down
3 changes: 3 additions & 0 deletions OmeSliCC/OmeZarrSource.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ def _find_metadata(self):
self.channels = channels
self.source_mag = 0

def as_dask(self):
return self.levels

def _asarray_level(self, level: int, x0: float = 0, y0: float = 0, x1: float = -1, y1: float = -1,
c: int = None, z: int = None, t: int = None) -> np.ndarray:
if x1 < 0 or y1 < 0:
Expand Down
9 changes: 7 additions & 2 deletions OmeSliCC/TiffSource.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# https://pypi.org/project/tifffile/


import os
from concurrent.futures import ThreadPoolExecutor
import dask.array as da
from enum import Enum
import numpy as np
import os
from tifffile import TiffFile, TiffPage
from concurrent.futures import ThreadPoolExecutor

from OmeSliCC import XmlDict
from OmeSliCC.OmeSource import OmeSource
Expand Down Expand Up @@ -51,6 +52,7 @@ def __init__(self,
self.executor = ThreadPoolExecutor(max_workers)

tiff = TiffFile(filename)
self.tiff = tiff
self.first_page = tiff.pages.first
if tiff.is_ome and tiff.ome_metadata is not None:
xml_metadata = tiff.ome_metadata
Expand Down Expand Up @@ -176,6 +178,9 @@ def _find_metadata(self):
self.source_mag = mag
self.channels = channels

def as_dask(self):
return [da.from_zarr(self.tiff.aszarr(level=level)) for level in range(len(self.sizes))]

def load(self, decompress: bool = False):
self.fh.seek(0)
self.data = self.fh.read()
Expand Down
2 changes: 1 addition & 1 deletion OmeSliCC/ome_zarr_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def create_channel_metadata(source):
if 'color' in channel:
channel['color'] = rgba_to_hexrgb(channel['color'])
if 'window' not in channel:
channel['window'] = source.get_window_min_max(channeli)
channel['window'] = source.get_channel_window(channeli)
channels.append(channel)

metadata = {
Expand Down
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies:
- scikit-learn
- imagecodecs
- numcodecs
- dask
- tifffile
- zarr
- ome-zarr
Expand Down
1 change: 1 addition & 0 deletions environment_no_bioformats.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies:
- scikit-learn
- imagecodecs
- numcodecs
- dask
- tifffile
- zarr
- ome-zarr
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "OmeSliCC"
version = "0.6.1"
version = "0.6.2"
description = "Ome(ro) Slide Image Conversion and Compression pipeline"
readme = "README.md"
license = {file = "LICENSE.md"}
Expand Down

0 comments on commit 9a1229b

Please sign in to comment.