Skip to content

FIX: Allow statespace models to be pickled in Python 3.6 - #3402

Merged
josef-pkt merged 6 commits into
statsmodels:masterfrom
bashtage:pickle-kalman-filter
Feb 12, 2017
Merged

josef-pkt merged 6 commits into
statsmodels:masterfrom
bashtage:pickle-kalman-filter

Conversation

@bashtage

Copy link
Copy Markdown
Member

Minimal patch to allow pickling.
Likely is not correct in the sense that the pickled objects would
not behave the same after pickling.

@bashtage

Copy link
Copy Markdown
Member Author

Fixes on Python 3.6. Probably doesn't contain enough to actually correctly run the three objects that can now be pickled since there is a lot of state. Correct method will require __reduce___ and __setstate__ to be defined so that the class can be initialized and then any state that can change can be restored.

@bashtage

Copy link
Copy Markdown
Member Author

Pinging @ChadFulton

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same at 88.081% when pulling bc31578 on bashtage:pickle-kalman-filter into 47b7c9d on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same at 88.081% when pulling b5005a2 on bashtage:pickle-kalman-filter into 47b7c9d on statsmodels:master.

@bashtage
bashtage force-pushed the pickle-kalman-filter branch from 005ecd7 to 429f724 Compare January 31, 2017 16:54
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same at 88.081% when pulling 429f724 on bashtage:pickle-kalman-filter into 47b7c9d on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same at 88.081% when pulling 429f724 on bashtage:pickle-kalman-filter into 47b7c9d on statsmodels:master.

@ChadFulton

Copy link
Copy Markdown
Member

If this fixes the tests, then that sounds great to me.

@bashtage

Copy link
Copy Markdown
Member Author

Unfortunately pickling is much harder than this. Probably not worth it.

@bashtage
bashtage force-pushed the pickle-kalman-filter branch 2 times, most recently from c2ebcd9 to 3bfff02 Compare February 1, 2017 17:14
@bashtage

bashtage commented Feb 1, 2017

Copy link
Copy Markdown
Member Author

I think it words correctly now. I probably copied more state that was strictly necessary since I wasn't sure what should persist across iterations and what was strictly temporary.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.1%) to 87.958% when pulling 0d662b9 on bashtage:pickle-kalman-filter into 47b7c9d on statsmodels:master.

@bashtage
bashtage force-pushed the pickle-kalman-filter branch from 0d662b9 to 6289e67 Compare February 1, 2017 20:22
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.1%) to 87.957% when pulling 6289e67 on bashtage:pickle-kalman-filter into 47b7c9d on statsmodels:master.

@bashtage

bashtage commented Feb 1, 2017

Copy link
Copy Markdown
Member Author

@ChadFulton I think this is ready. Would be helpful if you could check the variables that I save in state in the method __reduce__. If any of these don't need to be saved, I could reduce the size of the pickle. For example, I am even saving temporary arrays which I suspect are always over written when used. But in case they are not, it seemed safer to save them.

I might make one final commit where I add the pickle tests to their own file test_pickle.py

@bashtage
bashtage force-pushed the pickle-kalman-filter branch from 6289e67 to 18c802f Compare February 2, 2017 07:43
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.1%) to 87.977% when pulling 18c802f on bashtage:pickle-kalman-filter into 67dcd43 on statsmodels:master.

@bashtage
bashtage force-pushed the pickle-kalman-filter branch from e8ac27e to 6c70cad Compare February 2, 2017 12:22
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.2%) to 87.893% when pulling 6c70cad on bashtage:pickle-kalman-filter into 67dcd43 on statsmodels:master.

@bashtage

bashtage commented Feb 3, 2017

Copy link
Copy Markdown
Member Author

closes #3401

@bashtage
bashtage force-pushed the pickle-kalman-filter branch from 6c70cad to 69047c8 Compare February 6, 2017 10:30
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.2%) to 87.92% when pulling 69047c8 on bashtage:pickle-kalman-filter into d42afbd on statsmodels:master.

@josef-pkt josef-pkt added this to the 0.9 milestone Feb 6, 2017
@TomAugspurger TomAugspurger mentioned this pull request Feb 7, 2017
4 of 28 tasks
@bashtage
bashtage force-pushed the pickle-kalman-filter branch from 69047c8 to 357f526 Compare February 8, 2017 22:33
@coveralls

coveralls commented Feb 8, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.05%) to 90.211% when pulling 357f526 on bashtage:pickle-kalman-filter into dcff8e6 on statsmodels:master.

@bashtage

Copy link
Copy Markdown
Member Author

@josef-pkt as long as @ChadFulton is happy with this, then it is ready.

Minimal patch to allow pickling.
Likely is not correct in the sense that the pickled objects would
not behave the same after pickling.
Add __reduce__ and __setstate__ to Statespace objects
Add __reduce__ and __setstate__ to KamlanFilter objects
Add tests that will fail ifpickling doesn't work
Add reduce methods
Early version of NumPy (<= 1.7) do not preserve matrix order with
copying or pickling
Add tests the check that pickled models behave identically to
originals
@bashtage
bashtage force-pushed the pickle-kalman-filter branch from 357f526 to e02d40d Compare February 11, 2017 14:37
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.08%) to 90.261% when pulling e02d40d on bashtage:pickle-kalman-filter into c2acb1a on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member

Thanks, merging,
test failures are unrelated to this

@josef-pkt
josef-pkt merged commit f81cde3 into statsmodels:master Feb 12, 2017
@bashtage
bashtage deleted the pickle-kalman-filter branch February 12, 2017 20:29
@nruchika21

Copy link
Copy Markdown

Hi

I am using python 3.6 and trying to pickle and get the error:
TypeError: can't pickle statsmodels.tsa.statespace._statespace.dStatespace objects

This is how I am doing it:
filename ="model.pkl"
with open(filename, 'wb') as fp:
cPickle.dump(stepwise_fit,fp)

Any ideas as to what I can do to fix this?

Thanks, Any help is much appreciated!
Ruchika

@bashtage

bashtage commented Mar 22, 2018 via email

Copy link
Copy Markdown
Member Author

@nruchika21

Copy link
Copy Markdown

Master of what ?

@bashtage

bashtage commented Mar 22, 2018 via email

Copy link
Copy Markdown
Member Author

@nruchika21

nruchika21 commented Mar 22, 2018 via email

Copy link
Copy Markdown

@bashtage

Copy link
Copy Markdown
Member Author

Somethin like

pip install git+https://github.com/statsmodels/statsmodels

This requires you to a compiler for your version of Python.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants