Spaces:
Sleeping
Sleeping
MilesCranmer
commited on
Commit
•
76d478e
1
Parent(s):
bb4a4eb
Enable local .jl; update to SymbolicRegression.jl 0.3.0
Browse files- pysr/sr.py +44 -11
pysr/sr.py
CHANGED
@@ -103,6 +103,7 @@ def pysr(X=None, y=None, weights=None,
|
|
103 |
limitPowComplexity=False, #deprecated
|
104 |
threads=None, #deprecated
|
105 |
julia_optimization=3,
|
|
|
106 |
):
|
107 |
"""Run symbolic regression to fit f(X[i, :]) ~ y[i] for all i.
|
108 |
Note: most default parameters have been tuned over several example
|
@@ -257,7 +258,8 @@ def pysr(X=None, y=None, weights=None,
|
|
257 |
weightRandomize=weightRandomize,
|
258 |
weightSimplify=weightSimplify,
|
259 |
constraints=constraints,
|
260 |
-
extra_sympy_mappings=extra_sympy_mappings
|
|
|
261 |
|
262 |
kwargs = {**_set_paths(tempdir), **kwargs}
|
263 |
|
@@ -290,10 +292,9 @@ def _set_globals(X, equation_file, extra_sympy_mappings, variable_names, **kwarg
|
|
290 |
global_extra_sympy_mappings = extra_sympy_mappings
|
291 |
|
292 |
|
293 |
-
def _final_pysr_process(julia_optimization,
|
294 |
command = [
|
295 |
f'julia', f'-O{julia_optimization:d}',
|
296 |
-
f'-p', f'{procs}',
|
297 |
str(runfile_filename),
|
298 |
]
|
299 |
if timeout is not None:
|
@@ -315,17 +316,33 @@ def _final_pysr_process(julia_optimization, procs, runfile_filename, timeout, **
|
|
315 |
|
316 |
def _create_julia_files(dataset_filename, def_datasets, hyperparam_filename, def_hyperparams,
|
317 |
fractionReplaced, ncyclesperiteration, niterations, npop,
|
318 |
-
runfile_filename, topn, verbosity,
|
|
|
319 |
with open(hyperparam_filename, 'w') as f:
|
320 |
print(def_hyperparams, file=f)
|
321 |
with open(dataset_filename, 'w') as f:
|
322 |
print(def_datasets, file=f)
|
323 |
with open(runfile_filename, 'w') as f:
|
324 |
-
print(f'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
325 |
print(f'include("{_escape_filename(hyperparam_filename)}")', file=f)
|
326 |
print(f'include("{_escape_filename(dataset_filename)}")', file=f)
|
327 |
-
|
328 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
329 |
|
330 |
|
331 |
def _make_datasets_julia_str(X, X_filename, weights, weights_filename, y, y_filename, **kwargs):
|
@@ -335,7 +352,7 @@ def _make_datasets_julia_str(X, X_filename, weights, weights_filename, y, y_file
|
|
335 |
if weights is not None:
|
336 |
np.savetxt(weights_filename, weights.reshape(-1, 1), delimiter=',')
|
337 |
def_datasets += f"""
|
338 |
-
X = readdlm("{_escape_filename(X_filename)}", ',', Float32, '\\n')
|
339 |
y = readdlm("{_escape_filename(y_filename)}", ',', Float32, '\\n')[:, 1]"""
|
340 |
if weights is not None:
|
341 |
def_datasets += f"""
|
@@ -351,7 +368,25 @@ def _make_hyperparams_julia_str(X, alpha, annealing, batchSize, batching, binary
|
|
351 |
ncyclesperiteration, fractionReplaced, topn, verbosity,
|
352 |
weightDeleteNode, weightDoNothing, weightInsertNode, weightMutateConstant,
|
353 |
weightMutateOperator, weightRandomize, weightSimplify, weights, **kwargs):
|
354 |
-
def_hyperparams += f"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
355 |
unary_operators={'(' + ', '.join(unary_operators) + ')'},
|
356 |
{constraints_str}
|
357 |
parsimony={parsimony:f}f0,
|
@@ -368,10 +403,8 @@ npopulations={populations:d},
|
|
368 |
nrestarts={nrestarts:d},
|
369 |
perturbationFactor={perturbationFactor:f}f0,
|
370 |
annealing={"true" if annealing else "false"},
|
371 |
-
weighted={"true" if weights is not None else "false"},
|
372 |
batching={"true" if batching else "false"},
|
373 |
batchSize={min([batchSize, len(X)]) if batching else len(X):d},
|
374 |
-
useVarMap={"true" if use_custom_variable_names else "false"},
|
375 |
mutationWeights=[
|
376 |
{weightMutateConstant:f},
|
377 |
{weightMutateOperator:f},
|
|
|
103 |
limitPowComplexity=False, #deprecated
|
104 |
threads=None, #deprecated
|
105 |
julia_optimization=3,
|
106 |
+
local_install=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
|
|
|
258 |
weightRandomize=weightRandomize,
|
259 |
weightSimplify=weightSimplify,
|
260 |
constraints=constraints,
|
261 |
+
extra_sympy_mappings=extra_sympy_mappings,
|
262 |
+
local_install=local_install)
|
263 |
|
264 |
kwargs = {**_set_paths(tempdir), **kwargs}
|
265 |
|
|
|
292 |
global_extra_sympy_mappings = extra_sympy_mappings
|
293 |
|
294 |
|
295 |
+
def _final_pysr_process(julia_optimization, runfile_filename, timeout, **kwargs):
|
296 |
command = [
|
297 |
f'julia', f'-O{julia_optimization:d}',
|
|
|
298 |
str(runfile_filename),
|
299 |
]
|
300 |
if timeout is not None:
|
|
|
316 |
|
317 |
def _create_julia_files(dataset_filename, def_datasets, hyperparam_filename, def_hyperparams,
|
318 |
fractionReplaced, ncyclesperiteration, niterations, npop,
|
319 |
+
runfile_filename, topn, verbosity, local_install, procs, weights,
|
320 |
+
X, variable_names, **kwargs):
|
321 |
with open(hyperparam_filename, 'w') as f:
|
322 |
print(def_hyperparams, file=f)
|
323 |
with open(dataset_filename, 'w') as f:
|
324 |
print(def_datasets, file=f)
|
325 |
with open(runfile_filename, 'w') as f:
|
326 |
+
print(f'using Distributed', file=f)
|
327 |
+
print(f'procs = addprocs({procs})', file=f)
|
328 |
+
if local_install is None:
|
329 |
+
print(f'@everywhere using SymbolicRegression', file=f)
|
330 |
+
else:
|
331 |
+
local_install = Path(local_install) / "src" / "SymbolicRegression.jl"
|
332 |
+
print(f'@everywhere include("{_escape_filename(local_install)}")', file=f)
|
333 |
+
print(f'@everywhere using .SymbolicRegression', file=f)
|
334 |
print(f'include("{_escape_filename(hyperparam_filename)}")', file=f)
|
335 |
print(f'include("{_escape_filename(dataset_filename)}")', file=f)
|
336 |
+
if len(variable_names) == 0:
|
337 |
+
varMap = "[" + ",".join([f'"x{i}"' for i in range(X.shape[1])]) + "]"
|
338 |
+
else:
|
339 |
+
varMap = "[" + ",".join(variable_names) + "]"
|
340 |
+
|
341 |
+
if weights is not None:
|
342 |
+
print(f'EquationSearch(X, y, weights=weights, niterations={niterations:d}, varMap={varMap}, options=options)', file=f)
|
343 |
+
else:
|
344 |
+
print(f'EquationSearch(X, y, niterations={niterations:d}, varMap={varMap}, options=options)', file=f)
|
345 |
+
print(f'rmprocs(procs)', file=f)
|
346 |
|
347 |
|
348 |
def _make_datasets_julia_str(X, X_filename, weights, weights_filename, y, y_filename, **kwargs):
|
|
|
352 |
if weights is not None:
|
353 |
np.savetxt(weights_filename, weights.reshape(-1, 1), delimiter=',')
|
354 |
def_datasets += f"""
|
355 |
+
X = convert(Array, readdlm("{_escape_filename(X_filename)}", ',', Float32, '\\n')')
|
356 |
y = readdlm("{_escape_filename(y_filename)}", ',', Float32, '\\n')[:, 1]"""
|
357 |
if weights is not None:
|
358 |
def_datasets += f"""
|
|
|
368 |
ncyclesperiteration, fractionReplaced, topn, verbosity,
|
369 |
weightDeleteNode, weightDoNothing, weightInsertNode, weightMutateConstant,
|
370 |
weightMutateOperator, weightRandomize, weightSimplify, weights, **kwargs):
|
371 |
+
def_hyperparams += f"""div = SymbolicRegression.div
|
372 |
+
plus=SymbolicRegression.plus
|
373 |
+
sub=SymbolicRegression.sub
|
374 |
+
mult=SymbolicRegression.mult
|
375 |
+
square=SymbolicRegression.square
|
376 |
+
cube=SymbolicRegression.cube
|
377 |
+
pow=SymbolicRegression.pow
|
378 |
+
div=SymbolicRegression.div
|
379 |
+
logm=SymbolicRegression.logm
|
380 |
+
logm2=SymbolicRegression.logm2
|
381 |
+
logm10=SymbolicRegression.logm10
|
382 |
+
sqrtm=SymbolicRegression.sqrtm
|
383 |
+
neg=SymbolicRegression.neg
|
384 |
+
greater=SymbolicRegression.greater
|
385 |
+
relu=SymbolicRegression.relu
|
386 |
+
logical_or=SymbolicRegression.logical_or
|
387 |
+
logical_and=SymbolicRegression.logical_and
|
388 |
+
|
389 |
+
options = SymbolicRegression.Options(binary_operators={'(' + ', '.join(binary_operators) + ')'},
|
390 |
unary_operators={'(' + ', '.join(unary_operators) + ')'},
|
391 |
{constraints_str}
|
392 |
parsimony={parsimony:f}f0,
|
|
|
403 |
nrestarts={nrestarts:d},
|
404 |
perturbationFactor={perturbationFactor:f}f0,
|
405 |
annealing={"true" if annealing else "false"},
|
|
|
406 |
batching={"true" if batching else "false"},
|
407 |
batchSize={min([batchSize, len(X)]) if batching else len(X):d},
|
|
|
408 |
mutationWeights=[
|
409 |
{weightMutateConstant:f},
|
410 |
{weightMutateOperator:f},
|