MilesCranmer commited on
Commit
744d6e2
1 Parent(s): 25e2bc3

Use shared env instead of temp directory

Browse files
Files changed (1) hide show
  1. pysr/sr.py +16 -16
pysr/sr.py CHANGED
@@ -15,6 +15,8 @@ from sklearn.base import BaseEstimator, RegressorMixin
15
  from collections import OrderedDict
16
  from hashlib import sha256
17
 
 
 
18
  is_julia_warning_silenced = False
19
 
20
 
@@ -26,7 +28,7 @@ def install(julia_project=None, quiet=False): # pragma: no cover
26
 
27
  julia.install(quiet=quiet)
28
 
29
- julia_project, is_fresh_env = _get_julia_project(julia_project)
30
 
31
  Main = init_julia()
32
  Main.eval("using Pkg")
@@ -36,8 +38,10 @@ def install(julia_project=None, quiet=False): # pragma: no cover
36
 
37
  # Can't pass IO to Julia call as it evaluates to PyObject, so just directly
38
  # use Main.eval:
39
- Main.eval(f'Pkg.activate("{_escape_filename(julia_project)}", {io_arg})')
40
- if is_fresh_env:
 
 
41
  # Install SymbolicRegression.jl:
42
  _add_sr_to_julia_project(Main, io_arg)
43
 
@@ -73,8 +77,8 @@ sympy_mappings = {
73
  "div": lambda x, y: x / y,
74
  "mult": lambda x, y: x * y,
75
  "sqrt_abs": lambda x: sympy.sqrt(abs(x)),
76
- "square": lambda x: x**2,
77
- "cube": lambda x: x**3,
78
  "plus": lambda x, y: x + y,
79
  "sub": lambda x, y: x - y,
80
  "neg": lambda x: -x,
@@ -277,16 +281,12 @@ class CallableEquation:
277
 
278
  def _get_julia_project(julia_project):
279
  if julia_project is None:
280
- is_fresh_env = True
281
- # Create temp directory:
282
- tmp_dir = tempfile.mkdtemp()
283
- tmp_dir = Path(tmp_dir)
284
- # Will create Project.toml in temp dir:
285
- julia_project = tmp_dir
286
  else:
287
- is_fresh_env = False
288
  julia_project = Path(julia_project)
289
- return julia_project, is_fresh_env
290
 
291
 
292
  def silence_julia_warning():
@@ -1010,7 +1010,7 @@ class PySRRegressor(BaseEstimator, RegressorMixin):
1010
  else:
1011
  X, y = _denoise(X, y, Xresampled=Xresampled)
1012
 
1013
- self.julia_project, is_fresh_env = _get_julia_project(self.julia_project)
1014
 
1015
  tmpdir = Path(tempfile.mkdtemp(dir=self.params["tempdir"]))
1016
 
@@ -1044,11 +1044,11 @@ class PySRRegressor(BaseEstimator, RegressorMixin):
1044
  io_arg = f"io={io}" if is_julia_version_greater_eq(Main, "1.6") else ""
1045
 
1046
  Main.eval(
1047
- f'Pkg.activate("{_escape_filename(self.julia_project)}", {io_arg})'
1048
  )
1049
  from julia.api import JuliaError
1050
 
1051
- if is_fresh_env:
1052
  # Install SymbolicRegression.jl:
1053
  _add_sr_to_julia_project(Main, io_arg)
1054
 
 
15
  from collections import OrderedDict
16
  from hashlib import sha256
17
 
18
+ from .version import __version__
19
+
20
  is_julia_warning_silenced = False
21
 
22
 
 
28
 
29
  julia.install(quiet=quiet)
30
 
31
+ julia_project, is_shared = _get_julia_project(julia_project)
32
 
33
  Main = init_julia()
34
  Main.eval("using Pkg")
 
38
 
39
  # Can't pass IO to Julia call as it evaluates to PyObject, so just directly
40
  # use Main.eval:
41
+ Main.eval(
42
+ f'Pkg.activate("{_escape_filename(julia_project)}", shared = Bool({int(is_shared)}), {io_arg})'
43
+ )
44
+ if is_shared:
45
  # Install SymbolicRegression.jl:
46
  _add_sr_to_julia_project(Main, io_arg)
47
 
 
77
  "div": lambda x, y: x / y,
78
  "mult": lambda x, y: x * y,
79
  "sqrt_abs": lambda x: sympy.sqrt(abs(x)),
80
+ "square": lambda x: x ** 2,
81
+ "cube": lambda x: x ** 3,
82
  "plus": lambda x, y: x + y,
83
  "sub": lambda x, y: x - y,
84
  "neg": lambda x: -x,
 
281
 
282
  def _get_julia_project(julia_project):
283
  if julia_project is None:
284
+ is_shared = True
285
+ julia_project = f"pysr-{__version__}"
 
 
 
 
286
  else:
287
+ is_shared = False
288
  julia_project = Path(julia_project)
289
+ return julia_project, is_shared
290
 
291
 
292
  def silence_julia_warning():
 
1010
  else:
1011
  X, y = _denoise(X, y, Xresampled=Xresampled)
1012
 
1013
+ self.julia_project, is_shared = _get_julia_project(self.julia_project)
1014
 
1015
  tmpdir = Path(tempfile.mkdtemp(dir=self.params["tempdir"]))
1016
 
 
1044
  io_arg = f"io={io}" if is_julia_version_greater_eq(Main, "1.6") else ""
1045
 
1046
  Main.eval(
1047
+ f'Pkg.activate("{_escape_filename(self.julia_project)}", shared = Bool({int(is_shared)}), {io_arg})'
1048
  )
1049
  from julia.api import JuliaError
1050
 
1051
+ if is_shared:
1052
  # Install SymbolicRegression.jl:
1053
  _add_sr_to_julia_project(Main, io_arg)
1054