dump a lambda function in a file and load it in another function

1.1k views Asked by At

To begin with, here is the outline of the workflow I want to do: 1. Use sympy to "do the math" and develop some expressions 2. Lambdify the corresponding expressions 3. Store the corresponding lambda functions in a file 4. Load them in an independent part of my code

Everything goes fine until step 3. I tried different things, and in particular after having read this this is a minimalistic example I would like to make work:

import sympy as sp
import dill as pickle

x, y = sp.symbols("x, y") 

f_example = 2*x**2 + 2*x*y
f_lbda= sp.lambdify((x, y),f_example ) 
pickle.settings['recurse'] = True 
fileW = open('file_where_I_dump.dill', 'wb')
# the following line crashes 
pickle.dump([f_lbda, f_lbda], fileW)
fileR = open('file_where_I_dump.dill', 'rb')
f_lbda_loaded = pickle.load(fileR)

I get this error (after an important number of During handling of the above exception, another exception occurred :

ValueError: 'axis' entry is out of bounds

Am I missing something important here ?

Note: when I dump the sympy expressions instead and lambdify the function after a pickle.load, everything goes fine. But this is not exactly the workflow I need !

Thanks for your help !

2

There are 2 answers

0
Mike McKerns On

Here's a slightly modified version of your code (below): It looks like a bug. Note that pickling the lambda expression destroys the original symbolic expression object!

I'm the dill author. I'd suggest posting it as a sympy github issue -- f you post it as a dill issue, I'll dig into it then punt it over to sympy.

>>> import sympy as sp
>>> x,y = sp.symbols('x,y')
>>> f_ex = 2*x**2 + 2*x*y
>>> f_la = sp.lambdify((x,y),f_ex)
>>> f_ex
2*x**2 + 2*x*y
>>> import dill
>>> dill.settings['recurse'] = True
>>> la = dill.loads(dill.dumps(f_la))
>>> la
<function <lambda> at 0x10b4ed668>
>>> f_ex
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/core/basic.py", line 392, in __repr__
    return sstr(self, order=None)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/printing/str.py", line 748, in sstr
    s = p.doprint(expr)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/printing/printer.py", line 233, in doprint
    return self._str(self._print(expr))
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/printing/printer.py", line 257, in _print
    return getattr(self, printmethod)(expr, *args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/printing/str.py", line 51, in _print_Add
    terms = self._as_ordered_terms(expr, order=order)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/printing/printer.py", line 271, in _as_ordered_terms
    return expr.as_ordered_terms(order=order)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/core/expr.py", line 888, in as_ordered_terms
    terms, gens = self.as_terms()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/core/expr.py", line 920, in as_terms
    coeff = complex(coeff)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/core/expr.py", line 229, in __complex__
    result = self.evalf()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/core/evalf.py", line 1377, in evalf
    prec = dps_to_prec(n)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mpmath/libmp/libmpf.py", line 67, in dps_to_prec
    return max(1, int(round((int(n)+1)*3.3219280948873626)))
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 2293, in amax
    out=out, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/_methods.py", line 26, in _amax
    return umr_maximum(a, axis, None, out, keepdims)
ValueError: 'axis' entry is out of bounds
0
MarcoMag On

As a workaround I propose the following:

from sympy import *
from cloudpickle import dump, load

var("x, y") 
f_example = 2*x**2 + 2*x*y
f_lbda= lambdify((x, y),f_example ) 

with open('file_where_I_dump_flbda', 'wb') as f: 
    dump((f_lbda), f)

In another code that I have where I have complicated sympy expressions that I lambdify for later use, using dump and load from cloudpickle works! well