Skip to content

Commit

Permalink
Fixes make_scalp_surfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
vferat committed Dec 12, 2024
1 parent b329515 commit 512bf41
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
11 changes: 6 additions & 5 deletions mne/bem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2394,19 +2394,20 @@ def make_scalp_surfaces(
mri = mri if (subj_path / "mri" / mri).exists() else "T1"

logger.info("1. Creating a dense scalp tessellation with mkheadsurf...")
threshold = _ensure_int(threshold, "threshold")

def check_seghead(surf_path=subj_path / "surf"):
def check_seghead(surf_path=subj_path / "surf", overwrite=overwrite):
surf = None
for k in ["lh.seghead", "lh.smseghead"]:
this_surf = surf_path / k
if this_surf.exists():
surf = this_surf
_check_file(surf, overwrite)
break
return surf

my_seghead = check_seghead()
threshold = _ensure_int(threshold, "threshold")
if my_seghead is None:
my_seghead = check_seghead(overwrite=overwrite)
if (my_seghead is None) or overwrite:
this_env = deepcopy(os.environ)
this_env["SUBJECTS_DIR"] = str(subjects_dir)
this_env["SUBJECT"] = subject
Expand All @@ -2432,7 +2433,7 @@ def check_seghead(surf_path=subj_path / "surf"):
env=this_env,
)

surf = check_seghead()
surf = check_seghead(overwrite=False)
if surf is None:
raise RuntimeError("mkheadsurf did not produce the standard output file.")

Expand Down
24 changes: 21 additions & 3 deletions mne/tests/test_bem.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from mne.io import read_info
from mne.surface import _get_ico_surface, read_surface
from mne.transforms import translation
from mne.utils import _record_warnings, catch_logging, check_version
from mne.utils import _record_warnings, catch_logging, check_version, requires_freesurfer

fname_raw = Path(__file__).parents[1] / "io" / "tests" / "data" / "test_raw.fif"
subjects_dir = testing.data_path(download=False) / "subjects"
Expand Down Expand Up @@ -519,11 +519,26 @@ def test_io_head_bem(tmp_path):
assert np.allclose(head["tris"], head_defect["tris"])


@pytest.mark.slowtest # ~4 s locally
@pytest.mark.slowtest
@requires_freesurfer("mkheadsurf")
@testing.requires_testing_data
def test_make_scalp_surfaces_topology(tmp_path, monkeypatch):
"""Test topology checks for make_scalp_surfaces."""
"""Test make_scalp_surfaces and topology checks."""
pytest.importorskip("pyvista")
pytest.importorskip("nibabel")

# tests on 'sample'
subject = 'sample'
with pytest.raises(OSError, match="use --overwrite to overwrite it"):
make_scalp_surfaces(
subject, subjects_dir, force=False, verbose=True, overwrite=False
)

make_scalp_surfaces(
subject, subjects_dir, force=False, verbose=True, overwrite=True
)

# tests on custom surface
subjects_dir = tmp_path
subject = "test"
surf_dir = subjects_dir / subject / "surf"
Expand Down Expand Up @@ -551,6 +566,7 @@ def _decimate_surface(points, triangles, n_triangles):
make_scalp_surfaces(
subject, subjects_dir, force=False, verbose=True, overwrite=True
)

bem_dir = subjects_dir / subject / "bem"
sparse_path = bem_dir / f"{subject}-head-sparse.fif"
assert not sparse_path.is_file()
Expand All @@ -570,6 +586,8 @@ def _decimate_surface(points, triangles, n_triangles):
(surf,) = read_bem_surfaces(sparse_path, on_defects="ignore")
assert len(surf["tris"]) == 319




@pytest.mark.parametrize("bem_type", ["bem", "sphere"])
@pytest.mark.parametrize("n_pos", [1, 10])
Expand Down

0 comments on commit 512bf41

Please sign in to comment.