def run(): # Call the C NSAT print('Begin %s:run()' % (os.path.splitext(os.path.basename(__file__))[0])) cfg = nsat.ConfigurationNSAT.readfileb(nsat.fnames.pickled) nsat.run_c_nsat() # Load the results (read binary files) c_nsat_reader = nsat.C_NSATReader(cfg, nsat.fnames) states = c_nsat_reader.read_c_nsat_states() time_core0, states_core0 = states[0][0], states[0][1] out_spikelist = nsat.importAER(nsat.read_from_file(nsat.fnames.events + '_core_0.dat'), sim_ticks=sim_ticks, id_list=[0]) # Plot the results fig = plt.figure(figsize=(10, 10)) for i in range(1, 9): ax = fig.add_subplot(8, 1, i) ax.plot(states_core0[:, 0, i - 1], 'b', lw=2) plt.savefig('/tmp/%s.png' % (os.path.splitext(os.path.basename(__file__))[0])) plt.close() print('Begin %s:run()' % (os.path.splitext(os.path.basename(__file__))[0]))
def run(fnames): # Call the C NSAT print('Begin %s:run()' % (os.path.splitext(os.path.basename(__file__))[0])) cfg = nsat.ConfigurationNSAT.readfileb(fnames.pickled) nsat.run_c_nsat(fnames) # Load the results (read binary files) c_nsat_reader = nsat.C_NSATReader(cfg, fnames) states = c_nsat_reader.read_c_nsat_states() states_core0 = states[0][1] # np.save('states', states_core0) in_spikelist = SL ifname = fnames.events + '_core_0.dat' out_spikelist = nsat.importAER(nsat.read_from_file(ifname), sim_ticks=sim_ticks, id_list=[0]) # Plot the results fig = plt.figure(figsize=(10, 10)) for i in range(1, 9): ax = fig.add_subplot(8, 1, i) ax.plot(states_core0[:-1, 15, i - 1], 'b', lw=1.5) # fig = plt.figure(figsize=(10, 10)) # for i in range(1, 9): # ax = fig.add_subplot(8, 1, i) # ax.plot(states_core0[:-1, 16, i-1], 'b', lw=1.5) # out_spikelist.raster_plot() plt.savefig('/tmp/%s.png' % (os.path.splitext(os.path.basename(__file__))[0])) plt.close() print('End %s:run()' % (os.path.splitext(os.path.basename(__file__))[0]))
def run(): # Call the C NSAT print('Begin %s:run()' % (os.path.splitext(os.path.basename(__file__))[0])) cfg = nsat.ConfigurationNSAT.readfileb(nsat.fnames.pickled) nsat.run_c_nsat() # Load the results (read binary files) c_nsat_reader = nsat.C_NSATReader(cfg, nsat.fnames) states = c_nsat_reader.read_c_nsat_states() states_core0 = states[0][1] # wt = c_nsat_reader.read_c_nsat_weights_evo(0)[:, 1, 1] wt, pids = c_nsat_reader.read_synaptic_weights_history(post=[0]) in_spikelist = SL out_spikelist = nsat.importAER(nsat.read_from_file(nsat.fnames.events+'_core_0.dat'), sim_ticks=sim_ticks, id_list=[0]) # Plot the results fig = plt.figure(figsize=(10, 10)) i = 1 ax = fig.add_subplot(4, 1, i) ax.plot(states_core0[:-1, 0, i - 1], 'b', lw=3, label='$V_m$') ax.set_ylabel('$V_m$') ax.set_xticks([]) plt.locator_params(axis='y', nbins=4) i = 2 ax = fig.add_subplot(4, 1, i) ax.plot(states_core0[:-1, 0, i - 1], 'b', lw=3, label='$I_{syn}$') ax.set_ylabel('$I_{syn}$') ax.set_xticks([]) plt.locator_params(axis='y', nbins=4) for t in SL[0].spike_times: plt.axvline(t, color='k') i = 4 ax = fig.add_subplot(4, 1, i) for t in SL[0].spike_times: plt.axvline(t, color='k') ax.plot(states_core0[:-1, 0, 2], 'b', lw=3, label='$x_m$') ax.set_ylabel('$x_m$') plt.axhline(0, color='b', alpha=.5, linewidth=3) plt.locator_params(axis='y', nbins=4) i = 3 ax = fig.add_subplot(4, 1, i) for t in SL[0].spike_times: plt.axvline(t, color='k') ax.plot(wt[0][:, 1, 1], 'r', lw=3) ax.set_ylabel('$w$') ax.set_xticks([]) plt.locator_params(axis='y', nbins=4) plt.savefig('/tmp/%s.png' % (os.path.splitext(os.path.basename(__file__))[0])) plt.close() print('End %s:run()' % (os.path.splitext(os.path.basename(__file__))[0]))
def read_spikelist(self, sim_ticks=None, id_list=None, core=0): if sim_ticks is None: sim_ticks = self.cfg.sim_ticks if id_list is None: id_list = self.cfg.spk_rec_mon[core] filename = self.fname.events + '_core_{0}.dat'.format(core) spikelist = nsat.importAER(self.read_events(core), sim_ticks=sim_ticks, id_list=id_list) return spikelist
def run(): # Call the C NSAT print('Begin %s:run()' % (os.path.splitext(os.path.basename(__file__))[0])) cfg = nsat.ConfigurationNSAT.readfileb(nsat.fnames.pickled) nsat.run_c_nsat() # Load the results (read binary files) c_nsat_reader = nsat.C_NSATReader(cfg, nsat.fnames) states = c_nsat_reader.read_c_nsat_states() states_core0 = states[0][1] plt.figure() for i in range(1, 5): plt.subplot(4, 1, i) plt.plot(states_core0[:, :, i - 1]) plt.figure() S = np.maximum(states_core0[:, :, 0], 0) plt.imshow( S, interpolation='spline36', cmap=plt.get_cmap('gray'), #plt.cm.gray, aspect='auto', origin='lower') spks = nsat.importAER(nsat.read_from_file(nsat.fnames.events + '_core_0.dat'), sim_ticks=sim_ticks, id_list=list(range(cfg.core_cfgs[0].n_neurons))) try: raster = spks.raster_plot() raster.savefig('/tmp/%s_raster.png' % (os.path.splitext(os.path.basename(__file__))[0])) raster.close() except: print('test_nf: Failed to generate /tmp/%s_raster.png' % (os.path.splitext(os.path.basename(__file__))[0])) # Plot the results # events = spks.convert() # mat = np.zeros((sim_ticks, N_NEURONS[0])) # print(mat.shape) # print(len(events[0])) # print(len(events[1])) # mat[events[0].astype('i'), events[1].astype('i')] = 1 plt.savefig('/tmp/%s.png' % (os.path.splitext(os.path.basename(__file__))[0])) plt.close() print('End %s:run()' % (os.path.splitext(os.path.basename(__file__))[0]))
def SimSpikingStimulus(rates, t_sim=None): m = np.shape(rates)[0] n = int(m / 2) C1 = (np.ones((n, n)) + np.random.uniform(0, 1, (n, n)) * 2) np.fill_diagonal(C1, rates[:n]) C1 = np.maximum(C1, C1.T) cor_spk1 = correlated_spikes(C1, rates, n) cor_spk1.cox_process(time=t_sim) spk1 = cor_spk1.extract_pyNCS_list() tmp1 = nsat.importAER(spk1) SL = pyST.SpikeList(id_list=list(range(m))) for i in range(m): if i < n: SL[i] = tmp1[i] if i >= n: SL[i] = pyST.STCreate.poisson_generator(rates[i], t_stop=t_sim) return SL
def run(fnames): # Call the C NSAT print('Begin %s:run()' % (os.path.splitext(os.path.basename(__file__))[0])) cfg = nsat.ConfigurationNSAT.readfileb(fnames.pickled) nsat.run_c_nsat(fnames) # Load the results (read binary files) c_nsat_reader = nsat.C_NSATReader(cfg, fnames) ww = np.array(c_nsat_reader.read_c_nsat_synaptic_weights()[0]) spk = nsat.importAER(c_nsat_reader.read_events(0), sim_ticks=sim_ticks) spk.raster_plot() fig = plt.figure() ax = fig.add_subplot(111) ax.plot(ww[:cfg.core_cfgs[0].n_inputs, cfg.core_cfgs[0].n_inputs, 1], 'k.') plt.savefig('/tmp/%s.png' % (os.path.splitext(os.path.basename(__file__))[0])) plt.close() print('End %s:run()' % (os.path.splitext(os.path.basename(__file__))[0]))
def run(fnames): # Call the C NSAT print('Begin %s:run()' % (os.path.splitext(os.path.basename(__file__))[0])) cfg = nsat.ConfigurationNSAT.readfileb(fnames.pickled) nsat.run_c_nsat(fnames) print("Plotting data") # Load the results (read binary files) c_nsat_reader = nsat.C_NSATReader(cfg, fnames) states = c_nsat_reader.read_c_nsat_states() time_core0, states_core0 = states[0][0], states[0][1] pip = nsat.importAER(c_nsat_reader.read_events(0), sim_ticks=sim_ticks, id_list=list(range( cfg.core_cfgs[0].n_neurons))).time_slice( 1000, sim_ticks - 1000).mean_rates() x = np.arange(pip.shape[0]) from scipy.optimize import curve_fit # define "to-fit" function def sigmoid(x, a, b, c): return a / (1 + np.exp(-b * x / a) * (a - c) / a) # Fit data to function sigmoid popt, pcov = curve_fit(sigmoid, x, pip) print(("Sigmoid's parameters: a = {}, b = {}, c = {}".format( popt[0], popt[1], popt[2]))) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(x, pip, 'k', lw=2) ax.plot(x, sigmoid(x, popt[0], popt[1], popt[2]), 'r--', lw=2) plt.savefig('/tmp/%s.png' % (os.path.splitext(os.path.basename(__file__))[0])) plt.close() print('End %s:run()' % (os.path.splitext(os.path.basename(__file__))[0]))
def run(fnames): # Call the C NSAT print('Begin %s:run()' % (os.path.splitext(os.path.basename(__file__))[0])) cfg = nsat.ConfigurationNSAT.readfileb(fnames.pickled) nsat.run_c_nsat(fnames) # Load the results (read binary files) c_nsat_reader = nsat.C_NSATReader(cfg, fnames) states = c_nsat_reader.read_c_nsat_states() _, states_core0 = states[0][0], states[0][1] _, states_core1 = states[1][0], states[1][1] # Plot the results fig = plt.figure(figsize=(10, 10)) for i in range(1, states_core0.shape[2] + 1): ax = fig.add_subplot(2, 1, i) ax.plot(states_core0[:-1, 0, i - 1], 'b', lw=3) plt.savefig('/tmp/%s_0.png' % (os.path.splitext(os.path.basename(__file__))[0])) fig = plt.figure(figsize=(10, 10)) for i in range(1, states_core0.shape[2] + 1): ax = fig.add_subplot(2, 1, i) ax.plot(states_core1[:-1, 0, i - 1], 'b', lw=3) plt.savefig('/tmp/%s_1.png' % (os.path.splitext(os.path.basename(__file__))[0])) plt.close() print('End %s:run()' % (os.path.splitext(os.path.basename(__file__))[0])) out_spikelist = nsat.importAER(c_nsat_reader.read_events(0), sim_ticks=100, id_list=[0]) out_spikelist.id_slice([0]).raster_plot(kwargs={'color': 'b'}) plt.show()
def run(fnames): # Call the C NSAT print('Begin %s:run()' % (os.path.splitext(os.path.basename(__file__))[0])) cfg = nsat.ConfigurationNSAT.readfileb(fnames.pickled) nsat.run_c_nsat(fnames) # Load the results (read binary files) c_nsat_reader = nsat.C_NSATReader(cfg, fnames) states = c_nsat_reader.read_c_nsat_states() states_core0 = states[0][1] in_spikelist = SL out_spikelist = nsat.importAER(c_nsat_reader.read_events(0), sim_ticks=sim_ticks, id_list=[0]) spks = out_spikelist.convert("[times,ids]") spks = np.vstack([spks[0], spks[1]]).astype('int') pretty_fig(spks, states_core0, t_stop=t_stop) plt.savefig('/tmp/%s.png' % (os.path.splitext(os.path.basename(__file__))[0])) plt.close() print('End %s:run()' % (os.path.splitext(os.path.basename(__file__))[0]))
# Write C NSAT parameters binary files c_nsat_writer = nsat.C_NSATWriter(cfg, path='/tmp', prefix='test_eight_states_neuron') c_nsat_writer.write() # Write Intel FPGA parameters hex files # intel_fpga_writer = nsat.IntelFPGAWriter(cfg, path='.', # prefix='test_eight_states_neuron') # intel_fpga_writer.write() # intel_fpga_writer.write_globals() # Call the C NSAT print("Running C NSAT!") nsat.run_c_nsat(c_nsat_writer.fname) # Load the results (read binary files) c_nsat_reader = nsat.C_NSATReader(cfg, c_nsat_writer.fname) states = c_nsat_reader.read_c_nsat_states() time_core0, states_core0 = states[0][0], states[0][1] out_spikelist = nsat.importAER(nsat.read_from_file(c_nsat_writer.fname.events+'_core_0.dat'), sim_ticks=sim_ticks, id_list=[0]) # Plot the results fig = plt.figure(figsize=(10, 10)) for i in range(1, 9): ax = fig.add_subplot(8, 1, i) ax.plot(states_core0[:, 0, i-1], 'b', lw=2) plt.show()
cfg.set_ext_events(ext_evts_data) # Write C NSAT parameters binary files c_nsat_writer = nsat.C_NSATWriter(cfg, path='/tmp', prefix='test_wta') c_nsat_writer.write() # Write Intel FPGA parameters hex files ## intel_fpga_writer = nsat.IntelFPGAWriter(cfg, path='.', ## prefix='test_wta') ## intel_fpga_writer.write() ## intel_fpga_writer.write_globals() # Call the C NSAT print("Running C NSAT!") nsat.run_c_nsat(c_nsat_writer.fname) # Load the results (read binary files) c_nsat_reader = nsat.C_NSATReader(cfg, c_nsat_writer.fname) states = c_nsat_reader.read_c_nsat_states() states_core0 = states[0][1] in_apikelist = SL out_spikelist = nsat.importAER(c_nsat_reader.read_c_nsat_raw_events()[0], sim_ticks=sim_ticks, id_list=[0]) spks = out_spikelist.convert("[times,ids]") spks = np.vstack([spks[0], spks[1]]).astype('int') pretty_fig(spks, states_core0, t_stop=t_stop) plt.show()
print("Epoch #: ", i) c_nsat_writer_train.fname.stats_nsat = \ '/tmp/test_eCD_stats_nsat_full'+str(i) nsat.run_c_nsat(c_nsat_writer_train.fname) if n_epoch > 1: shutil.copyfile( fname_train.shared_mem + '_core_0.dat', fname_train.syn_wgt_table + '_core_0.dat', ) shutil.copyfile( fname_train.shared_mem + '_core_0.dat', fname_test.syn_wgt_table + '_core_0.dat', ) nsat.run_c_nsat(c_nsat_writer_test.fname) test_spk = nsat.importAER( nsat.read_from_file(c_nsat_writer_test.fname.events + '_core_0.dat'), sim_ticks=sim_ticks_test) test_spk = nsat.importAER(c_nsat_reader_test.read_events(0), sim_ticks=sim_ticks_test) # train_spk = nsat.importAER(nsat.read_from_file( # c_nsat_writer_train.fname.events+'_core_0.dat'), # sim_ticks=sim_ticks_train) sl = test_spk.id_slice([16, 17]) mm = np.argmax(sl.firing_rate(time_bin=test_duration), axis=0)[::2] print(100 * sum(labels[::2] != mm) / 32.0) e.append(sum(labels[::2] != mm) / 32.0) e = np.array(e) np.save('/tmp/error_full', e) # fig = plt.figure() # ax = fig.add_subplot(111)
c_nsat_writer.write() # Call the C NSAT print("Running C NSAT!") nsat.run_c_nsat(c_nsat_writer.fname) # Load the results (read binary files) c_nsat_reader = nsat.C_NSATReader(cfg, c_nsat_writer.fname) states = c_nsat_reader.read_c_nsat_states() states_core0 = states[0][1] # np.save('states', states_core0) in_spikelist = SL ifname = c_nsat_writer.fname.events + '_core_0.dat' out_spikelist = nsat.importAER(nsat.read_from_file(ifname), sim_ticks=sim_ticks, id_list=[0]) # Plot the results fig = plt.figure(figsize=(10, 10)) for i in range(1, 9): ax = fig.add_subplot(8, 1, i) ax.plot(states_core0[:-1, 15, i - 1], 'b', lw=1.5) # fig = plt.figure(figsize=(10, 10)) # for i in range(1, 9): # ax = fig.add_subplot(8, 1, i) # ax.plot(states_core0[:-1, 16, i-1], 'b', lw=1.5) # out_spikelist.raster_plot() plt.show()
cfg.core_cfgs[0].latex_print_parameters(1) print("Running C NSAT!") nsat.run_c_nsat(c_nsat_writer.fname) c_nsat_reader = nsat.C_NSATReader(cfg, c_nsat_writer.fname) states = c_nsat_reader.read_c_nsat_states() states_core0 = states[0][1] np.save('states_vmem_', states_core0) fW = c_nsat_reader.read_c_nsat_synaptic_weights() # Load results # Output spikes test_spikelist = nsat.importAER( nsat.read_from_file(c_nsat_writer.fname.events + '_core_0.dat'), sim_ticks=sim_ticks, id_list=range(N_NEURONS)) test_spikelist.save('nsat_spikes') # # Plot data figure(figsize=(8, 4)) gs = gridspec.GridSpec(4 + 5, 1) ax1 = plt.subplot(gs[0:3, 0]) np.save('times_vmem_', tp) for t in tp: ax1.fill_between([t - 25, t + pdict['tp'] - 25], [0, 0], [N_INPUTS, N_INPUTS], linewidth=0, alpha=0.5, facecolor='b')
# Call the C NSAT print("Running C NSAT!") nsat.run_c_nsat(c_nsat_writer.fname) # Load the results (read binary files) c_nsat_reader = nsat.C_NSATReader(cfg, c_nsat_writer.fname) states = c_nsat_reader.read_c_nsat_states() time_core0, states_core0 = states[0][0], states[0][1] wt = c_nsat_reader.read_c_nsat_weights_evo([0, 1])[0] # cfg.write_hex_weights_monitors(wt[:, 0, 1]) # out_spikelist = nsat.importAER(nsat.read_from_file(c_nsat_writer.fname.events+'_core_0.dat'), # sim_ticks=sim_ticks, id_list=[0]) out_spikelist = nsat.importAER( nsat.read_from_file(c_nsat_writer.fname.events + '_core_0.dat'.encode('utf-8')), sim_ticks=sim_ticks) # Plot the results fig = plt.figure(figsize=(15, 15)) ax = fig.add_subplot(4, 2, 1) ax.plot(states_core0[:-1, 0, 0], 'b', lw=3, label='$x_m$') ax.set_ylabel('$V_m$') ax = fig.add_subplot(4, 2, 2) ax.plot(states_core0[:-1, 1, 0], 'b', lw=3, label='$x_m$') ax.set_ylabel('$V_m$') ax = fig.add_subplot(4, 2, 3) ax.plot(states_core0[:-1, 0, 1], 'b', lw=3, label='$I_{syn}$')
print("############# Running simulation #####################") pip = [] for i in range(nepochs): nsat.run_c_nsat(fname_train) copyfile(fname_train.shared_mem+'_core_0.dat', fname_test.syn_wgt_table+'_core_0.dat',) # cfg_test.core_cfgs[0].W = c_nsat_reader_train.read_c_nsat_weights()[0] # cfg_train.core_cfgs[0].W = cfg_test.core_cfgs[0].W.copy() # c_nsat_writer_test.write_L0connectivity() # c_nsat_writer_train.write_L0connectivity() if test_every>0: if i % test_every == test_every-1: nsat.run_c_nsat(fname_test) test_spikelist = nsat.importAER(c_nsat_reader_test.read_c_nsat_raw_events()[0], sim_ticks=sim_ticks_test, id_list=np.arange(sP, sP+Np)) pip .append([i, float(sum(np.argmax(test_spikelist.id_slice(range(sP,sP+Np)).firing_rate(t_sample_test).T,axis=1) == targets_classify[:N_test]))/N_test*100]) print exp_name print pip copyfile(fname_train.shared_mem+'_core_0.dat', fname_train.syn_wgt_table+'_core_0.dat',) try: import experimentTools as et d=et.mksavedir() et.save(cfg_test, 'cfg_test.pkl') et.save(cfg_train, 'cfg_train.pkl') et.save(pip, 'pip.pkl')
def run(): # Call the C NSAT global spk0 print('Begin %s:run()' % (os.path.splitext(os.path.basename(__file__))[0])) cfg = nsat.ConfigurationNSAT.readfileb(nsat.fnames.pickled) nsat.run_c_nsat() # Load the results (read binary files) c_nsat_reader = nsat.C_NSATReader(cfg, nsat.fnames) #ww = np.array(c_nsat_reader.read_c_nsat_synaptic_weights()[0]) states = c_nsat_reader.read_c_nsat_states() time_core0, states_core0 = states[0][0], states[0][1] # wt = c_nsat_reader.read_c_nsat_syn_evo()[0][0] wt, pids = c_nsat_reader.read_synaptic_weights_history() wt = wt[0] spk = nsat.importAER(c_nsat_reader.read_events(0), sim_ticks=sim_ticks) spk.raster_plot() print((SL[0].spike_times)) print((SL[1].spike_times)) print((spk[0].spike_times)) # fig = plt.figure() # ax = fig.add_subplot(111) # ax.plot(ww[:N_INPUTS[0], N_INPUTS[0], 1], 'k.') # Plot the results fig = plt.figure(figsize=(15, 15)) ax = fig.add_subplot(4, 2, 1) ax.plot(states_core0[:-1, 0, 0], 'b', lw=3, label='$x_m$') for i in spk[0].spike_times: plt.axvline(i, color='k', lw=1) ax.set_ylabel('$V_m$') # ax = fig.add_subplot(4, 2, 2) # ax.plot(states_core0[:-1, 1, 0], 'b', lw=3, label='$x_m$') # ax.set_ylabel('$V_m$') ax = fig.add_subplot(4, 2, 3) ax.plot(states_core0[:-1, 0, 1], 'b', lw=3, label='$I_{syn}$') ax.set_ylabel('$I_{syn}$') # ax = fig.add_subplot(4, 2, 4) # ax.plot(states_core0[:-1, 1, 1], 'b', lw=3, label='$I_{syn}$') # ax.set_ylabel('$I_{syn}$') ax = fig.add_subplot(4, 2, 5) ax.plot(states_core0[:-1, 0, 2], 'b', lw=3, label='$x_m$') ax.set_ylabel('$x_m$') # ax = fig.add_subplot(4, 2, 6) # ax.plot(states_core0[:-1, 1, 1], 'b', lw=3, label='$x_m$') # ax.set_ylabel('$x_m$') print(wt.shape) ax = fig.add_subplot(4, 2, 7) # ax.plot(wt[wt[:, 0, :] == 0, 0], wt[wt[:, 1, :] == 0, 4], 'r', lw=3) ax.plot(wt[:, 0, 0], wt[:, 1, 0], 'r', lw=3) for i in spk[0].spike_times: plt.axvline(i, color='k', lw=1) for i in spk0[1].spike_times: plt.axvline(i, color='r', lw=1) ax.set_ylabel('$w$') ax = fig.add_subplot(4, 2, 8) # ax.plot(wt[wt[:, 1, :] == 1, 0], wt[wt[:, 1, :] == 1, 4], 'r', lw=3) for i in spk[0].spike_times: plt.axvline(i, color='k', lw=1) for i in spk0[1].spike_times: plt.axvline(i, color='r', lw=1) ax.set_ylabel('$w$') # ax = fig.add_subplot(4, 2, 8) # ax.plot(wt[:, 1, 1], 'r', lw=3) # ax.set_ylabel('$w$') # fig = plt.figure(figsize=(10, 5)) # for i in spk[0].spike_times: # plt.plot(wt[:, 1, 1], 'r', lw=3) # plt.axvline(i, color='k', lw=1) plt.savefig('/tmp/%s.png' % (os.path.splitext(os.path.basename(__file__))[0])) plt.close() print('End %s:run()' % (os.path.splitext(os.path.basename(__file__))[0]))
def run(fnames): # Call the C NSAT print('Begin %s:run()' % (os.path.splitext(os.path.basename(__file__))[0])) cfg = nsat.ConfigurationNSAT.readfileb(fnames.pickled) nsat.run_c_nsat(fnames) # Load the results (read binary files) c_nsat_reader = nsat.C_NSATReader(cfg, fnames) states = c_nsat_reader.read_c_nsat_states() time_core0, states_core0 = states[0][0], states[0][1] # w0 = c_nsat_reader.read_synaptic_weights_history(post=0)[0][:, 0, 1] w0, pids0 = c_nsat_reader.read_synaptic_weights_history(post=[5]) w0 = w0[0][:, 4, 1] w1, pids1 = c_nsat_reader.read_synaptic_weights_history(post=[6]) w1 = w1[0][:, 4, 1] w2, pids2 = c_nsat_reader.read_synaptic_weights_history(post=[7]) w2 = w2[0][:, 4, 1] np.save('w0', w0) np.save('w1', w1) np.save('w2', w2) # w = c_nsat_reader.read_synaptic_weights() out_spikelist = nsat.importAER(c_nsat_reader.read_events(0), sim_ticks=sim_ticks) np.save('spk0', out_spikelist[0].spike_times) np.save('spk1', out_spikelist[1].spike_times) np.save('spk2', out_spikelist[2].spike_times) np.save('spk3', out_spikelist[3].spike_times) # Plot the results fig = plt.figure(figsize=(15, 15)) ax = fig.add_subplot(5, 2, 1) ax.plot(states_core0[:-1, 0, 0], 'b', lw=3, label='$x_m$') ax.set_ylabel('$V_m$') ax = fig.add_subplot(5, 2, 2) ax.plot(states_core0[:-1, 1, 0], 'b', lw=3, label='$x_m$') ax.set_ylabel('$V_m$') ax = fig.add_subplot(5, 2, 3) ax.plot(states_core0[:-1, 0, 1], 'b', lw=3, label='$I_{syn}$') ax.set_ylabel('$I_{syn}$') ax = fig.add_subplot(5, 2, 4) ax.plot(states_core0[:-1, 1, 1], 'b', lw=3, label='$I_{syn}$') ax.set_ylabel('$I_{syn}$') ax = fig.add_subplot(5, 2, 5) ax.plot(states_core0[:-1, 0, 2], 'b', lw=3, label='$x_m$') ax.set_ylabel('$x_m$') ax = fig.add_subplot(5, 2, 6) ax.plot(states_core0[:-1, 1, 2], 'b', lw=3, label='$x_m$') ax.set_ylabel('$x_m$') fig = plt.figure() ax = fig.add_subplot(4, 1, 1) for i in range(4): for i in out_spikelist[0].spike_times: ax.axvline(i, color='k', lw=1, zorder=0, label='lal') ax.set_ylabel('Spikes') ax.set_xlim([0, sim_ticks]) # ax = fig.add_subplot(4, 1, 2) ax.step(w0, 'r', lw=2, zorder=10, where='post', label='lala') for i in out_spikelist[1].spike_times: ax.axvline(i, color='k', lw=1, zorder=0, label='dsds') # ax.set_ylabel('Spikes') # ax.set_xlim([0, sim_ticks]) # ax = fig.add_subplot(4, 1, 3) ax.step(w1, 'r', lw=2, zorder=10, where='post', label='ta') for i in out_spikelist[2].spike_times: ax.axvline(i, color='k', lw=1, zorder=0, label='dss') # ax.set_xlim([0, sim_ticks]) # ax = fig.add_subplot(4, 1, 4) ax.step(w2, 'r', lw=2, zorder=10, where='post', label='ar') for i in out_spikelist[3].spike_times: ax.axvline(i, color='k', lw=1, zorder=0, label='dadsa') # ax.set_xlim([0, sim_ticks]) ax.set_xlim([0, sim_ticks]) plt.savefig('/tmp/%s.png' % (os.path.splitext(os.path.basename(__file__))[0])) plt.close() print('End %s:run()' % (os.path.splitext(os.path.basename(__file__))[0]))
c_nsat_reader = nsat.C_NSATReader(cfg, c_nsat_writer.fname) states = c_nsat_reader.read_c_nsat_states() states_core0 = states[0][1] np.save('states_vmem_', states_core0) fW = c_nsat_reader.read_c_nsat_synaptic_weights() # Load results # Output spikes # test_spikelist = nsat.importAER(nsat.read_from_file(c_nsat_writer.fname.events + '_core_0.dat'), # sim_ticks=sim_ticks, # id_list=range(N_NEURONS)) test_spikelist = nsat.importAER(c_nsat_reader.read_events(0), sim_ticks=sim_ticks, id_list=range(N_NEURONS)) test_spikelist.save('nsat_spikes') # # Plot data figure(figsize=(8, 4)) gs = gridspec.GridSpec(4 + 5, 1) ax1 = plt.subplot(gs[0:3, 0]) np.save('times_vmem_', tp) for t in tp: ax1.fill_between([t - 25, t + pdict['tp'] - 25], [0, 0], [N_INPUTS, N_INPUTS], linewidth=0, alpha=0.5, facecolor='b')
# Write the C NSAT parameters binary files c_nsat_writer = nsat.C_NSATWriterMultithread(cfg, path='/tmp', prefix='test_stdp') c_nsat_writer.write() # Write Intel FPGA hex parameters files # intel_fpga_writer = nsat.IntelFPGAWriter(cfg, path='.', # prefix='test_stdp') # intel_fpga_writer.write() # intel_fpga_writer.write_globals() # Call the C NSAT print("Running C NSAT!") nsat.run_c_nsat(c_nsat_writer.fname) # Load the results (read binary files) c_nsat_reader = nsat.C_NSATReader(cfg, c_nsat_writer.fname) ww = np.array(c_nsat_reader.read_c_nsat_synaptic_weights()[0]) # spk = nsat.importAER(nsat.read_from_file(c_nsat_writer.fname.events+'_core_0.dat'), sim_ticks=sim_ticks) spk = nsat.importAER(c_nsat_reader.read_c_nsat_raw_events()[0], sim_ticks=sim_ticks) spk.raster_plot() fig = plt.figure() ax = fig.add_subplot(111) ax.plot(ww[:N_INPUTS[0], N_INPUTS[0], 1], 'k.') plt.show()
# intel_fpga_writer.write_globals() # Call the C NSAT print("Running C NSAT!") nsat.run_c_nsat(c_nsat_writer.fname) # Load the results (read binary files) c_nsat_reader = nsat.C_NSATReader(cfg, c_nsat_writer.fname) #ww = np.array(c_nsat_reader.read_c_nsat_synaptic_weights()[0]) states = c_nsat_reader.read_c_nsat_states() time_core0, states_core0 = states[0][0], states[0][1] # wt = c_nsat_reader.read_c_nsat_weights_evo(2)[0] wt = c_nsat_reader.read_c_nsat_syn_evo()[0][0] spk = nsat.importAER(nsat.read_from_file(c_nsat_writer.fname.events + '_core_0.dat'), sim_ticks=sim_ticks) spk.raster_plot() print((SL[0].spike_times)) print((SL[1].spike_times)) print((spk[0].spike_times)) #fig = plt.figure() #ax = fig.add_subplot(111) #ax.plot(ww[:N_INPUTS[0], N_INPUTS[0], 1], 'k.') # Plot the results fig = plt.figure(figsize=(15, 15)) ax = fig.add_subplot(4, 2, 1)
def run(): # Call the C NSAT print('Begin %s:run()' % (os.path.splitext(os.path.basename(__file__))[0])) cfg = nsat.ConfigurationNSAT.readfileb(nsat.fnames.pickled) nsat.run_c_nsat() # Load the results (read binary files) c_nsat_reader = nsat.C_NSATReader(cfg, nsat.fnames) states = c_nsat_reader.read_c_nsat_states() time_core0, states_core0 = states[core][0], states[core][1] wt, pids = c_nsat_reader.read_synaptic_weights_history( post=[130, 150, 120]) wt, pids = wt[0], pids[0] in_spikelist = SL out_spikelist = nsat.importAER(c_nsat_reader.read_events(0), sim_ticks=sim_ticks, id_list=[0]) plt.matplotlib.rcParams['figure.subplot.bottom'] = .1 plt.matplotlib.rcParams['figure.subplot.left'] = .2 plt.matplotlib.rcParams['figure.subplot.right'] = .98 plt.matplotlib.rcParams['figure.subplot.hspace'] = .1 plt.matplotlib.rcParams['figure.subplot.top'] = 1.0 # Plot the results fig = plt.figure(figsize=(14, 10)) i = 4 ax1 = fig.add_subplot(5, 1, i) for t in SL[100].spike_times: plt.axvline(t, color='g', alpha=.4) ax1.plot(states_core0[:-1, 0, 2], 'b', lw=3, label='$x_m$') ax1.set_ylabel('$x_m$') ax1.get_yaxis().set_label_coords(-0.12, 0.5) plt.setp(ax1.get_xticklabels(), visible=False) plt.axhline(0, color='b', alpha=.5, linewidth=3) plt.locator_params(axis='y', nbins=4) i = 5 ax = fig.add_subplot(5, 1, i, sharex=ax1) for t in SL[0].spike_times: plt.axvline(t, color='k', alpha=.4) ax.plot(wt[:, 19, 1], 'r', lw=3) # ax.imshow(wt[:, :, 1], aspect='auto', interpolation='nearest') ax.set_ylabel('$w$') ax.set_xlabel('Time Step') ax.get_yaxis().set_label_coords(-0.12, 0.5) plt.locator_params(axis='y', nbins=4) i = 2 ax = fig.add_subplot(5, 1, i, sharex=ax1) ax.plot(states_core0[:-1, 0, 0], 'b', lw=3, label='$V_m$') ax.set_ylabel('$V_m$') ax.get_yaxis().set_label_coords(-0.12, 0.5) plt.setp(ax.get_xticklabels(), visible=False) plt.locator_params(axis='y', nbins=4) i = 3 ax = fig.add_subplot(5, 1, i, sharex=ax1) ax.plot(states_core0[:-1, 0, 1], 'b', lw=3, label='$I_{syn}$') ax.set_ylabel('$I_{syn}$') ax.get_yaxis().set_label_coords(-0.12, 0.5) plt.setp(ax.get_xticklabels(), visible=False) plt.locator_params(axis='y', nbins=4) for t in np.ceil(SL[0].spike_times): plt.axvline(t, color='k', alpha=.4) ax1 = fig.add_subplot(5, 1, 1, sharex=ax1) out_spikelist.id_slice([0]).raster_plot(display=ax1, kwargs={'color': 'b'}) out_spikelist.id_slice(list(range(1, 30))).raster_plot(display=ax1, kwargs={'color': 'k'}) ax1.set_xlabel('') plt.setp(ax1.get_xticklabels(), visible=False) ax1.get_yaxis().set_label_coords(-0.12, 0.5) plt.tight_layout() plt.savefig('/tmp/%s.png' % (os.path.splitext(os.path.basename(__file__))[0])) plt.close() print('End %s:run()' % (os.path.splitext(os.path.basename(__file__))[0]))
copyfile( fname_train.shared_mem + '_core_0.dat', fname_test.syn_wgt_table + '_core_0.dat', ) # c_nsat_writer_test.write_L0connectivity() # c_nsat_writer_train.write_L0connectivity() s = c_nsat_reader_train.read_stats()[0] stats_nsat.append(s) #stats_ext.append(n_ext) if test_every > 0: if i % test_every == test_every - 1: nsat.run_c_nsat(fname_test) test_spikelist = nsat.importAER( nsat.read_from_file(fname_test.events + '_core_0.dat'), sim_ticks=sim_ticks_test, id_list=np.arange(sP, sP + Np)) pip.append([ i, float( sum( np.argmax(test_spikelist.id_slice(range( sP, sP + Np)).firing_rate(t_sample_test).T, axis=1) == targets_classify[:N_test])) / N_test * 100 ]) print exp_name print pip copyfile(
# Call the C NSAT print("Running C NSAT!") nsat.run_c_nsat(c_nsat_writer.fname) print("Plotting data") # Load the results (read binary files) c_nsat_reader = nsat.C_NSATReader(cfg, c_nsat_writer.fname) states = c_nsat_reader.read_c_nsat_states() time_core0, states_core0 = states[0][0], states[0][1] # pip = nsat.importAER(nsat.read_from_file(c_nsat_writer.fname.events+'_core_0.dat'), # sim_ticks=sim_ticks, # id_list=range(N_NEURONS[0])).time_slice(1000, sim_ticks-1000).mean_rates() pip = nsat.importAER(c_nsat_reader.read_c_nsat_raw_events()[0], sim_ticks=sim_ticks, id_list=list(range(N_NEURONS[0]))).time_slice( 1000, sim_ticks - 1000).mean_rates() x = np.arange(pip.shape[0]) from scipy.optimize import curve_fit # define "to-fit" function def sigmoid(x, a, b, c): return a / (1 + np.exp(-b * x / a) * (a - c) / a) # Fit data to function sigmoid popt, pcov = curve_fit(sigmoid, x, pip) print(("Sigmoid's parameters: a = {}, b = {}, c = {}".format( popt[0], popt[1], popt[2]))) fig = plt.figure()
def run(fnames): # Call the C NSAT print('Begin %s:run()' % (os.path.splitext(os.path.basename(__file__))[0])) cfg = nsat.ConfigurationNSAT.readfileb(fnames.pickled) nsat.run_c_nsat(fnames) # Load the results (read binary files) c_nsat_reader = nsat.C_NSATReader(cfg, fnames) states = c_nsat_reader.read_c_nsat_states() time_core0, states_core0 = states[0][0], states[0][1] wt, _ = c_nsat_reader.read_c_nsat_weights_evo([0, 1]) wt = wt[0] out_spikelist = nsat.importAER(nsat.read_from_file(fnames.events + '_core_0.dat'), sim_ticks=sim_ticks) # Plot the results fig = plt.figure(figsize=(15, 15)) ax = fig.add_subplot(4, 2, 1) ax.plot(states_core0[:-1, 0, 0], 'b', lw=3, label='$x_m$') ax.set_ylabel('$V_m$') ax = fig.add_subplot(4, 2, 2) ax.plot(states_core0[:-1, 1, 0], 'b', lw=3, label='$x_m$') ax.set_ylabel('$V_m$') ax = fig.add_subplot(4, 2, 3) ax.plot(states_core0[:-1, 0, 1], 'b', lw=3, label='$I_{syn}$') ax.set_ylabel('$I_{syn}$') ax = fig.add_subplot(4, 2, 4) ax.plot(states_core0[:-1, 1, 1], 'b', lw=3, label='$I_{syn}$') ax.set_ylabel('$I_{syn}$') ax = fig.add_subplot(4, 2, 5) ax.plot(states_core0[:-1, 0, 2], 'b', lw=3, label='$x_m$') ax.set_ylabel('$x_m$') ax = fig.add_subplot(4, 2, 6) ax.plot(states_core0[:-1, 1, 2], 'b', lw=3, label='$x_m$') ax.set_ylabel('$x_m$') ax = fig.add_subplot(4, 2, 7) ax.plot(wt[:, 1, 1], 'r', lw=3) ax.set_ylabel('$w$') ax = fig.add_subplot(4, 2, 8) ax.plot(wt[:, 0, 1], 'r', lw=3) ax.set_ylabel('$w$') fig = plt.figure(figsize=(10, 5)) plt.plot(wt[:, 0, 1], 'r', lw=2, zorder=10) plt.plot(wt[:, 1, 1], 'y', lw=2, zorder=10) # for i in out_spikelist[0].spike_times: # plt.axvline(i, color='k', lw=1, zorder=0) # for i in out_spikelist[1].spike_times: # plt.axvline(i, color='b', lw=1, zorder=0) plt.savefig('/tmp/%s.png' % (os.path.splitext(os.path.basename(__file__))[0])) plt.close() print('End %s:run()' % (os.path.splitext(os.path.basename(__file__))[0]))
# Load the results (read binary files) c_nsat_reader = nsat.C_NSATReader(cfg, c_nsat_writer.fname) states = c_nsat_reader.read_c_nsat_states() states_core0 = states[0][1] plt.figure() for i in range(1, 5): plt.subplot(4, 1, i) plt.plot(states_core0[:, :, i - 1]) plt.figure() S = np.maximum(states_core0[:, :, 0], 0) plt.imshow(S, interpolation='spline36', cmap=plt.cm.gray, aspect='auto', origin='lower') spks = nsat.importAER(nsat.read_from_file(c_nsat_writer.fname.events + '_core_0.dat'), sim_ticks=sim_ticks, id_list=list(range(N_NEURONS[0]))) spks.raster_plot() # Plot the results events = spks.convert() mat = np.zeros((sim_ticks, N_NEURONS[0])) mat[events[0].astype('i'), events[1].astype('i')] = 1 plt.show()