som_results.py.bak 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. # -*- coding: utf-8 -*-
  2. import eval_sim_data
  3. import os
  4. from sim_data import SimInfo, read_config
  5. from objsimpy.stim.stim_sequence import read_input_file, InputTrace
  6. import spike_file
  7. import numpy as np
  8. import numpy.testing as npt
  9. import pylab as plt
  10. #import pymeas.plot3d
  11. from mpl_toolkits.axes_grid import AxesGrid
  12. import colormaps
  13. from config import SIMDATA_BASE_DIR, MOVIE_DIR
  14. def main():
  15. for i in range(26):
  16. simname = 'taurec_9__2'
  17. data_dir = os.path.join(SIMDATA_BASE_DIR, 'csim', 'simresults', simname)
  18. evaluate_single_sim(data_dir=data_dir,
  19. test_nr=i,
  20. show=False,
  21. reevaluate=False)
  22. def evaluate_single_sim(data_dir=os.path.join(SIMDATA_BASE_DIR, 'csim', 'som02', 'asymgauss__1'),
  23. stim_dir=MOVIE_DIR,
  24. test_nr=30,
  25. reevaluate=False,
  26. evaluate_spikes=False,
  27. show=False,
  28. fig_format='.png',
  29. ):
  30. sim_info = SimInfo(os.path.join(data_dir, "Sim.SimInfo"))
  31. layer_name = "RepresentationLayer"
  32. input_name = "MovieScanInput0"
  33. layer_info = [l for l in sim_info.get_layers() if l.name==layer_name][0]
  34. plt.close("all")
  35. if evaluate_spikes:
  36. spike_file_name = layer_name + "spikes.datTEST"+str(test_nr)+".bin"
  37. input_file_name = input_name + ".inp.datTEST"+str(test_nr)
  38. spikes = spike_file.read(os.path.join(data_dir, spike_file_name))
  39. input = InputTrace(os.path.join(data_dir, input_file_name))
  40. print len(spikes[0])
  41. #max_time = max(spikes[0]) + 1
  42. #n_neurons = max(spikes[1]) + 1
  43. n_neurons = layer_info.LayerDimensions['N']
  44. #spikes = create_dummy_spikes(n_neurons=n_neurons, max_time_ms=max_time)
  45. #return
  46. response_strength_pickle_file = os.path.join(data_dir, spike_file_name + ".response.pickle")
  47. response_strength = eval_sim_data.calc_response_strength2_PICKLED(response_strength_pickle_file,
  48. spikes, input, 430,540, n_neurons=n_neurons, reevaluate=reevaluate)
  49. response_selectivity = calc_response_selectivity(response_strength)
  50. print "response_strength: "
  51. print response_strength.shape
  52. plt.plot(response_strength[:,0])
  53. stim_onsets = input.get_stim_onset()
  54. stim_numbers = [onset[1] for onset in stim_onsets]
  55. if False:
  56. fig = plt.figure()
  57. plt.hist(stim_numbers, bins=range(-2,410))
  58. nx = sim_info.get_inputs()[0].NXPara
  59. ny = sim_info.get_inputs()[0].NYPara
  60. pickle_file = os.path.join(data_dir, spike_file_name +".pref_xy_sel.pickle")
  61. preferred_stimuli_x, preferred_stimuli_y, selectivity = calc_preferred_stimuli_from_response_PICKLED(pickle_file, response_strength, nx, ny, reevaluate=reevaluate)
  62. fig = show_preferred_stimulus_maps(preferred_stimuli_x, preferred_stimuli_y, selectivity, nx, ny)
  63. weight_file = os.path.join(data_dir, "conForward0weights.dat" + str(test_nr))
  64. stim_file = os.path.join(stim_dir, "gauss20x20.idlmov")
  65. #fig = plt.figure()
  66. stim_nx = 20
  67. stim_ny = 20
  68. pickle_file = os.path.join(data_dir, weight_file + ".pref_stim_sel_inc_map" + ".pickle")
  69. fname_stimcorr_pickle = os.path.join(data_dir, weight_file + ".stimcorr" + ".pickle")
  70. pref_x, pref_y, selectivity, inc_sum = eval_sim_data.calc_pref_stim_maps_from_weights_PICKLED(
  71. pickle_file,
  72. weight_file,
  73. stim_file,
  74. stim_nx,
  75. stim_ny,
  76. reevaluate=reevaluate,
  77. fname_stimcorr_pickle=fname_stimcorr_pickle,
  78. stimcorr_reeval=reevaluate)
  79. fig = show_preferred_stimulus_maps(pref_x, pref_y, selectivity, inc_sum, stim_nx, stim_ny)
  80. # patch_sizes_x, patch_sizes_y = eval_sim_data.calc_patch_sizes(weight_file, stim_file, stim_nx, stim_ny)
  81. if False:
  82. show_neuron_vs_stimuli(response_strength)
  83. for x in [10]:
  84. for y in [10]:
  85. pass
  86. show_stimuli_vs_neuron(response_strength, x_0=x, y_0=y)
  87. if False:
  88. show_total_psth(spikes, input)
  89. total_stim_response = response_strength.sum(axis=0)
  90. plt.matshow(total_stim_response.reshape(20,20), cmap=plt.cm.gray)
  91. if False:
  92. fig = plt.figure()
  93. response_to_all_stimuli = response_strength.sum(axis=1)
  94. plt.imshow(response_to_all_stimuli.reshape(100,100), origin='lower', interpolation='nearest', cmap=plt.cm.gray)
  95. if show:
  96. plt.show()
  97. save_prefmap(pref_x, stim_nx, 'preferred stimuli x', os.path.join(data_dir, 'prefmap_x_' + str(test_nr) + fig_format))
  98. save_prefmap(pref_y, stim_ny, 'preferred stimuli y', os.path.join(data_dir, 'prefmap_y_' + str(test_nr) + fig_format))
  99. def create_dummy_spikes(n_neurons=10, max_time_ms=1000, spike_rate_herz=10):
  100. n_spikes = n_neurons * max_time_ms / (1000*spike_rate_herz)
  101. spike_times = round(np.linspace(0, max_time_ms-1, n_spikes))
  102. def calc_response_selectivity(response_strength):
  103. total_mean_response = response_strength.mean()
  104. no_response = response_strength.max(axis=1) < total_mean_response
  105. print "no response n=", no_response.sum()
  106. def show_preferred_stimulus_maps(preferred_stimuli_x,
  107. preferred_stimuli_y,
  108. selectivity=None,
  109. inc_sum=None,
  110. nx=20,
  111. ny=20,
  112. figtext=None):
  113. fig = plt.figure(figsize=(10,11))
  114. if figtext is not None:
  115. plt.figtext(0.5, 0.965, figtext, ha='center', color='black', weight='bold', size='large')
  116. show_preferred_stimuli_x_y(preferred_stimuli_x, preferred_stimuli_y, nx, ny, fig, 421, 422)
  117. if selectivity is not None:
  118. show_selectivity_plot(selectivity, fig, 423)
  119. if inc_sum is not None:
  120. show_incomming_weights_sum_plot(inc_sum, fig, 424)
  121. show_preferred_stimuli_histograms(preferred_stimuli_x, preferred_stimuli_y, nx, ny, fig, 413, 414)
  122. return fig
  123. def save_prefmap(pref, n_stim, title, fname):
  124. plt.figure()
  125. plot_prefmap(pref, n_stim, title)
  126. plt.savefig(fname)
  127. def plot_prefmap(pref, n_stim, title):
  128. circular_cm = colormaps.cmap_color_circle()
  129. norm = plt.matplotlib.colors.Normalize(0, n_stim)
  130. im = plt.imshow(pref, origin='lower', interpolation='nearest', cmap=circular_cm)
  131. im.set_norm(norm)
  132. plt.title(title)
  133. plt.colorbar()
  134. def print_y_hist(preferred_stimuli_y):
  135. y_hist = {}
  136. for y in preferred_stimuli_y.flat:
  137. if not y in y_hist:
  138. y_hist[y] = 1
  139. else:
  140. y_hist[y] += 1
  141. for y in y_hist:
  142. print "y = " + str(y) + " h=" + str(y_hist[y])
  143. def show_preferred_stimuli_x_y(preferred_stimuli_x, preferred_stimuli_y, nx, ny, fig, pos_x, pos_y):
  144. fig.canvas.set_window_title("preferred stimuli")
  145. fig.add_subplot(pos_x)
  146. plot_prefmap(preferred_stimuli_x, nx, "preferred stimuli x")
  147. fig.add_subplot(pos_y)
  148. plot_prefmap(preferred_stimuli_y, ny, "preferred stimuli y")
  149. def show_preferred_stimuli_histograms(preferred_stimuli_x, preferred_stimuli_y, nx, ny, fig, pos_x, pos_y):
  150. sub = fig.add_subplot(pos_x)
  151. plt.hist(preferred_stimuli_x.flat, bins=np.arange(-2,20)+0.5, facecolor='green', alpha=0.75)
  152. plt.title("x histogram")
  153. plt.xlim([-1,nx])
  154. sub = fig.add_subplot(pos_y)
  155. plt.hist(preferred_stimuli_y.flat, bins=np.arange(-2,20)+0.5, facecolor='blue', alpha=0.75)
  156. plt.title("y histogram")
  157. plt.xlim([-1,ny])
  158. def show_selectivity_plot(selectivity, fig, pos):
  159. sub = fig.add_subplot(pos)
  160. im_selectivity = plt.imshow(selectivity, origin='lower', interpolation='nearest', cmap=plt.cm.gray)
  161. plt.title("selectivity")
  162. norm_0_1 = plt.matplotlib.colors.Normalize(0, 1)
  163. im_selectivity.set_norm(norm_0_1)
  164. plt.colorbar()
  165. def show_incomming_weights_sum_plot(inc_sum, fig, pos):
  166. sub = fig.add_subplot(pos)
  167. im_inc_sum = plt.imshow(inc_sum, origin='lower', interpolation='nearest', cmap=plt.cm.gray)
  168. plt.title("incomming weight sum")
  169. plt.colorbar()
  170. def show_neuron_vs_stimuli(response_strength, n_x=5, n_y=5, x_0=0, y_0=0):
  171. columns = range(n_x)
  172. rows = range(n_y)
  173. fig = plt.figure(figsize=(n_x, n_y))
  174. grid = AxesGrid(fig, 111, nrows_ncols = (n_x, n_y), axes_pad=0.1)
  175. title = "neuron_vs_stimuli x0=" + str(x_0) + " y0=" + str(y_0)
  176. fig.canvas.set_window_title(title)
  177. for x in columns:
  178. for y in rows:
  179. neuron_nr = (x_0 + x) + 100*(y_0 + y)
  180. sgl_response_map = response_strength[neuron_nr,:].reshape(20,20)
  181. grid[x+n_x*y].matshow(sgl_response_map, cmap=plt.cm.gray)
  182. def show_stimuli_vs_neuron(response_strength, n_x=5, n_y=5, x_0=0, y_0=0):
  183. columns = range(n_x)
  184. rows = range(n_y)
  185. show_neuron_vs_stimuli(response_strength)
  186. fig = plt.figure()
  187. grid = AxesGrid(fig, 111, nrows_ncols = (n_x, n_y), axes_pad=0.1)
  188. title = "x0="+str(x_0)+", y0="+str(y_0)
  189. fig.canvas.set_window_title(title)
  190. for x in columns:
  191. for y in rows:
  192. stim_nr = x_0 + x + 20 * (y_0 + y)
  193. sgl_stim_response = response_strength[:,stim_nr].reshape(100,100)
  194. grid[x+n_x*y].matshow(sgl_stim_response, cmap=plt.cm.gray)
  195. def show_total_psth(spikes, input):
  196. total_psth = eval_sim_data.calc_total_psth(spikes, input, 1, 1000)
  197. fig = plt.figure()
  198. plt.plot(total_psth, 'o-')
  199. def show_pref_stim_maps_from_weights(weight_file, stim_file, fig, pos1, pos2):
  200. stim_nx = 20
  201. stim_ny = 20
  202. pref_x, pref_y, selectivity = eval_sim_data.calc_pref_stim_maps_from_weights(weight_file, stim_file, stim_nx, stim_ny)
  203. show_preferred_stimuli_x_y(pref_x, pref_y, stim_nx, stim_ny, fig, pos1, pos2)
  204. if __name__=="__main__":
  205. main()