def test_PRELUDE_outputs(): output_map = dict(unwrapped_phase_file=dict(), ) outputs = PRELUDE.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 _unwrap(fmap_data, mag_file, mask=None): from math import pi from nipype.interfaces.fsl import PRELUDE magnii = nb.load(mag_file) if mask is None: mask = np.ones_like(fmap_data, dtype=np.uint8) fmapmax = max(abs(fmap_data[mask > 0].min()), fmap_data[mask > 0].max()) fmap_data *= pi / fmapmax nb.Nifti1Image(fmap_data, magnii.affine).to_filename('fmap_rad.nii.gz') nb.Nifti1Image(mask, magnii.affine).to_filename('fmap_mask.nii.gz') nb.Nifti1Image(magnii.get_fdata(dtype='float32'), magnii.affine).to_filename('fmap_mag.nii.gz') # Run prelude res = PRELUDE(phase_file='fmap_rad.nii.gz', magnitude_file='fmap_mag.nii.gz', mask_file='fmap_mask.nii.gz').run() unwrapped = nb.load( res.outputs.unwrapped_phase_file).get_fdata(dtype='float32') * (fmapmax / pi) return unwrapped
def create_workflow(unwarp_direction='y'): workflow = Workflow(name='func_unwarp') inputs = Node( IdentityInterface(fields=[ # 'subject_id', # 'session_id', 'funcs', 'funcmasks', 'fmap_phasediff', 'fmap_magnitude', 'fmap_mask', ]), name='in') outputs = Node(IdentityInterface(fields=[ 'funcs', 'funcmasks', ]), name='out') # --- --- --- --- --- --- --- Convert to radians --- --- --- --- --- --- # fslmaths $FUNCDIR/"$SUB"_B0_phase -div 100 -mul 3.141592653589793116 # -odt float $FUNCDIR/"$SUB"_B0_phase_rescaled # in_file --> out_file phase_radians = Node(fsl.ImageMaths( op_string='-mul 3.141592653589793116 -div 100', out_data_type='float', suffix='_radians', ), name='phaseRadians') workflow.connect(inputs, 'fmap_phasediff', phase_radians, 'in_file') # --- --- --- --- --- --- --- Unwrap Fieldmap --- --- --- --- --- --- # --- Unwrap phase # prelude -p $FUNCDIR/"$SUB"_B0_phase_rescaled # -a $FUNCDIR/"$SUB"_B0_magnitude # -o $FUNCDIR/"$SUB"_fmri_B0_phase_rescaled_unwrapped # -m $FUNCDIR/"$SUB"_B0_magnitude_brain_mask # magnitude_file, phase_file [, mask_file] --> unwrapped_phase_file unwrap = MapNode( PRELUDE(), name='unwrap', iterfield=['mask_file'], ) workflow.connect([ (inputs, unwrap, [('fmap_magnitude', 'magnitude_file')]), (inputs, unwrap, [('fmap_mask', 'mask_file')]), (phase_radians, unwrap, [('out_file', 'phase_file')]), ]) # --- --- --- --- --- --- --- Convert to Radians / Sec --- --- --- --- --- # fslmaths $FUNCDIR/"$SUB"_B0_phase_rescaled_unwrapped # -mul 200 $FUNCDIR/"$SUB"_B0_phase_rescaled_unwrapped rescale = MapNode( fsl.ImageMaths(op_string='-mul 200'), name='rescale', iterfield=['in_file'], ) workflow.connect(unwrap, 'unwrapped_phase_file', rescale, 'in_file') # --- --- --- --- --- --- --- Unmask fieldmap --- --- --- --- --- unmask_phase = MapNode( FUGUE( save_unmasked_fmap=True, unwarp_direction=unwarp_direction, ), name='unmask_phase', iterfield=['mask_file', 'fmap_in_file'], ) workflow.connect(rescale, 'out_file', unmask_phase, 'fmap_in_file') workflow.connect(inputs, 'fmap_mask', unmask_phase, 'mask_file') # --- --- --- --- --- --- --- Undistort functionals --- --- --- --- --- # phasemap_in_file = phasediff # mask_file = mask # in_file = functional image # dwell_time = 0.0005585 s # unwarp_direction undistort = MapNode( FUGUE( dwell_time=0.0005585, # based on Process-NHP-MRI/Process_functional_data.md: asym_se_time=0.020, smooth3d=2.0, median_2dfilter=True, unwarp_direction=unwarp_direction, ), name='undistort', iterfield=['in_file', 'mask_file', 'fmap_in_file'], ) workflow.connect(unmask_phase, 'fmap_out_file', undistort, 'fmap_in_file') workflow.connect(inputs, 'fmap_mask', undistort, 'mask_file') workflow.connect(inputs, 'funcs', undistort, 'in_file') undistort_masks = undistort.clone('undistort_masks') workflow.connect(unmask_phase, 'fmap_out_file', undistort_masks, 'fmap_in_file') workflow.connect(inputs, 'fmap_mask', undistort_masks, 'mask_file') workflow.connect(inputs, 'funcmasks', undistort_masks, 'in_file') workflow.connect(undistort, 'unwarped_file', outputs, 'funcs') workflow.connect(undistort_masks, 'unwarped_file', outputs, 'funcmasks') return workflow
def test_PRELUDE_inputs(): input_map = dict( args=dict(argstr='%s', ), complex_phase_file=dict( argstr='--complex=%s', mandatory=True, xor=['magnitude_file', 'phase_file'], ), end=dict(argstr='--end=%d', ), environ=dict( nohash=True, usedefault=True, ), ignore_exception=dict( nohash=True, usedefault=True, ), label_file=dict( argstr='--labels=%s', hash_files=False, ), labelprocess2d=dict(argstr='--labelslices', ), magnitude_file=dict( argstr='--abs=%s', mandatory=True, xor=['complex_phase_file'], ), mask_file=dict(argstr='--mask=%s', ), num_partitions=dict(argstr='--numphasesplit=%d', ), output_type=dict(), phase_file=dict( argstr='--phase=%s', mandatory=True, xor=['complex_phase_file'], ), process2d=dict( argstr='--slices', xor=['labelprocess2d'], ), process3d=dict( argstr='--force3D', xor=['labelprocess2d', 'process2d'], ), rawphase_file=dict( argstr='--rawphase=%s', hash_files=False, ), removeramps=dict(argstr='--removeramps', ), savemask_file=dict( argstr='--savemask=%s', hash_files=False, ), start=dict(argstr='--start=%d', ), terminal_output=dict(nohash=True, ), threshold=dict(argstr='--thresh=%.10f', ), unwrapped_phase_file=dict( argstr='--unwrap=%s', genfile=True, hash_files=False, ), ) inputs = PRELUDE.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_PRELUDE_inputs(): input_map = dict(args=dict(argstr='%s', ), complex_phase_file=dict(argstr='--complex=%s', mandatory=True, xor=['magnitude_file', 'phase_file'], ), end=dict(argstr='--end=%d', ), environ=dict(nohash=True, usedefault=True, ), ignore_exception=dict(nohash=True, usedefault=True, ), label_file=dict(argstr='--labels=%s', hash_files=False, ), labelprocess2d=dict(argstr='--labelslices', ), magnitude_file=dict(argstr='--abs=%s', mandatory=True, xor=['complex_phase_file'], ), mask_file=dict(argstr='--mask=%s', ), num_partitions=dict(argstr='--numphasesplit=%d', ), output_type=dict(), phase_file=dict(argstr='--phase=%s', mandatory=True, xor=['complex_phase_file'], ), process2d=dict(argstr='--slices', xor=['labelprocess2d'], ), process3d=dict(argstr='--force3D', xor=['labelprocess2d', 'process2d'], ), rawphase_file=dict(argstr='--rawphase=%s', hash_files=False, ), removeramps=dict(argstr='--removeramps', ), savemask_file=dict(argstr='--savemask=%s', hash_files=False, ), start=dict(argstr='--start=%d', ), terminal_output=dict(nohash=True, ), threshold=dict(argstr='--thresh=%.10f', ), unwrapped_phase_file=dict(argstr='--unwrap=%s', genfile=True, hash_files=False, ), ) inputs = PRELUDE.input_spec() for key, metadata in input_map.items(): for metakey, value in metadata.items(): yield assert_equal, getattr(inputs.traits()[key], metakey), value