Example #1
0
def test_qc():

    outputs = Outputs()

    c = Configuration({
        "workingDirectory": "",
        "crashLogDirectory": "",
        "outputDirectory":""
    })

    workflow = pe.Workflow(name='workflow_name')
    workflow.base_dir = c.workingDirectory
    workflow.config['execution'] = {
        'hash_method': 'timestamp',
        'crashdump_dir': os.path.abspath(c.crashLogDirectory)
    }

    strat_initial = Strategy()
    strat_list = [strat_initial]

    output_df_dct = gather_outputs(
        c.outputDirectory,
        [
            "functional_brain_mask",
            "functional_to_anat_linear_xfm",
            "anatomical_brain",
            "anatomical_reorient",
            "mean_functional_in_anat",
            "motion_params",
            "frame_wise_displacement_power",
            "frame_wise_displacement_jenkinson",
        ],
        None,
        get_motion=False,
        get_raw_score=False,
        get_func=True,
        derivatives=[
            "functional_brain_mask",
            "functional_to_anat_linear_xfm",
            "anatomical_brain",
            "anatomical_reorient",
            "mean_functional_in_anat",
            "motion_params",
            "frame_wise_displacement_power",
            "frame_wise_displacement_jenkinson",
        ],
        exts=['nii', 'nii.gz', '1D', 'mat', 'txt']
    )

    for (resource, _), df in output_df_dct.items():
        strat_initial.update_resource_pool({
            resource: file_node(df.Filepath[0])
        })

    qc_montage_id_a, qc_montage_id_s, qc_hist_id, qc_plot_id = \
        create_qc_workflow(workflow, c, strat_list, outputs.qc)
Example #2
0
File: mocks.py Project: gkiar/C-PAC
def configuration_strategy_mock(method='FSL'):

    # mock the config dictionary
    c = Configuration({
        "num_ants_threads": 4,
        "workingDirectory": "/scratch/pipeline_tests",
        "crashLogDirectory": "/scratch",
        "outputDirectory":
        "/output/output/pipeline_analysis_nuisance/sub-M10978008_ses-NFB3",
        "resolution_for_func_preproc": "3mm",
        "resolution_for_func_derivative": "3mm",
        "template_for_resample":
        "/usr/share/fsl/5.0/data/standard/MNI152_T1_1mm_brain.nii.gz",
        "template_brain_only_for_func":
        "/usr/share/fsl/5.0/data/standard/MNI152_T1_${func_resolution}_brain.nii.gz",
        "template_skull_for_func":
        "/usr/share/fsl/5.0/data/standard/MNI152_T1_${func_resolution}.nii.gz",
        "identityMatrix": "/usr/share/fsl/5.0/etc/flirtsch/ident.mat",
        "funcRegFSLinterpolation": "sinc",
        "funcRegANTSinterpolation": "LanczosWindowedSinc"
    })

    if method == 'ANTS':
        c.update('regOption', 'ANTS')
    else:
        c.update('regOption', 'FSL')

    # mock the strategy
    strat = Strategy()

    resource_dict = {
        "mean_functional":
        os.path.join(
            c.outputDirectory,
            "mean_functional/sub-M10978008_ses-NFB3_task-test_bold_calc_tshift_resample_volreg_calc_tstat.nii.gz"
        ),
        "motion_correct":
        os.path.join(
            c.outputDirectory,
            "motion_correct/_scan_test/sub-M10978008_ses-NFB3_task-test_bold_calc_tshift_resample_volreg.nii.gz"
        ),
        "anatomical_brain":
        os.path.join(
            c.outputDirectory,
            "anatomical_brain/sub-M10978008_ses-NFB3_acq-ao_brain_resample.nii.gz"
        ),
        "ants_initial_xfm":
        os.path.join(
            c.outputDirectory,
            "ants_initial_xfm/transform0DerivedInitialMovingTranslation.mat"),
        "ants_affine_xfm":
        os.path.join(c.outputDirectory,
                     "ants_affine_xfm/transform2Affine.mat"),
        "ants_rigid_xfm":
        os.path.join(c.outputDirectory, "ants_rigid_xfm/transform1Rigid.mat"),
        "anatomical_to_mni_linear_xfm":
        os.path.join(
            c.outputDirectory,
            "anatomical_to_mni_linear_xfm/sub-M10978008_ses-NFB3_T1w_resample_calc_flirt.mat"
        ),
        "functional_to_anat_linear_xfm":
        os.path.join(
            c.outputDirectory,
            "functional_to_anat_linear_xfm/_scan_test/sub-M10978008_ses-NFB3_task-test_bold_calc_tshift_resample_volreg_calc_tstat_flirt.mat"
        ),
        'ants_symm_warp_field':
        os.path.join(
            c.outputDirectory,
            "anatomical_to_symmetric_mni_nonlinear_xfm/transform3Warp.nii.gz"),
        'ants_symm_affine_xfm':
        os.path.join(c.outputDirectory,
                     "ants_symmetric_affine_xfm/transform2Affine.mat"),
        'ants_symm_rigid_xfm':
        os.path.join(c.outputDirectory,
                     "ants_symmetric_rigid_xfm/transform1Rigid.mat"),
        'ants_symm_initial_xfm':
        os.path.join(
            c.outputDirectory,
            "ants_symmetric_initial_xfm/transform0DerivedInitialMovingTranslation.mat"
        ),
        "dr_tempreg_maps_files": [
            os.path.join(
                '/scratch', 'resting_preproc_sub-M10978008_ses-NFB3_cpac105',
                'temporal_dual_regression_0/_scan_test/_selector_CSF-2mmE-M_aC-WM-2mmE-DPC5_G-M_M-SDB_P-2/_spatial_map_PNAS_Smith09_rsn10_spatial_map_file_..cpac_templates..PNAS_Smith09_rsn10.nii.gz/split_raw_volumes/temp_reg_map_000{0}.nii.gz'
                .format(n)) for n in range(10)
        ]
    }

    if method == 'ANTS':
        resource_dict["anatomical_to_mni_nonlinear_xfm"] = os.path.join(
            c.outputDirectory,
            "anatomical_to_mni_nonlinear_xfm/transform3Warp.nii.gz")
    else:
        resource_dict["anatomical_to_mni_nonlinear_xfm"] = os.path.join(
            c.outputDirectory,
            "anatomical_to_mni_nonlinear_xfm/sub-M10978008_ses-NFB3_T1w_resample_fieldwarp.nii.gz"
        )

    file_node_num = 0
    for resource, filepath in resource_dict.items():
        strat.update_resource_pool(
            {resource: file_node(filepath, file_node_num)})
        strat.append_name(resource + '_0')
        file_node_num += 1

    templates_for_resampling = [
        (c.resolution_for_func_preproc, c.template_brain_only_for_func,
         'template_brain_for_func_preproc', 'resolution_for_func_preproc'),
        (c.resolution_for_func_preproc, c.template_brain_only_for_func,
         'template_skull_for_func_preproc', 'resolution_for_func_preproc')
    ]

    for resolution, template, template_name, tag in templates_for_resampling:
        resampled_template = pe.Node(Function(
            input_names=['resolution', 'template', 'template_name', 'tag'],
            output_names=['resampled_template'],
            function=resolve_resolution,
            as_module=True),
                                     name='resampled_' + template_name)

        resampled_template.inputs.resolution = resolution
        resampled_template.inputs.template = template
        resampled_template.inputs.template_name = template_name
        resampled_template.inputs.tag = tag

        strat.update_resource_pool(
            {template_name: (resampled_template, 'resampled_template')})
        strat.append_name('resampled_template_0')

    return c, strat
Example #3
0
def func_preproc_longitudinal_wf(subject_id, sub_list, config):
    """
    Parameters
    ----------
    subject_id : string
        the id of the subject
    sub_list : list of dict
        this is a list of sessions for one subject and each session if the same dictionary as the one given to
        prep_workflow
    config : configuration
        a configuration object containing the information of the pipeline config. (Same as for prep_workflow)

    Returns
    -------
    strat_list_ses_list : list of list
        a list of strategies; within each strategy, a list of sessions
    """

    datasink = pe.Node(nio.DataSink(), name='sinker')
    datasink.inputs.base_directory = \
    config.pipeline_setup['working_directory']['path']

    session_id_list = []
    ses_list_strat_list = {}

    workflow_name = 'func_preproc_longitudinal_' + str(subject_id)
    workflow = pe.Workflow(name=workflow_name)
    workflow.base_dir = config.pipeline_setup['working_directory']['path']
    workflow.config['execution'] = {
        'hash_method':
        'timestamp',
        'crashdump_dir':
        os.path.abspath(config.pipeline_setup['crash_directory']['path'])
    }

    for sub_dict in sub_list:
        if 'func' in sub_dict or 'rest' in sub_dict:
            if 'func' in sub_dict:
                func_paths_dict = sub_dict['func']
            else:
                func_paths_dict = sub_dict['rest']

            unique_id = sub_dict['unique_id']
            session_id_list.append(unique_id)

            try:
                creds_path = sub_dict['creds_path']
                if creds_path and 'none' not in creds_path.lower():
                    if os.path.exists(creds_path):
                        input_creds_path = os.path.abspath(creds_path)
                    else:
                        err_msg = 'Credentials path: "%s" for subject "%s" was not ' \
                                  'found. Check this path and try again.' % (
                                      creds_path, subject_id)
                        raise Exception(err_msg)
                else:
                    input_creds_path = None
            except KeyError:
                input_creds_path = None

            strat = Strategy()
            strat_list = [strat]
            node_suffix = '_'.join([subject_id, unique_id])

            # Functional Ingress Workflow
            # add optional flag
            workflow, diff, blip, fmap_rp_list = connect_func_ingress(
                workflow, strat_list, config, sub_dict, subject_id,
                input_creds_path, node_suffix)

            # Functional Initial Prep Workflow
            workflow, strat_list = connect_func_init(workflow, strat_list,
                                                     config, node_suffix)

            # Functional Image Preprocessing Workflow
            workflow, strat_list = connect_func_preproc(
                workflow, strat_list, config, node_suffix)

            # Distortion Correction
            workflow, strat_list = connect_distortion_correction(
                workflow, strat_list, config, diff, blip, fmap_rp_list,
                node_suffix)

            ses_list_strat_list[node_suffix] = strat_list

    # Here we have all the func_preproc set up for every session of the subject

    # TODO create a list of list ses_list_strat_list
    # a list of skullstripping strategies,
    # a list of sessions within each strategy list
    # TODO rename and reorganize dict
    # TODO update strat name
    strat_list_ses_list = {}
    strat_list_ses_list['func_default'] = []

    for sub_ses_id, strat_nodes_list in ses_list_strat_list.items():
        strat_list_ses_list['func_default'].append(strat_nodes_list[0])

    workflow.run()

    return strat_list_ses_list
def anat_preproc_afni(working_path,
                      input_path,
                      test_wf_name='test_anat_preproc_afni'):
    '''
    Test create_anat_preproc() with AFNI
    Parameters
    ----------
    working_path : string
        nipype working directory
    input_path : string
        input file path
    test_wf_name : string
        name of test workflow

    Returns
    -------
    None
    '''

    # create a configuration object
    config = Configuration({
        "num_ants_threads":
        4,
        "workingDirectory":
        os.path.join(working_path, "working"),
        "crashLogDirectory":
        os.path.join(working_path, "crash"),
        "outputDirectory":
        working_path,
        "non_local_means_filtering":
        False,
        "n4_bias_field_correction":
        False,
        "skullstrip_mask_vol":
        False,
        "skullstrip_shrink_factor":
        0.6,
        "skullstrip_var_shrink_fac":
        True,
        "skullstrip_shrink_factor_bot_lim":
        0.4,
        "skullstrip_avoid_vent":
        True,
        "skullstrip_n_iterations":
        250,
        "skullstrip_pushout":
        True,
        "skullstrip_touchup":
        True,
        "skullstrip_fill_hole":
        10,
        "skullstrip_NN_smooth":
        72,
        "skullstrip_smooth_final":
        20,
        "skullstrip_avoid_eyes":
        True,
        "skullstrip_use_edge":
        True,
        "skullstrip_exp_frac":
        0.1,
        "skullstrip_push_to_edge":
        False,
        "skullstrip_use_skull":
        False,
        "skullstrip_perc_int":
        0,
        "skullstrip_max_inter_iter":
        4,
        "skullstrip_fac":
        1,
        "skullstrip_blur_fwhm":
        0,
        "skullstrip_monkey":
        False,
    })

    # mock the strategy
    strat = Strategy()

    resource_dict = {"anatomical": input_path}

    file_node_num = 0
    for resource, filepath in resource_dict.items():
        strat.update_resource_pool(
            {resource: file_node(filepath, file_node_num)})
        strat.append_name(resource + '_0')
        file_node_num += 1

    # build the workflow
    workflow = pe.Workflow(name=test_wf_name)
    workflow.base_dir = config.workingDirectory
    workflow.config['execution'] = {
        'hash_method': 'timestamp',
        'crashdump_dir': os.path.abspath(config.crashLogDirectory)
    }

    # call create_anat_preproc
    anat_preproc = create_anat_preproc(
        method='afni',
        already_skullstripped=False,
        config=config,
        wf_name='anat_preproc',
        sub_dir=None  #TODO
    )

    # connect AFNI options
    anat_preproc.inputs.AFNI_options.set(
        mask_vol=config.skullstrip_mask_vol,
        shrink_factor=config.skullstrip_shrink_factor,
        var_shrink_fac=config.skullstrip_var_shrink_fac,
        shrink_fac_bot_lim=config.skullstrip_shrink_factor_bot_lim,
        avoid_vent=config.skullstrip_avoid_vent,
        niter=config.skullstrip_n_iterations,
        pushout=config.skullstrip_pushout,
        touchup=config.skullstrip_touchup,
        fill_hole=config.skullstrip_fill_hole,
        avoid_eyes=config.skullstrip_avoid_eyes,
        use_edge=config.skullstrip_use_edge,
        exp_frac=config.skullstrip_exp_frac,
        smooth_final=config.skullstrip_smooth_final,
        push_to_edge=config.skullstrip_push_to_edge,
        use_skull=config.skullstrip_use_skull,
        perc_int=config.skullstrip_perc_int,
        max_inter_iter=config.skullstrip_max_inter_iter,
        blur_fwhm=config.skullstrip_blur_fwhm,
        fac=config.skullstrip_fac,
        monkey=config.skullstrip_monkey)

    node, out_file = strat['anatomical']
    workflow.connect(node, out_file, anat_preproc, 'inputspec.anat')

    # run workflow
    workflow.run()
Example #5
0
def func_longitudinal_template_wf(subject_id, strat_list, config):
    '''
    Parameters
    ----------
    subject_id : string
        the id of the subject
    strat_list : list of list
        first level strategy, second level session
    config : configuration
        a configuration object containing the information of the pipeline config.

    Returns
    -------
        None
    '''

    workflow_name = 'func_longitudinal_template_' + str(subject_id)
    workflow = pe.Workflow(name=workflow_name)
    workflow.base_dir = config.pipeline_setup['working_directory']['path']
    workflow.config['execution'] = {
        'hash_method':
        'timestamp',
        'crashdump_dir':
        os.path.abspath(config.pipeline_setup['crash_directory']['path'])
    }

    # strat_nodes_list = strat_list['func_default']
    strat_init = Strategy()

    templates_for_resampling = [
        (config.resolution_for_func_preproc,
         config.template_brain_only_for_func,
         'template_brain_for_func_preproc', 'resolution_for_func_preproc'),
        (config.resolution_for_func_preproc, config.template_skull_for_func,
         'template_skull_for_func_preproc', 'resolution_for_func_preproc'),
        (config.resolution_for_func_preproc, config.ref_mask_for_func,
         'template_ref_mask', 'resolution_for_func_preproc'),
        # TODO check float resolution
        (config.resolution_for_func_preproc,
         config.functional_registration['2-func_registration_to_template']
         ['target_template']['EPI_template']['template_epi'], 'template_epi',
         'resolution_for_func_preproc'),
        (config.resolution_for_func_derivative,
         config.functional_registration['2-func_registration_to_template']
         ['target_template']['EPI_template']['template_epi'],
         'template_epi_derivative', 'resolution_for_func_derivative'),
        (config.resolution_for_func_derivative,
         config.template_brain_only_for_func,
         'template_brain_for_func_derivative', 'resolution_for_func_preproc'),
        (config.resolution_for_func_derivative, config.template_skull_for_func,
         'template_skull_for_func_derivative', 'resolution_for_func_preproc'),
    ]

    for resolution, template, template_name, tag in templates_for_resampling:
        resampled_template = pe.Node(Function(
            input_names=['resolution', 'template', 'template_name', 'tag'],
            output_names=['resampled_template'],
            function=resolve_resolution,
            as_module=True),
                                     name='resampled_' + template_name)

        resampled_template.inputs.resolution = resolution
        resampled_template.inputs.template = template
        resampled_template.inputs.template_name = template_name
        resampled_template.inputs.tag = tag

        strat_init.update_resource_pool(
            {template_name: (resampled_template, 'resampled_template')})

    merge_func_preproc_node = pe.Node(Function(
        input_names=['working_directory'],
        output_names=['brain_list', 'skull_list'],
        function=merge_func_preproc,
        as_module=True),
                                      name='merge_func_preproc')

    merge_func_preproc_node.inputs.working_directory = \
    config.pipeline_setup['working_directory']['path']

    template_node = subject_specific_template(
        workflow_name='subject_specific_func_template_' + subject_id)

    template_node.inputs.set(
        avg_method=config.longitudinal_template_average_method,
        dof=config.longitudinal_template_dof,
        interp=config.longitudinal_template_interp,
        cost=config.longitudinal_template_cost,
        convergence_threshold=config.
        longitudinal_template_convergence_threshold,
        thread_pool=config.longitudinal_template_thread_pool,
    )

    workflow.connect(merge_func_preproc_node, 'brain_list', template_node,
                     'input_brain_list')

    workflow.connect(merge_func_preproc_node, 'skull_list', template_node,
                     'input_skull_list')

    workflow, strat_list = register_func_longitudinal_template_to_standard(
        template_node, config, workflow, strat_init, 'default')

    workflow.run()

    return