Skip to content

Commit

Permalink
Made from_file pickle loading more robust
Browse files Browse the repository at this point in the history
Added backwards compatibility for loading models pickled under older versions.

Adds missing parameters with their default values.
  • Loading branch information
tbuckworth authored Aug 21, 2024
1 parent db5a055 commit 1772b0d
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pysr/sr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,17 @@ def from_file(
base_equation_file = os.path.basename(model.equation_file_)
model.equation_file_ = os.path.join(base_dir, base_equation_file)

# Get constructor parameters and default values
params = inspect.signature(model.__init__).parameters

# Filter for missing parameters excluding kwargs
missing_params = {k: v for k, v in params.items() if
k not in model.__dict__.keys() and v.name != "self" and v.kind != v.VAR_KEYWORD}

# Assign missing attributes
for k, v in missing_params.items():
setattr(model, k, v)

# Update any parameters if necessary, such as
# extra_sympy_mappings:
model.set_params(**pysr_kwargs)
Expand Down

0 comments on commit 1772b0d

Please sign in to comment.