Skip to content

Commit

Permalink
Merge pull request #386 from effigies/enh/multithread-mrisexpand
Browse files Browse the repository at this point in the history
ENH: Annotate mris_expand with thread usage
  • Loading branch information
effigies authored Nov 18, 2023
2 parents 1c00cee + 899f2f4 commit 6ce2121
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
18 changes: 17 additions & 1 deletion smriprep/interfaces/freesurfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,21 @@ def _gen_filename(self, name):
return super()._gen_filename(name)


class MakeMidthickness(nwfs.MakeMidthickness):
class MakeMidthicknessInputSpec(nwfs._MakeMidthicknessInputSpec, fs.base.FSTraitedSpecOpenMP):
pass


class MakeMidthickness(nwfs.MakeMidthickness, fs.base.FSCommandOpenMP):
"""Patched MakeMidthickness interface
Ensures output filenames are specified with hemisphere labels, when appropriate.
This may not cover all use-cases in MRIsExpand, but we're just making midthickness
files.
This interface also sets the OMP_NUM_THREADS environment variable to floor(1.5x) the
number of threads requested by the user, as tests indicate that cores are underutilized
by a factor of 2/3.
>>> from smriprep.interfaces.freesurfer import MakeMidthickness
>>> mris_expand = MakeMidthickness(thickness=True, distance=0.5)
>>> mris_expand.inputs.in_file = 'lh.white'
Expand All @@ -316,9 +324,17 @@ class MakeMidthickness(nwfs.MakeMidthickness):
'mris_expand -thickness lh.white 0.5 rh.graymid'
"""

input_spec = MakeMidthicknessInputSpec

def _format_arg(self, name, trait_spec, value):
# FreeSurfer at some point changed whether it would add the hemi label onto the
# surface. Therefore we'll do it ourselves.
if name == "out_name":
value = self._associated_file(self.inputs.in_file, value)
return super()._format_arg(name, trait_spec, value)

def _num_threads_update(self):
if self.inputs.num_threads:
self.inputs.environ.update(
{"OMP_NUM_THREADS": str(self.inputs.num_threads * 3 // 2)}
)
9 changes: 3 additions & 6 deletions smriprep/workflows/surfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ def init_surface_recon_wf(
MakeMidthickness(thickness=True, distance=0.5, out_name="midthickness"),
iterfield="in_file",
name="midthickness",
n_procs=min(omp_nthreads, 12),
)

save_midthickness = pe.Node(nio.DataSink(parameterization=False), name="save_midthickness")

Check warning on line 226 in smriprep/workflows/surfaces.py

View check run for this annotation

Codecov / codecov/patch

smriprep/workflows/surfaces.py#L226

Added line #L226 was not covered by tests
Expand All @@ -232,7 +233,6 @@ def init_surface_recon_wf(
name="sync",
)

# fmt:off
workflow.connect([
# Configuration
(inputnode, recon_config, [('t1w', 't1w_list'),
Expand Down Expand Up @@ -275,21 +275,18 @@ def init_surface_recon_wf(
(save_midthickness, sync, [('out_file', 'filenames')]),
(sync, outputnode, [('subjects_dir', 'subjects_dir'),
('subject_id', 'subject_id')]),
])
# fmt:on
]) # fmt:skip

if "fsnative" not in precomputed.get("transforms", {}):
fsnative2t1w_xfm = pe.Node(

Check warning on line 281 in smriprep/workflows/surfaces.py

View check run for this annotation

Codecov / codecov/patch

smriprep/workflows/surfaces.py#L281

Added line #L281 was not covered by tests
RobustRegister(auto_sens=True, est_int_scale=True), name="fsnative2t1w_xfm"
)

# fmt:off
workflow.connect([

Check warning on line 285 in smriprep/workflows/surfaces.py

View check run for this annotation

Codecov / codecov/patch

smriprep/workflows/surfaces.py#L285

Added line #L285 was not covered by tests
(inputnode, fsnative2t1w_xfm, [('t1w', 'target_file')]),
(autorecon1, fsnative2t1w_xfm, [('T1', 'source_file')]),
(fsnative2t1w_xfm, outputnode, [('out_reg_file', 'fsnative2t1w_xfm')]),
])
# fmt:on
]) # fmt:skip

return workflow

Check warning on line 291 in smriprep/workflows/surfaces.py

View check run for this annotation

Codecov / codecov/patch

smriprep/workflows/surfaces.py#L291

Added line #L291 was not covered by tests

Expand Down

0 comments on commit 6ce2121

Please sign in to comment.