MilesCranmer commited on
Commit
9aa85e5
2 Parent(s): 42bb187 af69b62

Merge pull request #107 from MilesCranmer/github-as-a-registry

Browse files
Files changed (2) hide show
  1. pysr/sr.py +28 -39
  2. pysr/version.py +1 -0
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 = _get_julia_project(julia_project)
30
 
31
  Main = init_julia()
32
  Main.eval("using Pkg")
@@ -36,16 +38,13 @@ 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
- try:
41
- Main.eval(f"Pkg.update({io_arg})")
42
- except RuntimeError as e:
43
- raise ModuleNotFoundError(
44
- "Could not update Julia project. "
45
- "It is possible that your Julia registry is out-of-date. "
46
- "To switch to an always-updated registry, "
47
- "see the solution in https://github.com/MilesCranmer/PySR/issues/27."
48
- ) from e
49
  Main.eval(f"Pkg.instantiate({io_arg})")
50
  Main.eval(f"Pkg.precompile({io_arg})")
51
  if not quiet:
@@ -282,14 +281,12 @@ class CallableEquation:
282
 
283
  def _get_julia_project(julia_project):
284
  if julia_project is None:
285
- # Create temp directory:
286
- tmp_dir = tempfile.mkdtemp()
287
- tmp_dir = Path(tmp_dir)
288
- # Create Project.toml in temp dir:
289
- _write_project_file(tmp_dir)
290
- return tmp_dir
291
  else:
292
- return Path(julia_project)
 
 
293
 
294
 
295
  def silence_julia_warning():
@@ -337,25 +334,13 @@ To silence this warning, you can run pysr.silence_julia_warning() after importin
337
  return Main
338
 
339
 
340
- def _write_project_file(tmp_dir):
341
- """This writes a Julia Project.toml to a temporary directory
342
-
343
- The reason we need this is because sometimes Python will compile a project to binary,
344
- and then Julia can't read the Project.toml file. It is more reliable to have Python
345
- simply create the Project.toml from scratch.
346
- """
347
-
348
- project_toml = """
349
- [deps]
350
- SymbolicRegression = "8254be44-1295-4e6a-a16d-46603ac705cb"
351
-
352
- [compat]
353
- SymbolicRegression = "0.7.7, 0.7.8"
354
- julia = "1.5"
355
- """
356
-
357
- project_toml_path = tmp_dir / "Project.toml"
358
- project_toml_path.write_text(project_toml)
359
 
360
 
361
  class PySRRegressor(BaseEstimator, RegressorMixin):
@@ -1025,7 +1010,7 @@ class PySRRegressor(BaseEstimator, RegressorMixin):
1025
  else:
1026
  X, y = _denoise(X, y, Xresampled=Xresampled)
1027
 
1028
- self.julia_project = _get_julia_project(self.julia_project)
1029
 
1030
  tmpdir = Path(tempfile.mkdtemp(dir=self.params["tempdir"]))
1031
 
@@ -1059,10 +1044,14 @@ class PySRRegressor(BaseEstimator, RegressorMixin):
1059
  io_arg = f"io={io}" if is_julia_version_greater_eq(Main, "1.6") else ""
1060
 
1061
  Main.eval(
1062
- f'Pkg.activate("{_escape_filename(self.julia_project)}", {io_arg})'
1063
  )
1064
  from julia.api import JuliaError
1065
 
 
 
 
 
1066
  try:
1067
  if update:
1068
  Main.eval(f"Pkg.resolve({io_arg})")
 
15
  from collections import OrderedDict
16
  from hashlib import sha256
17
 
18
+ from .version import __version__, __symbolic_regression_jl_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
+
 
 
 
48
  Main.eval(f"Pkg.instantiate({io_arg})")
49
  Main.eval(f"Pkg.precompile({io_arg})")
50
  if not quiet:
 
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():
 
334
  return Main
335
 
336
 
337
+ def _add_sr_to_julia_project(Main, io_arg):
338
+ Main.spec = Main.PackageSpec(
339
+ name="SymbolicRegression",
340
+ url="https://github.com/MilesCranmer/SymbolicRegression.jl",
341
+ rev="v" + __symbolic_regression_jl_version__,
342
+ )
343
+ Main.eval(f"Pkg.add(spec, {io_arg})")
 
 
 
 
 
 
 
 
 
 
 
 
344
 
345
 
346
  class PySRRegressor(BaseEstimator, RegressorMixin):
 
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
+
1055
  try:
1056
  if update:
1057
  Main.eval(f"Pkg.resolve({io_arg})")
pysr/version.py CHANGED
@@ -1 +1,2 @@
1
  __version__ = "0.7.4"
 
 
1
  __version__ = "0.7.4"
2
+ __symbolic_regression_jl_version__ = "0.7.8"