Skip to content

Commit

Permalink
- add physical_size_z if known but missing from ome.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wim Pomp committed Mar 19, 2024
1 parent a805613 commit 7d06db4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
37 changes: 25 additions & 12 deletions ndbioimage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import numpy as np
import ome_types
import yaml
from ome_types import model, ureg
from ome_types import model, ureg, OME
from pint import set_application_registry
from tiffwrite import IJTiffFile
from tqdm.auto import tqdm
Expand Down Expand Up @@ -77,8 +77,11 @@ def update(self, *args, **kwargs):

def find(obj, **kwargs):
for item in obj:
if all([getattr(item, key) == value for key, value in kwargs.items()]):
return item
try:
if all([getattr(item, key) == value for key, value in kwargs.items()]):
return item
except AttributeError:
pass


def try_default(fun, default, *args, **kwargs):
Expand Down Expand Up @@ -538,11 +541,11 @@ def summary(self):
if self.laserpowers:
s.append('laser powers: ' + ' | '.join([' & '.join(len(p) * ('{:.3g}',)).format(*[100 * i for i in p])
for p in self.laserpowers]) + ' %')
if self.objective:
if self.objective and self.objective.model:
s.append(f'objective: {self.objective.model}')
if self.magnification:
s.append(f'magnification: {self.magnification}x')
if self.tubelens:
if self.tubelens and self.tubelens.model:
s.append(f'tubelens: {self.tubelens.model}')
if self.filter:
s.append(f'filterset: {self.filter}')
Expand Down Expand Up @@ -815,14 +818,28 @@ def get_czt(self, c, z, t):
return [self.get_channel(c) for c in czt[0]], *czt[1:]

@staticmethod
def get_ome(path):
def get_ome(path: [str, Path]) -> OME:
""" Use java BioFormats to make an ome metadata structure. """
with multiprocessing.get_context('spawn').Pool(1) as pool:
ome = pool.map(get_ome, (path,))[0]
return ome

# fix ome if necessary
for image in ome.images:
try:
if image.pixels.physical_size_z is None and len(set([plane.the_z
for plane in image.pixels.planes])) > 1:
z = np.array([(plane.position_z * ureg.Quantity(plane.position_z_unit.value).to(ureg.m).magnitude,
plane.the_z)
for plane in image.pixels.planes if plane.the_c == 0 and plane.the_t == 0])
i = np.argsort(z[:, 1])
image.pixels.physical_size_z = np.nanmean(np.true_divide(*np.diff(z[i], axis=0).T)) * 1e6
image.pixels.physical_size_z_unit = 'µm'
except Exception:
pass
return ome

@cached_property
def ome(self):
def ome(self) -> OME:
return self.get_ome(self.path)

def is_noise(self, volume=None):
Expand Down Expand Up @@ -946,10 +963,6 @@ def _can_open(path): # Override this method, and return true when the subclass
def __frame__(self, c, z, t): # Override this, return the frame at c, z, t
return np.random.randint(0, 255, self.shape['yx'])

@cached_property
def ome(self):
return self.get_ome(self.path)

def open(self): # Optionally override this, open file handles etc.
""" filehandles cannot be pickled and should be marked such by setting do_not_pickle = 'file_handle_name' """
return
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "ndbioimage"
version = "2024.3.5"
version = "2024.3.6"
description = "Bio image reading, metadata and some affine registration."
authors = ["W. Pomp <w.pomp@nki.nl>"]
license = "GPLv3"
Expand Down

0 comments on commit 7d06db4

Please sign in to comment.