MCPcopy
hub / github.com/mne-tools/mne-python / test_average_forward_solution

Function test_average_forward_solution

mne/forward/tests/test_forward.py:437–482  ·  view source on GitHub ↗

Test averaging forward solutions.

(tmp_path)

Source from the content-addressed store, hash-verified

435@testing.requires_testing_data
436@requires_mne
437def test_average_forward_solution(tmp_path):
438 """Test averaging forward solutions."""
439 fwd = read_forward_solution(fname_meeg)
440 # input not a list
441 pytest.raises(TypeError, average_forward_solutions, 1)
442 # list is too short
443 pytest.raises(ValueError, average_forward_solutions, [])
444 # negative weights
445 pytest.raises(ValueError, average_forward_solutions, [fwd, fwd], [-1, 0])
446 # all zero weights
447 pytest.raises(ValueError, average_forward_solutions, [fwd, fwd], [0, 0])
448 # weights not same length
449 pytest.raises(ValueError, average_forward_solutions, [fwd, fwd], [0, 0, 0])
450 # list does not only have all dict()
451 pytest.raises(TypeError, average_forward_solutions, [1, fwd])
452
453 # try an easy case
454 fwd_copy = average_forward_solutions([fwd])
455 assert isinstance(fwd_copy, Forward)
456 assert_array_equal(fwd["sol"]["data"], fwd_copy["sol"]["data"])
457
458 # modify a fwd solution, save it, use MNE to average with old one
459 fwd_copy["sol"]["data"] *= 0.5
460 fname_copy = str(tmp_path / "copy-fwd.fif")
461 write_forward_solution(fname_copy, fwd_copy, overwrite=True)
462 cmd = (
463 "mne_average_forward_solutions",
464 "--fwd",
465 fname_meeg,
466 "--fwd",
467 fname_copy,
468 "--out",
469 fname_copy,
470 )
471 run_subprocess(cmd)
472
473 # now let's actually do it, with one filename and one fwd
474 fwd_ave = average_forward_solutions([fwd, fwd_copy])
475 assert_array_equal(0.75 * fwd["sol"]["data"], fwd_ave["sol"]["data"])
476 # fwd_ave_mne = read_forward_solution(fname_copy)
477 # assert_array_equal(fwd_ave_mne['sol']['data'], fwd_ave['sol']['data'])
478
479 # with gradient
480 fwd = read_forward_solution(fname_meeg_grad)
481 fwd_ave = average_forward_solutions([fwd, fwd])
482 assert_forward_allclose(fwd, fwd_ave)
483
484
485@testing.requires_testing_data

Callers

nothing calls this directly

Calls 5

read_forward_solutionFunction · 0.90
write_forward_solutionFunction · 0.90
run_subprocessFunction · 0.90
assert_forward_allcloseFunction · 0.85

Tested by

no test coverage detected