Spaces:
Sleeping
Sleeping
| """ | |
| Advanced Medical Feature Engineering and Clinical Metric Interpreter. | |
| Handles 21-input scaling pipelines, BMI triage math, and multi-modal feature vectors. | |
| """ | |
| import pandas as pd | |
| import numpy as np | |
| class ClinicalFeatureEngineer: | |
| def __init__(self): | |
| # Full explicit 21-feature mapping matching the BRFSS2015 indicator topology | |
| self.required_features = [ | |
| 'HighBP', 'HighChol', 'CholCheck', 'BMI', 'Smoker', 'Stroke', | |
| 'Diabetes', 'PhysActivity', 'Fruits', 'Veggies', 'HvyAlcoholConsump', | |
| 'AnyHealthcare', 'NoDocbcCost', 'GenHlth', 'MentHlth', 'PhysHlth', | |
| 'DiffWalk', 'Sex', 'Age', 'Education', 'Income' | |
| ] | |
| def compute_bmi_metrics(self, weight_kg, height_cm): | |
| """Calculates exact BMI and returns clinical classification boundaries.""" | |
| if height_cm <= 0 or weight_kg <= 0: | |
| return 0.0, "Unknown Parameters" | |
| height_m = height_cm / 100.0 | |
| bmi = float(weight_kg / (height_m ** 2)) | |
| if bmi < 18.5: | |
| category = "Underweight (Increased Risk)" | |
| elif 18.5 <= bmi < 25.0: | |
| category = "Normal Weight (Optimal Range)" | |
| elif 25.0 <= bmi < 30.0: | |
| category = "Overweight (Monitored Base)" | |
| else: | |
| category = "Obese (Severe Cardiovascular Risk Factor)" | |
| return round(bmi, 2), category | |
| def compute_engineered_metrics(self, raw_df): | |
| """Maps full 21-input arrays to deep structural interaction metrics.""" | |
| df = raw_df.copy() | |
| # Ensure all columns exist, fill default values if absent | |
| for col in self.required_features: | |
| if col not in df.columns: | |
| df[col] = 0.0 | |
| # High-order medical risk interactions | |
| df['BMI_BP_Interaction'] = df['BMI'] * df['HighBP'] | |
| df['Health_Risk_Score'] = ( | |
| df['HighBP'] + df['HighChol'] + df['Smoker'] + | |
| df['Stroke'] + df['Diabetes'] + df['DiffWalk'] | |
| ).astype(float) | |
| extended_columns = self.required_features + ['BMI_BP_Interaction', 'Health_Risk_Score'] | |
| return df[extended_columns] |