local_type_counts
Get the neighboring cell/nuclei type counts for every object in a GeoDataFrame.
Note
Neighborhoods are defined by the spatial_weights
object, which can be created
with the fit_graph
function. The function should be applied to the input
GeoDataFrame before using this function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gdf
|
GeoDataFrame
|
The GeoDataFrame containing the spatial data. |
required |
spatial_weights
|
W
|
A libpysal weights object defining the spatial relationships. |
required |
class_name
|
str
|
The name of the class for which to count local types. |
required |
id_col
|
str
|
The unique id column in the gdf. If None, this uses |
None
|
frac
|
bool
|
Whether to return the counts as fractions of the total neighborhood size. Defaults to False. |
False
|
parallel
|
bool
|
Whether to apply the function in parallel. Defaults to False. |
False
|
num_processes
|
int
|
The number of processes to use if |
1
|
create_copy
|
bool
|
Flag whether to create a copy of the input gdf and return that. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
GeoDataFrame
|
gpd.GeoDataFrame: The original GeoDataFrame with an additional column for local type counts. |
Examples:
>>> from histolytics.utils.gdf import set_uid
>>> from histolytics.data import cervix_nuclei
>>> from histolytics.spatial_graph.graph import fit_graph
>>> from histolytics.spatial_geom.shape_metrics import shape_metric
>>> from histolytics.spatial_agg.local_values import local_type_counts
>>>
>>> # input data
>>> nuc = cervix_nuclei()
>>> nuc = set_uid(nuc)
>>>
>>> # Fit delaunay graph
>>> w, _ = fit_graph(nuc, "delaunay", id_col="uid", threshold=100, use_polars=True)
>>> # Get the local counts of inflammatory cells in each neighborhood
>>> nuc = local_type_counts(
... nuc,
... w,
... class_name="inflammatory",
... id_col="uid",
... num_processes=6,
... )
>>> print(nuc.head(3))
geometry class_name uid uid
0 POLYGON ((940.01 5570.02, 939.01 5573, 939 559... connective 0
1 POLYGON ((906.01 5350.02, 906.01 5361, 908.01 ... connective 1
2 POLYGON ((866 5137.02, 862.77 5137.94, 860 513... squamous_epithel 2
nhood_classes inflammatory_cnt
uid
0 [connective, connective, connective, inflammat... 2.0
1 [connective, connective, connective, connectiv... 0.0
2 [squamous_epithel, connective, connective, gla... 0.0
Source code in src/histolytics/spatial_agg/local_values.py
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 |
|