Repository: martinfleis/momepy
Pull request #9 (2019-04-28 13:31:40)

Test distribution
Added test unit for distribution. During that I eliminated couple of minor bugs in the actual code.


Patch:
diff --git a/momepy/distribution.py b/momepy/distribution.py
index 95be84d..c981917 100644
--- a/momepy/distribution.py
+++ b/momepy/distribution.py
@@ -448,7 +448,7 @@ def neighbour_distance(objects, tessellation, unique_id, weights_matrix=None):
 
         neighbours_ids = tessellation.iloc[neighbours][unique_id]
         building_neighbours = objects.loc[objects[unique_id].isin(neighbours_ids)]
-        if building_neighbours:
+        if len(building_neighbours) > 0:
             results_list.append(np.mean(building_neighbours.geometry.distance(row['geometry'])))
         else:
             results_list.append(0)
@@ -510,7 +510,7 @@ def mean_interbuilding_distance(objects, tessellation, unique_id, weights_matrix
         print('Generating weights matrix (Queen) of {} topological steps...'.format(order))
         from momepy import Queen_higher
         # matrix to define area of analysis (more steps)
-        weights_matrix_higher = Queen_higher(tessellation, k=order)
+        weights_matrix_higher = Queen_higher(k=order, geodataframe=tessellation)
 
     # define empty list for results
     results_list = []
@@ -634,6 +634,7 @@ def neighbouring_street_orientation_deviation(objects):
 
     series = pd.Series(results_list)
     objects.drop(['tmporient'], axis=1)
+    print('Street alignments calculated.')
     return series
 
 
@@ -693,7 +694,7 @@ def building_adjacency(objects, tessellation, weights_matrix=None, weights_matri
         print('Generating weights matrix (Queen) of {} topological steps...'.format(order))
         from momepy import Queen_higher
         # matrix to define area of analysis (more steps)
-        weights_matrix_higher = Queen_higher(tessellation, k=order)
+        weights_matrix_higher = Queen_higher(k=order, geodataframe=tessellation)
 
     print('Generating dictionary of built-up patches...')
     # dict to store nr of courtyards for each uID
@@ -733,7 +734,7 @@ def building_adjacency(objects, tessellation, weights_matrix=None, weights_matri
         patches_sub = [patches[x] for x in indices]
         patches_nr = len(set(patches_sub))
 
-        results_list.append(len(building_neighbours) / patches_nr)
+        results_list.append(patches_nr / len(building_neighbours))
 
     series = pd.Series(results_list)
 


Test patch:
diff --git a/tests/test_distribution.py b/tests/test_distribution.py
index ffee543..e5589a3 100644
--- a/tests/test_distribution.py
+++ b/tests/test_distribution.py
@@ -1,6 +1,7 @@
 import momepy as mm
 import geopandas as gpd
 import numpy as np
+from libpysal.weights import Queen
 
 
 class TestDistribution:
@@ -24,13 +25,86 @@ class TestDistribution:
 
     def test_shared_walls_ratio(self):
         self.df_buildings['swr'] = mm.shared_walls_ratio(self.df_buildings, 'uID')
+        self.df_buildings['swr_uid'] = mm.shared_walls_ratio(self.df_buildings, range(len(self.df_buildings)))
         self.df_buildings['swr_array'] = mm.shared_walls_ratio(self.df_buildings, 'uID', self.df_buildings.geometry.length)
         check = 0.3424804411228673
         assert self.df_buildings['swr'][10] == check
+        assert self.df_buildings['swr_uid'][10] == check
         assert self.df_buildings['swr_array'][10] == check
 
     def test_street_alignment(self):
-        self.df_buildings['orient'] = mm.orientation(self.df_buildings)
+        self.df_buildings['orient'] = orient = mm.orientation(self.df_buildings)
         self.df_buildings['street_alignment'] = mm.street_alignment(self.df_buildings, self.df_streets, 'orient', 'nID', 'nID')
+        self.df_buildings['street_a_arr'] = mm.street_alignment(self.df_buildings, self.df_streets, orient,
+                                                                self.df_buildings['nID'], self.df_streets['nID'])
         check = 0.29073888476702336
         assert self.df_buildings['street_alignment'][0] == check
+        assert self.df_buildings['street_a_arr'][0] == check
+
+    def test_cell_alignment(self):
+        self.df_buildings['orient'] = orient = mm.orientation(self.df_buildings)
+        self.df_tessellation['orient'] = tess_orient = mm.orientation(self.df_tessellation)
+        self.df_buildings['c_align'] = mm.cell_alignment(self.df_buildings, self.df_tessellation, 'orient', 'orient', 'uID')
+        check = abs(self.df_buildings['orient'][0] -
+                    self.df_tessellation[self.df_tessellation['uID'] == self.df_buildings['uID'][0]]['orient'].iloc[0])
+        assert self.df_buildings['c_align'][0] == check
+
+    def test_alignment(self):
+        self.df_buildings['orient'] = orient = mm.orientation(self.df_buildings)
+        self.df_buildings['align'] = mm.alignment(self.df_buildings, 'orient', self.df_tessellation, 'uID')
+        sw = Queen.from_dataframe(self.df_tessellation)
+        self.df_buildings['align_sw'] = mm.alignment(self.df_buildings, 'orient', self.df_tessellation, 'uID', sw)
+        check = 18.299481296455237
+        assert self.df_buildings['align'][0] == check
+        assert self.df_buildings['align_sw'][0] == check
+
+    def test_neighbour_distance(self):
+        self.df_buildings['dist'] = mm.neighbour_distance(self.df_buildings, self.df_tessellation, 'uID')
+        sw = Queen.from_dataframe(self.df_tessellation)
+        self.df_buildings['dist_sw'] = mm.neighbour_distance(self.df_buildings, self.df_tessellation, 'uID', sw)
+        check = 29.18589019096464
+        assert self.df_buildings['dist'][0] == check
+        assert self.df_buildings['dist_sw'][0] == check
+
+    def test_mean_interbuilding_distance(self):
+        self.df_buildings['m_dist'] = mm.mean_interbuilding_distance(self.df_buildings, self.df_tessellation, 'uID')
+        sw = Queen.from_dataframe(self.df_tessellation)
+        swh = mm.Queen_higher(k=3, geodataframe=self.df_tessellation)
+        self.df_buildings['m_dist_sw'] = mm.mean_interbuilding_distance(self.df_buildings, self.df_tessellation, 'uID', sw, swh)
+        check = 29.305457092042744
+        assert self.df_buildings['m_dist'][0] == check
+        assert self.df_buildings['m_dist_sw'][0] == check
+
+    def test_neighbouring_street_orientation_deviation(self):
+        self.df_streets['dev'] = mm.neighbouring_street_orientation_deviation(self.df_streets)
+        check = 6.060070028371881
+        assert self.df_streets['dev'].mean() == check
+
+    def test_building_adjacency(self):
+        self.df_buildings['adj'] = mm.building_adjacency(self.df_buildings, self.df_tessellation)
+        sw = Queen.from_dataframe(self.df_buildings)
+        swh = mm.Queen_higher(k=3, geodataframe=self.df_tessellation)
+        self.df_buildings['adj_sw'] = mm.building_adjacency(self.df_buildings, self.df_tessellation, weights_matrix=sw, weights_matrix_higher=swh)
+        check = 0.2613824113909074
+        assert self.df_buildings['adj'].mean() == check
+        assert self.df_buildings['adj_sw'].mean() == check
+
+    def test_neighbours(self):
+        self.df_tessellation['nei'] = mm.neighbours(self.df_tessellation)
+        sw = Queen.from_dataframe(self.df_tessellation)
+        self.df_tessellation['nei_sw'] = mm.neighbours(self.df_tessellation, sw)
+        self.df_tessellation['nei_wei'] = mm.neighbours(self.df_tessellation, sw, weighted=True)
+        check = 5.180555555555555
+        check_w = 0.029066398893536072
+        assert self.df_tessellation['nei'].mean() == check
+        assert self.df_tessellation['nei_sw'].mean() == check
+        assert self.df_tessellation['nei_wei'].mean() == check_w
+
+    def test_node_degree(self):
+        graph = mm.gdf_to_nx(self.df_streets)
+        deg1 = mm.node_degree(graph=graph, target='graph')
+        deg2 = mm.node_degree(geodataframe=self.df_streets, target='graph')
+        deg3 = mm.node_degree(graph=graph, target='gdf')
+        assert deg1.nodes[(1603650.450422848, 6464368.600601688)]['degree'] == 4
+        assert deg2.nodes[(1603650.450422848, 6464368.600601688)]['degree'] == 4
+        assert deg3.degree.mean() == 2.0625