Switch to unified view

a b/docs-source/source/cellseg.rst
1
.. currentmodule:: slideflow.cellseg
2
3
.. _cellseg:
4
5
Cell Segmentation
6
=================
7
8
Many tasks in digital pathology rely on analysis of cellular features, as opposed to higher-level architectural features. Slideflow supports whole-slide analysis of cellular features with a cell detection and segmentation pipeline based on `Cellpose <https://www.nature.com/articles/s41592-020-01018-x>`_. To start, ensure ``cellpose`` has been installed via pip:
9
10
.. code-block:: bash
11
12
    pip install cellpose
13
14
Approach
15
********
16
17
.. figure:: cell_segmentation.png
18
19
The general approach for cell detection and segmentation in Slideflow is illustrated above, and will be discussed in the following sections. In short, the general approach is to tune the cell segmentation parameters on a single slide, use these parameters to detect cells in all of your slides, then extract cell images at these locations.
20
21
Slideflow Studio
22
*****************
23
24
Cellpose models have several configurable parameters which will affect the quality of your segmentation masks, namely the **pretrained model** and **cell diameter**. The best way to determine the optimal parameters to use for your dataset is through interactive visualization using :ref:`Slideflow Studio <studio>`.
25
26
Use Cellpose-based cell segmentation in Slideflow Studio by :ref:`enabling the extension <extensions>`, or start Studio with the ``--cellpose`` flag:
27
28
.. code-block:: bash
29
30
    python -m slideflow.studio --cellpose
31
32
Control panel
33
-------------
34
35
Open the Cell Segmentation section in the control panel to access the segmentation controls.
36
37
.. figure:: cellseg_workbench_panel.png
38
39
The **Model & Cell Diameter** subsection is used to customize the segmentation model (defaults to
40
'cyto2') and cell diameter (defaults to 10 microns). Selecting "Auto-detect diameter" then
41
clicking "Preview" will perform cell segmentation on the portion of the slide currently in view. Once complete, the diameter text box will be updated with the detected cell diameter. Any `user-trained models <https://cellpose.readthedocs.io/en/latest/gui.html#training-your-own-cellpose-model>`_ will be listed in the model dropdown selection.
42
43
Viewing cell segmentations
44
--------------------------
45
46
.. figure:: cellseg_workbench_masks.png
47
48
The **View Controls** subsection provides options for customizing how cell segmentations are displayed. By default, cell segmentation masks are shown in cyan on a black background. The black
49
background can be removed by unchecking "Black BG". You can add a green dot at each cell's detected centroid by selecting the "Centroid option." The "Alpha" slider controls transparency for the mask overlay.
50
51
You can also choose to view the segmentation masks as outlines. The "Outline" button will
52
convert any masks currently in view to outlines, allowing you to more easily see how the
53
masks match cells visible on the slide.
54
55
.. figure:: cellseg_workbench_outlines.png
56
57
Finally, the "gradXY" option will show the flow gradients calculated during cell segmentation.
58
59
.. figure:: cellseg_workbench_flows.png
60
61
Preparing WSI segmentation
62
--------------------------
63
64
Once you are satisifed with a chosen model and cell diameter, set the cell diameter to a
65
manual value in microns. Once the cell diameter has been set, the middle control panel will
66
activate, allowing you to perform whole-slide segmentation.
67
68
The **Otsu threshold** option will perform strict Otsu's thresholding on the whole slide image,
69
only performing cell segmentation in non-background areas (reducing computational time).
70
You can preview the Otsu's thresholding algorithm in the :ref:`Slide section <studio_wsi>`. This option is disabled by default, as Otsu's thresholding does not
71
work well for all slides (particularly cytology slides).
72
73
The **Save flows** option saves gradients during cell segmentation, allowing you to generate
74
visualizations as shown with the **gradXY** option above. This is disabled by default, as
75
calculation requires high RAM usage and may not be practical on all systems.
76
77
.. list-table::
78
    :widths: 60 40
79
80
    * - The **Advanced** subsection provides additional options for controlling the cell segmentation process.
81
82
        **Window** controls the window size during cell segmentation; cell segmentation is performed
83
        on images of this pixel size and then stitched together. The **Tile** option permits further sub-
84
        tiling of each window, reducing GPU and CPU memory utilization.
85
86
        **Downscale** will scale down the final generated cell segmentation mask, reducing memory
87
        utilization (both RAM and disk). **Enable spawn workers** enables a multiprocessing technique that improves cell segmentation speed at the cost of higher RAM usage.
88
89
      - .. image:: cellseg_workbench_advanced.png
90
            :width: 245
91
            :align: right
92
93
Running WSI segmentation
94
------------------------
95
96
Once you are satisifed with the settings, whole-slide cell segmentation can be initialized by
97
clicking **Segment**. You will see a notification in the bottom-right corner of the screen when
98
segmentation is complete. In the meantime, a progress bar will be shown in the terminal
99
along with ETA.
100
101
Exporting results
102
-----------------
103
104
Once segmentation is complete, masks can be saved to disk for later use with **Export**.
105
Masks are saved in \*.zip format, and can be loaded in Studio with drag-and-drop.
106
107
Segmenting cells
108
****************
109
110
Single slide segmentation
111
-------------------------
112
113
Once the segmentation parameters have been determined, you can run segmentation for a single slide using :func:`slideflow.cellseg.segment_slide`.
114
115
.. code-block::
116
117
    import slideflow as sf
118
    from slideflow.cellseg import segment_slide
119
120
    segmentation = segment_slide(
121
        '.../slide.svs',
122
        model='cyto2',
123
        diam_um=10,
124
        ...
125
    )
126
    segmentation.save('...masks.zip')
127
128
Project-wide segmentation
129
-------------------------
130
131
Cell segmentation can also be performed automatically for all slides in a Slideflow project.
132
Cell segmentation masks (and associated cell centroids) are calculated for all slides in the project using :meth:`slideflow.Project.cell_segmentation`.
133
134
.. code-block::
135
136
    import slideflow as sf
137
138
    # Load a slideflow project
139
    P = sf.Project(...)
140
141
    # Perform cell segmentation
142
    P.cell_segmentation(
143
        model='cyto2',
144
        diam_um=10
145
    )
146
147
Relevant arguments for this function include:
148
149
- ``model`` : Cell segmentation model. All cellpose models are supported, including 'cyto',
150
  'cyto2', 'nuclei', and more.
151
- ``diam_um`` : Cell diameter, in microns.
152
- ``buffer`` : Path to a buffer, significantly speeds up segmentation if running from a HDD
153
  (same as P.extract_tiles())
154
- ``window_size`` : Integer. Defaults to 256. Increasing this to 512 will make things slightly
155
  faster, but will use a bit more GPU memory.
156
- ``downscale`` : Factor by which to downscale the masks, to save memory. Defaults to 1
157
  (no downscaling, full resolution). Downscale of 2 is a nice balance between memory
158
  size and fidelity.
159
160
Depending on the size of the slide, this may take between 5-25 minutes per slide.
161
162
Masks will be saved in the project subfolder ``masks/`` . As described above,
163
these masks can be loaded in Studio for interactive visualization via drag-and-drop.
164
They can also be used for downstream analysis and cell extraction, as described in the next
165
section.
166
167
Accessing segmentation masks
168
----------------------------
169
170
Saved cell segmentation masks (in \*.zip format) can be loaded with :class:`slideflow.cellseg.Segmentation`.
171
172
.. code-block:: python
173
174
    from slideflow.cellseg import Segmentation
175
    seg = Segmentation.load('.../slide-masks.zip')
176
177
The mask array, ``Segmentation.masks`` , is a ``np.ndarray`` with dtype of np.uint32. Zero values are background, and masks for each cell are represented by a unique integer. Flows/gradients,
178
if calculated, will be available in ``Segmentation.flows``.
179
180
Centroids for detected cells can be calculated with Segmentation.centroids(), returning an array of centroid locations. By default, coordinates are returned in mask dimension space. With the argument ``wsi_dim=True``, centroid coordinates will be in the slide dimension space.
181
182
Caveats
183
-------
184
185
There are some caveats to the cell segmentation process, including:
186
187
- **Memory usage**: Cell segmentation requires at minimum 32 GB of RAM. Larger slides (particularly cytology) may require up to 64 GB of RAM.
188
- **Stitching artifacts**: At present, due to the algorithm by which whole-slide cell segmentations are stitched together, you may see some cells that are not detected, missing in a grid-like pattern. Work is ongoing to reduce these stitching artifacts.
189
- **Cell diameter**: The quality of cell segmentation results is highly dependent on an appropriately chosen cell diameter. Use Slideflow Studio to find the best cell diameter for your application.
190
191
Extracting cells from slides
192
****************************
193
194
Once segmentation masks have been calculated, images of individual cells can be extracted from a whole-slide image. This can be performed for either a single slide, or all slides in a project.
195
196
From a single slide
197
-------------------
198
199
Start by loading the saved segmentation, as described above. Then, use :meth:`slideflow.WSI.apply_segmentation`, followed by :meth:`slideflow.WSI.extract_cells`.
200
201
.. code-block:: python
202
203
    import slideflow as sf
204
    from slideflow.cellseg import Segmentation
205
206
    # Load WSI.
207
    wsi = sf.WSI('../slide.svs', tile_px=96, tile_um='40x')
208
209
    # Load cell segmentations.
210
    seg = Segmentation.load('.../slide-masks.zip')
211
212
    # Apply segmentations to the slide.
213
    wsi.apply_segmentation(seg)
214
215
    # Extract images of cells.
216
    wsi.extract_cells(tiles_dir=...)
217
218
219
.. list-table::
220
    :widths: 80 20
221
222
    * - By default, segmentation masks will be applied to the extracted cell images:
223
224
      - .. image:: cell_masked.png
225
226
    * - However, you can choose not to apply masks by using the argument ``apply_masks=False``.
227
228
229
      - .. image:: cell_unmasked.png
230
231
Tile extraction is then performed as usual. Cell images (tiles) can either be saved as loose images or in TFRecord format. See :meth:`slideflow.WSI.extract_cells` for more information.
232
233
From all slides
234
---------------
235
236
Additionally, cell images can be extracted from all slides in a project. This should only be
237
done after :meth:`slideflow.Project.cell_segmentation`.
238
239
.. code-block:: python
240
241
    P.extract_cells(
242
        tile_px=96,
243
        tile_um='40x',
244
        apply_masks=True
245
    )
246
247
Extracted cell images are saved by default in TFRecord format, and are otherwise handled
248
identically to tile images generated through :meth:`slideflow.Project.extract_tiles`.
249
250
Complete example
251
****************
252
253
An example of a complete cell segmentation pipeline is shown below, from parameter tuning
254
to final tile extraction from detected cells.
255
256
1. Slideflow Studio
257
-------------------
258
259
Determine optimal cell segmenation parameters using Studio, as described above:
260
261
.. code-block:: bash
262
263
    python -m slideflow.studio --cellpose
264
265
2. Cell segmentation
266
--------------------
267
268
Segment cells for all slides in a Slideflow project.
269
270
.. code-block:: python
271
272
    P = sf.Project(...)
273
    P.cell_segmentation(
274
        model='cyto2',
275
        diam_um=10,
276
        window_size=512,
277
        downscale=2
278
    )
279
280
3. Cell image extraction
281
------------------------
282
283
Extract image tiles of segmented cells, in this case using segmentation masks.
284
285
.. code-block:: python
286
287
    P.extract_cells(
288
        tile_px=96,
289
        tile_um='40x',
290
        apply_masks=True,
291
        grayspace_fraction=1
292
    )