def frag_dl_fe_process(args): # ** 当fragment为1,且选则深度学习特征提取方法时进行下列操作 ** # # 生成特征向量文件名 out_files = out_dl_frag_file(args.label, args.results_dir, ind=False) # 生成深度特征向量文件 res_base2frag_vec(args.fea_file, args.res_labels_list, args.fixed_len, out_files) # 获取深度特征向量; fixed_seq_len_list: 最大序列长度为fixed_len的序列长度的列表 vectors, sp_num_list, fixed_seq_len_list = read_dl_vec4seq(args.fixed_len, out_files, return_sp=True) # 生成标签数组 label_array = gen_label_array(sp_num_list, args.label) # 深度学习参数字典 params_dict = args.params_dict_list[0] # 深度学习的独立测试和交叉验证分开 if args.ind_seq_file is None: # 在参数便利前进行一系列准备工作: 1. 固定划分;2.设定指标;3.指定任务类型 args = prepare4train_seq(args, label_array, dl=True) # 构建深度学习分类器 seq_dcp(args.ml, vectors, label_array, fixed_seq_len_list, args.fixed_len, args.folds, args.results_dir, params_dict) else: # 独立验证开始 ind_frag_dl_fe_process(args, vectors, label_array, fixed_seq_len_list, params_dict)
def ind_ml_fe_process(args, vectors, labels, params_selected): print('########################## Independent Test Begin ##########################\n') # 合并独立测试集序列文件 ind_input_one_file = create_all_seq_file(args.ind_seq_file, args.results_dir) # 统计独立测试集样本数目和序列长度 ind_sp_num_list, ind_seq_len_list = seq_file2one(args.category, args.ind_seq_file, args.label, ind_input_one_file) # 生成独立测试集标签数组 ind_label_array = gen_label_array(ind_sp_num_list, args.label) # 生成独立测试集特征向量文件名 ind_out_files = out_ind_file(args.label, args.format, args.results_dir) # 特征提取 one_seq_fe_process(args, ind_input_one_file, ind_label_array, ind_out_files, ind_sp_num_list, True, **params_selected) # 获取独立测试集特征向量 ind_vectors = files2vectors_seq(ind_out_files, args.format) print(' Shape of Ind Feature vectors: [%d, %d] '.center(66, '*') % (ind_vectors.shape[0], ind_vectors.shape[1])) if args.score == 'none': ind_vectors = fa_process(args, ind_vectors, ind_label_array, after_ps=True, ind=True) print(' Shape of Ind Feature vectors after FA process: [%d, %d] '.center(66, '*') % (ind_vectors.shape[0], ind_vectors.shape[1])) # 为独立测试集构建分类器 args.ind_vec_file = ind_out_files ind_ml_results(args, vectors, labels, ind_vectors, ind_label_array, params_selected) print('########################## Independent Test Finish ##########################\n')
def seq_fe_process(args): current_path = os.path.dirname(os.path.realpath(__file__)) args.current_dir = os.path.dirname(os.getcwd()) # 判断是否包含中文路径 check_contain_chinese(current_path) # 生成结果文件夹 args.results_dir = create_results_dir(args, args.current_dir) # 合并序列文件 input_one_file = create_all_seq_file(args.seq_file, args.results_dir) # 统计样本数目和序列长度 sp_num_list, seq_len_list = seq_file2one(args.category, args.seq_file, args.label, input_one_file) # 生成标签数组 label_array = gen_label_array(sp_num_list, args.label) # 控制序列的固定长度 args.fixed_len = fixed_len_control(seq_len_list, args.fixed_len) # 通过遍历参数字典列表来获得特征向量,同时将特征向量写入文件(打分特征除外) # 对每个mode的method进行检查 seq_feature_check(args) # 对每个mode的words和method的参数进行检查 all_params_list_dict = {} # 适配框架 # params_list_dict 为只包括特征提取的参数的字典 params_list_dict, all_params_list_dict = mode_params_check(args, all_params_list_dict) params_dict_list = make_params_dicts(all_params_list_dict) # 这里的策略是遍历所有数值参数来并行计算 if args.dl == 0: # 多进程计算 pool = multiprocessing.Pool(args.cpu) for i in range(len(params_dict_list)): params_dict = params_dict_list[i] vec_files = out_seq_file(args.label, args.format, args.results_dir, params_dict, params_list_dict) params_dict['out_files'] = vec_files # 注意参数报错pool并不会显示,所以需要测试模式,而非直接并行 # 测试模式 # one_seq_fe_process(args, input_one_file, labels, vec_files, sample_num_list, False, **params_dict) pool.apply_async(one_seq_fe_process, (args, input_one_file, label_array, vec_files, sp_num_list, False, params_dict)) pool.close() pool.join() else: params_dict = params_dict_list[0] vec_files = out_dl_seq_file(args.label, args.results_dir, ind=False) params_dict['out_files'] = vec_files one_seq_fe_process(args, input_one_file, label_array, vec_files, sp_num_list, False, **params_dict)
def ind_frag_dl_fe_process(args, vectors, label_array, fixed_seq_len_list, params_dict): print('########################## Independent Test Begin ##########################\n') # 生成特征向量文件名 ind_out_files = out_dl_frag_file(args.label, args.results_dir, ind=False) # 生成深度特征向量文件 res_base2frag_vec(args.ind_fea_file, args.ind_res_labels_list, args.fixed_len, ind_out_files) # 获取深度特征向量; fixed_seq_len_list: 最大序列长度为fixed_len的序列长度的列表 ind_vectors, ind_sp_num_list, ind_fixed_seq_len_list = read_dl_vec4seq(args.fixed_len, ind_out_files, return_sp=True) # 生成标签数组 ind_label_array = gen_label_array(ind_sp_num_list, args.label) # 为独立测试构建深度学习分类器 seq_dip(args.ml, vectors, label_array, fixed_seq_len_list, ind_vectors, ind_label_array, ind_fixed_seq_len_list, args.fixed_len, args.results_dir, params_dict) print('########################## Independent Test Finish ##########################\n')
def ind_dl_fe_process(args, vectors, labels, fixed_seq_len_list, params_dict): print('########################## Independent Test Begin ##########################\n') # 合并独立测试集序列文件 ind_input_one_file = create_all_seq_file(args.ind_seq_file, args.results_dir) # 统计独立测试集样本数目和序列长度 ind_sp_num_list, ind_seq_len_list = seq_file2one(args.category, args.ind_seq_file, args.label, ind_input_one_file) # 生成独立测试集标签数组 ind_label_array = gen_label_array(ind_sp_num_list, args.label) # 生成独立测试集特征向量文件名 ind_out_files = out_dl_seq_file(args.label, args.results_dir, ind=True) # 特征提取 one_seq_fe_process(args, ind_input_one_file, ind_label_array, ind_out_files, ind_sp_num_list, True, **params_dict) # 获取独立测试集特征向量 ind_vectors, ind_fixed_seq_len_list = read_dl_vec4seq(args.fixed_len, ind_out_files, return_sp=False) # 为独立测试构建深度学习分类器 dl_ind_process(args.ml, vectors, labels, fixed_seq_len_list, ind_vectors, ind_label_array, ind_fixed_seq_len_list, args.fixed_len, args.results_dir, params_dict) print('########################## Independent Test Finish ##########################\n')
def dl_fe_process(args): # 合并序列文件 input_one_file = create_all_seq_file(args.seq_file, args.results_dir) # 统计样本数目和序列长度 sp_num_list, seq_len_list = seq_file2one(args.category, args.seq_file, args.label, input_one_file) # 生成标签数组 label_array = gen_label_array(sp_num_list, args.label) # 控制序列的固定长度(仅仅需要在基准数据集上操作一次) args.fixed_len = fixed_len_control(seq_len_list, args.fixed_len) all_params_list_dict = {} all_params_list_dict = dl_params_check(args, all_params_list_dict) # 对每个mode的words和method的参数进行检查 # params_list_dict 为只包括特征提取的参数的字典, all_params_list_dict为包含所有参数的字典 params_list_dict, all_params_list_dict = mode_params_check(args, all_params_list_dict) # 列表字典 ---> 字典列表 --> 参数字典 params_dict = make_params_dicts(all_params_list_dict)[0] # 特征向量文件命名 out_files = out_dl_seq_file(args.label, args.results_dir, ind=False) # 深度特征向量提取 one_seq_fe_process(args, input_one_file, label_array, out_files, sp_num_list, False, **params_dict) # 获取深度特征向量 # fixed_seq_len_list: 最大序列长度为fixed_len的序列长度的列表 vectors, fixed_seq_len_list = read_dl_vec4seq(args.fixed_len, out_files, return_sp=False) # 深度学习的独立测试和交叉验证分开 if args.ind_seq_file is None: # 在参数便利前进行一系列准备工作: 1. 固定划分;2.设定指标;3.指定任务类型 args = prepare4train_seq(args, label_array, dl=True) # 构建深度学习分类器 dl_cv_process(args.ml, vectors, label_array, fixed_seq_len_list, args.fixed_len, args.folds, args.results_dir, params_dict) else: # 独立验证开始 ind_dl_fe_process(args, vectors, label_array, fixed_seq_len_list, params_dict)
def ml_fe_process(args): # 合并序列文件 input_one_file = create_all_seq_file(args.seq_file, args.results_dir) # 统计样本数目和序列长度 sp_num_list, seq_len_list = seq_file2one(args.category, args.seq_file, args.label, input_one_file) # 生成标签数组 label_array = gen_label_array(sp_num_list, args.label) # 控制序列的固定长度(只需要操作一次) args.fixed_len = fixed_len_control(seq_len_list, args.fixed_len) # 多进程计算 pool = multiprocessing.Pool(args.cpu) # 对每个mode的method进行检查 seq_feature_check(args) # 对SVM或RF的参数进行检查并生成参数字典集合 all_params_list_dict = {} all_params_list_dict = ml_params_check(args, all_params_list_dict) # 对每个mode的words和method的参数进行检查 # params_list_dict 为只包括特征提取的参数的字典, all_params_list_dict为包含所有参数的字典 params_list_dict, all_params_list_dict = mode_params_check(args, all_params_list_dict) # 列表字典 ---> 字典列表 params_dict_list = make_params_dicts(all_params_list_dict) # print(params_dict_list) # exit() # 在参数便利前进行一系列准备工作: 1. 固定划分;2.设定指标;3.指定任务类型 args = prepare4train_seq(args, label_array, dl=False) # 指定分析层面 args.res = False params_dict_list_pro = [] print('\n') print('Parameter Selection Processing...') print('\n') for i in range(len(params_dict_list)): params_dict = params_dict_list[i] # 生成特征向量文件名 vec_files = out_seq_file(args.label, args.format, args.results_dir, params_dict, params_list_dict) params_dict['out_files'] = vec_files # 注意多进程计算的debug # one_ml_fe_process(args, input_one_file, label_array, vec_files, sp_num_list, args.folds, **params_dict) params_dict_list_pro.append(pool.apply_async(one_ml_fe_process, (args, input_one_file, label_array, vec_files, sp_num_list, args.folds, params_dict))) pool.close() pool.join() # exit() # 根据指标进行参数选择 params_selected = params_select(params_dict_list_pro, args.results_dir) # 将最优的特征向量文件从"all_fea_files/"文件夹下复制到主文件下 opt_files = opt_file_copy(params_selected['out_files'], args.results_dir) # 获取最优特征向量 opt_vectors = files2vectors_seq(opt_files, args.format) print(' Shape of Optimal Feature vectors: [%d, %d] '.center(66, '*') % (opt_vectors.shape[0], opt_vectors.shape[1])) # 特征分析 if args.score == 'none': opt_vectors = fa_process(args, opt_vectors, label_array, after_ps=True, ind=False) print(' Shape of Optimal Feature vectors after FA process: [%d, %d] '.center(66, '*') % (opt_vectors.shape[0], opt_vectors.shape[1])) # 构建分类器 ml_results(args, opt_vectors, label_array, args.folds, params_selected['out_files'], params_selected) # -------- 独立测试-------- # # 即,将独立测试数据集在最优的model上进行测试 if args.ind_seq_file is not None: ind_ml_fe_process(args, opt_vectors, label_array, params_selected)