|
3 | 3 | # Authors: The PyPREP developers |
4 | 4 | # SPDX-License-Identifier: MIT |
5 | 5 |
|
6 | | -import matplotlib.pyplot as plt |
7 | 6 | import numpy as np |
8 | 7 | import pytest |
9 | | -import scipy.io as sio |
10 | 8 |
|
11 | 9 | from pyprep.prep_pipeline import PrepPipeline |
12 | 10 |
|
13 | 11 | from .conftest import make_random_mne_object |
14 | 12 |
|
15 | 13 |
|
16 | | -@pytest.mark.usefixtures("raw", "montage") |
17 | | -def test_prep_pipeline(raw, montage): |
18 | | - """Test prep pipeline.""" |
19 | | - eeg_index = np.array( |
20 | | - [idx for idx, typ in enumerate(raw.get_channel_types()) if typ == "eeg"] |
21 | | - ) |
22 | | - raw_copy = raw.copy() |
23 | | - ch_names = raw_copy.info["ch_names"] |
24 | | - ch_names_eeg = list(np.asarray(ch_names)[eeg_index]) |
25 | | - sample_rate = raw_copy.info["sfreq"] |
26 | | - prep_params = { |
27 | | - "ref_chs": ch_names_eeg, |
28 | | - "reref_chs": ch_names_eeg, |
29 | | - "line_freqs": np.arange(60, sample_rate / 2, 60), |
30 | | - } |
31 | | - prep = PrepPipeline(raw_copy, prep_params, montage, random_state=42) |
32 | | - prep.fit() |
33 | | - |
34 | | - EEG_raw = raw_copy.get_data(picks="eeg") * 1e6 |
35 | | - EEG_raw_max = np.max(abs(EEG_raw), axis=None) |
36 | | - EEG_raw_matlab = sio.loadmat("./examples/matlab_results/EEG_raw.mat") |
37 | | - EEG_raw_matlab = EEG_raw_matlab["save_data"] |
38 | | - EEG_raw_diff = EEG_raw - EEG_raw_matlab |
39 | | - # EEG_raw_mse = (EEG_raw_diff / EEG_raw_max ** 2).mean(axis=None) |
40 | | - |
41 | | - fig, axs = plt.subplots(5, 3, sharex="all") |
42 | | - plt.setp(fig, facecolor=[1, 1, 1]) |
43 | | - fig.suptitle("Python versus Matlab PREP results", fontsize=16) |
44 | | - |
45 | | - im = axs[0, 0].imshow( |
46 | | - EEG_raw / EEG_raw_max, |
47 | | - aspect="auto", |
48 | | - extent=[0, (EEG_raw.shape[1] / sample_rate), 63, 0], |
49 | | - vmin=-1, |
50 | | - vmax=1, |
51 | | - cmap=plt.get_cmap("RdBu"), |
52 | | - ) |
53 | | - axs[0, 0].set_title("Python", fontsize=14) |
54 | | - axs[0, 1].imshow( |
55 | | - EEG_raw_matlab / EEG_raw_max, |
56 | | - aspect="auto", |
57 | | - extent=[0, (EEG_raw_matlab.shape[1] / sample_rate), 63, 0], |
58 | | - vmin=-1, |
59 | | - vmax=1, |
60 | | - cmap=plt.get_cmap("RdBu"), |
61 | | - ) |
62 | | - axs[0, 1].set_title("Matlab", fontsize=14) |
63 | | - axs[0, 2].imshow( |
64 | | - EEG_raw_diff / EEG_raw_max, |
65 | | - aspect="auto", |
66 | | - extent=[0, (EEG_raw_diff.shape[1] / sample_rate), 63, 0], |
67 | | - vmin=-1, |
68 | | - vmax=1, |
69 | | - cmap=plt.get_cmap("RdBu"), |
70 | | - ) |
71 | | - axs[0, 2].set_title("Difference", fontsize=14) |
72 | | - # axs[0, 0].set_title('Original EEG', loc='left', fontsize=14) |
73 | | - # axs[0, 0].set_ylabel('Channel Number', fontsize=14) |
74 | | - cb = fig.colorbar(im, ax=axs, fraction=0.05, pad=0.04) |
75 | | - cb.set_label("\u03bcVolt", fontsize=14) |
76 | | - |
77 | | - EEG_new_matlab = sio.loadmat("./examples/matlab_results/EEGNew.mat") |
78 | | - EEG_new_matlab = EEG_new_matlab["save_data"] |
79 | | - EEG_new = prep.EEG_new |
80 | | - EEG_new_max = np.max(abs(EEG_new), axis=None) |
81 | | - EEG_new_diff = EEG_new - EEG_new_matlab |
82 | | - # EEG_new_mse = ((EEG_new_diff / EEG_new_max) ** 2).mean(axis=None) |
83 | | - axs[1, 0].imshow( |
84 | | - EEG_new / EEG_new_max, |
85 | | - aspect="auto", |
86 | | - extent=[0, (EEG_new.shape[1] / sample_rate), 63, 0], |
87 | | - vmin=-1, |
88 | | - vmax=1, |
89 | | - cmap=plt.get_cmap("RdBu"), |
90 | | - ) |
91 | | - axs[1, 1].imshow( |
92 | | - EEG_new_matlab / EEG_new_max, |
93 | | - aspect="auto", |
94 | | - extent=[0, (EEG_new_matlab.shape[1] / sample_rate), 63, 0], |
95 | | - vmin=-1, |
96 | | - vmax=1, |
97 | | - cmap=plt.get_cmap("RdBu"), |
98 | | - ) |
99 | | - axs[1, 2].imshow( |
100 | | - EEG_new_diff / EEG_new_max, |
101 | | - aspect="auto", |
102 | | - extent=[0, (EEG_new_diff.shape[1] / sample_rate), 63, 0], |
103 | | - vmin=-1, |
104 | | - vmax=1, |
105 | | - cmap=plt.get_cmap("RdBu"), |
106 | | - ) |
107 | | - # axs[1, 0].set_title('High pass filter', loc='left', fontsize=14) |
108 | | - # axs[1, 0].set_ylabel('Channel Number', fontsize=14) |
109 | | - |
110 | | - EEG_clean_matlab = sio.loadmat("./examples/matlab_results/EEG.mat") |
111 | | - EEG_clean_matlab = EEG_clean_matlab["save_data"] |
112 | | - EEG_clean = prep.EEG |
113 | | - EEG_max = np.max(abs(EEG_clean), axis=None) |
114 | | - EEG_diff = EEG_clean - EEG_clean_matlab |
115 | | - # EEG_mse = ((EEG_diff / EEG_max) ** 2).mean(axis=None) |
116 | | - axs[2, 0].imshow( |
117 | | - EEG_clean / EEG_max, |
118 | | - aspect="auto", |
119 | | - extent=[0, (EEG_clean.shape[1] / sample_rate), 63, 0], |
120 | | - vmin=-1, |
121 | | - vmax=1, |
122 | | - cmap=plt.get_cmap("RdBu"), |
123 | | - ) |
124 | | - axs[2, 1].imshow( |
125 | | - EEG_clean_matlab / EEG_max, |
126 | | - aspect="auto", |
127 | | - extent=[0, (EEG_clean_matlab.shape[1] / sample_rate), 63, 0], |
128 | | - vmin=-1, |
129 | | - vmax=1, |
130 | | - cmap=plt.get_cmap("RdBu"), |
131 | | - ) |
132 | | - axs[2, 2].imshow( |
133 | | - EEG_diff / EEG_max, |
134 | | - aspect="auto", |
135 | | - extent=[0, (EEG_diff.shape[1] / sample_rate), 63, 0], |
136 | | - vmin=-1, |
137 | | - vmax=1, |
138 | | - cmap=plt.get_cmap("RdBu"), |
139 | | - ) |
140 | | - # axs[2, 0].set_title('Line-noise removal', loc='left', fontsize=14) |
141 | | - axs[2, 0].set_ylabel("Channel Number", fontsize=14) |
142 | | - |
143 | | - EEG = prep.EEG_before_interpolation |
144 | | - EEG_max = np.max(abs(EEG), axis=None) |
145 | | - EEG_ref_mat = sio.loadmat("./examples/matlab_results/EEGref.mat") |
146 | | - EEG_ref_matlab = EEG_ref_mat["save_EEG"] |
147 | | - # reference_matlab = EEG_ref_mat["save_reference"] |
148 | | - EEG_ref_diff = EEG - EEG_ref_matlab |
149 | | - # EEG_ref_mse = ((EEG_ref_diff / EEG_max) ** 2).mean(axis=None) |
150 | | - # reference_signal = prep.reference_before_interpolation |
151 | | - # reference_max = np.max(abs(reference_signal), axis=None) |
152 | | - # reference_diff = reference_signal - reference_matlab |
153 | | - # reference_mse = ((reference_diff / reference_max) ** 2).mean(axis=None) |
154 | | - axs[3, 0].imshow( |
155 | | - EEG / EEG_max, |
156 | | - aspect="auto", |
157 | | - extent=[0, (EEG.shape[1] / sample_rate), 63, 0], |
158 | | - vmin=-1, |
159 | | - vmax=1, |
160 | | - cmap=plt.get_cmap("RdBu"), |
161 | | - ) |
162 | | - axs[3, 1].imshow( |
163 | | - EEG_ref_matlab / EEG_max, |
164 | | - aspect="auto", |
165 | | - extent=[0, (EEG_ref_matlab.shape[1] / sample_rate), 63, 0], |
166 | | - vmin=-1, |
167 | | - vmax=1, |
168 | | - cmap=plt.get_cmap("RdBu"), |
169 | | - ) |
170 | | - axs[3, 2].imshow( |
171 | | - EEG_ref_diff / EEG_max, |
172 | | - aspect="auto", |
173 | | - extent=[0, (EEG_ref_diff.shape[1] / sample_rate), 63, 0], |
174 | | - vmin=-1, |
175 | | - vmax=1, |
176 | | - cmap=plt.get_cmap("RdBu"), |
177 | | - ) |
178 | | - # axs[3, 0].set_title('Referencing', loc='left', fontsize=14) |
179 | | - # axs[3, 0].set_ylabel('Channel Number', fontsize=14) |
180 | | - |
181 | | - EEG_final = prep.raw.get_data() * 1e6 |
182 | | - EEG_final_max = np.max(abs(EEG_final), axis=None) |
183 | | - EEG_final_matlab = sio.loadmat("./examples/matlab_results/EEGinterp.mat") |
184 | | - EEG_final_matlab = EEG_final_matlab["save_data"] |
185 | | - EEG_final_diff = EEG_final - EEG_final_matlab |
186 | | - # EEG_final_mse = ((EEG_final_diff / EEG_final_max) ** 2).mean(axis=None) |
187 | | - axs[4, 0].imshow( |
188 | | - EEG_final / EEG_final_max, |
189 | | - aspect="auto", |
190 | | - extent=[0, (EEG_final.shape[1] / sample_rate), 63, 0], |
191 | | - vmin=-1, |
192 | | - vmax=1, |
193 | | - cmap=plt.get_cmap("RdBu"), |
194 | | - ) |
195 | | - axs[4, 1].imshow( |
196 | | - EEG_final_matlab / EEG_final_max, |
197 | | - aspect="auto", |
198 | | - extent=[0, (EEG_final_matlab.shape[1] / sample_rate), 63, 0], |
199 | | - vmin=-1, |
200 | | - vmax=1, |
201 | | - cmap=plt.get_cmap("RdBu"), |
202 | | - ) |
203 | | - axs[4, 2].imshow( |
204 | | - EEG_final_diff / EEG_final_max, |
205 | | - aspect="auto", |
206 | | - extent=[0, (EEG_final_diff.shape[1] / sample_rate), 63, 0], |
207 | | - vmin=-1, |
208 | | - vmax=1, |
209 | | - cmap=plt.get_cmap("RdBu"), |
210 | | - ) |
211 | | - # axs[4, 0].set_title('Interpolation', loc='left', fontsize=14) |
212 | | - # axs[4, 0].set_ylabel('Channel Number', fontsize=14) |
213 | | - axs[4, 1].set_xlabel("Time(s)", fontsize=14) |
214 | | - |
215 | | - |
216 | 14 | @pytest.mark.usefixtures("raw", "montage") |
217 | 15 | def test_prep_pipeline_non_eeg(raw, montage): |
218 | 16 | """Test prep pipeline with non eeg channels.""" |
|
0 commit comments