Spaces:
Running
Running
MilesCranmer
commited on
Attempt to make PySR process a daemon
Browse files- gui/processing.py +67 -40
gui/processing.py
CHANGED
@@ -4,7 +4,6 @@ import tempfile
|
|
4 |
import time
|
5 |
from pathlib import Path
|
6 |
|
7 |
-
import numpy as np
|
8 |
import pandas as pd
|
9 |
from data import generate_data, read_csv
|
10 |
|
@@ -17,6 +16,37 @@ EMPTY_DF = lambda: pd.DataFrame(
|
|
17 |
)
|
18 |
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
def processing(
|
21 |
file_input,
|
22 |
force_run,
|
@@ -41,6 +71,11 @@ def processing(
|
|
41 |
batch_size,
|
42 |
):
|
43 |
"""Load data, then spawn a process to run the greet function."""
|
|
|
|
|
|
|
|
|
|
|
44 |
if file_input is not None:
|
45 |
try:
|
46 |
X, y = read_csv(file_input, force_run)
|
@@ -53,31 +88,41 @@ def processing(
|
|
53 |
base = Path(tmpdirname)
|
54 |
equation_file = base / "hall_of_fame.csv"
|
55 |
equation_file_bkup = base / "hall_of_fame.csv.bkup"
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
X=X,
|
60 |
y=y,
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
77 |
)
|
78 |
-
process.start()
|
79 |
last_yield_time = None
|
80 |
-
while
|
81 |
if equation_file_bkup.exists():
|
82 |
try:
|
83 |
# First, copy the file to a the copy file
|
@@ -109,21 +154,3 @@ def processing(
|
|
109 |
last_yield_time = time.time()
|
110 |
except pd.errors.EmptyDataError:
|
111 |
pass
|
112 |
-
|
113 |
-
process.join()
|
114 |
-
|
115 |
-
|
116 |
-
def pysr_fit(
|
117 |
-
*,
|
118 |
-
X,
|
119 |
-
y,
|
120 |
-
**pysr_kwargs,
|
121 |
-
):
|
122 |
-
import pysr
|
123 |
-
|
124 |
-
model = pysr.PySRRegressor(
|
125 |
-
progress=False,
|
126 |
-
timeout_in_seconds=1000,
|
127 |
-
**pysr_kwargs,
|
128 |
-
)
|
129 |
-
model.fit(X, y)
|
|
|
4 |
import time
|
5 |
from pathlib import Path
|
6 |
|
|
|
7 |
import pandas as pd
|
8 |
from data import generate_data, read_csv
|
9 |
|
|
|
16 |
)
|
17 |
|
18 |
|
19 |
+
def pysr_fit(queue: mp.Queue, out_queue: mp.Queue):
|
20 |
+
import pysr
|
21 |
+
|
22 |
+
while True:
|
23 |
+
# Get the arguments from the queue, if available
|
24 |
+
args = queue.get()
|
25 |
+
if args is None:
|
26 |
+
break
|
27 |
+
X = args["X"]
|
28 |
+
y = args["y"]
|
29 |
+
kwargs = args["kwargs"]
|
30 |
+
model = pysr.PySRRegressor(
|
31 |
+
progress=False,
|
32 |
+
timeout_in_seconds=1000,
|
33 |
+
**kwargs,
|
34 |
+
)
|
35 |
+
model.fit(X, y)
|
36 |
+
out_queue.put(None)
|
37 |
+
|
38 |
+
|
39 |
+
class PySRProcess:
|
40 |
+
def __init__(self):
|
41 |
+
self.queue = mp.Queue()
|
42 |
+
self.out_queue = mp.Queue()
|
43 |
+
self.process = mp.Process(target=pysr_fit, args=(self.queue, self.out_queue))
|
44 |
+
self.process.start()
|
45 |
+
|
46 |
+
|
47 |
+
PERSISTENT_WRITER = None
|
48 |
+
|
49 |
+
|
50 |
def processing(
|
51 |
file_input,
|
52 |
force_run,
|
|
|
71 |
batch_size,
|
72 |
):
|
73 |
"""Load data, then spawn a process to run the greet function."""
|
74 |
+
global PERSISTENT_WRITER
|
75 |
+
if PERSISTENT_WRITER is None:
|
76 |
+
print("Starting PySR process")
|
77 |
+
PERSISTENT_WRITER = PySRProcess()
|
78 |
+
|
79 |
if file_input is not None:
|
80 |
try:
|
81 |
X, y = read_csv(file_input, force_run)
|
|
|
88 |
base = Path(tmpdirname)
|
89 |
equation_file = base / "hall_of_fame.csv"
|
90 |
equation_file_bkup = base / "hall_of_fame.csv.bkup"
|
91 |
+
# Check if queue is empty, if not, kill the process
|
92 |
+
# and start a new one
|
93 |
+
if not PERSISTENT_WRITER.queue.empty():
|
94 |
+
print("Restarting PySR process")
|
95 |
+
if PERSISTENT_WRITER.process.is_alive():
|
96 |
+
PERSISTENT_WRITER.process.terminate()
|
97 |
+
PERSISTENT_WRITER.process.join()
|
98 |
+
|
99 |
+
PERSISTENT_WRITER = PySRProcess()
|
100 |
+
# Write these to queue instead:
|
101 |
+
PERSISTENT_WRITER.queue.put(
|
102 |
+
dict(
|
103 |
X=X,
|
104 |
y=y,
|
105 |
+
kwargs=dict(
|
106 |
+
niterations=niterations,
|
107 |
+
maxsize=maxsize,
|
108 |
+
binary_operators=binary_operators,
|
109 |
+
unary_operators=unary_operators,
|
110 |
+
equation_file=equation_file,
|
111 |
+
parsimony=parsimony,
|
112 |
+
populations=populations,
|
113 |
+
population_size=population_size,
|
114 |
+
ncycles_per_iteration=ncycles_per_iteration,
|
115 |
+
elementwise_loss=elementwise_loss,
|
116 |
+
adaptive_parsimony_scaling=adaptive_parsimony_scaling,
|
117 |
+
optimizer_algorithm=optimizer_algorithm,
|
118 |
+
optimizer_iterations=optimizer_iterations,
|
119 |
+
batching=batching,
|
120 |
+
batch_size=batch_size,
|
121 |
+
),
|
122 |
+
)
|
123 |
)
|
|
|
124 |
last_yield_time = None
|
125 |
+
while PERSISTENT_WRITER.out_queue.empty():
|
126 |
if equation_file_bkup.exists():
|
127 |
try:
|
128 |
# First, copy the file to a the copy file
|
|
|
154 |
last_yield_time = time.time()
|
155 |
except pd.errors.EmptyDataError:
|
156 |
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|