def test_extract_mean_ts():
    """test ExtractMeanTS"""
    _make_tmp_dir()

    extract_mean_ts = ExtractMeanTS()
    extract_mean_ts.inputs.file_4D = img_file
    extract_mean_ts.inputs.mask_file = wm_mask_file
    extract_mean_ts.inputs.suffix = "wm"

    val = extract_mean_ts.run().outputs
    print(val)
    assert os.path.exists(val.mean_masked_ts_file)
    os.remove(val.mean_masked_ts_file)
def test_regress_covar():
    """ test regress_covar"""
    _make_tmp_dir()

    extract_mean_wm_ts = ExtractMeanTS()
    extract_mean_wm_ts.inputs.file_4D = img_file
    extract_mean_wm_ts.inputs.mask_file = wm_mask_file
    extract_mean_wm_ts.inputs.suffix = "wm"
    mean_wm_ts_file = extract_mean_wm_ts.run().outputs.mean_masked_ts_file

    extract_mean_csf_ts = ExtractMeanTS()
    extract_mean_csf_ts.inputs.file_4D = img_file
    extract_mean_csf_ts.inputs.mask_file = csf_mask_file
    extract_mean_csf_ts.inputs.suffix = "csf"
    mean_csf_ts_file = extract_mean_csf_ts.run().outputs.mean_masked_ts_file

    extra_ts = ExtractTS()
    extra_ts.inputs.indexed_rois_file = indexed_mask_file
    extra_ts.inputs.file_4D = img_file
    masked_ts_file = extra_ts.run().outputs.mean_masked_ts_file

    regress_covar = RegressCovar()
    regress_covar.inputs.masked_ts_file = masked_ts_file
    regress_covar.inputs.mean_wm_ts_file = mean_wm_ts_file
    regress_covar.inputs.mean_csf_ts_file = mean_csf_ts_file

    val = regress_covar.run().outputs
    print(val)

    os.remove(val.resid_ts_file)

    os.remove(masked_ts_file)
    os.remove(mean_csf_ts_file)
    os.remove(mean_wm_ts_file)
Esempio n. 3
0
def test_compute_conf_cor_mat():
    """test ComputeConfCorMat"""
    _make_tmp_dir()

    extract_mean_wm_ts = ExtractMeanTS()
    extract_mean_wm_ts.inputs.file_4D = img_file
    extract_mean_wm_ts.inputs.filter_thr = 0.9
    extract_mean_wm_ts.inputs.filter_mask_file = csf_mask_file
    extract_mean_wm_ts.inputs.suffix = "wm"
    mean_wm_ts_file = extract_mean_wm_ts.run().outputs.mean_masked_ts_file

    extract_mean_csf_ts = ExtractMeanTS()
    extract_mean_csf_ts.inputs.file_4D = img_file
    extract_mean_csf_ts.inputs.filter_thr = 0.9
    extract_mean_csf_ts.inputs.filter_mask_file = csf_mask_file
    extract_mean_csf_ts.inputs.suffix = "csf"
    mean_csf_ts_file = extract_mean_csf_ts.run().outputs.mean_masked_ts_file

    extra_ts = ExtractTS()
    extra_ts.inputs.indexed_rois_file = indexed_mask_file
    extra_ts.inputs.file_4D = img_file
    masked_ts_file = extra_ts.run().outputs.mean_masked_ts_file

    regress_covar = RegressCovar()
    regress_covar.inputs.masked_ts_file = masked_ts_file
    regress_covar.inputs.mean_wm_ts_file = mean_wm_ts_file
    regress_covar.inputs.mean_csf_ts_file = mean_csf_ts_file

    resid_ts_file = regress_covar.run().outputs.resid_ts_file

    compute_conf_cor_mat = ComputeConfCorMat()
    compute_conf_cor_mat.inputs.ts_file = resid_ts_file

    val = compute_conf_cor_mat.run().outputs
    print(val)

    assert os.path.exists(val.cor_mat_file)
    assert os.path.exists(val.Z_cor_mat_file)
    assert os.path.exists(val.conf_cor_mat_file)
    assert os.path.exists(val.Z_conf_cor_mat_file)

    os.remove(val.cor_mat_file)
    os.remove(val.conf_cor_mat_file)
    os.remove(val.Z_cor_mat_file)
    os.remove(val.Z_conf_cor_mat_file)

    os.remove(resid_ts_file)
    os.remove(masked_ts_file)
    os.remove(mean_csf_ts_file)
    os.remove(mean_wm_ts_file)
Esempio n. 4
0
def test_split_ts():
    """ test SplitTS"""
    _make_tmp_dir()

    extract_mean_wm_ts = ExtractMeanTS()
    extract_mean_wm_ts.inputs.file_4D = img_file
    extract_mean_wm_ts.inputs.filter_thr = 0.9
    extract_mean_wm_ts.inputs.filter_mask_file = csf_mask_file
    extract_mean_wm_ts.inputs.suffix = "wm"
    mean_wm_ts_file = extract_mean_wm_ts.run().outputs.mean_masked_ts_file

    extract_mean_csf_ts = ExtractMeanTS()
    extract_mean_csf_ts.inputs.file_4D = img_file
    extract_mean_csf_ts.inputs.filter_thr = 0.9
    extract_mean_csf_ts.inputs.filter_mask_file = csf_mask_file
    extract_mean_csf_ts.inputs.suffix = "csf"
    mean_csf_ts_file = extract_mean_csf_ts.run().outputs.mean_masked_ts_file

    extra_ts = ExtractTS()
    extra_ts.inputs.indexed_rois_file = indexed_mask_file
    extra_ts.inputs.file_4D = img_file
    masked_ts_file = extra_ts.run().outputs.mean_masked_ts_file

    regress_covar = RegressCovar()
    regress_covar.inputs.masked_ts_file = masked_ts_file
    regress_covar.inputs.mean_wm_ts_file = mean_wm_ts_file
    regress_covar.inputs.mean_csf_ts_file = mean_csf_ts_file
    resid_ts_file = regress_covar.run().outputs.resid_ts_file

    split_ts = SplitTS()
    split_ts.inputs.ts_file = resid_ts_file
    split_ts.inputs.win_length = 10
    split_ts.inputs.offset = 5

    splitted_ts_files = split_ts.run().outputs.splitted_ts_files
    assert len(splitted_ts_files) != 0
    assert os.path.exists(splitted_ts_files[0])

    for splitted_ts_file in splitted_ts_files:

        os.remove(splitted_ts_file)
Esempio n. 5
0
def create_pipeline_nii_to_conmat_seg_template(main_path,
                                               pipeline_name="nii_to_conmat",
                                               conf_interval_prob=0.05):
    """
    Description:

    Pipeline from nifti 4D (after preprocessing) to connectivity matrices

    Inputs (inputnode):

        * nii_4D_file
        * rp_file

        * wm_anat_file
        * csf_anat_file

        * ROI_mask_file
        * ROI_coords_file
        * ROI_MNI_coords_file
        * ROI_labels_file

    Comments:

    Typically used after nipype preprocessing pipeline and
    before conmat_to_graph pipeline

    """

    pipeline = pe.Workflow(name=pipeline_name)
    pipeline.base_dir = main_path

    inputnode = pe.Node(niu.IdentityInterface(fields=[
        'nii_4D_file', 'rp_file', 'wm_anat_file', 'csf_anat_file',
        'ROI_mask_file', 'ROI_coords_file', 'ROI_MNI_coords_file',
        'ROI_labels_file'
    ]),
                        name='inputnode')

    # Nodes version: use min_BOLD_intensity and
    # return coords where signal is strong enough
    extract_mean_ROI_ts = pe.Node(interface=ExtractTS(plot_fig=False),
                                  name='extract_mean_ROI_ts')

    pipeline.connect(inputnode, 'nii_4D_file', extract_mean_ROI_ts, 'file_4D')
    pipeline.connect(inputnode, 'ROI_mask_file', extract_mean_ROI_ts,
                     'indexed_rois_file')
    pipeline.connect(inputnode, 'ROI_coords_file', extract_mean_ROI_ts,
                     'coord_rois_file')
    pipeline.connect(inputnode, 'ROI_MNI_coords_file', extract_mean_ROI_ts,
                     'MNI_coord_rois_file')
    pipeline.connect(inputnode, 'ROI_labels_file', extract_mean_ROI_ts,
                     'label_rois_file')

    # extract white matter signal
    compute_wm_ts = pe.Node(interface=ExtractMeanTS(plot_fig=False),
                            name='extract_wm_ts')
    compute_wm_ts.inputs.suffix = 'wm'

    pipeline.connect(inputnode, 'nii_4D_file', compute_wm_ts, 'file_4D')
    pipeline.connect(inputnode, 'wm_anat_file', compute_wm_ts,
                     'filter_mask_file')

    # extract csf signal
    compute_csf_ts = pe.Node(interface=ExtractMeanTS(plot_fig=False),
                             name='extract_csf_ts')
    compute_csf_ts.inputs.suffix = 'csf'

    pipeline.connect(inputnode, 'nii_4D_file', compute_csf_ts, 'file_4D')
    pipeline.connect(inputnode, 'csf_anat_file', compute_csf_ts,
                     'filter_mask_file')

    regress_covar = pe.Node(interface=RegressCovar(),
                            iterfield=['masked_ts_file', 'rp_file'],
                            name='regress_covar')

    pipeline.connect(extract_mean_ROI_ts, 'mean_masked_ts_file', regress_covar,
                     'masked_ts_file')
    pipeline.connect(compute_wm_ts, 'mean_masked_ts_file', regress_covar,
                     'mean_wm_ts_file')
    pipeline.connect(compute_csf_ts, 'mean_masked_ts_file', regress_covar,
                     'mean_csf_ts_file')
    pipeline.connect(inputnode, 'rp_file', regress_covar, 'rp_file')

    # compute correlations

    compute_conf_cor_mat = pe.Node(interface=ComputeConfCorMat(),
                                   name='compute_conf_cor_mat')
    compute_conf_cor_mat.inputs.conf_interval_prob = conf_interval_prob

    pipeline.connect(regress_covar, 'resid_ts_file', compute_conf_cor_mat,
                     'ts_file')
    pipeline.connect(extract_mean_ROI_ts, 'subj_label_rois_file',
                     compute_conf_cor_mat, 'labels_file')

    return pipeline
Esempio n. 6
0
def create_pipeline_nii_to_conmat(main_path,
                                  filter_gm_threshold=0.9,
                                  pipeline_name="nii_to_conmat",
                                  conf_interval_prob=0.05,
                                  background_val=-1.0,
                                  plot=True,
                                  reslice=False,
                                  resample=False,
                                  min_BOLD_intensity=50,
                                  percent_signal=0.5):
    """
    Description:

    Pipeline from nifti 4D (after preprocessing) to connectivity matrices

    Inputs (inputnode):

        * nii_4D_file
        * rp_file
        * ROI_mask_file
        * gm_anat_file
        * wm_anat_file
        * csf_anat_file
        * ROI_coords_file
        * ROI_MNI_coords_file
        * ROI_labels_file

    Comments:

    Typically used after nipype preprocessing pipeline and
    before conmat_to_graph pipeline

    """
    if reslice and resample:
        print("Only reslice OR resample can be true, setting reslice to False")
        reslice = False

    pipeline = pe.Workflow(name=pipeline_name)
    pipeline.base_dir = main_path

    inputnode = pe.Node(niu.IdentityInterface(fields=[
        'nii_4D_file', 'ROI_mask_file', 'rp_file', 'gm_anat_file',
        'wm_anat_file', 'csf_anat_file', 'ROI_coords_file',
        'ROI_MNI_coords_file', 'ROI_labels_file'
    ]),
                        name='inputnode')

    # reslice gm
    if reslice:

        reslice_gm = pe.Node(interface=spmu.Reslice(), name='reslice_gm')
        pipeline.connect(inputnode, 'ROI_mask_file', reslice_gm,
                         'space_defining')
        pipeline.connect(inputnode, 'gm_anat_file', reslice_gm, 'in_file')

    if resample:

        resample_gm = pe.Node(interface=RegResample(), name='resample_gm')
        pipeline.connect(inputnode, 'ROI_mask_file', resample_gm, 'ref_file')
        pipeline.connect(inputnode, 'gm_anat_file', resample_gm, 'flo_file')

    # reslice wm
    if reslice:

        reslice_wm = pe.Node(interface=spmu.Reslice(), name='reslice_wm')
        pipeline.connect(inputnode, 'ROI_mask_file', reslice_wm,
                         'space_defining')
        pipeline.connect(inputnode, 'wm_anat_file', reslice_wm, 'in_file')

    if resample:

        resample_wm = pe.Node(interface=RegResample(), name='resample_wm')
        pipeline.connect(inputnode, 'ROI_mask_file', resample_wm, 'ref_file')
        pipeline.connect(inputnode, 'wm_anat_file', resample_wm, 'flo_file')
    # reslice csf
    if reslice:

        reslice_csf = pe.Node(interface=spmu.Reslice(), name='reslice_csf')
        pipeline.connect(inputnode, 'ROI_mask_file', reslice_csf,
                         'space_defining')

        pipeline.connect(inputnode, 'csf_anat_file', reslice_csf, 'in_file')

    if resample:

        resample_csf = pe.Node(interface=RegResample(), name='resample_csf')
        pipeline.connect(inputnode, 'ROI_mask_file', resample_csf, 'ref_file')
        pipeline.connect(inputnode, 'csf_anat_file', resample_csf, 'flo_file')

    # Preprocess pipeline,
    filter_ROI_mask_with_GM = pe.Node(interface=IntersectMask(),
                                      name='filter_ROI_mask_with_GM')

    filter_ROI_mask_with_GM.inputs.filter_thr = filter_gm_threshold
    filter_ROI_mask_with_GM.inputs.background_val = background_val

    pipeline.connect(inputnode, 'ROI_mask_file', filter_ROI_mask_with_GM,
                     'indexed_rois_file')
    pipeline.connect(inputnode, 'ROI_coords_file', filter_ROI_mask_with_GM,
                     'coords_rois_file')
    pipeline.connect(inputnode, 'ROI_MNI_coords_file', filter_ROI_mask_with_GM,
                     'MNI_coords_rois_file')
    pipeline.connect(inputnode, 'ROI_labels_file', filter_ROI_mask_with_GM,
                     'labels_rois_file')

    if reslice:
        pipeline.connect(reslice_gm, 'out_file', filter_ROI_mask_with_GM,
                         'filter_mask_file')

    elif resample:
        pipeline.connect(resample_gm, 'out_file', filter_ROI_mask_with_GM,
                         'filter_mask_file')

    else:
        pipeline.connect(inputnode, 'gm_anat_file', filter_ROI_mask_with_GM,
                         'filter_mask_file')

    # Nodes version: use min_BOLD_intensity and
    # return coords where signal is strong enough
    extract_mean_ROI_ts = pe.Node(interface=ExtractTS(plot_fig=plot),
                                  name='extract_mean_ROI_ts')

    extract_mean_ROI_ts.inputs.percent_signal = percent_signal
    extract_mean_ROI_ts.inputs.min_BOLD_intensity = min_BOLD_intensity

    pipeline.connect(inputnode, 'nii_4D_file', extract_mean_ROI_ts, 'file_4D')
    pipeline.connect(filter_ROI_mask_with_GM, 'filtered_indexed_rois_file',
                     extract_mean_ROI_ts, 'indexed_rois_file')
    pipeline.connect(filter_ROI_mask_with_GM, 'filtered_MNI_coords_rois_file',
                     extract_mean_ROI_ts, 'MNI_coord_rois_file')
    pipeline.connect(filter_ROI_mask_with_GM, 'filtered_coords_rois_file',
                     extract_mean_ROI_ts, 'coord_rois_file')
    pipeline.connect(filter_ROI_mask_with_GM, 'filtered_labels_rois_file',
                     extract_mean_ROI_ts, 'label_rois_file')

    # extract white matter signal
    compute_wm_ts = pe.Node(interface=ExtractMeanTS(plot_fig=plot),
                            name='extract_wm_ts')
    compute_wm_ts.inputs.suffix = 'wm'

    pipeline.connect(inputnode, 'nii_4D_file', compute_wm_ts, 'file_4D')

    if reslice:
        pipeline.connect(reslice_wm, 'out_file', compute_wm_ts,
                         'filter_mask_file')

    elif resample:
        pipeline.connect(resample_wm, 'out_file', compute_wm_ts,
                         'filter_mask_file')

    else:
        pipeline.connect(inputnode, 'wm_anat_file', compute_wm_ts,
                         'filter_mask_file')

    # extract csf signal
    compute_csf_ts = pe.Node(interface=ExtractMeanTS(plot_fig=plot),
                             name='extract_csf_ts')
    compute_csf_ts.inputs.suffix = 'csf'

    pipeline.connect(inputnode, 'nii_4D_file', compute_csf_ts, 'file_4D')

    if reslice:
        pipeline.connect(reslice_csf, 'out_file', compute_csf_ts,
                         'filter_mask_file')

    elif resample:
        pipeline.connect(resample_csf, 'out_file', compute_csf_ts,
                         'filter_mask_file')

    else:
        pipeline.connect(inputnode, 'csf_anat_file', compute_csf_ts,
                         'filter_mask_file')

    # regress covariates
    regress_covar = pe.Node(interface=RegressCovar(plot_fig=plot),
                            iterfield=[
                                'masked_ts_file', 'rp_file', 'mean_wm_ts_file',
                                'mean_csf_ts_file'
                            ],
                            name='regress_covar')

    pipeline.connect(extract_mean_ROI_ts, 'mean_masked_ts_file', regress_covar,
                     'masked_ts_file')
    pipeline.connect(inputnode, 'rp_file', regress_covar, 'rp_file')

    pipeline.connect(compute_wm_ts, 'mean_masked_ts_file', regress_covar,
                     'mean_wm_ts_file')
    pipeline.connect(compute_csf_ts, 'mean_masked_ts_file', regress_covar,
                     'mean_csf_ts_file')

    # compute correlations
    compute_conf_cor_mat = pe.Node(interface=ComputeConfCorMat(plot_mat=plot),
                                   name='compute_conf_cor_mat')
    compute_conf_cor_mat.inputs.conf_interval_prob = conf_interval_prob

    pipeline.connect(regress_covar, 'resid_ts_file', compute_conf_cor_mat,
                     'ts_file')
    pipeline.connect(extract_mean_ROI_ts, 'subj_label_rois_file',
                     compute_conf_cor_mat, 'labels_file')

    return pipeline
Esempio n. 7
0
def create_pipeline_nii_to_conmat_no_seg(main_path,
                                         pipeline_name="nii_to_conmat",
                                         conf_interval_prob=0.05):
    """
    Description:
    
    Pipeline from nifti 4D (after preprocessing) to connectivity matrices
    
    Inputs (inputnode):
    
        * nii_4D_file
        * rp_file
        * ROI_mask_file
        * ROI_coords_file
        * ROI_MNI_coords_file
        * ROI_labels_file
    
    Comments:
    
    Typically used after nipype preprocessing pipeline and before conmat_to_graph pipeline
    
    """

    pipeline = pe.Workflow(name=pipeline_name)
    pipeline.base_dir = main_path

    inputnode = pe.Node(niu.IdentityInterface(fields=[
        'nii_4D_file', 'rp_file', 'ROI_mask_file', 'ROI_coords_file',
        'ROI_MNI_coords_file', 'ROI_labels_file'
    ]),
                        name='inputnode')

    #### Nodes version: use min_BOLD_intensity and return coords where signal is strong enough
    extract_mean_ROI_ts = pe.Node(interface=ExtractTS(plot_fig=False),
                                  name='extract_mean_ROI_ts')

    #pipeline.connect(inputnode, 'ROI_coords_file', filter_ROI_mask_with_GM, 'coords_rois_file')
    #pipeline.connect(inputnode, 'ROI_MNI_coords_file', filter_ROI_mask_with_GM, 'MNI_coords_rois_file')
    #pipeline.connect(inputnode, 'ROI_labels_file', filter_ROI_mask_with_GM, 'labels_rois_file')

    #extract_mean_ROI_ts.inputs.indexed_rois_file = ROI_mask_file

    #extract_mean_ROI_ts.inputs.coord_rois_file = ROI_coords_file
    #extract_mean_ROI_ts.inputs.min_BOLD_intensity = min_BOLD_intensity

    pipeline.connect(inputnode, 'nii_4D_file', extract_mean_ROI_ts, 'file_4D')
    pipeline.connect(inputnode, 'ROI_mask_file', extract_mean_ROI_ts,
                     'indexed_rois_file')
    pipeline.connect(inputnode, 'ROI_coords_file', extract_mean_ROI_ts,
                     'coord_rois_file')
    pipeline.connect(inputnode, 'ROI_labels_file', extract_mean_ROI_ts,
                     'label_rois_file')

    #### extract white matter signal
    compute_wm_ts = pe.Node(interface=ExtractMeanTS(plot_fig=False),
                            name='extract_wm_ts')
    compute_wm_ts.inputs.suffix = 'wm'

    pipeline.connect(inputnode, 'nii_4D_file', compute_wm_ts, 'file_4D')
    compute_wm_ts.inputs.ROI_coord = [46, 40, 76]

    #### extract csf signal
    compute_csf_ts = pe.Node(interface=ExtractMeanTS(plot_fig=False),
                             name='extract_csf_ts')
    compute_csf_ts.inputs.suffix = 'csf'

    pipeline.connect(inputnode, 'nii_4D_file', compute_csf_ts, 'file_4D')
    compute_csf_ts.inputs.ROI_coord = [47, 18, 83]

    #### regress covariates

    ### use R linear model to regress movement parameters, white matter and ventricule signals, and compute Z-score of the residuals
    #regress_covar = pe.MapNode(interface = RegressCovar(filtered = False, normalized = False),iterfield = ['masked_ts_file','rp_file','mean_wm_ts_file','mean_csf_ts_file'],name='regress_covar')
    regress_covar = pe.Node(interface=RegressCovar(),
                            iterfield=['masked_ts_file', 'rp_file'],
                            name='regress_covar')

    pipeline.connect(extract_mean_ROI_ts, 'mean_masked_ts_file', regress_covar,
                     'masked_ts_file')
    pipeline.connect(compute_wm_ts, 'mean_masked_ts_file', regress_covar,
                     'mean_wm_ts_file')
    pipeline.connect(compute_csf_ts, 'mean_masked_ts_file', regress_covar,
                     'mean_csf_ts_file')
    pipeline.connect(inputnode, 'rp_file', regress_covar, 'rp_file')

    ##################################### compute correlations ####################################################

    compute_conf_cor_mat = pe.Node(interface=ComputeConfCorMat(),
                                   name='compute_conf_cor_mat')
    compute_conf_cor_mat.inputs.conf_interval_prob = conf_interval_prob

    pipeline.connect(regress_covar, 'resid_ts_file', compute_conf_cor_mat,
                     'ts_file')
    pipeline.connect(extract_mean_ROI_ts, 'subj_label_rois_file',
                     compute_conf_cor_mat, 'labels_file')

    return pipeline