UCS2014 commited on
Commit
af9afd8
·
verified ·
1 Parent(s): 45b27aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -39
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # app_tc.py — ST_Sonic_Tc (Compressional Slowness Tc) — MAPE version
2
 
3
  import io, json, os, base64, math
4
  from pathlib import Path
@@ -18,12 +18,12 @@ import plotly.graph_objects as go
18
  from sklearn.metrics import mean_squared_error
19
 
20
  # =========================
21
- # Constants (Tc variant)
22
  # =========================
23
- APP_NAME = "ST_Log_Sonic (Tc)"
24
- TAGLINE = "Real-Time Compressional Slowness (Tc) Prediction"
25
 
26
- # Defaults (overridden by tc_meta.json if present)
27
  FEATURES = [
28
  "WOB (klbf)",
29
  "Torque (kft.lbf)",
@@ -32,11 +32,11 @@ FEATURES = [
32
  "ROP (ft/h)",
33
  "Flow Rate (gpm)",
34
  ]
35
- TARGET = "Tc (us/ft_Actual)"
36
- PRED_COL = "Tc_Pred"
37
 
38
  MODELS_DIR = Path("models")
39
- DEFAULT_MODEL = MODELS_DIR / "tc_model.joblib"
40
  MODEL_FALLBACKS = [MODELS_DIR / "model.joblib", MODELS_DIR / "model.pkl"]
41
  COLORS = {"pred": "#1f77b4", "actual": "#f2b702", "ref": "#5a5a5a"}
42
 
@@ -143,7 +143,7 @@ def mape(y_true, y_pred) -> float:
143
  a = np.asarray(y_true, dtype=float)
144
  p = np.asarray(y_pred, dtype=float)
145
  mask = np.isfinite(a) & np.isfinite(p) & (a != 0)
146
- if not np.any(mask):
147
  return float("nan")
148
  return float(np.mean(np.abs((p[mask] - a[mask]) / a[mask])) * 100.0)
149
 
@@ -164,7 +164,7 @@ def read_book_bytes(b: bytes):
164
  def _build_alias_map(canonical_features: list[str], target_name: str) -> dict:
165
  """
166
  Map common header variants -> the *canonical* names in canonical_features.
167
- Whatever appears in canonical_features (from tc_meta.json) wins.
168
  """
169
  def pick(expected_list, variants):
170
  for v in variants:
@@ -193,13 +193,13 @@ def _build_alias_map(canonical_features: list[str], target_name: str) -> dict:
193
  # Depth (plot only)
194
  "Depth (ft)": can_DEPTH, "Depth, ft": can_DEPTH, "Depth(ft)": can_DEPTH, "DEPTH, ft": can_DEPTH,
195
 
196
- # Target family (Tc)
197
- "Tc (us/ft_Actual)": target_name,
198
- "Tc,us/ft_Actual": target_name,
199
- "Tc, us/ft_Actual": target_name,
200
- "Tc": target_name,
201
- "TC_Actual": target_name,
202
- "Tc (us/ft)_Actual": target_name,
203
  }
204
  return alias
205
 
@@ -377,7 +377,7 @@ def build_export_workbook(selected: list[str], ndigits: int = 3, do_autofit: boo
377
  if do_autofit:
378
  _excel_autofit(writer, sheet, df)
379
  bio.seek(0)
380
- fname = f"TC_Export_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx"
381
  return bio.getvalue(), fname, order
382
 
383
  # --------- SIMPLE export UI ----------
@@ -402,7 +402,7 @@ def render_export_button(phase_key: str) -> None:
402
  st.download_button(
403
  label="⬇️ Export Excel",
404
  data=b"",
405
- file_name="TC_Export.xlsx",
406
  mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
407
  disabled=True,
408
  key=f"download_{phase_key}",
@@ -415,7 +415,7 @@ def render_export_button(phase_key: str) -> None:
415
  st.download_button(
416
  "⬇️ Export Excel",
417
  data=(data or b""),
418
- file_name=(fname or "TC_Export.xlsx"),
419
  mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
420
  disabled=(data is None),
421
  key=f"download_{phase_key}",
@@ -424,7 +424,7 @@ def render_export_button(phase_key: str) -> None:
424
  # =========================
425
  # Cross plot (Matplotlib)
426
  # =========================
427
- def cross_plot_static(actual, pred, xlabel="Actual Tc (µs/ft)", ylabel="Predicted Tc (µs/ft)"):
428
  a = pd.Series(actual, dtype=float)
429
  p = pd.Series(pred, dtype=float)
430
 
@@ -486,14 +486,14 @@ def track_plot(df, include_actual=True):
486
  x=df[PRED_COL], y=y, mode="lines",
487
  line=dict(color=COLORS["pred"], width=1.8),
488
  name=PRED_COL,
489
- hovertemplate=f"{PRED_COL}: "+"%{x:.0f}<br>"+ylab+": %{y}<extra></extra>"
490
  ))
491
  if include_actual and TARGET in df.columns:
492
  fig.add_trace(go.Scatter(
493
  x=df[TARGET], y=y, mode="lines",
494
  line=dict(color=COLORS["actual"], width=2.0, dash="dot"),
495
  name=f"{TARGET} (actual)",
496
- hovertemplate=f"{TARGET}: "+"%{x:.0f}<br>"+ylab+": %{y}<extra></extra>"
497
  ))
498
 
499
  fig.update_layout(
@@ -506,11 +506,11 @@ def track_plot(df, include_actual=True):
506
  legend_title_text=""
507
  )
508
  fig.update_xaxes(
509
- title_text="Tc (μs/ft)",
510
  title_font=dict(size=20, family=BOLD_FONT, color="#000"),
511
  tickfont=dict(size=15, family=BOLD_FONT, color="#000"),
512
  side="top", range=[xmin, xmax],
513
- ticks="outside", tickformat=",.0f", tickmode="auto", tick0=tick0,
514
  showline=True, linewidth=1.2, linecolor="#444", mirror=True,
515
  showgrid=True, gridcolor="rgba(0,0,0,0.12)", automargin=True
516
  )
@@ -599,7 +599,7 @@ def ensure_model() -> Path|None:
599
 
600
  mpath = ensure_model()
601
  if not mpath:
602
- st.error("Model not found. Upload models/tc_model.joblib (or set MODEL_URL).")
603
  st.stop()
604
  try:
605
  model = load_model(str(mpath))
@@ -607,9 +607,9 @@ except Exception as e:
607
  st.error(f"Failed to load model: {e}")
608
  st.stop()
609
 
610
- # Load meta (prefer Tc-specific)
611
  meta = {}
612
- meta_candidates = [MODELS_DIR / "tc_meta.json", MODELS_DIR / "meta.json"]
613
  meta_path = next((p for p in meta_candidates if p.exists()), None)
614
  if meta_path:
615
  try:
@@ -617,6 +617,9 @@ if meta_path:
617
  FEATURES = meta.get("features", FEATURES)
618
  TARGET = meta.get("target", TARGET)
619
  PRED_COL = meta.get("pred_col", PRED_COL)
 
 
 
620
  except Exception as e:
621
  st.warning(f"Could not parse meta file ({meta_path.name}): {e}")
622
 
@@ -636,7 +639,7 @@ if STRICT_VERSION_CHECK and meta.get("versions"):
636
  # =========================
637
  st.session_state.setdefault("app_step", "intro")
638
  st.session_state.setdefault("results", {})
639
- st.session_state.setdefault("train_ranges", None)
640
  st.session_state.setdefault("dev_file_name","")
641
  st.session_state.setdefault("dev_file_bytes",b"")
642
  st.session_state.setdefault("dev_file_loaded",False)
@@ -677,12 +680,12 @@ def sticky_header(title, message):
677
  # =========================
678
  if st.session_state.app_step == "intro":
679
  st.header("Welcome!")
680
- st.markdown("This software is developed by *Smart Thinking AI-Solutions Team* to estimate **Compressional Slowness (Tc)** from drilling data.")
681
  st.subheader("How It Works")
682
  st.markdown(
683
  "1) **Upload your data to build the case and preview the model performance.** \n"
684
  "2) Click **Run Model** to compute metrics and plots. \n"
685
- "3) **Proceed to Validation** (with actual Tc) or **Proceed to Prediction** (no Tc)."
686
  )
687
  if st.button("Start Showcase", type="primary"):
688
  st.session_state.app_step = "dev"; st.rerun()
@@ -757,7 +760,7 @@ if st.session_state.app_step == "dev":
757
  def _dev_block(df, m):
758
  c1,c2,c3 = st.columns(3)
759
  c1.metric("R", f"{m['R']:.3f}")
760
- c2.metric("RMSE", f"{m['RMSE']:.2f}")
761
  c3.metric("MAPE (%)", f"{m['MAPE']:.2f}")
762
  st.markdown("""
763
  <div style='text-align: left; font-size: 0.8em; color: #6b7280; margin-top: -16px; margin-bottom: 8px;'>
@@ -781,7 +784,7 @@ if st.session_state.app_step == "dev":
781
  render_export_button(phase_key="dev")
782
 
783
  # =========================
784
- # VALIDATION (with actual Tc)
785
  # =========================
786
  if st.session_state.app_step == "validate":
787
  st.sidebar.header("Validate the Model")
@@ -797,7 +800,7 @@ if st.session_state.app_step == "validate":
797
  if st.sidebar.button("⬅ Back to Case Building", use_container_width=True): st.session_state.app_step="dev"; st.rerun()
798
  if st.sidebar.button("Proceed to Prediction ▶", use_container_width=True): st.session_state.app_step="predict"; st.rerun()
799
 
800
- sticky_header("Validate the Model", "Upload a dataset with the same **features** and **Tc** to evaluate performance.")
801
 
802
  if go_btn and up is not None:
803
  book = read_book_bytes(up.getvalue())
@@ -815,7 +818,7 @@ if st.session_state.app_step == "validate":
815
  if any_viol.any():
816
  tbl = df.loc[any_viol, FEATURES].copy()
817
  for c in FEATURES:
818
- if pd.api.types.is_numeric_dtype(tbl[c]): tbl[c] = tbl[c].round(2)
819
  tbl["Violations"] = pd.DataFrame({f:(df[f]<ranges[f][0])|(df[f]>ranges[f][1]) for f in FEATURES}).loc[any_viol].apply(
820
  lambda r:", ".join([c for c,v in r.items() if v]), axis=1
821
  )
@@ -831,7 +834,7 @@ if st.session_state.app_step == "validate":
831
  m = st.session_state.results["m_val"]
832
  c1,c2,c3 = st.columns(3)
833
  c1.metric("R", f"{m['R']:.3f}")
834
- c2.metric("RMSE", f"{m['RMSE']:.2f}")
835
  c3.metric("MAPE (%)", f"{m['MAPE']:.2f}")
836
  st.markdown("""
837
  <div style='text-align: left; font-size: 0.8em; color: #6b7280; margin-top: -16px; margin-bottom: 8px;'>
@@ -859,10 +862,10 @@ if st.session_state.app_step == "validate":
859
  df_centered_rounded(st.session_state.results["oor_tbl"])
860
 
861
  # =========================
862
- # PREDICTION (no actual Tc)
863
  # =========================
864
  if st.session_state.app_step == "predict":
865
- st.sidebar.header("Prediction (No Actual Tc)")
866
  up = st.sidebar.file_uploader("Upload Prediction Excel", type=["xlsx","xls"])
867
  if up is not None:
868
  book = read_book_bytes(up.getvalue())
@@ -874,7 +877,7 @@ if st.session_state.app_step == "predict":
874
  go_btn = st.sidebar.button("Predict", type="primary", use_container_width=True)
875
  if st.sidebar.button("⬅ Back to Case Building", use_container_width=True): st.session_state.app_step="dev"; st.rerun()
876
 
877
- sticky_header("Prediction", "Upload a dataset with the feature columns (no **Tc**).")
878
 
879
  if go_btn and up is not None:
880
  book = read_book_bytes(up.getvalue()); name = list(book.keys())[0]
 
1
+ # app_rhob.py — ST_Log_RHOB (Formation Bulk Density, g/cc) — MAPE version
2
 
3
  import io, json, os, base64, math
4
  from pathlib import Path
 
18
  from sklearn.metrics import mean_squared_error
19
 
20
  # =========================
21
+ # Constants (RHOB variant)
22
  # =========================
23
+ APP_NAME = "ST_Log_RHOB"
24
+ TAGLINE = "Real-Time Formation Bulk Density (RHOB) Prediction"
25
 
26
+ # Defaults (overridden by rhob_meta.json if present)
27
  FEATURES = [
28
  "WOB (klbf)",
29
  "Torque (kft.lbf)",
 
32
  "ROP (ft/h)",
33
  "Flow Rate (gpm)",
34
  ]
35
+ TARGET = "RHOB" # canonical target name
36
+ PRED_COL = "RHOB_Pred"
37
 
38
  MODELS_DIR = Path("models")
39
+ DEFAULT_MODEL = MODELS_DIR / "rhob_model.joblib"
40
  MODEL_FALLBACKS = [MODELS_DIR / "model.joblib", MODELS_DIR / "model.pkl"]
41
  COLORS = {"pred": "#1f77b4", "actual": "#f2b702", "ref": "#5a5a5a"}
42
 
 
143
  a = np.asarray(y_true, dtype=float)
144
  p = np.asarray(y_pred, dtype=float)
145
  mask = np.isfinite(a) & np.isfinite(p) & (a != 0)
146
+ if not np.any(mask):
147
  return float("nan")
148
  return float(np.mean(np.abs((p[mask] - a[mask]) / a[mask])) * 100.0)
149
 
 
164
  def _build_alias_map(canonical_features: list[str], target_name: str) -> dict:
165
  """
166
  Map common header variants -> the *canonical* names in canonical_features.
167
+ Whatever appears in canonical_features (from rhob_meta.json) wins.
168
  """
169
  def pick(expected_list, variants):
170
  for v in variants:
 
193
  # Depth (plot only)
194
  "Depth (ft)": can_DEPTH, "Depth, ft": can_DEPTH, "Depth(ft)": can_DEPTH, "DEPTH, ft": can_DEPTH,
195
 
196
+ # Target family (RHOB)
197
+ "RHOB": target_name,
198
+ "RHOB (g/cc)": target_name,
199
+ "RHOB_Actual": target_name,
200
+ "RHOB (g/cc)_Actual": target_name,
201
+ "RhoB": target_name,
202
+ "RhoB (g/cc)": target_name,
203
  }
204
  return alias
205
 
 
377
  if do_autofit:
378
  _excel_autofit(writer, sheet, df)
379
  bio.seek(0)
380
+ fname = f"RHOB_Export_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx"
381
  return bio.getvalue(), fname, order
382
 
383
  # --------- SIMPLE export UI ----------
 
402
  st.download_button(
403
  label="⬇️ Export Excel",
404
  data=b"",
405
+ file_name="RHOB_Export.xlsx",
406
  mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
407
  disabled=True,
408
  key=f"download_{phase_key}",
 
415
  st.download_button(
416
  "⬇️ Export Excel",
417
  data=(data or b""),
418
+ file_name=(fname or "RHOB_Export.xlsx"),
419
  mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
420
  disabled=(data is None),
421
  key=f"download_{phase_key}",
 
424
  # =========================
425
  # Cross plot (Matplotlib)
426
  # =========================
427
+ def cross_plot_static(actual, pred, xlabel="Actual RHOB (g/cc)", ylabel="Predicted RHOB (g/cc)"):
428
  a = pd.Series(actual, dtype=float)
429
  p = pd.Series(pred, dtype=float)
430
 
 
486
  x=df[PRED_COL], y=y, mode="lines",
487
  line=dict(color=COLORS["pred"], width=1.8),
488
  name=PRED_COL,
489
+ hovertemplate=f"{PRED_COL}: "+"%{x:.3f}<br>"+ylab+": %{y}<extra></extra>"
490
  ))
491
  if include_actual and TARGET in df.columns:
492
  fig.add_trace(go.Scatter(
493
  x=df[TARGET], y=y, mode="lines",
494
  line=dict(color=COLORS["actual"], width=2.0, dash="dot"),
495
  name=f"{TARGET} (actual)",
496
+ hovertemplate=f"{TARGET}: "+"%{x:.3f}<br>"+ylab+": %{y}<extra></extra>"
497
  ))
498
 
499
  fig.update_layout(
 
506
  legend_title_text=""
507
  )
508
  fig.update_xaxes(
509
+ title_text="RHOB (g/cc)",
510
  title_font=dict(size=20, family=BOLD_FONT, color="#000"),
511
  tickfont=dict(size=15, family=BOLD_FONT, color="#000"),
512
  side="top", range=[xmin, xmax],
513
+ ticks="outside", tickformat=",.3f", tickmode="auto", tick0=tick0,
514
  showline=True, linewidth=1.2, linecolor="#444", mirror=True,
515
  showgrid=True, gridcolor="rgba(0,0,0,0.12)", automargin=True
516
  )
 
599
 
600
  mpath = ensure_model()
601
  if not mpath:
602
+ st.error("Model not found. Upload models/rhob_model.joblib (or set MODEL_URL).")
603
  st.stop()
604
  try:
605
  model = load_model(str(mpath))
 
607
  st.error(f"Failed to load model: {e}")
608
  st.stop()
609
 
610
+ # Load meta (prefer RHOB-specific)
611
  meta = {}
612
+ meta_candidates = [MODELS_DIR / "rhob_meta.json", MODELS_DIR / "meta.json"]
613
  meta_path = next((p for p in meta_candidates if p.exists()), None)
614
  if meta_path:
615
  try:
 
617
  FEATURES = meta.get("features", FEATURES)
618
  TARGET = meta.get("target", TARGET)
619
  PRED_COL = meta.get("pred_col", PRED_COL)
620
+ # if training ranges were saved in meta, seed them so OOR works before any dev step
621
+ if isinstance(meta.get("train_ranges"), dict) and "train_ranges" not in st.session_state:
622
+ st.session_state["train_ranges"] = meta["train_ranges"]
623
  except Exception as e:
624
  st.warning(f"Could not parse meta file ({meta_path.name}): {e}")
625
 
 
639
  # =========================
640
  st.session_state.setdefault("app_step", "intro")
641
  st.session_state.setdefault("results", {})
642
+ st.session_state.setdefault("train_ranges", st.session_state.get("train_ranges", None))
643
  st.session_state.setdefault("dev_file_name","")
644
  st.session_state.setdefault("dev_file_bytes",b"")
645
  st.session_state.setdefault("dev_file_loaded",False)
 
680
  # =========================
681
  if st.session_state.app_step == "intro":
682
  st.header("Welcome!")
683
+ st.markdown("This software is developed by *Smart Thinking AI-Solutions Team* to estimate **RHOB (Bulk Density)** from drilling data.")
684
  st.subheader("How It Works")
685
  st.markdown(
686
  "1) **Upload your data to build the case and preview the model performance.** \n"
687
  "2) Click **Run Model** to compute metrics and plots. \n"
688
+ "3) **Proceed to Validation** (with actual RHOB) or **Proceed to Prediction** (no RHOB)."
689
  )
690
  if st.button("Start Showcase", type="primary"):
691
  st.session_state.app_step = "dev"; st.rerun()
 
760
  def _dev_block(df, m):
761
  c1,c2,c3 = st.columns(3)
762
  c1.metric("R", f"{m['R']:.3f}")
763
+ c2.metric("RMSE", f"{m['RMSE']:.3f}")
764
  c3.metric("MAPE (%)", f"{m['MAPE']:.2f}")
765
  st.markdown("""
766
  <div style='text-align: left; font-size: 0.8em; color: #6b7280; margin-top: -16px; margin-bottom: 8px;'>
 
784
  render_export_button(phase_key="dev")
785
 
786
  # =========================
787
+ # VALIDATION (with actual RHOB)
788
  # =========================
789
  if st.session_state.app_step == "validate":
790
  st.sidebar.header("Validate the Model")
 
800
  if st.sidebar.button("⬅ Back to Case Building", use_container_width=True): st.session_state.app_step="dev"; st.rerun()
801
  if st.sidebar.button("Proceed to Prediction ▶", use_container_width=True): st.session_state.app_step="predict"; st.rerun()
802
 
803
+ sticky_header("Validate the Model", "Upload a dataset with the same **features** and **RHOB** to evaluate performance.")
804
 
805
  if go_btn and up is not None:
806
  book = read_book_bytes(up.getvalue())
 
818
  if any_viol.any():
819
  tbl = df.loc[any_viol, FEATURES].copy()
820
  for c in FEATURES:
821
+ if pd.api.types.is_numeric_dtype(tbl[c]): tbl[c] = tbl[c].round(3)
822
  tbl["Violations"] = pd.DataFrame({f:(df[f]<ranges[f][0])|(df[f]>ranges[f][1]) for f in FEATURES}).loc[any_viol].apply(
823
  lambda r:", ".join([c for c,v in r.items() if v]), axis=1
824
  )
 
834
  m = st.session_state.results["m_val"]
835
  c1,c2,c3 = st.columns(3)
836
  c1.metric("R", f"{m['R']:.3f}")
837
+ c2.metric("RMSE", f"{m['RMSE']:.3f}")
838
  c3.metric("MAPE (%)", f"{m['MAPE']:.2f}")
839
  st.markdown("""
840
  <div style='text-align: left; font-size: 0.8em; color: #6b7280; margin-top: -16px; margin-bottom: 8px;'>
 
862
  df_centered_rounded(st.session_state.results["oor_tbl"])
863
 
864
  # =========================
865
+ # PREDICTION (no actual RHOB)
866
  # =========================
867
  if st.session_state.app_step == "predict":
868
+ st.sidebar.header("Prediction (No Actual RHOB)")
869
  up = st.sidebar.file_uploader("Upload Prediction Excel", type=["xlsx","xls"])
870
  if up is not None:
871
  book = read_book_bytes(up.getvalue())
 
877
  go_btn = st.sidebar.button("Predict", type="primary", use_container_width=True)
878
  if st.sidebar.button("⬅ Back to Case Building", use_container_width=True): st.session_state.app_step="dev"; st.rerun()
879
 
880
+ sticky_header("Prediction", "Upload a dataset with the feature columns (no **RHOB**).")
881
 
882
  if go_btn and up is not None:
883
  book = read_book_bytes(up.getvalue()); name = list(book.keys())[0]