From 9616c2c31c870163a36c333022eb76d65170d275 Mon Sep 17 00:00:00 2001 From: Federico Catalano <44501803+PyMap@users.noreply.github.com> Date: Tue, 25 Jul 2023 18:44:33 -0300 Subject: [PATCH] Informative warning for shortest path unsigned integer (#169) --- pandana/network.py | 13 +++++++++++++ pandana/utils.py | 7 +++++++ 2 files changed, 20 insertions(+) diff --git a/pandana/network.py b/pandana/network.py index 61303ea6..fe3086cb 100644 --- a/pandana/network.py +++ b/pandana/network.py @@ -282,6 +282,12 @@ def shortest_path_length(self, node_a, node_b, imp_name=None): len = self.net.shortest_path_distance(node_a, node_b, imp_num) + if len == 4294967.295: + warnings.warn( + "Unsigned integer: shortest path distance is trying to be calculated between\ + external %s and %s unconntected nodes"%(node_a, node_b) + ) + return len def shortest_path_lengths(self, nodes_a, nodes_b, imp_name=None): @@ -322,6 +328,13 @@ def shortest_path_lengths(self, nodes_a, nodes_b, imp_name=None): lens = self.net.shortest_path_distances(nodes_a_idx, nodes_b_idx, imp_num) + if 4294967.295 in lens: + unconnected_idx = [i for i,v in enumerate(lens) if v == 4294967.295] + unconnected_nodes = [(nodes_a[i],nodes_b[i]) for i in unconnected_idx] + warnings.warn( + "Unsigned integer: shortest path distance is trying to be calculated \ + between the following external unconnected nodes: %s"%(unconnected_nodes)) + return lens def set(self, node_ids, variable=None, name="tmp"): diff --git a/pandana/utils.py b/pandana/utils.py index 10e0885d..b3ef06fc 100644 --- a/pandana/utils.py +++ b/pandana/utils.py @@ -25,3 +25,10 @@ def reindex(series1, series2): right_index=True, how="left") return df.right + +def adjacency_matrix(edges_df, plot_matrix=False): + df = pd.crosstab(edges_df['from'], edges_df['to']) + idx = df.columns.union(df.index) + df = df.reindex(index = idx, columns=idx, fill_value=0) + + return df