Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pysr/sr.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
AbstractExpressionSpec,
ExpressionSpec,
ParametricExpressionSpec,
TemplateExpressionSpec,
parametric_expression_deprecation_warning,
)
from .feature_selection import run_feature_selection
Expand Down Expand Up @@ -1467,6 +1468,18 @@ def __getstate__(self) -> dict[str, Any]:
]
return pickled_state

def __setstate__(self, state: dict[str, Any]) -> None:
self.__dict__.update(state)
if (
"equations_" in state
and state["equations_"] is not None
and isinstance(self.expression_spec, TemplateExpressionSpec)
):
raise NotImplementedError(
"Reloading fitted TemplateExpressionSpec models is not yet supported. "
"Please refit the model in the current session."
)

def _checkpoint(self):
"""Save the model's current state to a checkpoint file.

Expand Down
14 changes: 13 additions & 1 deletion pysr/test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2246,8 +2246,19 @@ def test_process_constraints_swaps_multiplication_constraints(self):


class TestTemplateExpressionSpec(unittest.TestCase):
def test_reload_raises_clear_error(self):
model = PySRRegressor(
expression_spec=TemplateExpressionSpec(
combine="f(x)", expressions=["f"], variable_names=["x"]
)
)
model.equations_ = pd.DataFrame({"loss": [0.0]})
model.feature_names_in_ = np.array(["x"])
model.nout_ = 1
with self.assertRaisesRegex(NotImplementedError, "not yet supported"):
pkl.loads(pkl.dumps(model))

def test_num_features_symbol_keys(self):
# ponytail: one check — dict keys must reach Julia as Symbols
spec = TemplateExpressionSpec(
["f", "g"],
"combine(fs, vars) = fs.f(vars[1], vars[2]) + fs.g(vars[3])",
Expand Down Expand Up @@ -2382,6 +2393,7 @@ def runtests(just_tests=False):
TestLaTeXTable,
TestDimensionalConstraints,
TestGuesses,
TestTemplateExpressionSpec,
]
if just_tests:
return test_cases
Expand Down
Loading