Spaces:
Running
Running
Johann Brehmer
commited on
Commit
•
bdd2ad4
1
Parent(s):
b41237e
System-independent temporary files with tempfile and pathlib
Browse files- pysr/sr.py +56 -45
pysr/sr.py
CHANGED
@@ -7,6 +7,8 @@ import pandas as pd
|
|
7 |
import sympy
|
8 |
from sympy import sympify, Symbol, lambdify
|
9 |
import subprocess
|
|
|
|
|
10 |
|
11 |
global_equation_file = 'hall_of_fame.csv'
|
12 |
global_n_features = None
|
@@ -393,51 +395,60 @@ const weights = convert(Array{Float32, 1}, """f"{weight_str})"
|
|
393 |
def_hyperparams += f"""
|
394 |
const varMap = {'["' + '", "'.join(variable_names) + '"]'}"""
|
395 |
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
print(
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
f'
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
441 |
|
442 |
return get_hof()
|
443 |
|
|
|
7 |
import sympy
|
8 |
from sympy import sympify, Symbol, lambdify
|
9 |
import subprocess
|
10 |
+
import tempfile
|
11 |
+
from pathlib import Path
|
12 |
|
13 |
global_equation_file = 'hall_of_fame.csv'
|
14 |
global_n_features = None
|
|
|
395 |
def_hyperparams += f"""
|
396 |
const varMap = {'["' + '", "'.join(variable_names) + '"]'}"""
|
397 |
|
398 |
+
# Get temporary directory in a system-independent way
|
399 |
+
with tempfile.TemporaryDirectory() as tmpdirname:
|
400 |
+
tmpdir = Path(tmpdirname)
|
401 |
+
hyperparam_filename = str(tmpdir / f'.hyperparams_{rand_string}.jl')
|
402 |
+
dataset_filename = str(tmpdir / f'.dataset_{rand_string}.jl')
|
403 |
+
runfile_filename = str(tmpdir / f'.runfile_{rand_string}.jl')
|
404 |
+
|
405 |
+
print(tmpdir)
|
406 |
+
|
407 |
+
with open(hyperparam_filename, 'w') as f:
|
408 |
+
print(def_hyperparams, file=f)
|
409 |
+
|
410 |
+
with open(dataset_filename, 'w') as f:
|
411 |
+
print(def_datasets, file=f)
|
412 |
+
|
413 |
+
with open(tmpdir / f'.runfile_{rand_string}.jl', 'w') as f:
|
414 |
+
print(f'@everywhere include("{hyperparam_filename}")', file=f)
|
415 |
+
print(f'@everywhere include("{dataset_filename}")', file=f)
|
416 |
+
print(f'@everywhere include("{pkg_directory}/sr.jl")', file=f)
|
417 |
+
print(f'fullRun({niterations:d}, npop={npop:d}, ncyclesperiteration={ncyclesperiteration:d}, fractionReplaced={fractionReplaced:f}f0, verbosity=round(Int32, {verbosity:f}), topn={topn:d})', file=f)
|
418 |
+
print(f'rmprocs(nprocs)', file=f)
|
419 |
+
|
420 |
+
|
421 |
+
command = [
|
422 |
+
f'julia', f'-O{julia_optimization:d}',
|
423 |
+
f'-p', f'{procs}',
|
424 |
+
runfile_filename,
|
425 |
+
]
|
426 |
+
if timeout is not None:
|
427 |
+
command = [f'timeout', f'{timeout}'] + command
|
428 |
+
|
429 |
+
global global_n_features
|
430 |
+
global global_equation_file
|
431 |
+
global global_variable_names
|
432 |
+
global global_extra_sympy_mappings
|
433 |
+
|
434 |
+
global_n_features = X.shape[1]
|
435 |
+
global_equation_file = equation_file
|
436 |
+
global_variable_names = variable_names
|
437 |
+
global_extra_sympy_mappings = extra_sympy_mappings
|
438 |
+
|
439 |
+
print("Running on", ' '.join(command))
|
440 |
+
process = subprocess.Popen(command, stdout=subprocess.PIPE, bufsize=1)
|
441 |
+
try:
|
442 |
+
while True:
|
443 |
+
line = process.stdout.readline()
|
444 |
+
if not line: break
|
445 |
+
print(line.decode('utf-8').replace('\n', ''))
|
446 |
+
|
447 |
+
process.stdout.close()
|
448 |
+
process.wait()
|
449 |
+
except KeyboardInterrupt:
|
450 |
+
print("Killing process... will return when done.")
|
451 |
+
process.kill()
|
452 |
|
453 |
return get_hof()
|
454 |
|