MilesCranmer commited on
Commit
13219e6
1 Parent(s): 65baea3

Move everything to app.py

Browse files
Files changed (2) hide show
  1. gui/app.py +36 -13
  2. gui/run_pysr_and_save.py +0 -71
gui/app.py CHANGED
@@ -1,7 +1,10 @@
1
  import gradio as gr
 
2
  import os
3
- import tempfile
4
  import pandas as pd
 
 
 
5
 
6
  empty_df = pd.DataFrame(
7
  {
@@ -13,7 +16,7 @@ empty_df = pd.DataFrame(
13
 
14
 
15
  def greet(
16
- file_obj: tempfile._TemporaryFileWrapper,
17
  col_to_fit: str,
18
  niterations: int,
19
  maxsize: int,
@@ -65,19 +68,39 @@ def greet(
65
 
66
  binary_operators = str(binary_operators).replace("'", '"')
67
  unary_operators = str(unary_operators).replace("'", '"')
68
- os.system(
69
- f"python run_pysr_and_save.py "
70
- f"--niterations {niterations} "
71
- f"--maxsize {maxsize} "
72
- f"--binary_operators '{binary_operators}' "
73
- f"--unary_operators '{unary_operators}' "
74
- f"--col_to_fit {col_to_fit} "
75
- f"--filename {file_obj.name}"
 
 
 
 
76
  )
77
- df = pd.read_csv("pysr_output.csv")
78
- error_log = open("error.log", "r").read()
79
- return df, error_log
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
 
 
81
 
82
  def main():
83
  demo = gr.Interface(
 
1
  import gradio as gr
2
+ import numpy as np
3
  import os
 
4
  import pandas as pd
5
+ import pysr
6
+ import tempfile
7
+ from typing import Optional
8
 
9
  empty_df = pd.DataFrame(
10
  {
 
16
 
17
 
18
  def greet(
19
+ file_obj: Optional[tempfile._TemporaryFileWrapper],
20
  col_to_fit: str,
21
  niterations: int,
22
  maxsize: int,
 
68
 
69
  binary_operators = str(binary_operators).replace("'", '"')
70
  unary_operators = str(unary_operators).replace("'", '"')
71
+
72
+ df = pd.read_csv(file_obj)
73
+ y = np.array(df[col_to_fit])
74
+ X = df.drop([col_to_fit], axis=1)
75
+
76
+ model = pysr.PySRRegressor(
77
+ progress=False,
78
+ verbosity=0,
79
+ maxsize=maxsize,
80
+ niterations=niterations,
81
+ binary_operators=binary_operators,
82
+ unary_operators=unary_operators,
83
  )
84
+ model.fit(X, y)
85
+
86
+ df = model.equations_[["equation", "loss", "complexity"]]
87
+ # Convert all columns to string type:
88
+ df = df.astype(str)
89
+ msg = (
90
+ "Success!\n"
91
+ f"You may run the model locally (faster) with "
92
+ f"the following parameters:"
93
+ +f"""
94
+ model = PySRRegressor(
95
+ niterations={niterations},
96
+ binary_operators={str(binary_operators)},
97
+ unary_operators={str(unary_operators)},
98
+ maxsize={maxsize},
99
+ )
100
+ model.fit(X, y)""")
101
 
102
+ df.to_csv("pysr_output.csv", index=False)
103
+ return df, msg
104
 
105
  def main():
106
  demo = gr.Interface(
gui/run_pysr_and_save.py DELETED
@@ -1,71 +0,0 @@
1
- import os
2
- import pandas as pd
3
- import traceback as tb
4
- import numpy as np
5
- from pysr import PySRRegressor
6
- from argparse import ArgumentParser
7
-
8
- # Args:
9
- # niterations
10
- # binary_operators
11
- # unary_operators
12
- # col_to_fit
13
-
14
- empty_df = pd.DataFrame(
15
- {
16
- "equation": [],
17
- "loss": [],
18
- "complexity": [],
19
- }
20
- )
21
-
22
- if __name__ == "__main__":
23
- parser = ArgumentParser()
24
- parser.add_argument("--niterations", type=int)
25
- parser.add_argument("--maxsize", type=int)
26
- parser.add_argument("--binary_operators", type=str)
27
- parser.add_argument("--unary_operators", type=str)
28
- parser.add_argument("--col_to_fit", type=str)
29
- parser.add_argument("--filename", type=str)
30
- args = parser.parse_args()
31
- niterations = args.niterations
32
- binary_operators = eval(args.binary_operators)
33
- unary_operators = eval(args.unary_operators)
34
- col_to_fit = args.col_to_fit
35
- filename = args.filename
36
- maxsize = args.maxsize
37
-
38
-
39
- df = pd.read_csv(filename)
40
- y = np.array(df[col_to_fit])
41
- X = df.drop([col_to_fit], axis=1)
42
-
43
- model = PySRRegressor(
44
- progress=False,
45
- verbosity=0,
46
- maxsize=maxsize,
47
- niterations=niterations,
48
- binary_operators=binary_operators,
49
- unary_operators=unary_operators,
50
- )
51
- model.fit(X, y)
52
-
53
- df = model.equations_[["equation", "loss", "complexity"]]
54
- # Convert all columns to string type:
55
- df = df.astype(str)
56
- error_message = (
57
- "Success!\n"
58
- f"You may run the model locally (faster) with "
59
- f"the following parameters:"
60
- +f"""
61
- model = PySRRegressor(
62
- niterations={niterations},
63
- binary_operators={str(binary_operators)},
64
- unary_operators={str(unary_operators)},
65
- maxsize={maxsize},
66
- )
67
- model.fit(X, y)""")
68
-
69
- df.to_csv("pysr_output.csv", index=False)
70
- with open("error.log", "w") as f:
71
- f.write(error_message)