def test_BrainMask_outputs(): output_map = dict(out_file=dict(), ) outputs = BrainMask.output_spec() for key, metadata in output_map.items(): for metakey, value in metadata.items(): yield assert_equal, getattr(outputs.traits()[key], metakey), value
def brain_extraction_pipeline(self, **name_maps): """ Generates a whole brain mask using MRtrix's 'dwi2mask' command Parameters ---------- mask_tool: Str Can be either 'bet' or 'dwi2mask' depending on which mask tool you want to use """ if self.branch('bet_method', 'mrtrix'): pipeline = self.new_pipeline( 'brain_extraction', desc="Generate brain mask from b0 images", citations=[mrtrix_cite], name_maps=name_maps) if self.provided('coreg_ref'): series = 'series_coreg' else: series = 'series_preproc' # Create mask node masker = pipeline.add( 'dwi2mask', BrainMask( out_file='brain_mask.nii.gz'), inputs={ 'in_file': (series, nifti_gz_format), 'grad_fsl': self.fsl_grads(pipeline, coregistered=False)}, outputs={ 'brain_mask': ('out_file', nifti_gz_format)}, requirements=[mrtrix_req.v('3.0rc3')]) merge = pipeline.add( 'merge_operands', Merge(2), inputs={ 'in1': ('mag_preproc', nifti_gz_format), 'in2': (masker, 'out_file')}) pipeline.add( 'apply_mask', MRCalc( operation='multiply'), inputs={ 'operands': (merge, 'out')}, outputs={ 'brain': ('out_file', nifti_gz_format)}, requirements=[mrtrix_req.v('3.0rc3')]) else: pipeline = super().brain_extraction_pipeline(**name_maps) return pipeline
def brain_extraction_pipeline(self, **kwargs): # @UnusedVariable @IgnorePep8 """ Generates a whole brain mask using MRtrix's 'dwi2mask' command Parameters ---------- mask_tool: Str Can be either 'bet' or 'dwi2mask' depending on which mask tool you want to use """ if self.branch('brain_extract_method', 'mrtrix'): pipeline = self.create_pipeline( 'brain_extraction', inputs=[ DatasetSpec('preproc', nifti_gz_format), DatasetSpec('grad_dirs', fsl_bvecs_format), DatasetSpec('bvalues', fsl_bvals_format) ], outputs=[DatasetSpec('brain_mask', nifti_gz_format)], desc="Generate brain mask from b0 images", version=1, citations=[mrtrix_cite], **kwargs) # Create mask node dwi2mask = pipeline.create_node(BrainMask(), name='dwi2mask', requirements=[mrtrix3_req]) dwi2mask.inputs.out_file = 'brain_mask.nii.gz' # Gradient merge node grad_fsl = pipeline.create_node(MergeTuple(2), name="grad_fsl") # Connect nodes pipeline.connect(grad_fsl, 'out', dwi2mask, 'grad_fsl') # Connect inputs pipeline.connect_input('grad_dirs', grad_fsl, 'in1') pipeline.connect_input('bvalues', grad_fsl, 'in2') pipeline.connect_input('preproc', dwi2mask, 'in_file') # Connect outputs pipeline.connect_output('brain_mask', dwi2mask, 'out_file') # Check inputs/outputs are connected pipeline.assert_connected() else: pipeline = super(DiffusionStudy, self).brain_extraction_pipeline(**kwargs) return pipeline
def brain_extraction_pipeline(self, **name_maps): # @UnusedVariable @IgnorePep8 """ Generates a whole brain mask using MRtrix's 'dwi2mask' command Parameters ---------- mask_tool: Str Can be either 'bet' or 'dwi2mask' depending on which mask tool you want to use """ if self.branch('brain_extract_method', 'mrtrix'): pipeline = self.new_pipeline( 'brain_extraction', desc="Generate brain mask from b0 images", references=[mrtrix_cite], name_maps=name_maps) # Gradient merge node grad_fsl = pipeline.add( "grad_fsl", MergeTuple(2), inputs={ 'in1': ('grad_dirs', fsl_bvecs_format), 'in2': ('bvalues', fsl_bvals_format)}) # Create mask node pipeline.add( 'dwi2mask', BrainMask( out_file='brain_mask.nii.gz'), inputs={ 'in_file': ('preproc', nifti_gz_format)}, connect={ 'grad_fsl': (grad_fsl, 'out')}, outputs={ 'out_file': ('brain_mask', nifti_gz_format)}, requirements=[mrtrix_req.v('3.0rc3')]) else: pipeline = super(DmriStudy, self).brain_extraction_pipeline( **name_maps) return pipeline
def test_BrainMask_inputs(): input_map = dict( args=dict(argstr='%s', ), bval_scale=dict(argstr='-bvalue_scaling %s', ), environ=dict( nohash=True, usedefault=True, ), grad_file=dict(argstr='-grad %s', ), grad_fsl=dict(argstr='-fslgrad %s %s', ), ignore_exception=dict( nohash=True, usedefault=True, ), in_bval=dict(), in_bvec=dict(argstr='-fslgrad %s %s', ), in_file=dict( argstr='%s', mandatory=True, position=-2, ), nthreads=dict( argstr='-nthreads %d', nohash=True, ), out_file=dict( argstr='%s', mandatory=True, position=-1, usedefault=True, ), terminal_output=dict(nohash=True, ), ) inputs = BrainMask.input_spec() for key, metadata in input_map.items(): for metakey, value in metadata.items(): yield assert_equal, getattr(inputs.traits()[key], metakey), value
def test_BrainMask_inputs(): input_map = dict(args=dict(argstr='%s', ), bval_scale=dict(argstr='-bvalue_scaling %s', ), environ=dict(nohash=True, usedefault=True, ), grad_file=dict(argstr='-grad %s', ), grad_fsl=dict(argstr='-fslgrad %s %s', ), ignore_exception=dict(nohash=True, usedefault=True, ), in_bval=dict(), in_bvec=dict(argstr='-fslgrad %s %s', ), in_file=dict(argstr='%s', mandatory=True, position=-2, ), nthreads=dict(argstr='-nthreads %d', nohash=True, ), out_file=dict(argstr='%s', mandatory=True, position=-1, usedefault=True, ), terminal_output=dict(nohash=True, ), ) inputs = BrainMask.input_spec() for key, metadata in input_map.items(): for metakey, value in metadata.items(): yield assert_equal, getattr(inputs.traits()[key], metakey), value
def preprocess_pipeline(self, **name_maps): """ Performs a series of FSL preprocessing steps, including Eddy and Topup Parameters ---------- phase_dir : str{AP|LR|IS} The phase encode direction """ # Determine whether we can correct for distortion, i.e. if reference # scans are provided # Include all references references = [fsl_cite, eddy_cite, topup_cite, distort_correct_cite, n4_cite] if self.branch('preproc_denoise'): references.extend(dwidenoise_cites) pipeline = self.new_pipeline( name='preprocess', name_maps=name_maps, desc=( "Preprocess dMRI studies using distortion correction"), citations=references) # Create nodes to gradients to FSL format if self.input('series').format == dicom_format: extract_grad = pipeline.add( "extract_grad", ExtractFSLGradients(), inputs={ 'in_file': ('series', dicom_format)}, outputs={ 'grad_dirs': ('bvecs_file', fsl_bvecs_format), 'bvalues': ('bvals_file', fsl_bvals_format)}, requirements=[mrtrix_req.v('3.0rc3')]) grad_fsl_inputs = {'in1': (extract_grad, 'bvecs_file'), 'in2': (extract_grad, 'bvals_file')} elif self.provided('grad_dirs') and self.provided('bvalues'): grad_fsl_inputs = {'in1': ('grad_dirs', fsl_bvecs_format), 'in2': ('bvalues', fsl_bvals_format)} else: raise BananaUsageError( "Either input 'magnitude' image needs to be in DICOM format " "or gradient directions and b-values need to be explicitly " "provided to {}".format(self)) # Gradient merge node grad_fsl = pipeline.add( "grad_fsl", MergeTuple(2), inputs=grad_fsl_inputs) gradients = (grad_fsl, 'out') # Create node to reorient preproc out_file if self.branch('reorient2std'): reorient = pipeline.add( 'fslreorient2std', fsl.utils.Reorient2Std( output_type='NIFTI_GZ'), inputs={ 'in_file': ('series', nifti_gz_format)}, requirements=[fsl_req.v('5.0.9')]) reoriented = (reorient, 'out_file') else: reoriented = ('series', nifti_gz_format) # Denoise the dwi-scan if self.branch('preproc_denoise'): # Run denoising denoise = pipeline.add( 'denoise', DWIDenoise(), inputs={ 'in_file': reoriented}, requirements=[mrtrix_req.v('3.0rc3')]) # Calculate residual noise subtract_operands = pipeline.add( 'subtract_operands', Merge(2), inputs={ 'in1': reoriented, 'in2': (denoise, 'noise')}) pipeline.add( 'subtract', MRCalc( operation='subtract'), inputs={ 'operands': (subtract_operands, 'out')}, outputs={ 'noise_residual': ('out_file', mrtrix_image_format)}, requirements=[mrtrix_req.v('3.0rc3')]) denoised = (denoise, 'out_file') else: denoised = reoriented # Preproc kwargs preproc_kwargs = {} preproc_inputs = {'in_file': denoised, 'grad_fsl': gradients} if self.provided('reverse_phase'): if self.provided('magnitude', default_okay=False): dwi_reference = ('magnitude', mrtrix_image_format) else: # Extract b=0 volumes dwiextract = pipeline.add( 'dwiextract', ExtractDWIorB0( bzero=True, out_ext='.nii.gz'), inputs={ 'in_file': denoised, 'fslgrad': gradients}, requirements=[mrtrix_req.v('3.0rc3')]) # Get first b=0 from dwi b=0 volumes extract_first_b0 = pipeline.add( "extract_first_vol", MRConvert( coord=(3, 0)), inputs={ 'in_file': (dwiextract, 'out_file')}, requirements=[mrtrix_req.v('3.0rc3')]) dwi_reference = (extract_first_b0, 'out_file') # Concatenate extracted forward rpe with reverse rpe combined_images = pipeline.add( 'combined_images', MRCat(), inputs={ 'first_scan': dwi_reference, 'second_scan': ('reverse_phase', mrtrix_image_format)}, requirements=[mrtrix_req.v('3.0rc3')]) # Create node to assign the right PED to the diffusion prep_dwi = pipeline.add( 'prepare_dwi', PrepareDWI(), inputs={ 'pe_dir': ('ped', float), 'ped_polarity': ('pe_angle', float)}) preproc_kwargs['rpe_pair'] = True distortion_correction = True preproc_inputs['se_epi'] = (combined_images, 'out_file') else: distortion_correction = False preproc_kwargs['rpe_none'] = True if self.parameter('preproc_pe_dir') is not None: preproc_kwargs['pe_dir'] = self.parameter('preproc_pe_dir') preproc = pipeline.add( 'dwipreproc', DWIPreproc( no_clean_up=True, out_file_ext='.nii.gz', # FIXME: Need to determine this programmatically # eddy_parameters = '--data_is_shelled ' temp_dir='dwipreproc_tempdir', **preproc_kwargs), inputs=preproc_inputs, outputs={ 'eddy_par': ('eddy_parameters', eddy_par_format)}, requirements=[mrtrix_req.v('3.0rc3'), fsl_req.v('5.0.10')], wall_time=60) if distortion_correction: pipeline.connect(prep_dwi, 'pe', preproc, 'pe_dir') mask = pipeline.add( 'dwi2mask', BrainMask( out_file='brainmask.nii.gz'), inputs={ 'in_file': (preproc, 'out_file'), 'grad_fsl': gradients}, requirements=[mrtrix_req.v('3.0rc3')]) # Create bias correct node pipeline.add( "bias_correct", DWIBiasCorrect( method='ants'), inputs={ 'grad_fsl': gradients, # internal 'in_file': (preproc, 'out_file'), 'mask': (mask, 'out_file')}, outputs={ 'series_preproc': ('out_file', nifti_gz_format)}, requirements=[mrtrix_req.v('3.0rc3'), ants_req.v('2.0')]) return pipeline