Skip to content

Commit

Permalink
- Transforms cast_image fix.
Browse files Browse the repository at this point in the history
- Prevent invalid value in log in is_noise.
- Fix bug when no bead_files found.
- Deal with some more czi files.
  • Loading branch information
Wim Pomp committed Dec 4, 2023
1 parent a6457ea commit 5508de1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
11 changes: 7 additions & 4 deletions ndbioimage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ def is_noise(self, volume=None):
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():
Expand Down Expand Up @@ -882,13 +882,16 @@ def with_transform(self, channels=True, drift=False, file=None, bead_files=()):
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:
Expand Down
41 changes: 27 additions & 14 deletions ndbioimage/readers/cziread.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,17 @@ def def_list(item):
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:
Expand All @@ -126,8 +132,11 @@ def def_list(item):
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

Expand Down Expand Up @@ -174,31 +183,35 @@ def def_list(item):

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)):
Expand Down
4 changes: 2 additions & 2 deletions ndbioimage/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ def copy(self):

@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
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 = "2023.11.1"
version = "2023.12.0"
description = "Bio image reading, metadata and some affine registration."
authors = ["W. Pomp <w.pomp@nki.nl>"]
license = "GPLv3"
Expand Down

0 comments on commit 5508de1

Please sign in to comment.