def plotHeatmap(dataPath, gaussianDistances, ax1=None): b.rcParams['font.size'] = 20 if ax1==None: b.figure(figsize=(8,15)) else: b.sca(ax1) nE = 1600 averagingWindowSize = 32 spikeCount = np.zeros((len(gaussianDistances), nE)) for i,dist in enumerate(gaussianDistances[:]): path = dataPath + '/dist'+str(dist)+'/' +'activity/' spikeCountTemp = np.load(path + 'spikeCountPerExample.npy') spikeCount[i,:] = spikeCountTemp[25,:,0]#np.loadtxt(path + 'spikeCountAe.txt') # spikeCount[i,:] = np.roll(spikeCount[i,:], int(0.25*len(spikeCount[i,:]))) spikeCount[i,:] = movingaverage(spikeCount[i,:], averagingWindowSize) spikeCount[i,:] /= np.max(spikeCount[i,:]) b.imshow(spikeCount[:,:], aspect='auto', extent=[0,1,2,0]) b.colorbar() b.xlabel('Neuron number (resorted)') b.xlabel('Neuron number (resorted)') if ax1==None: b.savefig(dataPath + '/multipleAnglesHeatmap.png', dpi=300, bbox_inches='tight')
def plotActivity(dataPath, gaussianDistances, ax1=None): averagingWindowSize = 32 if ax1==None: b.figure() else: b.sca(ax1) linewidth = 2 for i,dist in enumerate(gaussianDistances[:]): path = dataPath + '/dist'+str(dist)+'/' +'activity/' spikeCountTemp = np.load(path + 'spikeCountPerExample.npy') spikeCount = spikeCountTemp[25,:,0]#np.loadtxt(path + 'spikeCountAe.txt') # spikeCount = np.roll(spikeCount, 400) inputSpikeCount = np.loadtxt(path + 'spikeCountXe.txt') spikeCount = movingaverage(spikeCount,averagingWindowSize) inputSpikeCount = movingaverage(inputSpikeCount,averagingWindowSize) if i==len(gaussianDistances)-1: b.plot(spikeCount, 'b', alpha=0.6, linewidth=linewidth, label='Max. dist output')#alpha=0.5+(0.5*float(i)/float(len(lower_peaks))), b.plot(inputSpikeCount, 'r', alpha=1., linewidth=linewidth, label='Max. dist. input') elif i==0: b.plot(spikeCount, 'k--', alpha=0.6, linewidth=linewidth, label='Min. dist output') b.plot(inputSpikeCount, 'r--', alpha=1., linewidth=linewidth, label='Min. dist. input') else: b.plot(spikeCount, 'k', alpha=0.2+(0.4*float(i)/float(len(gaussianDistances))), linewidth=0.6) b.plot(inputSpikeCount, 'r', alpha=0.2+(0.4*float(i)/float(len(gaussianDistances))), linewidth=0.6) b.ylim(0,35) # b.title('spikes:' + str(sum(spikeCount)) + ', pop. value: ' + str(computePopVector(spikeCount))) if ax1==None: b.legend(loc='upper left', fancybox=True, framealpha=0.0) b.savefig(dataPath + '/multipleAnglesActivity.png', dpi=300)
def plotSingleTuningCurve(dataPath, gaussianDistance, ax1=None): b.rcParams['font.size'] = 20 averagingWindowSize = 1 nE = 1600 ymax = 1 if ax1==None: b.figure(figsize=(8,6.5)) fig_axis = b.subplot(1,1,1) else: fig_axis = ax1 b.sca(ax1) path = dataPath + '/dist'+str(gaussianDistance)+'/' +'activity/' spikeCount = np.load(path + 'spikeCountPerExample.npy') spikeCountSingleNeuron = (spikeCount[:,nE/2-2,0]) numMeasurements = len(spikeCount[:,0,0]) spikeCount = movingaverage(spikeCountSingleNeuron,averagingWindowSize) spikeCount /= np.max(spikeCountSingleNeuron) b.plot(spikeCount, color='deepskyblue', marker='o', alpha=0.6, linewidth=0, label='Output')#alpha=0.5+(0.5*float(i)/float(len(lower_peaks))), fig_axis.set_xticks([0., numMeasurements/2, numMeasurements]) fig_axis.set_xticklabels(['0', '0.5', '1']) fig_axis.set_yticks([0., ymax/2, ymax]) fig_axis.spines['top'].set_visible(False) fig_axis.spines['right'].set_visible(False) fig_axis.get_xaxis().tick_bottom() fig_axis.get_yaxis().tick_left() b.ylabel('Normalized response') b.ylim(0,ymax+0.05) # b.title('spikes:' + str(sum(spikeCount)) + ', pop. value: ' + str(computePopVector(spikeCount))) if ax1==None: b.legend(loc='upper left', fancybox=True, framealpha=0.0) b.savefig(dataPath + '/tuningCurveSingleNeuron.png', dpi=300, bbox_inches='tight')
def plotActivity(dataPath, lower_peaks, ax1=None): averagingWindowSize = 32 if ax1==None: b.figure() else: b.sca(ax1) for i,gaussian_peak_low in enumerate(lower_peaks[:]): path = dataPath + '/peak_'+str(gaussian_peak_low)+'/' +'activity/' spikeCount = np.loadtxt(path + 'spikeCountAe.txt') inputSpikeCount = np.loadtxt(path + 'spikeCountXe.txt') spikeCount = movingaverage(spikeCount,averagingWindowSize) inputSpikeCount = movingaverage(inputSpikeCount,averagingWindowSize) if i==len(lower_peaks)-1: b.plot(spikeCount, 'k', alpha=1., linewidth=3, label='Output')#alpha=0.5+(0.5*float(i)/float(len(lower_peaks))), b.plot(inputSpikeCount, 'r', alpha=1., linewidth=3, label='Input') elif i==0: b.plot(spikeCount, 'k', alpha=0.6, linewidth=3)# b.plot(inputSpikeCount, 'r', alpha=0.6, linewidth=3) else: b.plot(spikeCount, 'k', alpha=0.2+(0.4*float(i)/float(len(lower_peaks))), linewidth=0.6) b.plot(inputSpikeCount, 'r', alpha=0.2+(0.4*float(i)/float(len(lower_peaks))), linewidth=0.6) b.legend() b.ylim(0,35) # b.title('spikes:' + str(sum(spikeCount)) + ', pop. value: ' + str(computePopVector(spikeCount))) if ax1==None: b.savefig(dataPath + 'CueIntegration.png', dpi=900)
def plotActivity(dataPath, inputStrengths, ax1=None): averagingWindowSize = 10 nE = 1600 ymax = 40 linewidth = 5 # b.rcParams['lines.color'] = 'w' # b.rcParams['text.color'] = 'w' # b.rcParams['xtick.color'] = 'w' # b.rcParams['ytick.color'] = 'w' # b.rcParams['axes.labelcolor'] = 'w' # b.rcParams['axes.edgecolor'] = 'w' b.rcParams['font.size'] = 20 if ax1==None: b.figure(figsize=(8,6.5)) fig_axis = b.subplot(1,1,1) else: fig_axis = ax1 b.sca(ax1) for i,inputStrength in enumerate(inputStrengths[:]): path = dataPath + '/in_'+str(inputStrength)+'/' +'activity/' spikeCount = np.loadtxt(path + 'spikeCountAe.txt') inputSpikeCount = np.loadtxt(path + 'spikeCountXe.txt') spikeCount = movingaverage(spikeCount,averagingWindowSize) inputSpikeCount = movingaverage(inputSpikeCount,averagingWindowSize) if i==len(inputStrengths)-1: b.plot([0], 'w', label=' ')# b.plot([0], 'w', label='Avg. input 20 Hz:')# b.plot(inputSpikeCount, 'coral', alpha=0.6, linewidth=linewidth, label='Input firing rate') b.plot(spikeCount, 'deepskyblue', alpha=0.6, linewidth=linewidth, label='Output firing rate') elif i==1: b.plot([0], 'w', label=' ')# b.plot([0], 'w', label='Avg. input 6 Hz:')# b.plot(inputSpikeCount, 'red', alpha=1., linewidth=linewidth, label='Input firing rate') b.plot(spikeCount, 'blue', alpha=0.6, linewidth=linewidth, label='Output firing rate') elif i==0: b.plot([0], 'w', label='Avg. input 2 Hz:')# b.plot(inputSpikeCount, 'darkred', alpha=1., linewidth=linewidth, label='Input firing rate') b.plot(spikeCount, 'darkblue', alpha=1.0, linewidth=linewidth, label='Output firing rate') else: b.plot(spikeCount, 'k', alpha=0.2+(0.4*float(i)/float(len(inputStrengths))), linewidth=0.6) b.legend(loc='upper right', fancybox=True, framealpha=0.0, fontsize = 17) handles, labels = fig_axis.get_legend_handles_labels() # fig_axis.legend(handles[::-1], labels[::-1], loc='upper left', fancybox=True, framealpha=0.0) fig_axis.set_xticks([0., nE/2, nE]) fig_axis.set_yticks([0., ymax/2, ymax]) fig_axis.spines['top'].set_visible(False) fig_axis.spines['right'].set_visible(False) fig_axis.get_xaxis().tick_bottom() fig_axis.get_yaxis().tick_left() b.xlabel('Neuron number (resorted)') b.ylabel('Firing rate [Hz]') b.ylim(0,ymax)
def plotActivity(dataPath, inputStrengths, ax1=None): averagingWindowSize = 1 nE = 1600 ymax = 30 # b.rcParams['lines.color'] = 'w' # b.rcParams['text.color'] = 'w' # b.rcParams['xtick.color'] = 'w' # b.rcParams['ytick.color'] = 'w' # b.rcParams['axes.labelcolor'] = 'w' # b.rcParams['axes.edgecolor'] = 'w' b.rcParams['font.size'] = 20 if ax1==None: b.figure(figsize=(8,6.5)) fig_axis = b.subplot(1,1,1) else: fig_axis = ax1 b.sca(ax1) for i,inputStrength in enumerate(inputStrengths[:]): path = dataPath + '/peak_'+str(inputStrength)+'/' +'activity/' spikeCount = np.loadtxt(path + 'spikeCountAe.txt') inputSpikeCount = np.loadtxt(path + 'spikeCountXe.txt') spikeCount = movingaverage(spikeCount,averagingWindowSize) inputSpikeCount = movingaverage(inputSpikeCount,averagingWindowSize) if i==len(inputStrengths)-1: b.plot(inputSpikeCount, 'r', alpha=1., linewidth=3, label='Input') b.plot(spikeCount, 'deepskyblue', alpha=0.6, linewidth=3, label='Output')#alpha=0.5+(0.5*float(i)/float(len(numPeaks))), # elif i==0: # b.plot(spikeCount, 'k--', alpha=1., linewidth=3)# # b.plot(inputSpikeCount, 'r--', alpha=1., linewidth=3) else: b.plot(spikeCount, 'k', alpha=0.2+(0.4*float(i)/float(len(inputStrength))), linewidth=0.6) b.plot(inputSpikeCount, 'r', alpha=0.2+(0.4*float(i)/float(len(inputStrength))), linewidth=0.6) fig_axis.set_xticks([0., nE/2, nE]) fig_axis.set_xticklabels(['0', '0.5', '1']) fig_axis.set_yticks([0., ymax/2, ymax]) fig_axis.spines['top'].set_visible(False) fig_axis.spines['right'].set_visible(False) fig_axis.get_xaxis().tick_bottom() fig_axis.get_yaxis().tick_left() b.ylabel('Firing Rate [Hz]') b.ylim(0,ymax) # b.title('spikes:' + str(sum(spikeCount)) + ', pop. value: ' + str(computePopVector(spikeCount))) if ax1==None: b.xlabel('Neuron number (resorted)') b.legend(fancybox=True, framealpha=0.0, loc='upper left') b.savefig(dataPath + 'CueIntegration_single.png', dpi=900, transparent=True)
def plotPopulationTuningCurve(dataPath, gaussianDistance, ax1=None): b.rcParams['font.size'] = 20 averagingWindowSize = 1 nE = 1600 ymax = 1 if ax1==None: b.figure(figsize=(8,6.5)) fig_axis = b.subplot(1,1,1) else: fig_axis = ax1 b.sca(ax1) path = dataPath + '/dist'+str(gaussianDistance)+'/' +'activity/' spikeCount = np.load(path + 'spikeCountPerExample.npy') numMeasurements = len(spikeCount[:,0,0]) measurementSpace = np.arange(numMeasurements) populationSpikeCount = np.zeros((numMeasurements)) for i in xrange(nE): populationSpikeCount += np.roll(spikeCount[:,i,0], int(-1.*i/nE*numMeasurements+ numMeasurements/2)) populationSpikeCount = movingaverage(populationSpikeCount,averagingWindowSize) populationSpikeCount /= np.max(populationSpikeCount) if gaussianDistance==0.: mean = sum(measurementSpace*populationSpikeCount)/numMeasurements #note this correction sigma = sum(populationSpikeCount*(measurementSpace-mean)**2)/numMeasurements #note this correction popt, _ = curve_fit(gaus,measurementSpace,populationSpikeCount,p0=[1,mean,sigma]) print 'Gaussian amplitude: ', popt[0], ', mean: ', popt[1], ', std.: ', popt[2]**2 b.plot(measurementSpace,gaus(measurementSpace,*popt),'k') b.plot(populationSpikeCount, color='deepskyblue', marker='o', alpha=0.6, linewidth=0, label='Output')#alpha=0.5+(0.5*float(i)/float(len(lower_peaks))), fig_axis.set_xticks([0., numMeasurements/2, numMeasurements]) fig_axis.set_xticklabels(['0', '0.5', '1']) fig_axis.set_yticks([0., ymax/2, ymax]) fig_axis.spines['top'].set_visible(False) fig_axis.spines['right'].set_visible(False) fig_axis.get_xaxis().tick_bottom() fig_axis.get_yaxis().tick_left() # b.ylabel('Normalized response') b.ylim(0,ymax+0.05) # b.title('spikes:' + str(sum(spikeCount)) + ', pop. value: ' + str(computePopVector(spikeCount))) if ax1==None: b.legend(loc='upper left', fancybox=True, framealpha=0.0) b.savefig(dataPath + '/tuningCurvePopulation' + str(gaussianDistance) + '.png', dpi=300, bbox_inches='tight')
def plot_2d_input_weights(): name = 'XeAe' weights = get_2d_input_weights() fig = b.figure(fig_num, figsize = (18, 18)) im2 = b.imshow(weights, interpolation = "nearest", vmin = 0, vmax = wmax_ee, cmap = cmap.get_cmap('hot_r')) b.colorbar(im2) b.title('weights of connection' + name) fig.canvas.draw() return im2, fig
def plot_performance(fig_num): num_evaluations = int(num_examples/update_interval) time_steps = range(0, num_evaluations) performance = np.zeros(num_evaluations) fig = b.figure(fig_num, figsize = (5, 5)) fig_num += 1 ax = fig.add_subplot(111) im2, = ax.plot(time_steps, performance) #my_cmap b.ylim(ymax = 100) b.title('Classification performance') fig.canvas.draw() return im2, performance, fig_num, fig
def plotSingleActivity(dataPath, gaussianDistance, ax1=None): b.rcParams['font.size'] = 20 averagingWindowSize = 30 nE = 1600 ymax = 1 if ax1==None: b.figure(figsize=(8,6.5)) fig_axis = b.subplot(1,1,1) else: fig_axis = ax1 b.sca(ax1) linewidth = 3 path = dataPath + '/dist'+str(gaussianDistance)+'/' +'activity/' spikeCountTemp = np.load(path + 'spikeCountPerExample.npy') spikeCount = spikeCountTemp[25,:,0]#np.loadtxt(path + 'spikeCountAe.txt') # spikeCount = np.roll(spikeCount, 400) inputSpikeCount = np.roll(np.loadtxt(path + 'spikeCountXe.txt'), 400) spikeCount = movingaverage(spikeCount,averagingWindowSize) spikeCount /= np.max(spikeCount) inputSpikeCount = movingaverage(inputSpikeCount,averagingWindowSize) inputSpikeCount /= np.max(inputSpikeCount) b.plot(spikeCount, 'deepskyblue', alpha=0.6, linewidth=linewidth, label='Output')#alpha=0.5+(0.5*float(i)/float(len(lower_peaks))), b.plot(inputSpikeCount, 'r', alpha=1., linewidth=linewidth, label='Input') fig_axis.set_xticks([0., nE/2, nE]) fig_axis.set_xticklabels(['0', '0.5', '1']) fig_axis.set_yticks([0., ymax/2, ymax]) fig_axis.spines['top'].set_visible(False) fig_axis.spines['right'].set_visible(False) fig_axis.get_xaxis().tick_bottom() fig_axis.get_yaxis().tick_left() b.ylabel('Normalized response') b.ylim(0,ymax) # b.title('spikes:' + str(sum(spikeCount)) + ', pop. value: ' + str(computePopVector(spikeCount))) if ax1==None: b.legend(loc='upper left', fancybox=True, framealpha=0.0) b.savefig(dataPath + '/multipleAnglesSingleActivity.png', dpi=300)
def plotActivity(dataPath, ax1=None): averagingWindowSize = 1 nE = 1600 ymax = 40 # b.rcParams['lines.color'] = 'w' # b.rcParams['text.color'] = 'w' # b.rcParams['xtick.color'] = 'w' # b.rcParams['ytick.color'] = 'w' # b.rcParams['axes.labelcolor'] = 'w' # b.rcParams['axes.edgecolor'] = 'w' b.rcParams['font.size'] = 20 if ax1==None: fig = b.figure(figsize=(8,6.5)) fig_axis=b.subplot(1,1,1) else: fig_axis = ax1 b.sca(ax1) path = dataPath + 'activity/' spikeCount = np.loadtxt(path + 'spikeCountCe.txt') popVecs = np.loadtxt(path + 'popVecs1.txt') desiredResult = (popVecs[0] + popVecs[1])%1.*1600 resultMonitor = np.loadtxt(path + 'resultPopVecs1.txt') actualResult = resultMonitor[2]*1600 ax1.axvline(desiredResult, color='r', linewidth=3, ymax=ymax, label='Desired result') ax1.axvline(actualResult, color='blue', linewidth=3, ymax=ymax, label='Population vector') print 'desiredResult', desiredResult, ', actual result', actualResult # spikeCount = np.roll(spikeCount, 800+int(-1*desiredResult)) spikeCount = movingaverage(spikeCount,averagingWindowSize) b.plot(spikeCount, 'deepskyblue', alpha=0.6, linewidth=3, label='Population activity') fig_axis.set_xticks([0., nE/2, nE]) fig_axis.set_xticklabels(['0', '0.5', '1']) fig_axis.set_yticks([0., ymax/2, ymax]) fig_axis.spines['top'].set_visible(False) fig_axis.spines['right'].set_visible(False) fig_axis.get_xaxis().tick_bottom() fig_axis.get_yaxis().tick_left() b.ylabel('Firing Rate [Hz]') b.ylim(0,ymax) b.legend(fancybox=True, framealpha=0.0, loc='upper left') # b.title('spikes:' + str(sum(spikeCount)) + ', pop. value: ' + str(computePopVector(spikeCount))) if ax1==None: b.xlabel('Neuron number (resorted)') b.savefig(dataPath + 'SignalRestoration.png', dpi=900, transparent=True)
def run(self): #------------------------------------------------------------------------------ # run the simulation and set inputs #------------------------------------------------------------------------------ start = time.time() previousSpikeCount = np.zeros((self.nE,len(self.populationNames))) currentSpikeCount = np.zeros((self.nE,len(self.populationNames))) if self.recordSpikes: b.figure() b.ion() b.subplot(411) b.raster_plot(self.spikeMonitors['Ae'], refresh=1000*b.ms, showlast=1000*b.ms) b.subplot(412) b.raster_plot(self.spikeMonitors['Be'], refresh=1000*b.ms, showlast=1000*b.ms) b.subplot(413) b.raster_plot(self.spikeMonitors['Ce'], refresh=1000*b.ms, showlast=1000*b.ms) b.subplot(414) b.raster_plot(self.spikeMonitors['He'], refresh=1000*b.ms, showlast=1000*b.ms) # realTimeMonitor = None # realTimeMonitor = rltmMon.RealtimeConnectionMonitor(self.connections['HeAe'], cmap=cm.get_cmap('gist_rainbow'), # wmin=0, wmax=self.wmaxEE, clock=Clock(1000*b.ms)) for j in xrange(int(self.numExamples)): if self.restingTime or j==0: for i,name in enumerate(self.inputPopulationNames): rates = np.ones(self.nE) * 0 self.inputGroups[name+'e'].rate = rates self.net.run(self.restingTime)#, report='text') if j%self.normalization_interval == 0: self.normalizeWeights() print 'set new rates of the inputs' self.setNewInput(self,j) print 'run number:', j+1, 'of', int(self.numExamples) self.net.run(self.singleExampleTime)#, report='text') for i,name in enumerate(self.populationNames): name += 'e' currentSpikeCount[:,i] = np.asarray(self.spikeCounters[name].count[:]) - previousSpikeCount[:,i] # print currentSpikeCount, np.asarray(spikeCounters['Ce'].count[:]), previousSpikeCount previousSpikeCount[:,i] = np.copy(self.spikeCounters[name].count[:]) self.resultMonitor[j,i] = self.computePopVector(currentSpikeCount[:,i]) print name, 'pop. activity: ', self.resultMonitor[j,i], ', spikecount:', sum(currentSpikeCount[:,i]) for i,name in enumerate(self.inputPopulationNames): print name, 'pop. activity: ', (self.popValues[j,i]) if not self.testMode: if self.numExamples <= 1000: if j%100==0 and not j==0: self.saveConnections(str(j)) else: if j%10000==0 and not j==0: self.saveConnections(str(j)) end = time.time() print 'time needed to simulate:', end - start #------------------------------------------------------------------------------ # save results #------------------------------------------------------------------------------ print 'save results' if self.testMode: np.savetxt(self.dataPath + 'activity/resultPopVecs' + str(self.numExamples) + '.txt', self.resultMonitor) np.savetxt(self.dataPath + 'activity/spikeCount' + str(self.gaussian_peak_low) + '.txt', self.spikeCounters['Ae'].count[:]/(self.singleExampleTime*int(self.numExamples))) np.savetxt(self.dataPath + 'activity/inputSpikeCount' + str(self.gaussian_peak_low) + '.txt', self.spikeCounters['Xe'].count[:]/(self.singleExampleTime*int(self.numExamples))) np.savetxt(self.dataPath + 'activity/popVecs' + str(self.numExamples) + '.txt', self.popValues) else: self.saveConnections(str(j)) self.normalizeWeights() self.saveConnections()
print 'create monitors for', name rate_monitors[name+'e'] = b.PopulationRateMonitor(neuron_groups[name+'e'], bin = (single_example_time+resting_time)/b.second) rate_monitors[name+'i'] = b.PopulationRateMonitor(neuron_groups[name+'i'], bin = (single_example_time+resting_time)/b.second) spike_counters[name+'e'] = b.SpikeCounter(neuron_groups[name+'e']) if record_spikes: spike_monitors[name+'e'] = b.SpikeMonitor(neuron_groups[name+'e']) spike_monitors[name+'i'] = b.SpikeMonitor(neuron_groups[name+'i']) if record_states: state_monitors[name+'e'] = b.MultiStateMonitor(neuron_groups[name+'e'], ['v', 'ge', 'gi'], record=[0]) state_monitors[name+'i'] = b.MultiStateMonitor(neuron_groups[name+'i'], ['v', 'ge', 'gi'], record=[0]) if record_spikes: # if not use_remote_access: b.figure(fig_num) fig_num += 1 b.ion() b.subplot(211) b.raster_plot(spike_monitors['Ae'], refresh=1000*b.ms, showlast=1000*b.ms) b.subplot(212) b.raster_plot(spike_monitors['Ai'], refresh=1000*b.ms, showlast=1000*b.ms) #------------------------------------------------------------------------------ # create input population #------------------------------------------------------------------------------ pop_values = [0,0,0] for i,name in enumerate(input_population_names): if name == 'Y':
if ee_STDP_on: if 'ee' in recurrent_conn_names: stdp_methods[name+'e'+name+'e'] = b.STDP(connections[name+'e'+name+'e'], eqs=eqs_stdp_ee, pre = eqs_stdp_pre_ee, post = eqs_stdp_post_ee, wmin=0., wmax= wmax_ee) print 'create monitors for', name rate_monitors[name+'e'] = b.PopulationRateMonitor(neuron_groups[name+'e'], bin = (single_example_time+resting_time)/b.second) rate_monitors[name+'i'] = b.PopulationRateMonitor(neuron_groups[name+'i'], bin = (single_example_time+resting_time)/b.second) spike_counters[name+'e'] = b.SpikeCounter(neuron_groups[name+'e']) if record_spikes: spike_monitors[name+'e'] = b.SpikeMonitor(neuron_groups[name+'e']) spike_monitors[name+'i'] = b.SpikeMonitor(neuron_groups[name+'i']) if record_spikes: b.figure(fig_num) fig_num += 1 b.ion() b.subplot(211) b.raster_plot(spike_monitors['Ae'], refresh=1000*b.ms, showlast=1000*b.ms) b.subplot(212) b.raster_plot(spike_monitors['Ai'], refresh=1000*b.ms, showlast=1000*b.ms) #------------------------------------------------------------------------------ # create input population and connections from input populations #------------------------------------------------------------------------------ pop_values = [0,0,0] for i,name in enumerate(input_population_names): input_groups[name+'e'] = b.PoissonGroup(n_input, 0) rate_monitors[name+'e'] = b.PopulationRateMonitor(input_groups[name+'e'], bin = (single_example_time+resting_time)/b.second)
def run(self): #------------------------------------------------------------------------------ # run the simulation and set inputs #------------------------------------------------------------------------------ previousSpikeCountB = np.zeros(self.nE) previousSpikeCountC = np.zeros(self.nE) self.resultMonitor = np.zeros((self.numExamples,len(self.populationNames))) start = time.time() if self.recordSpikes: b.figure() b.ion() b.subplot(211) b.raster_plot(self.spikeMonitors['He'], refresh=1000*b.ms, showlast=1000*b.ms) b.subplot(212) b.raster_plot(self.spikeMonitors['Hi'], refresh=1000*b.ms, showlast=1000*b.ms) # realTimeMonitor = None # realTimeMonitor = rltmMon.RealtimeConnectionMonitor(self.connections['HeAe'], cmap=cm.get_cmap('gist_rainbow'), # wmin=0, wmax=self.wmaxEE, clock=Clock(1000*b.ms)) for j in xrange(int(self.numExamples)): if self.restingTime or j==0: for i,name in enumerate(self.inputPopulationNames): rates = np.ones(self.nE) * 0 self.inputGroups[name+'e'].rate = rates self.net.run(self.restingTime)#, report='text') if j%self.normalization_interval == 0: self.normalizeWeights() print 'set new rates of the inputs' self.popValues = [0]*len(self.inputPopulationNames) for i,name in enumerate(self.inputPopulationNames): if name == 'X': self.popValues[i] = np.random.rand(); rates = self.createTopoInput(self.nE, self.popValues[i]) self.resultMonitor[j,0] = self.popValues[i] else: if self.testMode: rates = np.ones(self.nE) * 0 elif name == 'Y': self.popValues[i] = (self.popValues[0]*2) % 1. rates = self.createTopoInput(self.nE, self.popValues[i]) elif name == 'Z': self.popValues[i] = (self.popValues[0]**2) % 1. rates = self.createTopoInput(self.nE, self.popValues[i]) if self.testMode: rates += noise self.inputGroups[name+'e'].rate = rates print 'run number:', j+1, 'of', int(self.numExamples) self.net.run(self.singleExampleTime)#, report='text') currentSpikeCountB = np.asarray(self.spikeCounters['Be'].count[:]) - previousSpikeCountB currentSpikeCountC = np.asarray(self.spikeCounters['Ce'].count[:]) - previousSpikeCountC # print currentSpikeCount, np.asarray(spikeCounters['Ce'].count[:]), previousSpikeCount previousSpikeCountB = np.copy(self.spikeCounters['Be'].count[:]) previousSpikeCountC = np.copy(self.spikeCounters['Ce'].count[:]) self.resultMonitor[j,1] = self.computePopVector(currentSpikeCountB) self.resultMonitor[j,2] = self.computePopVector(currentSpikeCountC) difference = np.abs((self.resultMonitor[j,0]**2)%1. - self.resultMonitor[j,2]) if difference > 0.5: difference = 1-difference print 'Pop. activity: ', self.resultMonitor[j,2], ', Desired activity: ', (self.resultMonitor[j,0]**2)%1., ', Difference: ', difference if not self.testMode: if self.numExamples <= 1000: if j%100 == 0: self.saveConnections(str(j)) else: if j%1000 == 0: self.saveConnections(str(j)) end = time.time() print 'time needed to simulate:', end - start #------------------------------------------------------------------------------ # save results #------------------------------------------------------------------------------ print 'save results' if self.testMode: np.savetxt(self.dataPath + 'activity/resultPopVecs' + str(self.numExamples) + '.txt', self.resultMonitor) else: self.saveConnections(str(j)) self.normalizeWeights() self.saveConnections()
def plotTuningHeatmap(dataPath, gaussianDistances, ax1=None): b.rcParams['font.size'] = 20 # averagingWindowSize = 1 nE = 1600 if ax1==None: b.figure(figsize=(8,15)) fig_axis = b.subplot(1,1,1) else: b.sca(ax1) fig_axis=ax1 path = dataPath + '/dist'+str(gaussianDistances[0])+'/' +'activity/' spikeCount = np.load(path + 'spikeCountPerExample.npy') numMeasurements = len(spikeCount[:,0,0]) measurementSpace = np.arange(numMeasurements) populationSpikeCount = np.zeros((numMeasurements, len(gaussianDistances))) for i,gaussianDistance in enumerate(gaussianDistances): # distanceAngle = gaussianDistance * 2 * 360 path = dataPath + '/dist'+str(gaussianDistance)+'/' +'activity/' spikeCount = np.load(path + 'spikeCountPerExample.npy') for j in xrange(nE): populationSpikeCount[:,i] += np.roll(spikeCount[:,j,0], int(-1.*j/nE*numMeasurements+ numMeasurements/2)) # j = 0 # populationSpikeCount[:,i] += np.roll(spikeCount[:,j,0], int(-1.*j/nE*numMeasurements+ numMeasurements/2)) # populationSpikeCount[:,i] = movingaverage(populationSpikeCount[:,i],averagingWindowSize) populationSpikeCount[:,i] /= np.max(populationSpikeCount[:,i]) if gaussianDistance==0.: mean = sum(measurementSpace*populationSpikeCount[:,i])/numMeasurements #note this correction sigma = sum(populationSpikeCount[:,i]*(measurementSpace-mean)**2)/numMeasurements #note this correction popt, _ = curve_fit(gaus,measurementSpace,populationSpikeCount[:,i],p0=[1,mean,sigma]) # tuningWidth = abs(popt[2])*2 tuningWidth = np.sum(populationSpikeCount>=0.5) tuningWidthAngle = tuningWidth/50.*360. print 'Gaussian amplitude:', popt[0], 'mean:', popt[1], 'std.:', popt[2], \ ', tuning width:', tuningWidth, ', tuning width angle:', tuningWidthAngle minX = max(0,round(numMeasurements/2-tuningWidth*2)) maxX = min(numMeasurements, round(numMeasurements/2+tuningWidth*2)) minY = round(0) maxY = round(tuningWidth/50.*len(gaussianDistances)*2*2) print minX, maxX, minY, maxY croppedCount = populationSpikeCount[minX:maxX,minY:maxY] fig_axis.imshow(croppedCount.transpose(), aspect='auto', extent=[minX, maxX, 2, 0]) # b.colorbar() fig_axis.set_xlabel('Distance from preferred stimulus, \ndivided by tuning width') fig_axis.set_xticks([numMeasurements/2-tuningWidth*2, numMeasurements/2-tuningWidth, numMeasurements/2, numMeasurements/2+tuningWidth, numMeasurements/2+tuningWidth*2]) fig_axis.set_xticklabels(['-2', '-1', '0', '1', '2']) fig_axis.set_yticks([0., 0.5, 1, 1.5, 2]) fig_axis.spines['top'].set_visible(False) fig_axis.spines['right'].set_visible(False) fig_axis.spines['bottom'].set_visible(False) fig_axis.spines['left'].set_visible(False) fig_axis.get_xaxis().tick_bottom() fig_axis.get_yaxis().tick_left() if ax1==None: b.savefig(dataPath + '/tuningHeatmap.png', dpi=300, bbox_inches='tight') return tuningWidthAngle
for i in xrange(num_layers): # print 'create neuron group', i neuron_groups[i] = neuron_groups['e'].subgroup(num_neurons[i]) # print 'create monitors for', i rate_monitors[i] = b.PopulationRateMonitor(neuron_groups[i], bin = (single_example_time+resting_time)/b.second) spike_counters[i] = b.SpikeCounter(neuron_groups[i]) if record_spikes: spike_monitors[i] = b.SpikeMonitor(neuron_groups[i]) state_monitors[i] = b.MultiStateMonitor(neuron_groups[i], ['v'], record=[0]) if record_spikes: b.figure() b.ion() b.subplot(211) b.raster_plot(spike_monitors[1], refresh=1000*b.ms, showlast=1000*b.ms) b.subplot(212) b.raster_plot(spike_monitors[1], refresh=1000*b.ms, showlast=1000*b.ms) #------------------------------------------------------------------------------ # create input population #------------------------------------------------------------------------------ pop_values = [0,0,0] for i,name in enumerate(input_population_names): if poisson_inputs: input_groups[name] = b.PoissonGroup(n_input*2, 0)
def build_network(): global fig_num neuron_groups['e'] = b.NeuronGroup(n_e_total, neuron_eqs_e, threshold=v_thresh_e, \ refractory=refrac_e, reset=scr_e, compile=True, freeze=True) neuron_groups['i'] = b.NeuronGroup(n_e_total, neuron_eqs_i, threshold=v_thresh_i, \ refractory=refrac_i, reset=v_reset_i, compile=True, freeze=True) for name in population_names: print '...Creating neuron group:', name # get a subgroup of size 'n_e' from all exc neuron_groups[name + 'e'] = neuron_groups['e'].subgroup(conv_features * n_e) # get a subgroup of size 'n_i' from the inhibitory layer neuron_groups[name + 'i'] = neuron_groups['i'].subgroup(conv_features * n_e) # start the membrane potentials of these groups 40mV below their resting potentials neuron_groups[name + 'e'].v = v_rest_e - 40. * b.mV neuron_groups[name + 'i'].v = v_rest_i - 40. * b.mV print '...Creating recurrent connections' for name in population_names: neuron_groups['e'].theta = np.load(os.path.join(best_weights_dir, '_'.join(['theta_A', ending +'_best.npy']))) for conn_type in recurrent_conn_names: if conn_type == 'ei': # create connection name (composed of population and connection types) conn_name = name + conn_type[0] + name + conn_type[1] # create a connection from the first group in conn_name with the second group connections[conn_name] = b.Connection(neuron_groups[conn_name[0:2]], neuron_groups[conn_name[2:4]], structure='sparse', state='g' + conn_type[0]) # instantiate the created connection for feature in xrange(conv_features): for n in xrange(n_e): connections[conn_name][feature * n_e + n, feature * n_e + n] = 10.4 elif conn_type == 'ie': # create connection name (composed of population and connection types) conn_name = name + conn_type[0] + name + conn_type[1] # load weight matrix weight_matrix = np.load(os.path.join(best_weights_dir, '_'.join([conn_name, ending, 'best.npy']))) # create a connection from the first group in conn_name with the second group connections[conn_name] = b.Connection(neuron_groups[conn_name[0:2]], neuron_groups[conn_name[2:4]], structure='sparse', state='g' + conn_type[0]) # define the actual synaptic connections and strengths for feature in xrange(conv_features): for other_feature in xrange(conv_features): if feature != other_feature: for n in xrange(n_e): connections[conn_name][feature * n_e + n, other_feature * n_e + n] = inhibition_level print '...Creating monitors for:', name # spike rate monitors for excitatory and inhibitory neuron populations rate_monitors[name + 'e'] = b.PopulationRateMonitor(neuron_groups[name + 'e'], bin=(single_example_time + resting_time) / b.second) rate_monitors[name + 'i'] = b.PopulationRateMonitor(neuron_groups[name + 'i'], bin=(single_example_time + resting_time) / b.second) spike_counters[name + 'e'] = b.SpikeCounter(neuron_groups[name + 'e']) # record neuron population spikes if specified if record_spikes and do_plot: spike_monitors[name + 'e'] = b.SpikeMonitor(neuron_groups[name + 'e']) spike_monitors[name + 'i'] = b.SpikeMonitor(neuron_groups[name + 'i']) if record_spikes and do_plot: b.figure(fig_num, figsize=(8, 6)) fig_num += 1 b.ion() b.subplot(211) b.raster_plot(spike_monitors['Ae'], refresh=1000 * b.ms, showlast=1000 * b.ms, title='Excitatory spikes per neuron') b.subplot(212) b.raster_plot(spike_monitors['Ai'], refresh=1000 * b.ms, showlast=1000 * b.ms, title='Inhibitory spikes per neuron') b.tight_layout() # creating Poission spike train from input image (784 vector, 28x28 image) for name in input_population_names: input_groups[name + 'e'] = b.PoissonGroup(n_input, 0) rate_monitors[name + 'e'] = b.PopulationRateMonitor(input_groups[name + 'e'], bin=(single_example_time + resting_time) / b.second) # creating connections from input Poisson spike train to excitatory neuron population(s) for name in input_connection_names: print '\n...Creating connections between', name[0], 'and', name[1] # for each of the input connection types (in this case, excitatory -> excitatory) for conn_type in input_conn_names: # saved connection name conn_name = name[0] + conn_type[0] + name[1] + conn_type[1] # get weight matrix depending on training or test phase weight_matrix = np.load(os.path.join(best_weights_dir, '_'.join([conn_name, ending + '_best.npy']))) # create connections from the windows of the input group to the neuron population input_connections[conn_name] = b.Connection(input_groups['Xe'], neuron_groups[name[1] + conn_type[1]], \ structure='sparse', state='g' + conn_type[0], delay=True, max_delay=delay[conn_type][1]) for feature in xrange(conv_features): for n in xrange(n_e): for idx in xrange(conv_size ** 2): input_connections[conn_name][convolution_locations[n][idx], feature * n_e + n] = \ weight_matrix[convolution_locations[n][idx], feature * n_e + n] if do_plot: plot_2d_input_weights() fig_num += 1 print '\n'
def build_network(): global fig_num neuron_groups['e'] = b.NeuronGroup(n_e_total, neuron_eqs_e, threshold=v_thresh_e, refractory=refrac_e, reset=scr_e, compile=True, freeze=True) neuron_groups['i'] = b.NeuronGroup(n_e_total, neuron_eqs_i, threshold=v_thresh_i, refractory=refrac_i, reset=v_reset_i, compile=True, freeze=True) for name in ['A']: print '...Creating neuron group:', name # get a subgroup of size 'n_e' from all exc neuron_groups[name + 'e'] = neuron_groups['e'].subgroup(conv_features * n_e) # get a subgroup of size 'n_i' from the inhibitory layer neuron_groups[name + 'i'] = neuron_groups['i'].subgroup(conv_features * n_e) # start the membrane potentials of these groups 40mV below their resting potentials neuron_groups[name + 'e'].v = v_rest_e - 40. * b.mV neuron_groups[name + 'i'].v = v_rest_i - 40. * b.mV print '...Creating recurrent connections' for name in ['A']: neuron_groups['e'].theta = np.load( os.path.join(best_weights_dir, '_'.join(['theta_A', ending + '_best.npy']))) for conn_type in ['ei', 'ie']: if conn_type == 'ei': # create connection name (composed of population and connection types) conn_name = name + conn_type[0] + name + conn_type[1] # create a connection from the first group in conn_name with the second group connections[conn_name] = b.Connection(neuron_groups[conn_name[0:2]], \ neuron_groups[conn_name[2:4]], structure='sparse', state='g' + conn_type[0]) # instantiate the created connection for feature in xrange(conv_features): for n in xrange(n_e): connections[conn_name][feature * n_e + n, feature * n_e + n] = 10.4 elif conn_type == 'ie' and not remove_inhibition: # create connection name (composed of population and connection types) conn_name = name + conn_type[0] + name + conn_type[1] # create a connection from the first group in conn_name with the second group connections[conn_name] = b.Connection(neuron_groups[conn_name[0:2]], \ neuron_groups[conn_name[2:4]], structure='sparse', state='g' + conn_type[0]) # define the actual synaptic connections and strengths for feature in xrange(conv_features): if inhib_scheme in ['far', 'strengthen']: for other_feature in set(range(conv_features)) - set( neighbor_mapping[feature]): if inhib_scheme == 'far': for n in xrange(n_e): connections[conn_name][feature * n_e + n, other_feature * n_e + n] = 17.4 elif inhib_scheme == 'strengthen': if n_e == 1: x, y = feature // np.sqrt( n_e_total), feature % np.sqrt( n_e_total) x_, y_ = other_feature // np.sqrt( n_e_total), other_feature % np.sqrt( n_e_total) else: x, y = feature // np.sqrt( conv_features), feature % np.sqrt( conv_features) x_, y_ = other_feature // np.sqrt( conv_features ), other_feature % np.sqrt(conv_features) for n in xrange(n_e): connections[conn_name][feature * n_e + n, other_feature * n_e + n] = \ min(17.4, inhib_const * np.sqrt(euclidean([x, y], [x_, y_]))) elif inhib_scheme == 'increasing': for other_feature in xrange(conv_features): if n_e == 1: x, y = feature // np.sqrt( n_e_total), feature % np.sqrt(n_e_total) x_, y_ = other_feature // np.sqrt( n_e_total), other_feature % np.sqrt( n_e_total) else: x, y = feature // np.sqrt( conv_features), feature % np.sqrt( conv_features) x_, y_ = other_feature // np.sqrt( conv_features), other_feature % np.sqrt( conv_features) if feature != other_feature: for n in xrange(n_e): connections[conn_name][feature * n_e + n, other_feature * n_e + n] = \ min(17.4, inhib_const * np.sqrt(euclidean([x, y], [x_, y_]))) else: raise Exception( 'Expecting one of "far", "increasing", or "strengthen" for argument "inhib_scheme".' ) # spike rate monitors for excitatory and inhibitory neuron populations rate_monitors[name + 'e'] = b.PopulationRateMonitor( neuron_groups[name + 'e'], bin=(single_example_time + resting_time) / b.second) rate_monitors[name + 'i'] = b.PopulationRateMonitor( neuron_groups[name + 'i'], bin=(single_example_time + resting_time) / b.second) spike_counters[name + 'e'] = b.SpikeCounter(neuron_groups[name + 'e']) # record neuron population spikes if specified if record_spikes: spike_monitors[name + 'e'] = b.SpikeMonitor(neuron_groups[name + 'e']) spike_monitors[name + 'i'] = b.SpikeMonitor(neuron_groups[name + 'i']) if record_spikes and do_plot: if reset_state_vars: time_window = single_example_time * 1000 else: time_window = (single_example_time + resting_time) * 1000 b.figure(fig_num, figsize=(8, 6)) b.ion() b.subplot(211) b.raster_plot(spike_monitors['Ae'], refresh=time_window * b.ms, showlast=time_window * b.ms, title='Excitatory spikes per neuron') b.subplot(212) b.raster_plot(spike_monitors['Ai'], refresh=time_window * b.ms, showlast=time_window * b.ms, title='Inhibitory spikes per neuron') b.tight_layout() fig_num += 1 # creating Poission spike train from input image (784 vector, 28x28 image) for name in ['X']: input_groups[name + 'e'] = b.PoissonGroup(n_input, 0) rate_monitors[name + 'e'] = b.PopulationRateMonitor( input_groups[name + 'e'], bin=(single_example_time + resting_time) / b.second) # creating connections from input Poisson spike train to convolution patch populations for name in ['XA']: print '\n...Creating connections between', name[0], 'and', name[1] # for each of the input connection types (in this case, excitatory -> excitatory) for conn_type in ['ee_input']: # saved connection name conn_name = name[0] + conn_type[0] + name[1] + conn_type[1] # get weight matrix depending on training or test phase weight_matrix = np.load( os.path.join(best_weights_dir, '_'.join([conn_name, ending + '_best.npy']))) # create connections from the windows of the input group to the neuron population input_connections[conn_name] = b.Connection(input_groups['Xe'], neuron_groups[name[1] + \ conn_type[1]], structure='sparse', state='g' + conn_type[0], delay=True, max_delay=delay[conn_type][1]) for feature in xrange(conv_features): for n in xrange(n_e): for idx in xrange(conv_size**2): input_connections[conn_name][convolution_locations[n][idx], feature * n_e + n] = \ weight_matrix[convolution_locations[n][idx], feature * n_e + n] if do_plot: plot_2d_input_weights() fig_num += 1
diffLastActivity = np.abs(resultMonitor[start_time+1:end_time,0] - resultMonitor[start_time:end_time-1,0]) correctionDiffIdxs = np.where(diffLastActivity > 0.5)[0] correctedPopDiff = [1 - diffLastActivity[i] if (i in correctionDiffIdxs) else diffLastActivity[i] for i in xrange(len(diffLastActivity))] print correctedErrorSum rcParams['lines.color'] = 'w' rcParams['text.color'] = 'w' rcParams['xtick.color'] = 'w' rcParams['ytick.color'] = 'w' rcParams['axes.labelcolor'] = 'w' rcParams['axes.edgecolor'] = 'w' b.figure() b.scatter(resultMonitor[start_time:end_time,2], correctedError, c=range(len(error)), cmap=cmap.gray) b.title('Error: ' + str(correctedErrorSum)) b.xlabel('Activity in B') b.ylabel('Error') fi = b.figure(figsize = (5.0,4.6)) ax = plt.subplot(1,1,1) matplotlib.rcParams.update({'font.size': 22}) b.scatter(resultMonitor[start_time:end_time,1]*1600, resultMonitor[start_time:end_time,0]*1600, c='k', cmap=cmap.gray) #range(len(error)) # b.title('Error: ' + str(correctedErrorSum)) # b.xlabel('Desired activity') # b.ylabel('Population activity') ax.set_xticks([0,800,1600]) ax.set_xticklabels(['0', '800', '1600'])
def run(self): #------------------------------------------------------------------------------ # run the simulation and set inputs #------------------------------------------------------------------------------ previousSpikeCount = np.zeros((self.nE, self.numAllPops)) currentSpikeCount = np.zeros((self.nE, self.numAllPops)) if self.saveSpikeCountsPerExample: self.spikeCounts=np.zeros((self.numExamples, self.nE, self.numAllPops)) if self.realTimePlotting and self.recordSpikes: b.ion() fig = b.figure(1) b.raster_plot(self.spikeMonitors['Ae']) if self.ending=='': initJ = 0 else: initJ = int(self.ending) for j in xrange(int(self.numExamples)): if self.restingTime or j==0: for i,name in enumerate(self.inputPopulationNames): rates = np.ones(self.nE) * 0 self.inputGroups[name+'e'].rate = rates self.net.run(self.restingTime) if j%self.normalization_interval == 0: self.normalizeWeights() print 'set new rates of the inputs' self.setNewInput(self,j) print 'run number:', j+1, 'of', int(self.numExamples) self.net.run(self.singleExampleTime) for i,name in enumerate(self.populationNames): name += 'e' currentSpikeCount[:,i] = np.asarray(self.spikeCounters[name].count[:]) - previousSpikeCount[:,i] previousSpikeCount[:,i] = np.copy(self.spikeCounters[name].count[:]) self.resultMonitor[j,i] = self.computePopVector(currentSpikeCount[:,i]) print name, 'pop. activity: ', self.resultMonitor[j,i], ', spikecount:', sum(currentSpikeCount[:,i]) if self.saveSpikeCountsPerExample: self.spikeCounts[j,:,i] = currentSpikeCount[:,i] for i,name in enumerate(self.inputPopulationNames): print name, 'pop. activity: ', (self.popValues[j,i]) name += 'e' currentSpikeCount[:,i+self.numPops] = np.asarray(self.spikeCounters[name].count[:]) - previousSpikeCount[:,i+self.numPops] previousSpikeCount[:,i+self.numPops] = np.copy(self.spikeCounters[name].count[:]) self.resultMonitor[j,i+self.numPops] = self.computePopVector(currentSpikeCount[:,i+self.numPops]) if self.saveSpikeCountsPerExample: self.spikeCounts[j,:,i+self.numPops] = currentSpikeCount[:,i+self.numPops] if not self.testMode: if self.numExamples <= 1000: if (j+1)%100==0 and not j==0: self.saveConnections(str(j+initJ+1)) else: if (j+1)%5000==0 and not j==0: self.saveConnections(str(j+initJ+1)) if self.realTimePlotting and self.recordSpikes: b.raster_plot(self.spikeMonitors['Ae'], showlast=1000*b.ms) fig.canvas.draw() #------------------------------------------------------------------------------ # save results #------------------------------------------------------------------------------ print 'save results' if self.testMode: np.savetxt(self.dataPath + 'activity/resultPopVecs' + str(self.numExamples) + '.txt', self.resultMonitor) np.savetxt(self.dataPath + 'activity/popVecs' + str(self.numExamples) + '.txt', self.popValues) for name in self.spikeCounters: np.savetxt(self.dataPath + 'activity/spikeCount' + name + '.txt', self.spikeCounters[name].count[:]/(self.singleExampleTime*int(self.numExamples))) if self.saveSpikeCountsPerExample: np.save(self.dataPath + 'activity/spikeCountPerExample', self.spikeCounts) else: self.saveConnections(str(self.numExamples+initJ)) self.normalizeWeights() self.saveConnections()
print 'create monitors for', name rate_monitors[name + 'e'] = b.PopulationRateMonitor( neuron_groups[name + 'e'], bin=(single_example_time + resting_time) / b.second) rate_monitors[name + 'i'] = b.PopulationRateMonitor( neuron_groups[name + 'i'], bin=(single_example_time + resting_time) / b.second) spike_counters[name + 'e'] = b.SpikeCounter(neuron_groups[name + 'e']) if record_spikes: spike_monitors[name + 'e'] = b.SpikeMonitor(neuron_groups[name + 'e']) spike_monitors[name + 'i'] = b.SpikeMonitor(neuron_groups[name + 'i']) if record_spikes: b.figure(fig_num) fig_num += 1 b.ion() b.subplot(211) b.raster_plot(spike_monitors['Ae'], refresh=1000 * b.ms, showlast=1000 * b.ms) b.subplot(212) b.raster_plot(spike_monitors['Ai'], refresh=1000 * b.ms, showlast=1000 * b.ms) #------------------------------------------------------------------------------ # create input population and connections from input populations #------------------------------------------------------------------------------ pop_values = [0, 0, 0]
def plotResults(self): #------------------------------------------------------------------------------ # plot results #------------------------------------------------------------------------------ if self.rateMonitors: b.figure() for i, name in enumerate(self.rateMonitors): b.subplot(len(self.rateMonitors), 1, i) b.plot(self.rateMonitors[name].times/b.second, self.rateMonitors[name].rate, '.') b.title('rates of population ' + name) if self.spikeMonitors: b.figure() for i, name in enumerate(self.spikeMonitors): b.subplot(len(self.spikeMonitors), 1, i) b.raster_plot(self.spikeMonitors[name]) b.title('spikes of population ' + name) if name=='Ce': timePoints = np.linspace(0+(self.singleExampleTime+self.restingTime)/(2*b.second)*1000, self.runtime/b.second*1000-(self.singleExampleTime+self.restingTime)/(2*b.second)*1000, self.numExamples) b.plot(timePoints, self.resultMonitor[:,0]*self.nE, 'g') b.plot(timePoints, self.resultMonitor[:,1]*self.nE, 'r') if self.stateMonitors: b.figure() for i, name in enumerate(self.stateMonitors): b.plot(self.stateMonitors[name].times/b.second, self.stateMonitors[name]['v'][0], label = name + ' v 0') b.legend() b.title('membrane voltages of population ' + name) b.figure() for i, name in enumerate(self.stateMonitors): b.plot(self.stateMonitors[name].times/b.second, self.stateMonitors[name]['ge'][0], label = name + ' v 0') b.legend() b.title('conductances of population ' + name) plotWeights = [ # 'XeAe', # 'XeAi', # 'AeAe', # 'AeAi', # 'AiAe', # 'AiAi', # 'BeBe', # 'BeBi', # 'BiBe', # 'BiBi', # 'CeCe', # 'CeCi', 'CiCe', # 'CiCi', # 'HeHe', # 'HeHi', # 'HiHe', # 'HiHi', 'AeHe', # 'BeHe', # 'CeHe', 'HeAe', # 'HeBe', # 'HeCe', ] for name in plotWeights: b.figure() # my_cmap = matplotlib.colors.LinearSegmentedColormap.from_list('own2',['#f4f4f4', '#000000']) # my_cmap2 = matplotlib.colors.LinearSegmentedColormap.from_list('own2',['#000000', '#f4f4f4']) if name[1]=='e': nSrc = self.nE else: nSrc = self.nI if name[3]=='e': nTgt = self.nE else: nTgt = self.nI w_post = np.zeros((nSrc, nTgt)) connMatrix = self.connections[name][:] for i in xrange(nSrc): w_post[i, connMatrix.rowj[i]] = connMatrix.rowdata[i] im2 = b.imshow(w_post, interpolation="nearest", vmin = 0, cmap=cm.get_cmap('gist_ncar')) #my_cmap b.colorbar(im2) b.title('weights of connection' + name) if self.plotError: error = np.abs(self.resultMonitor[:,1] - self.resultMonitor[:,0]) correctionIdxs = np.where(error > 0.5)[0] correctedError = [1 - error[i] if (i in correctionIdxs) else error[i] for i in xrange(len(error))] correctedErrorSum = np.average(correctedError) b.figure() b.scatter(self.resultMonitor[:,1], self.resultMonitor[:,0], c=range(len(error)), cmap=cm.get_cmap('gray')) b.title('Error: ' + str(correctedErrorSum)) b.xlabel('Desired activity') b.ylabel('Population activity') b.figure() error = np.abs(self.resultMonitor[:,1] - self.resultMonitor[:,0]) correctionIdxs = np.where(error > 0.5)[0] correctedError = [1 - error[i] if (i in correctionIdxs) else error[i] for i in xrange(len(error))] correctedErrorSum = np.average(correctedError) b.scatter(self.resultMonitor[:,1], self.resultMonitor[:,0], c=self.resultMonitor[:,2], cmap=cm.get_cmap('gray')) b.title('Error: ' + str(correctedErrorSum)) b.xlabel('Desired activity') b.ylabel('Population activity') b.ioff() b.show()
print '\n' # creating convolution locations inside the input image convolution_locations = {} for n in xrange(n_e): convolution_locations[n] = [ ((n % n_e_sqrt) * conv_stride + (n // n_e_sqrt) * n_input_sqrt * conv_stride) + (x * n_input_sqrt) + y for y in xrange(conv_size) for x in xrange(conv_size) ] # get the list of flattened input weights per neuron per feature weights = get_input_weights( get_matrix_from_file(weight_dir + file_name, n_input, conv_features * n_e)) # create and fit a KMeans model kmeans = KMeans(n_clusters=10).fit(weights) print kmeans.labels_ for idx, cluster_center in enumerate(kmeans.cluster_centers_): fig = b.figure(idx) im = b.imshow(cluster_center.reshape((conv_size, conv_size)).T, interpolation='nearest', vmin=0, vmax=1, cmap=cmap.get_cmap('hot_r')) fig.canvas.draw() plt.show()