def poisson_drift_rate(t=1, t_onset=0.2, tau=.5, sigma=0.01, rate=6.0, n=10, dt=1e-3, min_rate=0, name=None, seed=None): """Simulate a Poisson population with an OU rate drift""" prng = np.random.RandomState(seed) times = create_times((0, t), dt) # OU as a difference equation x = min_rate rates = [x] for t in times[1:]: if t < t_onset: rates.append(x) continue # Last x xi = prng.normal(0, 1) # Drift, scaling the step by sigma * sqrt(tau) delta_x = (sigma * np.sqrt(tau) * xi) / tau x -= delta_x # Save rates.append(x) # Drop initial value rates = np.asarray(rates) # No negative rates rates[rates < min_rate] = min_rate # Sample xs with N poisson 'cells' nrns = neurons.Spikes(n, t, dt=dt, seed=seed) ns, ts = fsutil.to_spiketimes(times, nrns.poisson(rates)) # - if name is not None: write_spikes(name, ns, ts) return None else: return ns, ts, times, rates
def poisson_impulse(t=1, t_onset=0.2, w=1, rate=6, n=10, dt=1e-3, name=None, seed=None): """Simulate a pulse of spikes w seconds wide, starting at t_onset.""" # Poisson sample the rate over w times = create_times((0, t), dt) nrns = neurons.Spikes(n, t, dt=dt, seed=seed) pulse = rates.square_pulse(times, rate, t_onset, w, dt, min_a=0) ns, ts = fsutil.to_spiketimes(times, nrns.poisson(pulse)) if name is not None: write_spikes(name, ns, ts) return None else: return ns, ts
def poisson_oscillation(t=1, t_onset=0.2, n_cycles=10, rate=6, f=8, phi=0, n=10, dt=1e-3, min_rate=0.0, name=None, seed=None): """Simulate a Poisson population, oscillating""" # Define time times = fsutil.create_times(t, dt=dt) # Create rate wave, over times osc = (rate / 2 * (1 + np.sin((times * f * 2 * np.pi) + phi))) + min_rate # Truncate it to a n_cycle burst, starting at t_onset if np.isclose(f, 0.0): osc[:] = min_rate else: burst_l = (1 / float(f)) * n_cycles m = np.logical_not( np.logical_and(times >= t_onset, times <= (t_onset + burst_l))) osc[m] = min_rate # Sample osc with N poisson 'cells' nrns = neurons.Spikes(n, t, dt=dt, seed=seed) ns, ts = fsutil.to_spiketimes(times, nrns.poisson(osc)) if name is not None: write_spikes(name, ns, ts) return None else: return ns, ts
def oscillatory_coupling(num_pop=50, num_background=5, t=5, osc_rate=6, f=6, g=1, g_max=1, q=0.5, stim_rate=12, frac_std=.01, m=8, priv_std=0, dt=0.001, squelch=False, save=None, stim_seed=None, seed=None): """Run a single OC simulation.""" # -- Safety ------------------------------------------------------------- if g > g_max: raise ValueError("g must be < g_max") min_rate = 2 if stim_rate <= min_rate: raise ValueError(f"stim_rate must be greater {min_rate}") if f < 3: raise ValueError("f (freq) must be greater then 2") # q can't be exactly 0 q += EPS # -- Init --------------------------------------------------------------- # Poisson neurons backspikes = neurons.Spikes(num_background, t, dt=dt, seed=seed) ocspikes = neurons.Spikes(num_pop, t, dt=dt, private_stdev=priv_std, seed=seed) times = ocspikes.times # brevity # Set stim std. It must be relative to stim_rate stim_std = frac_std * stim_rate # -- Drives ------------------------------------------------------------- # Create biases/drives d_bias = {} # Background is 2 Hz d_bias['back'] = rates.constant(times, 2) # Drives proper: # 1. O d_bias['osc'] = rates.osc(times, osc_rate, f) if squelch: d_bias['osc'] = rates.constant(times, osc_rate) # 2. S d_bias['stim'] = rates.stim(times, stim_rate, stim_std, seed=stim_seed, min_rate=min_rate) # - # Linear modulatons d_bias['mult'] = d_bias['stim'] * ((g_max - g + 1) * d_bias['osc']) d_bias['add'] = d_bias['stim'] + (g * d_bias['osc']) d_bias['sub'] = d_bias['stim'] - (g * d_bias['osc']) div = d_bias['stim'] / (g * d_bias['osc']) sub = d_bias['stim'] - (g * d_bias['osc']) d_bias['div_sub'] = (q * div) + ((1 - q) * sub) # - # Nonlinear transform (RELU) for k, v in d_bias.items(): d_bias[k] = phi(v) # -- Simulate spiking --------------------------------------------------- # Create a ref stimulus stim_ref = d_bias["stim"] # Create the background pool. b_spks = backspikes.poisson(d_bias['back']) # Create OC outputs. This includes a null op stimulus, our baseline. d_spikes = {} for k in d_bias.keys(): d_spikes[k + "_p"] = np.hstack([ocspikes.poisson(d_bias[k]), b_spks]) # -- I ------------------------------------------------------------------ # Scale stim y_ref = normalize(stim_ref) d_rescaled = {} d_rescaled["stim_ref"] = y_ref if not squelch: d_rescaled["osc_ref"] = normalize(d_bias["osc"]) else: # Can't norm a single number d_rescaled["osc_ref"] = np.zeros_like(d_bias["osc"]) # Calc MI and H d_mis = {} d_deltas = {} d_hs = {} d_py = {} # Save ref H and dist d_py["stim_ref"] = discrete_dist(y_ref, m) d_hs["stim_ref"] = discrete_entropy(y_ref, m) # p(y), H, MI following rate norm for k in d_spikes.keys(): y = normalize(d_spikes[k].sum(1)) d_rescaled[k] = y d_py[k] = discrete_dist(y, m) d_mis[k] = discrete_mutual_information(y_ref, y, m) d_hs[k] = discrete_entropy(y, m) # Change in MI for k in d_mis.keys(): d_deltas[k] = d_mis[k] - d_mis["stim_p"] # -- LFP ---------------------------------------------------------------- d_lfps = {} for k in d_spikes.keys(): d_lfps[k] = create_lfps(d_spikes[k], tau=0.002, dt=.001) # -- Extract peak power ------------------------------------------------- d_powers = {} d_centers = {} for k in d_lfps.keys(): freq, spectrum = welch(d_lfps[k], fs=1000, scaling='spectrum', average='median') max_i = np.argmax(spectrum) d_powers[k] = spectrum[max_i] d_centers[k] = freq[max_i] # -- Measure OC using PAC ----------------------------------------------- low_f = (int(f - 2), int(f + 2)) high_f = (80, 250) d_pacs = {} for k in d_lfps.keys(): d_pacs[k] = pacfn(d_lfps['osc_p'], d_lfps[k], low_f, high_f) # - result = { 'MI': d_mis, 'dMI': d_deltas, 'H': d_hs, 'PAC': d_pacs, 'power': d_powers, 'center': d_centers, 'p_y': d_py, 'bias': d_bias, 'spikes': d_spikes, 'rescaled': d_rescaled, 'lfp': d_lfps, 'times': times } if save is not None: save_result(save, result) return result
n = 50 # neuron number t = 5 # run 10 seconds Istim = 2 # Avg rate of 'natural' stimulation Sstim = 0.1 * Istim # Avg st dev of natural firing Iosc = 10 # Avg rate of the oscillation f = 1 # Freq of oscillation Iback = 10 # Avg rate of the background noise # Timing dt = 0.001 rate = 1 / dt # -- SIM --------------------------------------------------------------------- # Init spikers nrns = neurons.Spikes(n, t, dt=dt, seed=42) times = nrns.times # brevity # Create biases osc = rates.osc(times, Iosc, f) stim = rates.stim(times, Istim, Sstim, seed) noise = rates.constant(times, Iback) # Simulate spiking spks_osc = nrns.poisson(osc) spks_stim = nrns.poisson(stim) spks_noise = nrns.poisson(noise) # Reformat and plot a raster spks = np.hstack([spks_osc, spks_stim, spks_noise]) # Stack 'em for plotting ns, ts = util.to_spiketimes(times, spks)