Test Xdawn with regularization.
()
| 176 | |
| 177 | |
| 178 | def test_xdawn_regularization(): |
| 179 | """Test Xdawn with regularization.""" |
| 180 | pytest.importorskip("sklearn") |
| 181 | # Get data, this time MEG so we can test proper reg/ch type support |
| 182 | raw = read_raw_fif(raw_fname, verbose=False, preload=True) |
| 183 | events = read_events(event_name) |
| 184 | picks = pick_types( |
| 185 | raw.info, meg=True, eeg=False, stim=False, ecg=False, eog=False, exclude="bads" |
| 186 | )[::8] |
| 187 | raw.pick([raw.ch_names[pick] for pick in picks]) |
| 188 | del picks |
| 189 | raw.info.normalize_proj() |
| 190 | epochs = Epochs( |
| 191 | raw, events, event_id, tmin, tmax, preload=True, baseline=None, verbose=False |
| 192 | ) |
| 193 | |
| 194 | # Test with overlapping events. |
| 195 | # modify events to simulate one overlap |
| 196 | events = epochs.events |
| 197 | sel = np.where(events[:, 2] == 2)[0][:2] |
| 198 | modified_event = events[sel[0]] |
| 199 | modified_event[0] += 1 |
| 200 | epochs.events[sel[1]] = modified_event |
| 201 | # Fit and check that overlap was found and applied |
| 202 | xd = Xdawn(n_components=2, correct_overlap="auto", reg="oas") |
| 203 | xd.fit(epochs) |
| 204 | assert xd.correct_overlap_ |
| 205 | evoked = epochs["cond2"].average() |
| 206 | assert np.sum(np.abs(evoked.data - xd.evokeds_["cond2"].data)) |
| 207 | |
| 208 | # With covariance regularization |
| 209 | for reg in [0.1, 0.1, "ledoit_wolf", "oas"]: |
| 210 | xd = Xdawn( |
| 211 | n_components=2, |
| 212 | correct_overlap=False, |
| 213 | signal_cov=np.eye(len(epochs.ch_names)), |
| 214 | reg=reg, |
| 215 | ) |
| 216 | xd.fit(epochs) |
| 217 | # With bad shrinkage |
| 218 | xd = Xdawn( |
| 219 | n_components=2, |
| 220 | correct_overlap=False, |
| 221 | signal_cov=np.eye(len(epochs.ch_names)), |
| 222 | reg=2, |
| 223 | ) |
| 224 | with pytest.raises(ValueError, match="shrinkage must be"): |
| 225 | xd.fit(epochs) |
| 226 | # With rank-deficient input |
| 227 | # this is a bit wacky because `epochs` has projectors on from the old raw |
| 228 | # but it works as a rank-deficient test case |
| 229 | xd = Xdawn(correct_overlap=False, reg=0.5) |
| 230 | xd.fit(epochs) |
| 231 | xd = Xdawn(correct_overlap=False, reg="diagonal_fixed") |
| 232 | xd.fit(epochs) |
| 233 | # XXX in principle this should maybe raise an error due to deficiency? |
| 234 | # xd = Xdawn(correct_overlap=False, reg=None) |
| 235 | # with pytest.raises(ValueError, match='Could not compute eigenvalues'): |
nothing calls this directly
no test coverage detected