def kerasTrainer(classifier_num, X_train_raw, Y_train_raw, X_valid_raw, Y_valid_raw, path): feature_index = GVal.getPARA('feature_index_PARA') X, y, X_valid, y_valid, index_no = dataSetPreparation( feature_index, X_train_raw, Y_train_raw, X_valid_raw, Y_valid_raw) classifier_list = { 11: [sequentialNN, 'Sequential Frame NN'], 12: [sequentialNN, 'Sequential Frame NN'] } clf_cache = { 11: cell2dmatlab_jsp([1], 1, []), 12: cell2dmatlab_jsp([1], 1, []), } print('### With model: [' + classifier_list[classifier_num][1] + ']') print('######## [Predicting ... ] ########') # Loading model to do the classification clf, score, FRAP = classifier_list[int(str(classifier_num)[0:2])][0]( X, y, X_valid, y_valid, index_no, classifier_num) clf_cache[classifier_num] = clf # return clf,score,FRAP return classifier_list[classifier_num][1], score, FRAP
def train_test_constructor(N, mdl, X, Y): X_tr = cell2dmatlab_jsp([N], 1, []) y_tr = cell2dmatlab_jsp([N], 1, []) X_te = cell2dmatlab_jsp([N], 1, []) y_te = cell2dmatlab_jsp([N], 1, []) n_fold = 0 for train, test in mdl.split(X, Y[:, 0]): X_tr[n_fold] = X[train, :] X_te[n_fold] = X[test, :] y_tr[n_fold] = Y[train, :] y_te[n_fold] = Y[test, :] n_fold += 1 return X_tr, X_te, y_tr, y_te
def featurePreparation(select_index, online_fea_selectindex, noise_label_index, active_index, data_file): # Concatenate all the subjects together in one huge matrix. # Comprehensive feature preparation. # Bluring the concept of subjects. Combine all records together. # [TODO] Add the Developing Features dev_fea_signal = [] GVal.initPARA('online_fea_raw_cache', {'name': 'data'}) online_fea_signal = cell2dmatlab_jsp( [len(select_index), len(online_fea_selectindex)], 2, []) noise_label_signal = cell2dmatlab_jsp([len(select_index), 2], 2, []) lplb_count1 = 0 for i in select_index: if i not in active_index: lplb_count1 += 1 continue recordname = data_file[i] # Loading of the online features (Developed) online_fea_file = glob.glob(path['online_fea_path'] + recordname + '*.txt') if recordname in GVal.getPARA('online_fea_raw_cache').keys(): online_fea_raw = GVal.getPARA('online_fea_raw_cache')[recordname] else: online_fea_raw = np.loadtxt(online_fea_file[0]) GVal.getPARA('online_fea_raw_cache')[recordname] = online_fea_raw # Noise label information. (Contain noise and noblink label information) j_lplb_count = 0 for j in noise_label_index: noise_label_signal[lplb_count1][j_lplb_count] = online_fea_raw[:, j] j_lplb_count += 1 # Online Developed Features j_lplb_count = 0 for j in online_fea_selectindex: online_fea_signal[lplb_count1][j_lplb_count] = online_fea_raw[:, j] j_lplb_count += 1 # exit() lplb_count1 += 1 # Save for FINAL EVALUATION # GVal.setPARA('feas_PARA',online_fea_raw[]) GVal.setPARA('online_fea_signal_PARA', online_fea_signal) GVal.setPARA('noise_label_signal_PARA', noise_label_signal) return online_fea_signal, dev_fea_signal, noise_label_signal
def dataSetPreparation(feature_index, X_train_raw, Y_train_raw, X_valid_raw, Y_valid_raw): # Get only the features requested. X = X_train_raw[:, feature_index] # Get only the label itselt. no other information y = Y_train_raw[:, 0] X_valid = X_valid_raw[:, feature_index] y_valid = Y_valid_raw[:, 0] index_no = cell2dmatlab_jsp([2, 4], 2, []) for i in range(4): # Count up each label type 0,1,2,3 index_no[0][i] = np.nonzero(y == i)[0] index_no[1][i] = np.nonzero(y_valid == i)[0] print(('### Training Dataset Size: ' + str(X_train_raw.shape))) print(('### Fatigue Sample Numbers (TrainingSet): ' + '[0]-' + str(len(index_no[0][0])) + ' || [1]-' + str(len(index_no[0][1])) + ' || [2]-' + str(len(index_no[0][2])) + ' || [3]-' + str(len(index_no[0][3])) )) print(('### Validation Dataset Size: ' + str(X_valid_raw.shape))) print(('### Fatigue Sample Numbers (ValidationSet): ' + '[0]-' + str(len(index_no[1][0])) + ' || [1]-' + str(len(index_no[1][1])) + ' || [2]-' + str(len(index_no[1][2])) + ' || [3]-' + str(len(index_no[1][3])) )) return X, y, X_valid, y_valid, index_no
def dataSetPreparation(feature_index, X_train_raw, Y_train_raw, X_valid_raw, Y_valid_raw): X = X_train_raw[:, feature_index] y = Y_train_raw[:, 0] X_valid = X_valid_raw[:, feature_index] y_valid = Y_valid_raw[:, 0] index_no = cell2dmatlab_jsp([2, 4], 2, []) for i in range(4): index_no[0][i] = np.nonzero(y == i)[0] index_no[1][i] = np.nonzero(y_valid == i)[0] print(('### Training Dataset Size: ' + str(X_train_raw.shape))) print(('### Fatigue Sample Numbers (TrainingSet): ' + '[0]-' + str(len(index_no[0][0])) + ' || [1]-' + str(len(index_no[0][1])) + ' || [2]-' + str(len(index_no[0][2])) + ' || [3]-' + str(len(index_no[0][3])))) print(('### Validation Dataset Size: ' + str(X_valid_raw.shape))) print(('### Fatigue Sample Numbers (ValidationSet): ' + '[0]-' + str(len(index_no[1][0])) + ' || [1]-' + str(len(index_no[1][1])) + ' || [2]-' + str(len(index_no[1][2])) + ' || [3]-' + str(len(index_no[1][3])))) return X, y, X_valid, y_valid, index_no
def cFRAPPlotting(res): L_clf = len(GVal.getPARA('classifier_list_PARA')) if L_clf < 2: print( '### Warning! No enough classifiers for cFRAP Plotting, skip it.') return 0 ft_size = 18 path = GVal.getPARA('path_PARA') beta = GVal.getPARA('beta_PARA') y = np.zeros((5, L_clf)) xlbl = cell2dmatlab_jsp([L_clf], 1, []) for i in range(L_clf): xlbl[i] = res[i][1] for j in range(3): y[j][i] = res[i][3][j] for jj in range(5, 7): y[jj - 2][i] = res[i][3][jj] ylbl = [ '[P] Precision', '[A] Accuracy', '[R] Recall', '[F1] score', ['[F' + str(beta) + '] score'] ] colorlist = ['#0203e2', '#6ecb3c', '#fd3c06', '#000000', '#000000'] h = plt.figure(num=1, figsize=(17, 9.3)) ax = plt.gca() port = 0.1 ytick = np.arange(0, 1, 0.1) x = np.arange(1, L_clf + 1, 1) for j in range(5): delt = port * j + 0.01 * j plt.bar(x - 0.3 + delt, y[j], width=port, facecolor=colorlist[j], label=ylbl[j]) plt.legend(loc=2, fontsize=ft_size) ax.set_xticks(x) ax.set_yticks(ytick) ax.set_xticklabels(xlbl, fontproperties=zhfont, fontsize=ft_size) ax.set_yticklabels(ytick, fontsize=ft_size) plt.ylabel('scores', fontsize=ft_size) plt.ylim(0, 1.05) plt.grid() plt.show() plt.savefig((path['fig_path'] + 'cFRAP.png')) print('Picture for ' + str(L_clf) + ' Various Classifers Saved!')
def plainCodePool(nlevel, dims, temp, h, y): LP_nc = GVal.getPARA('loopPARA_namecache_PARA') dV = cell2dmatlab_jsp([len(LP_nc)], 1, []) for n in range(len(LP_nc)): if type(LP_nc[n][0]) == int: dV[n] = dVM[LP_nc[n][0]][2] else: dV[n] = GVal.getDVPARA(LP_nc[n][0]) for i in range(nlevel): for j in range(dims[i]): tempdV = copy.deepcopy(dV) tempdV[i] = h[i][j] y = np.vstack((y, tempdV)) return y
def labelReading(select_index, data_file, path): # Initialize several matrix label_signal = cell2dmatlab_jsp([len(select_index), 2], 2, []) subject_sample_length = np.zeros([len(select_index), 1]) active_index = [] GVal.initPARA('label_raw_cache', {'name': 'data'}) # Iplb LooPLaBel ,for the for loop lplb_count1 = 0 for i in select_index: recordname = data_file[i] print(recordname) # Loading of labels. label_file = glob.glob(path['label_path'] + recordname + '*.txt') # print(path['label_path'] + recordname + '*.txt') if not label_file: # return if no file in local database lplb_count1 += 1 continue active_index.append(lplb_count1) # If the if recordname in GVal.getPARA('label_raw_cache').keys(): label_raw = GVal.getPARA('label_raw_cache')[recordname] else: label_raw = np.loadtxt(label_file[0]) GVal.getPARA('label_raw_cache')[recordname] = label_raw # Save the total frame length of the current recording subject_sample_length[lplb_count1] = len(label_raw) # Label label_signal[lplb_count1][0] = label_raw[:, 1] # Label Confidence label_signal[lplb_count1][1] = label_raw[:, 2] lplb_count1 += 1 active_index += select_index[0] # Save for FINAL EVALUATION GVal.setPARA('label_signal_PARA', label_signal) return active_index, subject_sample_length, label_signal
def labelReading_NFDA(select_index, data_file, path): # Initialize several matrix label_signal = cell2dmatlab_jsp([len(select_index), 2], 2, []) subject_sample_length = np.zeros([len(select_index), 1]) active_index = [] GVal.initPARA('label_raw_cache', {'name': 'data'}) # Iplb LooPLaBel ,for the for loop lplb_count1 = 0 for i in select_index: recordname = data_file[i] print(recordname) # Loading of labels. label_file = glob.glob(path['label_path'] + recordname + '*.txt') if not label_file: lplb_count1 += 1 continue active_index.append(lplb_count1) if recordname in GVal.getPARA('label_raw_cache').keys(): label_raw = GVal.getPARA('label_raw_cache')[recordname] # print('^^^' * 100) else: label_raw = np.loadtxt(label_file[0]) GVal.getPARA('label_raw_cache')[recordname] = label_raw # print('&&&' * 100) # GVal.setPARA('label_raw_cache', {recordname: label_raw}) subject_sample_length[lplb_count1] = len(label_raw) # Label label_signal[lplb_count1][0] = label_raw[:, 1] # Label Confidence label_signal[lplb_count1][1] = label_raw[:, 2] lplb_count1 += 1 active_index += select_index[0] return active_index, subject_sample_length, label_signal
def mainProcesser(process_code): # The decoder translate the process_code into different parameter to be set. select_index, classifier_num = processCodeDecoder(process_code) # Prepare some information online_fea_selectindex = GVal.getPARA('online_fea_selectindex_PARA') noise_label_index = GVal.getPARA('noise_label_index_PARA') #################################### # [TODO] Raw Data Reading methods # FUNCTION rawDataReading() #################################### # [TODO] New preprocessing methods # FUNCTION raw_dev_fea = preProcessing() # Output raw_dev_fea should then become input of the Function featurePreparation() if FLAG['data_prepare_flag'] and GVal.getPARA('firstTimeConstruction'): # Load the label information from local database active_index, subject_sample_length, label_signal = labelReading( select_index, data_file, path) # Load and prepare the feature from local database online_fea_signal, dev_fea_signal, noise_label_signal = featurePreparation( select_index, online_fea_selectindex, noise_label_index, active_index, data_file) # Construct all the information, make a standard format. [label_all] & [online_fea_all] label_all, online_fea_all = dataConstruction( select_index, online_fea_selectindex, subject_sample_length, label_signal, online_fea_signal, noise_label_signal, path, FLAG['save_flag']) GVal.setPARA('firstTimeConstruction', 0) else: print('### Skip the data construction, load from the database') print('### The loading data directory is: ' + path['pdata_path']) label_all = np.loadtxt((path['pdata_path'] + 'label_all.txt')) online_fea_all = np.loadtxt( (path['pdata_path'] + 'online_fea_all.txt')) X_rawraw = np.transpose(online_fea_all) Y_raw = np.transpose(label_all) print('######## [Data Preparation Accomplished! ] ########') print('### Label Raw Matrix Size: ' + str(Y_raw.shape)) print('### Feature Raw Matrix Size: ' + str(X_rawraw.shape)) # Until here, all the developed label and features are prepared in a certain format. # for one single data sample, get the label by: # snum: Sample NUM || fnum: Feature NUM # label_all[0,snum] : label # label_all[1,snum] : confidence # online_fea_all[fnum,snum] : difference features. #################################### # [TODO DONE!] Compare with Y. Do the validation . Try leave-one-out, or K-fold X_train_rawraw, X_validation_raw, y_train_raw, y_validation_raw, N = datasetSeparation( X_rawraw, Y_raw, GVal.getPARA('split_type_num'), GVal.getPARA('split_type_para')) # Sample: X_train[n_fold][fnum,snum] classifier_frame = {3: sklearnTrainer, 2: sklearnTrainer, 1: kerasTrainer} # Loop working when n fold is working. for N_lplb_count in np.arange(N): print(' ') print('#' * 32) print('######## [Fold (#' + str(N_lplb_count + 1) + '/' + str(N) + ') ] ########') # Prepare the F, Y if FLAG['label_process_flag']: # label processor targeting on noconfident frame / noise frame / noblink frame 'N3 frame' # Extendable function strucutre. For any customized label processing tasks. X_train_raw, y_train, X_validation_rawS, y_validation_rawS = labelProcessor( X_train_rawraw[N_lplb_count], y_train_raw[N_lplb_count], X_validation_raw[N_lplb_count], y_validation_raw[N_lplb_count]) else: X_train_raw = copy.deepcopy(X_train_rawraw[N_lplb_count]) y_train = copy.deepcopy(y_train_raw[N_lplb_count]) X_validation_rawS = copy.deepcopy(X_validation_raw[N_lplb_count]) y_validation_rawS = copy.deepcopy(y_validation_raw[N_lplb_count]) # # # Feature Plotting [N/A] # if FLAG['fea_plotting_flag']: # featurePlotting(Y_train, X_train_raw, process_code) # else: # print('### No plotting, exit feature plotting...') #################################### # [TODO IN PROGRESS] Add the feature selection Method if FLAG['fea_selection_flag']: # feaSelection is doing the feature selection only on trainning set, return the fea selection result X_train, feaS_mdl = feaSelection(X_train_raw) # According the fea selection result, preprocess validation set in corresponding to the trainning set. X_validation = feaS_mdl.fit_transform(X_validation_rawS) else: X_train = copy.deepcopy(X_train_raw) X_validation = copy.deepcopy(X_validation_rawS) #################################### # [TODO] Feature Regulation # Three types of features: # 1 -- Continuous # 2 -- Discrete (Categorical) # 3 -- Discrete Binary (0 and 1) y_validation = copy.deepcopy(y_validation_rawS) #################################### # [TODO IN PROGRESS] Data balancing method! # Data balance only affect on training set. # The method is solving the unbalance problem. # The function is extendable, customized data balancing method can be applied. if FLAG['data_balance_flag']: X_train, y_train = dataBalance(X_train, y_train) res_temp = cell2dmatlab_jsp([1, 5], 2, []) # [TODO IN PROGRESS] Classifier Designing res_temp[0][0] = process_code res_temp[0][1:4] = classifier_frame[int(str(classifier_num)[0])]( classifier_num, X_train, y_train, X_validation, y_validation, path) # Sample: res[fold number][infoserial] # infoserial : 1- model object [1: num, 2:name, 3:mdl obejct] 2- Training Score 3- FRAP resCalMethodList = {'median': np.median, 'mean': np.mean} if N == 1: res = res_temp else: if N_lplb_count == 0: res_mid = res_temp elif N_lplb_count < N - 1: res_mid += res_temp elif N_lplb_count == N - 1: res_mid += res_temp res = res_temp for ires in range(len(res[0][3])): temptemp = [] for iN in range(N): # if np.logical_and(res_mid[iN][3][ires] != 0.0, res_mid[iN][3][ires] != 1.0): if res_mid[iN][3][ires] not in [0.0, 1.0, -1.0]: temptemp.append(res_mid[iN][3][ires]) res[0][3][ires] = resCalMethodList[GVal.getPARA( 'resCalMethod_PARA')](temptemp) if FLAG['pairDristributionPlotting_flag']: FRAP = res_temp[0][3] pairDistPlotting(FRAP, process_code) if FLAG['resultDristributionPlotting_flag']: FRAP = res_temp[0][3] resultPlotting(FRAP, process_code) else: print('### No plotting, exit feature plotting...') time.sleep(0.001) return res, N
def mainProcesser(process_code): select_index, classifier_num = processCodeDecoder(process_code, 1) online_fea_selectindex = GVal.getPARA('online_fea_selectindex_PARA') noise_label_index = GVal.getPARA('noise_label_index_PARA') ################################################################################ ############## [ Engine START! ] ################################################### ################################################################################ #################################### # [TODO] Raw Data Reading methods # FUNCTION rawDataReading_NFDA() #################################### # [TODO] New preprocessing methods # FUNCTION raw_dev_fea = preProcessing_NFDA() # Output raw_dev_fea should then become input of the Function featurePreparation_NFDA() if FLAG['data_prepare_flag'] and GVal.getPARA('firstTimeConstruction'): active_index, subject_sample_length, label_signal = labelReading_NFDA(select_index, data_file, path) online_fea_signal, dev_fea_signal, noise_label_signal = featurePreparation_NFDA(select_index, online_fea_selectindex, noise_label_index, active_index, data_file) label_all, online_fea_all = dataConstruction_NFDA(select_index, online_fea_selectindex, subject_sample_length, label_signal, online_fea_signal, noise_label_signal, path, FLAG['save_flag']) GVal.setPARA('firstTimeConstruction', 0) else: print('### Skip the data construction, load from the database') print('### The loading data directory is: ' + path['pdata_path']) label_all = np.loadtxt((path['pdata_path'] + 'label_all.txt')) online_fea_all = np.loadtxt((path['pdata_path'] + 'online_fea_all.txt')) X_rawraw = np.transpose(online_fea_all) Y_raw = np.transpose(label_all) print('######## [Data Preparation Accomplished! ] ########') print('### Label Raw Matrix Size: ' + str(Y_raw.shape)) print('### Feature Raw Matrix Size: ' + str(X_rawraw.shape)) # Until here, all the developed label and features are prepared in a certain format. # for one single data sample, get the label by: # snum: Sample NUM || fnum: Feature NUM # label_all[0,snum] : label # label_all[1,snum] : confidence # online_fea_all[fnum,snum] : difference features. # Feature Plotting [N/A] if FLAG['plotting_flag']: featurePlotting_NFDA(label_all, online_fea_all) else: print('### No plotting, exit feature plotting...') # Prepare the F, Y # X_raw[snum, fnum] # Y[snum,:] if FLAG['label_process_flag']: X_raw, Y = labelProcessor(X_rawraw, Y_raw) else: X_raw = copy.deepcopy(X_rawraw) Y = copy.deepcopy(Y_raw) #################################### # [TODO IN PROGRESS] Add the feature selection Method if FLAG['fea_selection_flag']: X = feaSelection(X_raw) else: X = copy.deepcopy(X_raw) #################################### # [TODO] Feature Regulation # Three types of features: # 1 -- Continuous # 2 -- Discrete (Categorical) # 3 -- Discrete Binary (0 and 1) #################################### # [TODO DONE!] Compare with Y. Do the validation . Try leave-one-out, or K-fold X_train, X_validation, y_train, y_validation, N = datasetSeparation(X, Y, GVal.getPARA('split_type_num'), GVal.getPARA('split_type_para')) # Sample: X_train[n_fold][fnum,snum] classifier_frame = { 3: sklearnTrainer, 2: sklearnTrainer, 1: kerasTrainer } # Loop working when n fold is working. for N_lplb_count in np.arange(N): #################################### # [TODO IN PROGRESS] Data balancing method! # [IMPORTANT!] Data balance should be after the separation and only affect on training set! if FLAG['data_balance_flag']: X_train[N_lplb_count], y_train[N_lplb_count] = dataBalance(X_train[N_lplb_count], y_train[N_lplb_count]) res_temp = cell2dmatlab_jsp([1, 4], 2, []) # [TODO IN PROGRESS] Classifier Designing # Try different classifiers, Get CY print(' ') print('#' * 35) print('######## [Fold (#' + str(N_lplb_count + 1) + '/' + str(N) + ') ] ########') print('#' * 35) res_temp[0][0] = process_code res_temp[0][1:4] = classifier_frame[int(str(classifier_num)[0])](classifier_num, X_train[N_lplb_count], y_train[N_lplb_count], X_validation[N_lplb_count], y_validation[N_lplb_count], path) # Sample: res[fold number][infoserial] # infoserial : 0- model object 1- Training Score 2- FRAP if N_lplb_count == 0: res = res_temp else: res += res_temp time.sleep(0.001) return res, N
def processCodeEncoder(): # processCodeEncoder, get all the parameters that set in the controlPanel(), and encode everything into unique process_code for each unique process loop. process_code_pack = [] # [ Loop Parameters #1] recording_index_list = GVal.getPARA('recording_index_list_PARA') # [ Loop Parameters #2] classifier_list = GVal.getPARA('classifier_list_PARA') # [ Other Loop Parameter] loopPARA_cache = GVal.getLoopPARA_cache() # Get total amount of loop parameters loopPARA_amount = len(loopPARA_cache) GVal.setPARA('loopPARA_amount_PARA', loopPARA_amount) # When there's no LOOP parameter if loopPARA_amount == 0: for recording_index_list_lplb in np.arange(len(recording_index_list)): for classifier_list_lplb in np.arange(len(classifier_list)): code_temp = int(1e0 * classifier_list[classifier_list_lplb] + 1e3 * (recording_index_list_lplb + 1) + 1e5 * 0 + 1e7 * 0 + 1e11 * 1) process_code_pack.append(code_temp) return process_code_pack # initialize the totalsize parameter. (n1*n2*n3...) if GVal.getPARA('codepoolCreator') == 'iter': loopPARA_totalsize = 1 if GVal.getPARA('codepoolCreator') == 'plain': loopPARA_totalsize = 0 # initialize the bitsize for each parameter ([n1, n2 , n3,...]) loopPARA_bitsize = cell2dmatlab_jsp([loopPARA_amount], 1, 0) # cache to save name for each loop parameter loopPARA_namecache = cell2dmatlab_jsp([loopPARA_amount, 1], 2, []) # dVPARA_cache = cell2dmatlab_jsp([loopPARA_amount], 1, 0) # Loop scan the loopPara_cache. j = 0 h = {} for loopPARA_name in loopPARA_cache.keys(): # The inner-classifier parameters are defined with int number. if type(loopPARA_name) == int: if len(loopPARA_cache[loopPARA_name][0]) < 1: loopPARA_cache[loopPARA_name][0] = dVM[loopPARA_name][1] loopPARA_cache[loopPARA_name][1] = len( loopPARA_cache[loopPARA_name][0]) dVPARA_cache[j] = dVM[loopPARA_name][2] else: dVPARA_cache[j] = GVal.getDVPARA(loopPARA_name) # print(type(dVPARA_cache[j])) # print(dVPARA_cache[j]) # print(type(loopPARA_cache[loopPARA_name][0].tolist())) # print(loopPARA_cache[loopPARA_name][0]) # print(type(loopPARA_cache[loopPARA_name][0][0].tolist())) # print(loopPARA_cache[loopPARA_name][0][0]) # When the default value is not in the loop list, added into loop list. if dVPARA_cache[j] not in loopPARA_cache[loopPARA_name][0]: loopPARA_cache[loopPARA_name][0] = np.append( loopPARA_cache[loopPARA_name][0], dVPARA_cache[j]) loopPARA_cache[loopPARA_name][1] += 1 # the totalsize of all loop if GVal.getPARA('codepoolCreator') == 'iter': loopPARA_totalsize *= loopPARA_cache[loopPARA_name][1] if GVal.getPARA('codepoolCreator') == 'plain': loopPARA_totalsize += loopPARA_cache[loopPARA_name][1] # the size of each loop parameter loopPARA_bitsize[j] = int(loopPARA_cache[loopPARA_name][1]) # the name of each loop parameter loopPARA_namecache[j][0] = loopPARA_name # the content of each loop parameter( the true value to be set in each loop) h[j] = loopPARA_cache[loopPARA_name][0] j += 1 # Important processCode Para 1.Convey into decoder GVal.setPARA('loopPARA_namecache_PARA', loopPARA_namecache) loopPARA_codepool = cell2dmatlab_jsp([loopPARA_amount], 1, 0) loopPARA_temppool = cell2dmatlab_jsp([loopPARA_amount], 1, 0) # Create the real code pool (n x 1 array, each line is a process code, n = totalsize) codepoolCreatorList = { 'iter': iterCodePool, 'plain': plainCodePool, } loopPARA_codepool = codepoolCreatorList[GVal.getPARA('codepoolCreator')]( loopPARA_amount, loopPARA_bitsize, loopPARA_temppool, h, loopPARA_codepool) # loopPARA_codepool = iterCodePool(loopPARA_amount, loopPARA_bitsize, loopPARA_temppool, h, loopPARA_codepool) loopPARA_codepool = copy.deepcopy(loopPARA_codepool[1:]) row_dV = loopPARA_codepool.shape[0] colomn_dV = loopPARA_codepool.shape[1] dVPARA_count = cell2dmatlab_jsp([row_dV, colomn_dV + 1], 2, 0) dVPARA_index = cell2dmatlab_jsp([colomn_dV], 1, []) dVPARA_value = cell2dmatlab_jsp([colomn_dV], 1, []) for line_dV in range(row_dV): for col_dV in range(colomn_dV): if str2num(loopPARA_codepool[line_dV] [col_dV]) == dVPARA_cache[col_dV]: dVPARA_count[line_dV][col_dV] = 1 else: dVPARA_count[line_dV][col_dV] = 0 dVPARA_count[line_dV][colomn_dV] = sum( dVPARA_count[line_dV][0:colomn_dV]) if dVPARA_count[line_dV][colomn_dV] == colomn_dV - 1: # We have all-1 '1' dVPARA_index[dVPARA_count[line_dV].index(0)].append(line_dV) dVPARA_value[dVPARA_count[line_dV].index(0)].append( str2num(loopPARA_codepool[line_dV][dVPARA_count[line_dV].index( 0)])) elif dVPARA_count[line_dV][colomn_dV] == colomn_dV: # We have all '1' for i in range(colomn_dV): dVPARA_index[i].append(line_dV) dVPARA_value[i].append(str2num(loopPARA_codepool[line_dV][i])) # dVPARA_index contain the index for single parameter analysis (mostly picturing) GVal.setPARA('dVPARA_index_PARA', dVPARA_index) GVal.setPARA('dVPARA_value_PARA', dVPARA_value) # Example: # loopPARA_codepool[loopPARA_serialnum][loopPARA_num] # Important processCode Para 2. Convey into decoder GVal.setPARA('loopPARA_codepool_PARA', loopPARA_codepool) print('### Loop Para Pool Size: [' + str(loopPARA_totalsize) + ', ' + str(loopPARA_amount) + '] ([Total Loop,Loop Parameter amount])') # Code rule: [seal bit set 1](11)[loopParaSerialCode](10-7)[loopParaAmount](6,5)[indexlistSerialNum](4,3)[ClassifierList](2-0) for recording_index_list_lplb in np.arange(len(recording_index_list)): for classifier_list_lplb in np.arange(len(classifier_list)): for loopPARA_serialnum in range(loopPARA_totalsize): code_temp = int(1e0 * classifier_list[classifier_list_lplb] + 1e3 * (recording_index_list_lplb + 1) + 1e5 * loopPARA_amount + 1e7 * loopPARA_serialnum + 1e11 * 1) process_code_pack.append(code_temp) print(process_code_pack) return process_code_pack
import pickle from toolkitJ import cell2dmatlab_jsp with open('res.pickle', 'rb') as f: res = pickle.load(f) print(res) print(len(res)) # L = len(res) ft_size = 24 xlbl = cell2dmatlab_jsp([L], 1, []) y = np.zeros((6, L)) for i in range(L): xlbl[i] = res[i][1] for j in range(6): y[j][i] = res[i][3][j] xlbl = ['LSVM', 'LDA', 'QDA', 'NB', 'ADAB', 'LRC', 'DT', 'RF'] ylbl = ['P(Precision)', 'A(Accuracy)', 'R(Recall)', 'MA(Missing Alert)', 'FA(False Alert)', 'F1(F1 score)'] x = np.arange(1, 9) h = plt.figure(num=str(j), figsize=(17, 9.3)) ax = plt.gca() port = 0.1 ytick = np.arange(0, 1, 0.2) colorlist = ['blue', 'green', 'yellow', 'yellowgreen', 'purple', 'red'] for j in range(6):
def sklearnTrainer(classifier_num, X_train_raw, Y_train_raw, X_valid_raw, Y_valid_raw, path): feature_index = GVal.getPARA('feature_index_PARA') X, y, X_valid, y_valid, index_no = dataSetPreparation( feature_index, X_train_raw, Y_train_raw, X_valid_raw, Y_valid_raw) classifier_list = { 21: [svmLinear, 'Linear SVM', []], 22: [svmKernel, 'Kernel SVM (Default:rbf)'], 221: [svmKernel, 'Kernel SVM (rbf)'], 222: [svmKernel, 'Kernel SVM (poly)'], 223: [svmKernel, 'Kernel SVM (sigmoid)'], 224: [svmKernel, 'Kernel SVM (precompute)'], 23: [lda, 'LDA'], 24: [qda, 'QDA'], 25: [naiveBayes, 'Naive Bayes (Default: Gaussian)'], 251: [naiveBayes, 'Naive Bayes (Guassian)'], 252: [naiveBayes, 'Naive Bayes (Multinominal)'], 253: [naiveBayes, 'Naive Bayes (Bernoulli)'], # 26: neuralNetwork, 27: [adaboost, 'Adaboost'], 271: [adaboost, 'Adaboost(WC:DecisionTree)'], # 28: [linearRegression, 'Linear Regression'], 29: [sgdClassifier, 'SGD Classifier'], 30: [logiRegression, 'Logistic Regression'], 31: [decisionTree, 'Decision Tree'], 32: [randomForest, 'Random Forest'] } # classifier serial code: [[model], [training score], [predicting rate]] clf_cache = { 21: cell2dmatlab_jsp([1], 1, []), 22: cell2dmatlab_jsp([1], 1, []), 221: cell2dmatlab_jsp([1], 1, []), 222: cell2dmatlab_jsp([1], 1, []), 223: cell2dmatlab_jsp([1], 1, []), 224: cell2dmatlab_jsp([1], 1, []), 23: cell2dmatlab_jsp([1], 1, []), 24: cell2dmatlab_jsp([1], 1, []), 25: cell2dmatlab_jsp([1], 1, []), 251: cell2dmatlab_jsp([1], 1, []), 252: cell2dmatlab_jsp([1], 1, []), 253: cell2dmatlab_jsp([1], 1, []), 27: cell2dmatlab_jsp([1], 1, []), 271: cell2dmatlab_jsp([1], 1, []), 28: cell2dmatlab_jsp([1], 1, []), 29: cell2dmatlab_jsp([1], 1, []), 30: cell2dmatlab_jsp([1], 1, []), 31: cell2dmatlab_jsp([1], 1, []), 32: cell2dmatlab_jsp([1], 1, []) } print('### With model: [' + classifier_list[classifier_num][1] + ']') print('######## [Predicting ... ] ########') # Loading model to do the classification clf, score, FRAP = classifier_list[int(str(classifier_num)[0:2])][0]( X, y, X_valid, y_valid, index_no, classifier_num) clf_cache[classifier_num] = clf # return clf,score,FRAP return classifier_list[classifier_num][1], score, FRAP
def FRAPPlotting(res): if GVal.getPARA('loopPARA_amount_PARA') < 1: print( '### Warning! No enough loop parameters for FRAP Plotting, skip it.' ) return 0 path = GVal.getPARA('path_PARA') beta = GVal.getPARA('beta_PARA') dVM = GVal.getPARA('dVM_PARA') dVPARA_index = GVal.getPARA('dVPARA_index_PARA') dVPARA_value = GVal.getPARA('dVPARA_value_PARA') loopPARA_namecache = GVal.getPARA('loopPARA_namecache_PARA') L_pic = len(dVPARA_index) # FontSize ftsize = 20 # color colorlist = ['#0203e2', '#6ecb3c', '#fd3c06', '#000000', '#929591'] # marker markerlist = ['^', '.', 'v', '*', 'h'] # linestyle linelist = ['-', '-', '-', ':', '-.'] params = { 'axes.labelsize': '15', 'xtick.labelsize': '22', 'ytick.labelsize': '22', 'lines.linewidth': 1, 'legend.fontsize': '15', # 'figure.figsize' : '12, 9' # set figure size } # pylab.rcParams.update(params) for i in range(L_pic): h = plt.figure(num=L_pic, figsize=(20, 9.3)) xdata_rawraw = dVPARA_value[i] if type(xdata_rawraw[0]) == str or type(xdata_rawraw[0]) == numpy.str_: xdata_raw = np.arange(len(xdata_rawraw)) xdata_rawraw_index = np.arange(len(xdata_rawraw)) else: xdata_raw = copy.deepcopy(xdata_rawraw) xdata_rawraw_index = xdata_raw ydata_raw = cell2dmatlab_jsp([7], 1, []) for dV_index in dVPARA_index[i]: for k in range(7): ydata_raw[k] += [res[dV_index][3][k]] xdata, ydata = zipSort(xdata_raw, ydata_raw) plt.plot(xdata, ydata[0], marker=markerlist[0], color=colorlist[0], linestyle=linelist[0], label='[P] Precision') plt.plot(xdata, ydata[1], marker=markerlist[1], color=colorlist[1], linestyle=linelist[1], label='[A] Accuracy') plt.plot(xdata, ydata[2], marker=markerlist[2], color=colorlist[2], linestyle=linelist[2], label='[R] Recall') plt.plot(xdata, ydata[5], marker=markerlist[3], color=colorlist[3], linestyle=linelist[3], label='[F1] score') plt.plot(xdata, ydata[6], marker=markerlist[4], color=colorlist[4], linestyle=linelist[4], label=['[F' + str(beta) + '] score']) legend = plt.legend(loc='best', prop={'size': 18}) # legend.get_title().set_fontsize(fontsize=50) # h.legend(Fontsize=ftsize) if type(loopPARA_namecache[i][0]) == int: xlabeltext = 'Classifier: [ ' + res[0][1][ 1] + ' ] | Parameter: [ ' + dVM[loopPARA_namecache[i] [0]][0] + ' ]' else: xlabeltext = 'Classifier: [ ' + res[0][1][ 1] + ' ] | General Parameter: [ ' + loopPARA_namecache[i][ 0] + ' ]' plt.xticks(xdata_rawraw_index, xdata_rawraw, rotation=0) plt.xlabel(xlabeltext, Fontsize=ftsize) plt.ylabel('FRAP Value', Fontsize=ftsize) plt.title('Recording Envorinment: ' + GVal.getPARA('recordname'), Fontsize=ftsize) plt.ylim(0, 1.05) plt.grid() plt.show() plt.savefig( (path['fig_path'] + 'FRAP_' + str(loopPARA_namecache[i][0]) + '_' + str(GVal.getPARA('process_code_PARA')) + '.png')) print('Picture' + str(i) + 'Saved!') plt.close(h) return 0
def pairDistPlotting(FRAP, processCode): # print(sns.axes_style()) path = GVal.getPARA('path_PARA') X_tra = GVal.getPARA('X_tra_res_PARA') X_val = GVal.getPARA('X_val_res_PARA') y_tra = GVal.getPARA('y_tra_res_PARA') y_val = GVal.getPARA('y_val_res_PARA') Z_tra = GVal.getPARA('Z_tra_res_PARA') Z = GVal.getPARA('Z_res_PARA') # p1_index_tra = np.nonzero(y_tra == 1)[0] # y_tra = np.delete(y_tra, p1_index_tra, axis=0) # X_tra = np.delete(X_tra, p1_index_tra, axis=0) p2_index_tra = np.nonzero(y_tra == 2)[0] y_tra[p2_index_tra] = 3 screen_fea_list = GVal.getPARA('screen_fea_list_PARA') online_fea_name = GVal.getPARA('online_fea_engname_PARA') online_fea_selectindex = GVal.getPARA('online_fea_selectindex_PARA') lplb_count = 0 columndata = cell2dmatlab_jsp([1, len(screen_fea_list) + 1], 2, []) for fea in screen_fea_list: fea_serial = np.nonzero(online_fea_selectindex == fea)[0][0] if lplb_count == 0: pairdata = X_tra[:, fea_serial].reshape(-1, 1) else: pairdata = np.concatenate( (pairdata, X_tra[:, fea_serial].reshape(-1, 1)), axis=1) columndata[0][lplb_count] = online_fea_name[fea_serial][0] lplb_count += 1 matcoef = np.corrcoef(pairdata.transpose()) sio.savemat(path['fig_path'] + 'matcoef.mat', {'matcoef': matcoef}) # print(matcoef) for line in matcoef: print(line) # GVal.setPARA('') pairdata = np.concatenate((pairdata, y_tra.reshape(-1, 1)), axis=1) columndata[0][lplb_count] = 'Label' pairdf = pd.DataFrame(pairdata, index=np.arange(len(y_tra)), columns=columndata) figcode = int(1e13 * 9 + processCode) h = plt.figure(num=figcode, figsize=(20, 9.3)) plt.subplot(211) plt.title('Training set') sns.set(font=zhfont.get_name()) g = sns.PairGrid(pairdf, vars=columndata[0][:-1], hue='Label', size=3) g.map_diag(plt.hist, edgecolor="w", bins=25) g.map_offdiag(plt.scatter, edgecolor="w", s=20) plt.show() plt.savefig( (path['fig_path'] + 'FeaturePairPloting' + str(figcode) + '.png')) print('Picture' + str(figcode) + 'Saved!') plt.close(h) return 0