Test projection of source space data to sensor space.
()
| 211 | |
| 212 | @testing.requires_testing_data |
| 213 | def test_apply_forward(): |
| 214 | """Test projection of source space data to sensor space.""" |
| 215 | start = 0 |
| 216 | stop = 5 |
| 217 | n_times = stop - start - 1 |
| 218 | sfreq = 10.0 |
| 219 | t_start = 0.123 |
| 220 | |
| 221 | fwd = read_forward_solution(fname_meeg) |
| 222 | fwd = convert_forward_solution(fwd, surf_ori=True, force_fixed=True, use_cps=True) |
| 223 | fwd = pick_types_forward(fwd, meg=True) |
| 224 | assert isinstance(fwd, Forward) |
| 225 | |
| 226 | vertno = [fwd["src"][0]["vertno"], fwd["src"][1]["vertno"]] |
| 227 | stc_data = np.ones((len(vertno[0]) + len(vertno[1]), n_times)) |
| 228 | stc = SourceEstimate(stc_data, vertno, tmin=t_start, tstep=1.0 / sfreq) |
| 229 | |
| 230 | gain_sum = np.sum(fwd["sol"]["data"], axis=1) |
| 231 | |
| 232 | # Evoked |
| 233 | evoked = read_evokeds(fname_evoked, condition=0) |
| 234 | evoked.pick(picks="meg") |
| 235 | with ( |
| 236 | _record_warnings(), |
| 237 | pytest.warns(RuntimeWarning, match="only .* positive values"), |
| 238 | ): |
| 239 | evoked = apply_forward(fwd, stc, evoked.info, start=start, stop=stop) |
| 240 | data = evoked.data |
| 241 | times = evoked.times |
| 242 | |
| 243 | # do some tests |
| 244 | assert_array_almost_equal(evoked.info["sfreq"], sfreq) |
| 245 | assert_array_almost_equal(np.sum(data, axis=1), n_times * gain_sum) |
| 246 | assert_array_almost_equal(times[0], t_start) |
| 247 | assert_array_almost_equal(times[-1], t_start + (n_times - 1) / sfreq) |
| 248 | |
| 249 | # vector |
| 250 | stc_vec = VectorSourceEstimate( |
| 251 | fwd["source_nn"][:, :, np.newaxis] * stc.data[:, np.newaxis], |
| 252 | stc.vertices, |
| 253 | stc.tmin, |
| 254 | stc.tstep, |
| 255 | ) |
| 256 | large_ctx = pytest.warns(RuntimeWarning, match="very large") |
| 257 | with large_ctx: |
| 258 | evoked_2 = apply_forward(fwd, stc_vec, evoked.info) |
| 259 | assert np.abs(evoked_2.data).mean() > 1e-5 |
| 260 | assert_allclose(evoked.data, evoked_2.data, atol=1e-10) |
| 261 | |
| 262 | # Raw |
| 263 | with large_ctx, pytest.warns(RuntimeWarning, match="only .* positive values"): |
| 264 | raw_proj = apply_forward_raw(fwd, stc, evoked.info, start=start, stop=stop) |
| 265 | data, times = raw_proj[:, :] |
| 266 | |
| 267 | # do some tests |
| 268 | assert_array_almost_equal(raw_proj.info["sfreq"], sfreq) |
| 269 | assert_array_almost_equal(np.sum(data, axis=1), n_times * gain_sum) |
| 270 | atol = 1.0 / sfreq |
nothing calls this directly
no test coverage detected