Esempio n. 1
0
def compareReconstructedWorms(cl, eigenworms, avTrue, thetaTrue, pc3, avP, tP,
                              tPnew):
    pcsNew, meanAngle, lengths, refPoint = dh.calculateEigenwormsFromCL(
        cl, eigenworms)
    pc3New, pc2New, pc1New = pcsNew
    cl2 = dh.calculateCLfromEW(pcsNew, eigenworms, meanAngle, lengths,
                               refPoint)
    cl = cl[:, :-1, :]
    cl = ndimage.gaussian_filter1d(cl, 5, 1)
    # transform eigenworms exactly the same way. Otherwise we get some artefacts from nans
    r = (pcsNew[2]**2 + pcsNew[1]**2)
    r = np.repeat(np.median(r), len(r))
    #lengths = 5
    #=============================================================================#
    # 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 = dh.recrWorm(avTrue, pc3, thetaTrue, r=r, show=False)
    pcsR = np.vstack([zt, yt, xt])
    clApprox = dh.calculateCLfromEW(pcsR, eigenworms, meanAngle, lengths,
                                    refPoint)
    #=============================================================================#
    # reconstruct worm from predicted angular velocity.
    #=============================================================================#
    xP, yP, zP = dh.recrWorm(avP, tP, thetaTrue, r=r)
    pcsP = np.vstack([zP, yP, xP])
    clPred = dh.calculateCLfromEW(pcsP, eigenworms, meanAngle, lengths,
                                  refPoint)
    #=============================================================================#
    # reconstruct worm from non-linear corrected turns
    #=============================================================================#
    xP, yP, zP = dh.recrWorm(avP, tPnew, thetaTrue, r=r)
    pcsP = np.vstack([zP, yP, xP])
    cl3 = dh.calculateCLfromEW(pcsP, eigenworms, meanAngle, lengths, refPoint)
    #=============================================================================#
    # reconstruct worm from non-linear corrected turns
    #=============================================================================#
    # center around midpoint
    originalCMS = np.tile(np.mean(cl2, axis=1)[:, np.newaxis, :], (1, 99, 1))
    clApprox -= originalCMS
    cl -= originalCMS
    cl2 -= originalCMS
    clPred -= originalCMS
    cl3 -= originalCMS
    return cl, cl2, clPred, cl3
for i in range(4):
    plt.subplot(5, 1, i + 2)
    plt.plot(oldEigenworms[i], label='old Eigenworms')
    plt.plot(newEigenworms[i], label='new Eigenworms')
plt.legend()
plt.show()
cl = []
# load a centerline dataset
for lindex, line in enumerate(np.loadtxt(dataLog, dtype=str, ndmin=2)[:2]):
    folder = ''.join([pathTemplate, line[0], '_MS'])
    # get all centerlines, alrady relative angles 'wormcentered' and mean angle subtracted
    #cl = dh.loadCenterlines(folder, full = True)[0]
    cl.append(dh.loadCenterlines(folder, full=True)[0])
cl = np.concatenate(cl)
# project a dataset on old and new Eigenworms
pcsOld, meanAngle, lengths, refPoint = dh.calculateEigenwormsFromCL(
    cl, oldEigenworms)
#RMatrix = np.loadtxt(folder+'../'+'Rotationmatrix.dat')
#pcs = np.array(np.dot(RMatrix, np.vstack([pcsOld[2],pcsOld[1], pcsOld[0]])))
pcs = [pcsOld[1], pcsOld[0], pcsOld[2]]
pcsNew, meanAngle, lengths, refPoint = dh.calculateEigenwormsFromCL(
    cl, newEigenworms)
pcsNew = pcsNew[[2, 1, 0]]

print np.mean(meanAngle), np.mean(lengths), refPoint[0]
# calculate base shapes of old and new eigenworms
for i in range(3):
    pcs = np.zeros(3)
    pcs[i] = 10
    clOld = dh.calculateCLfromEW(pcs, oldEigenworms[:3], meanAngle, lengths,
                                 refPoint)[0]
    clNew = dh.calculateCLfromEW(pcs, newEigenworms[:3], meanAngle, lengths,
Esempio n. 3
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()
Esempio n. 4
0
ax6 = plt.subplot(gsEWs[2,0],  adjustable='box')
ax6.axis('equal')
# plot eigenworms
eigenworms = dh.loadEigenBasis(filename = 'utility/Eigenworms.dat', nComp=3, new=True)
lengths = np.array([4])
meanAngle = np.array([-0.07945])
refPoint = np.array([[0,0]])
prefactor = [r'',r'$=a_1$ *',r'+ $a_2$ *',r'+ $a_3$ *' ][::-1]
descr = ['posture', 'undulation', 'undulation', 'turn'][::-1]
colors = [B1, B1, R1]
# from which volume to show the posture
tindex = 500
for i in range(4):
    pcs = np.zeros(3)
    if i ==3:
        pcsNew, meanAngle, _, refPoint = dh.calculateEigenwormsFromCL(moving['CL'], eigenworms)
        pc3New, pc2New, pc1New = pcsNew
        cl = dh.calculateCLfromEW(pcsNew, eigenworms, meanAngle, lengths, refPoint)
        
        new = createWorm(cl[tindex,:,0], cl[tindex,:,1])
    else:
        pcs[i] = 10
        clNew = dh.calculateCLfromEW(pcs, eigenworms[:3], meanAngle, lengths, refPoint)[0]
        new = createWorm(clNew[:,0], clNew[:,1])
    
    new -=np.mean(new, axis=0)
    new[:,0] -= i*600 - 800
    x, y = np.mean(new, axis =0)
    p2 = mpl.patches.Polygon(new, closed=True, fc=N0, ec='none')
    plt.text(x,y-200, descr[i], fontsize=12, horizontalalignment = 'center')
    plt.text(x-250,y, prefactor[i], fontsize=12, horizontalalignment = 'center')
Esempio n. 5
0
                                   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']
#=============================================================================#
# for debugging recreate an existing, approximated shape from 3 eigenworms
#=============================================================================#
pcsNew, meanAngle, lengths, refPoint = dh.calculateEigenwormsFromCL(
    cl, eigenworms)
pc3New, pc2New, pc1New = pcsNew
cl2 = dh.calculateCLfromEW(pcsNew, eigenworms, meanAngle, lengths, refPoint)
# transform eigenworms exactly the same way. Otherwise we get some artefacts from nans
r = (pcsNew[2]**2 + pcsNew[1]**2)
r = np.repeat(np.mean((pcsNew[2]**2 + pcsNew[1]**2)), len(pcsNew[0]))

#=============================================================================#
# 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 = dh.recrWorm(avTrue, pc3, thetaTrue, r=r, show=False)
pcsR = np.vstack([zt, yt, xt])
clApprox = dh.calculateCLfromEW(pcsR, eigenworms, meanAngle, lengths, refPoint)
#=============================================================================#
# load predicted worm
#=============================================================================#