WithinContext
cellseg_gsontools.spatial_context.WithinContext
¶
Handle & extract cells from cell_gdf
within the ROIs of area_gdf
.
Note
area_gdf
and cell_gdf
have to contain a column named 'class_name'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
area_gdf |
GeoDataFrame
|
A geo dataframe that contains large tissue area polygons enclosing
the smaller cellular objects in |
required |
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 |
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'
|
Attributes:
Name | Type | Description |
---|---|---|
context |
Dict[int, Dict[str, Union[GeoDataFrame, W]]]
|
A nested dict that contains dicts for each of the distinct ROIs
of type
|
Raises:
Type | Description |
---|---|
ValueError
|
if |
Source code in cellseg_gsontools/spatial_context/within.py
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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 |
|
fit(verbose=True, fit_graph=True, fit_grid=True)
¶
Fit the context.
This sets the self.context
attribute.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
verbose |
bool
|
Flag, whether to use tqdm pbar when creating the interfaces. |
True
|
fit_graph |
bool
|
Flag, whether to fit the spatial weights networks for the context. |
True
|
fit_grid |
bool
|
Flag, whether to fit the a grid on the contextes. |
True
|
Examples:
Define a within context and plot the cells inside a specific ROI.
>>> from cellseg_gsontools.spatial_context import WithinContext
>>> area_gdf = read_gdf("area.json")
>>> cell_gdf = read_gdf("cells.json")
>>> within_context = WithinContext(
... area_gdf=area_gdf,
... cell_gdf=cell_gdf,
... labels=["area_cin"],
... silence_warnings=True,
... min_area_size=100000.0,
... )
>>> within_context.fit()
>>> within_context.plot("roi_area", show_legends=True)
<AxesSubplot: >
Source code in cellseg_gsontools/spatial_context/within.py
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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
|
context2gdf(key)
¶
Merge the GeoDataFrames of type key
into one geodataframe.
Note
Returns None if no data is found.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
The key of the context dictionary that contains the data to be converted to gdf. One of "roi_area", "roi_cells", "roi_grid", |
required |
Returns:
Type | Description |
---|---|
GeoDataFrame
|
gpd.GeoDataFrame: Geo dataframe containing all the objects |
Source code in cellseg_gsontools/spatial_context/within.py
context2weights(key)
¶
Merge the networks of type key
into one spatial weights obj.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
The key of the context dictionary that contains the spatial weights to be merged. One of "roi_network" |
required |
Returns:
Type | Description |
---|---|
W
|
libpysal.weights.W: A spatial weights object containing all the distinct networks in the context. |
Source code in cellseg_gsontools/spatial_context/within.py
plot(key, network_key=None, grid_key=None, show_legends=True, color=None, figsize=(12, 12), edge_kws=None, **kwargs)
¶
Plot the context with areas, cells, and ROIs highlighted.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
The key of the context dictionary that contains the data to be plotted. One of "roi_area", |
required |
network_key |
str
|
The key of the context dictionary that contains the spatial weights to be plotted. One of "roi_network" |
None
|
grid_key |
str
|
The key of the context dictionary that contains the grid to be plotted. One of "roi_grid" |
None
|
show_legends |
bool
|
Flag, whether to include legends for each in the plot. |
True
|
color |
str
|
A color for the interfaces or rois, Ignored if |
None
|
figsize |
Tuple[int, int]
|
Size of the figure. |
(12, 12)
|
**kwargs |
Dict[str, Any])]
|
Extra keyword arguments passed to the |
{}
|
Returns:
Type | Description |
---|---|
Axes
|
AxesSubplot |
Examples:
Plot the context with stromal areas highlighted.
>>> from cellseg_gsontools.spatial_context import WithinContext
>>> cells = read_gdf("cells.feather")
>>> areas = read_gdf("areas.feather")
>>> stroma = WithinContext(
... cell_gdf=cells,
... area_gdf=areas,
... labels="stroma",
... )
>>> stroma.fit(verbose=False)
>>> stroma.plot("roi_area", show_legends=True)
<AxesSubplot: >
Source code in cellseg_gsontools/spatial_context/within.py
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 |
|