Update app.py
Browse files
app.py
CHANGED
|
@@ -171,6 +171,28 @@ def build_survival_targets(df: pd.DataFrame):
|
|
| 171 |
|
| 172 |
|
| 173 |
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
def norm_col(s: str) -> str:
|
| 176 |
"""
|
|
@@ -2326,7 +2348,12 @@ with tab_predict:
|
|
| 2326 |
" ".join(str(c).replace("\u00A0"," ").split())
|
| 2327 |
for c in st.session_state.df_inf.columns
|
| 2328 |
]
|
| 2329 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2330 |
st.session_state.df_inf = add_ethnicity_region(
|
| 2331 |
st.session_state.df_inf,
|
| 2332 |
"Ethnicity",
|
|
|
|
| 171 |
|
| 172 |
|
| 173 |
import re
|
| 174 |
+
def align_columns_to_schema(df: pd.DataFrame, required_cols: list[str]) -> pd.DataFrame:
|
| 175 |
+
"""
|
| 176 |
+
Rename df columns to match required_cols using normalized matching.
|
| 177 |
+
This fixes newline/space/underscore/case differences.
|
| 178 |
+
"""
|
| 179 |
+
# normalize inference columns
|
| 180 |
+
df_cols = list(df.columns)
|
| 181 |
+
norm_to_actual = {norm_col(c): c for c in df_cols}
|
| 182 |
+
|
| 183 |
+
rename_map = {}
|
| 184 |
+
for req in required_cols:
|
| 185 |
+
k = norm_col(req)
|
| 186 |
+
if k in norm_to_actual:
|
| 187 |
+
src = norm_to_actual[k]
|
| 188 |
+
if src != req:
|
| 189 |
+
rename_map[src] = req
|
| 190 |
+
|
| 191 |
+
if rename_map:
|
| 192 |
+
df = df.rename(columns=rename_map)
|
| 193 |
+
|
| 194 |
+
return df
|
| 195 |
+
|
| 196 |
|
| 197 |
def norm_col(s: str) -> str:
|
| 198 |
"""
|
|
|
|
| 2348 |
" ".join(str(c).replace("\u00A0"," ").split())
|
| 2349 |
for c in st.session_state.df_inf.columns
|
| 2350 |
]
|
| 2351 |
+
|
| 2352 |
+
# ✅ ALIGN TO TRAINED MODEL SCHEMA (THIS FIXES YOUR ERROR)
|
| 2353 |
+
st.session_state.df_inf = align_columns_to_schema(
|
| 2354 |
+
st.session_state.df_inf,
|
| 2355 |
+
meta["schema"]["features"]
|
| 2356 |
+
|
| 2357 |
st.session_state.df_inf = add_ethnicity_region(
|
| 2358 |
st.session_state.df_inf,
|
| 2359 |
"Ethnicity",
|