Spaces:
Running
Running
Jae-Won Chung
commited on
Commit
•
9899c96
1
Parent(s):
2b5e97a
Eval nitpicking
Browse files
app.py
CHANGED
@@ -11,6 +11,7 @@ import gradio as gr
|
|
11 |
import pandas as pd
|
12 |
import plotly.io as pio
|
13 |
import plotly.express as px
|
|
|
14 |
pio.templates.default = "plotly_white"
|
15 |
|
16 |
|
@@ -110,21 +111,33 @@ class TableManager:
|
|
110 |
# Evaluate the formula and catch any error.
|
111 |
try:
|
112 |
# Give the users some helper functions that can be used in the formula
|
113 |
-
# like "@sum(response_length)".
|
114 |
col = self.full_df.eval(
|
115 |
formula,
|
116 |
local_dict={"sum": sum, "len": len, "max": max, "min": min},
|
|
|
117 |
)
|
118 |
-
# Only round floating point columns.
|
119 |
-
if isinstance(col, pd.Series) and col.dtype.kind == "f":
|
120 |
-
col = col.round(2)
|
121 |
-
if column_name in self.full_df.columns:
|
122 |
-
self.full_df[column_name] = col
|
123 |
-
else:
|
124 |
-
self.full_df.insert(len(self.schema) + 1, column_name, col)
|
125 |
except Exception as exc:
|
126 |
return self.cur_df, self._format_msg(f"Invalid formula: {exc}")
|
127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
# If adding a column succeeded, `self.cur_df` should also be updated.
|
129 |
self.cur_df = self.full_df.loc[self.cur_index]
|
130 |
return self.cur_df, self._format_msg(f"{verb} column '{column_name}'.")
|
|
|
11 |
import pandas as pd
|
12 |
import plotly.io as pio
|
13 |
import plotly.express as px
|
14 |
+
from pandas.api.types import is_numeric_dtype, is_float_dtype
|
15 |
pio.templates.default = "plotly_white"
|
16 |
|
17 |
|
|
|
111 |
# Evaluate the formula and catch any error.
|
112 |
try:
|
113 |
# Give the users some helper functions that can be used in the formula
|
114 |
+
# like "@sum(response_length)". Also wipe out some global variables.
|
115 |
col = self.full_df.eval(
|
116 |
formula,
|
117 |
local_dict={"sum": sum, "len": len, "max": max, "min": min},
|
118 |
+
global_dict={"global_tbm": None},
|
119 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
except Exception as exc:
|
121 |
return self.cur_df, self._format_msg(f"Invalid formula: {exc}")
|
122 |
|
123 |
+
# If the result is a numeric scalar, make it a Series.
|
124 |
+
# We may have deleted some models (rows) form the full dataframe when we
|
125 |
+
# called dropna, so we need to query the maximum index instead of taking len.
|
126 |
+
if isinstance(col, (int, float)):
|
127 |
+
col = pd.Series([col] * (self.full_df.index.max() + 1))
|
128 |
+
# We only accept numeric columns.
|
129 |
+
if not is_numeric_dtype(col):
|
130 |
+
return self.cur_df, self._format_msg("Invalid formula: result must be numeric.")
|
131 |
+
# Round if it's floating point.
|
132 |
+
if is_float_dtype(col):
|
133 |
+
col = col.round(2)
|
134 |
+
|
135 |
+
# If the column already exists, update it.
|
136 |
+
if column_name in self.full_df.columns:
|
137 |
+
self.full_df[column_name] = col
|
138 |
+
else:
|
139 |
+
self.full_df.insert(len(self.schema) + 1, column_name, col)
|
140 |
+
|
141 |
# If adding a column succeeded, `self.cur_df` should also be updated.
|
142 |
self.cur_df = self.full_df.loc[self.cur_index]
|
143 |
return self.cur_df, self._format_msg(f"{verb} column '{column_name}'.")
|