<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Implements a sparse PING E-I model, based Borges et al PNAS 2005.
"""
from __future__ import division
import argparse
import numpy as np
from brian2 import *
from syncological.inputs import gaussian_impulse
from scipy.stats.mstats import zscore
from foof.util import create_psd
from fakespikes import util as futil
import pyspike as spk


def model(time, time_stim, rate_stim, 
          w_e, w_i, w_ei, w_ie, w_ee, 
          I_e, I_i, I_i_sigma, I_e_sigma,
          stdp, seed=None):
    """Model some BRAINS!"""

    time = time * second
    time_stim = time_stim * second

    # Network
    N = 400
    N_e = int(N * 0.8)
    N_i = int(N * 0.2)
    N_stim = N_e # int(N * 0.2)

    r_e = 0 * Hz
    r_i = r_e

    delay = 2 * ms
    p_ei = 0.1
    p_ie = 0.1
    p_ee = 0.1
    p_ii = 0.6

    w_e = w_e / (p_ee * N_e) * msiemens
    w_i = w_i * msiemens
    w_ei = w_ei / (p_ei * N_e) * msiemens
    w_ie = w_ie / (p_ie * N_i) * msiemens

    w_ee = w_ee / (p_ee * N_e) * msiemens
    w_ii = 0.1 / (p_ii * N_i) * msiemens

    w_m = 0 / N_e * msiemens  # Read ref 47 to get value

    # --
    # Model
    np.random.seed(seed)
    time_step = 0.1 * ms
    decimals = 4

    # --
    # Fixed
    # cell
    Cm = 1 * uF  # /cm2

    g_Na = 100 * msiemens
    g_K = 80 * msiemens
    g_l = 0.1 * msiemens

    V_Na = 50 * mV
    V_K = -100 * mV
    V_l = -67 * mV

    # syn
    tau_r_ampa = 0.2 * ms
    tau_d_ampa = 2 * ms
    tau_r_gaba = 0.5 * ms
    tau_d_gaba = 10 * ms
    tau_r_nmda = 1 * ms
    tau_d_nmda = 100 * ms

    V_thresh = 20 * mV
    V_i = -80 * mV
    V_e = 0 * mV

    hh = """
    dV/dt = (I_Na + I_K + I_l + I_m + I_syn + I_stim + I) / Cm : volt
    """ + """
    I_Na = g_Na * (m ** 3) * h * (V_Na - V) : amp
    m = a_m / (a_m + b_m) : 1
    a_m = (0.32 * (54 + V/mV)) / (1 - exp(-0.25 * (V/mV + 54))) / ms : Hz
    b_m = (0.28 * (27 + V/mV)) / (exp(0.2 * (V/mV + 27)) - 1) / ms : Hz
    h = clip(1 - 1.25 * n, 0, inf) : 1
    """ + """
    I_K = g_K * n ** 4 * (V_K - V) : amp
    dn/dt = (a_n - (a_n * n)) - b_n * n : 1
    a_n = (0.032 * (52 + V/mV)) / (1 - exp(-0.2 * (V/mV + 52))) / ms : Hz
    b_n = 0.5 * exp(-0.025 * (57 + V/mV)) / ms : Hz
    """ + """
    I_l = g_l * (V_l - V) : amp
    """ + """
    I_m = w_m * w * (V_K - V) : amp
    dw/dt = (w_inf - w) / tau_w/ms : 1
    w_inf = 1 / (1 + exp(-1 * (V/mV + 35) / 10)) : 1
    tau_w = 400 / ((3.3 * exp((V/mV + 35)/20)) + (exp(-1 * (V/mV + 35) / 20))) : 1
    """ + """
    I_syn = g_e * (V_e - V) +
        g_ee * (V_e - V) +
        g_i * (V_i - V) : amp
    g_e : siemens
    g_ee : siemens
    g_i : siemens
    """ + """
    I_stim = g_s * (V_e - V) : amp
    g_s : siemens
    """ + """
    I : amp
    """

    syn_e_in = """
    dg/dt = -g / tau_d_ampa : siemens
    g_e_post = g : siemens (summed)
    """

    syn_e_stim = """
    dg/dt = -g / tau_d_ampa : siemens
    g_s_post = g : siemens (summed)
    """

    syn_e = """
    dg/dt = -g  * 1 / (tau_d_ampa - tau_r_ampa): siemens
    g_e_post = g : siemens (summed)
    """

    syn_ee = """
    dg/dt = -g / tau_d_ampa : siemens
    g_ee_post = g : siemens (summed)
    """

    syn_i = """
    dg/dt = -g * 1 / (tau_d_gaba - tau_r_gaba) : siemens
    g_i_post = g : siemens (summed)
    """

    P_e = NeuronGroup(
        N_e, model=hh,
        threshold='V >= V_thresh',
        refractory=3 * ms,
        method='exponential_euler'
    )
    
    P_i = NeuronGroup(
        N_i,
        model=hh,
        threshold='V >= V_thresh',
        refractory=3 * ms,
        method='exponential_euler'
    )

    P_e.V = V_l
    P_i.V = V_l
    
    if np.allclose(I_e_sigma, 0.0):
        P_e.I = I_e * uamp
    else:
        P_e.I = np.random.normal(I_e, I_e_sigma, N_e) * uamp 

    if np.allclose(I_i_sigma, 0.0):
        P_i.I = I_i * uamp
    else:
        P_i.I = np.random.normal(I_i, I_i_sigma, N_i) * uamp 
  
    P_e_back = PoissonGroup(N_e, rates=r_e)
    P_i_back = PoissonGroup(N_i, rates=r_i)

    # --
    # Stimulus
    time_stim = time_stim / second
    window = 500 / 1000.
    t_min = time_stim - window / 2
    t_max = time_stim + window / 2
    stdev = 100 / 1000.  # 100 ms

    k = N_stim * int(rate_stim / 2)
    ts, idxs = gaussian_impulse(
        time_stim, t_min,
        t_max, stdev, N_stim, k,
        decimals=decimals
    )
    P_stim = SpikeGeneratorGroup(N_stim, idxs, ts * second)

    # --
    # Syn
    # External
    C_stim_e = Synapses(
        P_stim, P_e, model=syn_e_stim,
        pre='g += w_e'
    )
    C_stim_e.connect(True, p=p_ee)
    # C_stim_e = Synapses(P_stim, P_e, model=syn_e_stim,
    #                    pre='g += w_e', connect='i == j')

    C_back_e = Synapses(P_e_back, P_e, model=syn_e_in, pre='g += w_e',
                        connect='i == j')
    C_back_i = Synapses(P_i_back, P_i, model=syn_e_in, pre='g += w_i',
                        connect='i == j')

    # Internal
    C_ei = Synapses(P_e, P_i, model=syn_e, pre='g += w_ei', delay=delay)
    C_ei.connect(True, p=p_ei)

    C_ie = Synapses(P_i, P_e, model=syn_i, pre='g += w_ie', delay=delay)
    C_ie.connect(True, p=p_ie)

    C_ii = Synapses(P_i, P_i, model=syn_i, pre='g += w_ii', delay=delay)
    C_ii.connect(True, p=p_ii)

    if stdp:
        # params        
        tau_pre = 20 * ms
        tau_post = tau_pre

        gmax = w_ee
        delta_pre = 0.05 
        delta_post = -delta_pre * tau_pre / tau_post * 1.05
        delta_pre *= gmax
        delta_post *= gmax
        
        C_ee = Synapses(<commit_msg>Updating A to match Song and Abbot 2000<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Implements a sparse PING E-I model, based Borges et al PNAS 2005.
"""
from __future__ import division
import argparse
import numpy as np
from brian2 import *
from syncological.inputs import gaussian_impulse
from scipy.stats.mstats import zscore
from foof.util import create_psd
from fakespikes import util as futil
import pyspike as spk


def model(time, time_stim, rate_stim, 
          w_e, w_i, w_ei, w_ie, w_ee, 
          I_e, I_i, I_i_sigma, I_e_sigma,
          stdp, seed=None):
    """Model some BRAINS!"""

    time = time * second
    time_stim = time_stim * second

    # Network
    N = 400
    N_e = int(N * 0.8)
    N_i = int(N * 0.2)
    N_stim = N_e # int(N * 0.2)

    r_e = 0 * Hz
    r_i = r_e

    delay = 2 * ms
    p_ei = 0.1
    p_ie = 0.1
    p_ee = 0.1
    p_ii = 0.6

    w_e = w_e / (p_ee * N_e) * msiemens
    w_i = w_i * msiemens
    w_ei = w_ei / (p_ei * N_e) * msiemens
    w_ie = w_ie / (p_ie * N_i) * msiemens

    w_ee = w_ee / (p_ee * N_e) * msiemens
    w_ii = 0.1 / (p_ii * N_i) * msiemens

    w_m = 0 / N_e * msiemens  # Read ref 47 to get value

    # --
    # Model
    np.random.seed(seed)
    time_step = 0.1 * ms
    decimals = 4

    # --
    # Fixed
    # cell
    Cm = 1 * uF  # /cm2

    g_Na = 100 * msiemens
    g_K = 80 * msiemens
    g_l = 0.1 * msiemens

    V_Na = 50 * mV
    V_K = -100 * mV
    V_l = -67 * mV

    # syn
    tau_r_ampa = 0.2 * ms
    tau_d_ampa = 2 * ms
    tau_r_gaba = 0.5 * ms
    tau_d_gaba = 10 * ms
    tau_r_nmda = 1 * ms
    tau_d_nmda = 100 * ms

    V_thresh = 20 * mV
    V_i = -80 * mV
    V_e = 0 * mV

    hh = """
    dV/dt = (I_Na + I_K + I_l + I_m + I_syn + I_stim + I) / Cm : volt
    """ + """
    I_Na = g_Na * (m ** 3) * h * (V_Na - V) : amp
    m = a_m / (a_m + b_m) : 1
    a_m = (0.32 * (54 + V/mV)) / (1 - exp(-0.25 * (V/mV + 54))) / ms : Hz
    b_m = (0.28 * (27 + V/mV)) / (exp(0.2 * (V/mV + 27)) - 1) / ms : Hz
    h = clip(1 - 1.25 * n, 0, inf) : 1
    """ + """
    I_K = g_K * n ** 4 * (V_K - V) : amp
    dn/dt = (a_n - (a_n * n)) - b_n * n : 1
    a_n = (0.032 * (52 + V/mV)) / (1 - exp(-0.2 * (V/mV + 52))) / ms : Hz
    b_n = 0.5 * exp(-0.025 * (57 + V/mV)) / ms : Hz
    """ + """
    I_l = g_l * (V_l - V) : amp
    """ + """
    I_m = w_m * w * (V_K - V) : amp
    dw/dt = (w_inf - w) / tau_w/ms : 1
    w_inf = 1 / (1 + exp(-1 * (V/mV + 35) / 10)) : 1
    tau_w = 400 / ((3.3 * exp((V/mV + 35)/20)) + (exp(-1 * (V/mV + 35) / 20))) : 1
    """ + """
    I_syn = g_e * (V_e - V) +
        g_ee * (V_e - V) +
        g_i * (V_i - V) : amp
    g_e : siemens
    g_ee : siemens
    g_i : siemens
    """ + """
    I_stim = g_s * (V_e - V) : amp
    g_s : siemens
    """ + """
    I : amp
    """

    syn_e_in = """
    dg/dt = -g / tau_d_ampa : siemens
    g_e_post = g : siemens (summed)
    """

    syn_e_stim = """
    dg/dt = -g / tau_d_ampa : siemens
    g_s_post = g : siemens (summed)
    """

    syn_e = """
    dg/dt = -g  * 1 / (tau_d_ampa - tau_r_ampa): siemens
    g_e_post = g : siemens (summed)
    """

    syn_ee = """
    dg/dt = -g / tau_d_ampa : siemens
    g_ee_post = g : siemens (summed)
    """

    syn_i = """
    dg/dt = -g * 1 / (tau_d_gaba - tau_r_gaba) : siemens
    g_i_post = g : siemens (summed)
    """

    P_e = NeuronGroup(
        N_e, model=hh,
        threshold='V >= V_thresh',
        refractory=3 * ms,
        method='exponential_euler'
    )
    
    P_i = NeuronGroup(
        N_i,
        model=hh,
        threshold='V >= V_thresh',
        refractory=3 * ms,
        method='exponential_euler'
    )

    P_e.V = V_l
    P_i.V = V_l
    
    if np.allclose(I_e_sigma, 0.0):
        P_e.I = I_e * uamp
    else:
        P_e.I = np.random.normal(I_e, I_e_sigma, N_e) * uamp 

    if np.allclose(I_i_sigma, 0.0):
        P_i.I = I_i * uamp
    else:
        P_i.I = np.random.normal(I_i, I_i_sigma, N_i) * uamp 
  
    P_e_back = PoissonGroup(N_e, rates=r_e)
    P_i_back = PoissonGroup(N_i, rates=r_i)

    # --
    # Stimulus
    time_stim = time_stim / second
    window = 500 / 1000.
    t_min = time_stim - window / 2
    t_max = time_stim + window / 2
    stdev = 100 / 1000.  # 100 ms

    k = N_stim * int(rate_stim / 2)
    ts, idxs = gaussian_impulse(
        time_stim, t_min,
        t_max, stdev, N_stim, k,
        decimals=decimals
    )
    P_stim = SpikeGeneratorGroup(N_stim, idxs, ts * second)

    # --
    # Syn
    # External
    C_stim_e = Synapses(
        P_stim, P_e, model=syn_e_stim,
        pre='g += w_e'
    )
    C_stim_e.connect(True, p=p_ee)
    # C_stim_e = Synapses(P_stim, P_e, model=syn_e_stim,
    #                    pre='g += w_e', connect='i == j')

    C_back_e = Synapses(P_e_back, P_e, model=syn_e_in, pre='g += w_e',
                        connect='i == j')
    C_back_i = Synapses(P_i_back, P_i, model=syn_e_in, pre='g += w_i',
                        connect='i == j')

    # Internal
    C_ei = Synapses(P_e, P_i, model=syn_e, pre='g += w_ei', delay=delay)
    C_ei.connect(True, p=p_ei)

    C_ie = Synapses(P_i, P_e, model=syn_i, pre='g += w_ie', delay=delay)
    C_ie.connect(True, p=p_ie)

    C_ii = Synapses(P_i, P_i, model=syn_i, pre='g += w_ii', delay=delay)
    C_ii.connect(True, p=p_ii)

    if stdp:
        # params        
        tau_pre = 20 * ms
        tau_post = tau_pre

        gmax = w_ee
        delta_pre = 0.005 
        delta_post = -delta_pre * tau_pre / tau_post * 1.05
        delta_pre *= gmax
        delta_post *= gmax
        
        C_ee = Synapses(
{'fraction_non_alphanumeric': 0.15210964794410106, 'fraction_numerical': 0.03180148705545104, 'mean_word_length': 1.7295843520782397, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3443423', 'n_tokens_mistral': 5499, 'n_tokens_neox': 4988, 'n_words': 1144}