def open_image_stack(filepath, use_dask=False): """ Open a file using AICSImageIO and display it using napari :param path: filepath of the image :type path: str :param use_dask: use Dask Delayed reader, defaults to False :type use_dask: bool, optional """ if os.path.isfile(filepath): # remove existing layers from napari viewer.layers.select_all() viewer.layers.remove_selected() # get the metadata metadata, add_metadata = imf.get_metadata(filepath) # add the metadata and adapt the table display mdbrowser.update_metadata(metadata) mdbrowser.update_style() # get AICSImageIO object img = AICSImage(filepath) if not use_dask: stack = img.get_image_data() if use_dask: stack = img.get_image_dask_data() # add the image stack to the napari viewer show_image_napari(stack, metadata, blending='additive', gamma=0.85, rename_sliders=True)
from aicsimageio import AICSImage, imread from skimage import measure, segmentation from skimage.measure import regionprops from skimage.color import label2rgb from skimage.color import rgb2gray import progressbar from IPython.display import display, HTML from MightyMosaic import MightyMosaic # specify the filename of the CZI file #filename = r"C:\Users\m1srh\OneDrive - Carl Zeiss AG\Testdata_Zeiss\Atomic\Nuclei\nuclei_RGB\H&E\Tumor_H&E_small2.czi" filename = r"/datadisk1/tuxedo/testpictures/Testdata_Zeiss/Nuclei/nuclei_RGB/H+E/Tumor_H+E_small2.czi" # get the metadata from the czi file md, addmd = imf.get_metadata(filename) # show some metainformation print('------------------ Show Metainformation ------------------') # shape and dimension entry from CZI file as returned by czifile.py print('Array Shape (czifile) : ', md['Shape']) print('Dimension Entry (czifile) : ', md['Axes']) print('Array Shape (aicsimageio) : ', md['Shape_aics']) print('Dimension Entry (aicsimageio) : ', md['Axes_aics']) print('------------------------------------------------------------') print('SizeS : ', md['SizeS']) print('SizeT : ', md['SizeT']) print('SizeZ : ', md['SizeZ']) print('SizeC : ', md['SizeC']) print('SizeX (czifile) : ', md['SizeX'])
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 = imf.get_metadata(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['czi_isMosaic'] is False: use_aicsimageio = True elif metadata['ImageType'] == 'czi' and metadata['czi_isMosaic'] is True: use_aicsimageio = False """ # 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 """ # read CZI using aicspylibczi czi = CziFile(filepath) # 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() if use_aicsimageio: # get AICSImageIO object img = AICSImage(filepath) try: # check if the Dask Delayed Reader should be used # if there is a checkbox widget 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() except Exception as e: # in case there is no checkbox widget print(e, 'No Checkboxes found. Using AICSImageIO Dask Delayed ImageReader.') all_scenes_array = img.get_image_dask_data() if not use_aicsimageio and use_pylibczi is True: # 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).') all_scenes_array = zarr.create(tuple(shape_all), dtype=metadata['NumPy.dtype'], chunks=True) 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) # show the actual image stack imf.show_napari(viewer, all_scenes_array, metadata, blending='additive', gamma=0.85, add_mdtable=False, rename_sliders=True)
import zarr import numpy as np import imgfile_tools as imf filename = r"C:\Users\m1srh\OneDrive - Carl Zeiss AG\Testdata_Zeiss\CZI_Testfiles\S=2_3x3_T=1_Z=1_CH=2.czi" # get the metadata from the czi file md, additional_mdczi = imf.get_metadata(filename) shape = [2, 3, 5, 2, 10, 20] np_array = np.ones(shape, dtype=np.int16) print("Shape NP Array:", np_array.shape) z_array = zarr.create(shape) print("Shape ZARR Array:", z_array.shape) print('Is ZARR array: ', isinstance(z_array, zarr.Array)) s0 = slice(shape[0]) s1 = slice(shape[1]) s2 = slice(shape[2]) s3 = slice(1) #s4 = slice(shape[4]) s4 = slice(None) #s5 = slice(shape[5]) s5 = slice(None) sliceNd = (s0, s1, s2, s3, s4, s5) z_array_sliced = z_array[sliceNd]
def get_czi_planetable(czifile): # get the czi object using pylibczi czi = aicspylibczi.CziFile(czifile) # get the czi metadata md, add = imf.get_metadata(czifile) # initialize the plane table df_czi = define_czi_planetable() # define subblock counter sbcount = -1 # create progressbar # total = md['SizeS'] * md['SizeM'] * md['SizeT'] * md['SizeZ'] * md['SizeC'] # pbar = tqdm(total=total) # pbar = progressbar.ProgressBar(max_value=total) # in case the CZI has the M-Dimension if md['czi_isMosaic']: for s, m, t, z, c in product(range(md['SizeS']), range(md['SizeM']), range(md['SizeT']), range(md['SizeZ']), range(md['SizeC'])): sbcount += 1 # print(s, m, t, z, c) info = czi.read_subblock_rect(S=s, M=m, T=t, Z=z, C=c) # read information from subblock sb = czi.read_subblock_metadata(unified_xml=True, B=0, S=s, M=m, T=t, Z=z, C=c) try: time = sb.xpath('//AcquisitionTime')[0].text timestamp = dt.parse(time).timestamp() except IndexError as e: timestamp = 0.0 try: xpos = np.double(sb.xpath('//StageXPosition')[0].text) except IndexError as e: xpos = 0.0 try: ypos = np.double(sb.xpath('//StageYPosition')[0].text) except IndexError as e: ypos = 0.0 try: zpos = np.double(sb.xpath('//FocusPosition')[0].text) except IndexError as e: zpos = 0.0 df_czi = df_czi.append( { 'Subblock': sbcount, 'Scene': s, 'Tile': m, 'T': t, 'Z': z, 'C': c, 'X[micron]': xpos, 'Y[micron]': ypos, 'Z[micron]': zpos, 'Time[s]': timestamp, 'xstart': info[0], 'ystart': info[1], 'xwidth': info[2], 'ywidth': info[3] }, ignore_index=True) if not md['czi_isMosaic']: """ for s, t, z, c in it.product(range(md['SizeS']), range(md['SizeT']), range(md['SizeZ']), range(md['SizeC'])): """ for s, t, z, c in product(range(md['SizeS']), range(md['SizeT']), range(md['SizeZ']), range(md['SizeC'])): sbcount += 1 info = czi.read_subblock_rect(S=s, T=t, Z=z, C=c) # read information from subblocks sb = czi.read_subblock_metadata(unified_xml=True, B=0, S=s, T=t, Z=z, C=c) try: time = sb.xpath('//AcquisitionTime')[0].text timestamp = dt.parse(time).timestamp() except IndexError as e: timestamp = 0.0 try: xpos = np.double(sb.xpath('//StageXPosition')[0].text) except IndexError as e: xpos = 0.0 try: ypos = np.double(sb.xpath('//StageYPosition')[0].text) except IndexError as e: ypos = 0.0 try: zpos = np.double(sb.xpath('//FocusPosition')[0].text) except IndexError as e: zpos = 0.0 df_czi = df_czi.append( { 'Subblock': sbcount, 'Scene': s, 'Tile': 0, 'T': t, 'Z': z, 'C': c, 'X[micron]': xpos, 'Y[micron]': ypos, 'Z[micron]': zpos, 'Time[s]': timestamp, 'xstart': info[0], 'ystart': info[1], 'xwidth': info[2], 'ywidth': info[3] }, ignore_index=True) # normalize timestamps df_czi = imf.norm_columns(df_czi, colname='Time[s]', mode='min') # cast data types df_czi = df_czi.astype( { 'Subblock': 'int32', 'Scene': 'int32', 'Tile': 'int32', 'T': 'int32', 'Z': 'int32', 'C': 'int16', 'xstart': 'int32', 'xstart': 'int32', 'ystart': 'int32', 'xwidth': 'int32', 'ywidth': 'int32' }, copy=False, errors='ignore') return df_czi