Skip to content

Instantly share code, notes, and snippets.

@jlaura
Created July 8, 2014 19:49
Show Gist options
  • Save jlaura/1bf64fc88eafe0b3dc62 to your computer and use it in GitHub Desktop.
Save jlaura/1bf64fc88eafe0b3dc62 to your computer and use it in GitHub Desktop.
Test the cKDTree in scipy.spatial for replacement in the PySAL library
import os
from subprocess import call
import sys
import pysal
from numpy.testing import assert_equal
shps_to_test = []
examplesdir = 'examples/'
for f in os.listdir(examplesdir):
if f[-4:] == '.shp':
shps_to_test.append(os.path.join(examplesdir, f))
results = {}
for i in shps_to_test:
results[i] = []
lineshps = []
#Test the W generation with the stock kdtree
for s in shps_to_test:
try:
#Run the native kdtree
w = pysal.weights.user.knnW_from_shapefile(s, k=3)
results[s].append(w)
except:
print "KNN failed on {}".format(s)
lineshps.append(s)
#Remove the shp types that knn is not appropriate for - lines
for l in lineshps:
results.pop(l)
shps_to_test.remove(l)
#Alter the source code to utilize the cKDTree
cmd = "sed -i.bu s/scipy.spatial.KDTree/scipy.spatial.cKDTree/g /Users/jay/github/pysal/pysal/cg/kdtree.py"
call(cmd.split(' '))
reload(pysal)
for s in shps_to_test:
try:
w = pysal.weights.user.knnW_from_shapefile(s, k=3)
results[s].append(w)
except:
print "KNN failed on {}".format(s)
for k, v in results.iteritems():
print 'Testing: {}'.format(k)
kdw = v[0]
ckdw = v[1]
try:
assert(kdw.cardinalities == ckdw.cardinalities)
print "Passed cardinality match."
except:
print "cardinalities mismatch"
try:
assert(kdw.n == ckdw.n)
print "Number of observations match."
except:
print "n mismatch"
try:
assert_equal(kdw.full(), ckdw.full())
print "Full matrix representations match"
except:
print "matrix mismatch"
print
#Return the source file back to the original state.
cmd = "sed -i.bu s/scipy.spatial.cKDTree/scipy.spatial.KDTree/g /Users/jay/github/pysal/pysal/cg/kdtree.py"
call(cmd.split(' '))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment