Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,7 +19,7 @@ from sklearn.metrics import mean_squared_error, mean_absolute_error
|
|
| 19 |
# Constants (GR)
|
| 20 |
# =========================
|
| 21 |
APP_NAME = "ST_Log_GR"
|
| 22 |
-
TAGLINE = "Gamma Ray Prediction"
|
| 23 |
|
| 24 |
FEATURES = ["GPM", "SPP", "RPM", "WOB", "T", "ROP"]
|
| 25 |
|
|
@@ -690,68 +690,54 @@ if st.session_state.app_step == "validate":
|
|
| 690 |
st.write("*Out-of-range rows (vs. Training min–max):*")
|
| 691 |
df_centered_rounded(st.session_state.results["oor_tbl"])
|
| 692 |
|
|
|
|
| 693 |
# =========================
|
| 694 |
# PREDICTION (no actual GR)
|
| 695 |
# =========================
|
| 696 |
if st.session_state.app_step == "predict":
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
if
|
| 700 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 701 |
|
| 702 |
-
|
| 703 |
-
oor_pct = 0.0
|
| 704 |
-
oor_tbl = None # Initialize OOR table as None
|
| 705 |
-
if ranges:
|
| 706 |
-
any_viol = pd.DataFrame({f:(df[f]<ranges[f][0])|(df[f]>ranges[f][1]) for f in FEATURES}).any(axis=1)
|
| 707 |
-
oor_pct = float(any_viol.mean()*100.0)
|
| 708 |
-
if any_viol.any():
|
| 709 |
-
oor_tbl = df.loc[any_viol, FEATURES].copy()
|
| 710 |
-
for c in FEATURES:
|
| 711 |
-
if pd.api.types.is_numeric_dtype(oor_tbl[c]): oor_tbl[c] = oor_tbl[c].round(2)
|
| 712 |
-
oor_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)
|
| 713 |
-
|
| 714 |
-
st.session_state.results["PredictOnly"]=df
|
| 715 |
-
st.session_state.results["sv_pred"]={
|
| 716 |
-
"n":len(df),
|
| 717 |
-
"pred_min":float(df["GR_Pred"].min()),
|
| 718 |
-
"pred_max":float(df["GR_Pred"].max()),
|
| 719 |
-
"pred_mean":float(df["GR_Pred"].mean()),
|
| 720 |
-
"pred_std":float(df["GR_Pred"].std(ddof=0)),
|
| 721 |
-
"oor":oor_pct
|
| 722 |
-
}
|
| 723 |
-
st.session_state.results["oor_tbl_pred"] = oor_tbl # Save the OOR table for prediction
|
| 724 |
|
| 725 |
if "PredictOnly" in st.session_state.results:
|
| 726 |
-
|
| 727 |
-
sv = st.session_state.results["sv_pred"]
|
| 728 |
-
|
| 729 |
-
col_left, col_right = st.columns([2,3], gap="large")
|
| 730 |
-
with col_left:
|
| 731 |
-
table = pd.DataFrame({
|
| 732 |
-
"Metric": ["# points","Pred min","Pred max","Pred mean","Pred std","OOR %"],
|
| 733 |
-
"Value": [sv["n"],
|
| 734 |
-
round(sv["pred_min"],2),
|
| 735 |
-
round(sv["pred_max"],2),
|
| 736 |
-
round(sv["pred_mean"],2),
|
| 737 |
-
round(sv["pred_std"],2),
|
| 738 |
-
f'{sv["oor"]:.1f}%']
|
| 739 |
-
})
|
| 740 |
-
st.markdown('<div class="st-message-box st-success">Predictions ready ✓</div>', unsafe_allow_html=True)
|
| 741 |
-
df_centered_rounded(table, hide_index=True)
|
| 742 |
-
st.caption("**★ OOR** = % of rows whose input features fall outside the training min–max range.")
|
| 743 |
-
|
| 744 |
-
# Add this check and display logic to the prediction section
|
| 745 |
-
if st.session_state.results.get("oor_tbl_pred") is not None:
|
| 746 |
-
st.markdown('<div class="st-message-box st-warning">Some inputs fall outside **training min–max** ranges.</div>', unsafe_allow_html=True)
|
| 747 |
-
st.write("*Out-of-range rows (vs. Training min–max):*")
|
| 748 |
-
df_centered_rounded(st.session_state.results["oor_tbl_pred"])
|
| 749 |
-
|
| 750 |
-
with col_right:
|
| 751 |
-
st.plotly_chart(
|
| 752 |
-
track_plot(df, include_actual=False, pred_col="GR_Pred", actual_col="GR"),
|
| 753 |
-
use_container_width=False, config={"displayModeBar": False, "scrollZoom": True}
|
| 754 |
-
)
|
| 755 |
|
| 756 |
# =========================
|
| 757 |
# Preview modal (re-usable)
|
|
|
|
| 19 |
# Constants (GR)
|
| 20 |
# =========================
|
| 21 |
APP_NAME = "ST_Log_GR"
|
| 22 |
+
TAGLINE = "Real-Time Gamma Ray Prediction Using Drilling Data"
|
| 23 |
|
| 24 |
FEATURES = ["GPM", "SPP", "RPM", "WOB", "T", "ROP"]
|
| 25 |
|
|
|
|
| 690 |
st.write("*Out-of-range rows (vs. Training min–max):*")
|
| 691 |
df_centered_rounded(st.session_state.results["oor_tbl"])
|
| 692 |
|
| 693 |
+
|
| 694 |
# =========================
|
| 695 |
# PREDICTION (no actual GR)
|
| 696 |
# =========================
|
| 697 |
if st.session_state.app_step == "predict":
|
| 698 |
+
st.sidebar.header("Prediction (No Actual GR)")
|
| 699 |
+
up = st.sidebar.file_uploader("Upload Prediction Excel", type=["xlsx","xls"])
|
| 700 |
+
if up is not None:
|
| 701 |
+
book = read_book_bytes(up.getvalue())
|
| 702 |
+
if book:
|
| 703 |
+
df0 = next(iter(book.values()))
|
| 704 |
+
st.sidebar.caption(f"**Data loaded:** {up.name} • {df0.shape[0]} rows × {df0.shape[1]} cols")
|
| 705 |
+
if st.sidebar.button("Preview data", use_container_width=True, disabled=(up is None)):
|
| 706 |
+
st.session_state.show_preview_modal = True
|
| 707 |
+
|
| 708 |
+
# Corrected logic: Move the code that depends on the button's click
|
| 709 |
+
# to be immediately after the button is defined.
|
| 710 |
+
if st.sidebar.button("Predict", type="primary", use_container_width=True):
|
| 711 |
+
if up is not None:
|
| 712 |
+
book = read_book_bytes(up.getvalue()); name = list(book.keys())[0]
|
| 713 |
+
df = normalize_df(book[name].copy())
|
| 714 |
+
if not ensure_cols(df, FEATURES):
|
| 715 |
+
st.markdown('<div class="st-message-box st-error">Missing required feature columns.</div>', unsafe_allow_html=True);
|
| 716 |
+
st.stop()
|
| 717 |
+
|
| 718 |
+
pred_raw = model.predict(df[FEATURES])
|
| 719 |
+
df["GR_Pred"] = inverse_target(np.asarray(pred_raw, dtype=float), TARGET_TRANSFORM)
|
| 720 |
+
st.session_state.results["PredictOnly"]=df
|
| 721 |
+
|
| 722 |
+
ranges = st.session_state.train_ranges; oor_pct = 0.0
|
| 723 |
+
if ranges:
|
| 724 |
+
any_viol = pd.DataFrame({f:(df[f]<ranges[f][0])|(df[f]>ranges[f][1]) for f in FEATURES}).any(axis=1)
|
| 725 |
+
oor_pct = float(any_viol.mean()*100.0)
|
| 726 |
+
st.session_state.results["sv_pred"]={
|
| 727 |
+
"n":len(df),
|
| 728 |
+
"pred_min":float(df["GR_Pred"].min()),
|
| 729 |
+
"pred_max":float(df["GR_Pred"].max()),
|
| 730 |
+
"pred_mean":float(df["GR_Pred"].mean()),
|
| 731 |
+
"pred_std":float(df["GR_Pred"].std(ddof=0)),
|
| 732 |
+
"oor":oor_pct
|
| 733 |
+
}
|
| 734 |
+
|
| 735 |
+
if st.sidebar.button("⬅ Back to Case Building", use_container_width=True): st.session_state.app_step="dev"; st.rerun()
|
| 736 |
|
| 737 |
+
sticky_header("Prediction", "Upload a dataset with the feature columns (no **GR**).")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 738 |
|
| 739 |
if "PredictOnly" in st.session_state.results:
|
| 740 |
+
# ... (Rest of the code to display the results)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 741 |
|
| 742 |
# =========================
|
| 743 |
# Preview modal (re-usable)
|