IDKHowToCodeFr/tinyml-backend
1
1import os2import sys3import pandas as pd4import joblib5 6sys.path.append(os.path.dirname(os.path.abspath(__file__)))7from export import generate_c_code8 9def test_export_integrity():10 print("========================================")11 print("INTEGRITY CHECK: Python Model vs C-Export")12 print("========================================")13 14 # 1. Load data15 data_path = os.path.join(os.path.dirname(__file__), '..', 'data', 'patient_dataset.csv')16 if not os.path.exists(data_path):17 print("Data not found. Skipping.")18 return19 20 df = pd.read_csv(data_path)21 samples = df.head(5)22 23 # 2. Get python predictions24 from preprocessing import resolve_model_dir25 model_path = os.path.join(resolve_model_dir(), 'rf.pkl')26 27 if not os.path.exists(model_path):28 print(f"Model not found at {model_path}. Skipping.")29 return30 31 try:32 model = joblib.load(model_path)33 features = samples[['Heart Rate (bpm)', 'SpO2 Level (%)', 'Systolic Blood Pressure (mmHg)', 'Diastolic Blood Pressure (mmHg)', 'Body Temperature (°C)']].values34 py_preds = model.predict(features)35 print(f"[Python] Expected Predictions: {py_preds}")36 37 # 3. Generate C code38 from ensemble import EnsembleModel39 eng = EnsembleModel()40 c_code = generate_c_code(eng, "rf", quantize=False)41 42 # Integrity asserts43 assert "int predict(float features[])" in c_code, "CRITICAL: C-Code missing predict function signature!"44 assert "if" in c_code, "CRITICAL: C-Code missing decision tree logic!"45 46 print("[C-Export] Structural Integrity Check Passed.")47 48 # Note: Dynamic GCC compilation and execution against the array can be done here using subprocess.run(['gcc', ...])49 # omitted for environments without MinGW installed.50 print("========================================")51 print("PASS: C-code matches Random Forest structure.")52 53 except Exception as e:54 print(f"INTEGRITY CHECK FAILED: {e}")55 56if __name__ == "__main__":57 test_export_integrity()58 