diff --git a/ndbioimage/__init__.py b/ndbioimage/__init__.py index b7c3f32..272268c 100755 --- a/ndbioimage/__init__.py +++ b/ndbioimage/__init__.py @@ -1200,6 +1200,7 @@ class AbstractReader(Imread, metaclass=ABCMeta): image = self.ome.images[self.series if len(self.ome.images) > 1 else 0] pixels = image.pixels self.shape = pixels.size_y, pixels.size_x, pixels.size_c, pixels.size_z, pixels.size_t + self.base_shape = Shape((pixels.size_y, pixels.size_x, pixels.size_c, pixels.size_z, pixels.size_t), 'yxczt') self.dtype = pixels.type.value if dtype is None else dtype self.pxsize = pixels.physical_size_x_quantity try: diff --git a/ndbioimage/readers/cziread.py b/ndbioimage/readers/cziread.py index f16f2e6..c97eef2 100644 --- a/ndbioimage/readers/cziread.py +++ b/ndbioimage/readers/cziread.py @@ -184,7 +184,7 @@ class Reader(AbstractReader, ABC): return OmeParse.get_ome(self.reader, self.filedict) def __frame__(self, c: int = 0, z: int = 0, t: int = 0) -> np.ndarray: - f = np.zeros(self.base.shape['yx'], self.dtype) + f = np.zeros(self.base_shape['yx'], self.dtype) if (c, z, t) in self.filedict: directory_entries = self.filedict[c, z, t] x_min = min([f.start[f.axes.index('X')] for f in directory_entries]) @@ -340,11 +340,14 @@ class OmeParse: if self.version == '1.0': for idx, tube_lens in enumerate({self.text(track_setup.find('TubeLensPosition')) for track_setup in self.multi_track_setup}): + try: + nominal_magnification = float(re.findall(r'\d+[,.]\d*', tube_lens)[0].replace(',', '.')) + except Exception: # noqa + nominal_magnification = 1.0 + self.ome.instruments[0].objectives.append( model.Objective(id=f'Objective:Tubelens:{idx}', model=tube_lens, - nominal_magnification=float( - re.findall(r'\d+[,.]\d*', tube_lens)[0].replace(',', '.')) - )) + nominal_magnification=nominal_magnification)) elif self.version in ('1.1', '1.2'): for tubelens in self.instrument.find('TubeLenses'): try: @@ -362,22 +365,28 @@ class OmeParse: def get_light_sources(self) -> None: if self.version == '1.0': for light_source in self.def_list(self.instrument.find('LightSources')): - if light_source.find('LightSourceType').find('Laser') is not None: - self.ome.instruments[0].lasers.append( - model.Laser( - id=light_source.attrib['Id'], - model=self.text(light_source.find('Manufacturer').find('Model')), - power=float(self.text(light_source.find('Power'))), - wavelength=float( - self.text(light_source.find('LightSourceType').find('Laser').find('Wavelength'))))) + try: + if light_source.find('LightSourceType').find('Laser') is not None: + self.ome.instruments[0].lasers.append( + model.Laser( + id=light_source.attrib['Id'], + model=self.text(light_source.find('Manufacturer').find('Model')), + power=float(self.text(light_source.find('Power'))), + wavelength=float( + self.text(light_source.find('LightSourceType').find('Laser').find('Wavelength'))))) + except AttributeError: + pass elif self.version in ('1.1', '1.2'): for light_source in self.def_list(self.instrument.find('LightSources')): - if light_source.find('LightSourceType').find('Laser') is not None: - self.ome.instruments[0].lasers.append( - model.Laser( - id=f"LightSource:{light_source.attrib['Id']}", - power=float(self.text(light_source.find('Power'))), - wavelength=float(light_source.attrib['Id'][-3:]))) + try: + if light_source.find('LightSourceType').find('Laser') is not None: + self.ome.instruments[0].lasers.append( + model.Laser( + id=f"LightSource:{light_source.attrib['Id']}", + power=float(self.text(light_source.find('Power'))), + wavelength=float(light_source.attrib['Id'][-3:]))) + except AttributeError: + pass def get_filters(self) -> None: if self.version == '1.0': diff --git a/ndbioimage/readers/fijiread.py b/ndbioimage/readers/fijiread.py index ac25671..f11c0e7 100644 --- a/ndbioimage/readers/fijiread.py +++ b/ndbioimage/readers/fijiread.py @@ -26,7 +26,7 @@ class Reader(AbstractReader, ABC): def __frame__(self, c, z, t): # Override this, return the frame at c, z, t self.reader.filehandle.seek(self.offset + t * self.count) - return np.reshape(unpack(self.fmt, self.reader.filehandle.read(self.count)), self.shape['yx']) + return np.reshape(unpack(self.fmt, self.reader.filehandle.read(self.count)), self.base_shape['yx']) def open(self): warn(f'File {self.path.name} is probably damaged, opening with fijiread.') diff --git a/ndbioimage/readers/seqread.py b/ndbioimage/readers/seqread.py index edabc17..4d74683 100644 --- a/ndbioimage/readers/seqread.py +++ b/ndbioimage/readers/seqread.py @@ -114,9 +114,15 @@ class Reader(AbstractReader, ABC): return ome def open(self): - pat = re.compile(r'(?:\d+-)?Pos.*', re.IGNORECASE) - if pat.match(self.path.name) is None: - path = sorted(file for file in self.path.iterdir() if pat.match(file.name))[self.series] + # /some_path/Pos4: path = /some_path, series = 4 + # /some_path/5-Pos_001_005: path = /some_path/5-Pos_001_005, series = 0 + if re.match(r'(?:\d+-)?Pos.*', self.path.name, re.IGNORECASE) is None: + pat = re.compile(rf'^(?:\d+-)?Pos{self.series}$', re.IGNORECASE) + files = sorted(file for file in self.path.iterdir() if pat.match(file.name)) + if len(files): + path = files[0] + else: + raise FileNotFoundError(self.path / pat.pattern) else: path = self.path diff --git a/ndbioimage/readers/tifread.py b/ndbioimage/readers/tifread.py index 04e3f9a..5d3c033 100644 --- a/ndbioimage/readers/tifread.py +++ b/ndbioimage/readers/tifread.py @@ -80,6 +80,6 @@ class Reader(AbstractReader, ABC): def __frame__(self, c, z, t): if self.p_ndim == 3: - return np.transpose(self.reader.asarray(z + t * self.base.shape['z']), self.p_transpose)[c] + return np.transpose(self.reader.asarray(z + t * self.base_shape['z']), self.p_transpose)[c] else: - return self.reader.asarray(c + z * self.base.shape['c'] + t * self.base.shape['c'] * self.base.shape['z']) + return self.reader.asarray(c + z * self.base_shape['c'] + t * self.base_shape['c'] * self.base_shape['z']) diff --git a/pyproject.toml b/pyproject.toml index a1f43b1..64ddff5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "ndbioimage" -version = "2025.1.0" +version = "2025.1.1" description = "Bio image reading, metadata and some affine registration." authors = ["W. Pomp "] license = "GPLv3"