Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to pickle scipy.spatial.KDTree objects

I have a code where I build a huge tree and I need to save it for later use. Unluckily it seems I cannot pickle scipy.spatial.KDTree objects.

In fact, when I run this:

import pickle
import scipy.spatial
tree=scipy.spatial.KDTree([[1,2,3]])
pickle.dump(tree,open('tree.p','wb'))

I get the error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/pickle.py", line 1370, in dump
    Pickler(file, protocol).dump(obj)
  File "/usr/lib64/python2.7/pickle.py", line 224, in dump
    self.save(obj)
  File "/usr/lib64/python2.7/pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)
  File "/usr/lib64/python2.7/pickle.py", line 419, in save_reduce
    save(state)
  File "/usr/lib64/python2.7/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "/usr/lib64/python2.7/pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "/usr/lib64/python2.7/pickle.py", line 663, in _batch_setitems
    save(v)
  File "/usr/lib64/python2.7/pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)
  File "/usr/lib64/python2.7/pickle.py", line 401, in save_reduce
    save(args)
  File "/usr/lib64/python2.7/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "/usr/lib64/python2.7/pickle.py", line 562, in save_tuple
    save(element)
  File "/usr/lib64/python2.7/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "/usr/lib64/python2.7/pickle.py", line 748, in save_global
    (obj, module, name))     pickle.PicklingError: Can't pickle <class 'scipy.spatial.kdtree.leafnode'>: it's not found as scipy.spatial.kdtree.leafnode

Given this, is there a way to pickle it? or at least to save a part of the object that can be used to rebuild the tree fastly? Otherwise, are there other fast options besides scipy.spatial.KDTree?

like image 742
Antonio Ragagnin Avatar asked Nov 01 '25 13:11

Antonio Ragagnin


1 Answers

Use cKDTree instead of KDTree as follows:

import pickle
import scipy.spatial
tree=scipy.spatial.cKDTree([[1,2,3]])
pickle.dump(tree,open('tree.p','wb'))
like image 60
Nagabhushan Baddi Avatar answered Nov 04 '25 02:11

Nagabhushan Baddi