def _read_immediate(self) -> da.core.Array: # Init temp czi czi = CziFile(self._file) # Safely construct the numpy array or catch any exception try: # Get image dims indicies image_dim_indices = czi.dims_shape() # Catch inconsistent scene dimension sizes if len(image_dim_indices) > 1: # Choose the provided scene log.info( f"File contains variable dimensions per scene, " f"selected scene: {self.specific_s_index} for data retrieval." ) # Get the specific scene if self.specific_s_index < len(image_dim_indices): data, _ = czi.read_image( **{Dimensions.Scene: self.specific_s_index}) else: raise exceptions.InconsistentShapeError( f"The CZI image provided has variable dimensions per scene. " f"Please provide a valid index to the 'S' parameter to create " f"a dask array for the index provided. " f"Provided scene index: {self.specific_s_index}. " f"Scene index range: 0-{len(image_dim_indices)}.") else: # If the list is length one that means that all the scenes in the image # have the same dimensions # Read all data in the image data, _ = czi.read_image() # A really bad way to close any connection to the CZI object czi._bytes = None czi.reader = None except Exception as e: # A really bad way to close any connection to the CZI object czi._bytes = None czi.reader = None raise e return data
def _daread_safe( img: Union[str, Path], chunk_by_dims: List[str] = [ Dimensions.SpatialZ, Dimensions.SpatialY, Dimensions.SpatialX, ], S: int = 0, ) -> Tuple[da.core.Array, str]: """ Safely read a CZI image file as a delayed dask array where certain dimensions act as the chunk size. Parameters ---------- img: Union[str, Path] The filepath to read. chunk_by_dims: List[str] The dimensions to use as the for mapping the chunks / blocks. Default: [Dimensions.SpatialZ, Dimensions.SpatialY, Dimensions.SpatialX] Note: SpatialY and SpatialX will always be added to the list if not present. S: int If the image has different dimensions on any scene from another, the dask array construction will fail. In that case, use this parameter to specify a specific scene to construct a dask array for. Default: 0 (select the first scene) Returns ------- img: dask.array.core.Array The constructed dask array where certain dimensions are chunked. dims: str The dimension order as a string. """ # Resolve image path img = CziReader._resolve_image_path(img) # Init temp czi czi = CziFile(img) # Safely construct the dask array or catch any exception try: return CziReader._daread(img=img, czi=czi, chunk_by_dims=chunk_by_dims, S=S) except Exception as e: # A really bad way to close any connection to the CZI object czi._bytes = None czi.reader = None raise e