Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Annotate mris_expand with thread usage #386

Merged
merged 2 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion smriprep/interfaces/freesurfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,21 @@
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,18 @@
'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):
"""mris_expand"""
effigies marked this conversation as resolved.
Show resolved Hide resolved
if self.inputs.num_threads:
self.inputs.environ.update(

Check warning on line 339 in smriprep/interfaces/freesurfer.py

View check run for this annotation

Codecov / codecov/patch

smriprep/interfaces/freesurfer.py#L339

Added line #L339 was not covered by tests
{"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")
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(
RobustRegister(auto_sens=True, est_int_scale=True), name="fsnative2t1w_xfm"
)

# fmt:off
workflow.connect([
(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

Expand Down
Loading