|
31 | 31 | load_all_packages,
|
32 | 32 | )
|
33 | 33 | from pysr.export_latex import sympy2latex
|
| 34 | +from pysr.export_sympy import pysr2sympy |
34 | 35 | from pysr.feature_selection import _handle_feature_selection, run_feature_selection
|
35 | 36 | from pysr.julia_helpers import init_julia
|
36 | 37 | from pysr.sr import (
|
@@ -903,6 +904,28 @@ def test_feature_selection_handler(self):
|
903 | 904 | class TestMiscellaneous(unittest.TestCase):
|
904 | 905 | """Test miscellaneous functions."""
|
905 | 906 |
|
| 907 | + def test_pickle_inv_sympy_expression(self): |
| 908 | + """Test that sympy expressions with the inv operator can be pickled and unpickled correctly.""" |
| 909 | + expr_str = "inv(x0) + x1" |
| 910 | + sympy_expr = pysr2sympy(expr_str, feature_names_in=["x0", "x1"]) |
| 911 | + |
| 912 | + # Evaluate the original expression at a test point |
| 913 | + test_vals = {sympy.Symbol("x0"): 2.0, sympy.Symbol("x1"): 3.0} |
| 914 | + original_result = float(sympy_expr.subs(test_vals)) |
| 915 | + |
| 916 | + # Pickle and unpickle the sympy expression |
| 917 | + serialized = pkl.dumps(sympy_expr) |
| 918 | + deserialized_expr = pkl.loads(serialized) |
| 919 | + |
| 920 | + # Evaluate the unpickled expression at the same test point |
| 921 | + unpickled_result = float(deserialized_expr.subs(test_vals)) |
| 922 | + |
| 923 | + # Verify the results match |
| 924 | + self.assertEqual(original_result, unpickled_result) |
| 925 | + |
| 926 | + # Check that the same operator mapping was used (1/x for inv) |
| 927 | + self.assertEqual(original_result, 0.5 + 3.0) # 1/2 + 3 = 3.5 |
| 928 | + |
906 | 929 | def test_pickle_with_temp_equation_file(self):
|
907 | 930 | """If we have a temporary equation file, unpickle the estimator."""
|
908 | 931 | model = PySRRegressor(
|
|
0 commit comments