Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -194,11 +194,11 @@ def cross_plot(actual, pred):
|
|
| 194 |
margin=dict(l=64, r=18, t=8, b=48), hovermode="closest",
|
| 195 |
font=dict(size=FONT_SZ)
|
| 196 |
)
|
| 197 |
-
fig.update_xaxes(title_text="<b>Actual UCS
|
| 198 |
ticks="outside", tickformat=",.0f",
|
| 199 |
showline=True, linewidth=1.2, linecolor="#444", mirror=True, showgrid=True, gridcolor="rgba(0,0,0,0.12)",
|
| 200 |
automargin=True)
|
| 201 |
-
fig.update_yaxes(title_text="<b>Predicted UCS
|
| 202 |
ticks="outside", tickformat=",.0f",
|
| 203 |
showline=True, linewidth=1.2, linecolor="#444", mirror=True, showgrid=True, gridcolor="rgba(0,0,0,0.12)",
|
| 204 |
scaleanchor="x", scaleratio=1, automargin=True)
|
|
@@ -213,17 +213,17 @@ def track_plot(df, include_actual=True):
|
|
| 213 |
|
| 214 |
fig = go.Figure()
|
| 215 |
fig.add_trace(go.Scatter(
|
| 216 |
-
x=df["UCS_Pred
|
| 217 |
line=dict(color=COLORS["pred"], width=1.8),
|
| 218 |
-
name="UCS_Pred
|
| 219 |
-
hovertemplate="UCS_Pred
|
| 220 |
))
|
| 221 |
if include_actual and TARGET in df.columns:
|
| 222 |
fig.add_trace(go.Scatter(
|
| 223 |
x=df[TARGET], y=y, mode="lines",
|
| 224 |
line=dict(color=COLORS["actual"], width=2.0, dash="dot"),
|
| 225 |
-
name="UCS (actual)
|
| 226 |
-
hovertemplate="UCS (actual)
|
| 227 |
))
|
| 228 |
|
| 229 |
fig.update_layout(
|
|
@@ -236,7 +236,7 @@ def track_plot(df, include_actual=True):
|
|
| 236 |
),
|
| 237 |
legend_title_text=""
|
| 238 |
)
|
| 239 |
-
fig.update_xaxes(title_text="<b>UCS
|
| 240 |
ticks="outside", tickformat=",.0f",
|
| 241 |
showline=True, linewidth=1.2, linecolor="#444", mirror=True, showgrid=True, gridcolor="rgba(0,0,0,0.12)",
|
| 242 |
automargin=True)
|
|
@@ -416,12 +416,12 @@ if st.session_state.app_step == "dev":
|
|
| 416 |
st.error("Workbook must include Train/Training/training2 and Test/Testing/testing2 sheets."); st.stop()
|
| 417 |
tr = book[sh_train].copy(); te = book[sh_test].copy()
|
| 418 |
if not (ensure_cols(tr, FEATURES+[TARGET]) and ensure_cols(te, FEATURES+[TARGET])): st.stop()
|
| 419 |
-
tr["UCS_Pred
|
| 420 |
-
te["UCS_Pred
|
| 421 |
|
| 422 |
st.session_state.results["Train"]=tr; st.session_state.results["Test"]=te
|
| 423 |
-
st.session_state.results["m_train"]={"R2":r2_score(tr[TARGET],tr["UCS_Pred
|
| 424 |
-
st.session_state.results["m_test"] ={"R2":r2_score(te[TARGET],te["UCS_Pred
|
| 425 |
|
| 426 |
tr_min = tr[FEATURES].min().to_dict(); tr_max = tr[FEATURES].max().to_dict()
|
| 427 |
st.session_state.train_ranges = {f:(float(tr_min[f]), float(tr_max[f])) for f in FEATURES}
|
|
@@ -435,7 +435,7 @@ if st.session_state.app_step == "dev":
|
|
| 435 |
c1.metric("R²", f"{m['R2']:.4f}"); c2.metric("RMSE", f"{m['RMSE']:.4f}"); c3.metric("MAE", f"{m['MAE']:.4f}")
|
| 436 |
left, mid, right = st.columns(PLOT_COLS, gap="small")
|
| 437 |
with left:
|
| 438 |
-
st.plotly_chart(cross_plot(df[TARGET], df["UCS_Pred
|
| 439 |
use_container_width=False, config=PLOT_CFG)
|
| 440 |
with mid:
|
| 441 |
st.write("") # spacer
|
|
@@ -467,14 +467,14 @@ if st.session_state.app_step == "validate":
|
|
| 467 |
|
| 468 |
info, success = make_notice()
|
| 469 |
st.subheader("Validate the Model")
|
| 470 |
-
info("Upload a dataset with the same <b>features</b> and <b>UCS
|
| 471 |
|
| 472 |
if go_btn and up is not None:
|
| 473 |
book = read_book_bytes(up.getvalue())
|
| 474 |
name = find_sheet(book, ["Validation","Validate","validation2","Val","val"]) or list(book.keys())[0]
|
| 475 |
df = book[name].copy()
|
| 476 |
if not ensure_cols(df, FEATURES+[TARGET]): st.stop()
|
| 477 |
-
df["UCS_Pred
|
| 478 |
st.session_state.results["Validate"]=df
|
| 479 |
|
| 480 |
ranges = st.session_state.train_ranges; oor_pct = 0.0; tbl=None
|
|
@@ -484,8 +484,8 @@ if st.session_state.app_step == "validate":
|
|
| 484 |
if any_viol.any():
|
| 485 |
tbl = df.loc[any_viol, FEATURES].copy()
|
| 486 |
tbl["Violations"] = pd.DataFrame({f:(df[f]<ranges[f][0])|(df[f]>ranges[f][1]) for f in FEATURES}).loc[any_viol].apply(lambda r:", ".join([c for c,v in r.items() if v]), axis=1)
|
| 487 |
-
st.session_state.results["m_val"]={"R2":r2_score(df[TARGET],df["UCS_Pred
|
| 488 |
-
st.session_state.results["sv_val"]={"n":len(df),"pred_min":float(df["UCS_Pred
|
| 489 |
st.session_state.results["oor_tbl"]=tbl
|
| 490 |
st.rerun()
|
| 491 |
|
|
@@ -497,7 +497,7 @@ if st.session_state.app_step == "validate":
|
|
| 497 |
left, mid, right = st.columns(PLOT_COLS, gap="small")
|
| 498 |
with left:
|
| 499 |
st.plotly_chart(cross_plot(st.session_state.results["Validate"][TARGET],
|
| 500 |
-
st.session_state.results["Validate"]["UCS_Pred
|
| 501 |
use_container_width=False, config=PLOT_CFG)
|
| 502 |
with mid:
|
| 503 |
st.write("")
|
|
@@ -528,7 +528,7 @@ if st.session_state.app_step == "predict":
|
|
| 528 |
|
| 529 |
info, _success = make_notice()
|
| 530 |
st.subheader("Prediction")
|
| 531 |
-
info("Upload a dataset with the feature columns (no <b>UCS
|
| 532 |
|
| 533 |
if go_btn and up is not None:
|
| 534 |
book = read_book_bytes(up.getvalue()); name = list(book.keys())[0]
|
|
@@ -543,10 +543,10 @@ if st.session_state.app_step == "predict":
|
|
| 543 |
oor_pct = float(any_viol.mean()*100.0)
|
| 544 |
st.session_state.results["sv_pred"]={
|
| 545 |
"n":len(df),
|
| 546 |
-
"pred_min":float(df["UCS_Pred
|
| 547 |
-
"pred_max":float(df["UCS_Pred
|
| 548 |
-
"pred_mean":float(df["UCS_Pred
|
| 549 |
-
"pred_std":float(df["UCS_Pred
|
| 550 |
"oor":oor_pct
|
| 551 |
}
|
| 552 |
st.rerun()
|
|
|
|
| 194 |
margin=dict(l=64, r=18, t=8, b=48), hovermode="closest",
|
| 195 |
font=dict(size=FONT_SZ)
|
| 196 |
)
|
| 197 |
+
fig.update_xaxes(title_text="<b>Actual UCS</b>", range=[x0, x1],
|
| 198 |
ticks="outside", tickformat=",.0f",
|
| 199 |
showline=True, linewidth=1.2, linecolor="#444", mirror=True, showgrid=True, gridcolor="rgba(0,0,0,0.12)",
|
| 200 |
automargin=True)
|
| 201 |
+
fig.update_yaxes(title_text="<b>Predicted UCS</b>", range=[x0, x1],
|
| 202 |
ticks="outside", tickformat=",.0f",
|
| 203 |
showline=True, linewidth=1.2, linecolor="#444", mirror=True, showgrid=True, gridcolor="rgba(0,0,0,0.12)",
|
| 204 |
scaleanchor="x", scaleratio=1, automargin=True)
|
|
|
|
| 213 |
|
| 214 |
fig = go.Figure()
|
| 215 |
fig.add_trace(go.Scatter(
|
| 216 |
+
x=df["UCS_Pred"], y=y, mode="lines",
|
| 217 |
line=dict(color=COLORS["pred"], width=1.8),
|
| 218 |
+
name="UCS_Pred",
|
| 219 |
+
hovertemplate="UCS_Pred: %{x:.0f}<br>"+ylab+": %{y}<extra></extra>"
|
| 220 |
))
|
| 221 |
if include_actual and TARGET in df.columns:
|
| 222 |
fig.add_trace(go.Scatter(
|
| 223 |
x=df[TARGET], y=y, mode="lines",
|
| 224 |
line=dict(color=COLORS["actual"], width=2.0, dash="dot"),
|
| 225 |
+
name="UCS (actual)",
|
| 226 |
+
hovertemplate="UCS (actual): %{x:.0f}<br>"+ylab+": %{y}<extra></extra>"
|
| 227 |
))
|
| 228 |
|
| 229 |
fig.update_layout(
|
|
|
|
| 236 |
),
|
| 237 |
legend_title_text=""
|
| 238 |
)
|
| 239 |
+
fig.update_xaxes(title_text="<b>UCS</b>", side="top",
|
| 240 |
ticks="outside", tickformat=",.0f",
|
| 241 |
showline=True, linewidth=1.2, linecolor="#444", mirror=True, showgrid=True, gridcolor="rgba(0,0,0,0.12)",
|
| 242 |
automargin=True)
|
|
|
|
| 416 |
st.error("Workbook must include Train/Training/training2 and Test/Testing/testing2 sheets."); st.stop()
|
| 417 |
tr = book[sh_train].copy(); te = book[sh_test].copy()
|
| 418 |
if not (ensure_cols(tr, FEATURES+[TARGET]) and ensure_cols(te, FEATURES+[TARGET])): st.stop()
|
| 419 |
+
tr["UCS_Pred"] = model.predict(tr[FEATURES])
|
| 420 |
+
te["UCS_Pred"] = model.predict(te[FEATURES])
|
| 421 |
|
| 422 |
st.session_state.results["Train"]=tr; st.session_state.results["Test"]=te
|
| 423 |
+
st.session_state.results["m_train"]={"R2":r2_score(tr[TARGET],tr["UCS_Pred"]), "RMSE":rmse(tr[TARGET],tr["UCS_Pred"]), "MAE":mean_absolute_error(tr[TARGET],tr["UCS_Pred"])}
|
| 424 |
+
st.session_state.results["m_test"] ={"R2":r2_score(te[TARGET],te["UCS_Pred"]), "RMSE":rmse(te[TARGET],te["UCS_Pred"]), "MAE":mean_absolute_error(te[TARGET],te["UCS_Pred"])}
|
| 425 |
|
| 426 |
tr_min = tr[FEATURES].min().to_dict(); tr_max = tr[FEATURES].max().to_dict()
|
| 427 |
st.session_state.train_ranges = {f:(float(tr_min[f]), float(tr_max[f])) for f in FEATURES}
|
|
|
|
| 435 |
c1.metric("R²", f"{m['R2']:.4f}"); c2.metric("RMSE", f"{m['RMSE']:.4f}"); c3.metric("MAE", f"{m['MAE']:.4f}")
|
| 436 |
left, mid, right = st.columns(PLOT_COLS, gap="small")
|
| 437 |
with left:
|
| 438 |
+
st.plotly_chart(cross_plot(df[TARGET], df["UCS_Pred"]),
|
| 439 |
use_container_width=False, config=PLOT_CFG)
|
| 440 |
with mid:
|
| 441 |
st.write("") # spacer
|
|
|
|
| 467 |
|
| 468 |
info, success = make_notice()
|
| 469 |
st.subheader("Validate the Model")
|
| 470 |
+
info("Upload a dataset with the same <b>features</b> and <b>UCS</b> to evaluate performance.")
|
| 471 |
|
| 472 |
if go_btn and up is not None:
|
| 473 |
book = read_book_bytes(up.getvalue())
|
| 474 |
name = find_sheet(book, ["Validation","Validate","validation2","Val","val"]) or list(book.keys())[0]
|
| 475 |
df = book[name].copy()
|
| 476 |
if not ensure_cols(df, FEATURES+[TARGET]): st.stop()
|
| 477 |
+
df["UCS_Pred"] = model.predict(df[FEATURES])
|
| 478 |
st.session_state.results["Validate"]=df
|
| 479 |
|
| 480 |
ranges = st.session_state.train_ranges; oor_pct = 0.0; tbl=None
|
|
|
|
| 484 |
if any_viol.any():
|
| 485 |
tbl = df.loc[any_viol, FEATURES].copy()
|
| 486 |
tbl["Violations"] = pd.DataFrame({f:(df[f]<ranges[f][0])|(df[f]>ranges[f][1]) for f in FEATURES}).loc[any_viol].apply(lambda r:", ".join([c for c,v in r.items() if v]), axis=1)
|
| 487 |
+
st.session_state.results["m_val"]={"R2":r2_score(df[TARGET],df["UCS_Pred"]), "RMSE":rmse(df[TARGET],df["UCS_Pred"]), "MAE":mean_absolute_error(df[TARGET],df["UCS_Pred"])}
|
| 488 |
+
st.session_state.results["sv_val"]={"n":len(df),"pred_min":float(df["UCS_Pred"].min()),"pred_max":float(df["UCS_Pred"].max()),"oor":oor_pct}
|
| 489 |
st.session_state.results["oor_tbl"]=tbl
|
| 490 |
st.rerun()
|
| 491 |
|
|
|
|
| 497 |
left, mid, right = st.columns(PLOT_COLS, gap="small")
|
| 498 |
with left:
|
| 499 |
st.plotly_chart(cross_plot(st.session_state.results["Validate"][TARGET],
|
| 500 |
+
st.session_state.results["Validate"]["UCS_Pred"]),
|
| 501 |
use_container_width=False, config=PLOT_CFG)
|
| 502 |
with mid:
|
| 503 |
st.write("")
|
|
|
|
| 528 |
|
| 529 |
info, _success = make_notice()
|
| 530 |
st.subheader("Prediction")
|
| 531 |
+
info("Upload a dataset with the feature columns (no <b>UCS</b>).")
|
| 532 |
|
| 533 |
if go_btn and up is not None:
|
| 534 |
book = read_book_bytes(up.getvalue()); name = list(book.keys())[0]
|
|
|
|
| 543 |
oor_pct = float(any_viol.mean()*100.0)
|
| 544 |
st.session_state.results["sv_pred"]={
|
| 545 |
"n":len(df),
|
| 546 |
+
"pred_min":float(df["UCS_Pred"].min()),
|
| 547 |
+
"pred_max":float(df["UCS_Pred"].max()),
|
| 548 |
+
"pred_mean":float(df["UCS_Pred"].mean()),
|
| 549 |
+
"pred_std":float(df["UCS_Pred"].std(ddof=0)),
|
| 550 |
"oor":oor_pct
|
| 551 |
}
|
| 552 |
st.rerun()
|