Ejemplo n.º 1
0
elif typ == 'AML18':
    folder = "AML18_moving/{}_MS/"
    dataLog = "AML18_moving/AML18_datasets.txt"
    outLoc = "AML18_moving/Analysis/"
# immobile GCamp6
elif typ == 'AML32imm':
    folder = "AML32_immobilized/{}_MS/"
    dataLog = "AML32_immobilized/AML32_immobilized_datasets.txt"

# immobile GCamp6 -lite-1
elif typ == 'AML70imm':
    folder = "AML70_immobilized/{}_MS/"
    dataLog = "AML70_immobilized/AML70imm_datasets.txt"

# data parameters
dataPars = {
    'medianWindow':
    5,  # smooth eigenworms with gauss filter of that size, must be odd
    'gaussWindow':
    10,  # 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
}

dataSets = dh.loadMultipleDatasets(dataLog,
                                   pathTemplate=folder,
                                   dataPars=dataPars,
                                   nDatasets=None)
keyListAll = np.sort(dataSets.keys())
print keyListAll
Ejemplo n.º 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()
    dataLog = "AML32_moving/AML32_datasets.txt"
    outLoc = "AML32_moving/Analysis/"
##### GFP
elif typ == 'AML18':
    folder = "AML18_moving/{}_MS/"
    dataLog = "AML18_moving/AML18_datasets.txt"
    outLoc = "AML18_moving/Analysis/"
# output is stored here
elif typ == 'AML32imm':
    folder = "AML32_immobilized/{}_MS/"
    dataLog = "AML32_immobilized/AML32_immobilized_datasets.txt"  #loc = 'AML18_moving/'
#folder = os.path.join(loc,"{}_MS/")
#dataLog = os.path.join(loc,"AML18_datasets.txt")
## output is stored here
outfile = os.path.join(outLoc, "../Rotationmatrix.dat")
dataSets = dh.loadMultipleDatasets(dataLog, folder, dataPars)
nWorms = len(dataSets)
overwrite = True  #False # if True fits new rotation matrix and overwrites old one!
###############################
# concatenate Eigenworms from all datasets -- create new rotation matrix
###############################
if overwrite:
    xS, yS, zS = [], [], []
    for kindex, key in enumerate(dataSets.keys()):
        data = dataSets[key]
        xtmp, ytmp, ztmp = data['Behavior']['Eigenworm1'], data['Behavior'][
            'Eigenworm2'], data['Behavior']['Eigenworm3']
        xS.append(xtmp)
        yS.append(ytmp)
        zS.append(ztmp)
#
#    load data into dictionary
#
##############################################
#folder = "SelectDatasets/BrainScanner20170610_105634_linkcopy/"
#folder = "/home/monika/Dropbox/Work/BehaviorPrediction/PredictionCode/SelectDatasets/{}_linkcopy/"
#dataLog = "/home/monika/Dropbox/Work/BehaviorPrediction/PredictionCode/SelectDatasets/description.txt"
folder = "AML32_moving/{}_MS/"
dataLog = "AML32_moving/AML32_datasets.txt"
##### GFP
#folder = "AML18_moving/{}_MS/"
#dataLog = "AML18_moving/AML18_datasets.txt"
# output is stored here
outLoc = "AML32_moving/Analysis/"

dataSets = dh.loadMultipleDatasets(dataLog, pathTemplate=folder)
keyList = np.sort(dataSets.keys())
nWorms = len(keyList)
# results dictionary
resultDict = {}
for kindex, key in enumerate(keyList):
    resultDict[key] = {}
# analysis parameters

pars = {
    'nCompPCA': 10,  # no of PCA components
    'PCAtimewarp': True,  #timewarp so behaviors are equally represented
    'trainingCut': 0.6,  # what fraction of data to use for training 
    'trainingType':
    'middle',  # simple, random or middle.select random or consecutive data for training. Middle is a testset in the middle
    'linReg': 'simple',  # ordinary or ransac least squares