在 Python 中加速循环
Accelerate a loop in Python
我是 运行 一个循环,它为 GeoDataFrames
(城市)列表的每一行(街区)计算一个 networkx.classes.multidigraph.MultiDiGraph
。然后它为每一行计算一些统计数据并将文件写出到磁盘。问题是循环计算起来非常长,因为图形是为每一行计算的。我正在寻找一种加速循环的方法
我试图计算每个完整 GeoDataFrame
的图形,然后再将其剪裁到邻域边界,但我不知道剪裁 Networkx 图形的方法。
这是我的初始代码,需要 95 秒:
import osmnx as ox
import pandas as pd
import geopandas as gpd
import os
path="C:/folder/"
files=[os.path.join(path, f) for f in os.listdir(path)]
merged=[]
for i in range(0,2):
city=gpd.read_file(files[i])
circ=[]
for j in range(0,2):
graph_for_row=ox.graph_from_polygon(city.geometry[j])
#above is the long command
stat = ox.basic_stats(graph_for_row)
circ.append(stat['circuity_avg'])
circ=pd.Series(circ)
merged.append(pd.concat([city, circ], axis=1))
for k in (range(0,len(merged))):
with open(geofiles[k], 'w') as f:
f.write(merged[k].to_json())
我怎样才能加快我的循环?
答案确实是计算每个城市的图形,然后根据邻域多边形对其进行裁剪。这是 @gboeing 在他对 的回答中提出的。
city=gpd.read_file('C:/folder/file.geojson')
city_graph=ox.graph_from_polygon(city.unary_union, network_type='drive')
circ=[]
for j in (len(city)):
intersecting_nodes = nodes[nodes.intersects(j)].index
neighbourhood_graph = city_graph.subgraph(intersecting_nodes)
stat = ox.basic_stats(neighbourhood_graph)
circ.append(stat['circuity_avg'])
circ=pd.Series(circ)
merged.append(pd.concat([city, circ], axis=1))
我是 运行 一个循环,它为 GeoDataFrames
(城市)列表的每一行(街区)计算一个 networkx.classes.multidigraph.MultiDiGraph
。然后它为每一行计算一些统计数据并将文件写出到磁盘。问题是循环计算起来非常长,因为图形是为每一行计算的。我正在寻找一种加速循环的方法
我试图计算每个完整 GeoDataFrame
的图形,然后再将其剪裁到邻域边界,但我不知道剪裁 Networkx 图形的方法。
这是我的初始代码,需要 95 秒:
import osmnx as ox
import pandas as pd
import geopandas as gpd
import os
path="C:/folder/"
files=[os.path.join(path, f) for f in os.listdir(path)]
merged=[]
for i in range(0,2):
city=gpd.read_file(files[i])
circ=[]
for j in range(0,2):
graph_for_row=ox.graph_from_polygon(city.geometry[j])
#above is the long command
stat = ox.basic_stats(graph_for_row)
circ.append(stat['circuity_avg'])
circ=pd.Series(circ)
merged.append(pd.concat([city, circ], axis=1))
for k in (range(0,len(merged))):
with open(geofiles[k], 'w') as f:
f.write(merged[k].to_json())
我怎样才能加快我的循环?
答案确实是计算每个城市的图形,然后根据邻域多边形对其进行裁剪。这是 @gboeing 在他对
city=gpd.read_file('C:/folder/file.geojson')
city_graph=ox.graph_from_polygon(city.unary_union, network_type='drive')
circ=[]
for j in (len(city)):
intersecting_nodes = nodes[nodes.intersects(j)].index
neighbourhood_graph = city_graph.subgraph(intersecting_nodes)
stat = ox.basic_stats(neighbourhood_graph)
circ.append(stat['circuity_avg'])
circ=pd.Series(circ)
merged.append(pd.concat([city, circ], axis=1))