Spaces:
Sleeping
Sleeping
MilesCranmer
commited on
Commit
•
76e7a47
1
Parent(s):
4949116
Make internal functions obvious with underscore
Browse files- pysr/sr.py +20 -21
pysr/sr.py
CHANGED
@@ -222,19 +222,16 @@ def pysr(X=None, y=None, weights=None,
|
|
222 |
if X is None:
|
223 |
X, y = using_test_input(X, test, y)
|
224 |
|
225 |
-
def_hyperparams = ""
|
226 |
-
|
227 |
# Add pre-defined functions to Julia
|
228 |
-
def_hyperparams =
|
229 |
-
|
230 |
|
231 |
#arbitrary complexity by default
|
232 |
-
|
233 |
|
234 |
-
constraints_str =
|
235 |
|
236 |
|
237 |
-
def_hyperparams =
|
238 |
constraints_str, def_hyperparams, equation_file, fast_cycle,
|
239 |
fractionReplacedHof, hofMigration, limitPowComplexity, maxdepth,
|
240 |
maxsize, migration, nrestarts, operator_filename, parsimony,
|
@@ -243,16 +240,17 @@ def pysr(X=None, y=None, weights=None,
|
|
243 |
warmupMaxsize, weightAddNode, weightDeleteNode, weightDoNothing,
|
244 |
weightInsertNode, weightMutateConstant, weightMutateOperator,
|
245 |
weightRandomize, weightSimplify, weights)
|
246 |
-
def_auxiliary = make_auxiliary_julia_str(julia_auxiliary_filenames)
|
247 |
|
248 |
-
|
249 |
|
250 |
-
|
|
|
|
|
251 |
ncyclesperiteration, niterations, npop, pkg_filename, runfile_filename, topn, verbosity)
|
252 |
|
253 |
-
|
254 |
|
255 |
-
|
256 |
|
257 |
if delete_tempfiles:
|
258 |
shutil.rmtree(tmpdir)
|
@@ -260,14 +258,14 @@ def pysr(X=None, y=None, weights=None,
|
|
260 |
return get_hof()
|
261 |
|
262 |
|
263 |
-
def
|
264 |
def_auxiliary = '\n'.join([
|
265 |
f"""include("{_escape_filename(aux_fname)}")""" for aux_fname in julia_auxiliary_filenames
|
266 |
])
|
267 |
return def_auxiliary
|
268 |
|
269 |
|
270 |
-
def
|
271 |
global global_n_features
|
272 |
global global_equation_file
|
273 |
global global_variable_names
|
@@ -278,7 +276,7 @@ def set_globals(X, equation_file, extra_sympy_mappings, variable_names):
|
|
278 |
global_extra_sympy_mappings = extra_sympy_mappings
|
279 |
|
280 |
|
281 |
-
def
|
282 |
command = [
|
283 |
f'julia', f'-O{julia_optimization:d}',
|
284 |
f'-p', f'{procs}',
|
@@ -301,7 +299,7 @@ def final_pysr_process(julia_optimization, procs, runfile_filename, timeout):
|
|
301 |
process.kill()
|
302 |
|
303 |
|
304 |
-
def
|
305 |
ncyclesperiteration, niterations, npop, pkg_filename, runfile_filename, topn, verbosity):
|
306 |
with open(hyperparam_filename, 'w') as f:
|
307 |
print(def_hyperparams, file=f)
|
@@ -320,7 +318,7 @@ def create_julia_files(auxiliary_filename, dataset_filename, def_auxiliary, def_
|
|
320 |
print(f'rmprocs(nprocs)', file=f)
|
321 |
|
322 |
|
323 |
-
def
|
324 |
def_datasets = """using DelimitedFiles"""
|
325 |
np.savetxt(X_filename, X, delimiter=',')
|
326 |
np.savetxt(y_filename, y, delimiter=',')
|
@@ -335,7 +333,7 @@ const weights = readdlm("{_escape_filename(weights_filename)}", ',', Float32, '\
|
|
335 |
return def_datasets
|
336 |
|
337 |
|
338 |
-
def
|
339 |
def_hyperparams, equation_file, fast_cycle, fractionReplacedHof, hofMigration,
|
340 |
limitPowComplexity, maxdepth, maxsize, migration, nrestarts, operator_filename,
|
341 |
parsimony, perturbationFactor, populations, procs, shouldOptimizeConstants,
|
@@ -420,7 +418,7 @@ end"""
|
|
420 |
return def_hyperparams
|
421 |
|
422 |
|
423 |
-
def
|
424 |
constraints_str = "const una_constraints = ["
|
425 |
first = True
|
426 |
for op in unary_operators:
|
@@ -442,7 +440,7 @@ const bin_constraints = ["""
|
|
442 |
return constraints_str
|
443 |
|
444 |
|
445 |
-
def
|
446 |
for op in unary_operators:
|
447 |
if op not in constraints:
|
448 |
constraints[op] = -1
|
@@ -461,7 +459,8 @@ def handle_constraints(binary_operators, constraints, unary_operators):
|
|
461 |
constraints[op][0], constraints[op][1] = constraints[op][1], constraints[op][0]
|
462 |
|
463 |
|
464 |
-
def
|
|
|
465 |
for op_list in [binary_operators, unary_operators]:
|
466 |
for i in range(len(op_list)):
|
467 |
op = op_list[i]
|
|
|
222 |
if X is None:
|
223 |
X, y = using_test_input(X, test, y)
|
224 |
|
|
|
|
|
225 |
# Add pre-defined functions to Julia
|
226 |
+
def_hyperparams = _predefined_function_addition(binary_operators, unary_operators)
|
|
|
227 |
|
228 |
#arbitrary complexity by default
|
229 |
+
_handle_constraints(binary_operators, constraints, unary_operators)
|
230 |
|
231 |
+
constraints_str = _make_constraints_str(binary_operators, constraints, unary_operators)
|
232 |
|
233 |
|
234 |
+
def_hyperparams = _make_hyperparams_julia_str(X, alpha, annealing, batchSize, batching, binary_operators,
|
235 |
constraints_str, def_hyperparams, equation_file, fast_cycle,
|
236 |
fractionReplacedHof, hofMigration, limitPowComplexity, maxdepth,
|
237 |
maxsize, migration, nrestarts, operator_filename, parsimony,
|
|
|
240 |
warmupMaxsize, weightAddNode, weightDeleteNode, weightDoNothing,
|
241 |
weightInsertNode, weightMutateConstant, weightMutateOperator,
|
242 |
weightRandomize, weightSimplify, weights)
|
|
|
243 |
|
244 |
+
def_auxiliary = _make_auxiliary_julia_str(julia_auxiliary_filenames)
|
245 |
|
246 |
+
def_datasets = _make_datasets_julia_str(X, X_filename, weights, weights_filename, y, y_filename)
|
247 |
+
|
248 |
+
_create_julia_files(auxiliary_filename, dataset_filename, def_auxiliary, def_datasets, def_hyperparams, fractionReplaced, hyperparam_filename,
|
249 |
ncyclesperiteration, niterations, npop, pkg_filename, runfile_filename, topn, verbosity)
|
250 |
|
251 |
+
_final_pysr_process(julia_optimization, procs, runfile_filename, timeout)
|
252 |
|
253 |
+
_set_globals(X, equation_file, extra_sympy_mappings, variable_names)
|
254 |
|
255 |
if delete_tempfiles:
|
256 |
shutil.rmtree(tmpdir)
|
|
|
258 |
return get_hof()
|
259 |
|
260 |
|
261 |
+
def _make_auxiliary_julia_str(julia_auxiliary_filenames):
|
262 |
def_auxiliary = '\n'.join([
|
263 |
f"""include("{_escape_filename(aux_fname)}")""" for aux_fname in julia_auxiliary_filenames
|
264 |
])
|
265 |
return def_auxiliary
|
266 |
|
267 |
|
268 |
+
def _set_globals(X, equation_file, extra_sympy_mappings, variable_names):
|
269 |
global global_n_features
|
270 |
global global_equation_file
|
271 |
global global_variable_names
|
|
|
276 |
global_extra_sympy_mappings = extra_sympy_mappings
|
277 |
|
278 |
|
279 |
+
def _final_pysr_process(julia_optimization, procs, runfile_filename, timeout):
|
280 |
command = [
|
281 |
f'julia', f'-O{julia_optimization:d}',
|
282 |
f'-p', f'{procs}',
|
|
|
299 |
process.kill()
|
300 |
|
301 |
|
302 |
+
def _create_julia_files(auxiliary_filename, dataset_filename, def_auxiliary, def_datasets, def_hyperparams, fractionReplaced, hyperparam_filename,
|
303 |
ncyclesperiteration, niterations, npop, pkg_filename, runfile_filename, topn, verbosity):
|
304 |
with open(hyperparam_filename, 'w') as f:
|
305 |
print(def_hyperparams, file=f)
|
|
|
318 |
print(f'rmprocs(nprocs)', file=f)
|
319 |
|
320 |
|
321 |
+
def _make_datasets_julia_str(X, X_filename, weights, weights_filename, y, y_filename):
|
322 |
def_datasets = """using DelimitedFiles"""
|
323 |
np.savetxt(X_filename, X, delimiter=',')
|
324 |
np.savetxt(y_filename, y, delimiter=',')
|
|
|
333 |
return def_datasets
|
334 |
|
335 |
|
336 |
+
def _make_hyperparams_julia_str(X, alpha, annealing, batchSize, batching, binary_operators, constraints_str,
|
337 |
def_hyperparams, equation_file, fast_cycle, fractionReplacedHof, hofMigration,
|
338 |
limitPowComplexity, maxdepth, maxsize, migration, nrestarts, operator_filename,
|
339 |
parsimony, perturbationFactor, populations, procs, shouldOptimizeConstants,
|
|
|
418 |
return def_hyperparams
|
419 |
|
420 |
|
421 |
+
def _make_constraints_str(binary_operators, constraints, unary_operators):
|
422 |
constraints_str = "const una_constraints = ["
|
423 |
first = True
|
424 |
for op in unary_operators:
|
|
|
440 |
return constraints_str
|
441 |
|
442 |
|
443 |
+
def _handle_constraints(binary_operators, constraints, unary_operators):
|
444 |
for op in unary_operators:
|
445 |
if op not in constraints:
|
446 |
constraints[op] = -1
|
|
|
459 |
constraints[op][0], constraints[op][1] = constraints[op][1], constraints[op][0]
|
460 |
|
461 |
|
462 |
+
def _predefined_function_addition(binary_operators, unary_operators):
|
463 |
+
def_hyperparams = ""
|
464 |
for op_list in [binary_operators, unary_operators]:
|
465 |
for i in range(len(op_list)):
|
466 |
op = op_list[i]
|