Ejemplo n.º 1
0
def test_construction_single_synapses():
    '''
    Test the construction of synapses with a single synapse per connection.
    '''
    G = NeuronGroup(20, model='v:1', threshold=NoThreshold())

    # specifying only one group should use it as source and target
    syn = Synapses(G, model='w:1')
    assert syn.source is syn.target

    # specifying source and target with subgroups
    subgroup1, subgroup2 = G[:10], G[10:]
    syn = Synapses(subgroup1, subgroup2, model='w:1', pre='v += w')
    assert syn.source is subgroup1
    assert syn.target is subgroup2

    # create all-to-all connections
    syn[:, :] = True
    assert len(syn) == 10 * 10

    # deleting a synapse should not work
    def delete_synapse():
        syn[0, 0] = False

    assert_raises(ValueError, delete_synapse)

    # set the weights
    syn.w[:, :] = 2
    # set the delays
    syn.delay[:, :] = 1 * ms

    all_weights = np.array([
        syn.w[i, j] for i in xrange(len(subgroup1))
        for j in xrange(len(subgroup2))
    ])
    assert (all_weights == 2).all()
    all_delays = np.array([
        syn.delay[i, j] for i in xrange(len(subgroup1))
        for j in xrange(len(subgroup2))
    ])
    assert (all_delays == 1 * ms).all()

    # create one-to-one connections
    syn = Synapses(subgroup1, subgroup2, model='w:1', pre='v += w')
    syn[:, :] = 'i == j'
    syn.w[:, :] = 2
    assert len(syn) == len(subgroup1)
    for i in xrange(len(subgroup1)):
        for j in xrange(len(subgroup2)):
            if i == j:
                assert syn.w[i, j] == 2
            else:
                assert len(syn.w[i, j]) == 0

    net = Network(G, syn)
    net.run(defaultclock.dt)

    # adding synapses should not work after the simulation has already been run
    def adding_a_synapse():
        syn[0, 1] = True

    assert_raises(AttributeError, adding_a_synapse)

    syn = Synapses(subgroup1, subgroup2, model='w:1', pre='v += w')
    syn.connect_one_to_one(subgroup1, subgroup2)
    syn.w[:, :] = 2
    assert len(syn) == len(subgroup1)
    for i in xrange(len(subgroup1)):
        for j in xrange(len(subgroup2)):
            if i == j:
                assert syn.w[i, j] == 2
            else:
                assert len(syn.w[i, j]) == 0

    # Calling connect_one_to_one without specifying groups should use the full
    # source or target group
    syn = Synapses(subgroup1, subgroup2, model='w:1', pre='v += w')
    syn.connect_one_to_one(subgroup1)
    assert len(syn) == len(subgroup1)

    syn = Synapses(subgroup1, subgroup2, model='w:1', pre='v += w')
    syn.connect_one_to_one(post=subgroup2)
    assert len(syn) == len(subgroup1)

    syn = Synapses(subgroup1, subgroup2, model='w:1', pre='v += w')
    syn.connect_one_to_one()
    assert len(syn) == len(subgroup1)

    # connect_one_to_one should only work with subgroups of the same size
    assert_raises(TypeError, lambda: syn.connect_one_to_one(G[:2], G[:1]))

    # create random connections
    # the only two cases that can be tested exactly are 0 and 100% connection
    # probability
    syn = Synapses(subgroup1, subgroup2, model='w:1', pre='v += w')
    # floats are interpreted as connection probabilities
    syn[:, :] = 0.0
    assert len(syn) == 0

    syn = Synapses(subgroup1, subgroup2, model='w:1', pre='v += w')
    syn.connect_random(subgroup1, subgroup2, 0.0)
    assert len(syn) == 0

    syn = Synapses(subgroup1, subgroup2, model='w:1', pre='v += w')
    syn[:, :] = 1.0
    assert len(syn) == 10 * 10
    # set the weights
    syn.w[:, :] = 2
    # set the delays
    syn.delay[:, :] = 1 * ms

    all_weights = np.array([
        syn.w[i, j] for i in xrange(len(subgroup1))
        for j in xrange(len(subgroup2))
    ])
    assert (all_weights == 2).all()
    all_delays = np.array([
        syn.delay[i, j] for i in xrange(len(subgroup1))
        for j in xrange(len(subgroup2))
    ])
    assert (all_delays == 1 * ms).all()

    syn = Synapses(subgroup1, subgroup2, model='w:1', pre='v += w')
    syn.connect_random(subgroup1, subgroup2, 1.0)
    assert len(syn) == 10 * 10
    # set the weights
    syn.w[:, :] = 2
    # set the delays
    syn.delay[:, :] = 1 * ms

    all_weights = np.array([
        syn.w[i, j] for i in xrange(len(subgroup1))
        for j in xrange(len(subgroup2))
    ])
    assert (all_weights == 2).all()
    all_delays = np.array([
        syn.delay[i, j] for i in xrange(len(subgroup1))
        for j in xrange(len(subgroup2))
    ])
    assert (all_delays == 1 * ms).all()

    # Calling connect_random without specifying groups should use the full
    # source or target group
    syn = Synapses(subgroup1, subgroup2, model='w:1', pre='v += w')
    syn.connect_random(subgroup1, sparseness=1.0)
    assert len(syn) == 10 * 10

    syn = Synapses(subgroup1, subgroup2, model='w:1', pre='v += w')
    syn.connect_random(post=subgroup2, sparseness=1.0)
    assert len(syn) == 10 * 10

    syn = Synapses(subgroup1, subgroup2, model='w:1', pre='v += w')
    syn.connect_random(sparseness=1.0)
    assert len(syn) == 10 * 10

    # Just test that probabilities between zero and one work at all
    syn = Synapses(subgroup1, subgroup2, model='w:1', pre='v += w')
    syn.connect_random(subgroup1, subgroup2, 0.3)
    syn = Synapses(subgroup1, subgroup2, model='w:1', pre='v += w')
    syn[:, :] = 0.3

    # Test that probabilities outside of the legal range raise an error
    syn = Synapses(subgroup1, subgroup2, model='w:1', pre='v += w')
    assert_raises(ValueError,
                  lambda: syn.connect_random(subgroup1, subgroup2, 1.3))
    assert_raises(ValueError,
                  lambda: syn.connect_random(subgroup1, subgroup2, -.3))

    def wrong_probability():
        syn[:, :] = -0.3

    assert_raises(ValueError, wrong_probability)

    def wrong_probability():  #@DuplicatedSignature
        syn[:, :] = 1.3

    assert_raises(ValueError, wrong_probability)

    # this test requires python 2.6
    if sys.version_info[0] >= 2 and sys.version_info[1] >= 6:
        # Running a model with a synapses object with 0 synapses should raise a warning
        syn = Synapses(subgroup1, subgroup2, model='w:1', pre='v += w')
        with warnings.catch_warnings(record=True) as w:
            # Cause all warnings to always be triggered.
            warnings.simplefilter("always")
            # Trigger a warning.
            syn.compress()
            # Verify some things
            assert len(w) == 1

    # use arrays as neuron indices instead of slices or ints
    syn = Synapses(subgroup1, subgroup2, model='w:1', pre='v += w')
    syn[np.arange(5), np.arange(5)] = True
    syn.w[np.arange(5), np.arange(5)] = 2
    syn.delay[np.arange(5), np.arange(5)] = 5 * ms
    assert len(syn) == 5 * 5
    assert all([syn.w[i, j] == 2 for i in xrange(5) for j in xrange(5)])
    assert all(
        [syn.delay[i, j] == 5 * ms for i in xrange(5) for j in xrange(5)])
    assert all([
        len(syn.w[i, j]) == 0 for i in xrange(5, len(subgroup1))
        for j in xrange(5, len(subgroup2))
    ])
    assert all([
        len(syn.delay[i, j]) == 0 for i in xrange(5, len(subgroup1))
        for j in xrange(5, len(subgroup2))
    ])

    # Create a synapse without a model (e.g. a simple connection with constant
    # weights
    syn = Synapses(subgroup1, subgroup2, model='', pre='v+=1')