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

Cannot pickle lambdas from SymPy #104

Closed
parnmatt opened this issue Jun 2, 2015 · 3 comments
Closed

Cannot pickle lambdas from SymPy #104

parnmatt opened this issue Jun 2, 2015 · 3 comments

Comments

@parnmatt
Copy link

parnmatt commented Jun 2, 2015

I have been using SymPy recently. Anonymous functions can be generated using sympy.lambdify.
I required to store these for later, as they can be computationally expensive to generate the expressions in the first place; i.e., solving an ODE system symbolically.

dill came to mind as I have previously used it to pickle a lambda, however it was giving errors. Here is a minimalistic example of a simple function:

import sympy
import dill

# use basic session, puts multiple symbols in scope
sympy.init_session()

# create expression, simple for example
expr = 2*x + y

# generate anonymous function
func = sympy.lambdify((x, y), expr)

print(func(5, 6))  #=> prints 16

# pickle file
pickled_func = dill.dumps(func)

Where I get a big error that traces back to:

PicklingError: Can't pickle : it's not found as mpmath.ctx_mp_python.constant

Now I thought this maybe an issue with how SymPy generates the function, however cloudpickle was able to successfully pickle the function:

import pickle
import cloudpickle

print(pickle.loads(cloudpickle.dumps(func))(5, 6))  #=> prints 16

It was brought to my attention that this may be unexpected behaviour with how dill interprets lambdas of objects; and thus you should be made aware.

@mmckerns
Copy link
Member

mmckerns commented Jun 3, 2015

If you use the flag recurse=True on dumps, dill can pickle your func (see #86). This behavior will likely become the default setting once #105 is resolved.

@mmckerns mmckerns closed this as completed Jun 3, 2015
@mmckerns
Copy link
Member

mmckerns commented Jun 3, 2015

added global setting dill.settings['recurse']

@mmckerns
Copy link
Member

@mvanvleet: would you mind deleting your comment and posting it as a new issue? You can always refer back to this issue with a link.

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

No branches or pull requests

2 participants