Ejemplo n.º 1
0
def test_SCCircuit():
    EPS = 1e-12
    DTYPE = tf.float64

    np.random.seed(0)
    M = 100
    sess = tf.Session()
    # true_sys = v1_circuit(T=T, dt=dt)
    Z = tf.placeholder(dtype=DTYPE, shape=(1, M, None))

    p = 0.7
    std = 0.05
    var = std**2
    inact_str = "NI"
    N = 500
    param_dict = {"behavior_type": "WTA", 
                  "p": p, 
                  "var": var, 
                  "inact_str": inact_str,
                  "N":N}

    system = get_system_from_template("SCCircuit", param_dict)

    print(system.behavior_str)
    assert (
        system.behavior_str
        == "WTA_mu=7.00E-01_7.00E-01_2.50E-03_2.50E-03_0.00E+00_0.00E+00_1.00E+00_1.00E+00"
    )
    assert system.D == 8
    assert system.num_suff_stats == 8

    # Test simulation
    _Z = np.random.normal(0.0, 3.0, (1, M, system.D))

    r_t = system.simulate(Z)
    _r_t = sess.run(r_t, {Z: _Z})

    true_sys = sc_circuit()

    _sW_P = _Z[0, :, 0]
    _sW_A = _Z[0, :, 1]
    _vW_PA = _Z[0, :, 2]
    _vW_AP = _Z[0, :, 3]
    _dW_PA = _Z[0, :, 4]
    _dW_AP = _Z[0, :, 5]
    _hW_P = _Z[0, :, 6]
    _hW_A = _Z[0, :, 7]

    W = np.array(
        [
            [_sW_P, _vW_PA, _dW_PA, _hW_P],
            [_vW_AP, _sW_A, _hW_A, _dW_AP],
            [_dW_AP, _hW_A, _sW_A, _vW_AP],
            [_hW_P, _dW_PA, _vW_PA, _sW_P],
        ]
    )
    W = np.transpose(W, [2, 0, 1])

    E_constant = 0.0
    E_Pbias = 0.0
    E_Prule = 10.0
    E_Arule = 10.0
    E_choice = 2.0
    E_light = 1.0
    Es = [E_constant, E_Pbias, E_Prule, E_Arule, E_choice, E_light]

    _w = system.w
    N = _w.shape[4]

    eta_NI = np.ones((system.T,))

    r_t_true = np.zeros((2, system.T, M, 4, system.N))
    for i in range(M):
        for j in range(N):
            _r_t_true_ij = true_sys.simulate(W[i], Es, _w[:, 0, 0, :, j], eta_NI)
            r_t_true[:, :, i, :, j] = _r_t_true_ij
    r_t_true = np.transpose(r_t_true, [1, 0, 2, 3, 4])
    assert approx_equal(_r_t, r_t_true, EPS)

    return None
Ejemplo n.º 2
0
sysname = "SCCircuit"
dir_str = 'SC_WTA_%s' % inact_str

std = 0.15
var = std**2
N = 500
param_dict = {
    "behavior_type": "WTA",
    "p": p,
    "var": var,
    "inact_str": inact_str,
    "N": N,
}

system = get_system_from_template(sysname, param_dict)

# Get DSN architecture
repeats = 1
nlayers = 2
arch_params = {
    'D': system.D,
    'repeats': repeats,
    'nlayers': nlayers,
    'upl': upl,
    'sigma_init': sigma_init,
}

param_dict.update(arch_params)
arch_dict = get_arch_from_template(system, param_dict)
Ejemplo n.º 3
0
def test_V1Circuit():
    os.chdir('dsn/')
    np.random.seed(0)
    M = 100
    sysname = 'V1Circuit'
    T = 100
    dt = 0.005
    W_allen = read_W('allen')

    # Test rates behavior
    #TODO at some point this should update to incorporate s into the input function
    behavior_type = 'rates'
    fac = 1.0
    s = [5]
    C = len(s)
    param_dict = {'behavior_type': behavior_type, 'fac': fac, 's': s}
    system = get_system_from_template(sysname, param_dict)
    assert (len(system.z_labels) == system.D)

    # Test parameter setting
    Z = tf.placeholder(tf.float64, (1, None, system.D))
    W, b, h_FF, h_LAT, h_RUN, tau, n, s_0, a, c_50 = system.filter_Z(Z)

    _Z = np.zeros((1, M, system.D))
    Z_a, Z_b = system.density_network_bounds
    for i in range(system.D):
        _Z[0, :, i] = np.random.uniform(Z_a[i], Z_b[i], (M, ))

    tf_vars = [W, b, h_FF, h_LAT, h_RUN, tau, n]
    with tf.Session() as sess:
        _tf_vars = sess.run(tf_vars, {Z: _Z})
    _W, _b, _h_FF, _h_LAT, _h_RUN, _tau, _n = _tf_vars
    for i in range(M):
        assert (approx_equal(np.abs(_W[0, i, :, :]), W_allen, EPS))
        assert (approx_equal(_b[0, i, :, 0], _Z[0, i, :4], EPS))
        assert (approx_equal(_n[0, i, 0], _Z[0, i, 4], EPS))
    assert (approx_equal(_h_FF, 0.0, EPS))
    assert (approx_equal(_h_LAT, 0.0, EPS))
    assert (approx_equal(_h_RUN, 0.0, EPS))
    assert (approx_equal(_tau, 0.02, EPS))

    # Test input calculation
    h = system.compute_h(b, h_FF, h_LAT, h_RUN, s_0)
    with tf.Session() as sess:
        _h = sess.run(h, {Z: _Z})
    assert (approx_equal(_h[0, :, :, 0], _Z[0, :, :4], EPS))

    # Test suff_stat calculation
    r_t = system.simulate(Z)
    T_x = system.compute_suff_stats(Z)
    with tf.Session() as sess:
        _r_t = sess.run(r_t, {Z: _Z})
        _T_x = sess.run(T_x, {Z: _Z})
    v1_circuit_true = v1_circuit(T=T, dt=dt, init_conds=system.init_conds)
    r_ss = np.zeros((M, 4))
    for i in range(M):
        r_t_i = v1_circuit_true.simulate(_W[0, i, :, :], _h[0, i, :, :],
                                         _tau[0, i, 0, 0], _n[0, i, 0, 0])
        r_ss[i, :] = r_t_i[-1, :]
        #assert(approx_equal(_r_t[:,0,i,:,0], r_t_i, 1e-6)) # should make exact
    mean_true = r_ss
    var_true = np.square(r_ss - np.expand_dims(system.mu[:4], 0))
    T_x_true = np.concatenate((mean_true, var_true), axis=1)
    assert (approx_equal(_T_x, np.expand_dims(T_x_true, 0), 1e-6))

    # Check that mu matches data
    # Local test. Cant put this data on github.
    s_list = [5, [5], [10], np.array([60])]
    fac_list = [1.0, 0.5, 100.0]
    num_s = len(s_list)
    num_fac = len(fac_list)

    for i in range(num_s):
        s = s_list[i]
        means, stds = read_V1_responses(s)
        for j in range(num_fac):
            fac = fac_list[j]
            means_true = fac * means
            stds_true = fac * stds
            vars_true = np.square(stds_true)
            mu_true = np.concatenate((means_true, vars_true), axis=0)
            param_dict = {'behavior_type': behavior_type, 'fac': fac, 's': s}
            system = get_system_from_template(sysname, param_dict)
            assert (len(system.T_x_labels) == system.num_suff_stats)
            assert (system.mu.shape[0] == system.num_suff_stats)
            approx_equal(mu_true, system.mu, EPS)

    # Test differences behavior
    #TODO at some point this should update to incorporate s into the input function
    behavior_type = 'difference'
    alphas = ['E', 'P', 'S', 'V']
    num_alphas = len(alphas)
    for k in range(num_alphas):
        alpha = alphas[k]
        param_dict = {
            "behavior_type": behavior_type,
            "alpha": alpha,
            "inc_val": 0.5,
        }
        system = get_system_from_template(sysname, param_dict)
        assert (len(system.z_labels) == system.D)

        # Test parameter setting
        Z = tf.placeholder(tf.float64, (1, None, system.D))
        W, b, h_FF, h_LAT, h_RUN, tau, n, s_0, a, c_50 = system.filter_Z(Z)

        _Z = np.zeros((1, M, system.D))
        Z_a, Z_b = system.density_network_bounds
        for i in range(system.D):
            _Z[0, :, i] = np.random.uniform(Z_a[i], Z_b[i], (M, ))

        tf_vars = [W, b, h_FF, h_LAT, h_RUN, tau, n]
        with tf.Session() as sess:
            _tf_vars = sess.run(tf_vars, {Z: _Z})
        _W, _b, _h_FF, _h_LAT, _h_RUN, _tau, _n = _tf_vars
        for i in range(M):
            assert (approx_equal(np.abs(_W[0, i, :, :]), W_allen, EPS))
            assert (approx_equal(_h_RUN[0, i, :, 0], _Z[0, i, :], EPS))
        assert (approx_equal(_n, 2.0, EPS))
        assert (approx_equal(_h_FF, 0.0, EPS))
        assert (approx_equal(_h_LAT, 0.0, EPS))
        assert (approx_equal(_b, 1.0, EPS))
        assert (approx_equal(_tau, 0.02, EPS))

        # Test input calculation
        h = system.compute_h(b, h_FF, h_LAT, h_RUN, s_0)
        with tf.Session() as sess:
            _h = sess.run(h, {Z: _Z})

    # Test suff_stat calculation
        r_t = system.simulate(Z)
        T_x = system.compute_suff_stats(Z)
        with tf.Session() as sess:
            _r_t = sess.run(r_t, {Z: _Z})
            _T_x = sess.run(T_x, {Z: _Z})

        v1_circuit_true = v1_circuit(T=T, dt=dt, init_conds=system.init_conds)
        r_ss = np.zeros((M, 2))
        for i in range(M):
            r_t_i_NORUN = v1_circuit_true.simulate(_W[0, i, :, :], _h[0,
                                                                      i, :, :],
                                                   _tau[0, i, 0, 0], _n[0, i,
                                                                        0, 0])
            r_t_i_RUN = v1_circuit_true.simulate(_W[0, i, :, :], _h[1,
                                                                    i, :, :],
                                                 _tau[0, i, 0, 0], _n[0, i, 0,
                                                                      0])
            r_ss[i, 0] = r_t_i_NORUN[-1, k]
            r_ss[i, 1] = r_t_i_RUN[-1, k]
            #assert(approx_equal(_r_t[:,0,i,:,0], r_t_i, 1e-6)) # should make exact
        dr_ss = r_ss[:, 1] - r_ss[:, 0]
        mean_true = np.expand_dims(dr_ss, 1)
        var_true = np.square(mean_true - system.mu[0])
        T_x_true = np.concatenate((mean_true, var_true), axis=1)
        assert (approx_equal(_T_x, np.expand_dims(T_x_true, 0), 1e-6))

    return None