Beispiel #1
0
def hist2d_from_blobs(blobslists_list,SHAPE):
	'''takes list of lists of blob polys (i.e. contents of a single mouseols.tar)

	returns 2-d hist of occupied positions'''
	
	mh = numpy.zeros(SHAPE,int)
	for ols in blobslists_list:
		for poly in ols:
			mh += vidtools.mask_from_outline(poly,SHAPE)
	return mh
Beispiel #2
0
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
	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:
Beispiel #4
0
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)
Beispiel #5
0
def draw_rainbowtron_opencv(analysis_dir,fig=1,outformat='pdf',vid_fps=30,graph_height=0.2, \
			    bkgd_data='fullmice',spectrum_len=None,dawn_min=None,activity_percentile_plot=0.5, \
			    hill_bounds=None,skip_first=0,hsl_per_min=2,set_hill_max=None,set_dig_max=None, \
			    post_summarize_analysis=True,outfile_prefix=None, \
			    ground_step_coeff=1,max_pix_mult=None,max_obj=None,max_dist_mult=None,hill_win_coeff=None,write_yval=False, \
			    hill_max_cumul_digscale=None,dig_max_cumul_digscale=None):
	'''given a summarize_segment_opencv.py analysis directory (i.e. tarballs of analysis results by half-segment-length)

	generates a figure showing burrowing activity and sand-hill accumulation

	if outformat is not None, save figure to analysis_dir
	if vidformat is not None, record rainbowtron over time to analysis_dir

	bkgd_data: data to display as background for image. one of ["fullmice","flatmice","hillchange"]
	if post_summarize_analysis is True, look for ground and activity from  vidtools.find_ground_over_frames_matrix;  vidtools.find_burrow_over_frames_matrix

	HACK: if bkgd_data is NOT "fullmice", the mouse size vector will not be loaded, and a mouse_radius of 10 will be used
	'''

	if outfile_prefix is None:
		outfile_prefix = analysis_dir.rstrip('/').split('/')[-4].split('-')[0] + '-' + ('_'.join(analysis_dir.rstrip('/').split('/')[-3].split('_')[-2:]))
	outfile_prefix = '%s-hillmax%s-digmax%s-gsc%s-mpm%s-mo%s-mdm%s-hwc%s-hmcd%s-dmcd%s-bkgd_%s' % \
			 (outfile_prefix, set_hill_max,set_dig_max, \
			  ground_step_coeff,max_pix_mult,max_obj,max_dist_mult,hill_win_coeff,hill_max_cumul_digscale,dig_max_cumul_digscale, \
			  bkgd_data)
	
	#will get overwritten later if possible
	mouse_radius = 10

	print >> sys.stderr, 'load:'
	SHAPE = eval(open(analysis_dir+'/SHAPE').read())
	config = eval(open(os.path.split(os.path.split(analysis_dir.rstrip('/'))[0])[0]+'-config.dict').read())

	if hill_bounds is None:
		hill_left = 0
		hill_right = SHAPE[1]
	elif hill_bounds == 'config':
		hill_left,hill_right = config['hill_bounds']
	else:
		hill_left,hill_right = hill_bounds

	print >> sys.stderr, '\ttar handles'
	tars_d = dict([(os.path.basename(f),tarfile.open(f)) for f in glob(os.path.join(analysis_dir,'*.tar'))])

	print >> sys.stderr, '\tframes'
	frames = Util.tar2ar_all(tars_d['segavgs.tar'],SHAPE)

	if bkgd_data == 'hillchange':
		bkgd_mat = frames[-1]-(frames[0]-frames[-1])
	else:
		if bkgd_data == 'flatmice':
			print >> sys.stderr, '\tmouse masks'
			mouse_hists = Util.tar2ar_all(tars_d['mousemasks.tar'],SHAPE,bool)
		elif bkgd_data == 'fullmice':
			print >> sys.stderr, '\tmouse locations'
			hist2d_tarf = os.path.join(analysis_dir,'mousehists-mpm%s-mo%s-mdm%s.tar' % (max_pix_mult,max_obj,max_dist_mult) )
			outlines_tarf = os.path.join(analysis_dir,'miceols.tar')
			# this will save tons of time...eventually...
			mouse_hists,svec = load_cached_mouse_hist2d_segments(hist2d_tarf,outlines_tarf,SHAPE,check_lengths=True,return_sizes=True, \
									     max_pix_mult=max_pix_mult,max_obj=max_obj,max_dist_mult=max_dist_mult)
			#mouse_hists = load_mouse_hist2d_segments(tars_d['miceols.tar'],SHAPE)
			mouse_radius = math.sqrt(numpy.mean(svec)/math.pi)
		else:
			errstr = 'bkgd_data must be one of ["hillchange","flatmice","fullmice"]; got %s' % bkgd_data
			raise ValueError, errstr
		bkgd_mat = numpy.log1p(reduce(lambda x,y:x+numpy.array(y,dtype=int), mouse_hists))


	print >> sys.stderr, '\tgrounds'
	if post_summarize_analysis:
		grounds_postsummary_tarbase = 'grounds_postsummary-gsc%s.tar' % (ground_step_coeff)
		if grounds_postsummary_tarbase in tars_d:
			grounds = Util.tar2obj_all(tars_d[grounds_postsummary_tarbase],lambda t:numpy.array(eval(t),dtype=float))
			print >> sys.stderr, 'loaded %s grounds' % len(grounds)
		else:
			grounds_postsummary_tarf = os.path.join(analysis_dir,grounds_postsummary_tarbase)
			grounds = vidtools.find_ground_over_frames_matrix(frames,config['ground_anchors'],config['end_ground_anchors'],max_step_limit=mouse_radius*ground_step_coeff)
			#write grounds so future runs can load cached
			tarh = tarfile.open(grounds_postsummary_tarf,'w')
			for g,n in zip(grounds,sorted(tars_d['grounds.tar'].getnames())):
				Util.obj2tar(g,n,tarh)
			tarh.close()
			grounds = map(numpy.array,grounds)
	else:
		grounds = [Util.smooth(g,10) for g in Util.tar2obj_all(tars_d['grounds.tar'],lambda t:numpy.array(eval(t),dtype=float))]

	print >> sys.stderr, '\tactouts'
	if post_summarize_analysis:
		newact_ols_postsummary_tarbase = 'newact_ols_postsummary-gsc%s-mpm%s-mo%s-mdm%s.tar' % \
						 (ground_step_coeff,max_pix_mult,max_obj,max_dist_mult)
		pd_ols_postsummary_tarbase = 'pd_ols_postsummary-gsc%s-mpm%s-mo%s-mdm%s.tar' % \
						 (ground_step_coeff,max_pix_mult,max_obj,max_dist_mult)
		newact_ols_postsummary_tarf = os.path.join(analysis_dir,newact_ols_postsummary_tarbase)
		pd_ols_postsummary_tarf = os.path.join(analysis_dir,pd_ols_postsummary_tarbase)
		
		if newact_ols_postsummary_tarbase in tars_d and pd_ols_postsummary_tarbase in tars_d:
			actouts = Util.tar2obj_all(tars_d[newact_ols_postsummary_tarbase])
			pdols = Util.tar2obj_all(tars_d[pd_ols_postsummary_tarbase])
			print >> sys.stderr, 'loaded %s activity outlines' % len(actouts)
			print >> sys.stderr, 'loaded %s predug outlines' % len(pdols)
		else:
			if bkgd_data == 'hillchange':
				mousemasks = Util.tar2ar_all(tars_d['mousemasks.tar'],SHAPE,bool)
			else:
				mousemasks = [numpy.array(m,dtype=bool) for m in mouse_hists]
			pdmask,pdols,prevactols,actouts = vidtools.find_burrow_over_frames_matrix(frames,mousemasks,grounds,mouse_radius,predug_poly=config.get('predug',None))
			na_tarh = tarfile.open(newact_ols_postsummary_tarf,'w')
			pd_tarh = tarfile.open(pd_ols_postsummary_tarf,'w')
			for na,pd,n in zip(actouts,pdols,sorted(tars_d['newact_ols.tar'].getnames())):
				Util.obj2tar(na,n,na_tarh)
				Util.obj2tar(pd,n,pd_tarh)
			na_tarh.close()
			pd_tarh.close()
	else:
		actouts = Util.tar2obj_all(tars_d['newact_ols.tar'])
		
	
	if hill_win_coeff is None:
		hill_area = numpy.array([sum(SHAPE[0]-g[hill_left:hill_right]) for g in grounds])
		
		hill_increase = []
		
		for v in hill_area[1:] - hill_area[:-1]:
			if 0 < v:
				hill_increase.append(v)
			else:
				hill_increase.append(0)
				
	else:
		#ADD HILL AREA WINDOW CALC
		win = mouse_radius * hill_win_coeff
		hill_increase = []
		for i,g in enumerate(grounds[1:]):
			diff_vec = grounds[i] - g
			hi,win_start = max([(sum(diff_vec[x:x+win]),x) for x in range(len(diff_vec))])   
			hill_increase.append(hi)

	hill_increase = numpy.array(hill_increase)

	grounds = grounds[1:] #drop first ground after calculating differences
	
	# takes second (1-th) actout since first is always blank
	areas = [sum([len(filter(None,vidtools.mask_from_outline(p,frames[0].shape).flat)) for p in polys]) for polys in actouts[1:]]
	#progs = [True and sum([vidtools.hypotenuse(*t) for t in terms]) or 0 for terms in actterms]

	#write hill increase and dig areas
	areas_out = os.path.join(analysis_dir,outfile_prefix+'-dig_areas.list')
	hills_out = os.path.join(analysis_dir,outfile_prefix+'-hill_increases.list')
	open(areas_out,'w').write(list(areas).__repr__())
	open(hills_out,'w').write(list(hill_increase).__repr__())

	#PLOT
	if hill_max_cumul_digscale:
		hill_scale_factor = sum(hill_increase)/float(hill_max_cumul_digscale)
	else:
		hill_scale_factor = 1
	if dig_max_cumul_digscale:
		dig_scale_factor = sum(areas)/float(dig_max_cumul_digscale)
	else:
		dig_scale_factor = 1
	
	if spectrum_len is None:
		spectrum_len = len(actouts)
	
	spec = iplot.subspectrum(spectrum_len,reversed=True,noUV=True)

	figobj = pylab.figure(fig,figsize=(9,6))
	figobj.clf()
	print >> sys.stderr, 'render:'

	pylab.gray()


	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)

	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]))
	if post_summarize_analysis and len(config.get('predug',[])) > 0:
		for i,poly in enumerate(pdols):
			for p in poly:
				if p: ax.add_patch(matplotlib.patches.Polygon(p,ec='gray',fc='none',hatch='/'))
	
	vinf,ainf = analysis_dir.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:
		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)]
		print len(x),len(hill_increase)
		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(min(len(spec),len(areas))):
			if m%(60*hsl_per_min)==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:],-1*Util.normalize(numpy.array(hill_increase[skip_first:]),set_max=set_hill_max)*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*hill_scale_factor,'--w')
		for i,v in zip(x[skip_first:],Util.normalize(numpy.cumsum(numpy.array(hill_increase[skip_first:])))):
			if activity_percentile_plot and v > activity_percentile_plot:
				break
		pylab.plot((i,i),(base,base-(h/2)),'--w',lw=2)
		pylab.bar(x[skip_first:],Util.normalize(numpy.array(areas[skip_first:]),set_max=set_dig_max)*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*dig_scale_factor,'--w')
		for i,v in zip(x[skip_first:],Util.normalize(numpy.cumsum(numpy.array(areas[skip_first:])))):
			if activity_percentile_plot and v > activity_percentile_plot:
				break
		pylab.plot((i,i),(base,base+(h/2)),'--w',lw=2)
		if write_yval:
			pylab.text(5,base-h/2,'hill %0.1f' % max(hill_increase[skip_first:]),color='w')
			pylab.text(5,base+h/2,'dig %0.1f' % max(areas[skip_first:]),color='w')

	#'''
	for i, g in enumerate(grounds):
		pylab.plot(numpy.arange(hill_left,hill_right),g[hill_left:hill_right],c=spec[i])

	#'''

	pylab.plot()
	pylab.ylim((1+graph_height)*SHAPE[0],0)
	pylab.xlim(0,SHAPE[1])

	if outformat is not None:
		out_img = os.path.join(analysis_dir,outfile_prefix+'-activity_summary.'+outformat)
		print >> sys.stderr, 'save as %s' % out_img
		figobj.savefig(out_img)

	#VIDEO!
	spec = iplot.subspectrum(spectrum_len,noUV=True)	
	if vid_fps:
		import cv
		vidout = os.path.join(analysis_dir,outfile_prefix+'-activity_summary.avi')
		if os.path.exists(vidout): os.unlink(vidout)
		pixdim = tuple(reversed(SHAPE))
		pixdim = tuple(numpy.array(pixdim)*2)
		vidwriter = cv.CreateVideoWriter(vidout , cv.FOURCC('x','v','i','d'), vid_fps, pixdim,1)

		cols_polys = []
		cols_hatch_polys = []
		cols_lines = []
		hourbar_kwargs = {'alpha':0.4,'linewidth':0}
		bars = []
		bars.append(('w',(x,numpy.array(hours)*h/2,base,hourbar_kwargs)))
		bars.append(('w',(x,-1*numpy.array(hours)*h/2,base,hourbar_kwargs)))

		for vfi,m in enumerate(mouse_hists[1:]):
			if graph_height is not  None:
				h = frames[0].shape[0] * graph_height
				m = numpy.array(list(m) + list(numpy.zeros((h,m.shape[1]))))

			if len(actouts[vfi]):
				cols_polys.extend(zip([spec[vfi]]*len(actouts[vfi]),actouts[vfi]))
			if post_summarize_analysis and len(config.get('predug',[])) > 0 and len(pdols[vfi]):
				cols_hatch_polys.extend(zip(['gray']*len(pdols[vfi]),['/']*len(pdols[vfi]),pdols[vfi]))
				
				
			cols_lines.append((spec[vfi],(numpy.arange(hill_left,hill_right),grounds[vfi][hill_left:hill_right])))

			this_bars = []
			#pylab.bar(x[skip_first:],-1*Util.normalize(numpy.array(hill_increase[skip_first:]))*h/2,color=spec,edgecolor=spec,bottom=base)
			this_bars.append((spec[:vfi],(x[:vfi],(-1*Util.normalize(numpy.array(hill_increase))*h/2)[:vfi],base,{})))
			
			#pylab.plot(x[skip_first:],base-Util.normalize(numpy.cumsum(numpy.array(hill_increase[skip_first:])))*h/2,'--w')
			hi_line = [('w',(x[:vfi],(base-Util.normalize(numpy.cumsum(numpy.array(hill_increase)))*h/2)[:vfi]))]
			#pylab.bar(x[skip_first:],Util.normalize(numpy.array(areas[skip_first:]))*h/2,color=spec,edgecolor=spec,bottom=base)
			this_bars.append((spec[:vfi],(x[:vfi],(Util.normalize(numpy.array(areas))*h/2)[:vfi],base,{})))
			#pylab.plot(x[skip_first:],base+Util.normalize(numpy.cumsum(numpy.array(areas[skip_first:])))*h/2,'--w')
			ba_line = [('w',(x[:vfi],(base+Util.normalize(numpy.cumsum(numpy.array(areas)))*h/2)[:vfi]))]

				
			cv_im = vidtools.mat_polys2cv(m,cols_polys,hatchpolys=cols_hatch_polys,lines=cols_lines+hi_line+ba_line,bars=bars+this_bars,scale=2)
			nll = cv.WriteFrame(vidwriter,cv_im)
			print >> sys.stderr, '\r\twrite video frame %s' % vfi,
                                                        Util.merge_dictlist([objs_sizes,to_retire_objs_sizes]), \
                                                        Util.merge_dictlist([objs_fols,to_retire_objs_fols]), \
                                                        size_h, size_bins, fol_h, fol_bins,SHAPE)
        prelast_masked = prelast_avg.copy()
        prelast_masked[prelast_mm] = numpy.mean(prelast_avg[:50,:50])
        last_masked = last_avg.copy()
        last_masked[last_mm] = numpy.mean(last_avg[:50,:50])
        this_masked = this_avg.copy()
        this_masked[this_mm] = numpy.mean(this_avg[:50,:50])

        if opts.antfarm_config: #antfarm-specific analysis steps
            digdiffs.append(prelast_masked-this_masked)
            if len(digdiffs) > 3:
                nll = digdiffs.pop(0)
            if newactols and newactols[-1]:
                prevactmask += reduce(lambda x,y:x+y, [vidtools.shrink_mask(vidtools.mask_from_outline(ol,this_masked.shape),1) for ol in newactols[-1]])
            if len(digdiffs) > 1:
                m = digdiffs[-2] > 0.2 #ARBITRARY
                m[last_mm] = True #add mousemask to burrow area
                m[vidtools.grow_mask(vidtools.shrink_mask(prevactmask,1),1)] = False #MASK PREVIOUS DIGGING
                m[groundmask] = False #AND EVERYTHING ABOVE FIRST GROUND
                if opts.outline_engine == 'homebrew':
                    newactols.append(vidtools.chain_outlines_from_mask(m,preshrink=1,grow_by=1,debug=False,return_termini=False,order_points=True,sort_outlines=False))
                    prevol = vidtools.chain_outlines_from_mask(prevactmask,preshrink=1,grow_by=1,debug=False,return_termini=False,order_points=True,sort_outlines=False)
                elif opts.outline_engine == 'shapely':
                    newactols.append(vidtools.chain_outlines_from_mask_shapely(m,preshrink=1,grow_by=1))
                    prevol = vidtools.chain_outlines_from_mask_shapely(prevactmask,preshrink=1,grow_by=1)
                else:
                    print >> sys.stderr, 'outline_engine must be one of %s' % (['homebrew','shapely'])
                    raise ValueError
                digol = newactols[-1]
                                                        size_h, size_bins, fol_h, fol_bins,SHAPE)
        prelast_masked = prelast_avg.copy()
        prelast_masked[prelast_mm] = numpy.mean(prelast_avg[:50, :50])
        last_masked = last_avg.copy()
        last_masked[last_mm] = numpy.mean(last_avg[:50, :50])
        this_masked = this_avg.copy()
        this_masked[this_mm] = numpy.mean(this_avg[:50, :50])

        if opts.antfarm_config:  #antfarm-specific analysis steps
            digdiffs.append(prelast_masked - this_masked)
            if len(digdiffs) > 3:
                nll = digdiffs.pop(0)
            if newactols and newactols[-1]:
                prevactmask += reduce(lambda x, y: x + y, [
                    vidtools.shrink_mask(
                        vidtools.mask_from_outline(ol, this_masked.shape), 1)
                    for ol in newactols[-1]
                ])
            if len(digdiffs) > 1:
                m = digdiffs[-2] > 0.2  #ARBITRARY
                m[last_mm] = True  #add mousemask to burrow area
                m[vidtools.grow_mask(vidtools.shrink_mask(prevactmask, 1),
                                     1)] = False  #MASK PREVIOUS DIGGING
                m[groundmask] = False  #AND EVERYTHING ABOVE FIRST GROUND
                if opts.outline_engine == 'homebrew':
                    newactols.append(
                        vidtools.chain_outlines_from_mask(m,
                                                          preshrink=1,
                                                          grow_by=1,
                                                          debug=False,
                                                          return_termini=False,
Beispiel #8
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)