File size: 2,878 Bytes
9596562
 
 
 
 
 
2a9d346
9596562
 
 
 
 
 
aad3f84
9596562
 
 
 
 
 
 
 
 
aad3f84
 
9596562
 
 
 
aad3f84
9596562
aad3f84
 
 
 
 
 
 
 
 
 
 
 
9596562
 
 
 
 
 
 
 
aad3f84
9596562
 
 
aad3f84
 
 
0db9e5f
 
 
 
 
 
 
 
 
 
 
aad3f84
 
 
 
 
 
 
0db9e5f
aad3f84
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import pytest
import pandas as pd
import numpy as np
import os
import sys

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../backend')))
from preprocessing import preprocess_data, resolve_model_dir

def test_resolve_model_dir():
    md = resolve_model_dir()
    assert md.endswith("model")

def test_preprocess_data_training(tmp_path):
    df = pd.DataFrame({
        'Heart Rate (bpm)': [80, 90, np.nan],
        'SpO2 Level (%)': [98, 97, 95],
        'Systolic Blood Pressure (mmHg)': [120, 125, 130],
        'Diastolic Blood Pressure (mmHg)': [80, 85, 90],
        'Body Temperature (°C)': [37.0, 37.2, 37.5],
        'Predicted Disease': ['Normal', 'Asthma', 'Normal']
    })
    
    # Use tmp_path to isolate test artifacts from production models
    X, y = preprocess_data(df, is_training=True, model_dir=str(tmp_path))
    
    assert not X.isnull().values.any()
    assert len(y) == 3
    assert 'Risk_Severity' in X.columns
    assert os.path.exists(os.path.join(str(tmp_path), 'label_encoder.pkl'))

def test_preprocess_data_inference(tmp_path):
    # First fit on dummy data inside tmp_path
    train_df = pd.DataFrame({
        'Heart Rate (bpm)': [80, 90],
        'SpO2 Level (%)': [98, 97],
        'Systolic Blood Pressure (mmHg)': [120, 125],
        'Diastolic Blood Pressure (mmHg)': [80, 85],
        'Body Temperature (°C)': [37.0, 37.2],
        'Predicted Disease': ['Normal', 'Asthma']
    })
    preprocess_data(train_df, is_training=True, model_dir=str(tmp_path))
    
    df = pd.DataFrame({
        'Heart Rate (bpm)': [100],
        'SpO2 Level (%)': [99],
        'Systolic Blood Pressure (mmHg)': [120],
        'Diastolic Blood Pressure (mmHg)': [80],
        'Body Temperature (°C)': [37.0],
    })
    
    X, y = preprocess_data(df, is_training=False, model_dir=str(tmp_path))
    
    assert y is None
    assert 'Risk_Severity' in X.columns

def test_preprocess_data_alias_normalization(tmp_path):
    """Test that schema field names (e.g. Heart_Rate) are automatically normalized without error."""
    # First fit on dummy data inside tmp_path
    train_df = pd.DataFrame({
        'Heart Rate (bpm)': [80, 90],
        'SpO2 Level (%)': [98, 97],
        'Systolic Blood Pressure (mmHg)': [120, 125],
        'Diastolic Blood Pressure (mmHg)': [80, 85],
        'Body Temperature (°C)': [37.0, 37.2],
        'Predicted Disease': ['Normal', 'Asthma']
    })
    preprocess_data(train_df, is_training=True, model_dir=str(tmp_path))

    df = pd.DataFrame({
        'Heart_Rate': [115],
        'SpO2_Level': [92],
        'Systolic_BP': [145],
        'Diastolic_BP': [92],
        'Body_Temp': [37.4],
    })
    X, y = preprocess_data(df, is_training=False, model_dir=str(tmp_path))
    assert 'Risk_Severity' in X.columns
    assert 'Heart Rate (bpm)' in X.columns
    assert not X.isnull().values.any()