File size: 14,230 Bytes
a4b835f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1740736
 
a4b835f
 
 
 
 
 
1740736
 
 
 
 
 
 
a4b835f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1740736
 
 
 
 
 
 
 
 
 
 
 
 
 
 
604e817
a4b835f
1740736
 
604e817
1740736
 
 
 
 
 
 
 
 
 
604e817
1740736
604e817
 
1740736
 
 
604e817
1740736
604e817
1740736
 
 
 
 
a4b835f
604e817
 
 
 
 
 
 
 
 
 
 
 
a4b835f
 
 
 
 
 
 
 
1740736
 
 
 
 
 
 
 
 
 
96f06ce
 
 
 
 
 
 
 
a4b835f
 
1740736
 
96f06ce
604e817
 
 
1740736
604e817
1740736
 
 
 
 
 
604e817
 
 
4b3251b
604e817
 
 
 
 
 
 
 
96f06ce
 
 
604e817
96f06ce
 
 
 
 
 
 
 
 
 
 
 
 
 
1740736
 
604e817
 
 
1740736
 
 
 
 
 
604e817
1740736
 
 
604e817
 
 
 
 
 
 
 
 
 
 
 
 
1740736
 
 
 
 
 
 
604e817
 
 
 
 
 
 
1740736
604e817
 
 
 
 
 
 
1740736
 
 
 
 
604e817
 
 
1740736
604e817
 
7324bfd
604e817
7324bfd
604e817
 
 
 
 
 
 
 
 
7324bfd
604e817
7324bfd
1740736
 
 
604e817
7324bfd
 
 
 
 
 
 
604e817
7324bfd
604e817
 
1740736
604e817
 
 
1740736
96f06ce
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import gradio as gr
import numpy as np
import pandas as pd
from io import BytesIO
import matplotlib.pyplot as plt
from scipy.stats import skew, kurtosis
import joblib
import os

SAMPLING_RATE = 125
WINDOW_SIZE = 125
SEQUENCE_LENGTH = 10

scaler = joblib.load("models/scaler/scaler.joblib")

HYBRID_MODEL = [
    ("LSTM", "RandomForest", "LSTM + Random Forest", "models/LSTM/rf_lstm_optuna_10122025_122323.joblib", "models/RandomForest/rf_lstm_optuna_10122025_123350.joblib"),
    ("LSTM", "SVM", "LSTM + SVM", "models/LSTM/SVM_LSTM_optuna_10122025_124318.joblib", "models/SVM/svm_LSTM_optuna_10122025_125033.joblib"),
    ("LSTM", "XGBOOST", "LSTM + XGBoost", "models/LSTM/xgboost_LSTM_optuna_10122025_113333.joblib", "models/XGBOOST/xgboost_LSTM_optuna_10122025_113701.joblib"),
    ("TRANSFORMER", "RandomForest", "Transformer + Random Forest", "models/TRANSFORMER/rf_transformer_optuna_10122025_045920.joblib", "models/RandomForest/rf_transformer_optuna_10122025_045552.joblib"),
    ("TRANSFORMER", "SVM", "Transformer + SVM", "models/TRANSFORMER/svm_transformer_optuna_10122025_050213.joblib", "models/SVM/svm_transformer_optuna_10122025_045226.joblib"),
    ("TRANSFORMER", "XGBOOST", "Transformer + XGBoost", "models/TRANSFORMER/xgboost_transformer_optuna_10122025_050346.joblib", "models/XGBOOST/xgboost_transformer_optuna_10122025_044957.joblib"),
]

SINGLE_MODELS = [
    ("Single LSTM", "models/LSTM/single_LSTM_default_10122025_135523.joblib"),
    ("Single Transformer", "models/TRANSFORMER/single_transformer_grid_10122025_115452.joblib"),
]

MODEL_PAIRS = [m for m in HYBRID_MODEL if 'lstm' in m[3].lower() or 'lstm' in m[4].lower() or 'transformer' in m[3].lower() or 'transformer' in m[4].lower()]

def ekstraksi_fitur_statistik(w):
    return np.array([skew(w), kurtosis(w), np.min(w), np.max(w), np.std(w)])

def ekstraksi_fitur_sinyal(ecg):
    n = len(ecg) // WINDOW_SIZE
    fitur = []
    for i in range(n):
        w = ecg[i*WINDOW_SIZE:(i+1)*WINDOW_SIZE]
        fitur.append(ekstraksi_fitur_statistik(w))
    return np.array(fitur)

def buat_sequence(fitur):
    x = []
    for i in range(len(fitur) - SEQUENCE_LENGTH + 1):
        x.append(fitur[i:i+SEQUENCE_LENGTH])
    return np.array(x)

def preprocessing_sinyal(ecg):
    fitur = ekstraksi_fitur_sinyal(ecg)
    fitur_scaled = scaler.transform(fitur)
    seq = buat_sequence(fitur_scaled)
    return seq

def load_hybrid_models(p1, p2):
    m1 = joblib.load(p1)
    m2 = joblib.load(p2)
    return m1, m2

def analisis_sinyal(file, model_pair_label):

    single = next((s for s in SINGLE_MODELS if s[0] == model_pair_label), None)
    if single:
        model_path = os.path.join(os.path.dirname(__file__), single[1])
        model = joblib.load(model_path)
        df = pd.read_csv(file.name)
        sinyal = df.values.flatten()
        seq = preprocessing_sinyal(sinyal)
        try:
            pred = model.predict(seq)
            if hasattr(model, 'predict_proba'):
                pred_proba = model.predict_proba(seq)
                label = int(np.argmax(pred_proba[0]))
            else:
                label = int(pred[0])
        except Exception as e:
            return f"Failed to predict with single learning model: {e}", None
    else:
        selected = next((m for m in MODEL_PAIRS if m[2] == model_pair_label), None)
        if not selected:
            return "Model not found", None
        p1 = selected[3]
        p2 = selected[4]
        p1 = os.path.join(os.path.dirname(__file__), p1)
        p2 = os.path.join(os.path.dirname(__file__), p2)
        model_dl, model_clf = load_hybrid_models(p1, p2)
        df = pd.read_csv(file.name)
        sinyal = df.values.flatten()
        seq = preprocessing_sinyal(sinyal)
        try:
            fitur = model_dl.predict(seq)
            debug_info = f"Type model_dl: {type(model_dl)}, Output predict: {type(fitur)}, Shape: {getattr(fitur, 'shape', None)}"
        except Exception as e:
            return f"Failed to predict with feature extraction model: {e}", None
        
        n_features_model = getattr(model_clf, 'n_features_in_', None)
        if hasattr(fitur, 'shape') and n_features_model is not None and fitur.shape[0] >= n_features_model:
            fitur = fitur[-n_features_model:].reshape(1, n_features_model)

        if n_features_model is not None and (not hasattr(fitur, 'shape') or fitur.shape[1] != n_features_model):
            return debug_info + f"\nNumber of extracted features ({fitur.shape[1] if hasattr(fitur, 'shape') else '?'}) does not match the model's expected number ({n_features_model}). Ensure the feature extraction model and classifier are compatible.", None
        if hasattr(model_clf, "predict_proba"):
            pred = model_clf.predict_proba(fitur)[0]
            label = int(np.argmax(pred))
        else:
            label = int(model_clf.predict(fitur)[0])

    fig, ax = plt.subplots(figsize=(8, 3))
    ax.plot(sinyal, label="Raw ECG", color="#2196f3", linewidth=1)
 
    if len(sinyal) > 25:
        ma = pd.Series(sinyal).rolling(window=25, min_periods=1, center=True).mean()
        ax.plot(ma, label="Moving Average", color="#ff9800", linewidth=2, alpha=0.7)
    ax.set_title("ECG Signal (Raw & Smoothed)")
    ax.set_xlabel("Sample")
    ax.set_ylabel("Amplitude")
    ax.legend()
    ax.grid(True, linestyle='--', alpha=0.5)
    fig.tight_layout()
    buf = BytesIO()
    fig.savefig(buf, format="png")
    buf.seek(0)
    import PIL.Image
    img = PIL.Image.open(buf)
    return str(label), img

css = """
body {background-color: #181818; color: #f5f5f5;}
.gradio-container, .gradio-app {background-color: #181818 !important;}
#title {text-align:center; font-size:32px; font-weight:700; margin-bottom:20px; color:#f5f5f5;}
#subtitle {text-align:center; font-size:18px; margin-bottom:40px; color:#bbbbbb;}
input, select, textarea, .gr-button, .gr-input, .gr-textbox, .gr-dropdown, .gr-file, .gr-image {
    background-color: #232323 !important;
    color: #f5f5f5 !important;
    border-color: #444 !important;
}
.gr-button {border-radius: 6px;}
.license-box {
    border: 2px solid #fff;
    border-radius: 10px;
    padding: 18px;
    margin-top: 18px;
    background: #111;
    color: #fff;
}
"""



with gr.Blocks() as demo:
    gr.HTML('<div id="title">Atrial Fibrillation Detection</div>')
    gr.HTML('<div id="subtitle">Analyze ECG data for Atrial Fibrillation presence</div>')
    file_upload = gr.File(label="Upload Dataset/Signal (CSV)", file_types=[".csv"])
    shared_file_path = gr.State()
    clear_btn = gr.Button("Clear File")

    def store_file(file):
        return file.name if file is not None else None

    file_upload.change(store_file, inputs=file_upload, outputs=shared_file_path)

    def clear_file():
        return None, None
    clear_btn.click(clear_file, inputs=None, outputs=[file_upload, shared_file_path])

    with gr.Tab("Dataset Info"):
        gr.Markdown("""
    ## Dataset Files
    The dataset files below are the same as those used for model testing in this application. Please download using the buttons below:
            """)
        with gr.Row():
            gr.File(value="Data/mimic_perform_af_001_data.csv", label="Download Atrial Fibrillation Data", interactive=False)
            gr.File(value="Data/mimic_perform_non_af_001_data.csv", label="Download Non-Atrial Fibrillation Data", interactive=False)
        gr.Markdown(
            """
<div class="license-box">
<b>Dataset License</b>
<br>This dataset is licensed under the Open Data Commons Open Database License v1.0 (ODbL 1.0 license).<br>
Further details: <a href="https://opendatacommons.org/licenses/odbl/summary/" target="_blank">ODbL 1.0</a><br><br>
This dataset is derived from the MIMIC III Waveform Database:<br>
Moody, B., Moody, G., Villarroel, M., Clifford, G. D., & Silva, I. (2020). MIMIC-III Waveform Database (version 1.0). PhysioNet. <a href="https://doi.org/10.13026/c2607m" target="_blank">https://doi.org/10.13026/c2607m</a><br><br>
The MIMIC III Waveform Database is licensed under the ODbL 1.0 license.<br><br>
The MIMIC-III database is described in:<br>
Johnson, A. E. W., Pollard, T. J., Shen, L., Lehman, L. H., Feng, M., Ghassemi, M., Moody, B., Szolovits, P., Celi, L. A., & Mark, R. G. (2016). MIMIC-III, a freely accessible critical care database. Scientific Data, 3, 160035. <a href="https://doi.org/10.1038/sdata.2016.35" target="_blank">https://doi.org/10.1038/sdata.2016.35</a><br><br>
It is available on PhysioNet: <a href="https://physionet.org/" target="_blank">https://physionet.org/</a><br>
Goldberger, A., Amaral, L., Glass, L., Hausdorff, J., Ivanov, P. C., Mark, R., ... & Stanley, H. E. (2000). PhysioBank, PhysioToolkit, and PhysioNet: Components of a new research resource for complex physiologic signals. Circulation [Online]. 101 (23), pp. e215–e220.<br><br>
The following annotations of AF and non-AF were used to create the dataset:<br>
Bashar, Syed Khairul (2020): Atrial Fibrillation annotations of electrocardiogram from MIMIC III matched subset. figshare. Dataset. <a href="https://doi.org/10.6084/m9.figshare.12149091.v1" target="_blank">https://doi.org/10.6084/m9.figshare.12149091.v1</a><br><br>
Bashar, S.K., Ding, E., Walkey, A.J., McManus, D.D. and Chon, K.H., 2019. Noise Detection in Electrocardiogram Signals for Intensive Care Unit Patients. IEEE Access, 7, pp.88357-88368. <a href="https://doi.org/10.1109/ACCESS.2019.2926199" target="_blank">https://doi.org/10.1109/ACCESS.2019.2926199</a><br><br>
This annotation information is reproduced under the terms of the <a href="https://creativecommons.org/licenses/by/4.0/" target="_blank">CC BY 4.0 licence</a>
</div> """,elem_id=None)

    with gr.Tab("Analyze Dataset"):
        info_ds = gr.Textbox(label="Dataset Info", interactive=False, lines=5)
        preview_ds = gr.Dataframe(label="Data Preview", interactive=False)
        plot_ds = gr.Plot(label="ECG Signal Plot")
        btn_ds = gr.Button("Analyze Dataset")

        def analyze_dataset(file_path):
            import pandas as pd
            import matplotlib.pyplot as plt
            if file_path is None:
                return "No file uploaded. Please upload a CSV file first.", None, None
            try:
                df = pd.read_csv(file_path)
            except Exception as e:
                return f"Failed to read file. Make sure the file is a valid CSV. Error: {e}", None, None

            info_lines = []
            info_lines.append(f"Shape: {df.shape}")
            info_lines.append(f"Columns: {list(df.columns)}")
            info_lines.append(f"Missing: {df.isnull().sum().to_dict()}")

            duration = None
            sampling_rate = 125  
            n_samples = len(df)
            duration = n_samples / sampling_rate
            info_lines.append(f"Sampling rate: {sampling_rate} Hz")
            info_lines.append(f"Data duration: {duration:.2f} seconds ({duration/60:.2f} minutes)")
            preview = df.head(10)
            fig = None
            ecg_col = None
            for col in df.columns:
                if 'ecg' in col.lower():
                    ecg_col = col
                    break
            if ecg_col is None:
                return "The uploaded CSV does not contain an 'ecg' column. Please upload a CSV file with an 'ecg' feature/column.", preview, None
            if not pd.api.types.is_numeric_dtype(df[ecg_col]):
                return f"The selected signal column ('{ecg_col}') is not numeric. Please upload a valid ECG CSV.", preview, None

            plot_samples = min(sampling_rate*10, len(df))
            try:
                fig, ax = plt.subplots()
                ax.plot(df[ecg_col].values[:plot_samples])
                ax.set_title(f"First 10 Seconds Signal Plot: {ecg_col}")
                ax.set_xlabel("Sample")
                ax.set_ylabel("Amplitude")
            except Exception as e:
                return f"Failed to plot ECG signal: {e}", preview, None
            return "\n".join(info_lines), preview, fig

        btn_ds.click(analyze_dataset, inputs=[shared_file_path], outputs=[info_ds, preview_ds, plot_ds])

    with gr.Tab("Analyze Model"):
        all_model_labels = [m[2] for m in MODEL_PAIRS] + [s[0] for s in SINGLE_MODELS]
        pilih_model = gr.Dropdown(all_model_labels, label="Select Model", value=all_model_labels[0])
        hasil = gr.Textbox(label="Prediction Result", interactive=False)
        tombol = gr.Button("Predict")

        def handle_predict(file_path, model_label):
            if file_path is None or str(file_path).strip() == "":
                return "No file uploaded. Please upload a CSV file first."
            if not model_label:
                return "No model selected. Please select a model."
            try:
                import pandas as pd
                df = pd.read_csv(file_path)
                ecg_col = None
                for col in df.columns:
                    if 'ecg' in col.lower():
                        ecg_col = col
                        break
                if ecg_col is None:
                    return "The uploaded CSV does not contain an 'ecg' column. Please upload a CSV file with an 'ecg' feature/column for prediction."
                if not pd.api.types.is_numeric_dtype(df[ecg_col]):
                    return f"The selected signal column ('{ecg_col}') is not numeric. Please upload a valid ECG CSV."
                class DummyFile:
                    def __init__(self, name):
                        self.name = name
                dummy_file = DummyFile(file_path)
                result, _ = analisis_sinyal(dummy_file, model_label)
                if str(result).strip() == '0':
                    return 'Non-AF'
                elif str(result).strip() == '1':
                    return 'AF'
                else:
                    return str(result)
            except Exception as e:
                return f"Prediction failed: {e}"

        tombol.click(handle_predict, inputs=[shared_file_path, pilih_model], outputs=[hasil])

        def clear_file():
            return None, None, None
        clear_btn.click(clear_file, inputs=None, outputs=[file_upload, shared_file_path, hasil])

    demo.launch(css=css)