PointClusterContext
cellseg_gsontools.spatial_context.PointClusterContext
¶
Bases: WithinContext
Handle & extract dense point clusters from cell_gdf
.
Point-clusters are dense regions of points. This context is useful when you
want to extract dense regions of points of type label
from cell_gdf
such as
immune-cell clusters. The clusters are extracted using one of the clustering
methods: "dbscan", "adbscan", "optics" after which the clusters are converted
to polygons/areas using alpha-shapes.
Note
This class inherits from WithinContext
and thus has all the methods
and attributes of that class.
Note
cell_gdf
has to contain a column named 'class_name'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cell_gdf |
GeoDataFrame
|
A geo dataframe that contains small cellular objects that are
enclosed by larger tissue areas in |
required |
labels |
Union[Tuple[str, ...], str]
|
The class name(s) of the areas of interest. The objects within these areas are extracted. E.g. "cancer" or "stroma". |
required |
cluster_method |
str
|
The clustering method used to extract the point-clusters. One of: "dbscan", "adbscan", "optics" |
'dbscan'
|
min_area_size |
float or str
|
The minimum area of the objects that are kept. All the objects in
the |
None
|
graph_type |
str
|
The type of the graph to be fitted to the cells inside interfaces. One of: "delaunay", "distband", "relative_nhood", "knn". |
'distband'
|
dist_thresh |
float
|
Distance threshold for the length of the network links. |
100.0
|
grid_type |
str
|
The type of the grid to be fitted on the roi areas. One of: "square", "hex". |
'square'
|
patch_size |
Tuple[int, int]
|
The size of the grid patches to be fitted on the context. This is
used when |
(256, 256)
|
stride |
Tuple[int, int]
|
The stride of the sliding window for grid patching. This is used
when |
(256, 256)
|
pad |
int
|
The padding to add to the bounding box on the grid. This is used
when |
None
|
resolution |
int
|
The resolution of the h3 hex grid. This is used when
|
9
|
predicate |
str
|
The predicate to use for the spatial join when extracting the ROI
cells. See |
'intersects'
|
silence_warnings |
bool
|
Flag, whether to silence all the warnings. |
True
|
parallel |
bool
|
Flag, whether to parallelize the context fitting. If
|
False
|
num_processes |
int
|
The number of processes to use when parallel=True. If -1, this will use all the available cores. |
-1
|
backend |
str
|
The backend to use for the spatial context. One of "geopandas", "spatialpandas" "dask-geopandas". "spatialpandas" or "dask-geopandas" is recommended for gdfs that may contain huge polygons. |
'geopandas'
|
**kwargs |
Dict[str, Any]
|
Arbitrary key-word arguments passed to the clustering methods. |
{}
|
Attributes:
Name | Type | Description |
---|---|---|
context |
Dict[int, Dict[str, Union[GeoDataFrame, W]]]
|
A nested dict that contains dicts for each of the distinct clusters. The keys of the outer dict are the indices of these areas. The inner dicts contain the keys:
|
Raises:
Type | Description |
---|---|
ValueError
|
if |
Examples:
Create a point cluster context and plot the cells inside one cluster area.
>>> from cellseg_gsontools.spatial_context import ClusterContext
>>> cell_gdf = pre_proc_gdf(read_gdf("cells.json"))
>>> cluster_context = PointClusterContext(
... cell_gdf=cell_gdf,
... labels=["inflammatory"],
... cluster_method="adbscan",
... silence_warnings=True,
... )
>>> cluster_context.fit(parallel=False, fit_graph=False)
>>> cluster_context.plot("roi_area", show_legends=True)
<AxesSubplot: >
Source code in cellseg_gsontools/spatial_context/point_cluster.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
|
run_clustering(cell_gdf, labels, cluster_method, n_jobs, **kwargs)
¶
Run clustering on the cells and convert the clusters to areas.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cell_gdf |
GeoDataFrame
|
A geo dataframe that contains the cell/nuclei objects. |
required |
labels |
Union[Tuple[str, ...], str]
|
The class name(s of the objects of interest. E.g. "cancer", "immune". |
required |
cluster_method |
str
|
The clustering method used to extract the point-clusters. One of: "dbscan", "adbscan", "optics" |
required |
n_jobs |
int
|
The number of processes to use in clustering. |
required |
**kwargs |
Dict[str, Any]
|
Arbitrary key-word arguments passed to the clustering methods. |
{}
|
Returns:
Type | Description |
---|---|
GeoDataFrame
|
gpd.GeoDataFrame: A gdf containing the areas of the clusters. |