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_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 get_simulation(dv=.001, 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) i1_i1 = Connection(i1, i1, 1, weights=[.005], probs=[1.], delay=0.0) simulation = Simulation([b1, i1], [b1_i1, i1_i1], verbose=verbose) 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 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_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_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_access_matrices(): i1 = InternalPopulation(v_min=0, v_max=.02, dv=.0001) c1 = Connection(None, i1, 1, weights=.005) leak_matrix = get_leak_matrix(i1) synaptic_matrix, threshold_vector = get_connection_flux_matrices(c1) assert leak_matrix.shape == synaptic_matrix.shape assert synaptic_matrix.shape[0] == threshold_vector.shape[0]
def get_simulation(dv=.001, 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, delays=0.0) i1_i1 = Connection(i1, i1, 1, weights=.005, delays=0.0) simulation = Network([b1, i1], [b1_i1, i1_i1]) return simulation
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 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_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_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_general_interface(): # Settings: t0 = 0. dt = .001 tf = .01 # Run simulation: b1 = ODE(lambda y, t:(100-t)/.1) i1 = PopulationInterface() b1_i1 = Connection(b1, i1, 1, weights=.005) network = Network([b1, i1], [b1_i1]) network.run(dt, tf=tf, t0=t0) np.testing.assert_almost_equal(b1.curr_firing_rate, 9.99949999997, 10)
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 test_general_interface(): # Settings: t0 = 0. dt = .001 tf = .01 # Run simulation: b1 = ODE(lambda y, t: (100 - t) / .1) i1 = PopulationInterface() b1_i1 = Connection(b1, i1, 1, weights=.005) network = Network([b1, i1], [b1_i1]) network.run(dt, tf=tf, t0=t0) # Tests: final_firing_rate = 9.99949999997 np.testing.assert_almost_equal(b1.curr_firing_rate, final_firing_rate, 10) np.testing.assert_almost_equal(i1.source_firing_rate_dict[0], final_firing_rate, 10) assert len(i1.source_connection_list) == 1 assert i1.source_connection_list[0] == b1_i1
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
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, approx_order=approx_order, tol=tol, record=True, curr_firing_rate=0.0, norm='inf') b1_i1 = Connection(b1, i1, C_E, weights=[J_ex * 1e-3], probs=[1.], delay=delay * 1e-3) i1_i1 = Connection(i1, i1, C_E, weights=[J_ex * 1e-3], probs=[1.], delay=delay * 1e-3) b1_i2 = Connection(b1, i2, C_E, weights=[J_ex * 1e-3], probs=[1.], delay=delay * 1e-3)
from dipde.internals.internalpopulation import InternalPopulation from dipde.internals.connection import Connection from dipde.interfaces.access_matrices import get_leak_matrix, get_connection_flux_matrices import numpy as np import numpy.linalg as npla import logging logging.disable(logging.CRITICAL) # Components: i1 = InternalPopulation(tau_m=.02, v_min=0, v_max=.02, dv=.001) c1 = Connection(None, i1, 1, weights=.005) # Get matrices: leak_matrix = get_leak_matrix(i1) synaptic_matrix, threshold_vector = get_connection_flux_matrices(c1) A = leak_matrix + 100 * synaptic_matrix A[0, :] = 1 b = np.zeros(A.shape[0]) b[0] = 1 # Solve for steady state: p_star = npla.solve(A, b) # Steady state firing rate print np.dot(p_star, 100 * threshold_vector) # A2 = leak_matrix + 100./(1-np.dot(p_star, threshold_vector))*synaptic_matrix # w, vl = npla.eig(A2) # # # print w[np.argmin(np.abs(w))]
def test_connection_copy_2(): o1 = Connection(nsyn=2, weights=0.005, delays=0.005) o2 = o1.copy() compare(o1, o2)
# 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 D = (np.outer(c, 1. / c)) / (dX + (np.eye(N + 1)) ) # % off-diagonal entries return D - np.diag(np.sum( D, axis=1)) #, x#; % diagonal entries
def build(self): edge = self._edges[0] self._dipde_conn = Connection(self._src_pop._dipde_obj, self._trg_pop._dipde_obj, edge.nsyns, edge.delay, edge.syn_weight)
# rv = sps.norm(t0, sigma) # return lambda t: rv.pdf(t)*sigma*np.sqrt(2*np.pi)*amplitude # dv = .001 nsyn_bg = 1 bgfr = 200 weight_bg = .1 weight_recc = -.1 nsyn_recc = 20 # Hopf when delay between: (.08 , .09) # # # 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=weight_bg) i1_i1 = Connection(i1, i1, nsyn_recc, weights=weight_recc) # def chirp_fcn(n): # print n.t, n.population_list[1].curr_firing_rate # network = Network([b1, i1], [b1_i1, i1_i1], update_callback=chirp_fcn) # network.run(tf=20, dt=.0001) # i1.plot() # plt.show() # # # # # Get matrices: leak_matrix = get_leak_matrix(i1, sparse=False) synaptic_matrix_bg, threshold_vector_bg = get_connection_flux_matrices(b1_i1, sparse=False) synaptic_matrix_recc, _ = get_connection_flux_matrices(i1_i1, sparse=False)
def __create_connection(self, source, target, params): return Connection(source, target, nsyn=params.nsyns, delays=params.delay, weights=params.weight)
def test_connection_copy_1(): o1 = Connection(0, 1, 2, weights=0.005, delays=0.005) o2 = o1.copy() compare(o1, o2)
'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], weights=[conn_weights['e']], probs=[1.], delay=background_delay) connection_list.append(curr_connection) # Create recurrent connections: for source_layer, source_celltype in itertools.product([23, 4, 5, 6], ['e', 'i']): for target_layer, target_celltype in itertools.product([23, 4, 5, 6], ['e', 'i']): 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),
def test_marshal_connection(): c = Connection(0, 1, 1, weights=.005, delays=.005) c.to_json()