Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pickle issues #845

Merged
merged 5 commits into from
Oct 5, 2019
Merged

Fix pickle issues #845

merged 5 commits into from
Oct 5, 2019

Conversation

AbdealiLoKo
Copy link
Contributor

@AbdealiLoKo AbdealiLoKo commented Oct 5, 2019

For univariate models: _hazard is a property in the object, but actually a function - By making it a function, it becomes a part of the class definition and hence the pickle does not consider it as a property that needs to be serialized.

From my manual testing, these fitters can now be pickled:

  • LogLogisticFitter
  • WeibullFitter
  • GeneralizedGammaFitter
    Also, removed the xfail from the pickle and added joblib test cases in Univariate.

EDIT: I did some more digging and figured out the issue for the Regression models too.
For regression models: _neg_likelihood_with_penalty_function is a property in the object, but actually a function. By making it a functools.partial, pickle will know how to pickle it. Also, it needs the unflatten function which is an autograd function which cannot be pickled. So, we save the data to recreate it with autograd instead of saving the autograd function itself.

From my manual testing, these fitters can now be pickled:

  • WeibullAFTFitter
  • LogNormalAFTFitter
  • LogLogisticAFTFitter
    And I also added pickle test cases for this, and removed the xfail from joblib test case for Regression models

Fixes #188

X needs to be a dataframe currently. Print the type of X in the
exception message so it's clearer to users what is not supported.
_hazard() was a dynamic lambda function - make it a normal
function so it can be pickled.
For _create_neg_likelihood_with_penalty_function()
use the functools.partial approach instead of function inside
function so that it can be pickled correctly.

Also, save the _initial_point_dict so that the unflatten function
can be generated on the fly and the autograd function does not need
to be a property that needs to be saved.
@AbdealiLoKo AbdealiLoKo changed the title Fix pickle issues in Univariate Fitters Fix pickle issues Oct 5, 2019
@CamDavidsonPilon
Copy link
Owner

This is A+++++ 🙌

I'm going to point this PR at my release branch and get it out soon. I'd like to do some local testing with it too.

@CamDavidsonPilon CamDavidsonPilon changed the base branch from master to v0.22.8 October 5, 2019 15:20
@CamDavidsonPilon CamDavidsonPilon merged commit 15913e2 into CamDavidsonPilon:v0.22.8 Oct 5, 2019
@AbdealiLoKo
Copy link
Contributor Author

Sounds great, thanks!

@torresxavier
Copy link

Hi @AbdealiJK !!! good post. I have not been able to save/serialize my WeibullAFTFitter. It keeps saying "" Sorry, pickling not yet supported. See pydata/patsy#26 if you want to help." I have been able to save it with a CoxPHFitter, but not with WeibullAFTFitter. And this is the one wich shows best results.
Can you please provide the code to save/serialize the WeibullAFTFitter??
So many thanks

@CamDavidsonPilon
Copy link
Owner

Commented in #1138

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants