From 376f4dfd8e41a912502d8d49fa8b331713eb2b98 Mon Sep 17 00:00:00 2001 From: MilesCranmer Date: Fri, 13 Dec 2024 15:40:03 -0800 Subject: [PATCH 1/5] feat: add `input_stream` and bump version --- pysr/julia_import.py | 3 +++ pysr/juliapkg.json | 2 +- pysr/param_groupings.yml | 1 + pysr/sr.py | 11 +++++++++++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pysr/julia_import.py b/pysr/julia_import.py index 4ea6b88d..26288c75 100644 --- a/pysr/julia_import.py +++ b/pysr/julia_import.py @@ -64,5 +64,8 @@ def _import_juliacall(): jl.seval("using SymbolicRegression") SymbolicRegression = jl.SymbolicRegression +# Expose `D` operator: +jl.seval("using SymbolicRegression: D") + jl.seval("using Pkg: Pkg") Pkg = jl.Pkg diff --git a/pysr/juliapkg.json b/pysr/juliapkg.json index 9c28c96e..1673a713 100644 --- a/pysr/juliapkg.json +++ b/pysr/juliapkg.json @@ -3,7 +3,7 @@ "packages": { "SymbolicRegression": { "uuid": "8254be44-1295-4e6a-a16d-46603ac705cb", - "version": "=1.2.0" + "version": "=1.4.0" }, "Serialization": { "uuid": "9e88b42a-f829-5b0c-bbe9-9e923198166b", diff --git a/pysr/param_groupings.yml b/pysr/param_groupings.yml index f43c67f9..769e6647 100644 --- a/pysr/param_groupings.yml +++ b/pysr/param_groupings.yml @@ -92,6 +92,7 @@ - print_precision - progress - logger_spec + - input_stream - Environment: - temp_equation_file - tempdir diff --git a/pysr/sr.py b/pysr/sr.py index c23805b4..c7beae23 100644 --- a/pysr/sr.py +++ b/pysr/sr.py @@ -618,6 +618,12 @@ class PySRRegressor(MultiOutputMixin, RegressorMixin, BaseEstimator): Logger specification for the Julia backend. See, for example, `TensorBoardLoggerSpec`. Default is `None`. + input_stream : str + The stream to read user input from. By default, this is `"stdin"`. + If you encounter issues with reading from `stdin`, like a hang, + you can simply pass `"devnull"` to this argument. You can also + reference an arbitrary Julia object in the `Main` namespace. + Default is `"stdin"`. run_id : str A unique identifier for the run. Will be generated using the current date and time if not provided. @@ -863,6 +869,7 @@ def __init__( print_precision: int = 5, progress: bool = True, logger_spec: AbstractLoggerSpec | None = None, + input_stream: str = "stdin", run_id: str | None = None, output_directory: str | None = None, temp_equation_file: bool = False, @@ -969,6 +976,7 @@ def __init__( self.print_precision = print_precision self.progress = progress self.logger_spec = logger_spec + self.input_stream = input_stream # - Project management self.run_id = run_id self.output_directory = output_directory @@ -1888,6 +1896,8 @@ def _run( else "nothing" ) + input_stream = jl.seval(self.input_stream) + load_required_packages( turbo=self.turbo, bumper=self.bumper, @@ -2002,6 +2012,7 @@ def _run( crossover_probability=self.crossover_probability, skip_mutation_failures=self.skip_mutation_failures, max_evals=self.max_evals, + input_stream=input_stream, early_stop_condition=early_stop_condition, seed=seed, deterministic=self.deterministic, From ffced7f042418a77e687264078840f1842f2dbc4 Mon Sep 17 00:00:00 2001 From: MilesCranmer Date: Fri, 13 Dec 2024 15:43:26 -0800 Subject: [PATCH 2/5] fix: clear `logger_` from saved state, fixes #779 --- pysr/sr.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pysr/sr.py b/pysr/sr.py index c7beae23..5a330e1b 100644 --- a/pysr/sr.py +++ b/pysr/sr.py @@ -1225,6 +1225,7 @@ def __getstate__(self) -> dict[str, Any]: f"`{state_key}` at runtime." ) state_keys_to_clear = state_keys_containing_lambdas + state_keys_to_clear.append("logger_") pickled_state = { key: (None if key in state_keys_to_clear else value) for key, value in state.items() From 733470454973de9722e3b0295211a78150b3f6b8 Mon Sep 17 00:00:00 2001 From: MilesCranmer Date: Fri, 13 Dec 2024 15:57:11 -0800 Subject: [PATCH 3/5] ci: make apptainer test use sudo --- .github/workflows/CI_apptainer.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/CI_apptainer.yml b/.github/workflows/CI_apptainer.yml index e128de23..427b1ce1 100644 --- a/.github/workflows/CI_apptainer.yml +++ b/.github/workflows/CI_apptainer.yml @@ -31,7 +31,7 @@ jobs: with: apptainer-version: 1.3.0 - name: Build apptainer - run: apptainer build --notest pysr.sif Apptainer.def + run: sudo apptainer build --notest pysr.sif Apptainer.def - name: Test apptainer run: | TMPDIR=$(mktemp -d) From ddea9a3686bb9f02b2f078a816491d2e0d17b48b Mon Sep 17 00:00:00 2001 From: MilesCranmer Date: Fri, 13 Dec 2024 16:34:56 -0800 Subject: [PATCH 4/5] fix: only require sympy mapping if `supports_sympy` --- pysr/sr.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pysr/sr.py b/pysr/sr.py index 5a330e1b..00209378 100644 --- a/pysr/sr.py +++ b/pysr/sr.py @@ -111,6 +111,7 @@ def _maybe_create_inline_operators( binary_operators: list[str], unary_operators: list[str], extra_sympy_mappings: dict[str, Callable] | None, + expression_spec: AbstractExpressionSpec, ) -> tuple[list[str], list[str]]: binary_operators = binary_operators.copy() unary_operators = unary_operators.copy() @@ -132,9 +133,11 @@ def _maybe_create_inline_operators( "Only alphanumeric characters, numbers, " "and underscores are allowed." ) - if (extra_sympy_mappings is None) or ( - function_name not in extra_sympy_mappings - ): + missing_sympy_mapping = ( + extra_sympy_mappings is None + or function_name not in extra_sympy_mappings + ) + if missing_sympy_mapping and expression_spec.supports_sympy: raise ValueError( f"Custom function {function_name} is not defined in `extra_sympy_mappings`. " "You can define it with, " @@ -1846,6 +1849,7 @@ def _run( binary_operators=binary_operators, unary_operators=unary_operators, extra_sympy_mappings=self.extra_sympy_mappings, + expression_spec=self.expression_spec_, ) if constraints is not None: _constraints = _process_constraints( From 13f1cb058ccad7b5e1196fd90db1cc5192e9e74b Mon Sep 17 00:00:00 2001 From: MilesCranmer Date: Fri, 13 Dec 2024 17:02:35 -0800 Subject: [PATCH 5/5] ci: ensure we test apptainer with sudo --- .github/workflows/CI_apptainer.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/CI_apptainer.yml b/.github/workflows/CI_apptainer.yml index 427b1ce1..76583b65 100644 --- a/.github/workflows/CI_apptainer.yml +++ b/.github/workflows/CI_apptainer.yml @@ -37,4 +37,4 @@ jobs: TMPDIR=$(mktemp -d) cp pysr.sif $TMPDIR cd $TMPDIR - apptainer test ./pysr.sif + sudo apptainer test ./pysr.sif