Skip to content

Commit

Permalink
Fixes for position metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
folterj committed Sep 23, 2024
1 parent a3d6b3a commit d136d84
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 20 deletions.
9 changes: 6 additions & 3 deletions OmeSliCC/OmeSource.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ def _get_ome_metadata(self):

position = []
for plane in ensure_list(pixels.get('Plane', [])):
position = [(plane.get('PositionX'), plane.get('PositionXUnit')),
(plane.get('PositionY'), plane.get('PositionYUnit')),
(plane.get('PositionZ'), plane.get('PositionZUnit'))]
position = [(float(plane.get('PositionX')), plane.get('PositionXUnit')),
(float(plane.get('PositionY')), plane.get('PositionYUnit')),
(float(plane.get('PositionZ')), plane.get('PositionZUnit'))]
c, z, t = plane.get('TheC'), plane.get('TheZ'), plane.get('TheT')

self.position = position
Expand Down Expand Up @@ -211,6 +211,9 @@ def get_pixel_size(self) -> list:
def get_pixel_size_micrometer(self):
return get_value_units_micrometer(self.get_pixel_size())

def get_position_micrometer(self):
return get_value_units_micrometer(self.position)

def get_shape(self, dimension_order: str = None, xyzct: tuple = None) -> tuple:
shape = []
if dimension_order is None:
Expand Down
10 changes: 5 additions & 5 deletions OmeSliCC/OmeZarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ def write(self, sources, tile_size=[], compression=[],
else:
root = zarr_root
if index < len(translations):
translation = translations[index]
translation_um = translations[index]
else:
translation = []
self.write_dataset(root, data, source, npyramid_add, pyramid_downsample, translation)
translation_um = source.get_position_micrometer()
self.write_dataset(root, data, source, npyramid_add, pyramid_downsample, translation_um)
if multiple_images:
meta = root.attrs['multiscales'][0].copy()
for dataset_meta in meta['datasets']:
Expand All @@ -62,7 +62,7 @@ def write(self, sources, tile_size=[], compression=[],
zarr_root.attrs['omero'] = create_channel_metadata(sources[0], ome_version)

def write_dataset(self, zarr_root, data, source,
npyramid_add=0, pyramid_downsample=2, translation=[]):
npyramid_add=0, pyramid_downsample=2, translation_um=[]):

pixel_size_um = source.get_pixel_size_micrometer()
if len(pixel_size_um) == 0:
Expand All @@ -79,7 +79,7 @@ def write_dataset(self, zarr_root, data, source,
pixel_size_scales = []
scale = 1
for i in range(npyramid_add + 1):
pixel_size_scales.append(create_transformation_metadata(dimension_order, pixel_size_um, scale, translation))
pixel_size_scales.append(create_transformation_metadata(dimension_order, pixel_size_um, scale, translation_um))
if pyramid_downsample:
scale /= pyramid_downsample

Expand Down
12 changes: 9 additions & 3 deletions OmeSliCC/OmeZarrSource.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,31 @@ def __init__(self, filename: str,

def _find_metadata(self):
pixel_size = []
position = []
channels = []
metadata = self.metadata
axes = self.dimension_order

units = [axis.get('unit', '') for axis in metadata.get('axes', [])]

scale1 = [1, 1, 1, 1, 1]
scale1 = [1] * len(metadata.get('axes'))
position1 = [0] * len(metadata.get('axes'))
# get pixelsize using largest/first scale
transform = self.metadata.get('coordinateTransformations', [])
if transform:
for transform1 in transform[0]:
if transform1['type'] == 'scale':
scale1 = transform1['scale']
if transform1['type'] == 'translation':
position1 = transform1['translation']
for axis in 'xyz':
if axis in axes:
index = axes.index(axis)
pixel_size.append((scale1[index], units[index]))
position.append((position1[index], units[index]))
else:
pixel_size.append((1, ''))
position.append((0, ''))
nchannels = self.sizes_xyzct[0][3]
# look for channel metadata
for data in self.root_metadata.values():
Expand All @@ -117,10 +123,10 @@ def _find_metadata(self):
self.source_pixel_size = pixel_size
self.channels = channels
self.source_mag = 0
self.position = position

def get_source_dask(self):
return [redimension_data(level, self.dimension_order, self.get_dimension_order())
for level in self.levels]
return self.levels

def _asarray_level(self, level: int, **slicing) -> np.ndarray:
redim = redimension_data(self.levels[level], self.dimension_order, self.get_dimension_order())
Expand Down
3 changes: 2 additions & 1 deletion OmeSliCC/Zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def create(self, source, shapes=[], chunk_shapes=[], level_scales=[],
shape0 = [xyzct['xyzct'.index(dimension)] for dimension in dimension_order]
dtype = source.pixel_types[0]
pixel_size_um = source.get_pixel_size_micrometer()
translation_um = source.get_position_micrometer()
scale = 1
datasets = []
if tile_size and len(tile_size) < 5:
Expand Down Expand Up @@ -80,7 +81,7 @@ def create(self, source, shapes=[], chunk_shapes=[], level_scales=[],
# used for ome metadata:
datasets.append({
'path': str(level),
'coordinateTransformations': create_transformation_metadata(dimension_order, pixel_size_um, scale)
'coordinateTransformations': create_transformation_metadata(dimension_order, pixel_size_um, scale, translation_um)
})
scale /= pyramid_downsample

Expand Down
2 changes: 1 addition & 1 deletion OmeSliCC/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def combine_images(sources: list[OmeSource], params: dict):
nchannels = len(channels)

if image.shape[1] != nchannels:
logging.warning('#Comined image channels does not match #data channels')
logging.warning('#Combined image channels does not match #data channels')

new_source.channels = channels

Expand Down
1 change: 1 addition & 0 deletions OmeSliCC/image_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ def create_compression_codecs(compression: list) -> list:


def get_tiff_pages(tiff: TiffFile) -> list:
# TODO: review so this works for multi-level ome-tiff, tiff-stack, and z pages tiff, then later check for mmstack
pages = []
found = False
if tiff.series and not tiff.is_mmstack:
Expand Down
14 changes: 7 additions & 7 deletions OmeSliCC/ome_zarr_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def create_axes_metadata(dimension_order):
return axes


def create_transformation_metadata(dimension_order, pixel_size_um, scale, translation=[]):
def create_transformation_metadata(dimension_order, pixel_size_um, scale, translation_um=[]):
metadata = []
pixel_size_scale = []
translation_scale = []
Expand All @@ -37,12 +37,12 @@ def create_transformation_metadata(dimension_order, pixel_size_um, scale, transl
pixel_size_scale1 = 1
pixel_size_scale.append(pixel_size_scale1)

if dimension == 'z' and len(translation) > 2:
translation1 = translation[2]
elif dimension == 'y' and len(translation) > 1:
translation1 = translation[1] / scale
elif dimension == 'x' and len(translation) > 0:
translation1 = translation[0] / scale
if dimension == 'z' and len(translation_um) > 2:
translation1 = translation_um[2]
elif dimension == 'y' and len(translation_um) > 1:
translation1 = translation_um[1] * scale
elif dimension == 'x' and len(translation_um) > 0:
translation1 = translation_um[0] * scale
else:
translation1 = 0
translation_scale.append(translation1)
Expand Down

0 comments on commit d136d84

Please sign in to comment.