def return_scores_and_labels_for_5fold(i, type, model_number, projectName): # 有kmer的 # load datset # print('load dataset with kmer') datset_test = LncRNA_Protein_Interaction_dataset_1hop_1220_InMemory( f'data/dataset/{projectName}_inMemory{type}_test_{i}') print(f'载入数据集:data/dataset/{projectName}_inMemory{type}_test_{i}') test_loader = DataLoader(datset_test, batch_size=60) # load model # print('load model with kmer') device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = Net_1(datset_test.num_node_features) model.load_state_dict( torch.load( f'result/{projectName}{type}/model_{i}_fold/{model_number}')) print(f'载入模型:result/{projectName}{type}/model_{i}_fold/{model_number}') # run test scores_temp, labels_temp = return_scores_and_labels( model, test_loader, device) return scores_temp, labels_temp
parser.add_argument('--testName', help='name of this test') parser.add_argument('--path_model', help='the path of model') parser.add_argument('--path_dataset', help='the path of test dataset') return parser.parse_args() if __name__ == "__main__": args = parse_args() # load datset print('load dataset') datset_test = LncRNA_Protein_Interaction_dataset(args.path_dataset) test_loader = DataLoader(datset_test, batch_size=60) # load model print('load model') device = torch.device('cuda') model = Net_1(datset_test.num_node_features).to(device) model.load_state_dict(torch.load(args.path_model)) # run test print('run test') Accuracy, Precision, Sensitivity, Specificity, MCC = Accuracy_Precision_Sensitivity_Specificity_MCC( model, test_loader, device) output = 'Accuracy: {:.5f}, Precision: {:.5f}, Sensitivity: {:.5f}, Specificity: {:.5f}, MCC: {:.5f}'.format( Accuracy, Precision, Sensitivity, Specificity, MCC) print(output) # output log path_log_folder = osp.join(r'.\result', args.testName) if not osp.exists(path_log_folder): os.mkdir(path_log_folder) path_log = osp.join(path_log_folder, 'log.txt') with open(path_log, 'w') as log: log.write(output)
Accuracy_list = [] Precision_list = [] Sensitivity_list = [] Specificity_list = [] MCC_list = [] for i in range(k): print('{:d}-fold start'.format(i+1)) # 创建保存模型的文件夹 os.makedirs(saving_path + f'/model_{i}_fold') # 创建模型 num_of_classes = 2 if i != 0: del model gc.collect() model = Net_1(dataset.num_node_features, num_of_classes).to(device) optimizer = torch.optim.Adam(model.parameters(), lr=LR, weight_decay=L2_weight_decay) # scheduler = lr_scheduler.MultiStepLR(optimizer,milestones=[int(num_of_epoch * 0.2),int(num_of_epoch * 0.8)],gamma = 0.8) scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer=optimizer, gamma=0.95) # 训练集和测试集 test_dataset_start = 0 + i * step_length test_dataset_end = (i + 1) * step_length test_dataset = dataset[test_dataset_start:test_dataset_end] train_dataset = dataset[0:test_dataset_start] + dataset[test_dataset_end:dataset.len()] print('number of samples in testing dataset:', len(test_dataset), 'number of samples in training dataset:', len(train_dataset)) print('training dataset') dataset_analysis(train_dataset) print('testing dataset')
read_random_node_embedding() # load k-mer if args.noKmer == 0: lncRNA_3_mer_path = f'data/lncRNA_3_mer/{args.interactionDatasetName}/lncRNA_3_mer.txt' protein_2_mer_path = f'data/protein_2_mer/{args.interactionDatasetName}/protein_2_mer.txt' load_node_k_mer(lncRNA_list, 'lncRNA', lncRNA_3_mer_path) load_node_k_mer(protein_list, 'protein', protein_2_mer_path) # 执行检查 load_exam(args.noKmer, lncRNA_list, protein_list) # 载入模型 device = torch.device('cuda') model = Net_1(178).to(device) model.load_state_dict(torch.load(args.modelPath)) # 开始case study # 构建从serial number到名字的字典 dict_interactionKey_interaction = return_dict_interactionKey_interaction(interaction_list) # 创建保存数据集和log的文件夹 case_study_path = f'data/case_study/{args.caseStudyName}/datasets' if not osp.exists(case_study_path): os.makedirs(case_study_path) print(f'创建了文件夹:{case_study_path}') log_path = f'data/case_study/{args.caseStudyName}/logs' if not osp.exists(log_path): os.makedirs(log_path)