def figure3(paths):
    stable = []
    unstable = []
    for path in paths:
        
        traj = trajectories.genfromtxt(path)
        steps = np.genfromtxt(os.path.join(path,'Analysis\step_activity.csv'))
        time = np.genfromtxt(os.path.join(path,'front_video.csv'),dtype=str)
        trajtrials = trials.gettrialindices(path)
        stepstate = trials.gettrialstate(os.path.join(path,'step3_trials.csv'),trajtrials)
        
        ftraj = trajectories.heightfilter(trajectories.lengthfilter(traj,0,500),0,6)
        
    #    plt.figure()
    #    for t in traj.tolist():
    #        plt.plot(len(t),max(t[:,1]),'k.')
    #    for t in ftraj.tolist():
    #        plt.plot(len(t),max(t[:,1]),'r.')
        
        traj = trajectories.mirrorleft(ftraj)
        #activesteptrials = [sum(steps[s,3 if s.step < 0 else 2]) / 255 for s in traj.slices]
        activesteptrials = [s for s in traj.slices
        if (sum(steps[s,3 if s.step < 0 else 2]) / 255) > 500]
        traj = trajectories.trajectories(traj.data,activesteptrials)
    
    #    plt.figure()    
    #    for t in traj.tolist():
    #        plt.plot(t[:,0],t[:,1],'k',alpha=0.1)
        
        speed = [np.insert(np.diff(traj.data[s,0])/timedelta(time[s]),0,0)
        for s in traj.slices]
        validtrials = [traj.slices[i] for i,s in enumerate(speed) if np.mean(s) > 0]
        
        traj = trajectories.trajectories(traj.data,validtrials)
        speed = [s for s in speed if np.mean(s) > 0]
        
    #    plt.figure()
    #    for sp in speed:
    #        plt.plot(sp,'k',alpha=0.1)
            
        # Bin (progrssion - X) Speed Profiles (from position 200 to 1200)
        numBins = 25
        binSize = 50 / numBins
        bins = range(0,50,binSize)
        speedbins = np.zeros((len(traj.slices),numBins))
        for i,t in enumerate(traj.tolist()):
            xs = t[:,0]
            binindices = np.digitize(xs,bins)
            for b in range(numBins):
                speedbin = speed[i][binindices == b]
                speedbins[i,b] = np.mean(speedbin) if speedbin.size > 0 else np.nan
            basespeed = stats.nanmean(speedbins[i,0:numBins/3])
            speedbins[i,:] /= basespeed
        
        stabletrials = [i for i,s in enumerate(validtrials) if stepstate[s.start]]
        unstabletrials = [i for i,s in enumerate(validtrials) if not stepstate[s.start]]
        
        stablespeeds = speedbins[stabletrials,:]
        unstablespeeds = speedbins[unstabletrials,:]
        stable.append(stablespeeds)
        unstable.append(unstablespeeds)
        
    def plotcurve(speeds,color):
        x = np.arange(numBins) * binSize
        mu = np.mean(speeds,axis=0)
        sd = np.std(speeds,axis=0)
        plt.plot(x,mu,color,linewidth=3)
        plt.plot(x,mu-sd,'k--')
        plt.plot(x,mu+sd,'k--')
        plt.fill_between(x,mu-sd,mu+sd,alpha=0.1,color=color)
    
    plt.figure()
    stablespeeds = np.concatenate(stable,0)
    unstablespeeds = np.concatenate(unstable,0)
    plotcurve(stablespeeds,'g')
    plotcurve(unstablespeeds,'r')
    return stablespeeds,unstablespeeds
Пример #2
0
#p = [plt.plot(t[:,0],t[:,1]) for t in traj.tolist()]

# Load video time
vtimepath = os.path.join(path, 'Analysis/videotime.csv')
vtime = np.genfromtxt(vtimepath)

# Filter trajectories by height
ftraj = trajectories.heightfilter(traj, 0, 5)
print ftraj.slices.shape

# Filter trajectories by step activity
#steps = np.genfromtxt(os.path.join(path,'Analysis\step_activity.csv'))

# Compute speed and mirror values for left trials
sp = trajectories.speed(ftraj, vtime)
mtraj = trajectories.mirrorleft(ftraj)

for s in mtraj.slices:
    if s.start < s.stop:  # right
        x = mtraj.data[s, 0]
        y = mtraj.data[s, 1]
    else:
        x = mtraj.data[s, 0] - 7 * trajectories.width_pixel_to_cm
        y = mtraj.data[s, 1]
    plt.plot(x, y, 'k', alpha=0.1)

bins = np.linspace(0, 40, 40)
rawspeedbins = np.array(trajectories.speedbins(mtraj, sp, bins))

# Compute baseline speed in 1st third of assay and subtract
baselinesp = np.nanmean(rawspeedbins[:, 0:rawspeedbins.shape[1] / 3], axis=1)
#p = [plt.plot(t[:,0],t[:,1]) for t in traj.tolist()]

# Load video time
vtimepath = os.path.join(path, 'Analysis/videotime.csv')
vtime = np.genfromtxt(vtimepath)

# Filter trajectories by height
ftraj = trajectories.heightfilter(traj,0,5)
print ftraj.slices.shape

# Filter trajectories by step activity
#steps = np.genfromtxt(os.path.join(path,'Analysis\step_activity.csv'))

# Compute speed and mirror values for left trials
sp = trajectories.speed(ftraj,vtime)
mtraj = trajectories.mirrorleft(ftraj)

for s in mtraj.slices:
    if s.start < s.stop: # right
        x = mtraj.data[s,0]
        y = mtraj.data[s,1]
    else:
        x = mtraj.data[s,0]-7*trajectories.width_pixel_to_cm
        y = mtraj.data[s,1]
    plt.plot(x,y,'k',alpha=0.1)

bins = np.linspace(0,40,40)
rawspeedbins = np.array(trajectories.speedbins(mtraj,sp,bins))

# Compute baseline speed in 1st third of assay and subtract
baselinesp = np.nanmean(rawspeedbins[:,0:rawspeedbins.shape[1]/3],axis=1)