Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- backend/ensemble.py +4 -1
- backend/preprocessing.py +10 -4
- tests/test_preprocessing.py +12 -1
backend/ensemble.py
CHANGED
|
@@ -30,7 +30,10 @@ class EnsembleModel:
|
|
| 30 |
for name in self.model_names:
|
| 31 |
path = os.path.join(self.model_dir, f'{name}.pkl')
|
| 32 |
if os.path.exists(path):
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def decode_label(self, idx: int) -> str:
|
| 36 |
"""Safely maps class index to clinical label without throwing on unseen labels."""
|
|
|
|
| 30 |
for name in self.model_names:
|
| 31 |
path = os.path.join(self.model_dir, f'{name}.pkl')
|
| 32 |
if os.path.exists(path):
|
| 33 |
+
try:
|
| 34 |
+
self.models[name] = joblib.load(path)
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"Warning: Failed to load model {name}: {e}")
|
| 37 |
|
| 38 |
def decode_label(self, idx: int) -> str:
|
| 39 |
"""Safely maps class index to clinical label without throwing on unseen labels."""
|
backend/preprocessing.py
CHANGED
|
@@ -142,12 +142,18 @@ def preprocess_data(df, is_training=False, model_dir=None):
|
|
| 142 |
joblib.dump(scaler, scaler_path)
|
| 143 |
else:
|
| 144 |
if os.path.exists(imputer_path):
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
if os.path.exists(scaler_path):
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
return X_raw, y
|
| 153 |
|
|
|
|
| 142 |
joblib.dump(scaler, scaler_path)
|
| 143 |
else:
|
| 144 |
if os.path.exists(imputer_path):
|
| 145 |
+
try:
|
| 146 |
+
imputer = joblib.load(imputer_path)
|
| 147 |
+
X_raw[continuous_features] = imputer.transform(X_raw[continuous_features])
|
| 148 |
+
except Exception:
|
| 149 |
+
pass
|
| 150 |
|
| 151 |
if os.path.exists(scaler_path):
|
| 152 |
+
try:
|
| 153 |
+
scaler = joblib.load(scaler_path)
|
| 154 |
+
X_raw[continuous_features] = scaler.transform(X_raw[continuous_features])
|
| 155 |
+
except Exception:
|
| 156 |
+
pass
|
| 157 |
|
| 158 |
return X_raw, y
|
| 159 |
|
tests/test_preprocessing.py
CHANGED
|
@@ -56,6 +56,17 @@ def test_preprocess_data_inference(tmp_path):
|
|
| 56 |
|
| 57 |
def test_preprocess_data_alias_normalization(tmp_path):
|
| 58 |
"""Test that schema field names (e.g. Heart_Rate) are automatically normalized without error."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
df = pd.DataFrame({
|
| 60 |
'Heart_Rate': [115],
|
| 61 |
'SpO2_Level': [92],
|
|
@@ -63,7 +74,7 @@ def test_preprocess_data_alias_normalization(tmp_path):
|
|
| 63 |
'Diastolic_BP': [92],
|
| 64 |
'Body_Temp': [37.4],
|
| 65 |
})
|
| 66 |
-
X, y = preprocess_data(df, is_training=False)
|
| 67 |
assert 'Risk_Severity' in X.columns
|
| 68 |
assert 'Heart Rate (bpm)' in X.columns
|
| 69 |
assert not X.isnull().values.any()
|
|
|
|
| 56 |
|
| 57 |
def test_preprocess_data_alias_normalization(tmp_path):
|
| 58 |
"""Test that schema field names (e.g. Heart_Rate) are automatically normalized without error."""
|
| 59 |
+
# First fit on dummy data inside tmp_path
|
| 60 |
+
train_df = pd.DataFrame({
|
| 61 |
+
'Heart Rate (bpm)': [80, 90],
|
| 62 |
+
'SpO2 Level (%)': [98, 97],
|
| 63 |
+
'Systolic Blood Pressure (mmHg)': [120, 125],
|
| 64 |
+
'Diastolic Blood Pressure (mmHg)': [80, 85],
|
| 65 |
+
'Body Temperature (°C)': [37.0, 37.2],
|
| 66 |
+
'Predicted Disease': ['Normal', 'Asthma']
|
| 67 |
+
})
|
| 68 |
+
preprocess_data(train_df, is_training=True, model_dir=str(tmp_path))
|
| 69 |
+
|
| 70 |
df = pd.DataFrame({
|
| 71 |
'Heart_Rate': [115],
|
| 72 |
'SpO2_Level': [92],
|
|
|
|
| 74 |
'Diastolic_BP': [92],
|
| 75 |
'Body_Temp': [37.4],
|
| 76 |
})
|
| 77 |
+
X, y = preprocess_data(df, is_training=False, model_dir=str(tmp_path))
|
| 78 |
assert 'Risk_Severity' in X.columns
|
| 79 |
assert 'Heart Rate (bpm)' in X.columns
|
| 80 |
assert not X.isnull().values.any()
|