Ejemplo n.º 1
0
 def _load_data_infolder(self):
     """load training data and validation data and generate label for training data"""
     print("loading...")
     # train data
     data1,_=read_multiNii_LC(self.patients_path, self.suffix)
     data1=np.squeeze(np.array([np.array(data1).reshape(1,-1) for data1 in data1]))
     data2,_=read_multiNii_LC(self.hc_path, self.suffix)
     data2=np.squeeze(np.array([np.array(data2).reshape(1,-1) for data2 in data2]))
     data=np.vstack([data1,data2])
     
     # validation data
     data_validation,self.name_val=read_multiNii_LC(self.val_path, self.suffix)
     data_validation=np.squeeze(np.array([np.array(data_validation).reshape(1,-1) for data_validation in data_validation]))
     
     # data in mask
     mask,_=read_sigleNii_LC(self.mask)
     mask=mask>=0.2
     mask=np.array(mask).reshape(-1,)
     
     self.data_train=data[:,mask]
     self.data_validation=data_validation[:,mask]
     
     # label_tr
     self.label_tr=np.hstack([np.ones([len(data1),])-1,np.ones([len(data2),])])
     print("loaded")
     return self
Ejemplo n.º 2
0
    def split_roi_for_one_subj(self, file_path):
        """load nii--split roi--save to """
        # load nii
        nii_name = file_path
        nii_data, nii_object = read_sigleNii_LC(nii_name)
        header = nii_object.header
        affine = nii_object.affine

        # split roi
        uni_label = np.unique(nii_data)
        uni_label = list(set(uni_label) - set([0]))  # 去掉0背景
        subjname = os.path.basename(file_path).split('.')[0]
        # split and save to nii
        for label in uni_label:
            # creat folder
            save_folder_name = os.path.join(self.out_path, subjname,
                                            'ROI_' + str(label))
            if not os.path.exists(save_folder_name):
                os.makedirs(save_folder_name)
            save_file_name = os.path.join(save_folder_name,
                                          os.path.basename(nii_name))

            if os.path.exists(save_file_name):
                print('{} exist!\n'.format(save_file_name))
                if self.overcopy:
                    print(f'overwrite!')
                else:
                    continue

            # split
            roi_logic = np.array(nii_data == label, dtype=float)
            roi = nii_data * roi_logic
            # ndarry to nifti object
            roi = nib.Nifti1Image(roi, affine=affine, header=header)

            # save
            save_nii(roi, save_file_name)
Ejemplo n.º 3
0
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 12 19:10:02 2018

@author: lenovo
"""
from lc_read_nii import read_sigleNii_LC
import sys
sys.path.append(r'D:\myCodes\LC_MVPA\Python\MVPA_Python\utils')

img_path_3d = r'D:\其他\陈逸凡\aLL_mask3D.nii'
img_path_alff = r'D:\其他\陈逸凡\all_maskALFF.nii'
data_3d = read_sigleNii_LC(img_path_3d)
data_alff = read_sigleNii_LC(img_path_alff)
Ejemplo n.º 4
0
class SVCDeterministicTrAndTe():
    def init(self):
        # ==============================================================================
        # input begin
        patients_path = r'K:\XiaoweiJiang\KETI\3_新建文件夹(20190308)\DATA\REST\ALFF\ALFF_BD'
        hc_path = r'K:\XiaoweiJiang\KETI\3_新建文件夹(20190308)\DATA\REST\ALFF\ALFF_HC1'
        validation_path = r'K:\XiaoweiJiang\KETI\3_新建文件夹(20190308)\DATA\REST\ALFF\ALFF_HC1'
        mask = r'G:\Softer_DataProcessing\spm12\spm12\tpm\Reslice3_TPM_greaterThan0.2.nii'
        kfold = 5
        # ==============================================================================

    # mask
    mask = read_sigleNii_LC(mask) >= 0.2
    mask = np.array(mask).reshape(-1, )

    # is_train
    if_training = 1

    # input end

    def load_nii_and_gen_label(patients_path, hc_path, mask):
        # train data
        data1 = main(patients_path)
        data1 = np.squeeze(
            np.array([np.array(data1).reshape(1, -1) for data1 in data1]))
        data2 = main(hc_path)
        data2 = np.squeeze(
            np.array([np.array(data2).reshape(1, -1) for data2 in data2]))
        data = np.vstack([data1, data2])

        # validation data
        data_val = main(validation_path)
        data_val = np.squeeze(
            np.array(
                [np.array(data_val).reshape(1, -1) for data_val in data_val]))

        # data in mask
        data_tr = data[:, mask]
        data_val = data_val[:, mask]

        # label_tr
        label_tr = np.hstack(
            [np.ones([
                len(data1),
            ]) - 1, np.ones([
                len(data2),
            ])])
        return label_tr, data_tr, data_val

    # training and test
    def tr_te():
        import lc_svc_rfe_cv_V2 as lsvc
        svc = lsvc.SVCRefCv(pca_n_component=0.9,
                            show_results=1,
                            show_roc=0,
                            k=kfold)
        if if_training:
            results = svc.svc_rfe_cv(data_tr, label_tr)
        return results

    # run
    data_tr, label_tr = datasets.make_classification(n_samples=200,
                                                     n_classes=2,
                                                     n_informative=50,
                                                     n_redundant=3,
                                                     n_features=100,
                                                     random_state=1)

    label_tr, data_tr, data_val = load_nii_and_gen_label(
        patients_path, hc_path, mask)

    selector, weight = rfeCV(data_tr,
                             label_tr,
                             step=0.1,
                             cv=kfold,
                             n_jobs=1,
                             permutation=0)

    results = tr_te()
    results = results.__dict__
    y_pred = selector.predict(x)
Ejemplo n.º 5
0
 def read(self, nii_name):
     nii_data, nii_object = read_sigleNii_LC(nii_name)
     yield nii_data
Ejemplo n.º 6
0
)

# ==============================================================================
# input

# 外部数据
folder_p_2 = r'D:\WorkStation_2018\Workstation_Old\WorkStation_2018-05_MVPA_insomnia_FCS\Degree\degree_gray_matter\Zdegree\Z_degree_control\C_Weighted_selected'
folder_hc_2 = r'D:\WorkStation_2018\Workstation_Old\WorkStation_2018-05_MVPA_insomnia_FCS\Degree\degree_gray_matter\Zdegree\Z_degree_patient\P_Weighted_selected'

# 内部数据
folder_p_1 = r'D:\WorkStation_2018\Workstation_Old\WorkStation_2018-05_MVPA_insomnia_FCS\Degree\degree_gray_matter\Zdegree\Z_degree_control\C_Weighted_selected'
folder_hc_1 = r'D:\WorkStation_2018\Workstation_Old\WorkStation_2018-05_MVPA_insomnia_FCS\Degree\degree_gray_matter\Zdegree\Z_degree_patient\P_Weighted_selected'

# 灰质mask
mask = r'G:\Softer_DataProcessing\spm12\spm12\tpm\Reslice3_TPM_greaterThan0.2.nii'
mask = read_sigleNii_LC(mask) >= 0.2
mask = np.array(mask).reshape(-1, )

# 设置训练与否
if_training_inner_cv = 1
if_training_outer_cv = 0
if_show_data_distribution = 0  # 显示训练集和测试集数据分布
# ==============================================================================


def load_nii_and_gen_label(folder_p, folder_hc, mask):

    # data
    data_p = main(folder_p)
    data_p = np.squeeze(
        np.array([np.array(data_p).reshape(1, -1) for data_p in data_p]))
Ejemplo n.º 7
0
    r'D:\My_Codes\LC_Machine_Learning\lc_rsfmri_tools\lc_rsfmri_tools_python\Utils'
)
sys.path.append(
    r'D:\My_Codes\LC_Machine_Learning\lc_rsfmri_tools\lc_rsfmri_tools_python\Machine_learning\classfication'
)
from lc_read_nii import main
from lc_read_nii import read_sigleNii_LC
import numpy as np

# =====================================================================
patients_path = r'D:\WorkStation_2018\Workstation_Old\WorkStation_2018-05_MVPA_insomnia_FCS\Degree\degree_gray_matter\Zdegree\Z_degree_patient\P_Weighted_selected'
hc_path = r'D:\WorkStation_2018\Workstation_Old\WorkStation_2018-05_MVPA_insomnia_FCS\Degree\degree_gray_matter\Zdegree\Z_degree_control\C_Weighted_selected'
mask = r'G:\Softer_DataProcessing\spm12\spm12\tpm\Reslice3_TPM_greaterThan0.2.nii'
# =====================================================================
is_train = 1
mask, _ = read_sigleNii_LC(mask)
mask = mask >= 0.2
mask = np.array(mask).reshape(-1, )


def load_nii_and_gen_label(patients_path, hc_path, mask):
    # data
    data1, _ = main(patients_path, '.img')
    data1 = np.squeeze(
        np.array([np.array(data1).reshape(1, -1) for data1 in data1]))

    data2, _ = main(hc_path, '.img')
    data2 = np.squeeze(
        np.array([np.array(data2).reshape(1, -1) for data2 in data2]))

    data = np.vstack([data1, data2])