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 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_network_df(): p1 = ExternalPopulation(100, record=True) p2 = ExternalPopulation(200, record=False) p3 = InternalPopulation(v_min=0, v_max=.02, metadata={'X': 0}) p4 = InternalPopulation(v_min=0, v_max=.01, metadata={'X': 0}) n1 = Network(population_list=[p1, p2, p3, p4]) print n1.to_df()
def test_restart_interal(): # Run part-way and serialize: b1 = ExternalPopulation('100', record=True) i1 = InternalPopulation(v_min=0, v_max=.02, dv=.001) b1_i1 = Connection(b1, i1, 1, weights=.005, delays=0.0) simulation = Network([b1, i1], [b1_i1]) simulation.run(dt=.001, tf=.01, t0=0) i1_str = i1.to_json() b1_str = b1.to_json() # Rehydrate and continue run: b2 = ExternalPopulation(**json.loads(b1_str)) i2 = InternalPopulation(**json.loads(i1_str)) simulation2 = Network([b2, i2], [Connection(b2, i2, 1, weights=.005, delays=0.0)]) simulation2.run(dt=.001, tf=.02, t0=.01) # Run straight through, for comparison: b3 = ExternalPopulation('100', record=True) i3 = InternalPopulation(v_min=0, v_max=.02, dv=.001) simulation3 = Network([b3, i3], [Connection(b3, i3, 1, weights=.005, delays=0.0)]) simulation3.run(dt=.001, tf=.02, t0=0) # Test: for y1, y2 in zip(i1.firing_rate_record, i3.firing_rate_record): np.testing.assert_almost_equal(y1, y2, 8) b3.to_json(StringIO.StringIO()) i3.to_json(StringIO.StringIO()) b1_i1.to_json(StringIO.StringIO())
def get_simulation(dv=.001, verbose=False, update_method='approx', approx_order=None, tol=1e-8): import scipy.stats as sps # Create simulation: b1 = ExternalPopulation(100) i1 = InternalPopulation(v_min=0, v_max=.02, dv=dv, update_method=update_method, approx_order=approx_order, tol=tol) b1_i1 = Connection(b1, i1, 1, delay=0.0, distribution=sps.expon, N=201, scale=.005) simulation = Simulation([b1, i1], [b1_i1], verbose=verbose) return simulation
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 test_simulation_copy(): b1 = ExternalPopulation(100) i1 = InternalPopulation(v_min=0, v_max=.02) b1_i1 = Connection(b1, i1, 2, weights=.005) o1 = Network([b1, i1], [b1_i1]) o2 = o1.copy() compare(o1, o2)
def get_singlepop_benchmark_network(scale=2): from dipde.internals.internalpopulation import InternalPopulation from dipde.internals.externalpopulation import ExternalPopulation from dipde.internals.network import Network from dipde.internals.connection import Connection as Connection # Settings: dv = .0001 update_method = 'approx' approx_order = None tol = 1e-14 # Run simulation: b_list = [] i_list = [] conn_list = [] for _ in range(scale): b = ExternalPopulation(100, record=True) i = InternalPopulation(v_min=0, v_max=.02, dv=dv, update_method=update_method, approx_order=approx_order, tol=tol) c = Connection(b, i, 1, weights=.005) b_list += [b] i_list += [i] conn_list += [c] return Network(b_list + i_list, conn_list)
def get_matrices(): dv = .001 nsyn_bg = 1 bgfr = 200 we = .1 wi = -.1 nsyn_00, nsyn_01, nsyn_10, nsyn_11 = 5, 5, 2, 20 # Components: b0 = ExternalPopulation(bgfr, record=True) i0 = InternalPopulation(tau_m=.05, v_min=0, v_max=1, dv=dv, update_method='gmres') i1 = InternalPopulation(tau_m=.05, v_min=0, v_max=1, dv=dv, update_method='gmres') b0_i0 = Connection(b0, i0, nsyn_bg, weights=we, delays=0.0) b0_i1 = Connection(b0, i1, nsyn_bg, weights=we, delays=0.0) i0_i0 = Connection(i0, i0, nsyn_00, weights=we, delays=0.0) i0_i1 = Connection(i0, i1, nsyn_01, weights=we, delays=0.0) i1_i0 = Connection(i1, i0, nsyn_10, weights=wi, delays=0.0) i1_i1 = Connection(i1, i1, nsyn_11, weights=wi, delays=0.0) L = get_leak_matrix(i1) Se, te = get_connection_flux_matrices(b0_i0) Si, _ = get_connection_flux_matrices(i1_i0) return L, Se, Si, te
def get_simulation(dt=.001, dv=.001, tf=.2, verbose=False, update_method='exact', approx_order=None, tol=1e-8): # Create simulation: b1 = ExternalPopulation('100') i1 = InternalPopulation(v_min=0, v_max=.02, dv=dv, update_method=update_method, approx_order=approx_order, tol=tol) b1_i1 = Connection(b1, i1, 1, weights=[.005], probs=[1.], delay=0.0) simulation = Simulation([b1, i1], [b1_i1], dt=dt, tf=tf, verbose=verbose) return simulation
def get_simulation(dv=.001, update_method='exact', approx_order=None, tol=1e-8): # Create simulation: b1 = ExternalPopulation('100+50*abs(sin(40*t))') i1 = InternalPopulation(v_min=0, v_max=.02, dv=dv, update_method=update_method, approx_order=approx_order, tol=tol) b1_i1 = Connection(b1, i1, 1, weights=.005, delays=0.0) simulation = Network([b1, i1], [b1_i1]) return simulation
def get_network(dv=.001, verbose=False, update_method='approx', approx_order=1, tol=1e-14): # Create network: b1 = ExternalPopulation('100') i1 = InternalPopulation(v_min=-.02, v_max=.02, dv=dv, update_method=update_method, approx_order=approx_order, tol=tol) b1_i1 = Connection(b1, i1, 1, weights=.005, delays=([.005, .01],[.5,.5])) b1_i1_2 = Connection(b1, i1, 1, weights=-.005, delays=sps.uniform(0,.01)) network = Network([b1, i1], [b1_i1, b1_i1_2]) return network
def get_multipop_model(): # Settings: dv = .001 update_method = 'approx' approx_order = None tol = 1e-14 # Create simulation: b1 = ExternalPopulation(100) b2 = ExternalPopulation(50) i1 = InternalPopulation(v_min=0, v_max=.02, dv=dv, update_method=update_method, approx_order=approx_order, tol=tol) i2 = InternalPopulation(v_min=0, v_max=.02, dv=dv, update_method=update_method, approx_order=approx_order, tol=tol) b1_i1 = Connection(b1, i1, 1, weights=.005) i1_i2 = Connection(i1, i2, 20, weights=.005, delays=.001) b2_i2 = Connection(b2, i2, 2, weights=.005, delays=.002) simulation = Network([b1, b2, i1, i2], [b1_i1, i1_i2, b2_i2]) simulation_dict = simulation.to_dict() return simulation_dict
def get_network(dv=.001, update_method='exact', tol=1e-14): # Create simulation: b1 = ExternalPopulation('100', record=True) i1 = InternalPopulation(v_min=0, v_max=.02, dv=dv, update_method=update_method, tol=tol) b1_i1 = Connection(b1, i1, 1, weights=.005, delays=0.0) network = Network([b1, i1], [b1_i1]) return network
def get_simulation(dv=.001, update_method='approx', tol=1e-8): import scipy.stats as sps # Create simulation: b1 = ExternalPopulation(50) b2 = ExternalPopulation(1000) i1 = InternalPopulation(v_min=-.04, v_max=.02, dv=dv, update_method=update_method, tol=tol) b1_i1 = Connection(b1, i1, 1, delays=0.0, weights=(sps.expon(0, .00196), 301)) b2_i1 = Connection(b2, i1, 1, delays=0.0, weights=(sps.expon(0, .001), 301)) simulation = Network([b1, b2, i1], [b1_i1, b2_i1]) return simulation
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_get_J(): # Settings: bgfr=100 update_method='approx' weights={'distribution':'delta', 'loc':.005} p0=((0.,),(1.,)) tau_m=.02 dv = .001 v_min = -.01 v_max = .02 # 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) b1_i1 = Connection(b1, i1, 1, weights=weights) network = Network([b1, i1], [b1_i1]) network.get_total_flux_matrix(i1,.001)
def get_simulation(dv=.001, verbose=False, update_method='exact', approx_order=None, tol=1e-8): # Create simulation: f = RequestFiringRateFunction(5555) b1 = ExternalPopulation(f, record=True, name='b1') i1 = InternalPopulation(v_min=0, v_max=.02, dv=dv, update_method=update_method, approx_order=approx_order, tol=tol) b1_i1 = Connection(b1, i1, 1, weights=.005, delay=0.0) simulation = Network([b1, i1], [b1_i1], verbose=verbose) return simulation
def test_delay_distribution(): # Settings: t0 = 0. dt = .001 tf = .1 # Create populations: b1 = ExternalPopulation('100*Heaviside(t)') i1 = InternalPopulation(v_min=0, v_max=.02, dv=.001, update_method='exact') # Create connections: b1_i1 = Connection(b1, i1, 1, weights=.005, delays=((0,.05),(.5,.5))) # Create and run simulation: simulation = Network([b1, i1], [b1_i1]) simulation.run(dt=dt, tf=tf, t0=t0) true_ans = np.array([0.38560647739319964, 5.229266329159536]) np.testing.assert_almost_equal(np.array(i1.get_firing_rate([.04, .09])), true_ans, 8)
def test_delay_singlepop(): # Settings: t0 = 0. dt = .001 tf = .005 # 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, delays=2*dt, delay_queue=[0,0,50]) # # DEBUG: # b1_i1 = Connection(b1, i1, 1, weights=.005, delays=((0,.001, .002),(0,0,1.)), delay_queue=[0,0,50]) simulation = Network([b1, i1], [b1_i1]) 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 get_simulation(dv=.001, update_method='approx', approx_order=None, tol=1e-8): import scipy.stats as sps # Create simulation: b1 = ExternalPopulation(100) i1 = InternalPopulation(v_min=0, v_max=.02, dv=dv, update_method=update_method, approx_order=approx_order, tol=tol) b1_i1 = Connection(b1, i1, 1, delays=0.0, weights=(sps.expon(0, .005), 201)) simulation = Network([b1, i1], [b1_i1]) return simulation
def test_enternalpopulation_copy(): o1 = ExternalPopulation(100) o2 = o1.copy() compare(o1, o2)
print 'NEST Mean Exc rate: ', rate_ex, '[Hz]' print 'NEST Mean Inh rate: ', rate_in, '[Hz]' ###DipDe # Settings: t0 = 0. dt = 0.0001 dv = 0.1 tf = 1. #.1 #simulation duration. update_method = 'approx' approx_order = 1 tol = 1e-14 verbose = False # Create simulation: b1 = ExternalPopulation(p_rate, record=True) i1 = InternalPopulation(tau_m=t_m * 1e-3, v_min=-0.02, v_max=u_th * 1e-3, dv=dv, update_method=update_method, approx_order=approx_order, tol=tol, record=True, curr_firing_rate=0.0, norm='inf') i2 = InternalPopulation(tau_m=t_m * 1e-3, v_min=-0.02, v_max=u_th * 1e-3, dv=dv, update_method=update_method,
sstm = 90.0 sbase = 120.0 # Create visual stimuli External_stimuli_dict = {} for index, celltype in itertools.product([0,1],['bkg']): stm_tmp = np.zeros(np.int(tf/dt)) stm_tmp[np.int(background_start[index,celltype]/dt):np.int(background_end[index,celltype]/dt)] = sstm stm_tmp += sbase External_stimuli_dict[index,celltype] = stm_tmp # Create populations: background_population_dict = {} internal_population_dict = {} for index, celltype in itertools.product([0,1], ['bkg']): background_population_dict[index, celltype] = ExternalPopulation(External_stimuli[index,celltype],dt, record=False) for index, celltype in itertools.product([0,1,2,3], ['e','i']): internal_population_dict[index, celltype] = RecurrentPopulation(dt = dt,v_min=-1.0, v_max=1.0, dv=dv, update_method=update_method, approx_order=approx_order, tol=tol) # Create background connections: connection_list = [] for index, celltype in itertools.product([0], ['e', 'i']): source_population = background_population_dict[0,'bkg'] target_population = internal_population_dict[layer, celltype] if celltype == 'e': background_delay = .005 else: background_delay = 0. curr_connection = Connection(source_population, target_population, nsyn_background[layer, celltype], weights=[conn_weights['e']], probs=[1.], delay=background_delay) connection_list.append(curr_connection)
def test_enternalpopulation_df(): e1 = ExternalPopulation(100) e2 = ExternalPopulation(**dict_from_df(e1.to_df())) compare_dicts(e1.to_dict(), e2.to_dict())
b = np.zeros(A.shape[0]) b[0] = 1 f1_new = np.dot(npla.solve(A, b), (bgfr * nsyn_bg + nsyn_01 * f0) * te) return np.array([f0_new, f1_new]) # Compute steady state: f0_ss, f1_ss = sopt.fixed_point(steady_state_function, np.array([14, 9]), args=(bgfr, nsyn_bg, nsyn_00, nsyn_01, nsyn_10, nsyn_11, delay)) # Compute initial guesses for eigenvector and eigenvalue: # # Components: b1 = ExternalPopulation(bgfr, record=True) i1 = InternalPopulation(tau_m=.05, v_min=0, v_max=1, dv=dv, update_method='gmres') b1_i1 = Connection(b1, i1, nsyn_bg, weights=we, delays=0.0) i1_i1 = Connection(i1, i1, 0, weights=wi, delays=0.0) def cheb(N): x = np.cos(np.pi * np.arange(N + 1) / N) c = np.array([2] + [1] * (N - 1) + [2]) * np.array([(-1)**ii for ii in range(N + 1)]) X = npm.repmat(x, 1, N + 1).reshape(N + 1, N + 1).T dX = X - X.T
def build(self, firing_rate): if firing_rate is not None: self._firing_rate = firing_rate self._dipde_obj = ExternalPopulation(firing_rate)
# import matplotlib # matplotlib.use('Qt4Agg') # import matplotlib.pyplot as plt from dipde.internals.internalpopulation import InternalPopulation from dipde.internals.externalpopulation import ExternalPopulation from dipde.internals.network import Network from dipde.internals.connection import Connection as Connection from dipde.visualization import visualize dv = .0001 update_method = 'approx' approx_order = 1 tol = 1e-14 b1 = ExternalPopulation('100', record=True) i1 = InternalPopulation(v_min=0, v_max=.02, dv=dv, update_method=update_method, approx_order=approx_order, tol=tol) i2 = InternalPopulation(v_min=0, v_max=.02, dv=dv, update_method=update_method, approx_order=approx_order, tol=tol) b1_i1 = Connection(b1, i1, 1, weights=.005, delays=0.0) b1_i2 = Connection(b1, i2, 2, weights=.005, delays=0.0) network = Network([b1, i1, i2], [b1_i1, b1_i2])
def __create_external_pop(self, params, rates): pop = ExternalPopulation(rates, record=False) return pop
'tau_m': .01, 'record': True } # Simulation settings: t0 = 0. dt = .0002 tf = .1 verbose = True save = False # Create populations: background_population_dict = {} internal_population_dict = {} for layer, celltype in itertools.product([23, 4, 5, 6], ['e', 'i']): background_population_dict[layer, celltype] = ExternalPopulation( 'Heaviside(t)*%s' % background_firing_rate, record=False) internal_population_dict[layer, celltype] = InternalPopulation( **internal_population_settings) # Create background connections: connection_list = [] for layer, celltype in itertools.product([23, 4, 5, 6], ['e', 'i']): source_population = background_population_dict[layer, celltype] target_population = internal_population_dict[layer, celltype] if celltype == 'e': background_delay = .005 else: background_delay = 0. curr_connection = Connection(source_population, target_population, nsyn_background[layer, celltype],
"""This singlepop simulation provides a simple feedforward topology that uses every major class in the core library. A single 100 Hz External population population provides excitatory input. (Note that although here this frequencys specified as a string, a floating point or integer specification will also work). This external population is connected to an Internal population (modeled as a population density pde) via a delta-distributed synaptic weight distribution, with 5 mV strength. The in-degree (nsyn) of this Connection is set to 1 for this example. In general, this serves as a multiplier of the input firing rate of the source population. The internal population has a linearly binned voltage domain from v_min to v_max. No negative bins (i.e. v_min < 0) are required here, because no negative synaptic inputs ('weights' in the Connection object) are defined. """ # Create simulation externalPopFreq = '50' b1 = ExternalPopulation(externalPopFreq, record=True) i1 = InternalPopulation(v_min=0, v_max=.02, dv=dv, update_method=update_method, approx_order=approx_order, tol=tol) b1_i1 = Connection(b1, i1, 1, weights=[.005], probs=[1.], delay=0.0) simulation = Simulation([b1, i1], [b1_i1], verbose=verbose) # Run simulation simulation.run(dt=dt, tf=tf, t0=t0) # Visualize results i1 = simulation.population_list[1] plt.figure(figsize=(3, 3))