MRtrix3 Workflow#
MRtrix3-based tractography workflow. Sibling of the HCP package: same data layout and ROI pipeline, but the FSL BedpostX + ProbTrackX2 tail is replaced with 5ttgen + dhollander + msmt_csd + mtnormalise + tckgen (ACT) + tcksift2 + tckmap.
thesis.workflows.mrtrix3.workflow#
MRtrix3 tractography workflow orchestration.
Sibling of the HCP ProbTrackX2 workflow that swaps the FSL BedpostX +
ProbTrackX2 tail for MRtrix3 (5ttgen → dhollander → msmt_csd →
mtnormalise → tckgen with ACT → tcksift2 → tckmap). The ROI extraction
pipeline (atlas → ANTs warp → validation → merging → DWI-grid resampling)
reuses the HCP MapNode/JoinNode factories.
Outputs land under <output_dir>/tractography/mrtrix3/:
_shared/— patient-level intermediates (mask, 5TT, GM-WM interface, response functions, FODs, normalised FODs).Per-hemisphere tractography products plus
tracks.tck,sift2_weights.txt,fdt_paths.nii.gz, andwaytotal.
Bases:
objectHandles to the patient-level shared nodes (FOD, 5TT, mask, gmwmi).
- Parameters:
- thesis.workflows.mrtrix3.workflow.build_workflow(*, t1, mask, dwi, bvec, bval, tract_dir, config, context)[source]#
Build the MRtrix3 tractography workflow for one patient.
Hemisphere modes via
config.tractography.hemisphere:"left"/"right"(single hemisphere via fixed input),"both"(merged ROIs, default),"both-separately"(two hemispheres via Nipypeiterables).- Parameters:
t1 (
Path)mask (
Path)dwi (
Path)bvec (
Path)bval (
Path)tract_dir (
Path)config (
PipelineConfig)context (
ProcessingContext)
- Return type:
Workflow
Configuration Helpers#
MRtrix3 path resolution and preparation.
Resolves DWI image, gradient table, brain mask, T1, and per-patient output locations from the pipeline config. Reuses the HCP path conventions since the upstream data layout is identical (HCP-style preprocessed subjects); only the tractography backend differs.
- class thesis.workflows.mrtrix3.config.paths.MRtrix3Paths[source]#
Bases:
TypedDictResolved filesystem paths for the MRtrix3 workflow.
- thesis.workflows.mrtrix3.config.paths.prepare_mrtrix3_paths(config, context)[source]#
Resolve filesystem paths required by the MRtrix3 workflow.
- Parameters:
config (
PipelineConfig)context (
ProcessingContext)
- Return type:
MRtrix3 tractography parameter extraction.
Reads parameters from thesis.core.config.TractographyConfig (the
MRtrix-specific fields added there) and produces a flat TypedDict that
the node builders consume.
- class thesis.workflows.mrtrix3.config.params.MRtrix3Params[source]#
Bases:
TypedDictRuntime parameters for MRtrix3 tractography nodes.
- thesis.workflows.mrtrix3.config.params.prepare_mrtrix3_params(config)[source]#
Extract MRtrix3 tractography parameters from the pipeline config.
- Parameters:
config (
PipelineConfig) – Validated pipeline configuration.- Return type:
- Returns:
Flat dictionary of parameters consumed by MRtrix3 node builders.
MRtrix3 configuration value resolution.
The mrtrix3: block is an optional dict field on PipelineConfig
(default: empty dict). This helper unifies attribute-style and dict-style
access so callers can read overrides without caring how the loader chose
to materialise the block.
- thesis.workflows.mrtrix3.config.values.resolve_mrtrix3_value(config, key, default=None)[source]#
Read a value from
config.mrtrix3(dict or attribute) with a default.config.mrtrix3is declared as aDict[str, Any]onPipelineConfig, but this helper also accepts an attribute-style object for forward-compatibility with a future typed model.- Parameters:
config (
PipelineConfig) – Pipeline configuration.key (
str) – Configuration key to retrieve.default (
Optional[str]) – Value returned when the key is missing.
- Return type:
- Returns:
The configured value as a string, or
default.
Nodes#
DWI import and brain-mask preparation nodes.
- thesis.workflows.mrtrix3.nodes.preprocessing.prepare_dwi_import(dwi_image, bvec, bval, out_dir, name='dwi_import')[source]#
Convert NIfTI DWI + FSL gradient table to
dwi.mif.The resulting
.mifbundles the gradient table so downstream interfaces (ResponseSD,EstimateFOD) do not need to be repeatedly told aboutbvecs/bvals.- Parameters:
- Return type:
Node- Returns:
Configured Nipype Node.
- thesis.workflows.mrtrix3.nodes.preprocessing.prepare_mask_node(mask_path, out_dir, dilate_voxels=1, name='mask_prep')[source]#
Import the FSL brain mask and dilate it for MRtrix consumption.
Wrapped as a Function node (rather than chaining
MRConvertandMaskFilter) so the dilation pass count is preserved as a single visible parameter and the output filename is deterministic.- Parameters:
- Return type:
Node- Returns:
Configured Nipype Node with
out_fileoutput.
- thesis.workflows.mrtrix3.nodes.preprocessing.prepare_dwi2mask_node(out_dir, dilate_voxels=1, algorithm='legacy', name='mask_prep')[source]#
Generate the brain mask from the DWI via
dwi2mask(mask_source=’dwi2mask’).Drop-in alternative to
prepare_mask_node(): produces the samemask.mifoutput field, but derives the mask from the imported DWI.mifinstead of importing the FSLnodif_brain_mask. The workflow wiresdwi_filefrom thedwi_importnode’sout_fileat build time.- Parameters:
out_dir (
Path) – Output directory wheremask.mifis written.dilate_voxels (
int) – Number of dilation passes (0 disables dilation).algorithm (
str) –dwi2maskalgorithm selector (default"legacy").name (
str) – Nipype node name (default"mask_prep"so it is a true drop-in forprepare_mask_node()).
- Return type:
Node- Returns:
Configured Nipype Node with
out_fileoutput and an unconnecteddwi_fileinput (wired by the workflow).
5TT and GM-WM interface segmentation nodes.
These wrap the MRtrix3 Python scripts 5ttgen and 5tt2gmwmi via
subprocess rather than Nipype’s Generate5tt / Generate5tt2gmwmi
interfaces. Nipype places args before the positional algorithm
argument, and the resulting -force flag lands in a position the script
does not honour — so a restart cannot overwrite an existing output. The
Function-node wrappers pre-delete the target and pass -force after the
positional args, mirroring the tckgen / tcksift2 wrappers.
- thesis.workflows.mrtrix3.nodes.segmentation.prepare_5tt_node(out_dir, algorithm='fsl', t1_path=None, parc_path=None, name='fivett')[source]#
Run
5ttgento produce the 5-tissue-type segmentation used by ACT.Two algorithm modes are supported:
"fsl"consumes a T1-weighted image (t1_path). This is the historical default; FSL FAST drives the segmentation."freesurfer"consumes a FreeSurfer-LUT parcellation (parc_path). In this pipeline the parcellation is wired at runtime by the meta-workflow from the SynthSeg output (which uses the FreeSurfer LUT and covers brainstem/cerebellum robustly).
Both input fields are always exposed on the resulting node — the unused one is set to
""so a meta-workflow can override either at runtime without surprises.- Parameters:
out_dir (
Path) – Output directory;5tt.mifis written here.algorithm (
str) – 5ttgen backend ("fsl"or"freesurfer").t1_path (
Optional[Path]) – Optional T1 image path (used byalgorithm="fsl").parc_path (
Optional[Path]) – Optional FreeSurfer-LUT parcellation path (used byalgorithm="freesurfer").name (
str) – Nipype node name.
- Return type:
Node- Returns:
Configured Nipype Function Node with output field
out_file. Exposed input fields:output_dir,algorithm,t1_path,parc_path,freesurfer_home.
- thesis.workflows.mrtrix3.nodes.segmentation.prepare_5tt_to_dwi_node(out_dir, reference_image=None, name='fivett_to_dwi')[source]#
Warp the 5TT from T1 grid into the DWI grid for ACT.
When the meta-workflow connects
t1_to_dwi_transformfrompreprocess.dwi_to_t1_registration.composite_transform, this node usesantsApplyTransformswith the inverted transform to bring the 5TT into the DWI world. When unconnected (standalone runs on HCP-preprocessed data where T1 and DWI already share a world), the input.mifis converted to.nii.gzand passed through unchanged.The
fivett_fileandt1_to_dwi_transforminputs are wired by the workflow.referenceis set statically ifreference_imageexists at build time, and can be overridden by the meta-workflow with the live eddy brain mask.- Parameters:
- Return type:
Node- Returns:
Configured Nipype Function Node with output field
out_file.
- thesis.workflows.mrtrix3.nodes.segmentation.prepare_gmwmi_node(out_dir, name='gmwmi')[source]#
Run
5tt2gmwmito produce the GM-WM interface mask.The 5TT input is wired in by the workflow, not set statically here.
Response function, FOD estimation, and intensity normalisation nodes.
- thesis.workflows.mrtrix3.nodes.fod.prepare_response_node(out_dir, algorithm='dhollander', name='response')[source]#
Configure
dwi2response(multi-tissue response estimation).The DWI
in_fileandin_maskinputs are wired by the workflow. Output filenames are pinned so the artefacts land predictably inout_dir.
- thesis.workflows.mrtrix3.nodes.fod.prepare_fod_node(out_dir, algorithm='msmt_csd', name='fod')[source]#
Configure
dwi2fod(constrained spherical deconvolution).
- thesis.workflows.mrtrix3.nodes.fod.prepare_mtnormalise_node(out_dir, name='mtnormalise')[source]#
Configure
mtnormalise(intensity bias correction across tissues).Always produces normalised WM, GM, and CSF volumes; the workflow only builds this node for the multi-tissue (
msmt_csd) path.
tckgen, tcksift2, tckmap, and waytotal writer nodes.
- thesis.workflows.mrtrix3.nodes.tractography.prepare_tckgen_node(params, out_dir, name='tckgen')[source]#
Configure
tckgenas a Function node wrapping the binary directly.Nipype’s
Tractographyinterface declaresroi_incl/roi_exclas single-value traits, which cannot represent the multiple-includeand-excludeflags MRtrix3 supports. This builder uses a Function node that constructs the command line itself so any number of include and exclude masks can be wired through.ACT-specific flags (
-backtrack,-crop_at_gmwmi) are set from theparamsdict; the 5TT input is always passed asact_fileand the workflow controls the wiring.- Parameters:
params (
MRtrix3Params) – MRtrix3 parameter dictionary.out_dir (
Path) – Output directory;tracks.tckis written here.name (
str) – Nipype node name.
- Return type:
Node- Returns:
Configured Nipype Function Node with output field
out_file.
- thesis.workflows.mrtrix3.nodes.tractography.prepare_tcksift2_node(out_dir, name='tcksift2')[source]#
Configure
tcksift2(per-streamline weighting + mu coefficient).No Nipype interface exists for tcksift2, so this is a Function node wrapping a subprocess call. The streamline file, FOD, and 5TT inputs are wired by the workflow.
- thesis.workflows.mrtrix3.nodes.tractography.prepare_tckmap_node(template_image, out_dir, name='tckmap')[source]#
Configure
tckmap(track-density imaging).SIFT2 weighting is controlled entirely by the workflow, which decides whether to wire the SIFT2 weights file into
tck_weights.- Parameters:
- Return type:
Node- Returns:
Configured Nipype Node.
- thesis.workflows.mrtrix3.nodes.tractography.prepare_density_map_renamer(out_dir, name='fdt_paths_writer')[source]#
Copy the tckmap output to
fdt_paths.nii.gzfor downstream tools.
- thesis.workflows.mrtrix3.nodes.tractography.prepare_waytotal_writer(out_dir, name='waytotal_writer')[source]#
Write a single-line
waytotalfile for atlas / tract_similarity.Declares an optional
weights_fileinput so callers can wire the SIFT2 weights file when present; the underlying task sums those weights instead of relying ontckinfo -count.
- thesis.workflows.mrtrix3.nodes.tractography.prepare_per_target_maps_node(out_dir, name='per_target_maps')[source]#
Configure the per-target
seeds_to_<target>.nii.gzwriter.Wraps
make_per_target_maps_task(tckedit + tckmap) so MRtrix3 matches the ProbTrackX2 per-target filename layout thatqc.checks.collect_connectivity_map_statsalready consumes.
- thesis.workflows.mrtrix3.nodes.tractography.prepare_mrtrix3_params_writer(out_dir, select, name='mrtrix3_params_writer')[source]#
Write
tractography_params.json(backend, select, mu) for stats.
- thesis.workflows.mrtrix3.nodes.tractography.prepare_tckgen_input_adapter(name='tckgen_inputs')[source]#
Create the adapter node that maps canonical ROI fields to tckgen inputs.
Inputs (connected by the workflow):
seed,waypoints_file,target_mask,avoid_mask,stop_mask.Outputs:
seed_image(str),include_masks(list[str]),exclude_masks(list[str]),mask_stop(str).- Parameters:
name (
str)- Return type:
Node
Operations#
Brain-mask preparation for the MRtrix3 workflow.
Imports the FSL nodif_brain_mask.nii.gz to MRtrix’s native .mif
format and optionally dilates it. The dilation step prevents the white-
matter gaps that dwi2mask legacy produces in the cerebral peduncle and
other deep WM (a verified failure mode that killed tractography during
the manual MRtrix3 validation run).
Used as the body of a Nipype Function node so the workflow graph captures the dilation parameter and output path explicitly.
- thesis.workflows.mrtrix3.operations.mask_prep.prepare_brain_mask_task(mask_path, output_dir, dilate_voxels=1)[source]#
Import the FSL brain mask into
mask.mifwith optional dilation.
- thesis.workflows.mrtrix3.operations.mask_prep.generate_dwi2mask_task(dwi_file, output_dir, dilate_voxels=1, algorithm='legacy')[source]#
Generate a brain mask from the DWI via MRtrix3
dwi2mask.Alternative to
prepare_brain_mask_task(): instead of importing and dilating the FSLnodif_brain_mask, this derives the mask directly from the (already.mif) DWI series withdwi2mask. An optional dilation pass widens the mask the same wayprepare_brain_mask_task()does, so downstream ACT does not lose deep white matter.- Parameters:
- Return type:
- Returns:
Absolute path to the produced
.mifmask.
Density-map post-processing for MRtrix3 tractography outputs.
Bundles the steps that do not have Nipype interfaces:
run_tcksift2_task— wrapstcksift2(per-streamline weighting) and captures the proportionality coefficientmuvia-out_mu.muis required to make SIFT2 weight sums comparable across subjects and methods (units: fibre cross-sectional area).rename_density_map_task— moves theComputeTDIoutput to thefdt_paths.nii.gzfilename consumed by the existing tract_similarity workflow, preserving compatibility with the ProbTrackX2 layout.write_waytotal_task— writes a single-linewaytotalfile. When a SIFT2 weights file is supplied, the value is the sum of those weights (matches the units of the SIFT2-weighted TDI). Otherwise the raw streamline count fromtckinfois written.read_sift2_mu— module-level helper that readssift2_mu.txtproduced bytcksift2 -out_mu.
- thesis.workflows.mrtrix3.operations.density_map.run_tcksift2_task(tracks_file, fod_file, fivett_file, output_dir)[source]#
Run
tcksift2to produce per-streamline weights and mu.- Parameters:
- Return type:
- Returns:
(weights_file, mu_file)— absolute paths to the per-streamline weights file and the single-floatmuproportionality coefficient file.
- thesis.workflows.mrtrix3.operations.density_map.read_sift2_mu(mu_file)[source]#
Read the single-float
muproportionality coefficient.- Parameters:
mu_file – Path to
sift2_mu.txtwritten bytcksift2 -out_mu.- Returns:
muas a float, orNoneif the file is missing or empty.
- thesis.workflows.mrtrix3.operations.density_map.rename_density_map_task(in_file, output_dir)[source]#
Copy the TDI output to
fdt_paths.nii.gzfor downstream compatibility.The MRtrix tckmap interface writes to a temporary path inside the Nipype run-dir; downstream consumers (tract_similarity) expect the file to live next to a
waytotalunder the run output directory.
- thesis.workflows.mrtrix3.operations.density_map.write_waytotal_task(tracks_file, output_dir, weights_file=None)[source]#
Write a single-line
waytotalfile matching the TDI denominator.The downstream atlas / tract_similarity readers divide the TDI (
fdt_paths.nii.gz) by this value to get a streamline fraction. The written value therefore must match the units of the TDI numerator:When
weights_filepoints at a SIFT2 weights text file (sift2_weights.txt), the TDI was generated with-tck_weights_inand each voxel holds a sum of weights. The correct denominator is the sum of all per-streamline weights; we compute it here.When
weights_fileisNoneor missing, the TDI is a raw streamline count, so we fall back totckinfo -count.
- Parameters:
- Return type:
- Returns:
Absolute path to the written
waytotalfile.
Adapter from the canonical 5-tuple ROI fields to tckgen’s input shape.
The reused HCP ROI pipeline produces:
seed— single-path string for the seed maskwaypoints_file— text file listing one waypoint mask path per linestop_mask,avoid_mask,target_mask— single-path strings (or empty)
MRtrix’s tckgen instead expects:
-seed_image— single path-include— repeated flag; the Nipype interface accepts a list underroi_incl-exclude— repeated flag;roi_excl-mask— single path;roi_mask
This task adapts between the two shapes inside a single Function node so the workflow only has to wire the canonical fields once. Targets are folded into the include list (MRtrix has no separate target concept).
- thesis.workflows.mrtrix3.operations.tckgen_adapter.adapt_tckgen_inputs_task(seed, waypoints_file, target_mask, avoid_mask, stop_mask)[source]#
Translate canonical ROI fields into tckgen-friendly inputs.
- Parameters:
seed (
str) – Path to the seed mask. Required.waypoints_file (
str) – Optional text file listing one waypoint mask path per line. Empty string disables.target_mask (
str) – Optional path to a target mask, folded into the include list. Empty string disables.avoid_mask (
str) – Optional path to an avoid (exclude) mask. Empty string disables.stop_mask (
str) – Optional path to a stop mask (truncates streamlines on exit). Empty string disables.
- Returns:
Tuple of
(seed_image, include_masks, exclude_masks, mask_stop)whereinclude_masksandexclude_masksare lists of strings (possibly empty) andmask_stopis a string (possibly empty).