def test_delay_doublepop(): # Settings: t0 = 0. dt = .001 tf = .010 verbose = False # Create populations: b1 = ExternalPopulation(50) i1 = InternalPopulation(v_min=0, v_max=.02, dv=.001, update_method='exact') i2 = InternalPopulation(v_min=0, v_max=.02, dv=.001, update_method='exact') # Create connections: b1_i1 = Connection(b1, i1, 2, weights=[.005], probs=[1.]) i1_i2 = Connection(i1, i2, 20, weights=[.005], probs=[1.], delay=2 * dt) # Create and run simulation: simulation = Simulation([b1, i1, i2], [b1_i1, i1_i2], verbose=verbose) simulation.run(dt=dt, tf=tf, t0=t0) true_ans = np.array([ 0, 0.0, 0.0, 0.0, 1.9089656152757652e-13, 1.9787511418980406e-10, 9.5007650186649266e-09, 1.3334881090883857e-07, 1.0103767575651715e-06, 5.3604521936092067e-06, 2.2383604753409621e-05 ]) np.testing.assert_almost_equal(i2.firing_rate_record, true_ans, 12)
def singlepop(steady_state, tau_m=.02, p0=((0.,),(1.,)), weights={'distribution':'delta', 'loc':.005}, bgfr=100, network_update_callback=lambda s: None, update_method='approx', simulation_configuration=None, tol=None, checkpoint_callback=None, nsyn=1): # Settings: t0 = 0. dt = .001 dv = .001 v_min = -.01 v_max = .02 tf = .1 # Create simulation: b1 = ExternalPopulation(bgfr) i1 = InternalPopulation(v_min=v_min, tau_m=tau_m, v_max=v_max, dv=dv, update_method=update_method, p0=p0, tol=tol) b1_i1 = Connection(b1, i1, nsyn, weights=weights) network = Network([b1, i1], [b1_i1], update_callback=network_update_callback) if simulation_configuration is None: simulation_configuration = SimulationConfiguration(dt, tf, t0=t0) simulation = Simulation(network=network, simulation_configuration=simulation_configuration, checkpoint_callback=checkpoint_callback) simulation.run() b1.plot() i1.plot_probability_distribution() i1.plot() assert i1.n_edges == i1.n_bins+1 # Test steady-state: np.testing.assert_almost_equal(i1.get_firing_rate(.05), steady_state, 12)
def test_singlepop(): # Settings: t0 = 0. dt = .001 dv = .001 v_min = -.01 v_max = .02 tf = .2 verbose = False # Create simulation: b1 = ExternalPopulation(50) b2 = ExternalPopulation(50) i1 = InternalPopulation(v_min=v_min, v_max=v_max, dv=dv, update_method='exact') b1_i1 = Connection(b1, i1, 1, weights=[.005], probs=[1.]) b2_i1 = Connection(b2, i1, 1, weights=[.005], probs=[1.]) simulation = Simulation([b1, b2, i1], [b1_i1, b2_i1], verbose=verbose) simulation.run(dt=dt, tf=tf, t0=t0) np.testing.assert_almost_equal(i1.t_record[-1], .2, 15) np.testing.assert_almost_equal(i1.firing_rate_record[-1], 5.3550005434746355, 12) assert i1.n_bins == (v_max - v_min)/dv assert i1.n_edges - 1 == i1.n_bins assert len(simulation.population_list) == 3 i1.plot_probability_distribution()
def singlepop(steady_state, tau_m=.02, p0=((0.,),(1.,)), weights={'distribution':'delta', 'loc':.005}, bgfr=100, network_update_callback=lambda s: None, update_method='approx', simulation_configuration=None, tol=None): # Settings: t0 = 0. dt = .001 dv = .001 v_min = -.01 v_max = .02 tf = .1 # Create simulation: b1 = ExternalPopulation(bgfr) i1 = InternalPopulation(v_min=v_min, tau_m=tau_m, v_max=v_max, dv=dv, update_method=update_method, p0=p0, tol=tol) b1_i1 = Connection(b1, i1, 1, weights=weights) network = Network([b1, i1], [b1_i1], update_callback=network_update_callback) if simulation_configuration is None: simulation_configuration = SimulationConfiguration(dt, tf, t0=t0) simulation = Simulation(network=network, simulation_configuration=simulation_configuration) simulation.run() b1.plot() i1.plot_probability_distribution() i1.plot() assert i1.n_edges == i1.n_bins+1 # Test steady-state: np.testing.assert_almost_equal(i1.get_firing_rate(.05), steady_state, 12)
def test_singlepop(): # Settings: t0 = 0. dt = .001 dv = .001 v_min = -.01 v_max = .02 tf = .2 verbose = False # Create simulation: b1 = ExternalPopulation(50) b2 = ExternalPopulation(50) i1 = InternalPopulation(v_min=v_min, v_max=v_max, dv=dv, update_method='exact') b1_i1 = Connection(b1, i1, 1, weights=[.005], probs=[1.]) b2_i1 = Connection(b2, i1, 1, weights=[.005], probs=[1.]) simulation = Simulation([b1, b2, i1], [b1_i1, b2_i1], verbose=verbose) simulation.run(dt=dt, tf=tf, t0=t0) np.testing.assert_almost_equal(i1.t_record[-1], .2, 15) np.testing.assert_almost_equal(i1.firing_rate_record[-1], 5.3550005434746355, 12) assert i1.n_bins == (v_max - v_min) / dv assert i1.n_edges - 1 == i1.n_bins assert len(simulation.population_list) == 3 i1.plot_probability_distribution()
def test_delay_singlepop(): # Settings: t0 = 0. dt = .001 tf = .005 verbose = False # Create simulation: b1 = ExternalPopulation('Heaviside(t)*100') i1 = InternalPopulation(v_min=0, v_max=.02, dv=.001, update_method='exact') b1_i1 = Connection(b1, i1, 1, weights=[.005], probs=[1.], delay=2*dt) simulation = Simulation([b1, i1], [b1_i1], verbose=verbose) simulation.run(dt=dt, tf=tf, t0=t0) true_ans = np.array([0, 0.0, 0.0, 0.00066516669656511084, 0.025842290308637855, 0.08117342489138904]) np.testing.assert_almost_equal(i1.firing_rate_record, true_ans, 12)
def test_marshal_simulation(): from dipde.examples.excitatory_inhibitory import get_network simulation_configuration_full = SimulationConfiguration(dt=.001, tf=.02, t0=0) simulation_configuration_p1 = SimulationConfiguration(dt=.001, tf=.01, t0=0) simulation_configuration_p2 = SimulationConfiguration(dt=.001, tf=.01, t0=0) # Run full simulation: simulation_full = Simulation(network=get_network(), simulation_configuration=simulation_configuration_full) assert simulation_full.completed == False simulation_full.run() assert simulation_full.completed == True # Run simulation, part 1: simulation_p1 = Simulation(network=get_network(), simulation_configuration=simulation_configuration_p1) simulation_p1.run() s_midway = simulation_p1.to_json() # Run simulation, part 2: simulation_p2 = Simulation(**json.loads(s_midway)) simulation_p2.simulation_configuration = simulation_configuration_p2 simulation_p2.run() # Run copy half way, round trip, and then finish: # Compare: y1 = simulation_full.network.population_list[1].firing_rate_record y2 = simulation_p2.network.population_list[1].firing_rate_record assert len(y1) == len(y2) for y1i, y2i in zip(y1, y2): np.testing.assert_almost_equal(y1i, y2i, 12) simulation_full.to_json(StringIO.StringIO())
def test_delay_doublepop(): # Settings: t0 = 0. dt = .001 tf = .010 verbose = False # Create populations: b1 = ExternalPopulation(50) i1 = InternalPopulation(v_min=0, v_max=.02, dv=.001, update_method='exact') i2 = InternalPopulation(v_min=0, v_max=.02, dv=.001, update_method='exact') # Create connections: b1_i1 = Connection(b1, i1, 2, weights=[.005], probs=[1.]) i1_i2 = Connection(i1, i2, 20, weights=[.005], probs=[1.], delay=2*dt) # Create and run simulation: simulation = Simulation([b1, i1, i2], [b1_i1, i1_i2], verbose=verbose) simulation.run(dt=dt, tf=tf, t0=t0) true_ans = np.array([0, 0.0, 0.0, 0.0, 1.9089656152757652e-13, 1.9787511418980406e-10, 9.5007650186649266e-09, 1.3334881090883857e-07, 1.0103767575651715e-06, 5.3604521936092067e-06, 2.2383604753409621e-05]) np.testing.assert_almost_equal(i2.firing_rate_record, true_ans, 12)
weight = conn_weights[source_celltype] curr_connection = Connection(source_population, target_population, nsyn, weights=[weight], probs=[1.], delay=0) connection_list.append(curr_connection) # Create simulation: population_list = background_population_dict.values( ) + internal_population_dict.values() simulation = Simulation(population_list, connection_list, verbose=True) # Run simulation: simulation.run(dt=dt, tf=tf, t0=t0) # Visualize: y_label_dict = {23: '2/3', 4: '4', 5: '5', 6: '6'} fig, axes = plt.subplots(nrows=4, ncols=1, **{'figsize': (4, 8)}) for row_ind, layer in enumerate([23, 4, 5, 6]): for plot_color, celltype in zip(['r', 'b'], ['e', 'i']): curr_population = internal_population_dict[layer, celltype] axes[row_ind].plot(curr_population.t_record, curr_population.firing_rate_record, plot_color) axes[row_ind].set_xlim([0, tf]) axes[row_ind].set_ylim(ymin=0) axes[row_ind].set_ylabel('Layer %s\nfiring rate (Hz)' % y_label_dict[layer]) if layer == 5:
source_population = internal_population_dict[source_layer, source_celltype] target_population = internal_population_dict[target_layer, target_celltype] nsyn = ( connection_probabilities[(source_layer, source_celltype), (target_layer, target_celltype)] * internal_population_sizes[source_layer, source_celltype] ) weight = conn_weights[source_celltype] curr_connection = Connection(source_population, target_population, nsyn, weights=[weight], probs=[1.0], delay=0) connection_list.append(curr_connection) # Create simulation: population_list = background_population_dict.values() + internal_population_dict.values() simulation = Simulation(population_list, connection_list, verbose=True) # Run simulation: simulation.run(dt=dt, tf=tf, t0=t0) # Visualize: y_label_dict = {23: "2/3", 4: "4", 5: "5", 6: "6"} fig, axes = plt.subplots(nrows=4, ncols=1, **{"figsize": (4, 8)}) for row_ind, layer in enumerate([23, 4, 5, 6]): for plot_color, celltype in zip(["r", "b"], ["e", "i"]): curr_population = internal_population_dict[layer, celltype] axes[row_ind].plot(curr_population.t_record, curr_population.firing_rate_record, plot_color) axes[row_ind].set_xlim([0, tf]) axes[row_ind].set_ylim(ymin=0) axes[row_ind].set_ylabel("Layer %s\nfiring rate (Hz)" % y_label_dict[layer]) if layer == 5: axes[row_ind].legend(["Excitatory", "Inhibitory"], prop={"size": 10}, loc=4)