Spaces:
Running
Running
MilesCranmer
commited on
Commit
•
c0da614
1
Parent(s):
5568ec7
Set up PySR to automatically install packages
Browse files- README.md +5 -3
- pysr/sr.py +26 -2
README.md
CHANGED
@@ -51,14 +51,16 @@ Install Julia - see [downloads](https://julialang.org/downloads/), and
|
|
51 |
then instructions for [mac](https://julialang.org/downloads/platform/#macos)
|
52 |
and [linux](https://julialang.org/downloads/platform/#linux_and_freebsd).
|
53 |
(Don't use the `conda-forge` version; it doesn't seem to work properly.)
|
54 |
-
Then, at the command line, install and precompile the backend
|
55 |
-
and Python frontend with:
|
56 |
|
|
|
|
|
57 |
```bash
|
58 |
-
julia -e 'using Pkg; Pkg.add("SymbolicRegression"); using SymbolicRegression'
|
59 |
pip install pysr
|
60 |
```
|
61 |
|
|
|
|
|
|
|
62 |
# Quickstart
|
63 |
|
64 |
Here is some demo code (also found in `example.py`)
|
|
|
51 |
then instructions for [mac](https://julialang.org/downloads/platform/#macos)
|
52 |
and [linux](https://julialang.org/downloads/platform/#linux_and_freebsd).
|
53 |
(Don't use the `conda-forge` version; it doesn't seem to work properly.)
|
|
|
|
|
54 |
|
55 |
+
|
56 |
+
You can install PySR with:
|
57 |
```bash
|
|
|
58 |
pip install pysr
|
59 |
```
|
60 |
|
61 |
+
The first launch will automatically install the Julia packages
|
62 |
+
required.
|
63 |
+
|
64 |
# Quickstart
|
65 |
|
66 |
Here is some demo code (also found in `example.py`)
|
pysr/sr.py
CHANGED
@@ -104,6 +104,7 @@ def pysr(X=None, y=None, weights=None,
|
|
104 |
threads=None, #deprecated
|
105 |
julia_optimization=3,
|
106 |
julia_project=None,
|
|
|
107 |
):
|
108 |
"""Run symbolic regression to fit f(X[i, :]) ~ y[i] for all i.
|
109 |
Note: most default parameters have been tuned over several example
|
@@ -265,6 +266,11 @@ def pysr(X=None, y=None, weights=None,
|
|
265 |
|
266 |
kwargs = {**_set_paths(tempdir), **kwargs}
|
267 |
|
|
|
|
|
|
|
|
|
|
|
268 |
kwargs['def_hyperparams'] = _create_inline_operators(**kwargs)
|
269 |
|
270 |
_handle_constraints(**kwargs)
|
@@ -301,6 +307,9 @@ def _final_pysr_process(julia_optimization, runfile_filename, timeout, **kwargs)
|
|
301 |
]
|
302 |
if timeout is not None:
|
303 |
command = [f'timeout', f'{timeout}'] + command
|
|
|
|
|
|
|
304 |
print("Running on", ' '.join(command))
|
305 |
process = subprocess.Popen(command, stdout=subprocess.PIPE, bufsize=1)
|
306 |
try:
|
@@ -315,11 +324,10 @@ def _final_pysr_process(julia_optimization, runfile_filename, timeout, **kwargs)
|
|
315 |
print("Killing process... will return when done.")
|
316 |
process.kill()
|
317 |
|
318 |
-
|
319 |
def _create_julia_files(dataset_filename, def_datasets, hyperparam_filename, def_hyperparams,
|
320 |
fractionReplaced, ncyclesperiteration, niterations, npop,
|
321 |
runfile_filename, topn, verbosity, julia_project, procs, weights,
|
322 |
-
X, variable_names, pkg_directory, **kwargs):
|
323 |
with open(hyperparam_filename, 'w') as f:
|
324 |
print(def_hyperparams, file=f)
|
325 |
with open(dataset_filename, 'w') as f:
|
@@ -331,6 +339,10 @@ def _create_julia_files(dataset_filename, def_datasets, hyperparam_filename, de
|
|
331 |
julia_project = Path(julia_project)
|
332 |
print(f'import Pkg', file=f)
|
333 |
print(f'Pkg.activate("{_escape_filename(julia_project)}")', file=f)
|
|
|
|
|
|
|
|
|
334 |
print(f'using SymbolicRegression', file=f)
|
335 |
print(f'include("{_escape_filename(hyperparam_filename)}")', file=f)
|
336 |
print(f'include("{_escape_filename(dataset_filename)}")', file=f)
|
@@ -680,3 +692,15 @@ def _escape_filename(filename):
|
|
680 |
repr = str(filename)
|
681 |
repr = repr.replace('\\', '\\\\')
|
682 |
return repr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
threads=None, #deprecated
|
105 |
julia_optimization=3,
|
106 |
julia_project=None,
|
107 |
+
user_input=True
|
108 |
):
|
109 |
"""Run symbolic regression to fit f(X[i, :]) ~ y[i] for all i.
|
110 |
Note: most default parameters have been tuned over several example
|
|
|
266 |
|
267 |
kwargs = {**_set_paths(tempdir), **kwargs}
|
268 |
|
269 |
+
pkg_directory = kwargs['pkg_directory']
|
270 |
+
if not (pkg_directory / 'Manifest.toml').is_file():
|
271 |
+
kwargs['need_install'] = _yesno("I will install Julia packages using PySR's Project.toml file. OK?")
|
272 |
+
print("OK. I will install.")
|
273 |
+
|
274 |
kwargs['def_hyperparams'] = _create_inline_operators(**kwargs)
|
275 |
|
276 |
_handle_constraints(**kwargs)
|
|
|
307 |
]
|
308 |
if timeout is not None:
|
309 |
command = [f'timeout', f'{timeout}'] + command
|
310 |
+
_cmd_runner(command)
|
311 |
+
|
312 |
+
def _cmd_runner(command):
|
313 |
print("Running on", ' '.join(command))
|
314 |
process = subprocess.Popen(command, stdout=subprocess.PIPE, bufsize=1)
|
315 |
try:
|
|
|
324 |
print("Killing process... will return when done.")
|
325 |
process.kill()
|
326 |
|
|
|
327 |
def _create_julia_files(dataset_filename, def_datasets, hyperparam_filename, def_hyperparams,
|
328 |
fractionReplaced, ncyclesperiteration, niterations, npop,
|
329 |
runfile_filename, topn, verbosity, julia_project, procs, weights,
|
330 |
+
X, variable_names, pkg_directory, need_install, **kwargs):
|
331 |
with open(hyperparam_filename, 'w') as f:
|
332 |
print(def_hyperparams, file=f)
|
333 |
with open(dataset_filename, 'w') as f:
|
|
|
339 |
julia_project = Path(julia_project)
|
340 |
print(f'import Pkg', file=f)
|
341 |
print(f'Pkg.activate("{_escape_filename(julia_project)}")', file=f)
|
342 |
+
if need_install:
|
343 |
+
print(f'Pkg.add("SymbolicRegression")', file=f)
|
344 |
+
print(f'Pkg.instantiate()', file=f)
|
345 |
+
print(f'Pkg.precompile()', file=f)
|
346 |
print(f'using SymbolicRegression', file=f)
|
347 |
print(f'include("{_escape_filename(hyperparam_filename)}")', file=f)
|
348 |
print(f'include("{_escape_filename(dataset_filename)}")', file=f)
|
|
|
692 |
repr = str(filename)
|
693 |
repr = repr.replace('\\', '\\\\')
|
694 |
return repr
|
695 |
+
|
696 |
+
# https://gist.github.com/garrettdreyfus/8153571
|
697 |
+
def _yesno(question):
|
698 |
+
"""Simple Yes/No Function."""
|
699 |
+
prompt = f'{question} (y/n): '
|
700 |
+
ans = input(prompt).strip().lower()
|
701 |
+
if ans not in ['y', 'n']:
|
702 |
+
print(f'{ans} is invalid, please try again...')
|
703 |
+
return _yesno(question)
|
704 |
+
if ans == 'y':
|
705 |
+
return True
|
706 |
+
return False
|