fol_train_values_d = {} fol_values_d = {} print >> sys.stderr, 'load second pass blob outlines; calculate object properties', for i in range(nreps): offset = (i * seglen * step) + start_offset source_f = mousezopt.generate_outfile_names(vid, seglen, offset, nframes, peak_max, peak_min, target_int_step)[1] ols_d = eval(open(source_f).read()) ols = ([ v for k, v in ols_d.items() if numpy.abs(cutoff - k) < target_int_step / 2 ] + [[]])[0] objs_train = [ o for o in vidtools.find_objs(ols, SHAPE, n_obj, n_obj)[0] if len(o) > 0 ] obj_train_arcs_d[offset] = objs_train size_train_values_d[offset] = [[ vidtools.size_of_polygon(ols[i][j]) for i, j in o ] for o in objs_train] fol_train_values_d[offset] = [ vidtools.fol_from_obj_arc(o, ols, SHAPE) for o in objs_train ] objs = [o for o in vidtools.find_objs(ols, SHAPE)[0] if len(o) > 0] obj_arcs_d[offset] = objs size_values_d[offset] = [ [vidtools.size_of_polygon(ols[i][j]) for i, j in o] for o in objs ] fol_values_d[offset] = [
def score_segment_by_cutoff(mms,maxint,minint,stepint,maxblobs = 20,size_binw=50,fol_binw=0.05): import time ols_by_cut = {} scores = {} times = [] for cutoff in numpy.arange(maxint,minint,-stepint): print >> sys.stderr, '\nCUTOFF',cutoff ols_this = [] t1 = time.time() stop_seg = False for mm in mms: ols_this.append(vidtools.chain_outlines_from_mask(mm>cutoff, \ preshrink=1,debug=False,return_termini=False, \ order_points=False,sort_outlines=False)) if len(ols_this[-1]) > maxblobs: print >> sys.stderr, 'segmentation result (%s) exceeds maximum number of blobs per frame (%s); break' \ % (max([len(ol) for ol in ols_this]),maxblobs) stop_seg = True break t2 = time.time() print >> sys.stderr, 'find blobs %s sec' % (t2-t1) if stop_seg: scores[cutoff] = 0 break ols_by_cut[cutoff] = ols_this if fol_binw is None or size_binw is None: scores[cutoff] = 0 times.append(t2-t1) continue nblobs = len(reduce(lambda x,y:x+y,[[]]+[[vidtools.size_of_polygon(p) for p in ol if len(p) > 0] for ol in ols_this if len(ol) > 0])) if nblobs == 0: print >> sys.stderr, 'no blobs; skip' scores[cutoff] = 0 continue t3 = time.time() objs_this,splits_this,joins_this = vidtools.find_objs(ols_by_cut[cutoff],mm.shape) t4 = time.time() print >> sys.stderr, 'track objects %s sec' % (t4-t3) t5 = time.time() size_h,size_bins,fol_h,fol_bins = vidtools.get_object_arc_param_dists(ols_by_cut[cutoff],mm.shape,n_obj,size_binw=size_binw,fol_binw=fol_binw) t6 = time.time() if size_h is None: print >> sys.stderr, 'no object arcs; skip' scores[cutoff] = 0 continue print >> sys.stderr, 'calculate scoring distributions %s sec' % (t6-t5) keep,drop = vidtools.greedy_objs_filter(objs_this,ols_by_cut[cutoff],size_h,size_bins,fol_h,fol_bins,mm.shape) sscore = sum([vidtools.score_object_arc_size(o,ols_by_cut[cutoff],size_h,size_bins) for o in keep]) fscore = sum([vidtools.score_object_arc_fol(o,ols_by_cut[cutoff],fol_h,fol_bins,mm.shape) for o in keep]) t7 = time.time() print >> sys.stderr, cutoff, nblobs, sscore, fscore, sscore+fscore scores[cutoff] = sscore+fscore times.append(t7-t1) print >> sys.stderr, "total time %s minutes" % (sum(times)/60.0) return scores,ols_by_cut
def score_segment_by_cutoff(mms, maxint, minint, stepint, maxblobs=20, size_binw=50, fol_binw=0.05): import time ols_by_cut = {} scores = {} times = [] for cutoff in numpy.arange(maxint, minint, -stepint): print >> sys.stderr, '\nCUTOFF', cutoff ols_this = [] t1 = time.time() stop_seg = False for mm in mms: ols_this.append(vidtools.chain_outlines_from_mask(mm>cutoff, \ preshrink=1,debug=False,return_termini=False, \ order_points=False,sort_outlines=False)) if len(ols_this[-1]) > maxblobs: print >> sys.stderr, 'segmentation result (%s) exceeds maximum number of blobs per frame (%s); break' \ % (max([len(ol) for ol in ols_this]),maxblobs) stop_seg = True break t2 = time.time() print >> sys.stderr, 'find blobs %s sec' % (t2 - t1) if stop_seg: scores[cutoff] = 0 break ols_by_cut[cutoff] = ols_this if fol_binw is None or size_binw is None: scores[cutoff] = 0 times.append(t2 - t1) continue nblobs = len( reduce(lambda x, y: x + y, [[]] + [[vidtools.size_of_polygon(p) for p in ol if len(p) > 0] for ol in ols_this if len(ol) > 0])) if nblobs == 0: print >> sys.stderr, 'no blobs; skip' scores[cutoff] = 0 continue t3 = time.time() objs_this, splits_this, joins_this = vidtools.find_objs( ols_by_cut[cutoff], mm.shape) t4 = time.time() print >> sys.stderr, 'track objects %s sec' % (t4 - t3) t5 = time.time() size_h, size_bins, fol_h, fol_bins = vidtools.get_object_arc_param_dists( ols_by_cut[cutoff], mm.shape, n_obj, size_binw=size_binw, fol_binw=fol_binw) t6 = time.time() if size_h is None: print >> sys.stderr, 'no object arcs; skip' scores[cutoff] = 0 continue print >> sys.stderr, 'calculate scoring distributions %s sec' % (t6 - t5) keep, drop = vidtools.greedy_objs_filter(objs_this, ols_by_cut[cutoff], size_h, size_bins, fol_h, fol_bins, mm.shape) sscore = sum([ vidtools.score_object_arc_size(o, ols_by_cut[cutoff], size_h, size_bins) for o in keep ]) fscore = sum([ vidtools.score_object_arc_fol(o, ols_by_cut[cutoff], fol_h, fol_bins, mm.shape) for o in keep ]) t7 = time.time() print >> sys.stderr, cutoff, nblobs, sscore, fscore, sscore + fscore scores[cutoff] = sscore + fscore times.append(t7 - t1) print >> sys.stderr, "total time %s minutes" % (sum(times) / 60.0) return scores, ols_by_cut
stream = cv.CaptureFromFile(vid) SHAPE = vidtools.array_from_stream(stream).shape obj_train_arcs_d = {} obj_arcs_d = {} size_train_values_d = {} size_values_d = {} fol_train_values_d = {} fol_values_d = {} print >> sys.stderr, 'load second pass blob outlines; calculate object properties', for i in range(nreps): offset = (i*seglen*step)+start_offset source_f = mousezopt.generate_outfile_names(vid,seglen,offset,nframes,peak_max,peak_min,target_int_step)[1] ols_d = eval(open(source_f).read()) ols = ([v for k,v in ols_d.items() if numpy.abs(cutoff-k) < target_int_step/2]+[[]])[0] objs_train = [o for o in vidtools.find_objs(ols,SHAPE,n_obj,n_obj)[0] if len(o) > 0] obj_train_arcs_d[offset] = objs_train size_train_values_d[offset] = [[vidtools.size_of_polygon(ols[i][j]) for i,j in o] for o in objs_train] fol_train_values_d[offset] = [vidtools.fol_from_obj_arc(o,ols,SHAPE) for o in objs_train] objs = [o for o in vidtools.find_objs(ols,SHAPE)[0] if len(o) > 0] obj_arcs_d[offset] = objs size_values_d[offset] = [[vidtools.size_of_polygon(ols[i][j]) for i,j in o] for o in objs] fol_values_d[offset] = [vidtools.fol_from_obj_arc(o,ols,SHAPE) for o in objs] print >> sys.stderr, '.', print >> sys.stderr, 'done' if len(reduce(lambda x,y:x+y,size_train_values_d.values())) > 0 and len(reduce(lambda x,y:x+y,fol_train_values_d.values())) > 0: print >> sys.stderr, 'calculate scoring distributions ...' , size_h,size_bins,fol_h,fol_bins = vidtools.get_object_arc_param_dists_from_values_dict(size_train_values_d,fol_train_values_d,size_binw=20,fol_binw=0.01) print >> sys.stderr, 'done' print >> sys.stderr, 'score objects ...' ,