Ejemplo n.º 1
0
def test_ni_parcellate(clust_type):
    """
    Test for ni_parcellate
    """
    import tempfile

    k = 20
    base_dir = str(Path(__file__).parent / "examples")
    out_dir = f"{base_dir}/outputs/sub-25659/ses-1/func"
    tmpdir = tempfile.TemporaryDirectory()
    if clust_type != 'ncut':
        local_corr = 'allcorr'
    else:
        local_corr = 'tcorr'
    clust_mask = f"{base_dir}/miscellaneous/rMFG_node6mm.nii.gz"
    mask = f"{base_dir}/BIDS/sub-25659/ses-1/anat/sub-25659_desc-brain_mask.nii.gz"
    func_file = f"{base_dir}/BIDS/sub-25659/ses-1/func/sub-25659_ses-1_task-rest_space-MNI152NLin6Asym_desc-" \
        f"smoothAROMAnonaggr_bold_short.nii.gz"
    func_img = nib.load(func_file)
    nip = clustools.NiParcellate(func_file=func_file,
                                 clust_mask=clust_mask,
                                 k=k,
                                 clust_type=clust_type,
                                 local_corr=local_corr,
                                 outdir=out_dir,
                                 conf=None,
                                 mask=mask)
    atlas = nip.create_clean_mask()
    nip.create_local_clustering(overwrite=True, r_thresh=0.4)
    out_path = f"{str(tmpdir.name)}/parc_tmp.nii.gz"
    nib.save(nip.parcellate(func_img), out_path)

    assert out_path is not None
    assert atlas is not None
Ejemplo n.º 2
0
def test_ni_parcellate_mult_conn_comps(clust_type):
    """
    Test for ni_parcellate with multiple connected components
    """
    import tempfile

    base_dir = str(Path(__file__).parent / "examples")
    out_dir = f"{base_dir}/outputs/sub-0025427/ses-1/func"
    tmpdir = tempfile.TemporaryDirectory()

    k = 150
    if clust_type != 'ncut':
        local_corr = 'allcorr'
    else:
        local_corr = 'tcorr'
    clust_mask = f"{base_dir}/miscellaneous/pDMN_3_bin.nii.gz"
    mask = f"{base_dir}/BIDS/sub-0025427/ses-1/func/sub-0025427_ses-1_task-rest_space-MNI152NLin2009cAsym_desc-brain_mask.nii.gz"
    func_file = f"{base_dir}/BIDS/sub-0025427/ses-1/func/sub-0025427_ses-1_task-rest_space-MNI152NLin2009cAsym_desc-smoothAROMAnonaggr_bold_short.nii.gz"
    func_img = nib.load(func_file)
    nip = clustools.NiParcellate(func_file=func_file,
                                 clust_mask=clust_mask,
                                 k=k,
                                 clust_type=clust_type,
                                 local_corr=local_corr,
                                 outdir=out_dir,
                                 conf=None,
                                 mask=mask)

    atlas = nip.create_clean_mask()
    nip = clustools.NiParcellate(func_file=func_file,
                                 clust_mask=clust_mask,
                                 k=k,
                                 clust_type=clust_type,
                                 local_corr=local_corr,
                                 outdir=out_dir,
                                 conf=None,
                                 mask=mask)

    if not nip.uatlas:
        nip.uatlas = f"{tmpdir.name}/clust-{clust_type}_k{str(k)}.nii.gz"
    nip._clust_mask_corr_img = nib.load(clust_mask)
    nip.create_local_clustering(overwrite=True, r_thresh=0.4)
    out_path = f"{str(tmpdir.name)}/parc_tmp.nii.gz"
    nib.save(nip.parcellate(func_img), out_path)
    assert atlas is not None
    assert out_path is not None
Ejemplo n.º 3
0
    def _run_interface(self, runtime):
        import gc
        from nipype.utils.filemanip import fname_presuffix, copyfile
        from pynets.fmri import clustools
        from pynets.registration.reg_utils import check_orient_and_dims

        clust_list = ['kmeans', 'ward', 'complete', 'average', 'ncut', 'rena']

        clust_mask_temp_path = check_orient_and_dims(self.inputs.clust_mask, self.inputs.vox_size,
                                                     outdir=runtime.cwd)

        if self.inputs.mask:
            out_name_mask = fname_presuffix(self.inputs.mask, suffix='_tmp', newpath=runtime.cwd)
            copyfile(self.inputs.mask, out_name_mask, copy=True, use_hardlink=False)
        else:
            out_name_mask = None

        out_name_func_file = fname_presuffix(self.inputs.func_file, suffix='_tmp', newpath=runtime.cwd)
        copyfile(self.inputs.func_file, out_name_func_file, copy=True, use_hardlink=False)

        if self.inputs.conf:
            out_name_conf = fname_presuffix(self.inputs.conf, suffix='_tmp', newpath=runtime.cwd)
            copyfile(self.inputs.conf, out_name_conf, copy=True, use_hardlink=False)
        else:
            out_name_conf = None

        nip = clustools.NiParcellate(func_file=out_name_func_file,
                                     clust_mask=clust_mask_temp_path,
                                     k=self.inputs.k,
                                     clust_type=self.inputs.clust_type,
                                     local_corr=self.inputs.local_corr,
                                     conf=out_name_conf,
                                     mask=out_name_mask)

        atlas = nip.create_clean_mask()
        nip.create_local_clustering(overwrite=True, r_thresh=0.4)

        if self.inputs.clust_type in clust_list:
            uatlas = nip.parcellate()
        else:
            raise ValueError('Clustering method not recognized. '
                             'See: https://nilearn.github.io/modules/generated/nilearn.regions.Parcellations.'
                             'html#nilearn.regions.Parcellations')
        del nip
        gc.collect()

        self._results['atlas'] = atlas
        self._results['uatlas'] = uatlas
        self._results['clust_mask'] = self.inputs.clust_mask
        self._results['k'] = self.inputs.k
        self._results['clust_type'] = self.inputs.clust_type
        self._results['clustering'] = True
        return runtime
Ejemplo n.º 4
0
def test_ni_parcellate_mult_conn_comps(clust_type):
    """
    Test for ni_parcellate with multiple connected components
    """
    import tempfile

    base_dir = str(Path(__file__).parent / "examples")
    out_dir = f"{base_dir}/outputs/sub-25659/ses-1/func"
    tmpdir = tempfile.TemporaryDirectory()

    k = 150
    if clust_type != 'ncut':
        local_corr = 'allcorr'
    else:
        local_corr = 'tcorr'
    clust_mask = f"{base_dir}/miscellaneous/pDMN_3_bin.nii.gz"
    mask = f"{base_dir}/BIDS/sub-25659/ses-1/anat/sub-25659_desc-brain_" \
           f"mask.nii.gz"
    func_file = f"{base_dir}/BIDS/sub-25659/ses-1/func/sub-25659_ses-1_task-" \
                f"rest_space-MNI152NLin6Asym_desc-" \
        f"smoothAROMAnonaggr_bold_short.nii.gz"
    conf = f"{base_dir}/BIDS/sub-25659/ses-1/func/" \
           f"sub-25659_ses-1_task-rest_desc-confounds_regressors.tsv"
    func_img = nib.load(func_file)
    nip = clustools.NiParcellate(func_file=func_file,
                                 clust_mask=clust_mask,
                                 k=k,
                                 clust_type=clust_type,
                                 local_corr=local_corr,
                                 outdir=out_dir,
                                 conf=conf,
                                 mask=mask)

    atlas = nip.create_clean_mask()

    if not nip.uatlas:
        nip.uatlas = f"{tmpdir.name}/clust-{clust_type}_k{str(k)}.nii.gz"
    nip._clust_mask_corr_img = nib.load(clust_mask)
    nip.create_local_clustering(overwrite=True, r_thresh=0.4)
    out_path = f"{str(tmpdir.name)}/parc_tmp.nii.gz"

    parcellation = clustools.parcellate(
        func_img, local_corr, clust_type, nip._local_conn_mat_path,
        nip.num_conn_comps, nip._clust_mask_corr_img, nip._standardize,
        nip._detrending, nip.k, nip._local_conn, nip.conf, tmpdir.name,
        nip._conn_comps)

    nib.save(parcellation, out_path)
    assert atlas is not None
    assert os.path.isfile(out_path)
Ejemplo n.º 5
0
def test_ni_parcellate(local_corr, clust_type, k):
    """
    Test for ni_parcellate
    """
    base_dir = str(Path(__file__).parent / "examples")
    dir_path = base_dir + '/002/fmri'
    clust_mask = base_dir + '/triple_net_ICA_overlap_3_sig_bin.nii.gz'
    func_file = dir_path + '/002.nii.gz'
    conf = None
    mask = None
    nip = clustools.NiParcellate(func_file=func_file,
                                 clust_mask=clust_mask,
                                 k=k,
                                 clust_type=clust_type,
                                 local_corr=local_corr,
                                 conf=conf,
                                 mask=mask)
    atlas = nip.create_clean_mask()
    nip.create_local_clustering(overwrite=True, r_thresh=0.5)
    uatlas = nip.parcellate()
    assert atlas is not None
    assert uatlas is not None