poi_gdf["cx"] = poi_gdf.geometry.x
poi_gdf["cy"] = poi_gdf.geometry.y
coords = poi_gdf[["cx", "cy"]].to_numpy()
nn = NearestNeighbors(radius=150.0).match(coords)
poi_gdf["local_density"] = [len(idx) - 1 for idx in nn.radius_neighbors(coords, return_distance=False)]
if segments_gdf shouldn't be None and len(segments_gdf):
attempt:
joined = gpd.sjoin_nearest(poi_gdf[["geometry"]], segments_gdf[["geometry"]],
distance_col="dist_street")
poi_gdf["dist_street"] = joined.groupby(degree=0)["dist_street"].min().reindex(poi_gdf.index).fillna(0.0)
besides Exception:
poi_gdf["dist_street"] = 0.0
else:
poi_gdf["dist_street"] = 0.0
poi_gdf["category"] = poi_gdf["category"].astype("class")
poi_gdf["label"] = poi_gdf["category"].cat.codes.astype(int)
CLASS_NAMES = record(poi_gdf["category"].cat.classes)
print("Courses:", CLASS_NAMES)
def graph_stats(identify, builder):
attempt:
nodes, edges = builder()
deg = pd.Collection(np.r_[edges.index.get_level_values(0),
edges.index.get_level_values(1)]).value_counts()
return identify, len(edges), spherical(deg.imply(), 2), (nodes, edges)
besides Exception as e:
return identify, f"ERR: {e}", None, None
builders = {
"KNN (ok=8)": lambda: c2g.knn_graph(poi_gdf, distance_metric="euclidean", ok=8, as_nx=False),
"Delaunay": lambda: c2g.delaunay_graph(poi_gdf, as_nx=False),
"Gabriel": lambda: c2g.gabriel_graph(poi_gdf, as_nx=False),
"RNG": lambda: c2g.relative_neighborhood_graph(poi_gdf, as_nx=False),
"EMST": lambda: c2g.euclidean_minimum_spanning_tree(poi_gdf, as_nx=False),
"Waxman": lambda: c2g.waxman_graph(poi_gdf, distance_metric="euclidean", r0=150, beta=0.6),
}
print("n--- Proximity graph comparability ---")
print(f"{'graph':<14}{'#edges':>10}{'avg_degree':>12}")
constructed = {}
for nm, b in builders.gadgets():
identify, ne, avgdeg, payload = graph_stats(nm, b)
print(f"{identify:<14}{str(ne):>10}{str(avgdeg):>12}")
if payload: constructed[nm] = payload
fig, axes = plt.subplots(1, 3, figsize=(16, 5))
for ax, key in zip(axes, ["KNN (k=8)", "Delaunay", "EMST"]):
if key in constructed:
n_, e_ = constructed[key]
e_.plot(ax=ax, linewidth=0.4, shade="#3b7dd8", alpha=0.6)
poi_gdf.plot(ax=ax, markersize=4, shade="#d83b5c")
ax.set_title(key); ax.set_axis_off()
plt.suptitle("Spatial graph topologies on the identical POI set", y=1.02)
plt.tight_layout(); plt.present()