1. #!/usr/bin/env python
  2. from __future__ import division
  3. __author__ = 'Horea Christian'
  4. from matplotlib.ticker import MultipleLocator, FuncFormatter, FormatStrFormatter
  5. import mdp
  6. import matplotlib.pyplot
  7. import numpy as np
  8. from loadsums import pca_loadsum
  9. from pylab import figure, xlabel, ylabel, show, title, legend
  10. from load_vdaq_conds import data_type, bytes_per_pixel
  11. from get_data import data_name, lenheader, nframesperstim, framewidth, frameheight
  12. nframesperstim = np.array(nframesperstim)
  13. stim_start = 20
  14. stim_length = 5
  15. outfile = data_name + "-pca(80, 8).npy"
  16. timepts = np.array(nframesperstim)
  17. img_range_zero = np.arange(0, nframesperstim)
  18. img_range_one = np.arange(nframesperstim, nframesperstim * 2)
  19. fimg_pca_one = pca_loadsum(data_name, img_range_one, True, data_type, framewidth, frameheight, lenheader, bytes_per_pixel)
  20. fimg_pca_zero = pca_loadsum(data_name, img_range_zero, True, data_type, framewidth, frameheight, lenheader, bytes_per_pixel)
  21. fimg_pca = fimg_pca_one / fimg_pca_zero
  22. #fimg_pca_zero_norm = (fimg_pca_zero - np.mean(fimg_pca_zero)) / np.std(fimg_pca_zero)
  23. #result = matplotlib.mlab.PCA(fimg_pca).project(fimg_pca)
  24. #result = mdp.fastica(fimg_pca, white_comp=6)
  25. inp = fimg_pca.T # where fimg_pca is 120x(504^2)
  26. pca = mdp.nodes.PCANode(output_dim=0.97, svd=True, reduce=True) # keep only 97% of the input variance
  27. pca.train(inp)
  28. pca.stop_training()
  29. result = pca(inp)
  30. #result = np.load(outfile)
  31. result_img = pca.execute(inp,0)
  32. print np.shape(result)
  33. print np.shape(result[:,0])
  34. print result[:,0]
  35. print np.sqrt(len(result[:,0]))
  36. print np.shape(result[0])
  37. print np.sqrt(len(result[0]))
  38. print np.shape(result_img)
  39. print result_img
  40. outfile = data_name + "-pca" + str(np.shape(result)) + ".npy"
  41. np.save(outfile, result)
  42. fig = figure(facecolor='#eeeeee')
  43. x0=fig.add_subplot(111)
  44. x0.plot(result[:,0], 'k', alpha = 0.12, label="Component 1")
  45. x0.plot(result[:,1], 'r', alpha = 0.7, label="Component 2")
  46. x0.plot(result[:,2], 'g', alpha = 0.7, label="Component 3")
  47. #x0.plot(result[:,3], 'b', alpha = 0.7, label="Component 4")
  48. #x0.plot(result[:,4], 'c', alpha = 0.7, label="Component 5")
  49. #x0.plot(result[:,0] - result[:,1], 'g', alpha = 0.7, label="Component 2/Component 1")
  50. #x0.hlines(y=min(result), xmin=0, xmax = stim_start, colors = '#96D0FF', label="Baseline")
  51. #x0.hlines(y=min(result), xmin=stim_start, xmax = stim_start+stim_length, colors = '#0F23FF', label="Stimulus")
  52. #x0.axhline(y=0, color = 'k', alpha = 0.12)
  53. ylabel('R/R$_{baseline}$', fontsize='12')
  54. xlabel('Time [s]', fontsize='12')
  55. x0.xaxis.set_major_formatter(FuncFormatter(lambda x,i: "%.0f" % (x/10)))
  56. x0.xaxis.set_minor_locator(MultipleLocator(5))
  57. x0.yaxis.set_major_formatter(FormatStrFormatter('%.5f'))
  58. legend(bbox_to_anchor=(0., 1.02), loc=3, ncol=5, mode="expand", borderaxespad=0.)
  59. x0.set_xlim(0, timepts-1)
  60. #C = result[:,0]
  61. #height = np.sqrt(len(C))
  62. #a = np.reshape(C,(height,height))
  63. #x0 = fig.add_subplot(3,3,1)
  64. #cx0 = x0.imshow(a, interpolation='bilinear', origin='upper', extent=None)
  65. #cbar = fig.colorbar(cx0)
  66. #cbar.set_label('Reflectance change')
  67. #cbar.ax.tick_params(direction='out')
  68. #
  69. #C = result[:,1]
  70. #a = np.reshape(C,(height,height))
  71. #x0 = fig.add_subplot(3,3,2)
  72. #cx0 = x0.imshow(a, interpolation='bilinear', origin='upper', extent=None)
  73. #cbar = fig.colorbar(cx0)
  74. #cbar.set_label('Reflectance change')
  75. #cbar.ax.tick_params(direction='out')
  76. #
  77. #C = result[:,2]
  78. #a = np.reshape(C,(height,height))
  79. #x0 = fig.add_subplot(3,3,3)
  80. #cx0 = x0.imshow(a, interpolation='bilinear', origin='upper', extent=None)
  81. #cbar = fig.colorbar(cx0)
  82. #cbar.set_label('Reflectance change')
  83. #cbar.ax.tick_params(direction='out')
  84. #C = result[:,3]
  85. #a = np.reshape(C,(height,height))
  86. #x0 = fig.add_subplot(3,3,4)
  87. #cx0 = x0.imshow(a, interpolation='bilinear', origin='upper', extent=None)
  88. #cbar = fig.colorbar(cx0)
  89. #cbar.set_label('Reflectance change')
  90. #cbar.ax.tick_params(direction='out')
  91. #
  92. #C = result[:,4]
  93. #a = np.reshape(C,(height,height))
  94. #x0 = fig.add_subplot(3,3,5)
  95. #cx0 = x0.imshow(a, interpolation='bilinear', origin='upper', extent=None)
  96. #cbar = fig.colorbar(cx0)
  97. #cbar.set_label('Reflectance change')
  98. #cbar.ax.tick_params(direction='out')
  99. #
  100. #C = result[:,5]
  101. #a = np.reshape(C,(height,height))
  102. #x0 = fig.add_subplot(3,3,6)
  103. #cx0 = x0.imshow(a, interpolation='bilinear', origin='upper', extent=None)
  104. #cbar = fig.colorbar(cx0)
  105. #cbar.set_label('Reflectance change')
  106. #cbar.ax.tick_params(direction='out')
  107. #
  108. #C = result[:,6]
  109. #a = np.reshape(C,(height,height))
  110. #x0 = fig.add_subplot(3,3,7)
  111. #cx0 = x0.imshow(a, interpolation='bilinear', origin='upper', extent=None)
  112. #cbar = fig.colorbar(cx0)
  113. #cbar.set_label('Reflectance change')
  114. #cbar.ax.tick_params(direction='out')
  115. #
  116. #C = result[:,7]
  117. #a = np.reshape(C,(height,height))
  118. #x0 = fig.add_subplot(3,3,8)
  119. #cx0 = x0.imshow(a, interpolation='bilinear', origin='upper', extent=None)
  120. #cbar = fig.colorbar(cx0)
  121. #cbar.set_label('Reflectance change')
  122. #cbar.ax.tick_params(direction='out')
  123. #
  124. #C = result[:,8]
  125. #a = np.reshape(C,(height,height))
  126. #x0 = fig.add_subplot(3,3,9)
  127. #cx0 = x0.imshow(a, interpolation='bilinear', origin='upper', extent=None)
  128. #cbar = fig.colorbar(cx0)
  129. #cbar.set_label('Reflectance change')
  130. #cbar.ax.tick_params(direction='out')
  131. show()