Skip to content

Commit 519fcb9

Browse files
committed
Move more parts to other files
1 parent 9fa2182 commit 519fcb9

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed

gui/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from .data import test_equations
44
from .plots import replot, replot_pareto
5-
from .processing import process
5+
from .processing import processing
66

77

88
def _data_layout():
@@ -196,7 +196,7 @@ def main():
196196
blocks["run"] = gr.Button()
197197

198198
blocks["run"].click(
199-
process,
199+
processing,
200200
inputs=[
201201
blocks[k]
202202
for k in [

gui/data.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,25 @@ def generate_data(s: str, num_points: int, noise_level: float, data_seed: int):
2020
noise = rstate.normal(0, noise_level, y.shape)
2121
y_noisy = y + noise
2222
return pd.DataFrame({"x": x}), y_noisy
23+
24+
25+
def read_csv(file_input: str, force_run: bool):
26+
# Look at some statistics of the file:
27+
df = pd.read_csv(file_input)
28+
if len(df) == 0:
29+
raise ValueError("The file is empty!")
30+
if len(df.columns) == 1:
31+
raise ValueError("The file has only one column!")
32+
if len(df) > 10_000 and not force_run:
33+
raise ValueError(
34+
"You have uploaded a file with more than 10,000 rows. "
35+
"This will take very long to run. "
36+
"Please upload a subsample of the data, "
37+
"or check the box 'Ignore Warnings'.",
38+
)
39+
40+
col_to_fit = df.columns[-1]
41+
y = np.array(df[col_to_fit])
42+
X = df.drop([col_to_fit], axis=1)
43+
44+
return X, y

gui/processing.py

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import numpy as np
88
import pandas as pd
99

10-
from .data import generate_data
10+
from .data import generate_data, read_csv
1111

1212
EMPTY_DF = lambda: pd.DataFrame(
1313
{
@@ -18,7 +18,7 @@
1818
)
1919

2020

21-
def process(
21+
def processing(
2222
file_input,
2323
force_run,
2424
test_equation,
@@ -43,30 +43,10 @@ def process(
4343
):
4444
"""Load data, then spawn a process to run the greet function."""
4545
if file_input is not None:
46-
# Look at some statistics of the file:
47-
df = pd.read_csv(file_input)
48-
if len(df) == 0:
49-
return (
50-
EMPTY_DF(),
51-
"The file is empty!",
52-
)
53-
if len(df.columns) == 1:
54-
return (
55-
EMPTY_DF(),
56-
"The file has only one column!",
57-
)
58-
if len(df) > 10_000 and not force_run:
59-
return (
60-
EMPTY_DF(),
61-
"You have uploaded a file with more than 10,000 rows. "
62-
"This will take very long to run. "
63-
"Please upload a subsample of the data, "
64-
"or check the box 'Ignore Warnings'.",
65-
)
66-
67-
col_to_fit = df.columns[-1]
68-
y = np.array(df[col_to_fit])
69-
X = df.drop([col_to_fit], axis=1)
46+
try:
47+
X, y = read_csv(file_input, force_run)
48+
except ValueError as e:
49+
return (EMPTY_DF(), str(e))
7050
else:
7151
X, y = generate_data(test_equation, num_points, noise_level, data_seed)
7252

0 commit comments

Comments
 (0)