def test_mosaic_image_two(data_dir, fname, expects): with open(data_dir / fname, 'rb') as fp: czi = CziFile(czi_filename=fp) sze = czi.read_mosaic_size() rgion = (sze[0], sze[1], int(sze[2] / 2), int(sze[3] / 2)) img = czi.read_mosaic(region=rgion, C=0, M=0) assert img.shape == expects
def test_mosaic_image(data_dir, fname, expects): with open(data_dir / fname, 'rb') as fp: czi = CziFile(czi_filename=fp) sze = czi.read_mosaic_size() assert sze[2] == 1756 assert sze[3] == 624 img = czi.read_mosaic(scale_factor=0.1, C=0) assert img.shape[0] == 1
def __init__(self, slide_path: str): self.slide_path = slide_path reader = CziFile(slide_path) if not reader.is_mosaic(): raise NotImplementedError( 'This class has only been defined for mosaic czi files. ' 'You should be able to simply convert the non-mosaic czi-file ' 'into a format that can be opened with openslide.' ) self.bboxes = np.array(reader.mosaic_scene_bounding_boxes()) self.data_mask = self._get_data_mask(self.bboxes) self.shape = reader.read_mosaic_size() self.dimensions = self.shape[2:] self.region_mask = self._get_region_mask(self.shape)
print('Using AICSImageIO to read the image.') all_scenes_array = img.get_image_dask_data() if not use_aicsimageio and use_pylibczi is True: # read CZI using aicspylibczi czi = CziFile(filename) # for testing # Get the shape of the data print('Dimensions : ', czi.dims) print('Size : ', czi.size) print('Shape : ', czi.dims_shape()) print('IsMoasic : ', czi.is_mosaic()) if czi.is_mosaic(): print('Mosaic Size : ', czi.read_mosaic_size()) # get the required shape for all and single scenes shape_all, shape_single, same_shape = czt.get_shape_allscenes(czi, md) print('Required_Array Shape for all scenes: ', shape_all) for sh in shape_single: print('Required Array Shape for single scenes: ', sh) #array_type = 'dask' array_type = 'zarr' #array_type = 'numpy' if array_type == 'zarr': # define array to store all channels print('Using aicspylibCZI to read the image (ZARR array).')
def test_mosaic_size(data_dir, fname, expected): czi = CziFile(str(data_dir / fname)) ans = czi.read_mosaic_size() assert ans == expected
def open_image_stack(filepath): """ Open a file using AICSImageIO and display it using napari :param path: filepath of the image :type path: str """ if os.path.isfile(filepath): # remove existing layers from napari viewer.layers.select_all() viewer.layers.remove_selected() # get the metadata metadata, add_metadata = czt.get_metadata_czi(filepath) # add the global metadata and adapt the table display mdbrowser.update_metadata(metadata) mdbrowser.update_style() use_aicsimageio = True use_pylibczi = False # decide which tool to use to read the image if metadata['ImageType'] != 'czi': use_aicsimageio = True elif metadata['ImageType'] == 'czi' and metadata['isMosaic'] is False: use_aicsimageio = True elif metadata['ImageType'] == 'czi' and metadata['isMosaic'] is True: use_aicsimageio = False use_pylibczi = True """ # check if CZI has T or Z dimension hasT = False hasZ = False if 'T' in metadata['dims_aicspylibczi']: hasT = True if 'Z' in metadata['dims_aicspylibczi']: hasZ = True """ if use_aicsimageio: # get AICSImageIO object img = AICSImage(filepath) # check if the Dask Delayed Reader should be used if not checkboxes.cbox_dask.isChecked(): print('Using AICSImageIO normal ImageReader.') all_scenes_array = img.get_image_data() if checkboxes.cbox_dask.isChecked(): print('Using AICSImageIO Dask Delayed ImageReader') all_scenes_array = img.get_image_dask_data() if not use_aicsimageio and use_pylibczi is True: # read CZI using aicspylibczi czi = CziFile(filepath) # Get the shape of the data print('Dimensions : ', czi.dims) print('Size : ', czi.size) print('Shape : ', czi.dims_shape()) print('IsMoasic : ', czi.is_mosaic()) if czi.is_mosaic(): print('Mosaic Size : ', czi.read_mosaic_size()) # get the required shape for all and single scenes shape_all, shape_single, same_shape = czt.get_shape_allscenes(czi, metadata) print('Required_Array Shape for all scenes: ', shape_all) for sh in shape_single: print('Required Array Shape for single scenes: ', sh) if not same_shape: print('No all scenes have the same shape. Exiting ...') sys.exit() #array_type = 'dask' array_type = 'zarr' #array_type = 'numpy' if array_type == 'zarr': # define array to store all channels print('Using aicspylibCZI to read the image (ZARR array).') # option 1 all_scenes_array = zarr.create(tuple(shape_all), dtype=metadata['NumPy.dtype'], chunks=True) # option 2 # all_scenes_array = zarr.open(r'c:\Temp\czi_scene_all.zarr', mode='w', # shape=shape_all, # chunks=True, # dtype=md['NumPy.dtype']) if array_type == 'numpy': print('Using aicspylibCZI to read the image (Numpy.Array).') all_scenes_array = np.empty(shape_all, dtype=metadata['NumPy.dtype']) if array_type == 'zarr' or array_type == 'numpy': # loop over all scenes for s in range(metadata['SizeS']): # get the CZIscene for the current scene single_scene = czt.CZIScene(czi, metadata, sceneindex=s) out = czt.read_czi_scene(czi, single_scene, metadata) all_scenes_array[s, :, :, :, :, :] = np.squeeze(out, axis=0) print(all_scenes_array.shape) elif array_type == 'dask': def dask_load_sceneimage(czi, s, md): # get the CZIscene for the current scene single_scene = czt.CZIScene(czi, md, sceneindex=s) out = czt.read_czi_scene(czi, single_scene, md) return out sp = shape_all[1:] # create dask stack of lazy image readers print('Using aicspylibCZI to read the image (Dask.Array) + Delayed Reading.') lazy_process_image = dask.delayed(dask_load_sceneimage) # lazy reader lazy_arrays = [lazy_process_image(czi, s, metadata) for s in range(metadata['SizeS'])] dask_arrays = [ da.from_delayed(lazy_array, shape=sp, dtype=metadata['NumPy.dtype']) for lazy_array in lazy_arrays ] # Stack into one large dask.array all_scenes_array = da.stack(dask_arrays, axis=0) print(all_scenes_array.shape) do_scaling = checkboxes.cbox_autoscale.isChecked() # show the actual image stack nap.show_napari(viewer, all_scenes_array, metadata, blending='additive', adjust_contrast=do_scaling, gamma=0.85, add_mdtable=False, rename_sliders=True)
# filename = r"C:\Users\m1srh\OneDrive - Carl Zeiss AG\Testdata_Zeiss\CZI_Testfiles\Well_B2-4_S=4_T=1_Z=1_C=1.czi" # filename = r"C:\Users\m1srh\OneDrive - Carl Zeiss AG\Testdata_Zeiss\CZI_Testfiles\W96_B2+B4_S=2_T=1=Z=1_C=1_Tile=5x9.czi" # filename = r"C:\Users\m1srh\OneDrive - Carl Zeiss AG\Testdata_Zeiss\CZI_Testfiles\W96_B2+B4_S=2_T=2=Z=4_C=3_Tile=5x9.czi" #filename = r"C:\Users\m1srh\OneDrive - Carl Zeiss AG\Testdata_Zeiss\Castor\Z-Stack_DCV\CellDivision_T=10_Z=15_CH=2_DCV_small.czi" # filename = r"C:\Users\m1srh\OneDrive - Carl Zeiss AG\Testdata_Zeiss\Castor\Mouse Kidney_40x0.95_3CD_JK_comp.czi" # filename = r"C:\Temp\input\DTScan_ID4.czi" # filename = r"C:\Temp\input\OverViewScan_8Brains.czi" #filename = r"C:\Users\m1srh\OneDrive - Carl Zeiss AG\Testdata_Zeiss\Castor\testwell96.czi" filename = r"C:\Users\m1srh\OneDrive - Carl Zeiss AG\Testdata_Zeiss\CZI_Testfiles\Multiscene_CZI_3Scenes.czi" # get the metadata from the czi file md, additional_mdczi = imf.get_metadata(filename) # read CZI using aicslibczi cziobject = CziFile(filename) size = cziobject.read_mosaic_size() dimsizes = imf.getdims_pylibczi(cziobject) print('Mosaic Size: ', size) print('-----------------------------------------------------') # define which scene to read ch = 0 t = 0 z = 0 scalefactor = 1.0 #scenes2plot = 1 scenes2plot = md['SizeS'] """ rs = strategies.RectangularStrategy()
# filename = r"/datadisk1/tuxedo/testpictures/Testdata_Zeiss/CZI_Testfiles/S=2_3x3_T=1_Z=1_CH=2.czi" # filename = r"C:\Users\m1srh\OneDrive - Carl Zeiss AG\Testdata_Zeiss\CZI_Testfiles\S=2_3x3_T=1_Z=4_CH=2.czi" # filename = r"C:\Users\m1srh\OneDrive - Carl Zeiss AG\Testdata_Zeiss\CZI_Testfiles\S=2_3x3_T=3_Z=1_CH=2.czi" # filename = r"C:\Users\m1srh\OneDrive - Carl Zeiss AG\Testdata_Zeiss\CZI_Testfiles\S=1_3x3_T=3_Z=4_CH=2.czi" # filename = r"C:\Users\m1srh\OneDrive - Carl Zeiss AG\Testdata_Zeiss\CZI_Testfiles\S=2_3x3_T=3_Z=4_CH=2.czi" # filename = r"C:\Temp\input\DTScan_ID4_small.czi" # filename = r"C:\Temp\input\OverViewScan_8Brains.czi" # filename = r"C:\Users\m1srh\OneDrive - Carl Zeiss AG\Testdata_Zeiss\Castor\testwell96.czi" # filename = r"C:\Users\m1srh\OneDrive - Carl Zeiss AG\Testdata_Zeiss\CZI_Testfiles\Multiscene_CZI_3Scenes.czi" # get the metadata from the czi file md, additional_mdczi = imf.get_metadata(filename) # read CZI using aicslibczi czi = CziFile(filename) size = czi.read_mosaic_size() # get all bboxes for all scenes from the CZI file all_bboxes = czt.getbboxes_allscenes(czi, numscenes=md['SizeS']) # read data for first scene for s in all_bboxes: scene_array = zarr.create( (1, md['SizeT'], md['SizeZ'], md['SizeC'], s.height, s.width), chunks=True) # read a complete scene for t, z, c in product(range(md['SizeT']), range(md['SizeZ']), range(md['SizeC'])): scene_slice, bbox, md = czt.read_scene_bbox(czi, md,