Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# app_tc.py — ST_Sonic_Tc (Compressional Slowness Tc)
|
| 2 |
|
| 3 |
import io, json, os, base64, math
|
| 4 |
from pathlib import Path
|
|
@@ -15,7 +15,7 @@ import matplotlib.pyplot as plt
|
|
| 15 |
from matplotlib.ticker import FuncFormatter
|
| 16 |
|
| 17 |
import plotly.graph_objects as go
|
| 18 |
-
from sklearn.metrics import mean_squared_error
|
| 19 |
|
| 20 |
# =========================
|
| 21 |
# Constants (Tc variant)
|
|
@@ -63,8 +63,8 @@ st.markdown("""
|
|
| 63 |
.st-message-box.st-success { background-color: #d4edda; color: #155724; border-color: #c3e6cb; }
|
| 64 |
.st-message-box.st-warning { background-color: #fff3cd; color: #856404; border-color: #ffeeba; }
|
| 65 |
.st-message-box.st-error { background-color: #f8d7da; color: #721c24; border-color: #f5c6cb; }
|
| 66 |
-
.main .block-container { overflow: unset !
|
| 67 |
-
div[data-testid="stVerticalBlock"] { overflow: unset !
|
| 68 |
div[data-testid="stExpander"] > details > summary {
|
| 69 |
position: sticky; top: 0; z-index: 10; background: #fff; border-bottom: 1px solid #eee;
|
| 70 |
}
|
|
@@ -135,6 +135,18 @@ def pearson_r(y_true, y_pred) -> float:
|
|
| 135 |
if np.all(a == a[0]) or np.all(p == p[0]): return float("nan")
|
| 136 |
return float(np.corrcoef(a, p)[0, 1])
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
@st.cache_resource(show_spinner=False)
|
| 139 |
def load_model(model_path: str):
|
| 140 |
return joblib.load(model_path)
|
|
@@ -730,12 +742,12 @@ if st.session_state.app_step == "dev":
|
|
| 730 |
st.session_state.results["m_train"]={
|
| 731 |
"R": pearson_r(tr[TARGET], tr[PRED_COL]),
|
| 732 |
"RMSE": rmse(tr[TARGET], tr[PRED_COL]),
|
| 733 |
-
"
|
| 734 |
}
|
| 735 |
st.session_state.results["m_test"]={
|
| 736 |
"R": pearson_r(te[TARGET], te[PRED_COL]),
|
| 737 |
"RMSE": rmse(te[TARGET], te[PRED_COL]),
|
| 738 |
-
"
|
| 739 |
}
|
| 740 |
|
| 741 |
tr_min = tr[FEATURES].min().to_dict(); tr_max = tr[FEATURES].max().to_dict()
|
|
@@ -744,12 +756,14 @@ if st.session_state.app_step == "dev":
|
|
| 744 |
|
| 745 |
def _dev_block(df, m):
|
| 746 |
c1,c2,c3 = st.columns(3)
|
| 747 |
-
c1.metric("R", f"{m['R']:.3f}")
|
|
|
|
|
|
|
| 748 |
st.markdown("""
|
| 749 |
<div style='text-align: left; font-size: 0.8em; color: #6b7280; margin-top: -16px; margin-bottom: 8px;'>
|
| 750 |
<strong>R:</strong> Pearson Correlation Coefficient<br>
|
| 751 |
<strong>RMSE:</strong> Root Mean Square Error<br>
|
| 752 |
-
<strong>
|
| 753 |
</div>
|
| 754 |
""", unsafe_allow_html=True)
|
| 755 |
col_track, col_cross = st.columns([2, 3], gap="large")
|
|
@@ -808,7 +822,7 @@ if st.session_state.app_step == "validate":
|
|
| 808 |
st.session_state.results["m_val"]={
|
| 809 |
"R": pearson_r(df[TARGET], df[PRED_COL]),
|
| 810 |
"RMSE": rmse(df[TARGET], df[PRED_COL]),
|
| 811 |
-
"
|
| 812 |
}
|
| 813 |
st.session_state.results["sv_val"]={"n":len(df), "pred_min":float(df[PRED_COL].min()), "pred_max":float(df[PRED_COL].max()), "oor":oor_pct}
|
| 814 |
st.session_state.results["oor_tbl"]=tbl
|
|
@@ -816,12 +830,14 @@ if st.session_state.app_step == "validate":
|
|
| 816 |
if "Validate" in st.session_state.results:
|
| 817 |
m = st.session_state.results["m_val"]
|
| 818 |
c1,c2,c3 = st.columns(3)
|
| 819 |
-
c1.metric("R", f"{m['R']:.3f}")
|
|
|
|
|
|
|
| 820 |
st.markdown("""
|
| 821 |
<div style='text-align: left; font-size: 0.8em; color: #6b7280; margin-top: -16px; margin-bottom: 8px;'>
|
| 822 |
<strong>R:</strong> Pearson Correlation Coefficient<br>
|
| 823 |
<strong>RMSE:</strong> Root Mean Square Error<br>
|
| 824 |
-
<strong>
|
| 825 |
</div>
|
| 826 |
""", unsafe_allow_html=True)
|
| 827 |
|
|
|
|
| 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
|
|
|
|
| 15 |
from matplotlib.ticker import FuncFormatter
|
| 16 |
|
| 17 |
import plotly.graph_objects as go
|
| 18 |
+
from sklearn.metrics import mean_squared_error
|
| 19 |
|
| 20 |
# =========================
|
| 21 |
# Constants (Tc variant)
|
|
|
|
| 63 |
.st-message-box.st-success { background-color: #d4edda; color: #155724; border-color: #c3e6cb; }
|
| 64 |
.st-message-box.st-warning { background-color: #fff3cd; color: #856404; border-color: #ffeeba; }
|
| 65 |
.st-message-box.st-error { background-color: #f8d7da; color: #721c24; border-color: #f5c6cb; }
|
| 66 |
+
.main .block-container { overflow: unset !important; }
|
| 67 |
+
div[data-testid="stVerticalBlock"] { overflow: unset !important; }
|
| 68 |
div[data-testid="stExpander"] > details > summary {
|
| 69 |
position: sticky; top: 0; z-index: 10; background: #fff; border-bottom: 1px solid #eee;
|
| 70 |
}
|
|
|
|
| 135 |
if np.all(a == a[0]) or np.all(p == p[0]): return float("nan")
|
| 136 |
return float(np.corrcoef(a, p)[0, 1])
|
| 137 |
|
| 138 |
+
def mape(y_true, y_pred) -> float:
|
| 139 |
+
"""
|
| 140 |
+
Mean Absolute Percentage Error in PERCENT.
|
| 141 |
+
Ignores rows where true==0 or non-finite.
|
| 142 |
+
"""
|
| 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 |
+
|
| 150 |
@st.cache_resource(show_spinner=False)
|
| 151 |
def load_model(model_path: str):
|
| 152 |
return joblib.load(model_path)
|
|
|
|
| 742 |
st.session_state.results["m_train"]={
|
| 743 |
"R": pearson_r(tr[TARGET], tr[PRED_COL]),
|
| 744 |
"RMSE": rmse(tr[TARGET], tr[PRED_COL]),
|
| 745 |
+
"MAPE": mape(tr[TARGET], tr[PRED_COL])
|
| 746 |
}
|
| 747 |
st.session_state.results["m_test"]={
|
| 748 |
"R": pearson_r(te[TARGET], te[PRED_COL]),
|
| 749 |
"RMSE": rmse(te[TARGET], te[PRED_COL]),
|
| 750 |
+
"MAPE": mape(te[TARGET], te[PRED_COL])
|
| 751 |
}
|
| 752 |
|
| 753 |
tr_min = tr[FEATURES].min().to_dict(); tr_max = tr[FEATURES].max().to_dict()
|
|
|
|
| 756 |
|
| 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;'>
|
| 764 |
<strong>R:</strong> Pearson Correlation Coefficient<br>
|
| 765 |
<strong>RMSE:</strong> Root Mean Square Error<br>
|
| 766 |
+
<strong>MAPE:</strong> Mean Absolute Percentage Error
|
| 767 |
</div>
|
| 768 |
""", unsafe_allow_html=True)
|
| 769 |
col_track, col_cross = st.columns([2, 3], gap="large")
|
|
|
|
| 822 |
st.session_state.results["m_val"]={
|
| 823 |
"R": pearson_r(df[TARGET], df[PRED_COL]),
|
| 824 |
"RMSE": rmse(df[TARGET], df[PRED_COL]),
|
| 825 |
+
"MAPE": mape(df[TARGET], df[PRED_COL])
|
| 826 |
}
|
| 827 |
st.session_state.results["sv_val"]={"n":len(df), "pred_min":float(df[PRED_COL].min()), "pred_max":float(df[PRED_COL].max()), "oor":oor_pct}
|
| 828 |
st.session_state.results["oor_tbl"]=tbl
|
|
|
|
| 830 |
if "Validate" in st.session_state.results:
|
| 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;'>
|
| 838 |
<strong>R:</strong> Pearson Correlation Coefficient<br>
|
| 839 |
<strong>RMSE:</strong> Root Mean Square Error<br>
|
| 840 |
+
<strong>MAPE:</strong> Mean Absolute Percentage Error
|
| 841 |
</div>
|
| 842 |
""", unsafe_allow_html=True)
|
| 843 |
|