In [118]:
import pysal as ps
import time
import numpy as np
try:
reload(network)
except:
import network
try:
reload(util)
except:
import util
In [119]:
N = network.Network(ps.examples.get_path('geodanet/streets.shp'))
In [120]:
N.node_distance_matrix()
In [121]:
#Check that the nodes are all account for in the alldistances dict
assert(N.alldistances.keys() == N.node_list)
In [122]:
error = False
for k, v in N.alldistances.iteritems():
predlists = v[1]
distances = v[0]
#Confirm that distance to self is 0
assert(distances[k] == 0)
#Confirm that the destination for all previous lists is directly adjacent to the current root
for p, plists in predlists.iteritems():
try:
assert(plists[-1] == k)
except:
print k, plists, p
error = True
break
#Confirm that all nodes have a path to all other nodes
try:
assert(N.node_list == predlists.keys())
except:
print k, sorted(set(N.node_list).difference(set(predlists.keys())))
error = True
break
if error == True:
break
In [123]:
N.alldistances[215][0][229]
Out[123]:
In [124]:
N.alldistances[229][0][215]
Out[124]:
In [126]:
N.alldistances[215][1][229]
Out[126]:
In [127]:
N.alldistances[229][1][215]
Out[127]:
In [135]:
#The shortest path from A to B must equal B to A in an undirected graph
assert(N.alldistances[215][1][229][:-1] == N.alldistances[229][1][215][:-1][::-1])
In [ ]:
In [65]:
len(N.alldistances[0][1])
Out[65]:
In [67]:
len(N.alldistances)
Out[67]:
In [68]:
N.alldistances.keys()
Out[68]:
In [136]:
Is = N.alldistances.keys()
for i in Is[0:-1]:
for j in Is[i+1:]:
dij = N.alldistances[i][0][j]
dji = N.alldistances[j][0][i]
if (dij - dji) / dji > 0.001:
print i,j, dij, dji
try:
pij = N.alldistances[i][1][j]
except:
print i,j, 'missing path'
try:
pji = N.alldistances[j][1][i]
except:
print j,i, 'missing path'
In [ ]: