local_diversity
cellseg_gsontools.diversity.local_diversity(gdf, spatial_weights, val_col, id_col=None, metrics=('simpson_index'), scheme='FisherJenks', parallel=True, num_processes=-1, rm_nhood_cols=True, col_prefix=None, create_copy=True)
¶
Compute the local diversity/heterogenity metric for cell neighborhood.
Note
Allowed diversity metrics:
simpson_index
- for both categorical and real valued neighborhoodsshannon_index
- for both categorical and real valued neighborhoodsgini_index
- for only real valued neighborhoodstheil_index
- for only real valued neighborhoods
Note
If val_col
is not categorical, the values are binned using mapclassify
.
The bins are then used to compute the diversity metrics. If val_col
is
categorical, the values are used directly.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gdf |
GeoDataFrame
|
The input GeoDataFrame. |
required |
spatial_weights |
W
|
Libpysal spatial weights object. |
required |
val_col |
Union[str, Tuple[str, ...]]
|
The name of the column in the gdf for which the diversity is computed. You can also pass in a list of columns, in which case the diversity is computed for each column. |
required |
id_col |
str
|
The unique id column in the gdf. If None, this uses |
None
|
metrics |
Tuple[str, ...]
|
A Tuple/List of diversity metrics. Allowed metrics: "shannon_index", "simpson_index", "gini_index", "theil_index". Defaults to None. |
('simpson_index')
|
scheme |
str
|
|
'FisherJenks'
|
parallel |
bool
|
Flag whether to use parallel apply operations when computing the diversities. Defaults to True. |
True
|
num_processes |
int, default=-1
|
The number of processes to use when parallel=True. If -1, this will use all available cores. |
-1
|
rm_nhood_cols |
bool
|
Flag, whether to remove the extra neighborhood columns from the result gdf. Defaults to True. |
True
|
col_prefix |
str
|
Prefix for the new column names. Defaults to None. |
None
|
create_copy |
bool
|
Flag whether to create a copy of the input gdf or not. Defaults to True. |
True
|
Raises:
Type | Description |
---|---|
ValueError
|
If an illegal metric is given. |
Returns:
Type | Description |
---|---|
GeoDataFrame
|
gpd.GeoDataFrame: The input geodataframe with computed diversity metric columns added. |
Examples:
Compute the simpson diversity of eccentricity values for each cell neighborhood
>>> from cellseg_gsontools.diversity import local_diversity
>>> from cellseg_gsontools.graphs import fit_graph
>>> w = fit_graph(gdf, type="distband", thres=75.0)
>>> local_diversity(
... gdf,
... spatial_weights=w_dist,
... val_col="eccentricity",
... metrics=["simpson_index"],
... )
Source code in cellseg_gsontools/diversity.py
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 |
|