Spaces:
Running
Running
wip on predictions from equations
Browse files- gui/processing.py +68 -28
gui/processing.py
CHANGED
|
@@ -36,6 +36,42 @@ def pysr_fit(queue: mp.Queue, out_queue: mp.Queue):
|
|
| 36 |
out_queue.put(None)
|
| 37 |
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
class PySRProcess:
|
| 40 |
def __init__(self):
|
| 41 |
self.queue = mp.Queue()
|
|
@@ -44,6 +80,16 @@ class PySRProcess:
|
|
| 44 |
self.process.start()
|
| 45 |
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
PERSISTENT_WRITER = None
|
| 48 |
|
| 49 |
|
|
@@ -121,36 +167,30 @@ def processing(
|
|
| 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
|
| 129 |
-
equation_file_copy = base / "hall_of_fame_copy.csv"
|
| 130 |
-
os.system(f"cp {equation_file_bkup} {equation_file_copy}")
|
| 131 |
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()
|
| 155 |
except pd.errors.EmptyDataError:
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
out_queue.put(None)
|
| 37 |
|
| 38 |
|
| 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 |
+
|
| 75 |
class PySRProcess:
|
| 76 |
def __init__(self):
|
| 77 |
self.queue = mp.Queue()
|
|
|
|
| 80 |
self.process.start()
|
| 81 |
|
| 82 |
|
| 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 |
+
|
| 93 |
PERSISTENT_WRITER = None
|
| 94 |
|
| 95 |
|
|
|
|
| 167 |
),
|
| 168 |
)
|
| 169 |
)
|
|
|
|
| 170 |
while PERSISTENT_WRITER.out_queue.empty():
|
| 171 |
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}")
|
| 175 |
try:
|
|
|
|
|
|
|
|
|
|
| 176 |
equations = pd.read_csv(equation_file_copy)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
except pd.errors.EmptyDataError:
|
| 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)
|