Skip to content

Commit

Permalink
Merge pull request #182 from jGaboardi/nearest_tree_update
Browse files Browse the repository at this point in the history
Nearest tree update
  • Loading branch information
jGaboardi committed Dec 3, 2018
2 parents 1c91b9f + 6ed076b commit 8ea174c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
16 changes: 14 additions & 2 deletions spaghetti/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,13 @@ def allneighbordistances(self, sourcepattern, destpattern=None,
tree_nearest : dict
Nearest network node to point pattern vertex shortest
path lookup.
path lookup. The values of the dictionary are a ``tuple``
of the nearest source vertex and the near destination
vertex to query the lookup tree. If two observations are
snapped to the same network arc a flag of -.1 is set for
both the source and destination network vertex
indicating the same arc is used while also raising an
``IndexError``` when rebuilding the path.
Examples
--------
Expand Down Expand Up @@ -1218,12 +1224,18 @@ def allneighbordistances(self, sourcepattern, destpattern=None,
dest1, dest2 = dest_vertices[p2]
set2 = set(dest_vertices[p2])

if set1 == set2: # same arc
# when the observations are snapped to the same arc
if set1 == set2:
x1, y1 = sourcepattern.snapped_coordinates[p1]
x2, y2 = destpattern.snapped_coordinates[p2]

computed_length = util.compute_length((x1, y1),
(x2, y2))
nearest[p1, p2] = computed_length
# set the nearest network vertices to a flag of -.1
# indicating the same arc is used while also raising
# and indexing error when rebuilding the path
tree_nearest[p1, p2] = (-.1, -.1)

else:
ddist1, ddist2 = dst_d2n[p2].values()
Expand Down
12 changes: 11 additions & 1 deletion spaghetti/tests/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,17 @@ def test_all_neighbor_distances(self):
self.assertEqual(observed.all(), known.all())
del self.ntw.alldistances
del self.ntw.distancematrix


matrix5, tree = self.ntw.allneighbordistances(self.pp2_str,
gen_tree=True)
known_mtx_val = 1484112.694526529
known_tree_val = (-0.1, -0.1)

self.assertAlmostEqual(np.nansum(matrix5[0]), known_mtx_val, places=4)
self.assertEqual(tree[(18, 19)], known_tree_val)
del self.ntw.alldistances
del self.ntw.distancematrix

def test_all_neighbor_distances_multiproccessing(self):
matrix1, tree = self.ntw.allneighbordistances(self.pp1_str,
fill_diagonal=0.,
Expand Down
10 changes: 10 additions & 0 deletions spaghetti/tests/test_network_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,16 @@ def test_all_neighbor_distances(self):
self.assertEqual(observed.all(), known.all())
del self.ntw.alldistances
del self.ntw.distancematrix

matrix5, tree = self.ntw.allneighbordistances(self.pp2_str,
gen_tree=True)
known_mtx_val = 1484112.694526529
known_tree_val = (-0.1, -0.1)

self.assertAlmostEqual(np.nansum(matrix5[0]), known_mtx_val, places=4)
self.assertEqual(tree[(18, 19)], known_tree_val)
del self.ntw.alldistances
del self.ntw.distancematrix

def test_all_neighbor_distances_multiproccessing(self):
matrix1, tree = self.ntw.allneighbordistances(self.pp1_str,
Expand Down

0 comments on commit 8ea174c

Please sign in to comment.