KDtree使用嵌套類來定義其節點類型(innernode,leafnode)。泡菜只能在模塊級的類定義,所以嵌套類車次起來:
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
但是,通過猴子打補丁的類定義爲scipy.spatial.kdtree
在模塊範圍,所以,皮克勒一(哈克)解決方法可以找到他們。如果您的所有代碼的讀取和寫入醃製KDtree對象安裝這些補丁,這個技巧應該很好地工作:
import cPickle
import numpy
from scipy.spatial import kdtree
# patch module-level attribute to enable pickle to work
kdtree.node = kdtree.KDTree.node
kdtree.leafnode = kdtree.KDTree.leafnode
kdtree.innernode = kdtree.KDTree.innernode
x, y = numpy.mgrid[0:5, 2:8]
t1 = kdtree.KDTree(zip(x.ravel(), y.ravel()))
r1 = t1.query([3.4, 4.1])
raw = cPickle.dumps(t1)
# read in the pickled tree
t2 = cPickle.loads(raw)
r2 = t2.query([3.4, 4.1])
print t1.tree.__class__
print repr(raw)[:70]
print t1.data[r1[1]], t2.data[r2[1]]
輸出:
<class 'scipy.spatial.kdtree.innernode'>
"ccopy_reg\n_reconstructor\np1\n(cscipy.spatial.kdtree\nKDTree\np2\nc_
[3 4] [3 4]
你嘗試過酸洗? – helloworld922 2011-04-24 21:04:48
當我試圖在KDTree對象上使用cPickle時,我的計算機上出現錯誤 – JoshAdel 2011-04-24 22:19:04