-
Notifications
You must be signed in to change notification settings - Fork 4
5.1 Workflows (Custom workflow)
This section follows on from workflows and will demonstrate how you can generate custom workflows and apply it to your deskewed data within napari-lattice!
We will use examples to demonstrate this.
Image analysis workflows are usually bespoke and although we can generate segmented objects using pyclesperanto-assistant, what if we'd like to perform shape measurements as well?
In the previous section, we performed segmentation using:
- gaussian blur
- voronoi-otsu labelling
Now, we need to measure the shape properties of every label object across time. We will use napari-workflow API to design a workflow where we can callscikit image regionprops at the end to perform measurements.
For executable code and more notes, please refer to this notebook.
We will create a workflow to perform the above segmentation:
#import workflow
from napari_workflows import Workflow
#We initialise a workflow
segmentation_workflow = Workflow()
#Set task for gaussian blur first
input_image = "input_img"
#To set a task, we use the set method
segmentation_workflow.set("gaussian",
cle.gaussian_blur,
source = input_image,
sigma_x = 1,
sigma_y= 1,
sigma_z= 0)
#The second task will use input from above. so set the task name 'gaussian' as input
segmentation_workflow.set("voronoi-otsu",
cle.voronoi_otsu_labeling,
source = "gaussian",
spot_sigma = 14,
outline_sigma=0)
This only gives the images. To extract regionprops, we can write a custom function and save it as a .py
file
from skimage.measure import regionprops_table
import numpy as np
def measure_region_properties(label_img):
label_np = np.array(label_img)
measurements = regionprops_table(label_np, properties=( 'area',
'centroid',
'axis_major_length',
'axis_minor_length'))
return measurements,label_np
This is saved as measure_regionprops.py
. Now, we can import it within our workflow and set it as a task, where the output from voronoi-otsu
m which is a label image is the input for regionprops:
segmentation_workflow.set("region_props",
measure_regionprops.measure_region_properties,
"voronoi-otsu"
)
Save this workflow:
from napari_workflows import _io_yaml_v1
_io_yaml_v1.save_workflow("regionprops_workflow.yml", segmentation_workflow)
To use the segmentation workflow with the custom python module regionprops
, we need to ensure both the regionprops_workflow.yml
file and measure_regionprops.py
are in the same folder. This is ready to be used within napari-lattice
.
Open napari-lattice and initialize your plugin. You can try with test dataset from here.
You can use an ROI from the Crop and Deskew
or just test it on the whole image. Import the workflow and then click Apply and Preview Workflow
This will perform the segmentation and save the regionprops table as a csv
file.
Cellpose is a very popular generalist cell segmentation algorithm. It now has a 'human-in-the-loop' training feature making it relatively easier to generate custom models.
Let us use Cellpose for the RBC dataset. Cellpose has a napari plugin, so you can install this via Plugins -> Install/Uninstall Plugins
and searching for cellpose
. The installation may take a while. Once installed, if you'd like to have GPU-accelerated predictions, exit napari and then follow the installation instructions on the cellpose github page.
Once installed, you can create a python file like the following:
### cellpose_1.py
import numpy as np
from cellpose import models
def predict_cellpose(img,model_type:str="cyto"):
model = models.Cellpose(gpu=True, model_type=model_type)
channels = [0,0]
img =np.array(img)
masks, flows, styles, diams = model.eval(img, flow_threshold=None, channels=channels, diameter=25, do_3D=True)
return masks
For details on how to use the cellpose API, refer to their https://cellpose.readthedocs.io/en/latest/index.html.
We define a workflow using the predict_cellpose
from cellpose_1.py
.
from napari_workflows import Workflow
from cellpose_1 import predict_cellpose
#We initialise a workflow
cellpose_workflow = Workflow()
#define cellpose prediction
input_arg = "input"
task_name = "cellpose"
cellpose_workflow.set(task_name,predict_cellpose,input_arg,model_type="cyto")
We use the cyto
model here, but we can pass any model we like. Save the workflow:
from napari_workflows import _io_yaml_v1
_io_yaml_v1.save_workflow("cellpose_1_workflow.yml",cellpose_workflow)
Initialise napari-lattice and your LLS data. Import this workflow into the Workflow
tab to Preview
and apply cellpose to your datasets. Instructions on using workflows in napari-lattice can be found here.