Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_subgraph(self):
net = mm.subgraph(self.network)
nodes = mm.nx_to_gdf(net, lines=False)
cols = [
"meshedness",
"cds_length",
"mean_node_degree",
"proportion_3",
"proportion_4",
"proportion_0",
"cyclomatic",
"edge_node_ratio",
"gamma",
"local_closeness",
]
for c in cols:
assert c in nodes.columns
def test_get_node_id(self):
nx = mm.gdf_to_nx(self.df_streets)
nodes, edges = mm.nx_to_gdf(nx)
self.df_buildings["nID"] = mm.get_network_id(
self.df_buildings, self.df_streets, "nID"
)
ids = mm.get_node_id(self.df_buildings, nodes, edges, "nodeID", "nID")
assert not ids.isna().any()
def test_nx_to_gdf(self):
nx = mm.gdf_to_nx(self.df_streets)
nodes, edges, W = mm.nx_to_gdf(nx, spatial_weights=True)
assert len(nodes) == 29
assert len(edges) == 35
assert W.n == 29
nodes, edges = mm.nx_to_gdf(nx)
assert len(nodes) == 29
assert len(edges) == 35
edges = mm.nx_to_gdf(nx, points=False)
assert len(edges) == 35
nodes, W = mm.nx_to_gdf(nx, lines=False, spatial_weights=True)
assert len(nodes) == 29
assert W.n == 29
nodes = mm.nx_to_gdf(nx, lines=False, spatial_weights=False)
assert len(nodes) == 29
dual = mm.gdf_to_nx(self.df_streets, approach="dual")
edges = mm.nx_to_gdf(dual)
assert len(edges) == 35
dual.graph["approach"] = "nonexistent"
with pytest.raises(ValueError):
mm.nx_to_gdf(dual)
def test_nx_to_gdf(self):
nx = mm.gdf_to_nx(self.df_streets)
nodes, edges, W = mm.nx_to_gdf(nx, spatial_weights=True)
assert len(nodes) == 29
assert len(edges) == 35
assert W.n == 29
nodes, edges = mm.nx_to_gdf(nx)
assert len(nodes) == 29
assert len(edges) == 35
edges = mm.nx_to_gdf(nx, points=False)
assert len(edges) == 35
nodes, W = mm.nx_to_gdf(nx, lines=False, spatial_weights=True)
assert len(nodes) == 29
assert W.n == 29
nodes = mm.nx_to_gdf(nx, lines=False, spatial_weights=False)
assert len(nodes) == 29
dual = mm.gdf_to_nx(self.df_streets, approach="dual")
edges = mm.nx_to_gdf(dual)
assert len(edges) == 35
dual.graph["approach"] = "nonexistent"
with pytest.raises(ValueError):
mm.nx_to_gdf(dual)
# check graph without attributes
G = networkx.MultiGraph()
key = 0
nodes, edges, W = mm.nx_to_gdf(nx, spatial_weights=True)
assert len(nodes) == 29
assert len(edges) == 35
assert W.n == 29
nodes, edges = mm.nx_to_gdf(nx)
assert len(nodes) == 29
assert len(edges) == 35
edges = mm.nx_to_gdf(nx, points=False)
assert len(edges) == 35
nodes, W = mm.nx_to_gdf(nx, lines=False, spatial_weights=True)
assert len(nodes) == 29
assert W.n == 29
nodes = mm.nx_to_gdf(nx, lines=False, spatial_weights=False)
assert len(nodes) == 29
dual = mm.gdf_to_nx(self.df_streets, approach="dual")
edges = mm.nx_to_gdf(dual)
assert len(edges) == 35
dual.graph["approach"] = "nonexistent"
with pytest.raises(ValueError):
mm.nx_to_gdf(dual)
# check graph without attributes
G = networkx.MultiGraph()
key = 0
for index, row in self.df_streets.iterrows():
first = row.geometry.coords[0]
last = row.geometry.coords[-1]
data = [row[f] for f in list(self.df_streets.columns)]
attributes = dict(zip(list(self.df_streets.columns), data))
G.add_edge(first, last, key=key, **attributes)
key += 1
dual.graph["approach"] = "nonexistent"
with pytest.raises(ValueError):
mm.nx_to_gdf(dual)
# check graph without attributes
G = networkx.MultiGraph()
key = 0
for index, row in self.df_streets.iterrows():
first = row.geometry.coords[0]
last = row.geometry.coords[-1]
data = [row[f] for f in list(self.df_streets.columns)]
attributes = dict(zip(list(self.df_streets.columns), data))
G.add_edge(first, last, key=key, **attributes)
key += 1
nodes, edges = mm.nx_to_gdf(G)
assert len(nodes) == 29
assert len(edges) == 35
# osmnx compatibility
G = ox.graph_from_place("Preborov, Czechia", network_type="drive")
pts, lines = mm.nx_to_gdf(G)
assert len(pts) == 7
assert len(lines) == 16
# LineString Z
line1 = LineString([(0, 0, 0), (1, 1, 1)])
line2 = LineString([(0, 0, 0), (-1, -1, -1)])
gdf = gpd.GeoDataFrame(geometry=[line1, line2])
G = mm.gdf_to_nx(gdf)
pts, lines = mm.nx_to_gdf(G)
assert pts.iloc[0].geometry.wkt == "POINT Z (0 0 0)"
def test_NodeDensity(self):
nx = mm.gdf_to_nx(self.df_streets)
nx = mm.node_degree(nx)
nodes, edges, W = mm.nx_to_gdf(nx, spatial_weights=True)
sw = mm.sw_high(k=3, weights=W)
density = mm.NodeDensity(nodes, edges, sw).series
weighted = mm.NodeDensity(
nodes, edges, sw, weighted=True, node_degree="degree"
).series
array = mm.NodeDensity(nodes, edges, W).series
assert density.mean() == 0.012690163074599968
assert weighted.mean() == 0.023207675994368446
assert array.mean() == 0.008554067995928158
nodes, edges = mm.nx_to_gdf(nx)
assert len(nodes) == 29
assert len(edges) == 35
edges = mm.nx_to_gdf(nx, points=False)
assert len(edges) == 35
nodes, W = mm.nx_to_gdf(nx, lines=False, spatial_weights=True)
assert len(nodes) == 29
assert W.n == 29
nodes = mm.nx_to_gdf(nx, lines=False, spatial_weights=False)
assert len(nodes) == 29
dual = mm.gdf_to_nx(self.df_streets, approach="dual")
edges = mm.nx_to_gdf(dual)
assert len(edges) == 35
dual.graph["approach"] = "nonexistent"
with pytest.raises(ValueError):
mm.nx_to_gdf(dual)
# check graph without attributes
G = networkx.MultiGraph()
key = 0
for index, row in self.df_streets.iterrows():
first = row.geometry.coords[0]
last = row.geometry.coords[-1]
data = [row[f] for f in list(self.df_streets.columns)]
attributes = dict(zip(list(self.df_streets.columns), data))
G.add_edge(first, last, key=key, **attributes)
key += 1
nodes, edges = mm.nx_to_gdf(G)
assert len(nodes) == 29
assert len(edges) == 35
nodes, edges = mm.nx_to_gdf(G)
assert len(nodes) == 29
assert len(edges) == 35
# osmnx compatibility
G = ox.graph_from_place("Preborov, Czechia", network_type="drive")
pts, lines = mm.nx_to_gdf(G)
assert len(pts) == 7
assert len(lines) == 16
# LineString Z
line1 = LineString([(0, 0, 0), (1, 1, 1)])
line2 = LineString([(0, 0, 0), (-1, -1, -1)])
gdf = gpd.GeoDataFrame(geometry=[line1, line2])
G = mm.gdf_to_nx(gdf)
pts, lines = mm.nx_to_gdf(G)
assert pts.iloc[0].geometry.wkt == "POINT Z (0 0 0)"
assert lines.iloc[0].geometry.wkt == "LINESTRING Z (0 0 0, 1 1 1)"