From 102342538901e90f25399118e4301c45056b8ef4 Mon Sep 17 00:00:00 2001 From: vferat Date: Sun, 15 Dec 2024 11:31:29 +0100 Subject: [PATCH] Update test_commands.py --- mne/commands/tests/test_commands.py | 82 ++++++++++++++++++----------- 1 file changed, 51 insertions(+), 31 deletions(-) diff --git a/mne/commands/tests/test_commands.py b/mne/commands/tests/test_commands.py index ae885d3c21d..82d0f7f3a11 100644 --- a/mne/commands/tests/test_commands.py +++ b/mne/commands/tests/test_commands.py @@ -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):