File size: 8,257 Bytes
cab8122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"""
Utility functions for preprocessing tabular and audio data
"""

import numpy as np
import pandas as pd
import librosa
from sklearn.impute import SimpleImputer
import warnings
warnings.filterwarnings('ignore')


class TabularPreprocessor:
    """Preprocessor for tabular medical data"""
    
    def __init__(self):
        self.feature_cols = [
            'age', 'gender', 'tbContactHistory', 'wheezingHistory', 
            'phlegmCough', 'familyAsthmaHistory', 'feverHistory', 
            'coldPresent', 'packYears'
        ]
        self.imputer = SimpleImputer(strategy='most_frequent')
        self.is_fitted = False
    
    def fit(self, X_df):
        """Fit the imputer on training data"""
        X_train = X_df[self.feature_cols].copy()
        self.imputer.fit(X_train)
        self.is_fitted = True
        return self
    
    def transform(self, data_dict):
        """
        Transform a single input dictionary to model-ready format
        
        Args:
            data_dict: Dictionary with keys matching feature_cols
                      e.g., {'age': 43, 'gender': 1, ...}
        
        Returns:
            numpy array of shape (1, n_features)
        """
        if not self.is_fitted:
            # If not fitted, create a simple imputer with most_frequent strategy
            # This handles the case where we load a pre-trained model
            self.imputer = SimpleImputer(strategy='most_frequent')
            # Create a dummy dataframe to fit
            dummy_df = pd.DataFrame([data_dict])
            self.imputer.fit(dummy_df[self.feature_cols])
            self.is_fitted = True
        
        # Create dataframe from input
        df = pd.DataFrame([data_dict])
        
        # Ensure all feature columns exist
        for col in self.feature_cols:
            if col not in df.columns:
                df[col] = np.nan
        
        # Select and order features
        X = df[self.feature_cols].copy()
        
        # Impute missing values
        X_imputed = self.imputer.transform(X)
        
        # Convert to float32
        X_imputed = X_imputed.astype(np.float32)
        
        return X_imputed


class AudioPreprocessor:
    """Preprocessor for audio data (cough and vowel sounds)"""
    
    def __init__(self, sample_rate=16000, duration=1.0, n_mfcc=20, 
                 n_fft=2048, hop_length=512, n_mels=64):
        """
        Initialize audio preprocessor with same parameters as training
        
        Args:
            sample_rate: Target sample rate (Hz)
            duration: Target duration in seconds
            n_mfcc: Number of MFCC coefficients
            n_fft: FFT window size
            hop_length: Hop length for STFT
            n_mels: Number of mel bands
        """
        self.sample_rate = sample_rate
        self.duration = duration
        self.n_mfcc = n_mfcc
        self.n_fft = n_fft
        self.hop_length = hop_length
        self.n_mels = n_mels
        self.input_size = n_mfcc * 3  # MFCC + Delta + Delta-Delta
    
    def load_and_extract_features(self, audio_path_or_array):
        """
        Load audio file and extract MFCC features
        
        Args:
            audio_path_or_array: Path to audio file or numpy array
        
        Returns:
            numpy array of shape (n_frames, n_mfcc*3)
        """
        try:
            # Load audio
            if isinstance(audio_path_or_array, str):
                audio, sr = librosa.load(audio_path_or_array, sr=self.sample_rate)
            else:
                # Assume it's already an array
                audio = audio_path_or_array
                sr = self.sample_rate
            
            # Ensure audio is mono
            if len(audio.shape) > 1:
                audio = np.mean(audio, axis=0)
            
            # Segment to target duration (1 second)
            target_samples = int(self.sample_rate * self.duration)
            if len(audio) > target_samples:
                audio = audio[:target_samples]
            else:
                audio = np.pad(audio, (0, target_samples - len(audio)), mode='constant')
            
            # Extract MFCC
            mfcc = librosa.feature.mfcc(
                y=audio,
                sr=self.sample_rate,
                n_mfcc=self.n_mfcc,
                n_fft=self.n_fft,
                hop_length=self.hop_length,
                n_mels=self.n_mels
            )
            
            # Extract delta and delta-delta
            delta = librosa.feature.delta(mfcc)
            delta_delta = librosa.feature.delta(mfcc, order=2)
            
            # Combine features (n_mfcc*3, n_frames)
            features = np.vstack([mfcc, delta, delta_delta])
            
            # Transpose to (n_frames, n_mfcc*3)
            features = features.T
            
            # Handle NaN and Inf values
            features = np.nan_to_num(features, nan=0.0, posinf=0.0, neginf=0.0)
            
            # Clip extreme values
            features = np.clip(features, -1e6, 1e6)
            
            return features
            
        except Exception as e:
            print(f"Error processing audio: {str(e)}")
            # Return zero features on error
            expected_frames = int((self.sample_rate * self.duration) / self.hop_length) + 1
            return np.zeros((expected_frames, self.input_size))
    
    def extract_from_both_audios(self, cough_audio, vowel_audio, combine_mode="concat"):
        """
        Extract features from both cough and vowel audio
        
        Args:
            cough_audio: Path to cough audio or numpy array
            vowel_audio: Path to vowel audio or numpy array
            combine_mode: How to combine features ("concat" or "average")
        
        Returns:
            Combined features as numpy array
        """
        cough_features = self.load_and_extract_features(cough_audio)
        vowel_features = self.load_and_extract_features(vowel_audio)
        
        if combine_mode == "concat":
            # Concatenate along feature dimension
            combined = np.concatenate([cough_features, vowel_features], axis=1)
        elif combine_mode == "average":
            # Average the features
            combined = (cough_features + vowel_features) / 2.0
        else:
            raise ValueError(f"Unknown combine_mode: {combine_mode}")
        
        return combined


def get_disease_name(prediction):
    """Convert disease prediction to readable name"""
    disease_map = {
        0: "Healthy",
        1: "COPD (Chronic Obstructive Pulmonary Disease)",
        2: "Asthma"
    }
    return disease_map.get(int(prediction), "Unknown")


def get_disease_info(prediction):
    """Get detailed information about the predicted disease"""
    info_map = {
        0: {
            "name": "Healthy",
            "description": "No respiratory disease detected. Lung function appears normal.",
            "recommendations": [
                "Maintain regular exercise and healthy lifestyle",
                "Avoid smoking and secondhand smoke",
                "Get regular health check-ups"
            ]
        },
        1: {
            "name": "COPD (Chronic Obstructive Pulmonary Disease)",
            "description": "A chronic inflammatory lung disease that causes obstructed airflow from the lungs.",
            "recommendations": [
                "Consult with a pulmonologist for proper diagnosis",
                "Consider pulmonary rehabilitation program",
                "Quit smoking if applicable",
                "Use prescribed medications as directed",
                "Get vaccinated against flu and pneumonia"
            ]
        },
        2: {
            "name": "Asthma",
            "description": "A condition in which airways narrow and swell, producing extra mucus.",
            "recommendations": [
                "Consult with an allergist or pulmonologist",
                "Identify and avoid asthma triggers",
                "Use prescribed inhalers as directed",
                "Monitor breathing with a peak flow meter",
                "Have an asthma action plan"
            ]
        }
    }
    return info_map.get(int(prediction), info_map[0])