Test whether a reference has been correctly applied.
(raw, reref, ref_data, ref_from)
| 44 | |
| 45 | |
| 46 | def _test_reference(raw, reref, ref_data, ref_from): |
| 47 | """Test whether a reference has been correctly applied.""" |
| 48 | # Separate EEG channels from other channel types |
| 49 | picks_eeg = pick_types(raw.info, meg=False, eeg=True, exclude="bads") |
| 50 | picks_other = pick_types( |
| 51 | raw.info, meg=True, eeg=False, eog=True, stim=True, exclude="bads" |
| 52 | ) |
| 53 | |
| 54 | # Calculate indices of reference channesl |
| 55 | picks_ref = [raw.ch_names.index(ch) for ch in ref_from] |
| 56 | |
| 57 | # Get data |
| 58 | _data = raw._data |
| 59 | _reref = reref._data |
| 60 | |
| 61 | # Check that the ref has been properly computed |
| 62 | if ref_data is not None: |
| 63 | assert_array_equal(ref_data, _data[..., picks_ref, :].mean(-2)) |
| 64 | |
| 65 | # Get the raw EEG data and other channel data |
| 66 | raw_eeg_data = _data[..., picks_eeg, :] |
| 67 | raw_other_data = _data[..., picks_other, :] |
| 68 | |
| 69 | # Get the rereferenced EEG data |
| 70 | reref_eeg_data = _reref[..., picks_eeg, :] |
| 71 | reref_other_data = _reref[..., picks_other, :] |
| 72 | |
| 73 | # Check that non-EEG channels are untouched |
| 74 | assert_allclose(raw_other_data, reref_other_data, 1e-6, atol=1e-15) |
| 75 | |
| 76 | # Undo rereferencing of EEG channels if possible |
| 77 | if ref_data is not None: |
| 78 | if isinstance(raw, BaseEpochs): |
| 79 | unref_eeg_data = reref_eeg_data + ref_data[:, np.newaxis, :] |
| 80 | else: |
| 81 | unref_eeg_data = reref_eeg_data + ref_data |
| 82 | assert_allclose(raw_eeg_data, unref_eeg_data, 1e-6, atol=1e-15) |
| 83 | |
| 84 | |
| 85 | @testing.requires_testing_data |
no test coverage detected