Spaces:
Running
Running
tttc3
commited on
Commit
•
6baa534
1
Parent(s):
5f19660
Moved julia helper functions out of sr.py
Browse files- pysr/__init__.py +1 -1
- pysr/julia_helpers.py +123 -0
- pysr/sr.py +11 -54
pysr/__init__.py
CHANGED
@@ -6,8 +6,8 @@ from .sr import (
|
|
6 |
best_tex,
|
7 |
best_callable,
|
8 |
best_row,
|
9 |
-
install,
|
10 |
)
|
|
|
11 |
from .feynman_problems import Problem, FeynmanProblem
|
12 |
from .export_jax import sympy2jax
|
13 |
from .export_torch import sympy2torch
|
|
|
6 |
best_tex,
|
7 |
best_callable,
|
8 |
best_row,
|
|
|
9 |
)
|
10 |
+
from .julia_helpers import install
|
11 |
from .feynman_problems import Problem, FeynmanProblem
|
12 |
from .export_jax import sympy2jax
|
13 |
from .export_torch import sympy2torch
|
pysr/julia_helpers.py
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import warnings
|
2 |
+
from pathlib import Path
|
3 |
+
import os
|
4 |
+
|
5 |
+
from .version import __version__, __symbolic_regression_jl_version__
|
6 |
+
|
7 |
+
|
8 |
+
def install(julia_project=None, quiet=False): # pragma: no cover
|
9 |
+
"""Install PyCall.jl and all required dependencies for SymbolicRegression.jl.
|
10 |
+
|
11 |
+
Also updates the local Julia registry."""
|
12 |
+
import julia
|
13 |
+
|
14 |
+
julia.install(quiet=quiet)
|
15 |
+
|
16 |
+
julia_project, is_shared = _get_julia_project(julia_project)
|
17 |
+
|
18 |
+
Main = init_julia()
|
19 |
+
Main.eval("using Pkg")
|
20 |
+
|
21 |
+
io = "devnull" if quiet else "stderr"
|
22 |
+
io_arg = f"io={io}" if is_julia_version_greater_eq(Main, "1.6") else ""
|
23 |
+
|
24 |
+
# Can't pass IO to Julia call as it evaluates to PyObject, so just directly
|
25 |
+
# use Main.eval:
|
26 |
+
Main.eval(
|
27 |
+
f'Pkg.activate("{_escape_filename(julia_project)}", shared = Bool({int(is_shared)}), {io_arg})'
|
28 |
+
)
|
29 |
+
if is_shared:
|
30 |
+
# Install SymbolicRegression.jl:
|
31 |
+
_add_sr_to_julia_project(Main, io_arg)
|
32 |
+
|
33 |
+
Main.eval(f"Pkg.instantiate({io_arg})")
|
34 |
+
Main.eval(f"Pkg.precompile({io_arg})")
|
35 |
+
if not quiet:
|
36 |
+
warnings.warn(
|
37 |
+
"It is recommended to restart Python after installing PySR's dependencies,"
|
38 |
+
" so that the Julia environment is properly initialized."
|
39 |
+
)
|
40 |
+
|
41 |
+
|
42 |
+
def import_error_string(julia_project=None):
|
43 |
+
s = f"""
|
44 |
+
Required dependencies are not installed or built. Run the following code in the Python REPL:
|
45 |
+
|
46 |
+
>>> import pysr
|
47 |
+
>>> pysr.install()
|
48 |
+
"""
|
49 |
+
|
50 |
+
if julia_project is not None:
|
51 |
+
s += f"""
|
52 |
+
Tried to activate project {julia_project} but failed."""
|
53 |
+
|
54 |
+
return s
|
55 |
+
|
56 |
+
|
57 |
+
def _get_julia_project(julia_project):
|
58 |
+
if julia_project is None:
|
59 |
+
is_shared = True
|
60 |
+
julia_project = f"pysr-{__version__}"
|
61 |
+
else:
|
62 |
+
is_shared = False
|
63 |
+
julia_project = Path(julia_project)
|
64 |
+
return julia_project, is_shared
|
65 |
+
|
66 |
+
|
67 |
+
def is_julia_version_greater_eq(Main, version="1.6"):
|
68 |
+
"""Check if Julia version is greater than specified version."""
|
69 |
+
return Main.eval(f'VERSION >= v"{version}"')
|
70 |
+
|
71 |
+
|
72 |
+
def init_julia():
|
73 |
+
"""Initialize julia binary, turning off compiled modules if needed."""
|
74 |
+
from julia.core import JuliaInfo, UnsupportedPythonError
|
75 |
+
|
76 |
+
try:
|
77 |
+
info = JuliaInfo.load(julia="julia")
|
78 |
+
except FileNotFoundError:
|
79 |
+
env_path = os.environ["PATH"]
|
80 |
+
raise FileNotFoundError(
|
81 |
+
f"Julia is not installed in your PATH. Please install Julia and add it to your PATH.\n\nCurrent PATH: {env_path}",
|
82 |
+
)
|
83 |
+
|
84 |
+
if not info.is_pycall_built():
|
85 |
+
raise ImportError(import_error_string())
|
86 |
+
|
87 |
+
Main = None
|
88 |
+
try:
|
89 |
+
from julia import Main as _Main
|
90 |
+
|
91 |
+
Main = _Main
|
92 |
+
except UnsupportedPythonError:
|
93 |
+
# Static python binary, so we turn off pre-compiled modules.
|
94 |
+
from julia.core import Julia
|
95 |
+
|
96 |
+
jl = Julia(compiled_modules=False)
|
97 |
+
from julia import Main as _Main
|
98 |
+
|
99 |
+
Main = _Main
|
100 |
+
|
101 |
+
return Main
|
102 |
+
|
103 |
+
|
104 |
+
def _add_sr_to_julia_project(Main, io_arg):
|
105 |
+
Main.sr_spec = Main.PackageSpec(
|
106 |
+
name="SymbolicRegression",
|
107 |
+
url="https://github.com/MilesCranmer/SymbolicRegression.jl",
|
108 |
+
rev="v" + __symbolic_regression_jl_version__,
|
109 |
+
)
|
110 |
+
Main.eval(f"Pkg.add(sr_spec, {io_arg})")
|
111 |
+
Main.clustermanagers_spec = Main.PackageSpec(
|
112 |
+
name="ClusterManagers",
|
113 |
+
url="https://github.com/JuliaParallel/ClusterManagers.jl",
|
114 |
+
rev="14e7302f068794099344d5d93f71979aaf4fbeb3",
|
115 |
+
)
|
116 |
+
Main.eval(f"Pkg.add(clustermanagers_spec, {io_arg})")
|
117 |
+
|
118 |
+
|
119 |
+
def _escape_filename(filename):
|
120 |
+
"""Turns a file into a string representation with correctly escaped backslashes"""
|
121 |
+
str_repr = str(filename)
|
122 |
+
str_repr = str_repr.replace("\\", "\\\\")
|
123 |
+
return str_repr
|
pysr/sr.py
CHANGED
@@ -11,63 +11,20 @@ from pathlib import Path
|
|
11 |
from datetime import datetime
|
12 |
import warnings
|
13 |
from multiprocessing import cpu_count
|
14 |
-
from sklearn.base import BaseEstimator, RegressorMixin
|
15 |
-
from
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
from .deprecated import make_deprecated_kwargs_for_pysr_regressor
|
20 |
|
21 |
|
22 |
-
def install(julia_project=None, quiet=False): # pragma: no cover
|
23 |
-
"""Install PyCall.jl and all required dependencies for SymbolicRegression.jl.
|
24 |
-
|
25 |
-
Also updates the local Julia registry."""
|
26 |
-
import julia
|
27 |
-
|
28 |
-
julia.install(quiet=quiet)
|
29 |
-
|
30 |
-
julia_project, is_shared = _get_julia_project(julia_project)
|
31 |
-
|
32 |
-
Main = init_julia()
|
33 |
-
Main.eval("using Pkg")
|
34 |
-
|
35 |
-
io = "devnull" if quiet else "stderr"
|
36 |
-
io_arg = f"io={io}" if is_julia_version_greater_eq(Main, "1.6") else ""
|
37 |
-
|
38 |
-
# Can't pass IO to Julia call as it evaluates to PyObject, so just directly
|
39 |
-
# use Main.eval:
|
40 |
-
Main.eval(
|
41 |
-
f'Pkg.activate("{_escape_filename(julia_project)}", shared = Bool({int(is_shared)}), {io_arg})'
|
42 |
-
)
|
43 |
-
if is_shared:
|
44 |
-
# Install SymbolicRegression.jl:
|
45 |
-
_add_sr_to_julia_project(Main, io_arg)
|
46 |
-
|
47 |
-
Main.eval(f"Pkg.instantiate({io_arg})")
|
48 |
-
Main.eval(f"Pkg.precompile({io_arg})")
|
49 |
-
if not quiet:
|
50 |
-
warnings.warn(
|
51 |
-
"It is recommended to restart Python after installing PySR's dependencies,"
|
52 |
-
" so that the Julia environment is properly initialized."
|
53 |
-
)
|
54 |
-
|
55 |
-
|
56 |
-
def import_error_string(julia_project=None):
|
57 |
-
s = f"""
|
58 |
-
Required dependencies are not installed or built. Run the following code in the Python REPL:
|
59 |
-
|
60 |
-
>>> import pysr
|
61 |
-
>>> pysr.install()
|
62 |
-
"""
|
63 |
-
|
64 |
-
if julia_project is not None:
|
65 |
-
s += f"""
|
66 |
-
Tried to activate project {julia_project} but failed."""
|
67 |
-
|
68 |
-
return s
|
69 |
-
|
70 |
-
|
71 |
Main = None
|
72 |
|
73 |
already_ran = False
|
|
|
11 |
from datetime import datetime
|
12 |
import warnings
|
13 |
from multiprocessing import cpu_count
|
14 |
+
from sklearn.base import BaseEstimator, RegressorMixin, MultiOutputMixin
|
15 |
+
from sklearn.utils.validation import _check_feature_names_in, check_is_fitted
|
16 |
+
|
17 |
+
from .julia_helpers import (
|
18 |
+
init_julia,
|
19 |
+
_get_julia_project,
|
20 |
+
is_julia_version_greater_eq,
|
21 |
+
_escape_filename,
|
22 |
+
_add_sr_to_julia_project,
|
23 |
+
import_error_string,
|
24 |
+
)
|
25 |
from .deprecated import make_deprecated_kwargs_for_pysr_regressor
|
26 |
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
Main = None
|
29 |
|
30 |
already_ran = False
|