Exemplo n.º 1
0
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