Skip to content

Commit

Permalink
Update test_commands.py
Browse files Browse the repository at this point in the history
  • Loading branch information
vferat committed Dec 15, 2024
1 parent f17d3cf commit 1023425
Showing 1 changed file with 51 additions and 31 deletions.
82 changes: 51 additions & 31 deletions mne/commands/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,55 +167,75 @@ def test_kit2fiff():
check_usage(mne_kit2fiff, force_help=True)


@pytest.mark.slowtest
@pytest.mark.ultraslowtest
@testing.requires_testing_data
@requires_freesurfer("mkheadsurf")
def test_make_scalp_surfaces(tmp_path, monkeypatch):
"""Test mne make_scalp_surfaces."""
pytest.importorskip("nibabel")
pytest.importorskip("pyvista")
check_usage(mne_make_scalp_surfaces)
has = "SUBJECTS_DIR" in os.environ
# Copy necessary files to avoid FreeSurfer call

tempdir = str(tmp_path)
surf_path = op.join(subjects_dir, "sample", "surf")
surf_path_new = op.join(tempdir, "sample", "surf")
os.mkdir(op.join(tempdir, "sample"))
os.mkdir(surf_path_new)
subj_dir = op.join(tempdir, "sample", "bem")
os.mkdir(subj_dir)
t1_path = op.join(subjects_dir, "sample", "mri", "T1.mgz")
t1_path_new = op.join(tempdir, "sample", "mri", "T1.mgz")

cmd = ("-s", "sample", "--subjects-dir", tempdir)
monkeypatch.setattr(
mne.bem,
"decimate_surface",
lambda points, triangles, n_triangles: (points, triangles),
)
dense_fname = op.join(subj_dir, "sample-head-dense.fif")
medium_fname = op.join(subj_dir, "sample-head-medium.fif")
headseg_path = op.join(tempdir, "sample", "mri", "seghead.mgz")
surf_path = op.join(tempdir, "sample", "surf", "lh.seghead")
dense_fname = op.join(tempdir, "sample", "bem", "sample-head-dense.fif")
medium_fname = op.join(tempdir, "sample", "bem", "sample-head-medium.fif")
sparse_fname = op.join(tempdir, "sample", "bem", "sample-head-sparse.fif")

os.makedirs(op.join(tempdir, "sample", "mri"), exist_ok=True)
os.makedirs(op.join(tempdir, "sample", "surf"), exist_ok=True)

shutil.copy(op.join(t1_path, t1_path_new))
cmd = ("-s", "sample", "--subjects-dir", tempdir, "--no-decimate")
with ArgvSetter(cmd, disable_stdout=False, disable_stderr=False):
monkeypatch.delenv("FREESURFER_HOME", raising=False)
with pytest.raises(RuntimeError, match="The FreeSurfer environ"):
mne_make_scalp_surfaces.run()
shutil.copy(op.join(surf_path, "lh.seghead"), surf_path_new)
monkeypatch.setenv("FREESURFER_HOME", tempdir)

shutil.copy(op.join(t1_path, t1_path_new))
mne_make_scalp_surfaces.run()

assert op.isfile(headseg_path)
assert op.isfile(surf_path)
assert op.isfile(dense_fname)
assert op.isfile(medium_fname)
with pytest.raises(OSError, match="overwrite"):
assert not op.isfile(medium_fname)
assert not op.isfile(sparse_fname)

cmd = ("-s", "sample", "--subjects-dir", tempdir)
with ArgvSetter(cmd, disable_stdout=False, disable_stderr=False):
with pytest.raises(RuntimeError, match="use --overwrite to overwrite it"):
mne_make_scalp_surfaces.run()
# actually check the outputs
head_py = read_bem_surfaces(dense_fname)
assert_equal(len(head_py), 1)
head_py = head_py[0]
head_c = read_bem_surfaces(
op.join(subjects_dir, "sample", "bem", "sample-head-dense.fif")
)[0]
assert_allclose(head_py["rr"], head_c["rr"])
if not has:
assert "SUBJECTS_DIR" not in os.environ

cmd = ("-s", "sample", "--subjects-dir", tempdir, "--overwrite")
with ArgvSetter(cmd, disable_stdout=False, disable_stderr=False):
mne_make_scalp_surfaces.run()
assert op.isfile(headseg_path)
assert op.isfile(surf_path)
assert op.isfile(dense_fname)
assert op.isfile(medium_fname)
assert op.isfile(sparse_fname)

cmd = ("-s", "sample", "--subjects-dir", tempdir, "--no-decimate", "--overwrite")
with ArgvSetter(cmd, disable_stdout=False, disable_stderr=False):
mne_make_scalp_surfaces.run()
assert op.isfile(headseg_path)
assert op.isfile(surf_path)
assert op.isfile(dense_fname)
assert not op.isfile(medium_fname)
assert not op.isfile(sparse_fname)

os.remove(headseg_path)
os.remove(surf_path)
os.remove(dense_fname)
cmd = ("-s", "sample", "--subjects-dir", tempdir, "--no-decimate")
with ArgvSetter(cmd, disable_stdout=False, disable_stderr=False):
with pytest.raises(RuntimeError, match="Trying to generate new scalp surfaces"):
mne_make_scalp_surfaces.run()

@pytest.mark.slowtest
@testing.requires_testing_data
def test_report(tmp_path):
Expand Down

0 comments on commit 1023425

Please sign in to comment.