#
# grab all the data we will need
#
################################################

data = {}
for typ in ['AML32', 'AML18', 'AML175', 'AML70']:
    for condition in ['moving', 'chip']:  # ['moving', 'immobilized', 'chip']:
        folder = '{}_{}/'.format(typ, condition)
        dataLog = '{0}_{1}/{0}_{1}_datasets.txt'.format(typ, condition)
        outLoc = "Analysis/{}_{}_results.hdf5".format(typ, condition)
        outLocData = "Analysis/{}_{}.hdf5".format(typ, condition)

        try:
            # load multiple datasets
            dataSets = dh.loadDictFromHDF(outLocData)
            keyList = np.sort(dataSets.keys())
            results = dh.loadDictFromHDF(outLoc)
            # store in dictionary by typ and condition
            key = '{}_{}'.format(typ, condition)
            data[key] = {}
            data[key]['dsets'] = keyList
            data[key]['input'] = dataSets
            data[key]['analysis'] = results
        except IOError:
            print typ, condition, 'not found.'
            pass
print 'Done reading data.'

################################################
#
示例#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()