Skip to content

dwi2mask synthstrip: automate GPU usage, fix two pre-existing bugs - #3443

Merged
Lestropie merged 1 commit into
MRtrix3:devfrom
Bryandero98:synthstrip-gpu-fallback-3436
Sep 16, 2026
Merged

Lestropie merged 1 commit into
MRtrix3:devfrom
Bryandero98:synthstrip-gpu-fallback-3436

Conversation

@Bryandero98

Copy link
Copy Markdown

Summary

Closes #3436. Automates GPU usage in dwi2mask synthstrip, mirroring the pattern already used by dwi2mask hdbet: attempt the GPU invocation first, and silently fall back to CPU if it fails, instead of requiring an explicit -gpu opt-in.

SynthStrip does not itself degrade gracefully when -g is requested but the hardware/drivers are unavailable — it just fails, taking dwi2mask down with it. So the -g flag is now requested speculatively on every run and dropped on failure, same as hd-bet in hdbet.py. The -gpu option, its options test entry, and its generated docs entry are all removed accordingly.

If both the GPU and CPU attempts fail, both MRtrixCmdErrors are combined into a single raised error using the same ===GPU=== / ===CPU=== report format hdbet.py uses, so a genuine double failure is still fully diagnosable.

Also fixed (same file, confirmed as pre-existing bugs by @Lestropie on the issue)

  • cmd was being built from the SYNTHSTRIP_CMD constant instead of the resolved synthstrip_cmd path returned by shutil.which(...). This meant a Singularity-only install (found via SYNTHSTRIP_SINGULARITY) would still try to invoke mri_synthstrip directly and fail.
  • SYNTHSTRIP_SINGULARITY was misspelled 'sythstrip-singularity' (missing the n), so it could never match the actual container/executable name.

Happy to fix them in the same PR or leave them out and file separately — Both can happen in the same PR.

Testing

  • PYTHONPATH="$(pwd)/python/lib" python -m pylint --rcfile=testing/pylint.rc --disable=unexpected-line-ending-format python/mrtrix3/commands/dwi2mask/synthstrip.py — clean, apart from a pre-existing R0801 duplicate-code note shared with hdbet.py (also present on dev before this change, from the two files' now-similar GPU/CPU fallback blocks).
  • Updated testing/scripts/tests/dwi2mask/synthstrip_options to drop the now-removed -gpu flag.
  • Regenerated the affected section of docs/reference/commands/dwi2mask.rst by hand to match docs/generate_user_docs.sh's output (removed the -gpu bullet only; no SynthStrip/mri_synthstrip installed locally to run the full doc generation script).

🤖 Generated with Claude Code

Mirror "dwi2mask hdbet"'s behaviour: attempt GPU execution first and
silently fall back to CPU if it fails, rather than requiring the user
to opt in via a "-gpu" option that SynthStrip itself does not
gracefully degrade from (a failed "-g" invocation simply aborts and
takes dwi2mask with it). The "-gpu" option is therefore removed, along
with its use in the options test and the generated docs entry.

On CPU fallback, both the GPU and CPU command errors are combined into
a single raised MRtrixCmdError (same "===GPU===" / "===CPU===" combined
report format used by hdbet.py), so a genuine failure on both paths is
still fully diagnosable.

While in this file, also fixed two pre-existing bugs confirmed by
@Lestropie on the issue:
- "cmd" was built from the SYNTHSTRIP_CMD constant instead of the
  resolved "synthstrip_cmd" path, so a Singularity-based install (found
  only via SYNTHSTRIP_SINGULARITY) would still try to invoke
  "mri_synthstrip" directly and fail.
- SYNTHSTRIP_SINGULARITY was misspelled as 'sythstrip-singularity'
  (missing the 'n'), so it could never actually match the intended
  executable name.

Verified with pylint (testing/pylint.rc), clean apart from a
pre-existing R0801 duplicate-code note shared with hdbet.py.

Fixes MRtrix3#3436

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@Lestropie Lestropie self-assigned this Sep 15, 2026
@Lestropie
Lestropie self-requested a review September 15, 2026 05:07

@Lestropie Lestropie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks as I would have done it myself. 👍

@Lestropie Lestropie linked an issue Sep 15, 2026 that may be closed by this pull request
4 tasks
@Lestropie

Copy link
Copy Markdown
Member

Clang tidy review Action is crashing out, but won't be caused by any change in this PR, the PR does not involve any C++ changes, and that Action is considered non-blocking anyway as it only provides code suggestions, so ignoring.

Thanks for the contribution!

@Lestropie
Lestropie merged commit 9b0a2f1 into MRtrix3:dev Sep 16, 2026
6 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dwi2mask synthstrip: Automate GPU usage

2 participants