Skip to content

Commit fd28328

Browse files
committed
wip on predictions from equations
1 parent 7701310 commit fd28328

File tree

1 file changed

+68
-28
lines changed

1 file changed

+68
-28
lines changed

gui/processing.py

Lines changed: 68 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,42 @@ def pysr_fit(queue: mp.Queue, out_queue: mp.Queue):
3636
out_queue.put(None)
3737

3838

39+
def pysr_predict(queue: mp.Queue, out_queue: mp.Queue):
40+
import numpy as np
41+
42+
import pysr
43+
44+
while True:
45+
args = queue.get()
46+
47+
if args is None:
48+
break
49+
50+
X = args["X"]
51+
equation_file = str(args["equation_file"])
52+
complexity = args["complexity"]
53+
54+
equation_file_pkl = equation_file.replace(".csv", ".pkl")
55+
equation_file_bkup = equation_file + ".bkup"
56+
57+
equation_file_copy = equation_file.replace(".csv", "_copy.csv")
58+
equation_file_pkl_copy = equation_file.replace(".csv", "_copy.pkl")
59+
60+
# TODO: See if there is way to get lock on file
61+
os.system(f"cp {equation_file_bkup} {equation_file_copy}")
62+
os.system(f"cp {equation_file_pkl} {equation_file_pkl_copy}")
63+
64+
try:
65+
model = pysr.PySRRegressor.from_file(equation_file_pkl_copy, verbosity=0)
66+
except pd.errors.EmptyDataError:
67+
continue
68+
69+
index = np.abs(model.equations_.complexity - complexity).argmin
70+
ypred = model.predict(X, index)
71+
72+
out_queue.put(ypred)
73+
74+
3975
class PySRProcess:
4076
def __init__(self):
4177
self.queue = mp.Queue()
@@ -44,6 +80,16 @@ def __init__(self):
4480
self.process.start()
4581

4682

83+
class PySRReaderProcess:
84+
def __init__(self):
85+
self.queue = mp.Queue()
86+
self.out_queue = mp.Queue()
87+
self.process = mp.Process(
88+
target=pysr_predict, args=(self.queue, self.out_queue)
89+
)
90+
self.process.start()
91+
92+
4793
PERSISTENT_WRITER = None
4894

4995

@@ -121,36 +167,30 @@ def processing(
121167
),
122168
)
123169
)
124-
last_yield_time = None
125170
while PERSISTENT_WRITER.out_queue.empty():
126171
if equation_file_bkup.exists():
172+
# First, copy the file to a the copy file
173+
equation_file_copy = base / "hall_of_fame_copy.csv"
174+
os.system(f"cp {equation_file_bkup} {equation_file_copy}")
127175
try:
128-
# First, copy the file to a the copy file
129-
equation_file_copy = base / "hall_of_fame_copy.csv"
130-
os.system(f"cp {equation_file_bkup} {equation_file_copy}")
131176
equations = pd.read_csv(equation_file_copy)
132-
# Ensure it is pareto dominated, with more complex expressions
133-
# having higher loss. Otherwise remove those rows.
134-
# TODO: Not sure why this occurs; could be the result of a late copy?
135-
equations.sort_values("Complexity", ascending=True, inplace=True)
136-
equations.reset_index(inplace=True)
137-
bad_idx = []
138-
min_loss = None
139-
for i in equations.index:
140-
if min_loss is None or equations.loc[i, "Loss"] < min_loss:
141-
min_loss = float(equations.loc[i, "Loss"])
142-
else:
143-
bad_idx.append(i)
144-
equations.drop(index=bad_idx, inplace=True)
145-
146-
while (
147-
last_yield_time is not None
148-
and time.time() - last_yield_time < plot_update_delay
149-
):
150-
time.sleep(0.1)
151-
152-
yield equations[["Complexity", "Loss", "Equation"]]
153-
154-
last_yield_time = time.time()
155177
except pd.errors.EmptyDataError:
156-
pass
178+
continue
179+
180+
# Ensure it is pareto dominated, with more complex expressions
181+
# having higher loss. Otherwise remove those rows.
182+
# TODO: Not sure why this occurs; could be the result of a late copy?
183+
equations.sort_values("Complexity", ascending=True, inplace=True)
184+
equations.reset_index(inplace=True)
185+
bad_idx = []
186+
min_loss = None
187+
for i in equations.index:
188+
if min_loss is None or equations.loc[i, "Loss"] < min_loss:
189+
min_loss = float(equations.loc[i, "Loss"])
190+
else:
191+
bad_idx.append(i)
192+
equations.drop(index=bad_idx, inplace=True)
193+
194+
yield equations[["Complexity", "Loss", "Equation"]]
195+
196+
time.sleep(0.1)

0 commit comments

Comments
 (0)