def compute_probability_vector(self, bolt_obj):
        if bolt_obj.state  == bolt_obj.TAP:
            # Store results as they come in
            self.adjective_vectors = dict() 
            self.all_motion_results = dict()
        
        # Store dictionary of strings
        self.state_string = {bolt_obj.DISABLED:'disabled',
                    bolt_obj.THERMAL_HOLD:'thermal_hold',
                    bolt_obj.SLIDE:'slide',
                    bolt_obj.SQUEEZE:'squeeze',
                    bolt_obj.TAP:'tap',
                    bolt_obj.DONE:'done',
                    bolt_obj.SLIDE_FAST:'slide_fast',
                    bolt_obj.CENTER_GRIPPER:'center_gripper'
                    }   
       
        
        current_motion = self.state_string[bolt_obj.state] 
       
        # Create feature vector 
        self.bolt_object = bolt_obj 
        utilities.normalize_data(self.bolt_object)
        self.bolt_feature_object = extract_features.extract_features(self.bolt_object, self.pca_model[current_motion]) 

        # Create a dictionary to store the results in
        for adj in self.all_classifiers:
            results, prob = utilities.compute_adjective_probability_score(self.all_classifiers[adj], self.bolt_feature_object, self.feature_list, adj, self.scaler_dict)
            
            # Store off adjective probabilities for ensemble 
            if adj in self.adjective_vectors:
                pass 
            else:
                self.adjective_vectors[adj] = list()
           
            self.adjective_vectors[adj].append(prob)
           
            # Store classifier score based on best motion
            best_motion = self.best_motion_dict[adj][1]
            if current_motion  == best_motion:
                rospy.loginfo("Best Motion is: %s" % best_motion)
                self.all_motion_results[adj] = results
        
        print len(self.adjective_vectors[adj])
        if len(self.adjective_vectors[adj]) == 5:
            ensembled_results = dict() 
            print self.adjective_vectors 
            #for adj in self.adjective_vectors: 
            #    ensembled_results[adj] = self.ensemble_classifiers[adj].predict(self.adjective_vectors[adj])

            # Store off the adjectives that returned true
            adjectives_found = []
            for adj in self.all_motion_results:
                if self.all_motion_results[adj] == 1:
                    adjectives_found.append(adj)

            print "Results from max classification"
            print self.all_motion_results
            print str(adjectives_found) 
            self.adjectives_pub.publish(str(adjectives_found))
Exemplo n.º 2
0
    def compute_probability_vector(self, bolt_obj):
        
        if bolt_obj.state  == bolt_obj.TAP:
            # Store results as they come in
            self.adjective_vectors = dict() 
            self.all_motion_results = dict()
        
        # Store dictionary of strings
        self.state_string = {bolt_obj.DISABLED:'disabled',
                    bolt_obj.THERMAL_HOLD:'thermal_hold',
                    bolt_obj.SLIDE:'slide',
                    bolt_obj.SQUEEZE:'squeeze',
                    bolt_obj.TAP:'tap',
                    bolt_obj.DONE:'done',
                    bolt_obj.SLIDE_FAST:'slide_fast',
                    bolt_obj.CENTER_GRIPPER:'center_gripper'
                    }   
       
        # Get the current motion 
        current_motion = self.state_string[bolt_obj.state] 
       
        # Build the feature vector
        self.bolt_object = bolt_obj 
        utilities.normalize_data(self.bolt_object)
       
        if self.bolt_object.state == bolt_obj.DISABLED: 
            return
        else:
            self.bolt_feature_object = extract_features.extract_features(self.bolt_object, self.pca_model[current_motion]) 

        # Create a dictionary to store the results in
        for adj in self.all_classifiers:
            results, prob = utilities.compute_adjective_probability_score(self.all_classifiers[adj], self.bolt_feature_object, self.feature_list, adj, self.scaler_dict)
            
            # Store off adjective probabilities for ensemble 
            if adj in self.adjective_vectors:
                pass 
            else:
                self.adjective_vectors[adj] = list()
           
            self.adjective_vectors[adj].append(prob)
           
            # Store classifier score based on best motion
            best_motion = self.best_motion_dict[adj][1]
            if current_motion  == best_motion:
                rospy.loginfo("Best Motion is: %s"% best_motion)
                self.all_motion_results[adj] = results
        
        print len(self.adjective_vectors[adj])
        if len(self.adjective_vectors[adj]) == 5:
            ensembled_results = dict() 
            
            #print self.adjective_vectors 
            for adj in self.ensemble_classifiers: 
                ensembled_results[adj] = self.ensemble_classifiers[adj].predict(self.adjective_vectors[adj])[0]

            # Store off the adjectives that returned true
            adjectives_found = []
            for adj in self.all_motion_results:
                if self.all_motion_results[adj] == 1:
                    adjectives_found.append(adj)

            # Store off the adjectives that returns true for ensemble
            adjectives_ensemble = []
            for adj in ensembled_results:
                if ensembled_results[adj] == 1:
                    adjectives_ensemble.append(adj)

            publish_string = AdjList()
            publish_string = adjective_found

            print "Results from max classification"
            print self.all_motion_results
            print str(adjectives_found) 
            self.adjectives_pub.publish(str(adjectives_found))

            print "Results from ensemble"
            print ensembled_results 
            print adjectives_ensemble
def load_data(input_filename, output_filename, save_to_file):

    if not input_filename.endswith(".h5"):
        raise Exception("Input file is %s \nPlease pass in a hdf5 data file" % input_filename)

    if save_to_file: 
        if not output_filename.endswith(".pkl"):
            output_filename = output_filename + '.pkl'

    # Load the data from an h5 file
    all_data = tables.openFile(input_filename)

    # Flag to indicate if raw values are stored when normalizing
    discard_raw_flag = True 

    # Create a storage container for data
    tap_runs = list()
    squeeze_runs = list()
    hold_runs = list() 
    slow_slide_runs = list()
    fast_slide_runs = list()

    # Create dictonary to store the final lists
    segmented_data = dict()

    # Keep counter of the number of runs done
    num_runs = 0

    # Pull pointers to only the file heads of the data structure
    all_runs_root = [_g for _g in all_data.walkGroups("/") if _g._v_depth == 1]

    # For each file extract the segments and data
    for _objectRun in all_runs_root:
        num_runs += 1
        print num_runs
        
        # Pull out tap information
        tap_object = PullDataFromRun(_objectRun, BoltPR2MotionObj.TAP)
        utilities.normalize_data(tap_object, discard_raw_flag)
        tap_runs.append(tap_object)

        # Pull out squeeze information
        squeeze_object = PullDataFromRun(_objectRun, BoltPR2MotionObj.SQUEEZE)
        utilities.normalize_data(squeeze_object, discard_raw_flag)
        squeeze_runs.append(squeeze_object)

        # Pull out hold information
        hold_object = PullDataFromRun(_objectRun, BoltPR2MotionObj.THERMAL_HOLD) 
        utilities.normalize_data(hold_object, discard_raw_flag)
        hold_runs.append(hold_object)

        # Pull out slide fast information
        slide_fast_object = PullDataFromRun(_objectRun, BoltPR2MotionObj.SLIDE_FAST)
        utilities.normalize_data(slide_fast_object, discard_raw_flag)
        fast_slide_runs.append(slide_fast_object)

        # Pull out slide slow information
        slide_slow_object = PullDataFromRun(_objectRun, BoltPR2MotionObj.SLIDE)
        utilities.normalize_data(slide_slow_object, discard_raw_flag)
        slow_slide_runs.append(slide_slow_object)
   

    # Store all of the lists into the dictionary
    segmented_data['tap'] = tap_runs
    segmented_data['squeeze'] = squeeze_runs
    segmented_data['thermal_hold'] = hold_runs
    segmented_data['slide'] = slow_slide_runs
    segmented_data['slide_fast'] = fast_slide_runs

    # if we want to save to file
    if (save_to_file):
        file_ptr = open(output_filename, "w")
        cPickle.dump(segmented_data, file_ptr, cPickle.HIGHEST_PROTOCOL)
        file_ptr.close()

    return segmented_data
def load_data(input_filename, output_filename, save_to_file):

    if not input_filename.endswith(".h5"):
        raise Exception("Input file is %s \nPlease pass in a hdf5 data file" %
                        input_filename)

    if save_to_file:
        if not output_filename.endswith(".pkl"):
            output_filename = output_filename + '.pkl'

    # Load the data from an h5 file
    all_data = tables.openFile(input_filename)

    # Flag to indicate if raw values are stored when normalizing
    discard_raw_flag = True

    # Create a storage container for data
    tap_runs = list()
    squeeze_runs = list()
    hold_runs = list()
    slow_slide_runs = list()
    fast_slide_runs = list()

    # Create dictonary to store the final lists
    segmented_data = dict()

    # Keep counter of the number of runs done
    num_runs = 0

    # Pull pointers to only the file heads of the data structure
    all_runs_root = [_g for _g in all_data.walkGroups("/") if _g._v_depth == 1]

    # For each file extract the segments and data
    for _objectRun in all_runs_root:
        num_runs += 1
        print num_runs

        # Pull out tap information
        tap_object = PullDataFromRun(_objectRun, BoltPR2MotionObj.TAP)
        utilities.normalize_data(tap_object, discard_raw_flag)
        tap_runs.append(tap_object)

        # Pull out squeeze information
        squeeze_object = PullDataFromRun(_objectRun, BoltPR2MotionObj.SQUEEZE)
        utilities.normalize_data(squeeze_object, discard_raw_flag)
        squeeze_runs.append(squeeze_object)

        # Pull out hold information
        hold_object = PullDataFromRun(_objectRun,
                                      BoltPR2MotionObj.THERMAL_HOLD)
        utilities.normalize_data(hold_object, discard_raw_flag)
        hold_runs.append(hold_object)

        # Pull out slide fast information
        slide_fast_object = PullDataFromRun(_objectRun,
                                            BoltPR2MotionObj.SLIDE_FAST)
        utilities.normalize_data(slide_fast_object, discard_raw_flag)
        fast_slide_runs.append(slide_fast_object)

        # Pull out slide slow information
        slide_slow_object = PullDataFromRun(_objectRun, BoltPR2MotionObj.SLIDE)
        utilities.normalize_data(slide_slow_object, discard_raw_flag)
        slow_slide_runs.append(slide_slow_object)

    # Store all of the lists into the dictionary
    segmented_data['tap'] = tap_runs
    segmented_data['squeeze'] = squeeze_runs
    segmented_data['thermal_hold'] = hold_runs
    segmented_data['slide'] = slow_slide_runs
    segmented_data['slide_fast'] = fast_slide_runs

    # if we want to save to file
    if (save_to_file):
        file_ptr = open(output_filename, "w")
        cPickle.dump(segmented_data, file_ptr, cPickle.HIGHEST_PROTOCOL)
        file_ptr.close()

    return segmented_data