for i, cT in enumerate(sky['changeTimes'][:-1]): inds = (times > cT+POSTCHANGE_BUFFER) & (times < sky['changeTimes'][i+1]-PRECHANGE_BUFFER) ors = orientations[inds] ors = ors[~numpy.isnan(ors)] if len(ors)>0: totals[sky['directions'][i]] = numpy.concatenate((totals[sky['directions'][i]],ors)) for i, d in enumerate(COLORS): worldTotals = numpy.concatenate((worldTotals,totals[d]+ROTATIONS[d])) M[fNum]=circmean(worldTotals*numpy.pi/180) V[fNum]=circvar(worldTotals*numpy.pi/180) #pylab.figure() pylab.subplot(4,5,fNum+1,polar=True) plotArgs = dict(color='k',linewidth=2) orw,n,b,bc,ax = flypod.rose(worldTotals,plotArgs=plotArgs) pylab.hold('on') for i, d in enumerate(COLORS): plotArgs = dict(color=COLORS[d],linewidth=.5) flypod.rose(totals[d]+ROTATIONS[d],plotArgs=plotArgs) pylab.polar([0,numpy.pi/2-M[fNum]],[0,ax.get_rmax()*(1-V[fNum])],color='k') ax.set_rmax(.15) ax.set_rgrids([1],'') ax.set_thetagrids([0,90,180,270],['E','N','W','S']) pylab.title(fly['dirName']) pylab.suptitle(baseDir) circVar[baseDir] = numpy.copy(V)
orientations[inds] = numpy.nan inds = (times > TOTAL_TIME + times[0]) orientations[inds] = numpy.nan pylab.draw() fig = pylab.figure() fig.set_facecolor('w') pylab.suptitle(fly['fileName']) for i, cT in enumerate(sky['changeTimes'][:-1]): pylab.subplot(5,4,1+i,polar=True) ax=pylab.gca() inds = (times > cT) & (times < sky['changeTimes'][i+1]) ors = orientations[inds] ors = ors[~numpy.isnan(ors)] if len(ors)>0: orw,n,b,bc,ax = flypod.rose(ors,360) m=circmean(ors,high=180,low=-180) v=circvar(ors*numpy.pi/180,high=numpy.pi,low=-numpy.pi) hold('on') polar([0,numpy.pi/2-m*numpy.pi/180],[0,ax.get_rmax()*(1-v)]) ax.set_rmax(.4) ax.set_rgrids([1],'') ax.set_thetagrids([0,90,180,270],['','','','']) title(sky['directions'][i]) #ax.set_axis_bgcolor(COLORS[sky['directions'][i]]) ax.axesPatch.set_facecolor(COLORS[sky['directions'][i]]) ax.axesPatch.set_alpha(0.4) totals[sky['directions'][i]] = numpy.concatenate((totals[sky['directions'][i]],ors)) pylab.draw()
if mod(i, 2)==0: rotatorStateForPlots = sky['rotatorState'][i] else: rotatorStateForPlots = 'R' totals[rotatorStateForPlots] = numpy.concatenate((totals[rotatorStateForPlots],ors)) for i, d in enumerate(COLORS): ax = fig.add_subplot(i+1,numFlies,1+fNum,polar=True) if MOD_180: m = (circmean(totals[d]*2.0*numpy.pi/180.0)*180.0/numpy.pi)/2.0 m = mod(m,180) else: m = circmean(totals[d]*numpy.pi/180.0)*180.0/numpy.pi #m=circmean(totals[d]*numpy.pi/180.0)*180.0/numpy.pi v=circvar(totals[d]*numpy.pi/180) orw,n,b,bc,ax = flypod.rose(totals[d],360) pylab.polar([0,numpy.pi/2-m*numpy.pi/180],[0,ax.get_rmax()*(1-v)]) ax.set_rmax(.15) ax.set_rgrids([1],'') ax.set_thetagrids([0,90,180,270],['','','','']) pylab.title(str(int(round(m)))) ax.axesPatch.set_facecolor(COLORS[d]) ax.axesPatch.set_alpha(0.2) meanAngle[fNum,i] = m.copy() varAngle[fNum,i] = v.copy() mA[bd] = meanAngle.copy() vA[bd] = varAngle.copy() nS[bd] = numStops.copy() if MOD_180: diffAngles = abs(angleDiffDegrees(mA[bd][:,1],mA[bd][:,0]))
def make_frames(inDirName,outDirName): """saves frames in movie with orientation superimposed arguments: inDirName directory with fly, sky, and .fmf files outDirName directory to save frames to example: make_frames('/media/weir05/data/rotator/white/diffuserPol/12trials/fly08','./frames/') """ fly = flypod.analyze_directory(inDirName) sky = sky_times.analyze_directory(inDirName) orientations = fly['orientations'].copy() + 180 times = fly['times'].copy() rotatorStateEachFrame = -np.ones(times.shape) # we will store frame-based rotator state in here COLORS = dict({'U': 'm', 'R': 'c'}) ROTATOR_CODE = dict({'U': 0, 'R': 1}) FRAMESTEP = 65 #130 circleCenterX, circleCenterY, circleRadius = circle_fit(fly['x'],fly['y']) #NOTE:should have been saving this info from start. fmf = FMF.FlyMovie(join(inDirName,fly['fileName'])) nFrames = fmf.get_n_frames() timestamps = np.ma.masked_all(len(range(0,int(nFrames),FRAMESTEP))) fig = pylab.figure() frameAxes = fig.add_axes([0.1, 0.3, 0.5, 0.6]) timePlotAxes = fig.add_axes([0.1, 0.1, 0.8, 0.15]) polarTopAxes = fig.add_axes([0.55, 0.6, 0.4, 0.25],polar=True) polarBottomAxes = fig.add_axes([0.55, 0.3, 0.4, 0.25],polar=True) timePlotAxes.plot(times,orientations,'k') for i, cT in enumerate(sky['changeTimes'][:-1]): timePlotAxes.axvspan(cT, sky['changeTimes'][i+1], facecolor=COLORS[sky['rotatorState'][i]], alpha=0.2) startInd = np.argmin(abs(times-cT)) endInd = np.argmin(abs(times-sky['changeTimes'][i+1])) rotatorStateEachFrame[startInd:endInd] = ROTATOR_CODE[sky['rotatorState'][i]] unrotatedOrientations = np.ma.masked_where(rotatorStateEachFrame != 0,orientations) rotatedOrientations = np.ma.masked_where(rotatorStateEachFrame != 1,orientations) if sum(np.isnan(orientations)) > 0: for trackingErrorTime in times[np.isnan(orientations)]: timePlotAxes.axvline(trackingErrorTime,linewidth=2,color='k') if fly.has_key('stopTimes'): for i, sT in enumerate(fly['stopTimes']): timePlotAxes.axvspan(sT, fly['startTimes'][i], facecolor='r', alpha=0.5) timePlotAxes.set_axis_off() for fn,frameNumber in enumerate(range(0,int(nFrames),FRAMESTEP)): frame,timestamps[fn] = fmf.get_frame(frameNumber) frameAxes.imshow(frame,cmap='gray',origin='lower') lineX = [circleCenterX+fly['ROI'][2], 2*(circleCenterX+fly['ROI'][2])-(fly['x'][frameNumber]+fly['ROI'][2])] lineY = [circleCenterY+fly['ROI'][1], 2*(circleCenterY+fly['ROI'][1])-(fly['y'][frameNumber]+fly['ROI'][1])] frameAxes.plot(lineX,lineY,'b', linewidth=2) frameAxes.set_axis_off() movingDot = timePlotAxes.scatter(times[frameNumber],orientations[frameNumber]) timePlotAxes.set_ylim((-10,370)) orw,n,b,bc,ax = flypod.rose(unrotatedOrientations[:frameNumber].compressed(),360,ax=polarTopAxes,plotArgs = dict(color=COLORS['U'])) polarTopAxes.set_rmax(.25) polarTopAxes.set_rgrids([1],'') polarTopAxes.set_thetagrids([0,90,180,270],['','','','']) orw,n,b,bc,ax = flypod.rose(rotatedOrientations[:frameNumber].compressed(),360,ax=polarBottomAxes,plotArgs = dict(color=COLORS['R'])) polarBottomAxes.set_rmax(.25) polarBottomAxes.set_rgrids([1],'') polarBottomAxes.set_thetagrids([0,90,180,270],['','','','']) fig.savefig(join(outDirName,'frame')+("%05d" %(fn+1))+'.png') polarTopAxes.cla() polarBottomAxes.cla() frameAxes.cla() movingDot.remove() print 'frame rate = ' + str(1/np.mean(np.diff(timestamps)))