Johann Brehmer commited on
Commit
bdd2ad4
1 Parent(s): b41237e

System-independent temporary files with tempfile and pathlib

Browse files
Files changed (1) hide show
  1. 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
- with open(f'/tmp/.hyperparams_{rand_string}.jl', 'w') as f:
397
- print(def_hyperparams, file=f)
398
-
399
- with open(f'/tmp/.dataset_{rand_string}.jl', 'w') as f:
400
- print(def_datasets, file=f)
401
-
402
- with open(f'/tmp/.runfile_{rand_string}.jl', 'w') as f:
403
- print(f'@everywhere include("/tmp/.hyperparams_{rand_string}.jl")', file=f)
404
- print(f'@everywhere include("/tmp/.dataset_{rand_string}.jl")', file=f)
405
- print(f'@everywhere include("{pkg_directory}/sr.jl")', file=f)
406
- 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)
407
- print(f'rmprocs(nprocs)', file=f)
408
-
409
-
410
- command = [
411
- f'julia', f'-O{julia_optimization:d}',
412
- f'-p', f'{procs}',
413
- f'/tmp/.runfile_{rand_string}.jl',
414
- ]
415
- if timeout is not None:
416
- command = [f'timeout', f'{timeout}'] + command
417
-
418
- global global_n_features
419
- global global_equation_file
420
- global global_variable_names
421
- global global_extra_sympy_mappings
422
-
423
- global_n_features = X.shape[1]
424
- global_equation_file = equation_file
425
- global_variable_names = variable_names
426
- global_extra_sympy_mappings = extra_sympy_mappings
427
-
428
- print("Running on", ' '.join(command))
429
- process = subprocess.Popen(command, stdout=subprocess.PIPE, bufsize=1)
430
- try:
431
- while True:
432
- line = process.stdout.readline()
433
- if not line: break
434
- print(line.decode('utf-8').replace('\n', ''))
435
-
436
- process.stdout.close()
437
- process.wait()
438
- except KeyboardInterrupt:
439
- print("Killing process... will return when done.")
440
- process.kill()
 
 
 
 
 
 
 
 
 
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