def get_detection_scores(self,E,template,bgd_length, mean_background,
                          edge_feature_row_breaks,
                          edge_orientations,
                          abst_threshold=-np.inf *np.ones(8),
                          spread_length=3):
     """ gets the detection scores for later processing
     
     """
     template_height,template_length = template.shape
     num_detections = E.shape[1]-template_length+1
     E_background, estimated_background_idx = self._get_E_background(E,num_detections,bgd_length, mean_background,
                                                                     edge_feature_row_breaks,
                                                                     edge_orientations,
                                                                     abst_threshold=abst_threshold,
                                                                     spread_length=spread_length)
     E_stack= self._get_E_stack(E,template.shape,num_detections)
     print "E_stack has shape",E_stack.shape
     # perform thresholding for the stack of E features
     for frame_idx in xrange(E_stack.shape[1]):
         E_segment = E_stack[:,frame_idx].reshape(template_height,template_length)
         esp.threshold_edgemap(E_segment,.30,edge_feature_row_breaks,abst_threshold=abst_threshold)
         esp.spread_edgemap(E_segment,edge_feature_row_breaks,edge_orientations,spread_length=spread_length)
     bg_stack = np.tile(E_background,(template.shape[1],1))
     T_stack = np.tile(template.transpose().reshape(np.prod(template.shape),1),
                       (1,num_detections))
     T_inv = 1 - T_stack
     bg_inv = 1- bg_stack
     C_exp_inv_long = T_inv/bg_inv
     C = np.log(C_exp_inv_long).sum(0)
     return (E_stack*np.log(T_stack/bg_stack /\
                                C_exp_inv_long)).sum(0),\
                                C
 def next(self):
     if self.cur_window_id < self.num_detections-1:
         self.cur_window_id+=1
     else:
         print "Error: End of utterance reached, reset window"
         if self.pattern_times:
             self.compute_roc()                    
         return 
     self.cur_window = self.E[:,self.cur_window_id\
                               :self.cur_window_id+\
                               self.window_length].copy()
     esp.threshold_edgemap(self.cur_window,self.edge_quantile,
                           self.edge_feature_row_breaks,
                           abst_threshold=self.abst_threshold)
     esp.spread_edgemap(self.cur_window,
                        self.edge_feature_row_breaks,
                        self.edge_orientations,
                        spread_length=self.spread_length)
     self.j0_scores[self.cur_window_id] =\
         np.sum(cur_window[self.mask])
     if self.cur_window_id >=2:
         if self.j0_scores[self.cur_window_id-1]>\
                 max(self.j0_scores[self.cur_window_id],
                     self.j0_scores[self.cur_window_id-2]):
             self.j0_maxima.append(cur_window_id-1)
 def get_hyp_segment_bgd(self,E,E_loc,
                         edge_feature_row_breaks,
                         abst_threshold,
                         edge_orientations,
                         spread_length=3,bg_len=26):
     E_segment = E[:,E_loc-self.front_def_radius-bg_len:
                         E_loc-self.front_def_radius]
     esp.threshold_edgemap(E_segment,.30,edge_feature_row_breaks,abst_threshold=abst_threshold)
     esp.spread_edgemap(E_segment,edge_feature_row_breaks,edge_orientations,spread_length=spread_length)
 def get_hyp_segment(self,E,E_loc,
                         edge_feature_row_breaks,
                         abst_threshold,
                         edge_orientations,
                         spread_length=3):
     E_segment = E[:,E_loc:
                         E_loc+self.deformed_max_length].copy()
     esp.threshold_edgemap(E_segment,.30,edge_feature_row_breaks,abst_threshold=abst_threshold)
     esp.spread_edgemap(E_segment,edge_feature_row_breaks,edge_orientations,spread_length=spread_length)
     return E_segment
 def _get_bgd(self,E_bgd_window,
              edge_feature_row_breaks,
              edge_orientations,
              abst_threshold=-np.inf*np.ones(8),
              spread_length=3):
     esp.threshold_edgemap(E_bgd_window,.30,edge_feature_row_breaks,
                           abst_threshold=abst_threshold)
     esp.spread_edgemap(E_bgd_window,edge_feature_row_breaks,edge_orientations,
                        spread_length=spread_length)
     E_bgd_window = np.mean(E_bgd_window,axis=1)
     E_bgd_window = np.maximum(np.minimum(E_bgd_window,.4),.1)
     return E_bgd_window
 def get_detection_scores_slow(self,E,template,bgd_length,mean_background,
                               edge_feature_row_breaks,
                               edge_orientations,
                               abst_threshold=-np.inf *np.ones(8),
                               spread_length=3):
     template_height,template_length = template.shape
     num_detections = E.shape[1]-template_length+1
     E_background, estimated_background_idx = self._get_E_background(E,num_detections,bgd_length, mean_background,
                                                                     edge_feature_row_breaks,
                                                                     edge_orientations,
                                                                     abst_threshold=abst_threshold,
                                                                     spread_length=spread_length)
     Ps = np.zeros(num_detections)
     Cs = np.zeros(num_detections)
     for frame_idx in xrange(num_detections):
         E_segment = E[:,frame_idx:frame_idx+template_length].copy()
         esp.threshold_edgemap(E_segment,.30,edge_feature_row_breaks,abst_threshold=abst_threshold)
         esp.spread_edgemap(E_segment,edge_feature_row_breaks,edge_orientations,spread_length=spread_length)
         Ps[frame_idx],Cs[frame_idx] = tt.score_template_background_section(template,E_background[:,frame_idx],E_segment)
     return Ps,Cs
def get_roc_full(data_iter, classifier, coarse_thresh,
                   allowed_overlap = .2,
            edge_feature_row_breaks= np.array([   0.,   
                                               45.,   
                                               90.,  
                                               138.,  
                                               186.,  
                                               231.,  
                                               276.,  
                                               321.,  
                                               366.]),
            edge_orientations=np.array([[ 1.,  0.],
                                        [-1.,  0.],
                                        [ 0.,  1.],
                                        [ 0., -1.],
                                        [ 1.,  1.],
                                        [-1., -1.],
                                        [ 1., -1.],
                                        [-1.,  1.]]),
            abst_threshold=np.array([.025,.025,.015,.015,
                                      .02,.02,.02,.02]),
            spread_radius=3):
    """
    Find the appropriate threshold for the coarse classifier, this
    should be run on tuning data, and then we can get a level for the
    tradeoff between false positives and false negatives the first
    pair is the roc curve for the count test and the second is for the
    coarse likelihood test

    The way this works is that we find the scores for each window and
    then we rank them and remove overlapping windows that are lower
    rank if the overlap is below a certain percentage

    We are assuming that the classifier has been implemented and initialized properly
    """
    num_frames = 0
    all_positive_counts = []
    all_positive_likes = []
    all_negative_counts = []
    all_negative_likes = []
    data_iter.reset_exp()
    for datum_id in xrange(data_iter.num_data):
        if datum_id % 10 == 0:
            print "working on example", datum_id
        if data_iter.next(compute_pattern_times=True,
                            max_template_length=classifier.window[1]):
            pattern_times = data_iter.pattern_times
            num_detections = data_iter.E.shape[1] - classifier.window[1]
            num_frames += data_iter.E.shape[1]
            scores = -np.inf * np.ones(num_detections)
            for d in xrange(num_detections):
                E_segment = data_iter.E[:,d:d+classifier.window[1]].copy()                
                esp.threshold_edgemap(E_segment,.30,edge_feature_row_breaks,report_level=False,abst_threshold=abst_threshold)
                esp.spread_edgemap(E_segment,edge_feature_row_breaks,edge_orientations,spread_length=3)
                coarse_count_score = classifier.coarse_score_count(E_segment)
                if coarse_count_score > coarse_thresh:
                    scores[d] = classifier.score_no_bg(E_segment)
            # now we get the indices sorted
            score_indices = remove_overlapping_examples(np.argsort(scores),
                                                        classifier.window[1],
                                                        int(allowed_overlap*classifier.coarse_length))
            positive_scores, negative_scores =  get_pos_neg_scores(score_indices,pattern_times,
                                                                     scores)
            positive_likes, negative_likes = get_pos_neg_scores(like_indices,pattern_times,
                                                                coarse_like_scores)
            all_positive_counts.extend(positive_counts)
            all_negative_counts.extend(negative_counts)
            all_positive_likes.extend(positive_likes)
            all_negative_likes.extend(negative_likes)
        else:
            break
    count_roc = get_roc(np.sort(all_positive_counts)[::-1],
                        np.sort(all_negative_counts)[::-1],
                        num_frames)
    like_roc = get_roc(np.sort(all_positive_likes)[::-1], 
                       np.sort(all_negative_likes)[::-1],
                       num_frames)
    return count_roc, like_roc