def test_rates(Simulator, seed, logger): pytest.importorskip('scipy') functions = [ ('isi_zero', lambda t, s: rates_isi( t, s, midpoint=False, interp='zero')), ('isi_midzero', lambda t, s: rates_isi( t, s, midpoint=True, interp='zero')), ('isi_linear', lambda t, s: rates_isi( t, s, midpoint=False, interp='linear')), ('isi_midlinear', lambda t, s: rates_isi( t, s, midpoint=True, interp='linear')), ('kernel_expon', lambda t, s: rates_kernel(t, s, kind='expon')), ('kernel_gauss', lambda t, s: rates_kernel(t, s, kind='gauss')), ('kernel_expogauss', lambda t, s: rates_kernel( t, s, kind='expogauss')), ('kernel_alpha', lambda t, s: rates_kernel(t, s, kind='alpha')), ] for name, function in functions: rel_rmse = _test_rates(Simulator, function, None, seed) logger.info('rate estimator: %s', name) logger.info('relative RMSE: %0.4f', rel_rmse)
def test_rates(Simulator, seed): pytest.importorskip("scipy") functions = [ ("isi_zero", lambda t, s: rates_isi(t, s, midpoint=False, interp="zero")), ("isi_midzero", lambda t, s: rates_isi(t, s, midpoint=True, interp="zero")), ("isi_linear", lambda t, s: rates_isi(t, s, midpoint=False, interp="linear")), ("isi_midlinear", lambda t, s: rates_isi(t, s, midpoint=True, interp="linear")), ("kernel_expon", lambda t, s: rates_kernel(t, s, kind="expon")), ("kernel_gauss", lambda t, s: rates_kernel(t, s, kind="gauss")), ("kernel_expogauss", lambda t, s: rates_kernel(t, s, kind="expogauss")), ("kernel_alpha", lambda t, s: rates_kernel(t, s, kind="alpha")), ] for name, function in functions: rel_rmse = _test_rates(Simulator, function, None, seed) logging.info("rate estimate: %s", name) logging.info("relative RMSE: %0.4f", rel_rmse)
def run(model_dict, t_end, dt=0.001, kernel_tau=0.1, progress_bar=True, save_results=False): with nengo.Simulator(model_dict['network'], optimize=True, dt=dt, progress_bar=TerminalProgressBar() if progress_bar else None) as sim: sim.run(np.max(t_end)) p_srf_output_spikes = model_dict['neuron_probes']['srf_output_spikes'] p_srf_exc_spikes = model_dict['neuron_probes']['srf_exc_spikes'] p_srf_inh_spikes = model_dict['neuron_probes']['srf_inh_spikes'] p_decoder_spikes = model_dict['neuron_probes']['decoder_spikes'] p_decoder_inh_spikes = model_dict['neuron_probes']['decoder_inh_spikes'] p_srf_exc_weights = model_dict['weight_probes']['srf_exc_weights'] p_srf_inh_weights = model_dict['weight_probes']['srf_inh_weights'] p_srf_rec_weights = model_dict['weight_probes']['srf_rec_weights'] p_decoder_weights = model_dict['weight_probes']['decoder_weights'] srf_rec_weights = sim.data.get(p_srf_rec_weights, None) srf_exc_weights = sim.data[p_srf_exc_weights] srf_inh_weights = sim.data[p_srf_inh_weights] srf_output_spikes = sim.data[p_srf_output_spikes] srf_exc_spikes = sim.data[p_srf_exc_spikes] srf_inh_spikes = sim.data[p_srf_inh_spikes] decoder_spikes = sim.data[p_decoder_spikes] decoder_inh_spikes = sim.data[p_decoder_inh_spikes] decoder_weights = sim.data[p_decoder_weights] srf_exc_rates = rates_kernel(sim.trange(), sim.data[p_srf_exc_spikes], tau=kernel_tau) srf_inh_rates = rates_kernel(sim.trange(), sim.data[p_srf_inh_spikes], tau=kernel_tau) srf_output_rates = rates_kernel(sim.trange(), srf_output_spikes, tau=kernel_tau) decoder_rates = rates_kernel(sim.trange(), decoder_spikes, tau=kernel_tau) decoder_inh_rates = rates_kernel(sim.trange(), decoder_inh_spikes, tau=kernel_tau) if save_results: np.save("srf_autoenc_exc_weights", np.asarray(srf_exc_weights, dtype=np.float32)) np.save("srf_autoenc_exc_rates", np.asarray(srf_exc_rates, dtype=np.float32)) np.save("srf_autoenc_inh_rates", np.asarray(srf_inh_rates, dtype=np.float32)) np.save("srf_autoenc_output_spikes", np.asarray(srf_output_spikes, dtype=np.float32)) np.save("srf_autoenc_decoder_spikes", np.asarray(decoder_spikes, dtype=np.float32)) np.save("srf_autoenc_output_rates", np.asarray(srf_output_rates, dtype=np.float32)) np.save("srf_autoenc_decoder_rates", np.asarray(decoder_rates, dtype=np.float32)) np.save("srf_autoenc_decoder_inh_rates", np.asarray(decoder_inh_rates, dtype=np.float32)) np.save("srf_autoenc_time_range", np.asarray(sim.trange(), dtype=np.float32)) return sim, { 'srf_autoenc_output_rates': srf_output_rates, 'srf_autoenc_decoder_rates': decoder_rates, 'srf_autoenc_decoder_inh_rates': decoder_inh_rates, 'srf_autoenc_exc_rates': srf_exc_rates, 'srf_autoenc_inh_rates': srf_inh_rates, 'srf_autoenc_exc_spikes': srf_exc_spikes, 'srf_autoenc_inh_spikes': srf_inh_spikes, 'srf_autoenc_output_spikes': srf_output_spikes, 'srf_autoenc_decoder_spikes': decoder_spikes, 'srf_autoenc_exc_weights': srf_exc_weights, 'srf_autoenc_inh_weights': srf_inh_weights, 'srf_autoenc_rec_weights': srf_rec_weights, 'srf_autoenc_decoder_weights': decoder_weights, }
dt = 0.001 t = np.linspace(0, tstop, int(tstop / dt)) place_spikes = np.load("place_reader_spikes.npy") xy = np.load("xy.npy") place_selection = np.load("place_selection.npy") xy_reader = np.load("xy_reader.npy") xy_reader_spikes = np.load("xy_reader_spikes.npy") plt.figure(figsize=(16, 6)) plt.plot(t, xy[:, 0], label="X") plt.plot(t, xy[:, 1], label="Y") plt.legend() plt.show() plt.figure(figsize=(16, 6)) rates = rates_kernel(t, place_spikes).T im = plt.imshow(rates, origin='upper', aspect='auto', interpolation='none', extent=[np.min(t), np.max(t), 0, rates.shape[0]]) plt.show() t_sample, place_spikes_sample = sample_by_variance(t, place_spikes, num=250, filter_width=0.5) t_cycle_idxs = np.where(np.logical_and(t_sample >= 4.5, t_sample <= 10))[0] plt.figure(figsize=(16, 6)) rates = rates_kernel(t_sample, place_spikes_sample, tau=0.25).T
print(f'exc_rates_predictions_test = {exc_rates_predictions_test} exc_rates_test_score = {exc_rates_test_score}') example_spikes_train = [x[n_steps_skip:n_steps_present] for x in np.split(srf_autoenc_output_spikes_train[1*n_steps_frame:,:], (n_steps_train - 1*n_steps_frame)//n_steps_frame)] example_spikes_test = [x[n_steps_skip:n_steps_present] for x in np.split(srf_autoenc_output_spikes_test, n_steps_test/n_steps_frame)] ngram_n = 2 ngram_decoder = fit_ngram_decoder(example_spikes_train, train_labels[1:], n_labels, ngram_n, {}) output_predictions_train = predict_ngram(example_spikes_train, ngram_decoder, n_labels, ngram_n) output_train_score = accuracy_score(train_labels[1:], output_predictions_train) print(f'output_predictions_train = {output_predictions_train} output_train_score = {output_train_score}') output_predictions_test = predict_ngram(example_spikes_test, ngram_decoder, n_labels, ngram_n) output_test_score = accuracy_score(test_labels, output_predictions_test) print(f'output_predictions_test = {output_predictions_test} output_test_score = {output_test_score}') example_rates_train = [rates_kernel(np.arange(0, presentation_time-skip_time, dt), x, tau=kernel_tau) for x in example_spikes_train] example_rates_test = [rates_kernel(np.arange(0, presentation_time-skip_time, dt), x, tau=kernel_tau) for x in example_spikes_test] ngram_n = 2 ngram_decoder_rates = fit_ngram_decoder_rates(example_rates_train, train_labels[1:], n_labels, ngram_n, {}) output_rates_predictions_train = predict_ngram_rates(example_rates_train, ngram_decoder_rates, n_labels, ngram_n) output_rates_train_score = accuracy_score(train_labels[1:], output_rates_predictions_train) print(f'output_rates_predictions_train = {output_rates_predictions_train} output_rates_train_score = {output_rates_train_score}') output_rates_predictions_test = predict_ngram_rates(example_rates_test, ngram_decoder_rates, n_labels, ngram_n) output_rates_test_score = accuracy_score(test_labels, output_rates_predictions_test) print(f'output_rates_predictions_test = {output_rates_predictions_test} output_rates_test_score = {output_rates_test_score}') print(f"output modulation depth (train): {np.mean(modulation_depth(srf_autoenc_output_rates_train))}")
plt.colorbar() plt.show() print(f"t_max = {np.max(trj_t)}") with nengo.Simulator(srf_network, optimize=True, dt=0.01) as sim: sim.run(np.max(trj_t)) rec_weights = sim.data[p_rec_weights] exc_weights = sim.data[p_exc_weights] inh_weights = sim.data[p_inh_weights] exc_rates = sim.data[p_exc_rates] inh_rates = sim.data[p_inh_rates] output_spikes = sim.data[p_output_spikes] np.save("srf_output_spikes", np.asarray(output_spikes, dtype=np.float32)) np.save("srf_time_range", np.asarray(sim.trange(), dtype=np.float32)) output_rates = rates_kernel(sim.trange(), output_spikes, tau=0.1) np.save("srf_output_rates", np.asarray(output_rates, dtype=np.float32)) np.save("srf_exc_rates", np.asarray(exc_rates, dtype=np.float32)) np.save("srf_inh_rates", np.asarray(inh_rates, dtype=np.float32)) np.save("exc_weights", np.asarray(exc_weights, dtype=np.float32)) np.save("rec_weights", np.asarray(rec_weights, dtype=np.float32)) np.save("inh_weights", np.asarray(inh_weights, dtype=np.float32)) sorted_idxs = np.argsort(-np.argmax(output_rates[3928:].T, axis=1)) plt.imshow(output_rates[:, sorted_idxs].T, aspect="auto", interpolation="nearest") plt.colorbar() plt.show() #plot_spikes(sim.trange(), sim.data[p_inh_rates][0,:])
from nengo_extras.neurons import (rates_kernel, rates_isi) import matplotlib import matplotlib.pyplot as plt font = {'family': 'normal', 'size': 20} matplotlib.rc('font', **font) tstop = 10. dt = 0.001 t = np.linspace(0, tstop, int(tstop / dt)) xy = np.load("xy.npy") xy_reader = np.load("xy_reader.npy") xy_reader_spikes = np.load("xy_reader_spikes.npy") plt.figure(figsize=(16, 6)) rates = rates_kernel(t, xy_reader_spikes).T im = plt.imshow(rates, origin='upper', aspect='auto', interpolation='none', extent=[np.min(t), np.max(t), 0, rates.shape[0]]) plt.show() t_sample, xy_reader_spikes_sample = sample_by_variance(t, xy_reader_spikes, num=250, filter_width=0.1) t_cycle_idxs = np.where(np.logical_and(t_sample >= 4.5, t_sample <= 10))[0] plt.figure(figsize=(16, 6)) rates = rates_kernel(t_sample, xy_reader_spikes_sample, tau=0.1).T
def eval_srf_net(input_matrix, params): N_inputs = input_matrix.shape[0] N_outputs_srf = params['N_outputs_srf'] N_exc_srf = N_inputs N_inh_srf = params['N_inh_srf'] N_exc_place = params['N_exc_place'] N_inh_place = params['N_inh_place'] dt = 0.001 t_end = input_matrix.shape[1] * dt srf_place_network = nengo.Network( label="Learning with spatial receptive fields", seed=params['seed']) rng = np.random.RandomState(seed=params['seed']) with srf_place_network as model: srf_network = PRF(exc_input_func=partial(array_input, input_matrix, params['dt']), connect_exc_inh_input=True, n_excitatory=N_exc_srf, n_inhibitory=params['N_inh_srf'], n_outputs=params['N_outputs_srf'], w_initial_E=params['w_initial_E'], w_initial_I=params['w_initial_I'], w_initial_EI=params['w_initial_EI'], w_EI_Ext=params['w_EI_Ext'], p_E=params['p_E_srf'], p_EE=params['p_EE'], p_EI_Ext=params['p_EI_Ext'], p_EI=params['p_EI'], tau_E=params['tau_E'], tau_I=params['tau_I'], tau_input=params['tau_input'], isp_target_rate=params['isp_target_rate'], learning_rate_I=params['learning_rate_I'], learning_rate_E=params['learning_rate_E'], label="Spatial receptive field network", seed=params['seed']) place_network = EI(n_excitatory=params['N_exc_place'], n_inhibitory=params['N_inh_place'], isp_target_rate=params['isp_target_rate'], learning_rate_I=params['learning_rate_I'], learning_rate_E=params['learning_rate_E'], p_E=params['p_E_place'], p_EE=params['p_EE'], p_EI=params['p_EI'], tau_E=params['tau_E'], tau_I=params['tau_I'], tau_input=params['tau_input'], connect_exc_inh=True, label="Place network", seed=params['seed']) w_PV_E = params['w_PV_E'] p_PV_E = params['p_PV_E'] weights_dist_PV_E = rng.normal(size=N_outputs_srf * N_exc_place).reshape( (N_exc_place, N_outputs_srf)) weights_initial_PV_E = (weights_dist_PV_E - weights_dist_PV_E.min() ) / (weights_dist_PV_E.max() - weights_dist_PV_E.min()) * w_PV_E for i in range(N_exc_place): dist = i - np.asarray(range(N_outputs_srf)) sigma = p_PV_E * N_outputs_srf weights = np.exp(-dist / sigma**2) prob = weights / weights.sum(axis=0) sources = np.asarray(rng.choice(N_outputs_srf, round(p_PV_E * N_outputs_srf), replace=False, p=prob), dtype=np.int32) weights_initial_PV_E[ i, np.logical_not(np.in1d(range(N_outputs_srf), sources))] = 0. conn_PV_E = nengo.Connection( srf_network.output.neurons, place_network.exc.neurons, transform=weights_initial_PV_E, synapse=nengo.Alpha(params['tau_E']), learning_rule_type=HSP(learning_rate=params['learning_rate_E'])) w_SV_E = params['w_PV_E'] p_SV_E = params['p_SV_E'] weights_dist_SV_E = rng.normal(size=N_exc_srf * N_exc_place).reshape( (N_exc_place, N_exc_srf)) weights_initial_SV_E = (weights_dist_SV_E - weights_dist_SV_E.min() ) / (weights_dist_SV_E.max() - weights_dist_SV_E.min()) * w_SV_E for i in range(N_exc_place): dist = i - np.asarray(range(N_exc_srf)) sigma = p_SV_E * N_exc_srf weights = np.exp(-dist / sigma**2) prob = weights / weights.sum(axis=0) sources = np.asarray(rng.choice(N_exc_srf, round(p_SV_E * N_exc_srf), replace=False, p=prob), dtype=np.int32) weights_initial_SV_E[ i, np.logical_not(np.in1d(range(N_exc_srf), sources))] = 0. conn_SV_E = nengo.Connection( srf_network.exc.neurons, place_network.exc.neurons, transform=weights_initial_SV_E, synapse=nengo.Alpha(params['tau_E']), learning_rule_type=HSP(learning_rate=params['learning_rate_E'])) w_PV_I = params['w_PV_I'] weights_initial_PV_I = rng.uniform( size=N_inh_place * N_outputs_srf).reshape( (N_inh_place, N_outputs_srf)) * w_PV_I conn_PV_I = nengo.Connection(srf_network.output.neurons, place_network.inh.neurons, transform=weights_initial_PV_I, synapse=nengo.Alpha(params['tau_I'])) with place_network: p_place_output_spikes = nengo.Probe(place_network.exc.neurons, synapse=None) with srf_network: p_output_spikes = nengo.Probe(srf_network.output.neurons, synapse=None) with nengo.Simulator(srf_place_network, optimize=True) as sim: sim.run(t_end) srf_output_spikes = sim.data[p_output_spikes] srf_output_rates = rates_kernel(sim.trange(), srf_output_spikes, tau=0.1) place_output_spikes = sim.data[p_place_output_spikes] place_output_rates = rates_kernel(sim.trange(), place_output_spikes, tau=0.1) return np.asarray([ obj_modulation_depth(srf_output_rates), obj_modulation_depth(place_output_rates), obj_fraction_active(srf_output_rates), obj_fraction_active(place_output_rates) ])
for m in inh_input_rates_dict: for i in inh_input_rates_dict[m]: input_rates = inh_input_rates_dict[m][i](trj_x, trj_y) input_rates[np.isclose(input_rates, 0., atol=1e-4, rtol=1e-4)] = 0. inh_trajectory_input_rates[m][i] = input_rates input_rates_ip = make_interp_spline(trj_t, input_rates, k=3) inh_trajectory_inputs.append(input_rates_ip) reward_idxs = np.argwhere( np.logical_and(np.isclose(trj_x, reward_pos[0], atol=1e-2, rtol=1e-2), np.isclose(trj_y, reward_pos[1], atol=1e-2, rtol=1e-2))) reward_loc_ranges = consecutive(reward_idxs.flat)[:-1] reward_input_rates = np.zeros((trj_t.shape[0], 1)) for reward_loc_idxs in reward_loc_ranges: reward_input_rates[reward_loc_idxs, 0] = 1. reward_input_rates = rates_kernel(trj_t, reward_input_rates, tau=0.5) reward_input_rates = (reward_input_rates / np.max(reward_input_rates)) * reward_peak_rate reward_input_rates_ip = [make_interp_spline(trj_t, reward_input_rates, k=3)] plt.plot(trj_t, reward_input_rates_ip[0](trj_t)) plt.show() def plot_input_rates(input_rates_dict): for m in input_rates_dict: plt.figure() arena_map = np.zeros(arena_xx.shape) for i in range(len(input_rates_dict[m])): input_rates = input_rates_dict[m][i](arena_xx, arena_yy) arena_map += input_rates plt.pcolor(arena_xx, arena_yy, arena_map, cmap=cm.jet)