Test apply function to epoch objects.
()
| 4825 | |
| 4826 | |
| 4827 | def test_apply_function(): |
| 4828 | """Test apply function to epoch objects.""" |
| 4829 | n_channels = 10 |
| 4830 | data = np.arange(2 * n_channels * 1000).reshape(2, n_channels, 1000) |
| 4831 | events = np.array([[0, 0, 1], [INT32_MAX, 0, 2]]) |
| 4832 | info = mne.create_info(n_channels, 1000.0, "eeg") |
| 4833 | epochs = mne.EpochsArray(data, info, events) |
| 4834 | data_epochs = epochs.get_data() |
| 4835 | |
| 4836 | # apply_function to all channels at once |
| 4837 | def fun(data): |
| 4838 | """Reverse channel order without changing values.""" |
| 4839 | return np.eye(data.shape[1])[::-1] @ data |
| 4840 | |
| 4841 | want = data_epochs[:, ::-1] |
| 4842 | got = epochs.apply_function(fun, channel_wise=False).get_data() |
| 4843 | assert_array_equal(want, got) |
| 4844 | |
| 4845 | # apply_function channel-wise (to first 3 channels) by replacing with mean |
| 4846 | picks = np.arange(3) |
| 4847 | non_picks = np.arange(3, n_channels) |
| 4848 | |
| 4849 | def fun(data): |
| 4850 | return np.full_like(data, data.mean()) |
| 4851 | |
| 4852 | out = epochs.apply_function(fun, picks=picks, channel_wise=True) |
| 4853 | expected = epochs.get_data(picks).mean(axis=-1, keepdims=True) |
| 4854 | assert np.all(out.get_data(picks) == expected) |
| 4855 | assert_array_equal(out.get_data(non_picks), epochs.get_data(non_picks)) |
| 4856 | |
| 4857 | |
| 4858 | def test_apply_function_epo_ch_access(): |
nothing calls this directly
no test coverage detected