@@ -170,51 +170,129 @@ def test_kit2fiff():
170170 check_usage (mne_kit2fiff , force_help = True )
171171
172172
173- @pytest .mark .slowtest
174173@pytest .mark .ultraslowtest
175174@testing .requires_testing_data
175+ @requires_freesurfer ("mkheadsurf" )
176176def test_make_scalp_surfaces (tmp_path , monkeypatch ):
177177 """Test mne make_scalp_surfaces."""
178178 pytest .importorskip ("nibabel" )
179179 pytest .importorskip ("pyvista" )
180180 check_usage (mne_make_scalp_surfaces )
181181 has = "SUBJECTS_DIR" in os .environ
182- # Copy necessary files to avoid FreeSurfer call
183- tempdir = str (tmp_path )
184- surf_path = op .join (subjects_dir , "sample" , "surf" )
185- surf_path_new = op .join (tempdir , "sample" , "surf" )
186- os .mkdir (op .join (tempdir , "sample" ))
187- os .mkdir (surf_path_new )
188- subj_dir = op .join (tempdir , "sample" , "bem" )
189- os .mkdir (subj_dir )
182+ freesurfer_home = os .environ .get ("FREESURFER_HOME" )
190183
191- cmd = ("-s" , "sample" , "--subjects-dir" , tempdir )
192184 monkeypatch .setattr (
193185 mne .bem ,
194186 "decimate_surface" ,
195187 lambda points , triangles , n_triangles : (points , triangles ),
196188 )
197- dense_fname = op .join (subj_dir , "sample-head-dense.fif" )
198- medium_fname = op .join (subj_dir , "sample-head-medium.fif" )
189+
190+ tempdir = str (tmp_path )
191+ t1_path = op .join (subjects_dir , "sample" , "mri" , "T1.mgz" )
192+ t1_path_new = op .join (tempdir , "sample" , "mri" , "T1.mgz" )
193+
194+ headseg_path = op .join (subjects_dir , "sample" , "mri" , "seghead.mgz" )
195+ headseg_path_new = op .join (tempdir , "sample" , "mri" , "seghead.mgz" )
196+
197+ surf_path = op .join (subjects_dir , "sample" , "surf" , "lh.seghead" )
198+ surf_path_new = op .join (tempdir , "sample" , "surf" , "lh.seghead" )
199+
200+ dense_fname = op .join (tempdir , "sample" , "bem" , "sample-head-dense.fif" )
201+ medium_fname = op .join (tempdir , "sample" , "bem" , "sample-head-medium.fif" )
202+ sparse_fname = op .join (tempdir , "sample" , "bem" , "sample-head-sparse.fif" )
203+
204+ os .makedirs (op .join (tempdir , "sample" , "mri" ), exist_ok = True )
205+ os .makedirs (op .join (tempdir , "sample" , "surf" ), exist_ok = True )
206+
207+ shutil .copy (t1_path , t1_path_new )
208+ shutil .copy (headseg_path , headseg_path_new )
209+ shutil .copy (surf_path , surf_path_new )
210+
211+ cmd = (
212+ "-s" ,
213+ "sample" ,
214+ "--subjects-dir" ,
215+ tempdir ,
216+ "--no-decimate" ,
217+ "--reuse-seghead" ,
218+ )
199219 with ArgvSetter (cmd , disable_stdout = False , disable_stderr = False ):
200220 monkeypatch .delenv ("FREESURFER_HOME" , raising = False )
201- with pytest .raises (RuntimeError , match = "The FreeSurfer environ " ):
221+ with pytest .raises (RuntimeError , match = "The FREESURFER_HOME environment " ):
202222 mne_make_scalp_surfaces .run ()
203- shutil . copy ( op . join ( surf_path , "lh.seghead" ), surf_path_new )
204- monkeypatch .setenv ("FREESURFER_HOME" , tempdir )
223+
224+ monkeypatch .setenv ("FREESURFER_HOME" , freesurfer_home )
205225 mne_make_scalp_surfaces .run ()
226+
227+ assert op .isfile (headseg_path_new )
228+ assert op .isfile (surf_path_new )
229+ assert op .isfile (dense_fname )
230+ assert not op .isfile (medium_fname )
231+ assert not op .isfile (sparse_fname )
232+
233+ # actually check the outputs
234+ head_py = read_bem_surfaces (dense_fname )
235+ assert_equal (len (head_py ), 1 )
236+ head_py = head_py [0 ]
237+ head_c = read_bem_surfaces (
238+ op .join (subjects_dir , "sample" , "bem" , "sample-head-dense.fif" )
239+ )[0 ]
240+ assert_allclose (head_py ["rr" ], head_c ["rr" ])
241+
242+ cmd = ("-s" , "sample" , "--subjects-dir" , tempdir )
243+ with ArgvSetter (cmd , disable_stdout = False , disable_stderr = False ):
244+ with pytest .raises (OSError , match = "use --overwrite to overwrite it" ):
245+ mne_make_scalp_surfaces .run ()
246+
247+ cmd = ("-s" , "sample" , "--subjects-dir" , tempdir , "--overwrite" )
248+ with ArgvSetter (cmd , disable_stdout = False , disable_stderr = False ):
249+ mne_make_scalp_surfaces .run ()
250+ assert op .isfile (headseg_path_new )
251+ assert op .isfile (surf_path_new )
206252 assert op .isfile (dense_fname )
207253 assert op .isfile (medium_fname )
208- with pytest .raises (OSError , match = "overwrite" ):
254+ assert op .isfile (sparse_fname )
255+
256+ os .remove (headseg_path_new )
257+ os .remove (surf_path_new )
258+ os .remove (dense_fname )
259+ cmd = ("-s" , "sample" , "--subjects-dir" , tempdir , "--no-decimate" )
260+ with ArgvSetter (cmd , disable_stdout = False , disable_stderr = False ):
261+ with pytest .raises (OSError , match = "Trying to generate new scalp surfaces" ):
262+ mne_make_scalp_surfaces .run ()
263+
264+ os .remove (medium_fname )
265+ os .remove (sparse_fname )
266+ cmd = (
267+ "-s" ,
268+ "sample" ,
269+ "--subjects-dir" ,
270+ tempdir ,
271+ "--no-decimate" ,
272+ "--reuse-seghead" ,
273+ )
274+ with ArgvSetter (cmd , disable_stdout = False , disable_stderr = False ):
275+ with pytest .raises (ValueError , match = "No existing scalp surface found" ):
209276 mne_make_scalp_surfaces .run ()
210- # actually check the outputs
211- head_py = read_bem_surfaces (dense_fname )
212- assert_equal (len (head_py ), 1 )
213- head_py = head_py [0 ]
214- head_c = read_bem_surfaces (
215- op .join (subjects_dir , "sample" , "bem" , "sample-head-dense.fif" )
216- )[0 ]
217- assert_allclose (head_py ["rr" ], head_c ["rr" ])
277+
278+ cmd = ("-s" , "sample" , "--subjects-dir" , tempdir , "--no-decimate" , "--overwrite" )
279+ with ArgvSetter (cmd , disable_stdout = False , disable_stderr = False ):
280+ mne_make_scalp_surfaces .run ()
281+ assert op .isfile (headseg_path_new )
282+ assert op .isfile (surf_path_new )
283+ assert op .isfile (dense_fname )
284+ assert not op .isfile (medium_fname )
285+ assert not op .isfile (sparse_fname )
286+
287+ cmd = ("-s" , "sample" , "--subjects-dir" , tempdir , "--no-decimate" , "--overwrite" )
288+ with ArgvSetter (cmd , disable_stdout = False , disable_stderr = False ):
289+ mne_make_scalp_surfaces .run ()
290+ assert op .isfile (headseg_path_new )
291+ assert op .isfile (surf_path_new )
292+ assert op .isfile (dense_fname )
293+ assert not op .isfile (medium_fname )
294+ assert not op .isfile (sparse_fname )
295+
218296 if not has :
219297 assert "SUBJECTS_DIR" not in os .environ
220298
0 commit comments