JayLacoma commited on
Commit
900890b
·
verified ·
1 Parent(s): 194fe03

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -0
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ from pathlib import Path
4
+ import gradio as gr
5
+
6
+ # Import your pipeline
7
+ from ica_xtra import run_preprocessing_pipeline
8
+
9
+ # Constants
10
+ SUBJECT_ID = "sub-01"
11
+ OUTPUT_DIR = Path("outputs")
12
+ OUTPUT_DIR.mkdir(exist_ok=True)
13
+
14
+ def preprocess_eeg(
15
+ eeg_file,
16
+ gpsc_file,
17
+ input_format="fif",
18
+ apply_highpass=True,
19
+ apply_lowpass=True,
20
+ apply_notch=True,
21
+ line_freq=60.0,
22
+ pre_ica_mad=3.5,
23
+ post_ica_mad=5.0,
24
+ interpolate_before_ica=False,
25
+ use_artifact_detection=True,
26
+ ):
27
+ # Clean output dir
28
+ subject_dir = OUTPUT_DIR / SUBJECT_ID
29
+ if subject_dir.exists():
30
+ shutil.rmtree(subject_dir)
31
+ subject_dir.mkdir(parents=True)
32
+
33
+ # Copy uploaded files
34
+ eeg_path = subject_dir / Path(eeg_file.name).name
35
+ gpsc_path = subject_dir / Path(gpsc_file.name).name
36
+ shutil.copy(eeg_file.name, eeg_path)
37
+ shutil.copy(gpsc_file.name, gpsc_path)
38
+
39
+ try:
40
+ run_preprocessing_pipeline(
41
+ subject=SUBJECT_ID,
42
+ input_path=str(eeg_path),
43
+ gpsc_file=str(gpsc_path),
44
+ base_output_path=str(OUTPUT_DIR),
45
+ input_format=input_format,
46
+ apply_highpass=apply_highpass,
47
+ apply_lowpass=apply_lowpass,
48
+ apply_notch=apply_notch,
49
+ line_freq=line_freq,
50
+ pre_ica_mad_threshold=pre_ica_mad,
51
+ post_ica_mad_threshold=post_ica_mad,
52
+ interpolate_before_ica=interpolate_before_ica,
53
+ use_artifact_detection_channels=use_artifact_detection,
54
+ append_subject_to_output=True,
55
+ plot=True,
56
+ log_to_file=True,
57
+ random_state=99,
58
+ )
59
+
60
+ # Gather outputs
61
+ cleaned_fif = subject_dir / f"{SUBJECT_ID}_eeg_ica_cleaned_raw.fif"
62
+ log_file = subject_dir / f"{SUBJECT_ID}_preproc_log.txt"
63
+ plot_dir = subject_dir / "plots"
64
+
65
+ files_to_return = [str(cleaned_fif), str(log_file)]
66
+ if plot_dir.exists():
67
+ for ext in ["*.png"]:
68
+ files_to_return.extend([str(p) for p in plot_dir.glob(ext)])
69
+
70
+ return files_to_return
71
+
72
+ except Exception as e:
73
+ error_log = subject_dir / "error.txt"
74
+ with open(error_log, "w") as f:
75
+ f.write(f"Preprocessing failed:\n{str(e)}")
76
+ return [str(error_log)]
77
+
78
+ with gr.Blocks(theme=gr.themes.Base(), title="EEG Preprocessing") as demo:
79
+ gr.Markdown("# EEG Preprocessing Pipeline")
80
+ gr.Markdown("Upload EEG (.fif or .mff) and montage (.gpsc) files for automated cleaning.")
81
+
82
+ with gr.Row():
83
+ with gr.Column():
84
+ eeg_input = gr.File(label="EEG File (.fif or .mff)")
85
+ gpsc_input = gr.File(label="Montage File (.gpsc)")
86
+ format_dropdown = gr.Dropdown(choices=["fif", "mff"], value="fif", label="Input Format")
87
+
88
+ with gr.Accordion("Advanced Settings", open=False):
89
+ hp = gr.Checkbox(True, label="Apply Highpass (1.0 Hz)")
90
+ lp = gr.Checkbox(True, label="Apply Lowpass (100.0 Hz)")
91
+ notch = gr.Checkbox(True, label="Apply Notch Filter")
92
+ line_freq = gr.Number(60.0, label="Line Frequency (Hz)")
93
+ pre_mad = gr.Slider(2.0, 8.0, value=3.5, label="Pre-ICA MAD Threshold")
94
+ post_mad = gr.Slider(2.0, 8.0, value=5.0, label="Post-ICA MAD Threshold")
95
+ interp_before = gr.Checkbox(False, label="Interpolate Bad Channels Before ICA")
96
+ use_artifact = gr.Checkbox(True, label="Use Artifact Detection Channels")
97
+
98
+ run_btn = gr.Button("Run Preprocessing", variant="primary")
99
+
100
+ with gr.Column():
101
+ output_files = gr.Files(label="Download Results")
102
+
103
+ run_btn.click(
104
+ fn=preprocess_eeg,
105
+ inputs=[
106
+ eeg_input,
107
+ gpsc_input,
108
+ format_dropdown,
109
+ hp,
110
+ lp,
111
+ notch,
112
+ line_freq,
113
+ pre_mad,
114
+ post_mad,
115
+ interp_before,
116
+ use_artifact,
117
+ ],
118
+ outputs=output_files,
119
+ )
120
+
121
+ demo.launch()