From 5508de14f822d3c3dc0be8076fa6d603f20ecad7 Mon Sep 17 00:00:00 2001 From: Wim Pomp Date: Mon, 4 Dec 2023 17:14:12 +0100 Subject: [PATCH] - Transforms cast_image fix. - Prevent invalid value in log in is_noise. - Fix bug when no bead_files found. - Deal with some more czi files. --- ndbioimage/__init__.py | 11 ++++++---- ndbioimage/readers/cziread.py | 41 +++++++++++++++++++++++------------ ndbioimage/transforms.py | 4 ++-- pyproject.toml | 2 +- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/ndbioimage/__init__.py b/ndbioimage/__init__.py index 24bac10..f80ef42 100755 --- a/ndbioimage/__init__.py +++ b/ndbioimage/__init__.py @@ -828,7 +828,7 @@ class Imread(np.lib.mixins.NDArrayOperatorsMixin, ABC): volume = self fft = np.fft.fftn(volume) corr = np.fft.fftshift(np.fft.ifftn(fft * fft.conj()).real / np.sum(volume ** 2)) - return -np.log(1 - corr[tuple([0] * corr.ndim)]) > 5 + return 1 - corr[tuple([0] * corr.ndim)] < 0.0067 @staticmethod def kill_vm(): @@ -882,13 +882,16 @@ class Imread(np.lib.mixins.NDArrayOperatorsMixin, ABC): if file is None: file = Path(view.path.parent) / 'transform.yml' if not bead_files: - bead_files = Transforms.get_bead_files(view.path.parent) + try: + bead_files = Transforms.get_bead_files(view.path.parent) + except Exception: + if not file.exists(): + raise Exception('No transform file and no bead file found.') + bead_files = () if channels: try: view.transform = Transforms.from_file(file, T=drift) - # for key in view.channel_names: - # if except Exception: # noqa view.transform = Transforms().with_beads(view.cyllens, bead_files) if drift: diff --git a/ndbioimage/readers/cziread.py b/ndbioimage/readers/cziread.py index a2eb426..013a414 100644 --- a/ndbioimage/readers/cziread.py +++ b/ndbioimage/readers/cziread.py @@ -98,11 +98,17 @@ class Reader(AbstractReader, ABC): nominal_magnification=float(text(objective.find("NominalMagnification"))))) for tubelens in instrument.find("TubeLenses"): + try: + nominal_magnification = float(re.findall(r'\d+(?:[,.]\d*)?', + tubelens.attrib["Name"])[0].replace(',', '.')) + except Exception: + nominal_magnification = 1.0 + ome.instruments[0].objectives.append( model.Objective( id=f'Objective:{tubelens.attrib["Id"]}', model=tubelens.attrib["Name"], - nominal_magnification=1.0)) # TODO: nominal_magnification + nominal_magnification=nominal_magnification)) for light_source in def_list(instrument.find("LightSources")): if light_source.find("LightSourceType").find("Laser") is not None: @@ -126,8 +132,11 @@ class Reader(AbstractReader, ABC): if pixel_type.startswith("Gray"): pixel_type = "uint" + pixel_type[4:] objective_settings = image.find("ObjectiveSettings") - scenes = image.find("Dimensions").find("S").find("Scenes") - center_position = [float(pos) for pos in text(scenes[0].find("CenterPosition")).split(',')] + try: # TODO + scenes = image.find("Dimensions").find("S").find("Scenes") + center_position = [float(pos) for pos in text(scenes[0].find("CenterPosition")).split(',')] + except AttributeError: + center_position = [0, 0] um = model.UnitsLength.MICROMETER nm = model.UnitsLength.NANOMETER @@ -174,31 +183,35 @@ class Reader(AbstractReader, ABC): light_sources_settings = channel.find("LightSourcesSettings") # no space in ome for multiple lightsources simultaneously - light_source_settings = light_sources_settings[0] - light_source_settings = model.LightSourceSettings( - id="LightSource:" + "_".join([light_source_settings.find("LightSource").attrib["Id"] - for light_source_settings in light_sources_settings]), - attenuation=float(text(light_source_settings.find("Attenuation"))), - wavelength=float(text(light_source_settings.find("Wavelength"))), - wavelength_unit=nm) + if light_sources_settings is not None: + light_source_settings = light_sources_settings[0] + light_source_settings = model.LightSourceSettings( + id="LightSource:" + "_".join([light_source_settings.find("LightSource").attrib["Id"] + for light_source_settings in light_sources_settings]), + attenuation=float(text(light_source_settings.find("Attenuation"))), + wavelength=float(text(light_source_settings.find("Wavelength"))), + wavelength_unit=nm) + else: + light_source_settings = None ome.images[0].pixels.channels.append( model.Channel( id=f"Channel:{idx}", name=channel.attrib["Name"], - acquisition_mode=text(channel.find("AcquisitionMode")), + acquisition_mode=text(channel.find("AcquisitionMode")).replace('SingleMoleculeLocalisation', + 'SingleMoleculeImaging'), color=model.Color(text(channels_ds[channel.attrib["Id"]].find("Color"), 'white')), detector_settings=model.DetectorSettings( id=detector.attrib["Id"].replace(" ", ""), binning=binning), - emission_wavelength=text(channel.find("EmissionWavelength")), + emission_wavelength=i if (i := text(channel.find("EmissionWavelength"))) != '0' else '100', excitation_wavelength=text(channel.find("ExcitationWavelength")), # filter_set_ref=model.FilterSetRef(id=ome.instruments[0].filter_sets[filterset_idx].id), illumination_type=text(channel.find("IlluminationType")), light_source_settings=light_source_settings, - samples_per_pixel=int(text(laser_scan_info.find("Averaging"))))) + samples_per_pixel=int(text(laser_scan_info.find("Averaging"), "1")))) - exposure_times = [float(text(channel.find("LaserScanInfo").find("FrameTime"))) for channel in + exposure_times = [float(text(channel.find("LaserScanInfo").find("FrameTime"), "100")) for channel in channels_im.values()] delta_ts = attachments['TimeStamps'].data() for t, z, c in product(range(size_t), range(size_z), range(size_c)): diff --git a/ndbioimage/transforms.py b/ndbioimage/transforms.py index 3a3d603..6d21971 100644 --- a/ndbioimage/transforms.py +++ b/ndbioimage/transforms.py @@ -345,8 +345,8 @@ class Transform: @staticmethod def cast_image(im): - if isinstance(im, sitk.Image): - im = sitk.GetImageFromArray(im) + if not isinstance(im, sitk.Image): + im = sitk.GetImageFromArray(np.asarray(im)) return im @staticmethod diff --git a/pyproject.toml b/pyproject.toml index 2b0da3f..8b879cf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "ndbioimage" -version = "2023.11.1" +version = "2023.12.0" description = "Bio image reading, metadata and some affine registration." authors = ["W. Pomp "] license = "GPLv3"