str / srepr , clear_cache

28 views
Skip to first unread message

Vinzenz Bargsten

unread,
Jan 10, 2013, 5:01:52 PM1/10/13
to sy...@googlegroups.com
Hi,

I would like to use sympy expressions with multiprocessing. This
requires the expressions to be pickled. As this is currently not
possible with functions included in the expressions, I tried using srepr
and str to convert the expressions to strings and then hand them over to
other processes. However, I observed that my expressions are not
correctly restored (first using srepr, eval), especially if the cache is
cleared (also in other processes). Currently, I use str / S and do not
clear the cache. Note: I removed all assumptions from my symbols. Find
some test output and script below.

1. Are there better options than this to transfer such expressions to
other processes?
2. Is it intended that clearing the cache has this effect?
3. Did the 0.7.1 version have some problem with srepr ?

Thanks and kind regards,
Vinzenz
------------------------------------------------------------------------
:~> python3.2 sympy_strrepr_test.py
Version: 0.7.1-git
srepr(x): Function('x')(Symbol('t'))
str(x): x(t)
x==S(srepr(x)): False
x==eval(srepr(x))): False
x==S(str(x)): True
x(t)
Clearing cache...
x(t)
x==S(srepr(x)): False
x==eval(srepr(x))): False
x==S(str(x)): False
-------------------------------------------------------------------------
:~> python2.7 sympy_strrepr_test.py
Version: 0.7.2
srepr(x): Function('x')(Symbol('t'))
str(x): x(t)
x==S(srepr(x)): True
x==eval(srepr(x))): True
x==S(str(x)): True
x(t)
Clearing cache...
x(t)
x==S(srepr(x)): False
x==eval(srepr(x))): False
x==S(str(x)): False
-------------------------------------------------------------------------
:~> python2.7 sympy_strrepr_test.py
Version: 0.7.1-git
srepr(x): Function('x')(Symbol('t'))
str(x): x(t)
x==S(srepr(x)): False
x==eval(srepr(x))): False
x==S(str(x)): True
x(t)
Clearing cache...
x(t)
x==S(srepr(x)): False
x==eval(srepr(x))): False
x==S(str(x)): False
-------------------------------------------------------------------------


This is the script I use:
from __future__ import print_function
from sympy import *
from sympy import __version__ as sympyver
print("Version: ", sympyver)
x=S('x(t)')
print("srepr(x): ", srepr(x))
print("str(x): ", str(x))

print("x==S(srepr(x)): ",x==S(srepr(x)))
print("x==eval(srepr(x))): ",x==eval(srepr(x)))
print("x==S(str(x)): ",x==S(str(x)))
print(x)
print("Clearing cache...")
cache.clear_cache()
print(x)
print("x==S(srepr(x)): ",x==S(srepr(x)))
print("x==eval(srepr(x))): ",x==eval(srepr(x)))
print("x==S(str(x)): ",x==S(str(x)))


Aaron Meurer

unread,
Jan 10, 2013, 5:44:16 PM1/10/13
to sy...@googlegroups.com
On Thu, Jan 10, 2013 at 3:01 PM, Vinzenz Bargsten
<vbar...@googlemail.com> wrote:
> Hi,
>
> I would like to use sympy expressions with multiprocessing. This requires
> the expressions to be pickled. As this is currently not possible with
> functions included in the expressions, I tried using srepr and str to
> convert the expressions to strings and then hand them over to other
> processes. However, I observed that my expressions are not correctly
> restored (first using srepr, eval), especially if the cache is cleared (also
> in other processes). Currently, I use str / S and do not clear the cache.
> Note: I removed all assumptions from my symbols. Find some test output and
> script below.
>
> 1. Are there better options than this to transfer such expressions to other
> processes?

You could try to get around the pickling issues by writing a custom
pickler. See for example
https://code.google.com/p/sympy/issues/detail?id=2587 and associated
pull request https://github.com/sympy/sympy-live/pull/46. I don't
know what would be required to make this work for Functions. There
might be some hints at
https://code.google.com/p/sympy/issues/detail?id=1198, which explains
why it doesn't work. Actually, there's a trick in that issue that
works some of the time, but not all of the time (setting __module__).
You could see if that works for you.

Or we could try to actually fix the problem and rewrite Function in a
way that is pickable already.

> 2. Is it intended that clearing the cache has this effect?

No, that's a bug.

If you turn the cache off completely, you get False for everything. I
think the core of the issue is
https://code.google.com/p/sympy/issues/detail?id=1612 (and related
issue 440).

Actually, my old fix at seems to work (with it, your script gives all
True). I created a pull request with the fix at
https://github.com/sympy/sympy/pull/1710. We'll see if there are
issues still.

> 3. Did the 0.7.1 version have some problem with srepr ?

It wasn't an issue with srepr(), which returned the same thing. The
issues are are with the subtleties of how __eq__ works in the core.

Anyway, let's not worry about 0.7.1. That was released over a year
ago, and we have a hard enough time managing the bugs that are
current.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to
> sympy+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.
>
Reply all
Reply to author
Forward
0 new messages