jmschrei / yahmm

Yet Another Hidden Markov Model repository.
MIT License
249 stars 32 forks source link

yahmm Model can't be pickled #44

Closed shpigi closed 9 years ago

shpigi commented 9 years ago
IPython QtConsole 3.1.0
Python 2.7.9 |Anaconda 2.1.0 (64-bit)| (default, Mar  9 2015, 16:20:48) 
Type "copyright", "credits" or "license" for more information.

IPython 3.1.0 -- An enhanced Interactive Python.
Anaconda is brought to you by Continuum Analytics.

In [1]: import cPickle as pickle

In [2]: import yahmm as ym

In [3]: m=ym.Model()

In [4]: m.bake()

In [5]: m
Out[5]: <yahmm.yahmm.Model at 0xd8df30>

In [6]: pickle.dump(m,open("ttt.p","wb"))

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-a10217961e3f> in <module>()
----> 1 pickle.dump(m,open("ttt.p","wb"))

/opt/Anaconda/lib/python2.7/copy_reg.pyc in _reduce_ex(self, proto)
     68     else:
     69         if base is self.__class__:
---> 70             raise TypeError, "can't pickle %s objects" % base.__name__
     71         state = base(self)
     72     args = (self.__class__, base, state)

TypeError: can't pickle Model objects

My understanding is that cdef classes can not be automatically pickled by pickle. This can apparently be solved by adding the appropriate __reduce__() function to the class. So perhaps this issue should be labeled as an enhancement request.

jmschrei commented 9 years ago

Hi shpigi

I am not adding any more functionality to HMMs in the YAHMM project, just fixing major bugs. I have merged yahmm with my larger pomegranate project (where I will add functionality to HMMs as needed) as I am using different graphical models in my research now.

However, if you do not have any tied distributions or edges in your model you should be able to use model.write and model.read in order to save your models to an external source. In YAHMM this is a tsv text file, in pomegranate tied emissions/edges are handled and the output is a JSON.

If you'd like to transition your code from YAHMM to pomegranate, all you need to do after pip install pomegranate (assuming a successful install) is to replace from yahmm import * with from pomegranate import HiddenMarkovModel as Model.

Sorry for the inconvenience, let me know if the reading/writing scheme works for you.

shpigi commented 9 years ago

Switching to pomegranate ! Way to go ! Thanks.