From 1772b0dd23b1a3753cd87c900177f824612817aa Mon Sep 17 00:00:00 2001 From: tbuckworth <55180288+tbuckworth@users.noreply.github.com> Date: Wed, 21 Aug 2024 10:33:48 +0100 Subject: [PATCH 1/3] Made from_file pickle loading more robust Added backwards compatibility for loading models pickled under older versions. Adds missing parameters with their default values. --- pysr/sr.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pysr/sr.py b/pysr/sr.py index 0054ce50..6b5901c7 100644 --- a/pysr/sr.py +++ b/pysr/sr.py @@ -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) From 0b36c6762bbcfe91e74120e4950d7a7629aaf217 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 21 Aug 2024 09:35:15 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pysr/sr.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pysr/sr.py b/pysr/sr.py index 6b5901c7..590f6030 100644 --- a/pysr/sr.py +++ b/pysr/sr.py @@ -1017,13 +1017,18 @@ def from_file( 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} + 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) From 716ef19c8a3ea1e38a9af850f7aafbc73e36b0d4 Mon Sep 17 00:00:00 2001 From: tbuckworth <55180288+tbuckworth@users.noreply.github.com> Date: Thu, 22 Aug 2024 10:26:35 +0100 Subject: [PATCH 3/3] added warning to missing parameters in from_file --- pysr/sr.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pysr/sr.py b/pysr/sr.py index 590f6030..3c0e796b 100644 --- a/pysr/sr.py +++ b/pysr/sr.py @@ -1025,6 +1025,13 @@ def from_file( and v.kind != v.VAR_KEYWORD } + if len(missing_params) > 0: + warnings.warn( + "The following missing parameters will be assigned with default values:" + f"{', '.join(missing_params.keys())}" + "This may be due to the model being saved under an old package version." + ) + # Assign missing attributes for k, v in missing_params.items(): setattr(model, k, v)