def get_r_p_feature_name(self, sorted_number, show=False): r_list = [] p_list = [] for feature_name in self.pd_feature.columns[1:]: # 注意不要casename feature_value = self.pd_feature[feature_name] new_t2_value, new_feature_value = self.remove_bad_data( t2_value=self.t2_value, feature_value=feature_value) r, p = pearsonr(new_t2_value, new_feature_value) if p < 0.05: r_list.append(r) p_list.append(p) r_min = sorted([abs(i) for i in r_list], reverse=True)[sorted_number - 1] new_r_list = [] new_p_list = [] new_feature_name = [] for feature_name in self.pd_feature.columns[1:]: # 注意不要casename feature_value = self.pd_feature[feature_name] new_t2_value, new_feature_value = self.remove_bad_data( t2_value=self.t2_value, feature_value=feature_value) r, p = pearsonr(new_t2_value, new_feature_value) if p < 0.05 and abs(r) >= r_min: # 一定要注意不要忘记绝对值 r = round(r, 4) p = round(p, 4) new_r_list.append(r) new_p_list.append(p) new_feature_name.append(feature_name) #################### # 显示相关系数图 #################### if show is True: sns.regplot( x=new_feature_value, # 特征值 y=new_t2_value, # t2值 scatter_kws={'s': 15}, ) plt.xlabel(feature_name) plt.ylabel("T2 value") plt.title("correlation value = " + str(round(r, 4))) store_path = make_folder( os.path.join(self.folder_path, self.feature_name)) plt.savefig(os.path.join(store_path, feature_name + ".png")) plt.clf() # plt.show() ##################### p_r_feature_name_result = pd.DataFrame( data={ "feature_name": new_feature_name, "r_value": new_r_list, "p_value": new_p_list }) store_path = make_folder( os.path.join(self.folder_path, self.feature_name)) p_r_feature_name_result.to_csv(os.path.join(store_path, "correlation_index.csv"), index=None)
def get_dicom_by_echo(): """获取dicom按照回波的顺序进行排序保存""" root_path = r"Y:\DYB\2020832DATA\doctor_xie\normal_control" for case in os.listdir(root_path): case_path = os.path.join(root_path, case) # 创建保存五个回波的文件夹 save_folder_name = ["new_folder_" + str(i) for i in range(1, 6)] save_folder_path = [ os.path.join(case_path, i) for i in save_folder_name ] # case TE 是一个case的TE值 case_TE = get_TE_in_one_case(case_path) dicom_folder_path = os.path.join(case_path, "all_dicom") for file in sort_file(dicom_folder_path): # 务必进行排序 dicom_path = os.path.join(dicom_folder_path, file) # 每一张dicom的路径 _, TE, _ = get_TE(dicom_path) if TE == case_TE[0]: shutil.copy(dicom_path, make_folder(save_folder_path[0])) elif TE == case_TE[1]: shutil.copy(dicom_path, make_folder(save_folder_path[1])) elif TE == case_TE[2]: shutil.copy(dicom_path, make_folder(save_folder_path[2])) elif TE == case_TE[3]: shutil.copy(dicom_path, make_folder(save_folder_path[3])) else: shutil.copy(dicom_path, make_folder(save_folder_path[4])) print("case {} is finished!".format(case))
def __init__(self, src_folder_path): self.src_folder_path = src_folder_path # 里面存放了train,test self.train_path = os.path.join(self.src_folder_path, "train") self.test_path = os.path.join(self.src_folder_path, "test") self.store_trian_path = os.path.join(self.src_folder_path, "new_train") make_folder(self.store_trian_path) self.store_test_path = os.path.join(self.src_folder_path, "new_test") make_folder(self.store_test_path)
def get_case(self): for case in os.listdir(self.folder_path): case_path = os.path.join(self.folder_path, case) store_case_path = os.path.join(self.store_path, case) make_folder(store_case_path) for file in os.listdir(case_path): if file != "SeriesRois": file_path = os.path.join(case_path, file) shutil.copy(file_path, store_case_path)
def move_test(self, store_name): roi_number, data_path_list, roi_path_list = self.get_roi_number( self.test_path) for data, roi, count in zip(data_path_list, roi_path_list, range(1, roi_number)): store_data_path = os.path.join(self.store_test_path, store_name + "_test_" + str(count)) make_folder(store_data_path) shutil.copy(data, store_data_path) shutil.copy(roi, store_data_path)
def get_dicom_and_roi(): """将数据的dicom和roi放在一起""" root_dicom_path = r"Y:\DYB\2020832DATA\doctor_xie\normal_control" root_roi_path = r"Y:\DYB\2020832DATA\doctor_xie\Normal_data_nii_and_T2" store_path = r"Y:\DYB\2020832DATA\doctor_xie\data" for dicom, roi in zip(os.listdir(root_dicom_path), os.listdir(root_roi_path)): dicom_path = os.path.join(root_dicom_path, dicom, "folder_1") roi_path = os.path.join(root_roi_path, roi, "roi.nii") store_case_path = make_folder(os.path.join(store_path, dicom)) store_case_dicom = make_folder(os.path.join(store_case_path, "dicom")) shutil.copy(roi_path, store_case_path) for file in os.listdir(dicom_path): dicom_file_path = os.path.join(os.path.join(dicom_path, file)) shutil.copy(dicom_file_path, store_case_dicom) print(dicom)
def __init__(self): # 这里最后初始化的时候,就传入case_path就可以了 self.case_path = r"Y:\DYB\2020832DATA\doctor_xie\normal_control\case0" self.nii_folder_path = os.path.join(self.case_path, "nii_folder") self.dcm_folder_path = os.path.join(self.case_path, "all_dicom") self.store_dicom_path = make_folder( os.path.join(self.case_path, "store_dicom"))
def get_all_dicom(): root_path = r"Y:\DYB\2020832DATA\doctor_xie\split_data_ACLR_T2" for case in os.listdir(root_path): case_path = os.path.join(root_path, case) store_all_folder = make_folder(os.path.join(case_path, "all_dicom")) for folder in os.listdir(case_path): folder_path = os.path.join(case_path, folder) for file in os.listdir(folder_path): file_path = os.path.join(folder_path, file) shutil.copy(file_path, store_all_folder) print(case)
def check_all_case_roi(): root_path = r"Y:\DYB\PETCT_EGFR" store_path = r"Y:\DYB\2020832DATA\doctor_gao\EGFR_PET_CT_DATA" for folder in os.listdir(root_path): folder_path = os.path.join(root_path, folder) store_folder_path = make_folder(os.path.join(store_path, folder)) for case in os.listdir(folder_path): store_case_path = make_folder(os.path.join(store_folder_path, case)) case_path = os.path.join(folder_path, case) ct_path = os.path.join(case_path, "ct.nii") pet_path = os.path.join(case_path, "new_pet_resize.nii") roi_path = os.path.join(case_path, "ct_roi.nii") shutil.copy(ct_path, store_case_path) shutil.copy(pet_path, store_case_path) shutil.copy(roi_path, store_case_path) print("case {} is finished!".format(case))
def get_2d_roi(): root_path = r"Y:\LS\liusheng _shidaoai_train_and_test" store_path = r"Y:\LS\xxxxxxxxxxxx" for folder in os.listdir(root_path): folder_path = os.path.join(root_path, folder) store_folder_path = make_folder(os.path.join(store_path, folder)) for case in os.listdir(folder_path): case_path = os.path.join(folder_path, case) data_path = os.path.join(case_path, "data.nii.gz") roi_path = os.path.join(case_path, "ROI.nii.gz") data_array = get_array_from_path(data_path) roi_array = get_array_from_path(roi_path) # 存放一个方向的结果 for i in range(roi_array.shape[-1]): if np.sum(roi_array[..., i]) != 0: # 开始存放: # data 1 是从一个方向看的结果 data_1 = data_array[..., i-1:i+2] roi_1 = roi_array[..., i-1:i+2] store_case_path = make_folder(os.path.join(store_folder_path, str(i)+"_1_"+case)) np.save(os.path.join(store_case_path, "data.npy"), data_1) np.save(os.path.join(store_case_path, "roi.npy"), roi_1) print("---1---") # 存放另一个方向的结果 for j in range(roi_array.shape[0]): if np.sum(roi_array[j, ...]) != 0: # 开始存放 # data 2 是从第二个方向看的结果 data_2 = data_array[j-1:j+2, ...] roi_2 = roi_array[j-1:j+2, ...] store_case_path = make_folder(os.path.join(store_folder_path, str(j) + "_2_" + case)) np.save(os.path.join(store_case_path, "data.npy"), data_2) np.save(os.path.join(store_case_path, "roi.npy"), roi_2) print("---2---") # 存放第三个方向看的结果 for k in range(roi_array.shape[1]): if np.sum(roi_array[:, k, :]) != 0: # 开始存放第三个方向的结果 data 3 data_3 = data_array[:, k-1:k+2, :] roi_3 = roi_array[:, k-1:k+2, :] store_case_path = make_folder(os.path.join(store_folder_path, str(k) + "_3_" + case)) np.save(os.path.join(store_case_path, "data.npy"), data_3) np.save(os.path.join(store_case_path, "roi.npy"), roi_3) print("---3---") print("case {} is finished!".format(case))
def get_nii_by_echo(): """将每个回波的图像转为nii""" # 正常对照组 # root_path = r"Y:\DYB\2020832DATA\doctor_xie\normal_control" # 有病的组 root_path = r"Y:\DYB\2020832DATA\doctor_xie\split_data_ACLR_T2" for case in os.listdir(root_path): case_path = os.path.join(root_path, case) # 创建保存五个回波的文件夹 save_folder_name = ["folder_" + str(i) for i in range(1, 6)] save_folder_path = [ os.path.join(case_path, i) for i in save_folder_name ] for folder in save_folder_path: save_folder = make_folder(os.path.join(case_path, folder + "_nii")) folder_path = os.path.join(case_path, folder) ProcessSeries(folder_path, save_folder)
def __init__(self, root_path, save_folder_name): self.root_path = root_path self.store_path = r"Y:\DYB\2020832DATA\doctor_gao\pet_ct_egfr" self.save_folder_path = make_folder( os.path.join(self.store_path, save_folder_name))
def get_k_means_label(data_path, store_path, store_name): data = pd.read_csv(data_path) data_load = LoadData() data_load.set_pd_data(data) case_name, label, feature_name, array = data_load.get_element() array = StandardScaler().fit_transform(array) array = np.transpose(array) k_means_model = KMeans(n_clusters=6).fit(array) labels = k_means_model.labels_ # result = update_frame_by_data(case_name=feature_name, feature_name=case_name, # feature_value=array, label=labels) # result.to_csv(os.path.join(r"Y:\DYB\data_and_result\doctor tao\roi_3", "聚类.csv"), # index=None) label_0_feature = ["CaseName", "label"] + [] label_1_feature = ["CaseName", "label"] + [] label_2_feature = ["CaseName", "label"] + [] label_3_feature = ["CaseName", "label"] + [] label_4_feature = ["CaseName", "label"] + [] label_5_feature = ["CaseName", "label"] + [] for feature, l in zip(feature_name, labels): if l == 0: label_0_feature.append(feature) elif l == 1: label_1_feature.append(feature) elif l == 2: label_2_feature.append(feature) elif l == 3: label_3_feature.append(feature) elif l == 4: label_4_feature.append(feature) elif l == 5: label_5_feature.append(feature) origin_feature = pd.read_csv(data_path) group_1_feature = origin_feature[label_0_feature] group_2_feature = origin_feature[label_1_feature] group_3_feature = origin_feature[label_2_feature] group_4_feature = origin_feature[label_3_feature] group_5_feature = origin_feature[label_4_feature] group_6_feature = origin_feature[label_5_feature] group_1_feature.to_csv(os.path.join( make_folder(os.path.join(store_path, "group_1")), store_name + ".csv"), index=None) group_2_feature.to_csv(os.path.join( make_folder(os.path.join(store_path, "group_2")), store_name + ".csv"), index=None) group_3_feature.to_csv(os.path.join( make_folder(os.path.join(store_path, "group_3")), store_name + ".csv"), index=None) group_4_feature.to_csv(os.path.join( make_folder(os.path.join(store_path, "group_4")), store_name + ".csv"), index=None) group_5_feature.to_csv(os.path.join( make_folder(os.path.join(store_path, "group_5")), store_name + ".csv"), index=None) group_6_feature.to_csv(os.path.join( make_folder(os.path.join(store_path, "group_6")), store_name + ".csv"), index=None)
def __init__(self, data_folder_path, store_folder_path): self.data_folder_path = data_folder_path self.store_folder_path = make_folder(store_folder_path)
import os import shutil from Tool.FolderProcress import make_folder, remove_folder store_path = r"E:\Data\doctor_xie\新建文件夹 (2)" data_path = r"E:\Data\doctor_xie\新建文件夹" for case in os.listdir(data_path): store_case_path = os.path.join(store_path, case) make_folder(store_case_path) case_path = os.path.join(data_path, case) for file in os.listdir(case_path): if file != "data_1.nii" and file != "roi.nii.gz": file_path = os.path.join(case_path, file) shutil.move(file_path, store_case_path)