Spaces:
Paused
Paused
Update app.py
Browse files123417 unmodified lines2223242526272829301 unmodified line3233343536373811 unmodified lines505152535455565758596061626376 unmodified lines1401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861 unmodified line18818919019119219319424 unmodified lines219220221222223224272 unmodified lines49749849950050150250350450550650750850951051151251351451551651751851952052152252327 unmodified lines5515525535545555565576 unmodified lines564565566567568569570100 unmodified lines6716726736746756766771 unmodified line67968068168268368468568668758 unmodified lines7467477487497507517527537544 unmodified lines75976076176276376476552 unmodified lines818819820821822823824825826# app.py — ST_Sonic_Ts (Shear Slowness Ts)import io, json, os, base64, mathfrom pathlib import Pathimport streamlit as st17 unmodified linesAPP_NAME = "ST_Log_Sonic (Ts)"TAGLINE = "Real-Time Shear Slowness (Ts) Prediction"# Defaults (will be overridden by meta if present)FEATURES = ["WOB(klbf)", "TORQUE(kft.lbf)", "SPP(psi)", "RPM(1/min)", "ROP(ft/h)", "Flow Rate, gpm"]TARGET = "Ts"PRED_COL = "Ts_Pred"MODELS_DIR = Path("models")1 unmodified lineMODEL_FALLBACKS = [MODELS_DIR / "model.joblib", MODELS_DIR / "model.pkl"]COLORS = {"pred": "#1f77b4", "actual": "#f2b702", "ref": "#5a5a5a"}# Toggle to show strict version banner from metaSTRICT_VERSION_CHECK = False# ---- Plot sizing ----11 unmodified linesst.markdown("""<style>.brand-logo { width: 200px; height: auto; object-fit: contain; }.sidebar-header { display:flex; align-items:center; gap:12px; }.sidebar-header .text h1 { font-size: 1.05rem; margin:0; line-height:1.1; }.sidebar-header .text .tag { font-size: .85rem; color:#6b7280; margin:2px 0 0; }.centered-container { display: flex; flex-direction: column; align-items: center; text-align: center; }.st-message-box { background-color: #f0f2f6; color: #333; padding: 10px; border-radius: 10px; border: 1px solid #e6e9ef; }.st-message-box.st-success { background-color: #d4edda; color: #155724; border-color: #c3e6cb; }.st-message-box.st-warning { background-color: #fff3cd; color: #856404; border-color: #ffeeba; }.st-message-box.st-error { background-color: #f8d7da; color: #721c24; border-color: #f5c6cb; }.main .block-container { overflow: unset !important; }div[data-testid="stVerticalBlock"] { overflow: unset !important; }div[data-testid="stExpander"] > details > summary {76 unmodified linesxl = pd.ExcelFile(bio)return {sh: xl.parse(sh) for sh in xl.sheet_names}def read_book_bytes(b: bytes):return parse_excel(b) if b else {}# ---- Canonical feature aliasing ------------------------------------------def _build_alias_map(canonical_features: list[str], target_name: str) -> dict:"""Returns a dict of common header variants -> canonical names as per the model's FEATURES.We choose the canonical for each family by checking which string exists in canonical_features."""def pick(expected_list, family_variants):# pick the first variant that exists in expected_list, else fall back to first in family_variantsfor v in family_variants:if v in expected_list:return vreturn family_variants[0]can_WOB = pick(canonical_features, ["WOB, klbf","WOB(klbf)","WOB (klbf)"])can_TORQUE = pick(canonical_features, ["Torque(kft.lbf)","TORQUE(kft.lbf)"])can_SPP = pick(canonical_features, ["SPP(psi)"])can_RPM = pick(canonical_features, ["RPM(1/min)","RPM (1/min)"])can_ROP = pick(canonical_features, ["ROP(ft/h)","ROP (ft/h)"])can_FR = pick(canonical_features, ["Flow Rate, gpm","Flow Rate , gpm","Flow Rate,gpm"])alias = {# WOB"WOB, klbf": can_WOB, "WOB(klbf)": can_WOB, "WOB (klbf)": can_WOB, "WOB( klbf)": can_WOB, "WOB , klbf": can_WOB,# Torque"Torque(kft.lbf)": can_TORQUE, "TORQUE(kft.lbf)": can_TORQUE,# SPP"SPP(psi)": can_SPP,# RPM"RPM(1/min)": can_RPM, "RPM (1/min)": can_RPM,# ROP"ROP(ft/h)": can_ROP, "ROP (ft/h)": can_ROP,# Flow"Flow Rate, gpm": can_FR, "Fow Rate, gpm": can_FR, "Fow Rate, gpm ": can_FR, "Flow Rate , gpm": can_FR, "Flow Rate,gpm": can_FR,# Depth (for plotting only)"Depth, ft": "Depth, ft", "Depth(ft)": "Depth, ft", "DEPTH, ft": "Depth, ft",# Ts targets (map all to the chosen TARGET)"Ts": target_name, "Ts,us/ft_Actual": target_name, "Ts, us/ft_Actual": target_name,"TS_Actual": target_name, "Ts (us/ft)_Actual": target_name}return alias1 unmodified lineout = df.copy()out.columns = [str(c).strip().replace(" ,", ",").replace(", ", ", ").replace(" ", " ") for c in out.columns]alias = _build_alias_map(canonical_features, target_name)# only rename keys that actually existactual = {k: v for k, v in alias.items() if k in out.columns and k != v}return out.rename(columns=actual)24 unmodified lines)st.dataframe(styler, use_container_width=True, hide_index=hide_index)# === Excel export helpers =================================================def _excel_engine() -> str:try:272 unmodified lines# ---------- Preview (matplotlib) ----------def preview_tracks(df: pd.DataFrame, cols: list[str]):cols = [c for c in cols if c in df.columns]n = len(cols)if n == 0:fig, ax = plt.subplots(figsize=(4, 2))ax.text(0.5,0.5,"No selected columns",ha="center",va="center"); ax.axis("off")return figfig, axes = plt.subplots(1, n, figsize=(2.2*n, 7.0), sharey=True, dpi=100)if n == 1: axes = [axes]depth_col = next((c for c in df.columns if 'depth' in str(c).lower()), None)if depth_col is not None:idx = pd.to_numeric(df[depth_col], errors="coerce"); y_label = depth_colelse:idx = pd.Series(np.arange(1, len(df) + 1)); y_label = "Point Index"for i, (ax, col) in enumerate(zip(axes, cols)):ax.plot(pd.to_numeric(df[col], errors="coerce"), idx, '-', lw=1.6, color="#333")ax.set_xlabel(col); ax.xaxis.set_label_position('top'); ax.xaxis.tick_top()ax.set_ylim(float(idx.max()), float(idx.min()))ax.grid(True, linestyle=":", alpha=0.3)if i == 0: ax.set_ylabel(y_label)else:ax.tick_params(labelleft=False); ax.set_ylabel("")fig.tight_layout()return fig27 unmodified linesst.error(f"Failed to load model: {e}")st.stop()# Prefer Ts metameta = {}meta_candidates = [MODELS_DIR / "ts_meta.json", MODELS_DIR / "meta.json", MODELS_DIR / "ym_meta.json"]meta_path = next((p for p in meta_candidates if p.exists()), None)6 unmodified linesexcept Exception as e:st.warning(f"Could not parse meta file ({meta_path.name}): {e}")# Optional: version banner (silenced by default)if STRICT_VERSION_CHECK and meta.get("versions"):import numpy as _np, sklearn as _sklmv = meta["versions"]; msg=[]100 unmodified linesst.markdown('<div class="st-message-box st-error">Workbook must include Train/Training/training2 and Test/Testing/testing2 sheets.</div>', unsafe_allow_html=True)st.stop()# Use meta FEATURES as canonical when normalizingtr = _normalize_columns(book[sh_train].copy(), FEATURES, TARGET)te = _normalize_columns(book[sh_test].copy(), FEATURES, TARGET)1 unmodified linest.markdown('<div class="st-message-box st-error">Missing required columns.</div>', unsafe_allow_html=True)st.stop()# Predict with exactly the columns the model was trained ontr[PRED_COL] = model.predict(tr[FEATURES])te[PRED_COL] = model.predict(te[FEATURES])st.session_state.results["Train"]=tr; st.session_state.results["Test"]=test.session_state.results["m_train"]={58 unmodified linesbook = read_book_bytes(up.getvalue())name = find_sheet(book, ["Validation","Validate","validation2","Val","val"]) or list(book.keys())[0]df = _normalize_columns(book[name].copy(), FEATURES, TARGET)if not ensure_cols(df, FEATURES+[TARGET]):st.markdown('<div class="st-message-box st-error">Missing required columns.</div>', unsafe_allow_html=True); st.stop()df[PRED_COL] = model.predict(df[FEATURES])st.session_state.results["Validate"]=dfranges = st.session_state.train_ranges; oor_pct = 0.0; tbl=None4 unmodified linestbl = df.loc[any_viol, FEATURES].copy()for c in FEATURES:if pd.api.types.is_numeric_dtype(tbl[c]): tbl[c] = tbl[c].round(2)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)st.session_state.results["m_val"]={"R": pearson_r(df[TARGET], df[PRED_COL]),"RMSE": rmse(df[TARGET], df[PRED_COL]),52 unmodified linesif go_btn and up is not None:book = read_book_bytes(up.getvalue()); name = list(book.keys())[0]df = _normalize_columns(book[name].copy(), FEATURES, TARGET)if not ensure_cols(df, FEATURES):st.markdown('<div class="st-message-box st-error">Missing required columns.</div>', unsafe_allow_html=True); st.stop()df[PRED_COL] = model.predict(df[FEATURES])st.session_state.results["PredictOnly"]=dfranges = st.session_state.train_ranges; oor_pct = 0.01234517 unmodified lines232425262728293031323334353637381 unmodified line4041424344454611 unmodified lines585960616263646566676876 unmodified lines1451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931 unmodified line19519619719819920024 unmodified lines225226227228229230231232233234235236237238239240241272 unmodified lines51451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656727 unmodified lines5955965975985996006016 unmodified lines608609610611612613614100 unmodified lines7157167177187197201 unmodified line72272372472572672772872973058 unmodified lines7897907917927937947957967974 unmodified lines80280380480580680780880981052 unmodified lines863864865866867868869870871# app.py — ST_Sonic_Ts (Shear Slowness Ts)import io, json, os, base64, mathfrom pathlib import Pathimport streamlit as st17 unmodified linesAPP_NAME = "ST_Log_Sonic (Ts)"TAGLINE = "Real-Time Shear Slowness (Ts) Prediction"# Defaults (overridden by ts_meta.json if present)FEATURES = ["WOB (klbf)","Torque (kft.lbf)","SPP (psi)","RPM (1/min)","ROP (ft/h)","Flow Rate (gpm)",]TARGET = "Ts (us/ft_Actual)"PRED_COL = "Ts_Pred"MODELS_DIR = Path("models")1 unmodified lineMODEL_FALLBACKS = [MODELS_DIR / "model.joblib", MODELS_DIR / "model.pkl"]COLORS = {"pred": "#1f77b4", "actual": "#f2b702", "ref": "#5a5a5a"}# Optional env banner from metaSTRICT_VERSION_CHECK = False# ---- Plot sizing ----11 unmodified linesst.markdown("""<style>.brand-logo { width: 200px; height: auto; object-fit: contain; }.centered-container { display: flex; flex-direction: column; align-items: center; text-align: center; }.st-message-box { background-color: #f0f2f6; color: #333; padding: 10px; border-radius: 10px; border: 1px solid #e6e9ef; }.st-message-box.st-success { background-color: #d4edda; color: #155724; border-color: #c3e6cb; }.st-message-box.st-warning { background-color: #fff3cd; color: #856404; border-color: #ffeeba; }.st-message-box.st-error { background-color: #f8d7da; color: #721c24; border-color: #f5c6cb; }.main .block-container { overflow: unset !important; }div[data-testid="stVerticalBlock"] { overflow: unset !important; }div[data-testid="stExpander"] > details > summary {76 unmodified linesxl = pd.ExcelFile(bio)return {sh: xl.parse(sh) for sh in xl.sheet_names}def read_book_bytes(b: bytes):return parse_excel(b) if b else {}# ---- Canonical feature aliasing ------------------------------------------def _build_alias_map(canonical_features: list[str], target_name: str) -> dict:"""Map common header variants -> the *canonical* names in canonical_features.Whatever appears in canonical_features (from ts_meta.json) wins."""def pick(expected_list, variants):for v in variants:if v in expected_list:return vreturn variants[0]can_WOB = pick(canonical_features, ["WOB (klbf)", "WOB, klbf", "WOB(klbf)", "WOB( klbf)"])can_TORQUE = pick(canonical_features, ["Torque (kft.lbf)", "Torque(kft.lbf)", "TORQUE(kft.lbf)"])can_SPP = pick(canonical_features, ["SPP (psi)", "SPP(psi)"])can_RPM = pick(canonical_features, ["RPM (1/min)", "RPM(1/min)"])can_ROP = pick(canonical_features, ["ROP (ft/h)", "ROP(ft/h)"])can_FR = pick(canonical_features, ["Flow Rate (gpm)", "Flow Rate, gpm", "Flow Rate,gpm", "Flow Rate , gpm", "Fow Rate, gpm", "Fow Rate, gpm "])can_DEPTH = "Depth (ft)"alias = {# Features"WOB (klbf)": can_WOB, "WOB, klbf": can_WOB, "WOB(klbf)": can_WOB, "WOB( klbf)": can_WOB,"Torque (kft.lbf)": can_TORQUE, "Torque(kft.lbf)": can_TORQUE, "TORQUE(kft.lbf)": can_TORQUE,"SPP (psi)": can_SPP, "SPP(psi)": can_SPP,"RPM (1/min)": can_RPM, "RPM(1/min)": can_RPM,"ROP (ft/h)": can_ROP, "ROP(ft/h)": can_ROP,"Flow Rate (gpm)": can_FR, "Flow Rate, gpm": can_FR, "Flow Rate,gpm": can_FR, "Flow Rate , gpm": can_FR,"Fow Rate, gpm": can_FR, "Fow Rate, gpm ": can_FR,# Depth (plot only)"Depth (ft)": can_DEPTH, "Depth, ft": can_DEPTH, "Depth(ft)": can_DEPTH, "DEPTH, ft": can_DEPTH,# Target family"Ts (us/ft_Actual)": target_name,"Ts,us/ft_Actual": target_name,"Ts, us/ft_Actual": target_name,"Ts": target_name,"TS_Actual": target_name,"Ts (us/ft)_Actual": target_name,}return alias1 unmodified lineout = df.copy()out.columns = [str(c).strip().replace(" ,", ",").replace(", ", ", ").replace(" ", " ") for c in out.columns]alias = _build_alias_map(canonical_features, target_name)actual = {k: v for k, v in alias.items() if k in out.columns and k != v}return out.rename(columns=actual)24 unmodified lines)st.dataframe(styler, use_container_width=True, hide_index=hide_index)# ---------- Build X exactly as trained ----------def _make_X(df: pd.DataFrame, features: list[str]) -> pd.DataFrame:"""Reindex columns to the exact training feature order and coerce to numeric.Prevents scikit-learn 'feature names should match' errors."""X = df.reindex(columns=features, copy=False)for c in X.columns:X[c] = pd.to_numeric(X[c], errors="coerce")return X# === Excel export helpers =================================================def _excel_engine() -> str:try:272 unmodified lines# ---------- Preview (matplotlib) ----------def preview_tracks(df: pd.DataFrame, cols: list[str]):"""Quick-look multi-track preview:- one subplot per selected column- distinct stable colors per column- shared & reversed Y-axis (Depth downwards)"""cols = [c for c in cols if c in df.columns]n = len(cols)if n == 0:fig, ax = plt.subplots(figsize=(4, 2))ax.text(0.5, 0.5, "No selected columns", ha="center", va="center")ax.axis("off")return fig# Depth or fallback to indexdepth_col = next((c for c in df.columns if 'depth' in str(c).lower()), None)if depth_col is not None:idx = pd.to_numeric(df[depth_col], errors="coerce")y_label = depth_colelse:idx = pd.Series(np.arange(1, len(df) + 1))y_label = "Point Index"y_min, y_max = float(idx.min()), float(idx.max())# Stable qualitative palettecmap = plt.get_cmap("tab20")col_colors = {col: cmap(i % cmap.N) for i, col in enumerate(cols)}fig, axes = plt.subplots(1, n, figsize=(2.3 * n, 7.0), sharey=True, dpi=100)if n == 1:axes = [axes]for i, (ax, col) in enumerate(zip(axes, cols)):x = pd.to_numeric(df[col], errors="coerce")ax.plot(x, idx, '-', lw=1.8, color=col_colors[col])ax.set_xlabel(col)ax.xaxis.set_label_position('top')ax.xaxis.tick_top()ax.set_ylim(y_max, y_min) # reversed Y (Depth down)ax.grid(True, linestyle=":", alpha=0.3)if i == 0:ax.set_ylabel(y_label)else:ax.tick_params(labelleft=False)ax.set_ylabel("")fig.tight_layout()return fig27 unmodified linesst.error(f"Failed to load model: {e}")st.stop()# Load meta (prefer Ts-specific)meta = {}meta_candidates = [MODELS_DIR / "ts_meta.json", MODELS_DIR / "meta.json", MODELS_DIR / "ym_meta.json"]meta_path = next((p for p in meta_candidates if p.exists()), None)6 unmodified linesexcept Exception as e:st.warning(f"Could not parse meta file ({meta_path.name}): {e}")# Optional: version bannerif STRICT_VERSION_CHECK and meta.get("versions"):import numpy as _np, sklearn as _sklmv = meta["versions"]; msg=[]100 unmodified linesst.markdown('<div class="st-message-box st-error">Workbook must include Train/Training/training2 and Test/Testing/testing2 sheets.</div>', unsafe_allow_html=True)st.stop()tr = _normalize_columns(book[sh_train].copy(), FEATURES, TARGET)te = _normalize_columns(book[sh_test].copy(), FEATURES, TARGET)1 unmodified linest.markdown('<div class="st-message-box st-error">Missing required columns.</div>', unsafe_allow_html=True)st.stop()# Predict with exactly the training feature ordertr[PRED_COL] = model.predict(_make_X(tr, FEATURES))te[PRED_COL] = model.predict(_make_X(te, FEATURES))st.session_state.results["Train"]=tr; st.session_state.results["Test"]=test.session_state.results["m_train"]={58 unmodified linesbook = read_book_bytes(up.getvalue())name = find_sheet(book, ["Validation","Validate","validation2","Val","val"]) or list(book.keys())[0]df = _normalize_columns(book[name].copy(), FEATURES, TARGET)if not ensure_cols(df, FEATURES+[TARGET]):st.markdown('<div class="st-message-box st-error">Missing required columns.</div>', unsafe_allow_html=True); st.stop()df[PRED_COL] = model.predict(_make_X(df, FEATURES))st.session_state.results["Validate"]=dfranges = st.session_state.train_ranges; oor_pct = 0.0; tbl=None4 unmodified linestbl = df.loc[any_viol, FEATURES].copy()for c in FEATURES:if pd.api.types.is_numeric_dtype(tbl[c]): tbl[c] = tbl[c].round(2)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)st.session_state.results["m_val"]={"R": pearson_r(df[TARGET], df[PRED_COL]),"RMSE": rmse(df[TARGET], df[PRED_COL]),52 unmodified linesif go_btn and up is not None:book = read_book_bytes(up.getvalue()); name = list(book.keys())[0]df = _normalize_columns(book[name].copy(), FEATURES, TARGET)if not ensure_cols(df, FEATURES):st.markdown('<div class="st-message-box st-error">Missing required columns.</div>', unsafe_allow_html=True); st.stop()df[PRED_COL] = model.predict(_make_X(df, FEATURES))st.session_state.results["PredictOnly"]=dfranges = st.session_state.train_ranges; oor_pct = 0.0
app.py+105-60