Setup network


In [1]:
import nest
import itertools
import numpy as np

nest.ResetKernel()

new_neuron = lambda x: nest.Create('iaf_neuron')[0]

sources = [new_neuron(x) for x in range(5)]  # 5 input neurons
targets = [new_neuron(x) for x in range(5)]  # 5 output neurons


# connect neurons "all-to-all" with "plastic" synapses
for s, t in itertools.product(sources, targets):
    syn_spec = {'weight': 500 * np.random.rand(1)[0], 'model': 'stdp_pl_synapse_hom'}
    nest.Connect([s], [t], syn_spec=syn_spec)


# inject current / noise to one input neuron
dc = nest.Create("dc_generator", 1)
nest.SetStatus(dc, {'amplitude': 900.0, 'start': 0., 'stop': 5000.})

nest.Connect(dc, [sources[0]], syn_spec={'weight': 150.0, 'model': 'static_synapse'})

Prepare file and run simulation


In [2]:
import nix
import nix4nest
from nix4nest.nix.weightstack import WeightStack


# open the file where to dump data
f = nix.File.open("/tmp/simulation.h5", nix.FileMode.Overwrite)
block = f.create_nest_block("demo", "simulation")


# create a "weight stack" object to dump weights
params = {
    'where': block,
    'name': "evolution",
    'weights': np.empty((0, len(sources), len(targets))),  # empty before simulation
    'sources': sources,
    'targets': targets
}
ws = WeightStack.create_weight_stack(**params)


# simulate
simulation_time = 5000
time_passed = 0
step = 1000

while time_passed < simulation_time:
    # capture actual weights between <sources> and <targets>
    snapshot = block.capture_weights(sources, targets)
    
    # save weights (to file) with weight stack object
    ws.append_snapshot([snapshot], time_passed)
    
    nest.Simulate(step)
    time_passed += step
    
f.close()

Access weight evolution


In [3]:
from plotting import weights_multiple

f = nix.File.open("/tmp/simulation.h5", nix.FileMode.ReadOnly)
ws = f.blocks[0].weightstacks[0]

# explore dimensions
for dim in ws.dimensions:
    print "Dimension #%d: %s" % (dim.index, dim.label)


Dimension #1: time
Dimension #2: source
Dimension #3: target

In [4]:
# understand time dimension
times = ws.dimensions[0]

print times.ticks
print times.unit


(0.0, 1000.0, 2000.0, 3000.0, 4000.0)
ms

In [5]:
# compare weights at timepoints 0 ms and 3000 ms
i1 = int(np.where(np.array(times.ticks) == 0.0)[0][0])
i2 = int(np.where(np.array(times.ticks) == 3000.0)[0][0])

fig = weights_multiple([ws.data[i1], ws.data[i2]])



In [6]:
f.close()