python - 加速 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 在他对 this question 的回答中提出的。 .

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))

https://stackoverflow.com/questions/60269760/

相关文章:

css - 如何在不破坏所有内容的情况下 stub Sprockets 4 中的 Assets ?

r - 尝试写入或绘制光栅时 .local(object, ...) 出错

ruby - RSpec Mock - 类未实现实例方法 : jql. 也许您打算改用 `class

python - 如何在 TensorFlow 2 中保存/加载模型的一部分?

azure-devops - 在 Azure Devops 中编写 EF6 迁移脚本

php - 无服务器框架,处理程序不存在

javascript - Vue.js 清除用户缓存

node.js - 谷歌 API + Passport + react : Authenticati

pytorch - 在 Pytorch tensorboard 中绘制几张图

google-cloud-automl - AutoML 视觉 : Error: ImportDat