gdf2sem
gdf2sem ¶
gdf2sem(
gdf: GeoDataFrame,
xoff: int = 0,
yoff: int = 0,
class_dict: dict[str, int] | None = None,
width: int | None = None,
height: int | None = None,
transform: Affine | None = None,
) -> np.ndarray
Rasterize semantic geometry onto a discrete integer grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gdf
|
GeoDataFrame
|
Geometries with a class_name column, in the output grid's coordinate units. |
required |
xoff
|
int
|
Unit-grid x origin when |
0
|
yoff
|
int
|
Unit-grid y origin when |
0
|
class_dict
|
dict[str, int] | None
|
Class-name to integer mapping. Without one, retain the existing pandas categorical-code mapping plus one. |
None
|
width
|
int | None
|
Output columns. Inferred from geometry bounds only on the unit grid. |
None
|
height
|
int | None
|
Output rows. Inferred from geometry bounds only on the unit grid. |
None
|
transform
|
Affine | None
|
Rasterio affine mapping (column, row) pixel corners to geometry coordinates. Requires explicit width/height and zero xoff/yoff. Geometry and transform must use the same units; no CRS conversion occurs. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Int32 class labels with shape (height, width), background zero, and |
ndarray
|
Rasterio's existing pixel-center/edge burning rules. Classes are not interpolated. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If an explicit grid lacks dimensions or also supplies offsets. |
TypeError
|
If empty geometry has no unit-grid output dimensions. |
Examples:
>>> from histolytics.data import hgsc_cancer_nuclei
>>> from histolytics.utils.raster import gdf2sem
>>> import matplotlib.pyplot as plt
>>> from skimage.measure import label
>>> from skimage.color import label2rgb
>>>
>>> nuc = hgsc_cancer_nuclei()
>>> # Convert the GeoDataFrame to an instance segmentation raster
>>> nuc_raster = gdf2sem(nuc, xoff=0, yoff=0, width=1500, height=1500)
>>> # Visualize the semantic segmentation raster and the GeoDataFrame
>>> fig, ax = plt.subplots(1, 2, figsize=(8, 4))
>>> ax[0].imshow(label2rgb(nuc_raster, bg_label=0))
>>> ax[0].set_axis_off()
>>> nuc.plot(column="class_name", ax=ax[1])
>>> ax[1].set_axis_off()
>>> fig.tight_layout()

Source code in src/histolytics/utils/raster.py
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 | |