segwrap/utils_cellpose.py
def cellpose_predict(data, config, path_save, callback_log=None):
    """ Perform prediction with CellPose. 

    Parameters
    ----------
    data : dict
        Contains data on which prediction should be performed. 
    config : dict
        Configuration of CellPose prediction. 
    path_save : pathline Path object
        Path where results will be saved. 
    """

    # Get data
    imgs = data['imgs']
    file_names = data['file_names']
    channels = data['channels']
    obj_name = data['obj_name']
    sizes_orginal = data['sizes_orginal']
    new_size = data['new_size']
    
    # Get config
    model_type = config['model_type']
    diameter = config['diameter']
    net_avg = config['net_avg']
    resample = config['resample']   

    log_message(f'\nPerforming segmentation of {obj_name}\n', callback_fun=callback_log)

    start_time = time.time()

    if not path_save.is_dir():
        path_save.mkdir()

    # Perform segmentation with CellPose
    model = models.Cellpose(gpu=False, model_type=model_type)  # model_type can be 'cyto' or 'nuclei'
    masks, flows, styles, diams = model.eval(imgs, diameter=diameter, channels=channels, net_avg=net_avg, resample=resample)

    # Display and save results
    log_message(f'\n Creating outputs ...\n', callback_fun=callback_log)
    n_img = len(imgs)

    for idx in tqdm(range(n_img)):
        
        # Get images and file-name
        file_name = file_names[idx]
        maski = masks[idx]
        flowi = flows[idx][0]
        imgi = imgs[idx]
        
        # Rescale each channel separately
        imgi_norm = imgi.copy()
        for idim in range(3):
            imgdum = imgi[:, :, idim]
            
            # Renormalize to 8bit between 1 and 99 percentile
            pa = np.percentile(imgdum, 0.5)
            pb = np.percentile(imgdum, 99.5)
            
            if pb > pa:
                imgi_norm[:, :, idim] = 255 * (imgdum - pa) / (pb - pa)

        # Save flow
        io.imsave(str(path_save / f'{file_name.stem}__flow__{obj_name}.png'), flowi)

        # Resize masks if necessary 
        if new_size:
            mask_full = resize_mask(maski, sizes_orginal[idx])

            io.imsave(str(path_save / f'{file_name.stem}__mask__{obj_name}.png'), mask_full)
            io.imsave(str(path_save / f'{file_name.stem}__mask_resize__{obj_name}.png'), maski)

        else:
            io.imsave(str(path_save / f'{file_name.stem}__mask__{obj_name}.png'), maski)

        # Save mask and flow images
        #f_mask = str(path_save / f'{file_name.stem}__mask__{obj_name}.png')
        #log_message(f'\nMask saved to file: {f_mask}\n', callback_fun=callback_log)

        #io.imsave(str(path_save / f'{file_name.stem}__mask__{obj_name}.png'), maski)


        # Save overview image
        fig = plt.figure(figsize=(12,3))
        plot.show_segmentation(fig, imgi_norm.astype('uint8'), maski, flowi)
        plt.tight_layout()
        fig.savefig(str(path_save / f'{file_name.stem}__seg__{obj_name}.png'), dpi=300)
        plt.close(fig)

    log_message(f"\nSegmentation of provided images finished ({(time.time() - start_time)}s)", callback_fun=callback_log)
 Perform prediction with CellPose. 

    Parameters
    ----------
    data : dict
        Contains data on which prediction should be performed. 
    config : dict
        Configuration of CellPose prediction. 
    path_save : pathline Path object
        Path where results will be saved. 
    
Perform prediction with CellPose.
Parameters

data : dict
Contains data on which prediction should be performed.
config : dict
Configuration of CellPose prediction.
path_save : pathline Path object
Path where results will be saved.
Perform prediction with CellPose . Parameters data : dict Contains data on which prediction should be performed . config : dict Configuration of CellPose prediction . path_save : pathline Path object Path where results will be saved .
def cellpose_predict(data, config, path_save, callback_log=None):
    imgs = data['imgs']
    file_names = data['file_names']
    channels = data['channels']
    obj_name = data['obj_name']
    sizes_orginal = data['sizes_orginal']
    new_size = data['new_size']
    model_type = config['model_type']
    diameter = config['diameter']
    net_avg = config['net_avg']
    resample = config['resample']   
    log_message(f'\nPerforming segmentation of {obj_name}\n', callback_fun=callback_log)
    start_time = time.time()
    if not path_save.is_dir():
        path_save.mkdir()
    model = models.Cellpose(gpu=False, model_type=model_type)  
    masks, flows, styles, diams = model.eval(imgs, diameter=diameter, channels=channels, net_avg=net_avg, resample=resample)
    log_message(f'\n Creating outputs ...\n', callback_fun=callback_log)
    n_img = len(imgs)
    for idx in tqdm(range(n_img)):
        file_name = file_names[idx]
        maski = masks[idx]
        flowi = flows[idx][0]
        imgi = imgs[idx]
        imgi_norm = imgi.copy()
        for idim in range(3):
            imgdum = imgi[:, :, idim]
            pa = np.percentile(imgdum, 0.5)
            pb = np.percentile(imgdum, 99.5)
            if pb > pa:
                imgi_norm[:, :, idim] = 255 * (imgdum - pa) / (pb - pa)
        io.imsave(str(path_save / f'{file_name.stem}__flow__{obj_name}.png'), flowi)
        if new_size:
            mask_full = resize_mask(maski, sizes_orginal[idx])
            io.imsave(str(path_save / f'{file_name.stem}__mask__{obj_name}.png'), mask_full)
            io.imsave(str(path_save / f'{file_name.stem}__mask_resize__{obj_name}.png'), maski)
        else:
            io.imsave(str(path_save / f'{file_name.stem}__mask__{obj_name}.png'), maski)
        fig = plt.figure(figsize=(12,3))
        plot.show_segmentation(fig, imgi_norm.astype('uint8'), maski, flowi)
        plt.tight_layout()
        fig.savefig(str(path_save / f'{file_name.stem}__seg__{obj_name}.png'), dpi=300)
        plt.close(fig)
    log_message(f"\nSegmentation of provided images finished ({(time.time() - start_time)}s)", callback_fun=callback_log)
def cellpose_predict ( data , config , path_save , callback_log = None ) : imgs = data [ 'imgs' ] file_names = data [ 'file_names' ] channels = data [ 'channels' ] obj_name = data [ 'obj_name' ] sizes_orginal = data [ 'sizes_orginal' ] new_size = data [ 'new_size' ] model_type = config [ 'model_type' ] diameter = config [ 'diameter' ] net_avg = config [ 'net_avg' ] resample = config [ 'resample' ] log_message ( f'\nPerforming segmentation of {obj_name}\n' , callback_fun = callback_log ) start_time = time . time ( ) if not path_save . is_dir ( ) : path_save . mkdir ( ) model = models . Cellpose ( gpu = False , model_type = model_type ) masks , flows , styles , diams = model . eval ( imgs , diameter = diameter , channels = channels , net_avg = net_avg , resample = resample ) log_message ( f'\n Creating outputs ...\n' , callback_fun = callback_log ) n_img = len ( imgs ) for idx in tqdm ( range ( n_img ) ) : file_name = file_names [ idx ] maski = masks [ idx ] flowi = flows [ idx ] [ 0 ] imgi = imgs [ idx ] imgi_norm = imgi . copy ( ) for idim in range ( 3 ) : imgdum = imgi [ : , : , idim ] pa = np . percentile ( imgdum , 0.5 ) pb = np . percentile ( imgdum , 99.5 ) if pb > pa : imgi_norm [ : , : , idim ] = 255 * ( imgdum - pa ) / ( pb - pa ) io . imsave ( str ( path_save / f'{file_name.stem}__flow__{obj_name}.png' ) , flowi ) if new_size : mask_full = resize_mask ( maski , sizes_orginal [ idx ] ) io . imsave ( str ( path_save / f'{file_name.stem}__mask__{obj_name}.png' ) , mask_full ) io . imsave ( str ( path_save / f'{file_name.stem}__mask_resize__{obj_name}.png' ) , maski ) else : io . imsave ( str ( path_save / f'{file_name.stem}__mask__{obj_name}.png' ) , maski ) fig = plt . figure ( figsize = ( 12 , 3 ) ) plot . show_segmentation ( fig , imgi_norm . astype ( 'uint8' ) , maski , flowi ) plt . tight_layout ( ) fig . savefig ( str ( path_save / f'{file_name.stem}__seg__{obj_name}.png' ) , dpi = 300 ) plt . close ( fig ) log_message ( f"\nSegmentation of provided images finished ({(time.time() - start_time)}s)" , callback_fun = callback_log )
Perform prediction with CellPose.
Perform prediction with CellPose .
""" Perform prediction with CellPose. 

    Parameters
    ----------
    data : dict
        Contains data on which prediction should be performed. 
    config : dict
        Configuration of CellPose prediction. 
    path_save : pathline Path object
        Path where results will be saved. 
    """ # Get data # Get config # Perform segmentation with CellPose # model_type can be 'cyto' or 'nuclei' # Display and save results # Get images and file-name # Rescale each channel separately # Renormalize to 8bit between 1 and 99 percentile # Save flow # Resize masks if necessary  # Save mask and flow images #f_mask = str(path_save / f'{file_name.stem}__mask__{obj_name}.png') #log_message(f'\nMask saved to file: {f_mask}\n', callback_fun=callback_log) #io.imsave(str(path_save / f'{file_name.stem}__mask__{obj_name}.png'), maski) # Save overview image
data
config
path_save
callback_log