Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
applyxfm = MapNode(freesurfer.ApplyVolTransform(inverse=True,
interp='nearest'),
iterfield=['target_file'],
name='inverse_transform')
register.connect(inputnode, 'subjects_dir', applyxfm, 'subjects_dir')
register.connect(bbregister, 'out_reg_file', applyxfm, 'reg_file')
register.connect(binarize, 'out_file', applyxfm, 'target_file')
register.connect(inputnode, 'mean_image', applyxfm, 'source_file')
"""
Apply inverse transform to aparc file
"""
aparcxfm = Node(freesurfer.ApplyVolTransform(inverse=True,
interp='nearest'),
name='aparc_inverse_transform')
register.connect(inputnode, 'subjects_dir', aparcxfm, 'subjects_dir')
register.connect(bbregister, 'out_reg_file', aparcxfm, 'reg_file')
register.connect(fssource, ('aparc_aseg', get_aparc_aseg),
aparcxfm, 'target_file')
register.connect(inputnode, 'mean_image', aparcxfm, 'source_file')
"""
Convert the BBRegister transformation to ANTS ITK format
"""
convert2itk = Node(C3dAffineTool(), name='convert2itk')
convert2itk.inputs.fsl2ras = True
convert2itk.inputs.itk_transform = True
register.connect(bbregister, 'out_fsl_file', convert2itk, 'transform_file')
subjects_dir=None,
sink_directory=os.getcwd(),
target_subject=['fsaverage3', 'fsaverage4'],
name='resting'):
wf = Workflow(name=name)
# Rename files in case they are named identically
name_unique = MapNode(Rename(format_string='rest_%(run)02d'),
iterfield=['in_file', 'run'],
name='rename')
name_unique.inputs.keep_ext = True
name_unique.inputs.run = range(1, len(files) + 1)
name_unique.inputs.in_file = files
realign = Node(interface=spm.Realign(), name="realign")
realign.inputs.jobtype = 'estwrite'
num_slices = len(slice_times)
slice_timing = Node(interface=spm.SliceTiming(), name="slice_timing")
slice_timing.inputs.num_slices = num_slices
slice_timing.inputs.time_repetition = TR
slice_timing.inputs.time_acquisition = TR - TR/float(num_slices)
slice_timing.inputs.slice_order = (np.argsort(slice_times) + 1).tolist()
slice_timing.inputs.ref_slice = int(num_slices/2)
# Comute TSNR on realigned data regressing polynomials upto order 2
tsnr = MapNode(TSNR(regress_poly=2), iterfield=['in_file'], name='tsnr')
wf.connect(slice_timing, 'timecorrected_files', tsnr, 'in_file')
# Compute the median image across runs
calc_median = Node(Function(input_names=['in_files'],
Example
-------
See code below
"""
register = Workflow(name=name)
inputnode = Node(
interface=IdentityInterface(fields=[
'source_files', 'mean_image', 'subject_id', 'subjects_dir',
'target_image'
]),
name='inputspec')
outputnode = Node(
interface=IdentityInterface(fields=[
'func2anat_transform', 'out_reg_file', 'anat2target_transform',
'transforms', 'transformed_mean', 'segmentation_files',
'anat2target', 'aparc', 'min_cost_file'
]),
name='outputspec')
# Get the subject's freesurfer source directory
fssource = Node(FreeSurferSource(), name='fssource')
fssource.run_without_submitting = True
register.connect(inputnode, 'subject_id', fssource, 'subject_id')
register.connect(inputnode, 'subjects_dir', fssource, 'subjects_dir')
convert = Node(freesurfer.MRIConvert(out_type='nii'), name="convert")
register.connect(fssource, 'T1', convert, 'in_file')
segment.inputs.wm_output_type = [False, False, True]
segment.inputs.csf_output_type = [False, False, True]
segment.inputs.gm_output_type = [False, False, True]
def merge_files(in1, in2):
out_files = filename_to_list(in1)
out_files.extend(filename_to_list(in2))
return out_files
merge = Node(Merge(3), name='merge')
wf.connect(segment, 'native_wm_image', merge, 'in1')
wf.connect(segment, 'native_csf_image', merge, 'in2')
wf.connect(segment, 'native_gm_image', merge, 'in3')
normalize_segs = Node(interface=spm.Normalize(), name = "normalize_segs")
normalize_segs.inputs.jobtype = "write"
normalize_segs.inputs.write_voxel_sizes = [2., 2., 2.]
wf.connect(merge, 'out', normalize_segs, 'apply_to_files')
wf.connect(segment, 'transformation_mat', normalize_segs, 'parameter_file')
# binarize and erode
bin_and_erode = MapNode(fsl.ImageMaths(),
iterfield=['in_file'],
name='bin_and_erode')
bin_and_erode.inputs.op_string = '-thr 0.99 -bin -ero'
wf.connect(normalize_segs, 'normalized_files',
bin_and_erode, 'in_file')
# filter some noise
name='bandpass_unsmooth')
bandpass.inputs.fs = 1./TR
bandpass.inputs.highpass_freq = highpass_freq
bandpass.inputs.lowpass_freq = lowpass_freq
wf.connect(filter2, 'out_res', bandpass, 'files')
"""Smooth the functional data using
:class:`nipype.interfaces.spm.Smooth`.
"""
smooth = Node(interface=spm.Smooth(), name="smooth")
smooth.inputs.fwhm = vol_fwhm
wf.connect(bandpass, 'out_files', smooth, 'in_files')
collector = Node(Merge(2), name='collect_streams')
wf.connect(smooth, 'smoothed_files', collector, 'in1')
wf.connect(bandpass, 'out_files', collector, 'in2')
"""
Transform the remaining images. First to anatomical and then to target
"""
warpall = MapNode(ants.ApplyTransforms(), iterfield=['input_image'],
name='warpall')
warpall.inputs.input_image_type = 3
warpall.inputs.interpolation = 'BSpline'
warpall.inputs.invert_transform_flags = [False, False]
warpall.inputs.terminal_output = 'file'
warpall.inputs.reference_image = target_file
warpall.inputs.args = '--float'
warpall.inputs.num_threads = 1
name='bandpass_unsmooth')
bandpass.inputs.fs = 1./TR
bandpass.inputs.highpass_freq = highpass_freq
bandpass.inputs.lowpass_freq = lowpass_freq
wf.connect(filter2, 'out_res', bandpass, 'files')
"""Smooth the functional data using
:class:`nipype.interfaces.fsl.IsotropicSmooth`.
"""
smooth = MapNode(interface=fsl.IsotropicSmooth(), name="smooth", iterfield=["in_file"])
smooth.inputs.fwhm = vol_fwhm
wf.connect(bandpass, 'out_files', smooth, 'in_file')
collector = Node(Merge(2), name='collect_streams')
wf.connect(smooth, 'out_file', collector, 'in1')
wf.connect(bandpass, 'out_files', collector, 'in2')
"""
Transform the remaining images. First to anatomical and then to target
"""
warpall = MapNode(ants.ApplyTransforms(), iterfield=['input_image'],
name='warpall')
warpall.inputs.input_image_type = 3
warpall.inputs.interpolation = 'Linear'
warpall.inputs.invert_transform_flags = [False, False]
warpall.inputs.terminal_output = 'file'
warpall.inputs.reference_image = target_file
warpall.inputs.args = '--float'
warpall.inputs.num_threads = 2
bbregister = Node(freesurfer.BBRegister(),
name='bbregister')
bbregister.inputs.init = 'fsl'
bbregister.inputs.contrast_type = 't2'
bbregister.inputs.out_fsl_file = True
bbregister.inputs.epi_mask = True
register.connect(inputnode, 'subject_id', bbregister, 'subject_id')
register.connect(inputnode, 'mean_image', bbregister, 'source_file')
register.connect(inputnode, 'subjects_dir', bbregister, 'subjects_dir')
"""
Estimate the tissue classes from the anatomical image. But use spm's segment
as FSL appears to be breaking.
"""
stripper = Node(fsl.BET(), name='stripper')
register.connect(convert, 'out_file', stripper, 'in_file')
fast = Node(fsl.FAST(), name='fast')
register.connect(stripper, 'out_file', fast, 'in_files')
"""
Binarize the segmentation
"""
binarize = MapNode(fsl.ImageMaths(op_string='-nan -thr 0.9 -ero -bin'),
iterfield=['in_file'],
name='binarize')
register.connect(fast, 'partial_volume_files', binarize, 'in_file')
"""
Apply inverse transform to take segmentations to functional space
"""
# Sample the zstat image to the surface
hemisource = Node(IdentityInterface(["mni_hemi"]), "hemisource")
hemisource.iterables = ("mni_hemi", ["lh", "rh"])
zstatproj = Node(freesurfer.SampleToSurface(
sampling_method=exp_info["sampling_method"],
sampling_range=exp_info["sampling_range"],
sampling_units=exp_info["sampling_units"],
smooth_surf=exp_info["surf_smooth"],
subject_id="fsaverage",
mni152reg=True,
target_subject="fsaverage"),
"zstatproj")
# Sample the mask to the surface
maskproj = Node(freesurfer.SampleToSurface(
sampling_range=exp_info["sampling_range"],
sampling_units=exp_info["sampling_units"],
subject_id="fsaverage",
mni152reg=True,
target_subject="fsaverage"),
"maskproj")
if exp_info["sampling_method"] == "point":
maskproj.inputs.sampling_method = "point"
else:
maskproj.inputs.sampling_method = "max"
outputnode = Node(IdentityInterface(["surf_zstat",
"surf_mask"]), "outputs")
# Define and connect the workflow
proj = Workflow(name)
realign = Node(nipy.SpaceTimeRealigner(), name='realign')
realign.inputs.tr = TR
realign.inputs.slice_times = slice_times
realign.inputs.slice_info = 2
if despike:
wf.connect(despiker, 'out_file', realign, 'in_file')
else:
wf.connect(remove_vol, 'roi_file', realign, 'in_file')
# Comute TSNR on realigned data regressing polynomials upto order 2
tsnr = MapNode(TSNR(regress_poly=2), iterfield=['in_file'], name='tsnr')
wf.connect(realign, 'out_file', tsnr, 'in_file')
# Compute the median image across runs
calc_median = Node(Function(input_names=['in_files'],
output_names=['median_file'],
function=median,
imports=imports),
name='median')
wf.connect(tsnr, 'detrended_file', calc_median, 'in_files')
# Coregister the median to the surface
register = Node(freesurfer.BBRegister(),
name='bbregister')
register.inputs.subject_id = subject_id
register.inputs.init = 'fsl'
register.inputs.contrast_type = 't2'
register.inputs.out_fsl_file = True
register.inputs.epi_mask = True
# Compute fieldmaps and unwarp using them