Spaces:
Running
Running
MilesCranmer
commited on
Commit
•
9c1e6af
1
Parent(s):
0e9470e
Finish integration of SymbolicRegression.jl
Browse files- pysr/sr.py +43 -43
pysr/sr.py
CHANGED
@@ -261,7 +261,7 @@ def pysr(X=None, y=None, weights=None,
|
|
261 |
|
262 |
kwargs = {**_set_paths(tempdir), **kwargs}
|
263 |
|
264 |
-
kwargs['def_hyperparams'] =
|
265 |
|
266 |
_handle_constraints(**kwargs)
|
267 |
|
@@ -321,9 +321,9 @@ def _create_julia_files(dataset_filename, def_datasets, hyperparam_filename, de
|
|
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'@everywhere
|
325 |
-
print(f'
|
326 |
-
print(f'
|
327 |
print(f'RunSR(X, y, {niterations:d}, options)', file=f)
|
328 |
print(f'rmprocs(nprocs)', file=f)
|
329 |
|
@@ -331,48 +331,48 @@ def _create_julia_files(dataset_filename, def_datasets, hyperparam_filename, de
|
|
331 |
def _make_datasets_julia_str(X, X_filename, weights, weights_filename, y, y_filename, **kwargs):
|
332 |
def_datasets = """using DelimitedFiles"""
|
333 |
np.savetxt(X_filename, X, delimiter=',')
|
334 |
-
np.savetxt(y_filename, y, delimiter=',')
|
335 |
if weights is not None:
|
336 |
-
np.savetxt(weights_filename, weights, delimiter=',')
|
337 |
def_datasets += f"""
|
338 |
-
|
339 |
-
|
340 |
if weights is not None:
|
341 |
def_datasets += f"""
|
342 |
-
|
343 |
return def_datasets
|
344 |
|
345 |
def _make_hyperparams_julia_str(X, alpha, annealing, batchSize, batching, binary_operators, constraints_str,
|
346 |
def_hyperparams, equation_file, fast_cycle, fractionReplacedHof, hofMigration,
|
347 |
-
limitPowComplexity, maxdepth, maxsize, migration, nrestarts,
|
348 |
parsimony, perturbationFactor, populations, procs, shouldOptimizeConstants,
|
349 |
-
unary_operators, useFrequency, use_custom_variable_names,
|
|
|
|
|
350 |
weightDeleteNode, weightDoNothing, weightInsertNode, weightMutateConstant,
|
351 |
weightMutateOperator, weightRandomize, weightSimplify, weights, **kwargs):
|
352 |
-
def_hyperparams += f"""{
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
useVarMap = {"true" if use_custom_variable_names else "false"},
|
375 |
-
mutationWeights = [
|
376 |
{weightMutateConstant:f},
|
377 |
{weightMutateOperator:f},
|
378 |
{weightAddNode:f},
|
@@ -382,9 +382,9 @@ mutationWeights = [
|
|
382 |
{weightRandomize:f},
|
383 |
{weightDoNothing:f}
|
384 |
],
|
385 |
-
warmupMaxsize
|
386 |
-
limitPowComplexity
|
387 |
-
useFrequency
|
388 |
npop={npop:d},
|
389 |
ncyclesperiteration={ncyclesperiteration:d},
|
390 |
fractionReplaced={fractionReplaced:f}f0,
|
@@ -400,7 +400,7 @@ verbosity=round(Int32, {verbosity:f})
|
|
400 |
|
401 |
|
402 |
def _make_constraints_str(binary_operators, constraints, unary_operators, **kwargs):
|
403 |
-
constraints_str = "
|
404 |
first = True
|
405 |
for op in unary_operators:
|
406 |
val = constraints[op]
|
@@ -408,8 +408,8 @@ def _make_constraints_str(binary_operators, constraints, unary_operators, **kwar
|
|
408 |
constraints_str += ", "
|
409 |
constraints_str += f"{val:d}"
|
410 |
first = False
|
411 |
-
constraints_str += """]
|
412 |
-
|
413 |
first = True
|
414 |
for op in binary_operators:
|
415 |
tup = constraints[op]
|
@@ -417,7 +417,7 @@ const bin_constraints = ["""
|
|
417 |
constraints_str += ", "
|
418 |
constraints_str += f"({tup[0]:d}, {tup[1]:d})"
|
419 |
first = False
|
420 |
-
constraints_str += "]"
|
421 |
return constraints_str
|
422 |
|
423 |
|
@@ -440,7 +440,7 @@ def _handle_constraints(binary_operators, constraints, unary_operators, **kwargs
|
|
440 |
constraints[op][0], constraints[op][1] = constraints[op][1], constraints[op][0]
|
441 |
|
442 |
|
443 |
-
def
|
444 |
def_hyperparams = ""
|
445 |
for op_list in [binary_operators, unary_operators]:
|
446 |
for i in range(len(op_list)):
|
|
|
261 |
|
262 |
kwargs = {**_set_paths(tempdir), **kwargs}
|
263 |
|
264 |
+
kwargs['def_hyperparams'] = _create_inline_operators(**kwargs)
|
265 |
|
266 |
_handle_constraints(**kwargs)
|
267 |
|
|
|
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'@everywhere using SymbolicRegression', file=f)
|
325 |
+
print(f'include("{_escape_filename(hyperparam_filename)}")', file=f)
|
326 |
+
print(f'include("{_escape_filename(dataset_filename)}")', file=f)
|
327 |
print(f'RunSR(X, y, {niterations:d}, options)', file=f)
|
328 |
print(f'rmprocs(nprocs)', file=f)
|
329 |
|
|
|
331 |
def _make_datasets_julia_str(X, X_filename, weights, weights_filename, y, y_filename, **kwargs):
|
332 |
def_datasets = """using DelimitedFiles"""
|
333 |
np.savetxt(X_filename, X, delimiter=',')
|
334 |
+
np.savetxt(y_filename, y.reshape(-1, 1), delimiter=',')
|
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"""
|
342 |
+
weights = readdlm("{_escape_filename(weights_filename)}", ',', Float32, '\\n')[:, 1]"""
|
343 |
return def_datasets
|
344 |
|
345 |
def _make_hyperparams_julia_str(X, alpha, annealing, batchSize, batching, binary_operators, constraints_str,
|
346 |
def_hyperparams, equation_file, fast_cycle, fractionReplacedHof, hofMigration,
|
347 |
+
limitPowComplexity, maxdepth, maxsize, migration, nrestarts, npop,
|
348 |
parsimony, perturbationFactor, populations, procs, shouldOptimizeConstants,
|
349 |
+
unary_operators, useFrequency, use_custom_variable_names,
|
350 |
+
variable_names, warmupMaxsize, weightAddNode,
|
351 |
+
ncyclesperiteration, fractionReplaced, topn, verbosity,
|
352 |
weightDeleteNode, weightDoNothing, weightInsertNode, weightMutateConstant,
|
353 |
weightMutateOperator, weightRandomize, weightSimplify, weights, **kwargs):
|
354 |
+
def_hyperparams += f"""options = SymbolicRegression.Options(binary_operators={'(' + ', '.join(binary_operators) + ')'},
|
355 |
+
unary_operators={'(' + ', '.join(unary_operators) + ')'},
|
356 |
+
{constraints_str}
|
357 |
+
parsimony={parsimony:f}f0,
|
358 |
+
alpha={alpha:f}f0,
|
359 |
+
maxsize={maxsize:d},
|
360 |
+
maxdepth={maxdepth:d},
|
361 |
+
fast_cycle={'true' if fast_cycle else 'false'},
|
362 |
+
migration={'true' if migration else 'false'},
|
363 |
+
hofMigration={'true' if hofMigration else 'false'},
|
364 |
+
fractionReplacedHof={fractionReplacedHof}f0,
|
365 |
+
shouldOptimizeConstants={'true' if shouldOptimizeConstants else 'false'},
|
366 |
+
hofFile="{equation_file}",
|
367 |
+
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},
|
378 |
{weightAddNode:f},
|
|
|
382 |
{weightRandomize:f},
|
383 |
{weightDoNothing:f}
|
384 |
],
|
385 |
+
warmupMaxsize={warmupMaxsize:d},
|
386 |
+
limitPowComplexity={"true" if limitPowComplexity else "false"},
|
387 |
+
useFrequency={"true" if useFrequency else "false"},
|
388 |
npop={npop:d},
|
389 |
ncyclesperiteration={ncyclesperiteration:d},
|
390 |
fractionReplaced={fractionReplaced:f}f0,
|
|
|
400 |
|
401 |
|
402 |
def _make_constraints_str(binary_operators, constraints, unary_operators, **kwargs):
|
403 |
+
constraints_str = "una_constraints = ["
|
404 |
first = True
|
405 |
for op in unary_operators:
|
406 |
val = constraints[op]
|
|
|
408 |
constraints_str += ", "
|
409 |
constraints_str += f"{val:d}"
|
410 |
first = False
|
411 |
+
constraints_str += """],
|
412 |
+
bin_constraints = ["""
|
413 |
first = True
|
414 |
for op in binary_operators:
|
415 |
tup = constraints[op]
|
|
|
417 |
constraints_str += ", "
|
418 |
constraints_str += f"({tup[0]:d}, {tup[1]:d})"
|
419 |
first = False
|
420 |
+
constraints_str += "],"
|
421 |
return constraints_str
|
422 |
|
423 |
|
|
|
440 |
constraints[op][0], constraints[op][1] = constraints[op][1], constraints[op][0]
|
441 |
|
442 |
|
443 |
+
def _create_inline_operators(binary_operators, unary_operators, **kwargs):
|
444 |
def_hyperparams = ""
|
445 |
for op_list in [binary_operators, unary_operators]:
|
446 |
for i in range(len(op_list)):
|