In [1]:
from matplotlib.mlab import PCA as mlabPCA
import h5py
from pylab import*
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d import proj3d

params = { 'figure.figsize' : [20, 20],
          'figure.dpi:' : 150,
          'figure.autolayout:' : True,
          'text.usetex' : True,
          'font.size' : 11,
          'font.family' : 'lmodern',
          'text.latex.unicode': True,
          'legend.fontsize' : 'medium',
          'legend.handlelength' : 2.2,
          'lines.linewidth' : 0.7,
          }
plt.rcParams.update(params)

In [2]:
h5_file = h5py.File("data/koendata.h5", 'r')

dataset_A = h5_file.get("/an0074")
dataset_B = h5_file.get("/an0075")

valueMatrices_A = []
valueMatrices_B = []

for day in dataset_A:
    group = day + "/valueMatrix"
    valueMatrices_A.append(dataset_A.get(group))
    
    
for day in dataset_B:
    group = day + "/valueMatrix"
    valueMatrices_B.append(dataset_B.get(group))

In [3]:
for index, matrix in enumerate(valueMatrices_A):
   if any(isnan(matrix)):
        print matrix.name
        print valueMatrices_A[index].shape
        badData = array(unique(where(isnan(matrix))[0]))
        matrix = delete(matrix, badData,0)
        valueMatrices_A[index] = matrix
        
        print valueMatrices_A[index].shape
        

for index, matrix in enumerate(valueMatrices_B):
   if any(isnan(matrix)):
        print matrix.name
        print valueMatrices_B[index].shape
        badData = array(unique(where(isnan(matrix))[0]))
        matrix = delete(matrix, badData,0)
        valueMatrices_B[index] = matrix
        print valueMatrices_B[index].shape


/an0074/2013_09_24/valueMatrix
(18987, 46)
(18824, 46)
/an0075/2013_10_01/valueMatrix
(28697, 50)
(28615, 50)

In [4]:
fig = plt.figure()
ax1 = fig.add_subplot(111, projection='3d')

day = 0
for matrix in valueMatrices_A:
    matrix = array(matrix)
    mlab_pca = mlabPCA(matrix)
    day +=1 
    
    ax1.plot(mlab_pca.Y[:,0],mlab_pca.Y[:,1],mlab_pca.Y[:,2], 'o', markersize=7, alpha=0.5, label = 'day: ' + str(day))
    ax1.legend()

fig = plt.figure()
ax2 = fig.add_subplot(111, projection='3d')
day = 0
for matrix in valueMatrices_B:
    matrix = array(matrix)
    mlab_pca = mlabPCA(matrix)
    day +=1 
    ax2.plot(mlab_pca.Y[:,0],mlab_pca.Y[:,1],mlab_pca.Y[:,2],  'o', markersize=7, alpha=0.5, label = 'day: ' + str(day))
    ax2.legend()



In [5]:
#%pylab qt
fig = plt.figure()
title('Animal A')
day = 0
for matrix in valueMatrices_A:
    matrix = array(matrix)
    mlab_pca = mlabPCA(matrix)
    day +=1
    ax= fig.add_subplot(3,5,day, projection='3d')
    ax.plot(mlab_pca.Y[:,0],mlab_pca.Y[:,1],mlab_pca.Y[:,2], 'o', markersize=7, alpha=0.5, label = 'day: ' + str(day))
    ax.legend()
    ax.set_xlim([-20., 20.])
    ax.set_ylim([-20., 20.])
    ax.set_zlim([-10., 10.])



In [6]:
dayIndex = zeros(len(valueMatrices_A))
allValueMatrices_A = valueMatrices_A[0]
dayIndex[1] = valueMatrices_A[0].shape[0]


for i in range(1, len(valueMatrices_A)-1,1):
    dayIndex[i+1]=  dayIndex[i] + valueMatrices_A[i].shape[0]
    allValueMatrices_A = concatenate((allValueMatrices_A, valueMatrices_A[i]))
   
#%pylab qt
fig = plt.figure()
title(' Join PCA- Animal A')

day = 0
mlab_pca = mlabPCA(allValueMatrices_A)

for i in range(len(dayIndex)-1):
    day +=1
    ax= fig.add_subplot(2,5,day, projection='3d')
    ax.plot(mlab_pca.Y[dayIndex[i]:dayIndex[i+1]:20,0], 
            mlab_pca.Y[dayIndex[i]:dayIndex[i+1]:20,1],
            mlab_pca.Y[dayIndex[i]:dayIndex[i+1]:20,2], 
            'o', markersize=7, alpha=0.5, label = 'day: ' + str(day))
    ax.legend()
    ax.set_xlim([-20., 20.])
    ax.set_ylim([-20., 20.])
    ax.set_zlim([-10., 10.])



In [7]:
dayIndex = zeros(len(valueMatrices_B))
allValueMatrices_B = valueMatrices_B[0]
dayIndex[1] = valueMatrices_B[0].shape[0]


for i in range(1, len(valueMatrices_B)-2):
    dayIndex[i+1]=  dayIndex[i] + valueMatrices_B[i].shape[0]
    allValueMatrices_B = concatenate((allValueMatrices_B, valueMatrices_B[i]))
    
#%pylab qt
fig = plt.figure()
title(' Join PCA- Animal B')

day = 0
mlab_pca = mlabPCA(allValueMatrices_B)

for i in range(len(dayIndex)-2):
    day +=1
    ax= fig.add_subplot(2,5,day, projection='3d')
    ax.plot(mlab_pca.Y[dayIndex[i]:dayIndex[i+1],0], 
            mlab_pca.Y[dayIndex[i]:dayIndex[i+1],1],
            mlab_pca.Y[dayIndex[i]:dayIndex[i+1],2], 
            'o', markersize=7, alpha=0.5, label = 'day: ' + str(day))
    ax.legend()
    ax.set_xlim([-20., 20.])
    ax.set_ylim([-20., 20.])
    ax.set_zlim([-10., 10.])



In [8]:
#%pylab qt
fig = plt.figure()
mlab_pca = mlabPCA(allValueMatrices_A)
ax = fig.add_subplot(111, projection='3d')
day = 0

for i in range(len(dayIndex)-1):
    day +=1 
    ax.plot(mlab_pca.Y[dayIndex[i]:dayIndex[i+1]:20,0], 
            mlab_pca.Y[dayIndex[i]:dayIndex[i+1]:20,1],
            mlab_pca.Y[dayIndex[i]:dayIndex[i+1]:20,2], 
            'o', markersize=7, alpha=0.1, label = 'day: ' + str(day))
    ax.legend()
    ax.set_xlabel('PC1')
    ax.set_ylabel('PC2')
    ax.set_zlabel('PC3')
    ax.set_xlim([-20., 20.])
    ax.set_ylim([-20., 20.])
    ax.set_zlim([-10., 10.])



In [9]:
#%pylab qt
fig = plt.figure()
mlab_pca = mlabPCA(allValueMatrices_B)
ax = fig.add_subplot(111, projection='3d')
day = 0

for i in range(len(dayIndex)-1):
    day +=1 
    ax.plot(mlab_pca.Y[dayIndex[i]:dayIndex[i+1]:20,0], 
            mlab_pca.Y[dayIndex[i]:dayIndex[i+1]:20,1],
            mlab_pca.Y[dayIndex[i]:dayIndex[i+1]:20,2], 
            'o', markersize=7, alpha=0.5, label = 'day: ' + str(day))
    ax.legend()
    ax.set_xlabel('PC1')
    ax.set_ylabel('PC2')
    ax.set_zlabel('PC3')
    ax.set_xlim([-20., 20.])
    ax.set_ylim([-20., 20.])
    ax.set_zlim([-10., 10.])



In [9]: