In [3]:
import moose

In [4]:
size = 1024     # number of IntFire objects in a vec
delayMin = 0    
delayMax = 4
Vmax = 1.0
thresh = 0.8
refractoryPeriod = 0.4
connectionProbability = 0.1
weightMax = 0.5

The above sets the constants we shall use in this example. Now we create a vector of IntFire elements of size size.


In [5]:
net = moose.IntFire('/network', size)

This creates a vec of IntFire elements of size 1024 and returns the first element, i.e. "/network[0]".


In [6]:
net == moose.element('/network[0]')


Out[6]:
True

You can access the underlying vector of elements using the vec field on any element. This is very useful for vectorized field access:


In [7]:
net.vec.bufferTime = [2 * delayMax] * size

The right part of the assigment creates a Python list of length size with each element set to 2 * delayMax, which is 8.0. You can index into the vec to access individual elements' field:


In [8]:
print net.vec[1].bufferTime


8.0

IntFire class has an ElementField called synapse. It is just like a vec above in terms of field access, but by default its size is 0.


In [9]:
print len(net.synapse)


0

To actually create synapses, you can explicitly assign the num field of this, or set the numSynapses field of the IntFire element. There are some functions which can implicitly set the size of the ElementField. Here is the implicit way:


In [10]:
synapse = moose.element('/network[0]/synapse')
mid = moose.connect(net, 'spikeOut', synapse, 'addSpike', 'Sparse') # This creates a `Sparse` message from `spikeOut` source of net to `addSpike` destination on synapse.

In [12]:
msg = moose.element(mid)
msg.setRandomConnectivity(connectionProbability, 5489)


Out[12]:
True

The above sets random connectivity in the sparse message. Now you can see what are the sizes of each synapse in each element of '/network'


In [14]:
for ii in range(10, 15):
    el = moose.element(net.vec[ii])
    print el.path, 'has', len(el.synapse), 'synaptic connections'


/network[10] has 102 synaptic connections
/network[11] has 107 synaptic connections
/network[12] has 101 synaptic connections
/network[13] has 109 synaptic connections
/network[14] has 101 synaptic connections

You can set the weight and delay fields of each synapse using various looping methods


In [22]:
import random # We need this for random number generation
from numpy import random as nprand
for item in net.vec:
    neuron = moose.element(item) # You have to convert each entry of a `vec` into corresponding IntFire element in order to access class-specific fields
    # Below is one (inefficient) way of setting the individual weights of the elements in 'synapse'
    for ii in range(len(neuron.synapse)):
        neuron.synapse[ii].weight = random.random() * weightMax
    # This is a more efficient way - rhs of `=` is list comprehension in Python and rather fast
    neuron.synapse.delay = [delayMin + random.random() * delayMax for ii in range(len(neuron.synapse))]
    # An even faster way will be to use numpy.random.rand(size) which produces array of random numbers uniformly distributed between 0 and 1
    neuron.synapse.delay = delayMin + nprand.rand(len(neuron.synapse)) * delayMax

Now display the results, we use slice notation on vec to show the values of delay and weight for the first 5 elements in /network


In [25]:
for item in net.vec[:5]:
    neuron = moose.element(item)
    print 'Delays for synapses on ', neuron.path, ':', neuron.synapse.delay
    print 'Weights for synapses on ', neuron.path, ':', neuron.synapse.weight


Delays for synapses on  /network[0] : [ 2.16949827  2.87672892  0.55775816  1.5307453   1.2545798   3.08150047
  1.69471445  1.72783837]
Weights for synapses on  /network[0] : [ 0.01378044  0.16739938  0.18983276  0.36676815  0.1026694   0.40671933
  0.01169365  0.2939791 ]
Delays for synapses on  /network[1] : [ 0.66554382  0.69812038  3.84877248]
Weights for synapses on  /network[1] : [ 0.32135398  0.2293562   0.35553433]
Delays for synapses on  /network[2] : [ 2.84297188  3.64714918  3.59338126  2.01446565  1.1388517   1.91031348
  3.82746114]
Weights for synapses on  /network[2] : [ 0.12762092  0.06323899  0.00916775  0.11610243  0.04453261  0.46170508
  0.30207453]
Delays for synapses on  /network[3] : [ 3.14259038  1.73694338  1.9056121   1.96542664  1.11544011  2.55803099]
Weights for synapses on  /network[3] : [ 0.30468313  0.04061773  0.18337534  0.47436774  0.04179184  0.30259949]
Delays for synapses on  /network[4] : [ 1.08777299  1.71847959  3.5191226   3.84163071  3.84634335]
Weights for synapses on  /network[4] : [ 0.29244741  0.10342571  0.3912255   0.02361261  0.27258286]

In [ ]: