fig = plt.figure('Worm crawling', figsize=(8, 4.5), dpi=300)
gs1 = gridspec.GridSpec(1, 2, width_ratios=[1, 3])
gs1.update(left=0.075,
           right=0.95,
           wspace=0.1,
           bottom=0.15,
           top=0.9,
           hspace=0.15)

# recreate worms
#=============================================================================#
#                           # load eigenworms
#=============================================================================#
eigenwormsFull = dh.loadEigenBasis(filename='utility/Eigenworms.dat',
                                   nComp=5,
                                   new=True)
eigenworms = dh.loadEigenBasis(filename='utility/Eigenworms.dat',
                               nComp=3,
                               new=True)

pc1, pc2, pc3, avTrue, thetaTrue = moving['Behavior']['Eigenworm1'],moving['Behavior']['Eigenworm2'],\
                        moving['Behavior']['Eigenworm3'],  moving['Behavior']['AngleVelocity'],  moving['Behavior']['Theta']
pcs = np.vstack([pc3, pc2, pc1])

# actual centerline
cl = moving['CL']

#=============================================================================#
# load predicted worm
#=============================================================================#
示例#2
0
def main():    
    show = 1
    
    #=============================================================================#
    #                           # data parameters
    #=============================================================================#
    
    dataPars = {'medianWindow':5, # smooth eigenworms with gauss filter of that size, must be odd
            'gaussWindow':5, # sgauss window for angle velocity derivative. must be odd
            'rotate':True, # rotate Eigenworms using previously calculated rotation matrix
            'windowGCamp': 5  # gauss window for red and green channel
            }
    folder = "AML32_moving/"
    dataLog = "AML32_moving/AML32_moving_datasets.txt"
    outLoc = "Analysis/AML32_moving_results.hdf5"
    
    #=============================================================================#
    #                           # load eigenworms
    #=============================================================================#
    eigenworms = dh.loadEigenBasis(filename='utility/Eigenworms.dat', nComp=3, new = True)    
    #=============================================================================#
    # load datasets from hdf5/matlab in a dictionary
    #=============================================================================#
    dataSets = dh.loadMultipleDatasets(dataLog, pathTemplate=folder, dataPars = dataPars, nDatasets = 2)
    keyList = np.sort(dataSets.keys())
    # pick one dataset
    key = keyList[1]    
    data = dataSets[key]
    pc1, pc2, pc3, avTrue, thetaTrue = data['Behavior']['Eigenworm1'],data['Behavior']['Eigenworm2'],\
                            data['Behavior']['Eigenworm3'],  data['Behavior']['AngleVelocity'],  data['Behavior']['Theta']
    pcs = np.vstack([pc3,pc2, pc1])
    # actual centerline
    cl= data['CL']     
    #=============================================================================#
    # for debugging recreate an existing, approximated shape from 3 eigenworms
    #=============================================================================#    
    pcsNew, meanAngle, lengths, refPoint = dh.calculateEigenwormsFromCL(cl, eigenworms)
    pc3New, pc2New, pc1New = pcsNew
    cl = dh.calculateCLfromEW(pcsNew, eigenworms, meanAngle, lengths, refPoint)
    # transform eigenworms exactly the same way. Otherwise we get some artefacts from nans
    #_,_,_, av, theta = dh.transformEigenworms(pcsNew, dataPars)
    r = (pcsNew[2]**2+pcsNew[1]**2)
    #=============================================================================#
    # here we reconstruct from the true angular velocity to check the math. This is smoothed, so we need to compare with this version
    #=============================================================================#
    xt, yt, zt = recrWorm(avTrue, pc3, thetaTrue, r=r)
    pcsR = np.vstack([zt,yt, xt])
    clApprox = dh.calculateCLfromEW(pcsR, eigenworms, meanAngle, lengths, refPoint)
    #=============================================================================#
    # load predicted worm
    #=============================================================================#
    resultDict = dh.loadDictFromHDF(outLoc)
    results = resultDict[key]
    avP = results['ElasticNet']['AngleVelocity']['output'][:len(pcs[0])]
    tP = results['ElasticNet']['Eigenworm3']['output'][:len(pcs[0])]
    print 'R2'
    print results['ElasticNet']['AngleVelocity']['score'],results['ElasticNet']['AngleVelocity']['scorepredicted']
    print results['ElasticNet']['Eigenworm3']['score'],results['ElasticNet']['Eigenworm3']['scorepredicted']
    #=============================================================================#    
    # reconstruct worm from predicted angular velocity.
    #=============================================================================#
    xP, yP, zP = recrWorm(avP, tP, thetaTrue,r=r)
    pcsP = np.vstack([zP,yP, xP])
    clPred = dh.calculateCLfromEW(pcsP, eigenworms, meanAngle, lengths, refPoint)
    # center around midpoint
    originalCMS = np.tile(np.mean(cl, axis=1)[:,np.newaxis,:], (1,99,1))
    clApprox -= originalCMS
    cl -= originalCMS
    clPred -= originalCMS# +(0,100)

    if show:
        
        # animate centerlines
        fig = plt.figure('prediction')
        frames = np.arange(1750,2550)
        #frames = np.arange(1750,1760)
        f, (ax0, ax1, ax) = plt.subplots(2,1)
        ax0.plot(avTrue[frames], label = 'True', color=mp.colorBeh['AngleVelocity'])
        ax0.plot(avP[frames], label = 'Predicted', color=mp.colorPred['AngleVelocity'])
        plt.xlabel('Frames')
        plt.ylabel('Wave velocity')
        plt.legend()
        
        ax1.plot(pc3[frames], color=mp.colorBeh['Eigenworm3'], label = 'True')
        ax1.plot(tP[frames], color=mp.colorPred['Eigenworm3'], label = 'Predicted')
        plt.xlabel('Frames')
        plt.ylabel('Turns')
        plt.show()
        fig = plt.figure('wiggly centerlines')
        ax = fig.add_subplot(111, adjustable='box', aspect=0.66)

        ax.set_ylim(-400, 400)
        ax.set_xlim(-400, 800)
        plt.tight_layout()
        
        #frames = results['Training']['AngleVelocity']['Test']
        mp.make_animation3(fig, ax, data1= clPred, data2=clApprox +(500,0) , frames = frames, save=True)
        #mp.make_animation2(fig, ax, data1= clApprox +(500,0), data2=clApprox +(500,0) , frames = np.arange(120, 1000), color=mp.UCred[0])
        plt.show()
        
        # show overlay of eigenworms, correlations etc.
        plt.figure('True reconstruction relative to original centerlines')
        for i in range(3):
            plt.subplot(3,1,i+1)
           
            #plt.plot(pcsNew[i], alpha=0.5, label='centerline projected')
            plt.plot(pcs[i], alpha=0.5, label='dataHandler')
            plt.plot(pcsR[i], alpha=0.5, label='true reconstructed')
            plt.legend()
            plt.ylabel('Projection on Eigenworm {}'.format(3-i))
            plt.xlabel('Time (Volumes)')
        plt.tight_layout()
        plt.show()       
        #example centerlines
        i=99
        plt.figure()
        plt.subplot(111, aspect = 'equal')
        plt.plot(clApprox[i,:,0]+30*i,clApprox[i,:,1], '0.5', label = 'Approximated centerline by 4 Eigenworms')
        plt.plot(cl[i,:,0]+30*i,cl[i,:,1], 'r', label = 'Real Centerline')
        plt.plot(clPred[i,:,0]+30*i,clPred[i,:,1], 'b', label = 'Predicted Centerline')
        for i in range(100, 200, 10):
            plt.plot(clApprox[i,:,0]+30*i,clApprox[i,:,1], '0.5')
            plt.plot(cl[i,:,0]+30*i,cl[i,:,1], 'r')
            plt.plot(clPred[i,:,0]+30*i,clPred[i,:,1], 'b')
        plt.legend()
        plt.tight_layout()
        plt.show()
        # 
        cut = 1700
        sz = 3
        a = 0.5
        plt.figure(figsize=(12,7))
        plt.subplot(231)
        plt.scatter(yt[:cut], xt[:cut], label = 'True reconstructed', alpha=a, s = sz)
        plt.scatter(pcs[1][:cut], pcs[2][:cut], label = 'True projected', alpha=a, s = sz)
        plt.ylabel('Projection on Eigenworm 1')
        plt.xlabel('Projection on Eigenworm 2')
        plt.legend()
        plt.subplot(234)
        plt.scatter(yt[cut:], xt[cut:], label = 'True reconstructed', alpha=a, s = sz)
        plt.scatter(pcs[1][cut:], pcs[2][cut:], label = 'True projected', alpha=a, s = sz)
        plt.legend()
        plt.ylabel('Projection on Eigenworm 1')
        plt.xlabel('Projection on Eigenworm 2')
        
        plt.subplot(232)
        plt.scatter(xt[:cut], pcs[2][:cut], label = 'First part R^2 = {:.2f}'.format(explained_variance_score(xt[:cut], pcs[2][:cut])), alpha=a, s = sz, color ='C2' )
        plt.ylabel('Projection on Eigenworm 1')
        plt.xlabel('Reconstruction')
        plt.legend()
        plt.subplot(235)
        plt.scatter(xt[cut:], pcs[2][cut:], label = 'Second part R^2 = {:.2f}'.format(explained_variance_score(xt[cut:], pcs[2][cut:])), alpha=a, s = sz, color ='C2' )
        plt.legend()
        plt.ylabel('Projection on Eigenworm 1')
        plt.xlabel('Reconstruction')
        plt.subplot(233)
        plt.scatter(yt[:cut], pcs[1][:cut], label = 'First part R^2 = {:.2f}'.format(explained_variance_score(yt[:cut], pcs[1][:cut])), alpha=a, s = sz, color ='C3' )
        plt.ylabel('Projection on Eigenworm 2')
        plt.xlabel('Reconstruction')
        plt.legend()
        plt.subplot(236)
        plt.scatter(yt[cut:], pcs[1][cut:], label = 'Second part R^2 = {:.2f}'.format(explained_variance_score(yt[cut:], pcs[1][cut:])), alpha=a, s = sz, color ='C3' )
        plt.legend()
        plt.ylabel('Projection on \n Eigenworm 2')
        plt.xlabel('Reconstruction')
        plt.tight_layout()
        plt.show()
        
    
        plt.figure('Predicted velocity and turns')
        plt.subplot(211)
        plt.plot(avTrue, label = 'True')
        plt.plot(avP, label = 'Predicted')
        plt.legend()
        plt.subplot(212)
        plt.plot(pc3)
        plt.plot(tP)
        plt.tight_layout()
        plt.show()
        
        # show predicted reconstruction
        plt.figure('Predicted Eigenworm reconstruction')
        plt.subplot(311)
        plt.plot(xP,label = 'Prediction reconstructed', alpha=0.5)    
        plt.plot(xt, label = 'True projected', alpha=0.5)  
        plt.ylabel('Projection on Eigenworm 1')
        plt.xlabel('Time (Volumes)')
        plt.subplot(312)
         
        plt.plot(yP,label = 'Prediction reconstructed', alpha=0.5)    
        plt.plot(yt, label = 'True projected', alpha=0.5) 
        plt.ylabel('Projection on Eigenworm 2')
        plt.xlabel('Time (Volumes)')
        plt.subplot(313)
        
        plt.plot(zP, label = 'Prediction reconstructed', alpha=0.5)    
        plt.plot(zt, label = 'True projected', alpha=0.5)
        plt.ylabel('Projection on Eigenworm 3')
        plt.xlabel('Time (Volumes)')
        plt.legend()
        plt.tight_layout()
        # 
        cut = 1700
        sz = 3
        a = 0.5
        plt.figure(figsize=(12,7))
        plt.subplot(231)
        plt.scatter(yt[:cut], xt[:cut], label = 'True reconstructed', alpha=a, s = sz)
        plt.scatter(yP[:cut], xP[:cut], label = 'Predicted', alpha=a, s = sz)
        plt.ylabel('Projection on Eigenworm 1')
        plt.xlabel('Projection on Eigenworm 2')
        plt.legend()
        plt.subplot(234)
        plt.scatter(yt[cut:], xt[cut:], label = 'True reconstructed', alpha=a, s = sz)
        plt.scatter(yP[cut:],xP[cut:], label = 'Predicted', alpha=a, s = sz)
        plt.legend()
        plt.ylabel('Projection on Eigenworm 1')
        plt.xlabel('Projection on Eigenworm 2')
        
        plt.subplot(232)
        plt.scatter(xt[:cut], xP[:cut], label = 'First part R^2 = {:.2f}'.format(explained_variance_score(xt[:cut], xP[:cut])), alpha=a, s = sz, color ='C2' )
        plt.legend()    
        plt.ylabel('Predicted')
        plt.xlabel('Reconstruction')
        plt.subplot(235)
        plt.scatter(xt[cut:], xP[cut:], label = 'Second part R^2 = {:.2f}'.format(explained_variance_score(xt[cut:], xP[cut:])), alpha=a, s = sz, color ='C2' )
        plt.legend()  
        plt.ylabel('Predicted')
        plt.xlabel('Reconstruction')
        plt.subplot(233)
        plt.scatter(yt[:cut], yP[:cut], label = 'First part R^2 = {:.2f}'.format(explained_variance_score(yt[:cut], yP[:cut])), alpha=a, s = sz, color ='C3' )
        plt.legend()   
        plt.ylabel('Predicted')
        plt.xlabel('Reconstruction')
        plt.legend()
        plt.subplot(236)
        plt.scatter(yt[cut:], yP[cut:], label = 'Second part R^2 = {:.2f}'.format(explained_variance_score(yt[cut:],yP[cut:])), alpha=a, s = sz, color ='C3' )
        plt.legend()
        plt.ylabel('Predicted')
        plt.xlabel('Reconstruction')
        plt.tight_layout()
        plt.show()
pca = PCA(n_components=nComp)

pcs = pca.fit_transform(allCenterlines)
newEigenworms = pca.components_

print 'Explained variance with 2 components: ', np.cumsum(
    pca.explained_variance_ratio_)[2]
print 'Explained variance with 4 components: ', np.cumsum(
    pca.explained_variance_ratio_)[3]

# save data as file
#np.savetxt('Eigenworms.dat', newEigenworms)

# plot old Eigenworms for comparison
oldEigenworms = dh.loadEigenBasis(filename='utility/eigenWorms.mat',
                                  nComp=4,
                                  new=False)

plt.figure()
ax4 = plt.subplot(111)
ax4.fill_between(np.arange(0.5, nComp + 0.5),
                 pca.explained_variance_ratio_ * 100,
                 step='post',
                 color='k',
                 alpha=0.75)
#ax4.step(np.arange(1,nComp+1),np.cumsum(results['expVariance'])*100, where = 'pre')
ax4.plot(np.arange(1, nComp + 1),
         np.cumsum(pca.explained_variance_ratio_) * 100,
         'ko-',
         lw=1)
ax4.set_ylabel('Explained variance (%)')
dataPars = {'medianWindow':1, # smooth eigenworms with median filter of that size, must be odd
            'savGolayWindow':13, # savitzky-golay window for angle velocity derivative. must be odd
            'rotate':False, # rotate Eigenworms using previously calculated rotation matrix
            'savGolayWindowGCamp': 5 # savitzky-golay window for red and green channel
            }

folder = "../BrainScanner20170613_134800_MS/"
ewfile = "eigenWorms.mat"
#RMatrix = np.loadtxt(folder+'../'+'Rotationmatrix.dat')
points = dh.loadPoints(folder)
#print points
cl, wc, ep = dh.loadCenterlines(folder)
#cl = dh.loadCenterlines(folder, wormcentered=0)

data = dh.loadData(folder, dataPars)
eigenworms = dh.loadEigenBasis(ewfile)[:3]
print eigenworms.shape
eigenworms = resize(eigenworms, (3,99))

pc1,pc2,pc3 = [data['Behavior']['Eigenworm1'],data['Behavior']['Eigenworm2'],data['Behavior']['Eigenworm3']]
# reverse rotate the Eigenworms
#pcs = np.array(np.dot(-RMatrix, np.vstack([pc1, pc2, pc3]))).T 
# switch pc1 and two--they otherwise are switched everywhere :-(
#IMPORTANT TODO: Change to correct this
pcs = np.vstack([pc3, pc1, pc2]).T
print 'prefactors', pcs.shape
print 'Eigenworms', eigenworms.shape


index = 1000