Test simulation of raw data with sphere model.
(raw_data, tmp_path)
| 241 | |
| 242 | |
| 243 | def test_simulate_raw_sphere(raw_data, tmp_path): |
| 244 | """Test simulation of raw data with sphere model.""" |
| 245 | pytest.importorskip("nibabel") |
| 246 | seed = 42 |
| 247 | raw, src, stc, trans, sphere = raw_data |
| 248 | assert len(pick_types(raw.info, meg=False, ecg=True)) == 1 |
| 249 | |
| 250 | # head pos |
| 251 | head_pos_sim = _get_head_pos_sim(raw) |
| 252 | head_pos_sim_2 = np.zeros((len(head_pos_sim), 10)) |
| 253 | for ii, (t, mat) in enumerate(head_pos_sim.items()): |
| 254 | head_pos_sim_2[ii, :7] = [t] + list(_affine_to_quat(mat)) |
| 255 | head_pos_sim_3 = tmp_path / "head_pos.txt" |
| 256 | write_head_pos(head_pos_sim_3, head_pos_sim_2) |
| 257 | |
| 258 | # |
| 259 | # Test raw simulation with basic parameters |
| 260 | # |
| 261 | raw.info.normalize_proj() |
| 262 | cov = read_cov(cov_fname) |
| 263 | cov["projs"] = raw.info["projs"] |
| 264 | raw.info["bads"] = raw.ch_names[:1] |
| 265 | raw_meg = raw.copy().pick("meg") |
| 266 | raw_sim = simulate_raw(raw_meg.info, stc, trans, src, sphere, head_pos=head_pos_sim) |
| 267 | raw_data = raw_sim[:][0] |
| 268 | # Test IO on processed data |
| 269 | test_outname = tmp_path / "sim_test_raw.fif" |
| 270 | raw_sim.save(test_outname) |
| 271 | |
| 272 | raw_sim_loaded = read_raw_fif(test_outname, preload=True) |
| 273 | assert_allclose(raw_sim_loaded[:][0], raw_sim[:][0], rtol=1e-6, atol=1e-20) |
| 274 | del raw_sim |
| 275 | |
| 276 | # make sure it works with EEG-only and MEG-only |
| 277 | raw_sim_meg = simulate_raw(raw.copy().pick("meg").info, stc, trans, src, sphere) |
| 278 | raw_sim_eeg = simulate_raw(raw.copy().pick("eeg").info, stc, trans, src, sphere) |
| 279 | raw_sim_meeg = simulate_raw( |
| 280 | raw.copy().pick(["meg", "eeg"]).info, stc, trans, src, sphere |
| 281 | ) |
| 282 | for this_raw in (raw_sim_meg, raw_sim_eeg, raw_sim_meeg): |
| 283 | add_eog(this_raw, random_state=seed) |
| 284 | for this_raw in (raw_sim_meg, raw_sim_meeg): |
| 285 | add_ecg(this_raw, random_state=seed) |
| 286 | with pytest.raises(RuntimeError, match="only add ECG artifacts if MEG"): |
| 287 | add_ecg(raw_sim_eeg) |
| 288 | assert_allclose( |
| 289 | np.concatenate((raw_sim_meg[:][0], raw_sim_eeg[:][0])), |
| 290 | raw_sim_meeg[:][0], |
| 291 | rtol=1e-7, |
| 292 | atol=1e-20, |
| 293 | ) |
| 294 | del raw_sim_meg, raw_sim_eeg, raw_sim_meeg |
| 295 | |
| 296 | # check that raw-as-info is supported |
| 297 | n_samp = len(stc.times) |
| 298 | raw_crop = raw.copy().crop(0.0, (n_samp - 1.0) / raw.info["sfreq"]) |
| 299 | assert len(raw_crop.times) == len(stc.times) |
| 300 | raw_sim = simulate_raw(raw_crop.info, stc, trans, src, sphere) |
nothing calls this directly
no test coverage detected