diff --git a/LinkBikeNet_MVP.ipynb b/LinkBikeNet_MVP.ipynb index 6395046..e344ada 100644 --- a/LinkBikeNet_MVP.ipynb +++ b/LinkBikeNet_MVP.ipynb @@ -141,7 +141,7 @@ "closest_pairs = []\n", "for i in range(to_iterate):\n", " wcc = [H.subgraph(c).copy() for c in sorted(nx.connected_components(H), key=lambda c: sum([l[-1] for l in H.subgraph(c).copy().edges.data('length')]), reverse=True)]\n", - " pair = pair_between_closest_components(wcc)\n", + " pair = pair_between_largest_components(wcc)\n", " closest_pairs.append(pair)\n", " H.add_edge(pair[0], pair[1], length=0)" ], @@ -201,6 +201,48 @@ "outputs": [], "execution_count": null }, + { + "metadata": {}, + "cell_type": "code", + "source": "len(gdf)", + "id": "88be0fda819f9012", + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "code", + "source": [ + "# reset Graph\n", + "H = G.edge_subgraph(edges).copy()\n", + "\n", + "# calculating connectivity metrics\n", + "print(\"Calculating connectivity metrics...\")\n", + "network_lengths = []\n", + "lcc_lengths = []\n", + "edge_lengths = gdf['geometry'].length\n", + "\n", + "for i in range(len(gdf)):\n", + " H.add_edge(closest_pairs[i][0], closest_pairs[i][1], length=edge_lengths[i])\n", + " total, largest = calculate_network_statistics(H)\n", + " network_lengths.append(total)\n", + " lcc_lengths.append(largest)" + ], + "id": "fb40177941e881f0", + "outputs": [], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "code", + "source": [ + "gdf['network_length'] = network_lengths\n", + "gdf['lcc_length'] = lcc_lengths" + ], + "id": "57d9c49a74f60f01", + "outputs": [], + "execution_count": null + }, { "metadata": {}, "cell_type": "code", diff --git a/linkbikenet/functions.py b/linkbikenet/functions.py index 87c4729..b6e4663 100644 --- a/linkbikenet/functions.py +++ b/linkbikenet/functions.py @@ -2,6 +2,7 @@ from . import settings import re import osmnx as ox +import networkx as nx import geopandas as gpd import numpy as np from scipy.spatial import cKDTree @@ -358,4 +359,38 @@ def slugify(s): "aeiouaeiouaeiouaeiouaeiouaeiouaaaccilnnoorssssuyyzz" ) s = s.translate(tab) - return s \ No newline at end of file + return s + +def calculate_network_statistics(H): + """Return total network length and largest component length. + + Parameters + ---------- + H: networkx.Graph + undirected simple graph representing the street network with weighted edges + + Returns + ------- + total_length: float + total length of the network + largest_length: float + length of the largest connected component + """ + + total_length = sum( + data["length"] + for _, _, data in H.edges(data=True) + ) + components = nx.connected_components(H) + + largest_component = max(components, key=lambda c: sum( + H[u][v]["length"] + for u, v in H.subgraph(c).edges() + )) + + largest_length = sum( + data["length"] + for _, _, data in H.subgraph(largest_component).edges(data=True) + ) + + return total_length, largest_length \ No newline at end of file diff --git a/linkbikenet/linkbikenet.py b/linkbikenet/linkbikenet.py index 95acb66..eb11f5e 100644 --- a/linkbikenet/linkbikenet.py +++ b/linkbikenet/linkbikenet.py @@ -157,6 +157,24 @@ def linkbikenet( gdf['ordering'] = gdf.index + # reset Graph + H = G.edge_subgraph(edges).copy() + + # calculating connectivity metrics + print("Calculating connectivity metrics...") + network_lengths = [] + lcc_lengths = [] + edge_lengths = gdf['geometry'].length + + for i in range(len(gdf)): + H.add_edge(closest_pairs[i][0], closest_pairs[i][1], length=edge_lengths[i]) + total, largest = calculate_network_statistics(H) + network_lengths.append(total) + lcc_lengths.append(largest) + + gdf['network_length'] = network_lengths + gdf['lcc_length'] = lcc_lengths + edges_pbi_gdf = edges_gdf[edges_gdf["pbi"] == 1] # Back to unprojected (potentially). No more calculations after here.