Skip to content

Commit

Permalink
DOC: Update dijkstra index and pointer dtype
Browse files Browse the repository at this point in the history
- Move getting started notebook to it's folder
- Remove ipynb table styling warning
- Refine timing for optimized betweenness centrality benchmark
  • Loading branch information
cbueth committed Apr 18, 2024
1 parent 4cb44a6 commit 6860cac
Show file tree
Hide file tree
Showing 5 changed files with 425 additions and 15,972 deletions.
12 changes: 9 additions & 3 deletions docs/guide/10_dist_calc_restricted.myst
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def colored_predecessors(preds, graph=G, node_order=node_order,
node_order_indices=node_order_indices):
return pd.DataFrame(preds, index=node_order_indices,
columns=node_order_indices).style. \
applymap(lambda x: f"color: {graph.nodes[node_order[x]]['partition']}"
map(lambda x: f"color: {graph.nodes[node_order[x]]['partition']}"
if x != -9999 else "")


Expand Down Expand Up @@ -333,8 +333,11 @@ differences are the following:

```{code-cell} ipython3
:tags: [hide-input]
G_sparse = nx.to_scipy_sparse_array(G, nodelist=list(G.nodes))
G_sparse.indices, G_sparse.indptr = G_sparse.indices.astype(
np.int32), G_sparse.indptr.astype(np.int32)
display(colored_distances(
dist_le - dijkstra(nx.to_scipy_sparse_array(G, nodelist=list(G.nodes)),
dist_le - dijkstra(G_sparse,
directed=True,
return_predecessors=False),
1, 'Reds')
Expand Down Expand Up @@ -490,8 +493,11 @@ To finish, the difference between the unrestricted and restricted graph distance

```{code-cell} ipython3
:tags: [hide-input]
G_2_sparse = nx.to_scipy_sparse_array(G_2, nodelist=node_order_2)
G_2_sparse.indices, G_2_sparse.indptr = G_2_sparse.indices.astype(
np.int32), G_2_sparse.indptr.astype(np.int32)
display(colored_distances(
dist - dijkstra(nx.to_scipy_sparse_array(G_2, nodelist=node_order_2),
dist - dijkstra(G_2_sparse,
directed=True,
return_predecessors=False),
1, 'Reds', node_order=node_order_2). \
Expand Down
29 changes: 15 additions & 14 deletions docs/guide/20_betweenness_centrality.myst
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,11 @@ restricted to the sparsified nodes.

```{code-cell} ipython3
node_order = list(range(len(G.nodes)))
dist, pred = dijkstra(
nx.to_scipy_sparse_array(G, nodelist=node_order, weight="weight"),
directed=True, indices=node_order, return_predecessors=True)
G_sparse = nx.to_scipy_sparse_array(G, nodelist=node_order, weight="weight")
G_sparse.indices, G_sparse.indptr = G_sparse.indices.astype(
np.int32), G_sparse.indptr.astype(np.int32)
dist, pred = dijkstra(G_sparse, directed=True, indices=node_order,
return_predecessors=True)
dist_restr, pred_restr = shortest_paths_restricted(G, partitions, weight="weight",
node_order=node_order)
```
Expand Down Expand Up @@ -351,8 +353,8 @@ display(pd.DataFrame({
).sort_values(by=[("", "C_B")], ascending=False)
.style.background_gradient(cmap="Blues", axis=None)
.format(precision=3)
.applymap_index(lambda x: f"color: {G.nodes[x]['partition']}" if x in G
.nodes else ""). \
.map_index(lambda x: f"color: {G.nodes[x]['partition']}" if x in G
.nodes else ""). \
set_table_attributes('style="font-size: 12px"'),
)
```
Expand All @@ -375,8 +377,8 @@ display(pd.DataFrame({
).sort_values(by=[("Lin scales", "C_B_e_restr")], ascending=False)
.style.background_gradient(cmap="Blues", axis=None)
.format(precision=3)
.applymap_index(lambda x: f"color: {G.nodes[x]['partition']}" if x in G
.nodes else ""). \
.map_index(lambda x: f"color: {G.nodes[x]['partition']}" if x in G
.nodes else ""). \
set_table_attributes('style="font-size: 12px"'),
)
```
Expand Down Expand Up @@ -589,7 +591,6 @@ to see a difference.
```{code-cell} ipython3
:tags: [hide-input, hide-output]
from superblockify import ResidentialPartitioner
from superblockify.metrics.distances import calculate_path_distance_matrix
from superblockify.config import Config

CITY_NAME, SEARCH_STR = Config.PLACES_SMALL[1]
Expand All @@ -601,19 +602,19 @@ part.run(calculate_metrics=False)
# Intended use usually only with `run` method
# or `calculate_metrics_before` partitioning
node_list = part.get_sorted_node_list()
dist, pred = calculate_path_distance_matrix(
part.graph,
weight="travel_time",
node_order=node_list,
)
```

```{code-cell} ipython3
from superblockify.metrics.measures import betweenness_centrality
%timeit betweenness_centrality(part.graph, node_list, dist, pred, weight="travel_time")
from superblockify.metrics.distances import calculate_path_distance_matrix
%timeit betweenness_centrality(part.graph, node_list, *calculate_path_distance_matrix(part.graph, weight = "travel_time", node_order = node_list), weight="travel_time")
%timeit nx.betweenness_centrality(part.graph, weight="travel_time")
```

Now, the speedup is obvious. The simplified version is about 10 times faster than the
original networkx implementation, already for a street graph that can be considered
small.

The code scales with the number of nodes and number of edges. As in real world cities
the edges do not scale with the number of nodes, the runtime is well bearable for
simplified graphs of metropolitan cities.
Expand Down
420 changes: 127 additions & 293 deletions scripts/TestingNotebooks/20230409-dist_calc_restricted_code.ipynb

Large diffs are not rendered by default.

15,936 changes: 274 additions & 15,662 deletions scripts/TestingNotebooks/20230503-betweenness_calc.ipynb

Large diffs are not rendered by default.

File renamed without changes.

0 comments on commit 6860cac

Please sign in to comment.