fit_graph
Fit a spatial graph to a GeoDataFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gdf
|
GeoDataFrame
|
The input GeoDataFrame with spatial data. |
required |
method
|
str
|
Type of spatial graph to fit. Options are: "delaunay", "knn", "rel_nhood", "distband", "gabriel", "voronoi". |
required |
id_col
|
str
|
Column name for unique identifiers in the GeoDataFrame. |
'uid'
|
threshold
|
int
|
Distance threshold (in pixels) for distance-based graphs. |
100
|
use_polars
|
bool
|
If True, use Polars for computations during gdf conversion. This can speed
up the process for large datasets. Requires |
False
|
use_parallel
|
bool
|
If True, use parallel processing for computations during gdf conversion. If
|
False
|
num_processes
|
int
|
Number of processes to use for parallel processing. If -1, uses all
available cores. Ignored if |
1
|
**kwargs
|
Any
|
Additional keyword arguments for specific graph fitting functions.
For example, |
{}
|
Returns:
Type | Description |
---|---|
W | GeoDataFrame
|
W and gpd.GeoDataFrame: returns a libpysal weights object and a GeoDataFrame containing the spatial graph edges. |
Examples:
>>> from histolytics.data import hgsc_cancer_nuclei
>>> from histolytics.utils.gdf import set_uid
>>> from histolytics.spatial_graph.graph import fit_graph
>>> # load the HGSC cancer nuclei dataset
>>> nuc = hgsc_cancer_nuclei()
>>> # set unique identifiers if not present
>>> nuc = set_uid(nuc, id_col="uid")
>>> # Fit a Delaunay triangulation graph
>>> w, w_gdf = fit_graph(
... nuc, "delaunay", id_col="uid", threshold=100
... )
>>> print(w_gdf.head(3))
index ... geometry
0 0 ... LINESTRING (1400.038 1.692, 1386.459 9.581)
1 1 ... LINESTRING (1400.038 1.692, 1306.06 2.528)
2 6 ... LINESTRING (1386.459 9.581, 1306.06 2.528)
[3 rows x 12 columns]
>>> # Plot the spatial graph
>>> ax = nuc.plot(column="class_name", figsize=(5, 5), aspect=1)
>>> w_gdf.plot(ax=ax, column="class_name", aspect=1, lw=0.5)
>>> ax.set_axis_off()
Source code in src/histolytics/spatial_graph/graph.py
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 |
|