From 05d7f4b3b5d1f049423c234ac98058426939f755 Mon Sep 17 00:00:00 2001 From: Martin Fleischmann Date: Tue, 20 Oct 2020 19:06:13 +0100 Subject: [PATCH] interbuilding distance --- measuring/morphometrics.ipynb | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/measuring/morphometrics.ipynb b/measuring/morphometrics.ipynb index 0d719c0..d593ba4 100644 --- a/measuring/morphometrics.ipynb +++ b/measuring/morphometrics.ipynb @@ -44,6 +44,7 @@ "import geopandas\n", "import libpysal\n", "import momepy\n", + "import networkx as nx\n", "import numpy as np\n", "import pandas as pd\n", "import pygeos\n", @@ -491,22 +492,25 @@ " \n", " # mean interbuilding distance\n", " # define adjacency list from lipysal\n", - " adj_list = w.to_adjlist(remove_symmetric=True)\n", - " adj_list[\"distance\"] = (\n", + " adj_list = w.to_adjlist(remove_symmetric=False)\n", + " adj_list[\"weight\"] = (\n", " df.buildings.iloc[adj_list.focal]\n", " .reset_index(drop=True)\n", " .distance(df.buildings.iloc[adj_list.neighbor].reset_index(drop=True)).values\n", " )\n", - " adj_list = adj_list.set_index(['focal', 'neighbor'])\n", - "\n", - "\n", - " def mean_interbuilding_distance(x):\n", - " neighbours = [x]\n", - " neighbours += w3.neighbors[x]\n", - " return adj_list.distance.loc[neighbours, neighbours].mean()\n", "\n", + " G = nx.from_pandas_edgelist(\n", + " adj_list, source=\"focal\", target=\"neighbor\", edge_attr=\"weight\"\n", + " )\n", + " ibd = []\n", + " for i in range(len(df)):\n", + " try:\n", + " sub = nx.ego_graph(G, i, radius=3)\n", + " ibd.append(np.nanmean([x[-1] for x in list(sub.edges.data('weight'))]))\n", + " except:\n", + " ibd.append(np.nan)\n", "\n", - " df['ltbIBD'] = [mean_interbuilding_distance(x) for x in range(len(df))]\n", + " df['ltbIBD'] = ibd\n", " \n", " # Reached neighbors and area on 3 topological steps on tessellation\n", " df['ltcRea'] = [w3.cardinalities[i] for i in range(len(df))]\n",