-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: correctly pickle UnevaluatedExpression (#139)
* fix: define getnewargs_ex instead of getnewargs Fixes pickling problem for UnevaluatedExpression https://docs.python.org/3/library/pickle.html#object.__getnewargs_ex__ * test: test pickling of UnevaluatedExpressions * docs: unpickle helicity module in notebook (serves as an additional test)
- Loading branch information
Showing
3 changed files
with
39 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import pickle | ||
|
||
import sympy as sp | ||
|
||
from ampform.dynamics import BlattWeisskopfSquared | ||
from ampform.sympy import UnevaluatedExpression | ||
|
||
|
||
class TestUnevaluatedExpression: | ||
@staticmethod | ||
def test_pickle(): | ||
z = sp.Symbol("z") | ||
angular_momentum = sp.Symbol("L", integer=True) | ||
|
||
# Pickle simple SymPy expression | ||
expr = z * angular_momentum | ||
pickled_obj = pickle.dumps(expr) | ||
imported_expr = pickle.loads(pickled_obj) | ||
assert expr == imported_expr | ||
|
||
# Pickle UnevaluatedExpression | ||
expr = UnevaluatedExpression() | ||
pickled_obj = pickle.dumps(expr) | ||
imported_expr = pickle.loads(pickled_obj) | ||
assert expr == imported_expr | ||
|
||
# Pickle class derived from UnevaluatedExpression | ||
expr = BlattWeisskopfSquared(angular_momentum, z=z) | ||
pickled_obj = pickle.dumps(expr) | ||
imported_expr = pickle.loads(pickled_obj) | ||
assert expr == imported_expr |