def fsl_brain_connector(wf, cfg, strat_pool, pipe_num, opt): inputnode_bet = pe.Node(util.IdentityInterface(fields=[ 'frac', 'mask_boolean', 'mesh_boolean', 'outline', 'padding', 'radius', 'reduce_bias', 'remove_eyes', 'robust', 'skull', 'surfaces', 'threshold', 'vertical_gradient' ]), name=f'BET_options_{pipe_num}') anat_skullstrip = pe.Node(interface=fsl.BET(), name=f'anat_BET_skullstrip_{pipe_num}') anat_skullstrip.inputs.output_type = 'NIFTI_GZ' inputnode_bet.inputs.set( frac=cfg.anatomical_preproc['brain_extraction']['FSL-BET']['frac'], mask_boolean=cfg.anatomical_preproc['brain_extraction']['FSL-BET'] ['mask_boolean'], mesh_boolean=cfg.anatomical_preproc['brain_extraction']['FSL-BET'] ['mesh_boolean'], outline=cfg.anatomical_preproc['brain_extraction']['FSL-BET'] ['outline'], padding=cfg.anatomical_preproc['brain_extraction']['FSL-BET'] ['padding'], radius=cfg.anatomical_preproc['brain_extraction']['FSL-BET']['radius'], reduce_bias=cfg.anatomical_preproc['brain_extraction']['FSL-BET'] ['reduce_bias'], remove_eyes=cfg.anatomical_preproc['brain_extraction']['FSL-BET'] ['remove_eyes'], robust=cfg.anatomical_preproc['brain_extraction']['FSL-BET']['robust'], skull=cfg.anatomical_preproc['brain_extraction']['FSL-BET']['skull'], surfaces=cfg.anatomical_preproc['brain_extraction']['FSL-BET'] ['surfaces'], threshold=cfg.anatomical_preproc['brain_extraction']['FSL-BET'] ['threshold'], vertical_gradient=cfg.anatomical_preproc['brain_extraction']['FSL-BET'] ['vertical_gradient'], ) node, out = strat_pool.get_data( ['desc-preproc_T1w', 'desc-reorient_T1w', 'T1w']) wf.connect(node, out, anat_skullstrip, 'in_file') wf.connect([(inputnode_bet, anat_skullstrip, [ ('frac', 'frac'), ('mask_boolean', 'mask'), ('mesh_boolean', 'mesh'), ('outline', 'outline'), ('padding', 'padding'), ('radius', 'radius'), ('reduce_bias', 'reduce_bias'), ('remove_eyes', 'remove_eyes'), ('robust', 'robust'), ('skull', 'skull'), ('surfaces', 'surfaces'), ('threshold', 'threshold'), ('vertical_gradient', 'vertical_gradient'), ])]) outputs = {'space-T1w_desc-brain_mask': (anat_skullstrip, 'mask_file')} return (wf, outputs)
def create_anat_datasource(wf_name='anat_datasource'): from CPAC.pipeline import nipype_pipeline_engine as pe import nipype.interfaces.utility as util wf = pe.Workflow(name=wf_name) inputnode = pe.Node(util.IdentityInterface( fields=['subject', 'anat', 'creds_path', 'dl_dir', 'img_type'], mandatory_inputs=True), name='inputnode') check_s3_node = pe.Node(function.Function( input_names=['file_path', 'creds_path', 'dl_dir', 'img_type'], output_names=['local_path'], function=check_for_s3, as_module=True), name='check_for_s3') wf.connect(inputnode, 'anat', check_s3_node, 'file_path') wf.connect(inputnode, 'creds_path', check_s3_node, 'creds_path') wf.connect(inputnode, 'dl_dir', check_s3_node, 'dl_dir') wf.connect(inputnode, 'img_type', check_s3_node, 'img_type') outputnode = pe.Node(util.IdentityInterface(fields=['subject', 'anat']), name='outputspec') wf.connect(inputnode, 'subject', outputnode, 'subject') wf.connect(check_s3_node, 'local_path', outputnode, 'anat') # Return the workflow return wf
def anatomical_init(wf, cfg, strat_pool, pipe_num, opt=None): ''' {"name": "anatomical_init", "config": "None", "switch": "None", "option_key": "None", "option_val": "None", "inputs": ["T1w"], "outputs": ["desc-preproc_T1w", "desc-reorient_T1w"]} ''' anat_deoblique = pe.Node(interface=afni.Refit(), name=f'anat_deoblique_{pipe_num}') anat_deoblique.inputs.deoblique = True node, out = strat_pool.get_data('T1w') wf.connect(node, out, anat_deoblique, 'in_file') anat_reorient = pe.Node(interface=afni.Resample(), name=f'anat_reorient_{pipe_num}') anat_reorient.inputs.orientation = 'RPI' anat_reorient.inputs.outputtype = 'NIFTI_GZ' wf.connect(anat_deoblique, 'out_file', anat_reorient, 'in_file') outputs = { 'desc-preproc_T1w': (anat_reorient, 'out_file'), 'desc-reorient_T1w': (anat_reorient, 'out_file') } return (wf, outputs)
def create_connectome(name='connectome'): wf = pe.Workflow(name=name) inputspec = pe.Node( util.IdentityInterface(fields=[ 'time_series', 'method' ]), name='inputspec' ) outputspec = pe.Node( util.IdentityInterface(fields=[ 'connectome', ]), name='outputspec' ) node = pe.Node(Function(input_names=['time_series', 'method'], output_names=['connectome'], function=compute_correlation, as_module=True), name='connectome') wf.connect([ (inputspec, node, [('time_series', 'time_series')]), (inputspec, node, [('method', 'method')]), (node, outputspec, [('connectome', 'connectome')]), ]) return wf
def create_grp_analysis_dataflow(wf_name='gp_dataflow'): from CPAC.pipeline import nipype_pipeline_engine as pe import nipype.interfaces.utility as util from CPAC.utils.datasource import select_model_files wf = pe.Workflow(name=wf_name) inputnode = pe.Node(util.IdentityInterface( fields=['ftest', 'grp_model', 'model_name'], mandatory_inputs=True), name='inputspec') selectmodel = pe.Node(function.Function( input_names=['model', 'ftest', 'model_name'], output_names=['fts_file', 'con_file', 'grp_file', 'mat_file'], function=select_model_files, as_module=True), name='selectnode') wf.connect(inputnode, 'ftest', selectmodel, 'ftest') wf.connect(inputnode, 'grp_model', selectmodel, 'model') wf.connect(inputnode, 'model_name', selectmodel, 'model_name') outputnode = pe.Node(util.IdentityInterface( fields=['fts', 'grp', 'mat', 'con'], mandatory_inputs=True), name='outputspec') wf.connect(selectmodel, 'mat_file', outputnode, 'mat') wf.connect(selectmodel, 'grp_file', outputnode, 'grp') wf.connect(selectmodel, 'fts_file', outputnode, 'fts') wf.connect(selectmodel, 'con_file', outputnode, 'con') return wf
def create_qc_skullstrip(wf_name='qc_skullstrip'): wf = pe.Workflow(name=wf_name) input_node = pe.Node(util.IdentityInterface( fields=['anatomical_brain', 'anatomical_reorient']), name='inputspec') output_node = pe.Node( util.IdentityInterface(fields=['axial_image', 'sagittal_image']), name='outputspec') skull_edge = pe.Node(Function(input_names=['in_file'], output_names=['out_file'], function=afni_Edge3, as_module=True), name='skull_edge') montage_skull = create_montage('montage_skull', 'red', 'skull_vis', mapnode=False) wf.connect(input_node, 'anatomical_reorient', skull_edge, 'in_file') wf.connect(input_node, 'anatomical_brain', montage_skull, 'inputspec.underlay') wf.connect(skull_edge, 'out_file', montage_skull, 'inputspec.overlay') wf.connect(montage_skull, 'outputspec.axial_png', output_node, 'axial_image') wf.connect(montage_skull, 'outputspec.sagittal_png', output_node, 'sagittal_image') return wf
def fisher_z_score_standardize(wf_name, label, input_image_type='func_derivative', opt=None): wf = pe.Workflow(name=wf_name) map_node = False if input_image_type == 'func_derivative_multi': map_node = True inputnode = pe.Node(util.IdentityInterface(fields=['correlation_file', 'timeseries_oned']), name='inputspec') fisher_z_score_std = get_fisher_zscore(label, map_node, 'fisher_z_score_std') wf.connect(inputnode, 'correlation_file', fisher_z_score_std, 'inputspec.correlation_file') wf.connect(inputnode, 'timeseries_oned', fisher_z_score_std, 'inputspec.timeseries_one_d') outputnode = pe.Node(util.IdentityInterface(fields=['out_file']), name='outputspec') wf.connect(fisher_z_score_std, 'outputspec.fisher_z_score_img', outputnode, 'out_file') return wf
def test_nonlinear_register(): from ..registration import create_nonlinear_register from CPAC.pipeline import nipype_pipeline_engine as pe import nipype.interfaces.fsl as fsl ## necessary inputs ## -input_brain ## -input_skull ## -reference_brain ## -reference_skull ## -fnirt_config ## -fnirt_warp_res ## input_brain anat_bet_file = '/home/data/Projects/nuisance_reliability_paper/working_dir_CPAC_order/resting_preproc/anatpreproc/_session_id_NYU_TRT_session1_subject_id_sub05676/anat_skullstrip/mprage_anonymized_RPI_3dT.nii.gz' ## input_skull ## reference_brain mni_file = '/usr/share/fsl/4.1/data/standard/MNI152_T1_3mm_brain.nii.gz' ## reference_skull ## fnirt_config fnirt_config = 'T1_2_MNI152_3mm' ## fnirt_warp_res fnirt_warp_res = None #?? what is this for?: func_file = '/home/data/Projects/nuisance_reliability_paper/working_dir_CPAC_order/resting_preproc/nuisance_preproc/_session_id_NYU_TRT_session1_subject_id_sub05676/_csf_threshold_0.4/_gm_threshold_0.2/_wm_threshold_0.66/_run_scrubbing_False/_nc_5/_selector_6.7/regress_nuisance/mapflow/_regress_nuisance0/residual.nii.gz' mni_workflow = pe.Workflow(name='mni_workflow') linear_reg = pe.Node(interface=fsl.FLIRT(), name='linear_reg_0') linear_reg.inputs.cost = 'corratio' linear_reg.inputs.dof = 6 linear_reg.inputs.interp = 'nearestneighbour' linear_reg.inputs.in_file = func_file linear_reg.inputs.reference = anat_bet_file #T1 to MNI Node c = create_nonlinear_register() c.inputs.inputspec.input = anat_bet_file c.inputs.inputspec.reference = '/usr/share/fsl/4.1/data/standard/MNI152_T1_3mm_brain.nii.gz' c.inputs.inputspec.fnirt_config = 'T1_2_MNI152_3mm' #EPI to MNI warp Node mni_warp = pe.Node(interface=fsl.ApplyWarp(), name='mni_warp') mni_warp.inputs.ref_file = '/usr/share/fsl/4.1/data/standard/MNI152_T1_3mm_brain.nii.gz' mni_warp.inputs.in_file = func_file mni_workflow.connect(c, 'outputspec.nonlinear_xfm', mni_warp, 'field_file') mni_workflow.connect(linear_reg, 'out_matrix_file', mni_warp, 'premat') mni_workflow.base_dir = './' mni_workflow.run()
def get_normalized_moments(wf_name='normalized_moments'): """ Workflow to calculate the normalized moments for skewedness calculations Parameters ---------- wf_name : string name of the workflow Returns ------- wflow : workflow object workflow object Notes ----- `Source <https://github.com/FCP-INDI/C-PAC/blob/master/CPAC/timeseries/timeseries_analysis.py>`_ Workflow Inputs:: inputspec.spatial_timeseries : string (nifti file) spatial map timeseries Workflow Outputs:: outputspec.moments: list list of moment values Example ------- >>> import CPAC.timeseries.timeseries_analysis as t >>> wf = t.get_normalized_moments() >>> wf.inputs.inputspec.spatial_timeseries = '/home/data/outputs/SurfaceRegistration/lh_surface_file.nii.gz' >>> wf.base_dir = './' >>> wf.run() """ wflow = pe.Workflow(name=wf_name) inputNode = pe.Node(util.IdentityInterface(fields=['spatial_timeseries']), name='inputspec') # calculate normalized moments # output of this node is a list, 'moments' norm_moments = pe.Node(util.CalculateNormalizedMoments(moment='3'), name='norm_moments') outputNode = pe.Node(util.IdentityInterface(fields=['moments_outputs']), name='outputspec') wflow.connect(inputNode, 'spatial_timeseries', norm_moments, 'timeseries_file') wflow.connect(norm_moments, 'moments', outputNode, 'moments_outputs') return wflow
def vmhc(wf, cfg, strat_pool, pipe_num, opt=None): '''Compute Voxel-Mirrored Homotopic Connectivity. VMHC is the map of brain functional homotopy, the high degree of synchrony in spontaneous activity between geometrically corresponding interhemispheric (i.e., homotopic) regions. Node Block: {"name": "vmhc", "config": ["voxel_mirrored_homotopic_connectivity"], "switch": ["run"], "option_key": "None", "option_val": "None", "inputs": [["space-symtemplate_desc-cleaned-sm_bold", "space-symtemplate_desc-brain-sm_bold", "space-symtemplate_desc-preproc-sm_bold", "space-symtemplate_desc-sm_bold"]], "outputs": ["vmhc"]} ''' # write out a swapped version of the file # copy and L/R swap file copy_and_L_R_swap = pe.Node(interface=fsl.SwapDimensions(), name=f'copy_and_L_R_swap_{pipe_num}', mem_gb=3.0) copy_and_L_R_swap.inputs.new_dims = ('-x', 'y', 'z') node, out = strat_pool.get_data([ "space-symtemplate_desc-cleaned-sm_bold", "space-symtemplate_desc-brain-sm_bold", "space-symtemplate_desc-preproc-sm_bold", "space-symtemplate_desc-sm_bold" ]) wf.connect(node, out, copy_and_L_R_swap, 'in_file') # calculate correlation between original and swapped images pearson_correlation = pe.Node(interface=preprocess.TCorrelate(), name=f'pearson_correlation_{pipe_num}', mem_gb=3.0) pearson_correlation.inputs.pearson = True pearson_correlation.inputs.polort = -1 pearson_correlation.inputs.outputtype = 'NIFTI_GZ' wf.connect(node, out, pearson_correlation, 'xset') wf.connect(copy_and_L_R_swap, 'out_file', pearson_correlation, 'yset') outputs = {'vmhc': (pearson_correlation, 'out_file')} return (wf, outputs)
def identity_input(name, field, val): pool_input = pe.Node(util.IdentityInterface(fields=[field]), name=name) pool_input.inputs.set({field: val}) return pool_input, field
def timeseries_extraction_Voxel(wf, cfg, strat_pool, pipe_num, opt=None): ''' {"name": "timeseries_extraction_Voxel", "config": ["timeseries_extraction"], "switch": ["run"], "option_key": "None", "option_val": "None", "inputs": [["space-template_desc-cleaned_bold", "space-template_desc-brain_bold", "space-template_desc-motion_bold", "space-template_desc-preproc_bold", "space-template_bold"]], "outputs": ["desc-Voxel_timeseries", "atlas_name"]} ''' resample_functional_to_mask = pe.Node(Function( input_names=['in_func', 'in_roi', 'realignment', 'identity_matrix'], output_names=['out_func', 'out_roi'], function=resample_func_roi, as_module=True), name=f'resample_functional_to_mask_' f'{pipe_num}') resample_functional_to_mask.inputs.realignment = cfg.timeseries_extraction[ 'realignment'] resample_functional_to_mask.inputs.identity_matrix = \ cfg.registration_workflows['functional_registration'][ 'func_registration_to_template']['FNIRT_pipelines']['identity_matrix'] mask_dataflow = create_roi_mask_dataflow( cfg.timeseries_extraction['tse_atlases']['Voxel'], f'mask_dataflow_{pipe_num}') voxel_timeseries = get_voxel_timeseries(f'voxel_timeseries_{pipe_num}') #voxel_timeseries.inputs.inputspec.output_type = cfg.timeseries_extraction[ # 'roi_tse_outputs'] node, out = strat_pool.get_data([ "space-template_desc-cleaned_bold", "space-template_desc-brain_bold", "space-template_desc-motion_bold", "space-template_desc-preproc_bold", "space-template_bold" ]) # resample the input functional file to mask wf.connect(node, out, resample_functional_to_mask, 'in_func') wf.connect(mask_dataflow, 'outputspec.out_file', resample_functional_to_mask, 'in_roi') # connect it to the voxel_timeseries wf.connect(resample_functional_to_mask, 'out_roi', voxel_timeseries, 'input_mask.mask') wf.connect(resample_functional_to_mask, 'out_func', voxel_timeseries, 'inputspec.rest') outputs = { 'desc-Voxel_timeseries': (voxel_timeseries, 'outputspec.mask_outputs'), 'atlas_name': (mask_dataflow, 'outputspec.out_name') } return (wf, outputs)
def create_check_for_s3_node(name, file_path, img_type='other', creds_path=None, dl_dir=None, map_node=False): if map_node: check_s3_node = pe.MapNode(function.Function( input_names=['file_path', 'creds_path', 'dl_dir', 'img_type'], output_names=['local_path'], function=check_for_s3, as_module=True), iterfield=['file_path'], name='check_for_s3_%s' % name) else: check_s3_node = pe.Node(function.Function( input_names=['file_path', 'creds_path', 'dl_dir', 'img_type'], output_names=['local_path'], function=check_for_s3, as_module=True), name='check_for_s3_%s' % name) check_s3_node.inputs.set(file_path=file_path, creds_path=creds_path, dl_dir=dl_dir, img_type=img_type) return check_s3_node
def prep_cwas_workflow(c, subject_infos): print('Preparing CWAS workflow') p_id, s_ids, scan_ids, s_paths = (list(tup) for tup in zip(*subject_infos)) print('Subjects', s_ids) wf = pe.Workflow(name='cwas_workflow') wf.base_dir = c.pipeline_setup['working_directory']['path'] from CPAC.cwas import create_cwas import numpy as np regressor = np.loadtxt(c.cwasRegressorFile) cw = create_cwas() cw.inputs.inputspec.roi = c.cwasROIFile cw.inputs.inputspec.subjects = s_paths cw.inputs.inputspec.regressor = regressor cw.inputs.inputspec.cols = c.cwasRegressorCols cw.inputs.inputspec.f_samples = c.cwasFSamples cw.inputs.inputspec.strata = c.cwasRegressorStrata # will stay None? cw.inputs.inputspec.parallel_nodes = c.cwasParallelNodes ds = pe.Node(nio.DataSink(), name='cwas_sink') out_dir = os.path.dirname(s_paths[0]).replace(s_ids[0], 'cwas_results') ds.inputs.base_directory = out_dir ds.inputs.container = '' wf.connect(cw, 'outputspec.F_map', ds, 'F_map') wf.connect(cw, 'outputspec.p_map', ds, 'p_map') wf.run(plugin='MultiProc', plugin_args={'n_procs': c.numCoresPerSubject})
def test_match_epi_fmaps(): # good data to use s3_prefix = "s3://fcp-indi/data/Projects/HBN/MRI/Site-CBIC/sub-NDARAB708LM5" s3_paths = [ "func/sub-NDARAB708LM5_task-rest_run-1_bold.json", "fmap/sub-NDARAB708LM5_dir-PA_acq-fMRI_epi.nii.gz", "fmap/sub-NDARAB708LM5_dir-PA_acq-fMRI_epi.json", "fmap/sub-NDARAB708LM5_dir-AP_acq-fMRI_epi.nii.gz", "fmap/sub-NDARAB708LM5_dir-AP_acq-fMRI_epi.json" ] wf, ds, local_paths = setup_test_wf(s3_prefix, s3_paths, "test_match_epi_fmaps") opposite_pe_json = local_paths["fmap/sub-NDARAB708LM5_dir-PA_acq-fMRI_epi.json"] same_pe_json = local_paths["fmap/sub-NDARAB708LM5_dir-AP_acq-fMRI_epi.json"] func_json = local_paths["func/sub-NDARAB708LM5_task-rest_run-1_bold.json"] with open(opposite_pe_json, "r") as f: opposite_pe_params = json.load(f) with open(same_pe_json, "r") as f: same_pe_params = json.load(f) with open(func_json, "r") as f: func_params = json.load(f) bold_pedir = func_params["PhaseEncodingDirection"] fmap_paths_dct = {"epi_PA": {"scan": local_paths["fmap/sub-NDARAB708LM5_dir-PA_acq-fMRI_epi.nii.gz"], "scan_parameters": opposite_pe_params}, "epi_AP": {"scan": local_paths["fmap/sub-NDARAB708LM5_dir-AP_acq-fMRI_epi.nii.gz"], "scan_parameters": same_pe_params} } match_fmaps = \ pe.Node(util.Function(input_names=['fmap_dct', 'bold_pedir'], output_names=['opposite_pe_epi', 'same_pe_epi'], function=match_epi_fmaps, as_module=True), name='match_epi_fmaps') match_fmaps.inputs.fmap_dct = fmap_paths_dct match_fmaps.inputs.bold_pedir = bold_pedir ds.inputs.func_json = func_json ds.inputs.opposite_pe_json = opposite_pe_json ds.inputs.same_pe_json = same_pe_json wf.connect(match_fmaps, 'opposite_pe_epi', ds, 'should_be_dir-PA') wf.connect(match_fmaps, 'same_pe_epi', ds, 'should_be_dir-AP') wf.run()
def calc_avg(workflow, output_name, strat, num_strat, map_node=False): """Calculate the average of an output using AFNI 3dmaskave.""" if map_node: calc_average = pe.MapNode(interface=preprocess.Maskave(), name='{0}_mean_{1}'.format(output_name, num_strat), iterfield=['in_file']) mean_to_csv = pe.MapNode(function.Function(input_names=['in_file', 'output_name'], output_names=[ 'output_mean'], function=extract_output_mean, as_module=True), name='{0}_mean_to_txt_{1}'.format(output_name, num_strat), iterfield=['in_file']) else: calc_average = pe.Node(interface=preprocess.Maskave(), name='{0}_mean_{1}'.format(output_name, num_strat)) mean_to_csv = pe.Node(function.Function(input_names=['in_file', 'output_name'], output_names=['output_mean'], function=extract_output_mean, as_module=True), name='{0}_mean_to_txt_{1}'.format(output_name, num_strat)) mean_to_csv.inputs.output_name = output_name node, out_file = strat[output_name] workflow.connect(node, out_file, calc_average, 'in_file') workflow.connect(calc_average, 'out_file', mean_to_csv, 'in_file') strat.append_name(calc_average.name) strat.update_resource_pool({ 'output_means.@{0}_average'.format(output_name): (mean_to_csv, 'output_mean') }) return strat
def subject_specific_template(workflow_name='subject_specific_template', method='flirt'): """ Parameters ---------- workflow_name method Returns ------- """ imports = [ 'import os', 'import warnings', 'import numpy as np', 'from collections import Counter', 'from multiprocessing.dummy import Pool as ThreadPool', 'from nipype.interfaces.fsl import ConvertXFM', 'from CPAC.longitudinal_pipeline.longitudinal_preproc import (' ' create_temporary_template,' ' register_img_list,' ' template_convergence' ')' ] if method == 'flirt': template_gen_node = pe.Node( util.Function( input_names=[ 'input_brain_list', 'input_skull_list', 'init_reg', 'avg_method', 'dof', 'interp', 'cost', 'mat_type', 'convergence_threshold', 'thread_pool', 'unique_id_list'], output_names=['brain_template', 'skull_template', 'output_brain_list', 'output_skull_list', 'warp_list'], imports=imports, function=template_creation_flirt ), name=workflow_name ) else: raise ValueError(str(method) + 'this method has not yet been implemented') return template_gen_node
def freesurfer_brain_connector(wf, cfg, strat_pool, pipe_num, opt): # register FS brain mask to native space fs_brain_mask_to_native = pe.Node(interface=freesurfer.ApplyVolTransform(), name='fs_brain_mask_to_native') fs_brain_mask_to_native.inputs.reg_header = True node, out = strat_pool.get_data('space-T1w_desc-brain_mask') wf.connect(node, out, fs_brain_mask_to_native, 'source_file') node, out = strat_pool.get_data('raw_average') wf.connect(node, out, fs_brain_mask_to_native, 'target_file') node, out = strat_pool.get_data('freesurfer_subject_dir') wf.connect(node, out, fs_brain_mask_to_native, 'subjects_dir') # convert brain mask file from .mgz to .nii.gz fs_brain_mask_to_nifti = pe.Node(util.Function(input_names=['in_file'], output_names=['out_file'], function=mri_convert), name='fs_brainmask_to_nifti') wf.connect(fs_brain_mask_to_native, 'transformed_file', fs_brain_mask_to_nifti, 'in_file') # binarize the brain mask binarize_fs_brain_mask = pe.Node(interface=fsl.maths.MathsCommand(), name='binarize_fs_brainmask') binarize_fs_brain_mask.inputs.args = '-bin' wf.connect(fs_brain_mask_to_nifti, 'out_file', binarize_fs_brain_mask, 'in_file') # fill holes fill_fs_brain_mask = pe.Node(interface=afni.MaskTool(), name='fill_fs_brainmask') fill_fs_brain_mask.inputs.fill_holes = True fill_fs_brain_mask.inputs.outputtype = 'NIFTI_GZ' wf.connect(binarize_fs_brain_mask, 'out_file', fill_fs_brain_mask, 'in_file') outputs = {'space-T1w_desc-brain_mask': (fill_fs_brain_mask, 'out_file')} return (wf, outputs)
def run_warp_nipype(inputs,output_dir=None,run=True): import EPI_DistCorr warp_workflow = pe.Workflow(name = 'preproc') if output_dir == None: output_dir = '/home/nrajamani' workflow_dir = os.path.join(output_dir,"workflow_output_with_aroma_with_change") warp_workflow.base_dir = workflow_dir # taken from QAP files #resource_pool = {} num_of_cores = 1 #resource_pool({'epireg': (warp_nipype2.warp_nipype, 'outputspec.epireg')}) t_node = EPI_DistCorr.create_EPI_DistCorr()#### t_node.inputs.inputspec.anat_file= '/Users/nanditharajamani/Downloads/ExBox19/T1.nii.gz' t_node.inputs.inputspec.func_file= '/Users/nanditharajamani/Downloads/ExBox19/func.nii.gz' t_node.inputs.inputspec.fmap_pha= '/Users/nanditharajamani/Downloads/ExBox19/fmap_phase.nii.gz' t_node.inputs.inputspec.fmap_mag= '/Users/nanditharajamani/Downloads/ExBox19/fmap_mag.nii.gz' t_node.inputs.inputspec.bbr_schedule='/usr/local/fsl/etc/flirtsch/bbr.sch' t_node.inputs.inputspec.deltaTE = 2.46 t_node.inputs.inputspec.dwellT = 0.0005 t_node.inputs.inputspec.dwell_asym_ratio = 0.93902439 t_node.inputs.inputspec.bet_frac = 0.5 #'home/nrajamani/FieldMap_SubjectExampleData/SubjectData/epi_run2/fMT0160-0015-00003-000003-01_BRAIN.nii.gz', # for image in inputs: # if not(image.endswith('.nii') or image.endswith('.nii.gz')): # raise 'The input image is not the right format' # try: # for image in inputs: # size = image.get_shape() # assert len(size) == 3 # except: # if len(size) < 3: # raise 'input image is not 3D' # intensity = ImageStats(in_file = t_node.inputs.inputspec.fmap_pha, op_string = '-p 90') # if intensity < 3686: # raise 'input phase image does not have the correct range values' dataSink = pe.Node(nio.DataSink(), name='dataSink_file') dataSink.inputs.base_directory = workflow_dir #node, out_file = resource_pool["epireg"] #warp_workflow.connect(t_node,'outputspec.roi_file',dataSink,'roi_file') warp_workflow.connect(t_node,'outputspec.fieldmap',dataSink,'fieldmap_file') warp_workflow.connect(t_node,'outputspec.fmapmagbrain',dataSink,'fmapmagbrain') warp_workflow.connect(t_node,'outputspec.fieldmapmask',dataSink,'fieldmapmask') warp_workflow.connect(t_node,'outputspec.fmap_despiked',dataSink,'fmap_despiked') warp_workflow.connect(t_node,'outputspec.struct',dataSink,'epi2struct') warp_workflow.connect(t_node,'outputspec.anat_func',dataSink,'anat_func') if run == True: warp_workflow.run(plugin='MultiProc', plugin_args ={'n_procs': num_of_cores}) #outpath = glob.glob(os.path.join(workflow_dir, "EPI_DistCorr","*"))[0] #return outpath else: return warp_workflow, warp_workflow.base_dir
def setup_test_wf(s3_prefix, paths_list, test_name, workdirs_to_keep=None): """Set up a basic template Nipype workflow for testing single nodes or small sub-workflows. """ import os import shutil from CPAC.pipeline import nipype_pipeline_engine as pe from CPAC.utils.datasource import check_for_s3 from CPAC.utils.interfaces.datasink import DataSink test_dir = os.path.join(os.getcwd(), test_name) work_dir = os.path.join(test_dir, "workdir") out_dir = os.path.join(test_dir, "output") if os.path.exists(out_dir): try: shutil.rmtree(out_dir) except: pass if os.path.exists(work_dir): for dirname in os.listdir(work_dir): if workdirs_to_keep: for keepdir in workdirs_to_keep: print("{0} --- {1}\n".format(dirname, keepdir)) if keepdir in dirname: continue try: shutil.rmtree(os.path.join(work_dir, dirname)) except: pass local_paths = {} for subpath in paths_list: s3_path = os.path.join(s3_prefix, subpath) local_path = check_for_s3(s3_path, dl_dir=test_dir) local_paths[subpath] = local_path wf = pe.Workflow(name=test_name) wf.base_dir = os.path.join(work_dir) wf.config['execution'] = { 'hash_method': 'timestamp', 'crashdump_dir': os.path.abspath(test_dir) } ds = pe.Node(DataSink(), name='sinker_{0}'.format(test_name)) ds.inputs.base_directory = out_dir ds.inputs.parameterization = True return (wf, ds, local_paths)
def resolve_resolution(resolution, template, template_name, tag=None): import nipype.interfaces.afni as afni from CPAC.pipeline import nipype_pipeline_engine as pe from CPAC.utils.datasource import check_for_s3 tagname = None local_path = None if "{" in template and tag is not None: tagname = "${" + tag + "}" try: if tagname is not None: local_path = check_for_s3( template.replace(tagname, str(resolution))) except (IOError, OSError): local_path = None ## TODO debug - it works in ipython but doesn't work in nipype wf # try: # local_path = check_for_s3('/usr/local/fsl/data/standard/MNI152_T1_3.438mmx3.438mmx3.4mm_brain_mask_dil.nii.gz') # except (IOError, OSError): # local_path = None if local_path is None: if tagname is not None: ref_template = template.replace(tagname, '1mm') local_path = check_for_s3(ref_template) elif tagname is None and "s3" in template: local_path = check_for_s3(template) else: local_path = template if "x" in str(resolution): resolution = tuple( float(i.replace('mm', '')) for i in resolution.split("x")) else: resolution = (float(resolution.replace('mm', '')), ) * 3 resample = pe.Node(interface=afni.Resample(), name=template_name) resample.inputs.voxel_size = resolution resample.inputs.outputtype = 'NIFTI_GZ' resample.inputs.resample_mode = 'Cu' resample.inputs.in_file = local_path resample.base_dir = '.' resampled_template = resample.run() local_path = resampled_template.outputs.out_file return local_path
def z_score_standardize(wf_name, input_image_type='func_derivative', opt=None): wf = pe.Workflow(name=wf_name) map_node = False if input_image_type == 'func_derivative_multi': map_node = True inputnode = pe.Node(util.IdentityInterface(fields=['in_file', 'mask']), name='inputspec') z_score_std = get_zscore(map_node, 'z_score_std') wf.connect(inputnode, 'in_file', z_score_std, 'inputspec.input_file') wf.connect(inputnode, 'mask', z_score_std, 'inputspec.mask_file') outputnode = pe.Node(util.IdentityInterface(fields=['out_file']), name='outputspec') wf.connect(z_score_std, 'outputspec.z_score_img', outputnode, 'out_file') return wf
def create_qc_fd(wf_name='qc_fd'): wf = pe.Workflow(name=wf_name) input_node = pe.Node( util.IdentityInterface(fields=['fd', 'excluded_volumes']), name='inputspec') output_node = pe.Node(util.IdentityInterface(fields=['fd_histogram_plot']), name='outputspec') fd_plot = pe.Node(Function(input_names=['arr', 'measure', 'ex_vol'], output_names=['hist_path'], function=gen_plot_png, as_module=True), name='fd_plot') fd_plot.inputs.measure = 'FD' wf.connect(input_node, 'fd', fd_plot, 'arr') wf.connect(input_node, 'excluded_volumes', fd_plot, 'ex_vol') wf.connect(fd_plot, 'hist_path', output_node, 'fd_histogram_plot') return wf
def create_qc_motion(wf_name='qc_motion'): wf = pe.Workflow(name=wf_name) input_node = pe.Node(util.IdentityInterface(fields=['motion_parameters']), name='inputspec') output_node = pe.Node(util.IdentityInterface( fields=['motion_translation_plot', 'motion_rotation_plot']), name='outputspec') mov_plot = pe.Node(Function( input_names=['motion_parameters'], output_names=['translation_plot', 'rotation_plot'], function=gen_motion_plt, as_module=True), name='motion_plot') wf.connect(input_node, 'motion_parameters', mov_plot, 'motion_parameters') wf.connect(mov_plot, 'translation_plot', output_node, 'motion_translation_plot') wf.connect(mov_plot, 'rotation_plot', output_node, 'motion_rotation_plot') return wf
def run_randomize(inputs, output_dir=None, run=True): from . import pipeline randomise_workflow = pe.Workflow(name='preproc') if output_dir == None: output_dir = '' workflow_dir = os.path.join(output_dir, "randomise_results") randomise_workflow.base_dir = workflow_dir #resource_pool = {} num_of_cores = 1 t_node = pipeline.create_randomise() t_node.inputs.inputspec.subjects = '' t_node.inputs.inputspec.design_matrix_file = '' t_node.inputs.inputspec.constrast_file = '' #t_node.inputs.inputspec.f_constrast_file= '' t_node.inputs.inputspec.permutations = 5000 #t_node.inputs.inputspec.mask = ''# dataSink = pe.Node(nio.DataSink(), name='dataSink_file') dataSink.inputs.base_directory = workflow_dir randomise_workflow.connect(t_node, 'outputspec.index_file', dataSink, 'index_file') #randomise_workflow.connect(t_node,'outputspec.thresh_out',dataSink,'threshold_file') randomise_workflow.connect(t_node, 'outputspec.localmax_txt_file', dataSink, 'localmax_txt_file') randomise_workflow.connect(t_node, 'outputspec.localmax_vol_file', dataSink, 'localmax_vol_file') randomise_workflow.connect(t_node, 'outputspec.max_file', dataSink, 'max_file') randomise_workflow.connect(t_node, 'outputspec.mean_file', dataSink, 'mean_file') randomise_workflow.connect(t_node, 'outputspec.pval_file', dataSink, 'pval_file') randomise_workflow.connect(t_node, 'outputspec.size_file', dataSink, 'size_file') randomise_workflow.connect(t_node, 'outputspec.tstat_files', dataSink, 'tstat_files') randomise_workflow.connect(t_node, 'outputspec.t_corrected_p_files', dataSink, 't_corrected_p_files') if run == True: randomise_workflow.run(plugin='MultiProc', plugin_args={'n_procs': num_of_cores}) else: return randomise_workflow, randomise_workflow.base_dir
def test_function_str(): f = pe.Node(Function(input_names=['scan', 'rest_dict', 'resource'], output_names=['file_path'], function=get_rest), name='get_rest') f.inputs.set( resource=resource, rest_dict=rest_dict, scan=scan ) results = f.run() assert rest_dict['rest_acq-1_run-1']['scan'] == results.outputs.file_path
def brain_extraction(wf, cfg, strat_pool, pipe_num, opt=None): ''' {"name": "brain_extraction", "config": "None", "switch": "None", "option_key": "None", "option_val": "None", "inputs": [(["desc-preproc_T1w", "desc-reorient_T1w", "T1w"], ["space-T1w_desc-brain_mask", "space-T1w_desc-acpcbrain_mask"])], "outputs": ["desc-brain_T1w"]} ''' ''' brain_mask_deoblique = pe.Node(interface=afni.Refit(), name='brain_mask_deoblique') brain_mask_deoblique.inputs.deoblique = True wf.connect(inputnode, 'brain_mask', brain_mask_deoblique, 'in_file') brain_mask_reorient = pe.Node(interface=afni.Resample(), name='brain_mask_reorient') brain_mask_reorient.inputs.orientation = 'RPI' brain_mask_reorient.inputs.outputtype = 'NIFTI_GZ' wf.connect(brain_mask_deoblique, 'out_file', brain_mask_reorient, 'in_file') ''' anat_skullstrip_orig_vol = pe.Node(interface=afni.Calc(), name=f'brain_extraction_{pipe_num}') anat_skullstrip_orig_vol.inputs.expr = 'a*step(b)' anat_skullstrip_orig_vol.inputs.outputtype = 'NIFTI_GZ' node, out = strat_pool.get_data( ['desc-preproc_T1w', 'desc-reorient_T1w', 'T1w']) wf.connect(node, out, anat_skullstrip_orig_vol, 'in_file_a') node, out = strat_pool.get_data( ['space-T1w_desc-brain_mask', 'space-T1w_desc-acpcbrain_mask']) wf.connect(node, out, anat_skullstrip_orig_vol, 'in_file_b') outputs = {'desc-brain_T1w': (anat_skullstrip_orig_vol, 'out_file')} return (wf, outputs)
def prep_randomise_workflow(c, subject_infos): print('Preparing Randomise workflow') p_id, s_ids, scan_ids, s_paths = (list(tup) for tup in zip(*subject_infos)) print('Subjects', s_ids) wf = pe.Workflow(name='randomise_workflow') wf.base_dir = c.pipeline_setup['working_directory']['path'] from CPAC.randomise import create_randomise rw = create_randomise() rw.inputs.inputspec.permutations = c.randopermutations rw.inputs.inputspec.subjects = s_paths #rw.inputs.inputspec.pipeline_ouput_folder = c.os.path.join(c.outputDirectory, # 'pipeline_{0}'.format(c.pipelineName)) rw.inputs.inputspec.mask_boolean = c.mask_boolean #TODO pipe from output dir, not the user input rw.inputs.inputspec.tfce = c.tfce # will stay None? rw.inputs.inputspec.demean = c.demean rw.inputs.inputspec.c_thresh = c.c_thresh ds = pe.Node(nio.DataSink(), name='randomise_sink') out_dir = os.path.dirname(s_paths[0]).replace(s_ids[0], 'randomise_results') ds.inputs.base_directory = out_dir ds.inputs.container = '' #'tstat_files' ,'t_corrected_p_files','index_file','threshold_file','localmax_txt_file','localmax_vol_file','max_file','mean_file','pval_file','size_file' wf.connect(rw, 'outputspec.tstat_files', ds, 'tstat_files') wf.connect(rw, 'outputspec.t_corrected_p_files', ds, 't_corrected_p_files') wf.connect(rw, 'outputspec.index_file', ds, 'index_file') wf.connect(rw, 'outputspec.threshold_file', ds, 'threshold_file') wf.connect(rw, 'outputspec.localmax_vol_file', ds, 'localmax_vol_file') wf.connect(rw, 'outputspec.localmax_txt_file', ds, 'localmax_txt_file') wf.connect(rw, 'outputspec.max_file', ds, 'max_file') wf.connect(rw, 'outputspec.mean_file', ds, 'mean_file') wf.connect(rw, 'outputspec.max_file', ds, 'max_file') wf.connect(rw, 'outputspec.pval_file', ds, 'pval_file') wf.connect(rw, 'outputspec.size_file', ds, 'size_file') wf.run(plugin='MultiProc', plugin_args={'n_procs': c.numCoresPerSubject}) return wf
def qc_T1w_standard(wf, cfg, strat_pool, pipe_num, opt=None): ''' {"name": "qc_brain_extraction", "config": ["pipeline_setup", "output_directory"], "switch": ["generate_quality_control_images"], "option_key": "None", "option_val": "None", "inputs": ["space-template_desc-brain_T1w", "T1w_brain_template"], "outputs": ["space-template_desc-brain_T1w-axial-qc", "space-template_desc-brain_T1w-sagittal-qc"]} ''' # make QC montages for mni normalized anatomical image montage_mni_anat = create_montage(f'montage_mni_anat_{pipe_num}', 'red', 'mni_anat', mapnode=False) node, out = strat_pool.get_data('space-template_desc-brain_T1w') wf.connect(node, out, montage_mni_anat, 'inputspec.underlay') anat_template_edge = pe.Node(Function(input_names=['in_file'], output_names=['out_file'], function=afni_Edge3, as_module=True), name=f'anat_template_edge_{pipe_num}') node, out = strat_pool.get_data('T1w_brain_template') wf.connect(node, out, anat_template_edge, 'in_file') wf.connect(anat_template_edge, 'out_file', montage_mni_anat, 'inputspec.overlay') outputs = { 'space-template_desc-brain_T1w-axial-qc': (montage_mni_anat, 'outputspec.axial_png'), 'space-template_desc-brain_T1w-sagittal-qc': (montage_mni_anat, 'outputspec.sagittal_png') } return (wf, outputs)
def qc_bold_registration(wf, cfg, strat_pool, pipe_num, opt=None): ''' {"name": "qc_bold_registration", "config": ["pipeline_setup", "output_directory"], "switch": ["generate_quality_control_images"], "option_key": "None", "option_val": "None", "inputs": ["space-template_desc-mean_bold", "T1w_brain_template_funcreg"], "outputs": ["space-template_desc-mean_bold-axial-qc", "space-template_desc-mean_bold-sagittal-qc"]} ''' # make QC montage for Mean Functional in MNI with MNI edge montage_mfi = create_montage(f'montage_mfi_{pipe_num}', 'red', 'MNI_edge_on_mean_func_mni', mapnode=False) node, out = strat_pool.get_data('space-template_desc-mean_bold') wf.connect(node, out, montage_mfi, 'inputspec.underlay') func_template_edge = pe.Node(Function(input_names=['in_file'], output_names=['out_file'], function=afni_Edge3, as_module=True), name=f'func_template_edge_{pipe_num}') node, out = strat_pool.get_data("T1w_brain_template_funcreg") wf.connect(node, out, func_template_edge, 'in_file') wf.connect(func_template_edge, 'out_file', montage_mfi, 'inputspec.overlay') outputs = { 'space-template_desc-mean_bold-axial-qc': (montage_mfi, 'outputspec.axial_png'), 'space-template_desc-mean_bold-sagittal-qc': (montage_mfi, 'outputspec.sagittal_png') } return (wf, outputs)