def test_weak_classifier_adjective(test_feature_objects, adjective, classifiers, scalers, file_ptr, comma_file_ptr): ''' takes in a dictionary of all features returns a dictionary of weak classifiers for each feature ''' # specify feature to be extracted feature_name_list = [ "pdc_rise_count", "pdc_area", "pdc_max", "pac_energy", "pac_sc", "pac_sv", "pac_ss", "pac_sk", "tac_area", "tdc_exp_fit", "gripper_min", "gripper_mean", "transform_distance", "electrode_polyfit" ] motion_scores = dict() # for each motion (slide, squeeze, etc.) for motion in test_feature_objects: motion_test_set = test_feature_objects[motion] # pull out the features specified as a vector test_feature_vector, test_label_dict = utilities.feature_obj_2_feature_vector( motion_test_set, feature_name_list) # create scaler test_feature_vector_scaled = scalers[motion].transform( test_feature_vector) clf = classifiers[motion] results = clf.predict(test_feature_vector_scaled) print classification_report(test_label_dict[1][adjective], results) file_ptr.write('Adjective: ' + adjective + ' Motion name: ' + motion) file_ptr.write( '\n' + classification_report(test_label_dict[1][adjective], results) + '\n\n') motion_scores[motion] = f1_score(test_label_dict[1][adjective], results) # Pull out the best motion best_motion, best_score = utilities.get_best_motion(motion_scores) # Write results to comma split file to load comma_file_ptr.write(adjective + ',' + str(motion_scores['tap']) + ',' + str(motion_scores['squeeze']) + ',' + str(motion_scores['thermal_hold']) + ',' + str(motion_scores['slide']) + ',' + str(motion_scores['slide_fast']) + ',' + best_motion + '\n')
def test_adj_motion_classifier(test_feature_objects, adjective, classifiers, scalers, feature_name_list, file_ptr, comma_file_ptr): ''' takes in a dictionary of all features returns a dictionary of weak classifiers for each feature ''' motion_scores = dict() # for each motion (slide, squeeze, etc.) for motion in test_feature_objects: motion_test_set = test_feature_objects[motion] # pull out the features specified as a vector test_feature_vector, test_label_dict = utilities.feature_obj_2_feature_vector( motion_test_set, feature_name_list) # create scaler test_feature_vector_scaled = scalers[motion].transform( test_feature_vector) clf = classifiers[motion] results = clf.predict(test_feature_vector_scaled) print classification_report(test_label_dict[1][adjective], results) file_ptr.write('Adjective: ' + adjective + ' Motion name: ' + motion) file_ptr.write( '\n' + classification_report(test_label_dict[1][adjective], results) + '\n\n') motion_scores[motion] = f1_score(test_label_dict[1][adjective], results) # Pull out the best motion best_motion, best_score = utilities.get_best_motion(motion_scores) # Write results to comma split file to load comma_file_ptr.write(adjective + ',' + str(motion_scores['tap']) + ',' + str(motion_scores['squeeze']) + ',' + str(motion_scores['thermal_hold']) + ',' + str(motion_scores['slide']) + ',' + str(motion_scores['slide_fast']) + ',' + best_motion + '\n')
def test_weak_classifier_adjective(test_feature_objects, adjective, classifiers, scalers, file_ptr, comma_file_ptr): ''' takes in a dictionary of all features returns a dictionary of weak classifiers for each feature ''' # specify feature to be extracted feature_name_list = ["pdc_rise_count", "pdc_area", "pdc_max", "pac_energy", "pac_sc", "pac_sv", "pac_ss", "pac_sk", "tac_area", "tdc_exp_fit", "gripper_min", "gripper_mean", "transform_distance", "electrode_polyfit"] motion_scores = dict() # for each motion (slide, squeeze, etc.) for motion in test_feature_objects: motion_test_set = test_feature_objects[motion] # pull out the features specified as a vector test_feature_vector, test_label_dict = utilities.feature_obj_2_feature_vector(motion_test_set, feature_name_list) # create scaler test_feature_vector_scaled = scalers[motion].transform(test_feature_vector) clf = classifiers[motion] results = clf.predict(test_feature_vector_scaled) print classification_report(test_label_dict[1][adjective], results) file_ptr.write('Adjective: ' + adjective + ' Motion name: '+motion) file_ptr.write('\n'+classification_report(test_label_dict[1][adjective], results)+ '\n\n') motion_scores[motion] = f1_score(test_label_dict[1][adjective], results) # Pull out the best motion best_motion, best_score = utilities.get_best_motion(motion_scores) # Write results to comma split file to load comma_file_ptr.write(adjective+','+str(motion_scores['tap'])+',' +str(motion_scores['squeeze'])+','+str(motion_scores['thermal_hold'])+','+str(motion_scores['slide'])+','+str(motion_scores['slide_fast'])+','+best_motion +'\n')
def test_adj_motion_classifier(test_feature_objects, adjective, classifiers, scalers, feature_name_list, file_ptr, comma_file_ptr): ''' takes in a dictionary of all features returns a dictionary of weak classifiers for each feature ''' motion_scores = dict() # for each motion (slide, squeeze, etc.) for motion in test_feature_objects: motion_test_set = test_feature_objects[motion] # pull out the features specified as a vector test_feature_vector, test_label_dict = utilities.feature_obj_2_feature_vector(motion_test_set, feature_name_list) # create scaler test_feature_vector_scaled = scalers[motion].transform(test_feature_vector) clf = classifiers[motion] results = clf.predict(test_feature_vector_scaled) print classification_report(test_label_dict[1][adjective], results) file_ptr.write('Adjective: ' + adjective + ' Motion name: '+motion) file_ptr.write('\n'+classification_report(test_label_dict[1][adjective], results)+ '\n\n') motion_scores[motion] = f1_score(test_label_dict[1][adjective], results) # Pull out the best motion best_motion, best_score = utilities.get_best_motion(motion_scores) # Write results to comma split file to load comma_file_ptr.write(adjective+','+str(motion_scores['tap'])+',' +str(motion_scores['squeeze'])+','+str(motion_scores['thermal_hold'])+','+str(motion_scores['slide'])+','+str(motion_scores['slide_fast'])+','+best_motion +'\n')
def main(classifiers_pkl, all_classifiers_pkl, test_feature_pkl, ensemble_test_feature_pkl, scaler_pkl, final_classifier_pkl): # Load data into pipeline print "Loading data from file" file_ptr = open(scaler_pkl) scaler_dict = cPickle.load(file_ptr) file_ptr = open(test_feature_pkl) test_feature_dict = cPickle.load(file_ptr) file_ptr = open(ensemble_test_feature_pkl) ensemble_feature_dict = cPickle.load(file_ptr) classifier_text_report = open("all_classifier_report_svm.txt", "w") final_classification_text_report = open("final_classifier_report_svm.txt", "w") feature_name_list = ["pdc_rise_count", "pdc_area", "pdc_max", "pac_energy", "pac_sc", "pac_sv", "pac_ss", "pac_sk", "tac_area", "tdc_exp_fit", "gripper_min", "gripper_mean", "transform_distance", "electrode_polyfit"] if final_classifier_pkl != None: file_ptr = open(final_classifier_pkl) final_classifiers_dict = cPickle.load(file_ptr) if all_classifiers_pkl == None: file_ptr = open(classifiers_pkl) classifiers_dict = cPickle.load(file_ptr) # Parse the adjective classifier name being tested adjective_name = classifiers_pkl.split('/')[-1].split('_')[0] test_adj_motion_classifier(classifiers_dict, adjective_name, test_feature_dict, feature_name_list, scaler_dict) else: file_ptr = open(all_classifiers_pkl) classifiers_dict = cPickle.load(file_ptr) # Get the name of the classifier #classifier_name = all_classifiers_pkl.split('_')[1] classifier_name = "svm" # Open up text file to store best report_best_classifier = open("best_classifier_report_" + classifier_name+ ".txt", "w") # Store the best classifier best_classifiers = dict() report_best_classifier.write('Adjective'+','+'tap'+','+'squeeze'+','+'thermal_hold'+','+'slide'','+'slide_fast,'+'best_motion'+'\n') results_prediction = dict() # Go through all of the adjective classifiers for adj in classifiers_dict: # Test the adjective scores probability_vector, motion_scores, prediction, truth_vector = test_adj_motion_classifier(classifiers_dict[adj], adj, test_feature_dict, feature_name_list, scaler_dict, classifier_text_report) # Compute the best scores best_motion, best_score = utilities.get_best_motion(motion_scores) #report_best_classifier.write('Adjective: '+adj) #report_best_classifier.write('\nMotion Scores:\n' + str(motion_scores)) #report_best_classifier.write('\n\nBest Motion is: ' +best_motion+ "\n\n") report_best_classifier.write(adj+','+str(motion_scores['tap'])+','+str(motion_scores['squeeze'])+','+str(motion_scores['thermal_hold'])+','+str(motion_scores['slide'])+','+str(motion_scores['slide_fast'])+','+best_motion+'\n') results_prediction[adj] = prediction[best_motion] best_classifiers[adj] = (classifiers_dict[adj][best_motion], best_motion) # Test the final paper #import pdb; pdb.set_trace() #final_results = final_classifiers_dict[adj].predict(probability_vector) #final_score = f1_score(truth_vector,final_results) #print final_score classifier_text_report.close() # Store the pickle file cPickle.dump(best_classifiers, open("best_classifiers_"+classifier_name+".pkl", "w")) report_best_classifier.close()
def main(classifiers_pkl, all_classifiers_pkl, test_feature_pkl, ensemble_test_feature_pkl, scaler_pkl, final_classifier_pkl): # Load data into pipeline print "Loading data from file" file_ptr = open(scaler_pkl) scaler_dict = cPickle.load(file_ptr) file_ptr = open(test_feature_pkl) test_feature_dict = cPickle.load(file_ptr) file_ptr = open(ensemble_test_feature_pkl) ensemble_feature_dict = cPickle.load(file_ptr) classifier_text_report = open("all_classifier_report_svm.txt", "w") final_classification_text_report = open("final_classifier_report_svm.txt", "w") feature_name_list = [ "pdc_rise_count", "pdc_area", "pdc_max", "pac_energy", "pac_sc", "pac_sv", "pac_ss", "pac_sk", "tac_area", "tdc_exp_fit", "gripper_min", "gripper_mean", "transform_distance", "electrode_polyfit" ] if final_classifier_pkl != None: file_ptr = open(final_classifier_pkl) final_classifiers_dict = cPickle.load(file_ptr) if all_classifiers_pkl == None: file_ptr = open(classifiers_pkl) classifiers_dict = cPickle.load(file_ptr) # Parse the adjective classifier name being tested adjective_name = classifiers_pkl.split('/')[-1].split('_')[0] test_adj_motion_classifier(classifiers_dict, adjective_name, test_feature_dict, feature_name_list, scaler_dict) else: file_ptr = open(all_classifiers_pkl) classifiers_dict = cPickle.load(file_ptr) # Get the name of the classifier #classifier_name = all_classifiers_pkl.split('_')[1] classifier_name = "svm" # Open up text file to store best report_best_classifier = open( "best_classifier_report_" + classifier_name + ".txt", "w") # Store the best classifier best_classifiers = dict() report_best_classifier.write('Adjective' + ',' + 'tap' + ',' + 'squeeze' + ',' + 'thermal_hold' + ',' + 'slide' ',' + 'slide_fast,' + 'best_motion' + '\n') results_prediction = dict() # Go through all of the adjective classifiers for adj in classifiers_dict: # Test the adjective scores probability_vector, motion_scores, prediction, truth_vector = test_adj_motion_classifier( classifiers_dict[adj], adj, test_feature_dict, feature_name_list, scaler_dict, classifier_text_report) # Compute the best scores best_motion, best_score = utilities.get_best_motion(motion_scores) #report_best_classifier.write('Adjective: '+adj) #report_best_classifier.write('\nMotion Scores:\n' + str(motion_scores)) #report_best_classifier.write('\n\nBest Motion is: ' +best_motion+ "\n\n") report_best_classifier.write(adj + ',' + str(motion_scores['tap']) + ',' + str(motion_scores['squeeze']) + ',' + str(motion_scores['thermal_hold']) + ',' + str(motion_scores['slide']) + ',' + str(motion_scores['slide_fast']) + ',' + best_motion + '\n') results_prediction[adj] = prediction[best_motion] best_classifiers[adj] = (classifiers_dict[adj][best_motion], best_motion) # Test the final paper #import pdb; pdb.set_trace() #final_results = final_classifiers_dict[adj].predict(probability_vector) #final_score = f1_score(truth_vector,final_results) #print final_score classifier_text_report.close() # Store the pickle file cPickle.dump(best_classifiers, open("best_classifiers_" + classifier_name + ".pkl", "w")) report_best_classifier.close()