def stitch_and_load_column(self): """ Stitches the current columns and loads them for molecule detection. Returns ------- Stitched nick and backbone columns for the current column id. """ if not self.saphyr: backbone_label_column, nick_label_column = return_column( self.frames, self.current_column_id, self.chip_dimension) backbone_label_column, [nick_label_column ] = stitch_column(backbone_label_column, [nick_label_column]) self.current_lab_column = ndimage.zoom(nick_label_column, 0.5) self.current_mol_column = ndimage.zoom(backbone_label_column, 0.5) else: print(self.frames["CH1"][self.current_column_id]) concat_mol_column = imageio.imread( self.frames["CH1"][self.current_column_id]).astype(float) concat_lab_column = imageio.imread( self.frames["CH2"][self.current_column_id]).astype(float) if self.channel_num == 3: concat_lab_column_2nd_channel = imageio.imread( self.frames["CH3"][self.current_column_id]).astype(float) backbone_frames = [ ndimage.white_tophat(concat_mol_column[i:i + 2048], structure=np.ones((1, 3))) for i in range(0, concat_mol_column.shape[0], 2048) ] if self.channel_num == 3: lab_frames = [ [ ndimage.white_tophat(concat_lab_column[i:i + 2048], structure=disk(6)) for i in range(0, concat_lab_column.shape[0], 2048) ], [ ndimage.white_tophat( concat_lab_column_2nd_channel[i:i + 2048], structure=disk(6)) for i in range(0, concat_lab_column_2nd_channel.shape[0], 2048) ] ] else: lab_frames = [[ ndimage.white_tophat(concat_lab_column[i:i + 2048], structure=disk(6)) for i in range(0, concat_lab_column.shape[0], 2048) ]] # print([f.shape for f in backbone_frames],[ff.shape for ff in lab_frames]) if self.channel_num == 3: self.current_mol_column, ( self.current_lab_column, self.current_lab_column2) = stitch_column( backbone_frames, lab_frames, saphyr=self.saphyr) else: self.current_mol_column, [self.current_lab_column ] = stitch_column(backbone_frames, lab_frames, saphyr=self.saphyr)
def get_tile(self, uuid, x, y, z, t, c, level, format="tiff"): file_ext = ".tif" if format == "tiff" else f".{format}" filename = f"C{c}-T{t}-Z{z}-L{level}-Y{y}-X{x}{file_ext}" path = os.path.join(self.base_dir, uuid, filename) logger.debug("Opening path: %s", path) with open(path, mode="rb") as file: data = file.read() stream = BytesIO(data) if format == "tiff": return tifffile.imread(stream) else: return imagecodecs.imread(stream)
def imread(file_name, image_type='file-extension-default'): ''' Read image :param file_name: File name and path in the disk that function loads from. :param image_type: The type of image as: 'kitti-disparity', 'optical-flow', 'disparity', 'file-extension-default' :return: image array ''' ext = os.path.splitext(file_name)[1].lower()[1:] image_type = image_type.lower() if not image_type in IMAGE_TYPES: print(IMAGE_TYPES_ERR_MSG) return if image_type == 'file-extension-default': if ext in NORMAL_IMAGE_FORMATS: return flowlib.read_image(file_name) elif ext in ['flo']: return read_flo(file_name) elif ext in ['dpt']: return read_dpt(file_name) elif ext in ['pfm']: return read_pfm(file_name) elif ext in ['jxr']: return imagecodecs.imread(file_name, codec='jpegxr').astype( np.float32) / (2**16 - 1) elif ext in ['exr']: img = cv2.imread(file_name, cv2.IMREAD_ANYCOLOR | cv2.IMREAD_ANYDEPTH) return cv2.cvtColor(img, cv2.COLOR_BGR2RGB) else: print("ERROR: The image type is unknown.") elif image_type == 'optical-flow': return read_flo(file_name) elif image_type == 'disparity': return read_pfm(file_name) elif image_type == 'depth': return read_dpt(file_name) elif image_type == 'kitti-disparity': return flowlib.read_image(file_name).astype(np.float32) / 255. else: print("This image type is not implemented.")
def get_tile(self, uuid, x, y, z, t, c, level, format="tiff"): """Fetch a specific tile from S3 and decode""" start = time.time() # Use the indices to build the key file_ext = ".tif" if format == "tiff" else f".{format}" key = f"{uuid}/C{c}-T{t}-Z{z}-L{level}-Y{y}-X{x}{file_ext}" try: image = self._get_cached_object(key) if image is None: if format == "zarr": # ZARR image = self._zarr_get(uuid, x, y, z, t, c, level) else: data = self._s3_get(key) stream = BytesIO(data) if format == "tiff": # Use tifffile to open TIFF formats image = tifffile.imread(stream) else: # Use imagecodecs to open other formats, such as PNG image = imagecodecs.imread(stream) self._put_cached_object(key, image) t = round((time.time() - start) * 1000) return image except ClientError as e: logger.error(e) logger.info("%s - Fetch COMPLETE %s ms", key, str(t)) sys.stdout.flush() if (e.response["Error"]["Code"] == "NoSuchKey" and self.missing_tile_callback is not None): self.missing_tile_callback(uuid, x, y, z, t, c, level) else: raise e except Exception as e: logger.error(e) raise e
def __init__( self, arg: Any, mode: Literal["rb"] | None = None, name: str | None = None, *_args: Any, **_kwargs: Any, ) -> None: if mode is not None and mode != "rb": raise ValueError("mode must be 'rb'") if name is None: if isinstance(arg, str): name = os.path.basename(arg) elif isinstance(arg, PurePath): name = arg.name elif hasattr(arg, "fullname"): name = arg.fullname self.filename = name array, codec = imread(arg, return_codec=True) self.pages = [NotTiffPage(array, codec=codec.__name__)] self.series = [NotTiffPageSeries(self.pages)]
def imagecodecs_reader(path): """Return napari LayerData from first page in TIFF file.""" from imagecodecs import imread return [(imread(path), {}, 'image')]