def get_calibration(fignum = 1, pct_wide = 0.2, pct_high = 0.33, tunneldicts=None): ax = pylab.figure(fignum).axes[0] ymax = round(ax.get_ybound()[1]) xmax = round(ax.get_xbound()[1]) pix_wide = ymax*pct_wide pix_high = ymax*pct_high print >> sys.stderr, 'image is %s x %s; windows will be %s, %s' % (xmax,ymax,pix_wide,pix_high) cm_dists = [] print >> sys.stderr, 'click 0, 1, 10 cm marks' pylab.xlim(0,pix_high) pylab.ylim(ymax,ymax-pix_wide) p0,p1,p10 = pylab.ginput(3,0) cm_dists.append(vidtools.hypotenuse(p0,p1)) cm_dists.append(vidtools.hypotenuse(p0,p10)/10.0) print >> sys.stderr, 'click 0, 1, 10 cm marks' pylab.xlim(xmax-pix_wide,xmax) pylab.ylim(pix_high,0) p0,p1,p10 = pylab.ginput(3,0) cm_dists.append(vidtools.hypotenuse(p0,p1)) cm_dists.append(vidtools.hypotenuse(p0,p10)/10.0) print >> sys.stderr, 'click bolt 1' pylab.xlim(0,pix_wide) pylab.ylim(ymax,ymax-pix_wide) horiz = [pylab.ginput(1,0)[0]] print >> sys.stderr, 'click bolt 2' pylab.xlim(xmax-pix_wide,xmax) pylab.ylim(ymax,ymax-pix_wide) horiz.append(pylab.ginput(1,0)[0]) pylab.xlim(0,xmax) pylab.ylim(ymax,0) if tunneldicts is not None: tunneldicts['cm_pts'] = cm_dists tunneldicts['horiz'] = horiz else: return cm_dists,horiz
def calc_chord_stats(tun,cm_factor=1): h = vidtools.hypotenuse(tun[0],tun[-1]) xdiff = tun[-1][0] - tun[0][0] m,b = vidtools.line_fx_from_pts(tun[0],tun[-1]) xstep = xdiff/h lpoly = [(x,(m*x+b)) for x in numpy.arange(tun[0][0],tun[-1][0],xstep)] on_chord_pt,on_curve_pt = max([(vidtools.hypotenuse(*vidtools.perigee(lpoly,[p])),vidtools.perigee(lpoly,[p])) for p in tun])[1] chord_len = vidtools.hypotenuse(tun[0],tun[-1])/cm_factor chord_pt = vidtools.hypotenuse(tun[0],on_chord_pt)/cm_factor chord_dp = vidtools.hypotenuse(on_chord_pt,on_curve_pt)/cm_factor return chord_len, chord_pt, chord_dp
def nearest_point_on_curve(curve,qpt): lseg_dists = [] p1 = curve[0] for i,p in enumerate(curve[1:]): lseg_dists.append((vidtools.hypotenuse(*nearest_point_on_line(p1,p,qpt)),(i,nearest_point_on_line(p1,p,qpt)[0]))) p1 = p return min(lseg_dists)[-1]
def nearest_point_on_line(lpt1,lpt2,qpt): h = vidtools.hypotenuse(lpt1,lpt2) xdiff = lpt2[0] - lpt1[0] m,b = vidtools.line_fx_from_pts(lpt1,lpt2) xstep = xdiff/h lpoly = [(x,(m*x+b)) for x in numpy.arange(lpt1[0],lpt2[0],xstep)] return vidtools.perigee(lpoly,[qpt])
def tunnel_len(tun,cm_factor=1): p1 = tun[0] tot = 0 for p in tun[1:]: tot += vidtools.hypotenuse(p1,p) p1 = p return tot/cm_factor
def load_mouse_hist2d_segments(tarh,SHAPE,return_sizes=False, max_pix_mult=None,max_obj=None,max_dist_mult=None,svec=None): mm_hists = [] segments = sorted(tarh.getnames()) print >> sys.stderr, 'Load mouse locations for %s segments' % len(segments) lastmax = 0 if svec is None: svec = [] print >> sys.stderr, 'mouse sizes:' for i,olsf in enumerate(segments,1): print >> sys.stderr, '\r %s / %s' % (i,len(segments)), blobslists_list = Util.tar2obj(olsf,tarh) for ols in blobslists_list: if len(ols) == 1: svec.append(vidtools.size_of_polygon(ols[0])) mouse_size = numpy.mean(svec) mouse_radius = math.sqrt(mouse_size/math.pi) print >> sys.stderr, 'mouse locations:' for i,olsf in enumerate(segments,1): print >> sys.stderr, '\r %s / %s (lastmax %s)' % (i,len(segments),lastmax), blobslists_list = Util.tar2obj(olsf,tarh) mh = numpy.zeros(SHAPE,dtype=int) for ols in blobslists_list: if len(ols) == 0: continue if max_obj is not None and len(ols) > max_obj: print >> sys.stderr, 'max number of objects (%s) exceeded: %s' % (max_obj,len(ols)) continue this_mh = numpy.zeros(SHAPE,dtype=int) dist_to_all = [] for poly in ols: this_mh += vidtools.mask_from_outline(poly,SHAPE) if max_dist_mult is not None: for o_poly in ols: dist_to_all.append(vidtools.hypotenuse(*vidtools.apogee(poly,o_poly))) if max_pix_mult is not None: tot_pix = numpy.array(this_mh,dtype=bool).sum() if tot_pix > max_pix_mult*mouse_size: print >> sys.stderr, 'max total pixels of objects (%s) exceeded: %s' % (max_pix_mult*mouse_size,tot_pix) continue if max_dist_mult is not None and max(dist_to_all) > max_dist_mult*mouse_radius: print >> sys.stderr, 'max separation of objects (%s) exceeded: %s' % (max_dist_mult*mouse_radius,max(dist_to_all)) continue mh += this_mh mm_hists.append(mh) lastmax = mm_hists[-1].max() if return_sizes: return mm_hists,svec else: return mm_hists
def get_epm_config(vid,fignum=1,cfg_fn=epm_cfg_fn,close_fig=False): pylab.figure(fignum) pylab.clf() fr = vidtools.extract_keyframe(vid,sec=120) pylab.matshow(fr,fignum=fignum) print >> sys.stderr, 'click 8 arm corners, proceeding clockwise from the top left corner of the top arm' pts = pylab.ginput(8,timeout=0) zones = calc_epm_zones(pts) show_epm_config(pts,zones,fignum) pylab.ylim(fr.shape[0],0) pylab.xlim(0,fr.shape[1]) print >> sys.stderr, 'click to revise points or press enter to accept and write configuration' change_pt = pylab.ginput(1,timeout=0) #change_pt = raw_input('enter the number of an anchor point to change or press enter to save configuration: ') while change_pt: # print >> sys.stderr, 'click the revised position for point %s' % change_pt # pts[int(change_pt)] = pylab.ginput(1,timeout=0)[0] change_pt = change_pt[0] change_idx = min([(vidtools.hypotenuse(change_pt,pt),i) for i,pt in enumerate(pts)])[1] pts[change_idx] = change_pt zones = calc_epm_zones(pts) pylab.clf() pylab.matshow(fr,fignum=fignum) show_epm_config(pts,zones,fignum) pylab.ylim(fr.shape[0],0) pylab.xlim(0,fr.shape[1]) print >> sys.stderr, 'click to revise points or press enter to accept and write configuration' change_pt = pylab.ginput(1,timeout=0) # change_pt = raw_input('enter the number of an anchor point to change or press enter to save configuration: ') cfg = cfg_fn(vid) try: os.makedirs(os.path.dirname(cfg)) except: pass open(cfg,'w').write(zones.__repr__()) if close_fig: pylab.close(fignum)
fcopy = frameav.copy() print >>sys.stderr,'finalize mouse calls' for i,f in enumerate(frames): #print i #get xy coords, outlines of above-cut regions, and mean z-scores in each outline xy,ol,zsc = vidtools.find_mouse(f,fcopy,zcut=mousezcut,outline_zcut=mousezcut/2.0,grow_by=mouse_grow_by,preshrink=mouse_preshrink) if opts['ground_anchors'] is None and opts['burrowz'] is None: pass else: for p in ol: try: vidtools.subtract_outline(p,frameav) except: print >> sys.stderr, 'failure in frame %s' % i size = [len(filter(None,vidtools.shrink_mask(vidtools.mask_from_outline(p,f.shape),mouse_grow_by).flat)) for p in ol] mlen = [max([vidtools.hypotenuse(p1,p2) for p1 in p for p2 in p]) for p in ol] miceli.append( (images[i],xy) ) miceoutli.append( (images[i],ol) ) micez.append( (images[i],zsc) ) micesize.append( (images[i],size) ) micelen.append( (images[i],mlen) ) if i % tick == 0: print >> sys.stderr, 'frame %s done' % i mice = dict(miceli) miceout = dict(miceoutli) micez = dict(micez) micesize = dict(micesize) micelen = dict(micelen) try: os.makedirs(outroot)
def draw_analysis_activity_summary(adir,fig=1,outformat=None,graph_height=0.2,show_mice=False,dawn_min=None,hill_bounds=None,skip_first=10): '''given an analysis directory, generates a figure showing activity and grounds if format is not None, also outputs summary figure to adir if graph_height is not None, plots a color-coded activity graph on bottom <graph_height> portion of fig ''' print >> sys.stderr, 'load:' shape = eval(open(adir+'/shape.tuple').read()) if hill_bounds is None: hill_left = 0 hill_right = shape[1] else: hill_left,hill_right = hill_bounds print >> sys.stderr, '\tframes' frames = [numpy.fromfile(f).reshape(shape) for f in sorted(glob(adir+'/*.frame'))] actoutfs = sorted(glob(adir+'/*.newactout')) acttermfs = sorted(glob(adir+'/*.newactterm')) groundfs = sorted(glob(adir+'/*.ground')) print >> sys.stderr, '\tactouts' actouts = [eval(open(f).read()) for f in actoutfs] print >> sys.stderr, '\tactterms' actterms = [eval(open(f).read()) for f in acttermfs] print >> sys.stderr, '\tgrounds' grounds = [Util.smooth(numpy.fromfile(f,sep='\n'),10) for f in groundfs[:-1]] hill_area = numpy.array([sum(shape[0]-g[hill_left:hill_right]) for g in grounds]) grounds = grounds[1:] #drop first ground after calculating differences spec = iplot.subspectrum(len(actouts)) figobj = pylab.figure(fig,figsize=(9,6)) figobj.clf() print >> sys.stderr, 'render:' pylab.gray() if not show_mice: bkgd_mat = frames[-1]-(frames[0]-frames[-1]) if graph_height is not None: h = frames[0].shape[0] * graph_height bkgd_mat = numpy.array(list(bkgd_mat) + list(numpy.zeros((h,bkgd_mat.shape[1])))) pylab.matshow(bkgd_mat,fignum=fig) else: print >> sys.stderr, 'show mice invoked; loading mice...', mice = [eval(open(f).read()) for f in sorted(glob(adir+'/*.mice'))] xco,yco = Util.dezip(reduce(lambda x,y: x+y,[[v for v in m.values() if v] for m in mice])) print >> sys.stderr, 'done' bkgd_mat = numpy.zeros(shape) #raise NotImplementedError if graph_height is not None: h = frames[0].shape[0] * graph_height bkgd_mat = numpy.array(list(bkgd_mat) + list(numpy.zeros((h,bkgd_mat.shape[1])))) pylab.matshow(bkgd_mat,fignum=fig) m,x,y = numpy.histogram2d(xco,yco,bins=numpy.array(shape[::-1])/5) #the following is nasty: re-center to -0.5 bin width, log transform counts pylab.contourf(x[1:]-(x[1]-x[0])/2,y[1:]-(y[1]-y[0])/2,numpy.log(m.transpose()+1),100) ax = figobj.axes[0] ax.set_xticks([]) ax.set_yticks([]) for i,poly in enumerate(actouts): for p in poly: if p: ax.add_patch(pylab.matplotlib.patches.Polygon(p,fc='none',ec=spec[i])) for i, terms in enumerate(actterms): for t in terms: x,y = Util.dezip(t) pylab.plot(x,y,c=spec[i]) vinf,ainf = adir.split('/analysis/') ax.text(10,20,vinf,fontsize=8,color='w') ax.text(10,35,ainf,fontsize=8,color='w') if graph_height is not None: areas = [sum([len(filter(None,vidtools.mask_from_outline(p,frames[0].shape).flat)) for p in polys]) for polys in actouts] progs = [True and sum([vidtools.hypotenuse(*t) for t in terms]) or 0 for terms in actterms] hill_increase = [] for v in hill_area[1:] - hill_area[:-1]: if 0 < v: hill_increase.append(v) else: hill_increase.append(0) hill_increase = numpy.array(hill_increase) z3 = (3*hill_increase.std()) + hill_increase.mean() #willing to accept that a mouse kicks out at no more than 2x max observed burrow-build max_increase = max(2*max(areas),z3) print >> sys.stderr, 'for max ground change, z3 = %s; 2*max_areas = %s. Choose %s' % (z3,2*max(areas),max_increase) for i,v in enumerate(hill_increase): if v > max_increase: hill_increase[i] = 0 h = frames[0].shape[0] * graph_height base = frames[0].shape[0] + h/2 - 10 scale = frames[0].shape[1]/float(len(areas)) x = numpy.arange(0,frames[0].shape[1],scale)[:len(areas)] if dawn_min: pylab.bar(x[dawn_min],1*h/2,color='w',edgecolor='w',bottom=base) pylab.bar(x[dawn_min],-1*h/2,color='w',edgecolor='w',bottom=base) hours = [] for m in range(len(spec)): if m%60==0: hours.append(1) else: hours.append(0) print len(hours) pylab.bar(x,numpy.array(hours)*h/2,color='w',edgecolor='w',alpha=0.4,linewidth=0,bottom=base) pylab.bar(x,-1*numpy.array(hours)*h/2,color='w',edgecolor='w',alpha=0.4,linewidth=0,bottom=base) pylab.bar(x[skip_first:],Util.normalize(numpy.array(hill_increase[skip_first:]))*h/2,color=spec,edgecolor=spec,bottom=base) pylab.plot(x[skip_first:],base+Util.normalize(numpy.cumsum(numpy.array(hill_increase[skip_first:])))*h/2,'--w') for i,v in zip(x[skip_first:],Util.normalize(numpy.cumsum(numpy.array(hill_increase[skip_first:])))): if v > 0.5: break pylab.plot((i,i),(base,base+(h/2)),'--w',lw=2) pylab.bar(x[skip_first:],-1*Util.normalize(numpy.array(areas[skip_first:]))*h/2,color=spec,edgecolor=spec,bottom=base) pylab.plot(x[skip_first:],base-Util.normalize(numpy.cumsum(numpy.array(areas[skip_first:])))*h/2,'--w') for i,v in zip(x[skip_first:],Util.normalize(numpy.cumsum(numpy.array(areas[skip_first:])))): if v > 0.5: break pylab.plot((i,i),(base,base-(h/2)),'--w',lw=2) pylab.text(5,base+h/2,'%0.1f' % max(hill_increase[skip_first:]),color='w') pylab.text(5,base-h/2,'%0.1f' % max(areas[skip_first:]),color='w') #''' spec.reverse() #flip spectrum to draw grounds back-to-front for i, g in enumerate(grounds[::-1]): pylab.plot(numpy.arange(hill_left,hill_right),g[hill_left:hill_right],c=spec[i]) spec.reverse() #flip back #''' pylab.plot() pylab.ylim(1.2*shape[0],0) pylab.xlim(0,shape[1]) if outformat is not None: print >> sys.stderr, 'save as '+adir+'/activity_summary.'+outformat figobj.savefig(adir+'/activity_summary.'+outformat)
def calc_ori_pt(this_name,tunnels,tunnel_oris,cm_factor=1): i,near_pt = nearest_point_on_curve(tunnels[tunnel_oris[this_name]],tunnels[this_name][0]) ori = (tunnel_len(tunnels[tunnel_oris[this_name]][:i+1]) + vidtools.hypotenuse(tunnels[tunnel_oris[this_name]][i],near_pt)) / cm_factor return ori
def calc_tunnel_H(this_name,tunnels,horiz,cm_factor=1): Hstart = vidtools.hypotenuse(*nearest_point_on_line(horiz[0],horiz[1],tunnels[this_name][0]))/ cm_factor Hend = vidtools.hypotenuse(*nearest_point_on_line(horiz[0],horiz[1],tunnels[this_name][-1]))/ cm_factor return Hstart, Hend
else: for p in ol: try: vidtools.subtract_outline(p, frameav) except: print >> sys.stderr, 'failure in frame %s' % i size = [ len( filter( None, vidtools.shrink_mask( vidtools.mask_from_outline(p, f.shape), mouse_grow_by).flat)) for p in ol ] mlen = [ max([vidtools.hypotenuse(p1, p2) for p1 in p for p2 in p]) for p in ol ] miceli.append((images[i], xy)) miceoutli.append((images[i], ol)) micez.append((images[i], zsc)) micesize.append((images[i], size)) micelen.append((images[i], mlen)) if i % tick == 0: print >> sys.stderr, 'frame %s done' % i mice = dict(miceli) miceout = dict(miceoutli) micez = dict(micez) micesize = dict(micesize) micelen = dict(micelen)