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 __init__(self,base_object, coarse_factor=2, coarse_template_threshold = .5, bg = None): self.coarse_factor = coarse_factor if base_object.__class__ is list: # we assume that all objects have the same # length so we are comparing apples to apples template_height = base_object[0].shape[0] template_length = max([t.shape[1] for t in base_object]) self.type = "collection" self.window = np.array((template_height,template_length)) self.template = base_object self.bg = bg self.pad_template = lambda tmplt, bg: np.hstack((tmplt, np.tile( bg.reshape(self.window[0],1), (1,self.window[1]-tmplt.shape[1])))) self.score = lambda E_window,bg:\ max([sum(tt.score_template_background_section( self.pad_template(tmplt,bg), bg,E_window)) for tmplt in self.template]) self.score_register = lambda E_window,bg:\ max([sum(tt.score_template_background_section( _stretch_template(tmplt,E_window.shape[0],E_window.shape[1]), bg,E_window)) for tmplt in self.template]) self.score_no_bg = lambda E_window:\ max([sum(tt.score_template_background_section( self.pad_template(tmplt,self.bg), self.bg, E_window)) for tmplt in self.template]) self.coarse_template = [get_coarse_segment( tmplt, coarse_type="avg", coarse_factor = self.coarse_factor) for tmplt in self.template] self.coarse_length = self.coarse_template[0].shape[1] self.coarse_template_mask = [T >.7 for T in self.coarse_template] self.coarse_score_like = lambda E_window,bg:\ max([sum(tt.score_template_background_section(T, bg, get_coarse_segment(E_window, coarse_type='max', coarse_factor=self.coarse_factor))) for T in self.coarse_template]) self.coarse_score_like_no_bg = lambda E_window:\ max([sum(tt.score_template_background_section(T, self.bg, get_coarse_segment(E_window, coarse_type='max', coarse_factor=self.coarse_factor))) for T in self.coarse_template]) self.coarse_score_count = lambda E_window:\ max([np.sum(get_coarse_segment(E_window, coarse_type='max', coarse_factor=self.coarse_factor)[T_mask]) for T_mask in self.coarse_template_mask]) if base_object.__class__ == np.ndarray: # this means that we just have a template which will be a # 2-d ndarray, our function assumes this template_height, template_length = base_object.shape self.type = "template" self.window = np.array((template_height,template_length)) self.template = base_object self.bg = bg self.score = lambda E_window,bg:\ sum(tt.score_template_background_section(self.template, bg,E_window)) # just create a uniform background with .4 as the edge frequency self.score_register = lambda E_window,bg:\ sum(tt.score_template_background_section( _stretch_template(self.template,E_window.shape[0],E_window.shape[1]), bg,E_window)) self.score_no_bg = lambda E_window:\ sum(tt.score_template_background_section(self.template, self.bg, E_window)) self.coarse_template = get_coarse_segment(self.template, coarse_type="avg", coarse_factor = self.coarse_factor) self.coarse_length = self.coarse_template.shape[1] self.coarse_template_mask = self.coarse_template > .7 self.coarse_score_like = lambda E_window,bg:\ sum(tt.score_template_background_section(self.coarse_template, bg, get_coarse_segment(E_window, coarse_type='max', coarse_factor=self.coarse_factor))) self.coarse_score_like_no_bg = lambda E_window:\ sum(tt.score_template_background_section(self.coarse_template, self.bg, get_coarse_segment(E_window, coarse_type='max', coarse_factor=self.coarse_factor))) self.coarse_score_count = lambda E_window:\ np.sum(get_coarse_segment(E_window, coarse_type='max', coarse_factor=self.coarse_factor)[self.coarse_template_mask]) elif base_object.__class__ == pm.TwoPartModel: template_height, template_length = base_object.bg.shape[0],base_object.length_range[1] if bg is not None: self.bg = bg else: self.bg = .4 * np.ones(template_height) self.type = "TwoPartModel" self.window = np.array((template_height,template_length)) self.template = base_object self._score_sub_no_bg = lambda E_window,t_id:\ sum(tt.score_template_background_section(self.template.def_templates[t_id], self.template.bg,E_window)) self.score_no_bg = lambda E_window:\ max([ self._score_sub_no_bg(E_window,t_id) for t_id in xrange(self.template.def_templates.shape[0])]) # just create a uniform background with .4 as the edge frequency self.coarse_template = get_coarse_segment(self.template.base_template, coarse_type="avg", coarse_factor = self.coarse_factor) self.coarse_length = self.coarse_template.shape[1] self.coarse_template_mask = self.coarse_template > .7 self.coarse_score_like_no_bg = lambda E_window:\ sum(tt.score_template_background_section(self.coarse_template, self.bg, get_coarse_segment(E_window, coarse_type='max', coarse_factor=self.coarse_factor)[:,:self.coarse_length])) self.coarse_score_count = lambda E_window:\ np.sum(get_coarse_segment(E_window, coarse_type='max', coarse_factor=self.coarse_factor)[:,:self.coarse_length][self.coarse_template_mask]) def score_pad(E_window,bg): diff = E_window.shape[1] - self.window[1] if diff >= 0: return max([self.score(E_window[:, loc:loc+self.window[1]],bg) for loc in np.arange(diff+1,dtype=int)]) else: # diff < 0 # padd E_window with background return self.score( np.hstack((E_window, np.tile(bg,(-diff,1)).T)), bg)
def score_def_template(self,hyp_segment,def_template,bgd): P,C = tt.score_template_background_section(def_template,bgd,hyp_segment) return P+C