KDtree uses nested classes to determine its node types (innernode, leafnode). Pickle only works on module level definitions, so a nested class disables it:
import cPickle class Foo(object): class Bar(object): pass obj = Foo.Bar() print obj.__class__ cPickle.dumps(obj) <class '__main__.Bar'> cPickle.PicklingError: Can't pickle <class '__main__.Bar'>: attribute lookup __main__.Bar failed
However, there is a (hacky) workaround by decapitating class definitions in scipy.spatial.kdtree in the module area so that the sorter can find them. If all of your code that reads and writes pickled KDtree objects installs these patches, this hack should work fine:
import cPickle import numpy from scipy.spatial import kdtree
Output:
<class 'scipy.spatial.kdtree.innernode'> "ccopy_reg\n_reconstructor\np1\n(cscipy.spatial.kdtree\nKDTree\np2\nc_ [3 4] [3 4]
samplebias
source share