def draw_class_scatter_and_pie(locsfile,format='pdf',sps=None):
    '''given a .mouselocs file
    (and assuming .locsumm, .frame and .mice are present)
    draws a .scatter.[format] and .pie.[format] summarizing the data in that segment
    '''

    def secs(x):
        return '%0.1f sec' % ((x*sps)/100)

    if sps is None:
        match = re.search(r'\/([\d\.]+)sec_',locsfile)
        if match:
            sps = float(match.groups()[0])
        else:
            sps = 1.0
    
    pylab.gray()

    mice = eval(open(os.path.splitext(locsfile)[0]+'.mice').read())
    locsumm = eval(open(os.path.splitext(locsfile)[0]+'.locsumm').read())
    mouselocs = eval(open(locsfile).read())
    bkgd_image = sorted(mice.keys())[0] # grabs first frame as background

    sca_out = os.path.splitext(locsfile)[0]+'.scatter.'+format
    pie_out = os.path.splitext(locsfile)[0]+'.pie.'+format

    print >> sys.stderr, 'drawing pie (%s) and scatter (%s) with data from %s (%s frames, %s seconds per segment)' % \
          (pie_out,sca_out,locsfile,len(mouselocs.keys()),sps)

    class_locs = Util.invert_dict(mouselocs)

    fig = pylab.figure(1,figsize=(9,6))
    pylab.matshow(vidtools.load_normed_arrays([bkgd_image])[0],fignum=1)
    fig.axes[0].set_xticks([])
    fig.axes[0].set_yticks([])

    counts = []
    piecols = []
    
    for act_class,frames in class_locs.items():
        if act_class is None:
            continue
        coords = filter(None,[mice[f] for f in frames])
        if (coords):
            x,y = Util.dezip(coords)
            pylab.scatter(x,y,c=col[act_class])
        counts.append(len(frames))
        piecols.append(col[act_class])

    pylab.savefig(sca_out)

    fig = pylab.figure(2,figsize=(6,6))
    pylab.pie(counts,colors=piecols,autopct=secs )
    pylab.savefig(pie_out)
Beispiel #2
0
def draw_class_scatter_and_pie(locsfile, format='pdf', sps=None):
    '''given a .mouselocs file
    (and assuming .locsumm, .frame and .mice are present)
    draws a .scatter.[format] and .pie.[format] summarizing the data in that segment
    '''
    def secs(x):
        return '%0.1f sec' % ((x * sps) / 100)

    if sps is None:
        match = re.search(r'\/([\d\.]+)sec_', locsfile)
        if match:
            sps = float(match.groups()[0])
        else:
            sps = 1.0

    pylab.gray()

    mice = eval(open(os.path.splitext(locsfile)[0] + '.mice').read())
    locsumm = eval(open(os.path.splitext(locsfile)[0] + '.locsumm').read())
    mouselocs = eval(open(locsfile).read())
    bkgd_image = sorted(mice.keys())[0]  # grabs first frame as background

    sca_out = os.path.splitext(locsfile)[0] + '.scatter.' + format
    pie_out = os.path.splitext(locsfile)[0] + '.pie.' + format

    print >> sys.stderr, 'drawing pie (%s) and scatter (%s) with data from %s (%s frames, %s seconds per segment)' % \
          (pie_out,sca_out,locsfile,len(mouselocs.keys()),sps)

    class_locs = Util.invert_dict(mouselocs)

    fig = pylab.figure(1, figsize=(9, 6))
    pylab.matshow(vidtools.load_normed_arrays([bkgd_image])[0], fignum=1)
    fig.axes[0].set_xticks([])
    fig.axes[0].set_yticks([])

    counts = []
    piecols = []

    for act_class, frames in class_locs.items():
        if act_class is None:
            continue
        coords = filter(None, [mice[f] for f in frames])
        if (coords):
            x, y = Util.dezip(coords)
            pylab.scatter(x, y, c=col[act_class])
        counts.append(len(frames))
        piecols.append(col[act_class])

    pylab.savefig(sca_out)

    fig = pylab.figure(2, figsize=(6, 6))
    pylab.pie(counts, colors=piecols, autopct=secs)
    pylab.savefig(pie_out)
Beispiel #3
0
    ybounds = [int(i) for i in sys.argv[-2:]]
elif len(sys.argv) == 10:
    mousezcut = float(sys.argv[7])
    ybounds = [int(i) for i in sys.argv[-2:]]
else:
    mousezcut = None
    ybounds = None

print >> sys.stderr, 'summarizing frames %s to %s from %s. output to %s. Masked by %s and %s. Movement z score of %s, ybounds of %s' % (
    start, stop, imagedir, outroot, maskfile, burrow_maskfile, mousezcut,
    ybounds)

images = sorted(glob(imagedir + '/*.png'))[start:stop]
bkgd_images = sorted(glob(imagedir + '/*.png'))[stop:stop + (stop - start)]

frames = vidtools.load_normed_arrays(images, pixav)

#print >> sys.stderr, 'time averaging invoked, window of %s frames' % (timeav)
#frames = Util.zsmooth_convol(frames,timeav)

#windowed_frames = vidtools.average_frames(frames,num_wins=wins)
#frameav = vidtools.average_frames(windowed_frames)
'''
try:
	frameav = vidtools.average_frames(bkgd_images,pixav)
except IndexError:
	frameav = vidtools.average_frames(frames)
'''
frameav = vidtools.average_frames(frames)

SHAPE = frames[0].shape
micez_outfile = os.path.join( outroot,'%07d-%07d.micez' % (start,stop) )
micesize_outfile = os.path.join( outroot,'%07d-%07d.micesize' % (start,stop) )
micelen_outfile = os.path.join( outroot,'%07d-%07d.micelen' % (start,stop) )

if os.path.exists(outfile):
	print >> sys.stderr, 'file %s present; skipping' % outfile
else:
	print >> sys.stderr, 'summarizing frames %s to %s from %s. output to %s. Masked by %s. Movement z score of %s, xybounds of %s. Intensities averaged over %s pixels' % (start,stop,imagedir,outroot,maskfile,mousezcut,opts['xybounds'],pixav)

	#transparent support for supplying a tarball in lieu of a directory; assumes the tarball is only images
	if imagedir.endswith('.tar'):
		import tarfile
		imtar = tarfile.open(imagedir)
		tarcont = sorted(imtar.getnames())[start:stop]
		images = ['%s:%s' % (imagedir,f) for f in tarcont]
		frames = vidtools.load_normed_arrays([imtar.extractfile(f) for f in tarcont],pixav)
	else:
		images = sorted(glob(imagedir+'/*.png'))[start:stop]
		#new toys! image smoothing
		SMOOTH5 = ImageFilter.Kernel( (5, 5) , (1,2,2,2,1,2,3,3,3,2,2,3,5,3,2,2,3,3,3,2,1,2,2,2,1 ) )
		frames = vidtools.load_normed_arrays(images,img_smooth_kernel=SMOOTH5)

	print >> sys.stderr, len(images),'images selected'
	print >> sys.stderr, len(frames),'frames loaded'
	
	exp_frames = int(stop) - int(start)
	if len(frames) != exp_frames:
		raise ValueError, 'expected %s frames, found %s' % (exp_frames,len(frames))

	if maskfile == 'None':
		mask = numpy.zeros(frames[0].shape,dtype=bool)
Beispiel #5
0
def draw_class_scatter_and_pie(locsfile,format='pdf',sps=None,scatter_lines=None,scatter_polys=None,draw_pie=True,shape=(480,720),poly_col=None):
    '''given a .mouselocs file
    (and assuming .locsumm, .frame and .mice are present)
    draws a .scatter.[format] and .pie.[format] summarizing the data in that segment

	if .ground or .actmask are present, these will be plotted as lines and polygons respectively
    '''

    def secs(x):
        return '%0.1f sec' % ((x*sps)/100)

    if sps is None:
        match = re.search(r'\/([\d\.]+)sec_',locsfile)
        if match:
            sps = float(match.groups()[0])
        else:
            sps = 1.0

    if scatter_polys is not None and poly_col is None:
        poly_col = ['b']*len(scatter_polys)
    pylab.gray()
    print >> sys.stderr, 'loading mice'
    mice = eval(open(os.path.splitext(locsfile)[0]+'.mice').read())
    print >> sys.stderr, 'loading location summaries'
    locsumm = eval(open(os.path.splitext(locsfile)[0]+'.locsumm').read())
    print >> sys.stderr, 'loading locations'
    mouselocs = eval(open(locsfile).read())
    bkgd_image = sorted(mice.keys())[0] # grabs first frame as background

    sca_out = os.path.splitext(locsfile)[0]+'.scatter.'+format
    pie_out = os.path.splitext(locsfile)[0]+'.pie.'+format
    if draw_pie:
        print >> sys.stderr, 'drawing pie (%s) and scatter (%s) with data from %s (%s frames, %s seconds per segment)' % \
	          (pie_out,sca_out,locsfile,len(mouselocs.keys()),sps)
    else:
        print >> sys.stderr, 'drawing scatter (%s) with data from %s (%s frames, %s seconds per segment)' % \
          (sca_out,locsfile,len(mouselocs.keys()),sps)

    class_locs = Util.invert_dict(mouselocs)

    fig = pylab.figure(1,figsize=(9,6))
    fig.clf()
    print >> sys.stderr, 'render:'
    try:
	    bkgd_mat = vidtools.load_normed_arrays([bkgd_image])[0]
    except:
	    print >> sys.stderr, '\timages unavailable, attempting with .frame'
	    frame = os.path.splitext(locsfile)[0]+'.frame'
	    bkgd_mat = numpy.fromfile(frame).reshape(shape)
    print >> sys.stderr, '\tframe'
    pylab.matshow(bkgd_mat,fignum=1)
    fig.axes[0].set_xticks([])
    fig.axes[0].set_yticks([])

    if os.path.exists(os.path.splitext(locsfile)[0]+'.ground'):
        ground = pylab.fromfile(os.path.splitext(locsfile)[0]+'.ground',sep='\n')
        pylab.plot(ground,'g')

	if os.path.exists(os.path.splitext(locsfile)[0]+'.actpoly'):
		actpoly = eval(open(os.path.splitext(locsfile)[0]+'.actpoly').read())
	#elif os.path.exists(os.path.splitext(locsfile)[0]+'.actmat'):
	#	actmat = numpy.fromfile(os.path.splitext(locsfile)[0]+'.actmat').reshape(bkgd_mat.shape)
	#	if ground:
	#		actmat = Util.subtract_mask(actmat,vidtools.mask_from_vector(ground+10,bkgd_mat.shape))
	#attempt to draw activity polygons from actmasks directly is too much work at this step;
	#supply scatter_polys or make sure an actpoly file is present	

    counts = []
    piecols = []
    
    print >> sys.stderr, '\tlines'
    if isinstance(scatter_lines,list):
        for l in scatter_lines:
            pylab.plot(l)
			
    print >> sys.stderr, '\tpolygons'
    if isinstance(scatter_polys,list):
        for i,p in enumerate(scatter_polys):
	    try:
                fig.axes[0].add_patch(pylab.matplotlib.patches.Polygon(p,fc='none',ec=poly_col[i]))
	    except AssertionError:
	        print >> sys.stderr, 'poly %s improper dimensions' % p

    print >> sys.stderr, '\tscatter'
    for act_class,frames in class_locs.items():
        if act_class is None:
            continue
        coords = filter(None,[mice[f] for f in frames])
        if (coords):
            x,y = Util.dezip(coords)
            #pylab.scatter(x,y,s=10,c=col[act_class])
	    pylab.scatter(x,y,s=3,c='r',edgecolors='none')
        counts.append(len(frames))
        piecols.append(col[act_class])

    pylab.savefig(sca_out)
    if draw_pie:
        print >> sys.stderr, '\tpie'
        fig = pylab.figure(2,figsize=(6,6))
        fig.clf()
        pylab.pie(counts,colors=piecols,autopct=secs )
        pylab.savefig(pie_out)
Beispiel #6
0
    FORMAT='jpg'
    print >> sys.stderr, '"jpg" found in path, assuming FORMAT=jpg\n'
else:
    FORMAT='png'

match = re.search(r'\/([\d\.]+)fps\/',imagedir)
if match:
    FPS = match.groups()[0]
else:
    FPS = 30

images = sorted(glob(imagedir+'/*.'+FORMAT))
print >> sys.stderr, len(images),'images found in imagedir\n'
print >> sys.stderr, 'split, top and bottom XY:',splitxy,topxy,bottomxy

SHAPE=vidtools.load_normed_arrays(images[:1])[0].shape

match = re.search('(\d+)fps',imagedir)
if match:
    segment_step = int(seglen)*int(match.groups()[0])
    print >> sys.stderr, 'segment length will be %s frames (%s seconds, %s fps)\n' % (segment_step,seglen,match.groups()[0])
    unit = 'sec'
else:
    segment_step = int(seglen)
    print >> sys.stderr,'fps could not be found in image path, using seglen (%s) as segment_step (i.e. 1 fps)\n' % seglen
    unit = 'frames'

outroot = os.path.join(imagedir,'analysis','%s%s_%smousez' % (seglen,unit,mousez))

try:
    os.makedirs(outroot)
	for cnt,image in enumerate(mice.keys()):
		if cnt%tickon == 0:
			print >> sys.stderr, '%6d/%6d' % (cnt,len(mice))
		im = os.path.split(image)[1]
		n,ext = os.path.splitext(im)
		n = int(n)
		outf = os.path.join(an_im_dir,'%07d.png' % (n-startn))
                #print >> sys.stderr, '\tdraw %s...' % outf,

		if os.path.exists(outf):
                	#print >> sys.stderr, 'skip'
			continue

		#pylab.matshow(vidtools.load_normed_arrays([image])[0],fignum=1)
                fig = pylab.figure(1)
                fr = vidtools.load_normed_arrays([image])[0]
                aspect = fr.shape[1]/float(fr.shape[0])
                pylab.imshow(fr)
                ax = pylab.figure(1).axes[0]

		try:
			pylab.plot(g,'g')
		except:
			pass

		# out for now; if "mouseloc" concept returns, uncomment
		#if mouselocs[image] and mice[image] is not None:
                #	lastcoord = Util.dezip([mice[image]])
		#	lastcol = col[mouselocs[image]]
		#	pylab.scatter(c=lastcol,*lastcoord)
Beispiel #8
0
micelen_outfile = os.path.join(outroot, '%07d-%07d.micelen' % (start, stop))

if os.path.exists(outfile):
    print >> sys.stderr, 'file %s present; skipping' % outfile
else:
    print >> sys.stderr, 'summarizing frames %s to %s from %s. output to %s. Masked by %s. Movement z score of %s, xybounds of %s. Intensities averaged over %s pixels' % (
        start, stop, imagedir, outroot, maskfile, mousezcut, opts['xybounds'],
        pixav)

    #transparent support for supplying a tarball in lieu of a directory; assumes the tarball is only images
    if imagedir.endswith('.tar'):
        import tarfile
        imtar = tarfile.open(imagedir)
        tarcont = sorted(imtar.getnames())[start:stop]
        images = ['%s:%s' % (imagedir, f) for f in tarcont]
        frames = vidtools.load_normed_arrays(
            [imtar.extractfile(f) for f in tarcont], pixav)
    else:
        images = sorted(glob(imagedir + '/*.png'))[start:stop]
        #new toys! image smoothing
        SMOOTH5 = ImageFilter.Kernel(
            (5, 5), (1, 2, 2, 2, 1, 2, 3, 3, 3, 2, 2, 3, 5, 3, 2, 2, 3, 3, 3,
                     2, 1, 2, 2, 2, 1))
        frames = vidtools.load_normed_arrays(images, img_smooth_kernel=SMOOTH5)

    print >> sys.stderr, len(images), 'images selected'
    print >> sys.stderr, len(frames), 'frames loaded'

    exp_frames = int(stop) - int(start)
    if len(frames) != exp_frames:
        raise ValueError, 'expected %s frames, found %s' % (exp_frames,
                                                            len(frames))
elif len(sys.argv) == 9:
	mousezcut = None
	ybounds = [int(i) for i in sys.argv[-2:]]
elif len(sys.argv) == 10:
	mousezcut = float(sys.argv[7])
	ybounds = [int(i) for i in sys.argv[-2:]]
else:
	mousezcut = None
	ybounds = None

print >> sys.stderr, 'summarizing frames %s to %s from %s. output to %s. Masked by %s and %s. Movement z score of %s, ybounds of %s' % (start,stop,imagedir,outroot,maskfile,burrow_maskfile,mousezcut,ybounds)

images = sorted(glob(imagedir+'/*.png'))[start:stop]
bkgd_images = sorted(glob(imagedir+'/*.png'))[stop:stop+(stop-start)]

frames = vidtools.load_normed_arrays(images,pixav)

#print >> sys.stderr, 'time averaging invoked, window of %s frames' % (timeav)
#frames = Util.zsmooth_convol(frames,timeav)

#windowed_frames = vidtools.average_frames(frames,num_wins=wins)
#frameav = vidtools.average_frames(windowed_frames)
'''
try:
	frameav = vidtools.average_frames(bkgd_images,pixav)
except IndexError:
	frameav = vidtools.average_frames(frames)
'''
frameav = vidtools.average_frames(frames)

SHAPE = frames[0].shape
Beispiel #10
0
def draw_class_scatter_and_pie(locsfile,
                               format='pdf',
                               sps=None,
                               scatter_lines=None,
                               scatter_polys=None):
    '''given a .mouselocs file
    (and assuming .locsumm, .frame and .mice are present)
    draws a .scatter.[format] and .pie.[format] summarizing the data in that segment

	if .ground or .actmask are present, these will be plotted as lines and polygons respectively
    '''
    def secs(x):
        return '%0.1f sec' % ((x * sps) / 100)

    if sps is None:
        match = re.search(r'\/([\d\.]+)sec_', locsfile)
        if match:
            sps = float(match.groups()[0])
        else:
            sps = 1.0

    pylab.gray()

    mice = eval(open(os.path.splitext(locsfile)[0] + '.mice').read())
    locsumm = eval(open(os.path.splitext(locsfile)[0] + '.locsumm').read())
    mouselocs = eval(open(locsfile).read())
    bkgd_image = sorted(mice.keys())[0]  # grabs first frame as background

    sca_out = os.path.splitext(locsfile)[0] + '.scatter.' + format
    pie_out = os.path.splitext(locsfile)[0] + '.pie.' + format

    print >> sys.stderr, 'drawing pie (%s) and scatter (%s) with data from %s (%s frames, %s seconds per segment)' % \
          (pie_out,sca_out,locsfile,len(mouselocs.keys()),sps)

    class_locs = Util.invert_dict(mouselocs)

    fig = pylab.figure(1, figsize=(9, 6))
    fig.clf()
    bkgd_mat = vidtools.load_normed_arrays([bkgd_image])[0]
    pylab.matshow(bkgd_mat, fignum=1)
    fig.axes[0].set_xticks([])
    fig.axes[0].set_yticks([])

    if os.path.exists(os.path.splitext(locsfile)[0] + '.ground'):
        ground = pylab.fromfile(os.path.splitext(locsfile)[0] + '.ground',
                                sep='\n')
        pylab.plot(ground, 'g')

        if os.path.exists(os.path.splitext(locsfile)[0] + '.actpoly'):
            actpoly = eval(
                open(os.path.splitext(locsfile)[0] + '.actpoly').read())

#elif os.path.exists(os.path.splitext(locsfile)[0]+'.actmat'):
#	actmat = numpy.fromfile(os.path.splitext(locsfile)[0]+'.actmat').reshape(bkgd_mat.shape)
#	if ground:
#		actmat = Util.subtract_mask(actmat,vidtools.mask_from_vector(ground+10,bkgd_mat.shape))
#attempt to draw activity polygons from actmasks directly is too much work at this step;
#supply scatter_polys or make sure an actpoly file is present

    counts = []
    piecols = []

    if isinstance(scatter_lines, list):
        for l in scatter_lines:
            pylab.plot(l)

    if isinstance(scatter_polys, list):
        for p in scatter_polys:
            fig.axes[0].add_patch(
                pylab.matplotlib.patches.Polygon(p, fc='none', ec='b'))

    for act_class, frames in class_locs.items():
        if act_class is None:
            continue
        coords = filter(None, [mice[f] for f in frames])
        if (coords):
            x, y = Util.dezip(coords)
            pylab.scatter(x, y, s=10, c=col[act_class])
        counts.append(len(frames))
        piecols.append(col[act_class])

    pylab.savefig(sca_out)

    fig = pylab.figure(2, figsize=(6, 6))
    fig.clf()
    pylab.pie(counts, colors=piecols, autopct=secs)
    pylab.savefig(pie_out)