def test_recording(): """ tests if the shape of recording is correct """ import numpy as np from rockpool import timeseries as ts from rockpool.layers import FFIAFNest # - Generic parameters weights = np.array([[-0.1, 0.02, 0.4], [0.2, -0.3, -0.15]]) bias = 0.01 tau_mem = [0.02, 0.05, 0.1] # - Layer generation fl0 = FFIAFNest( weights=weights, dt=0.0001, bias=bias, tau_mem=tau_mem, refractory=0.001, record=True, ) # - Input signal tsInCont = ts.TSContinuous(times=np.arange(15) * 0.01, samples=np.ones((15, 2))) # - Compare states before and after vStateBefore = np.copy(fl0.state) dFl0 = fl0.evolve(tsInCont, duration=0.1) assert np.shape(fl0.recorded_states.samples) == (1000, 3) fl0.terminate()
def test_ffiaf(): """ Test FFIAFBrian """ from brian2 import second from rockpool import timeseries as ts from rockpool.layers import FFIAFBrian # - Generic parameters weights = np.array([[-0.1, 0.02, 0.4], [0.2, -0.3, -0.15]]) bias = 0.01 tau_mem = [0.02, 0.05, 0.1] # - Layer generation fl0 = FFIAFBrian( weights=weights, dt=0.01, bias=bias, tau_mem=tau_mem, noise_std=0.1, refractory=0.001 * second, ) # - Input signal tsInCont = ts.TSContinuous(times=np.arange(15) * 0.01, samples=np.ones((15, 2))) # - Compare states and time before and after vStateBefore = np.copy(fl0.state) fl0.evolve(tsInCont, duration=0.1) assert fl0.t == 0.1 assert (vStateBefore != fl0.state).any() fl0.reset_all() assert fl0.t == 0 assert (vStateBefore == fl0.state).all()
def test_chargeAndSpikeSingleNeuron(): """ single neuron test charge neuron exactly to threshold without crossing using the bias add small input to make it spike """ from rockpool import timeseries as ts from rockpool.layers import FFIAFNest weights = [[1.0]] bias = [0.375] tau_mem = [0.01] vReset = -0.07 vRest = -0.07 vTh = -0.055 fC = 0.25 dt = 0.001 refractory = 0.002 fl0 = FFIAFNest( weights=weights, dt=dt, bias=bias, tau_mem=tau_mem, v_reset=vReset, v_rest=vRest, v_thresh=vTh, capacity=fC, refractory=refractory, record=True, name="test", ) # - Input signal vTime = np.arange(0, 1, dt) vVal = np.zeros([len(vTime), 1]) vVal[500] = 0.001 tsInCont = ts.TSContinuous(vTime, vVal) dFl0 = fl0.evolve(tsInCont, duration=1.0) assert not dFl0.isempty() fl0.terminate()
def test_FFNestLayer(): """ Test FFIAFNest""" from rockpool import timeseries as ts from rockpool.layers import FFIAFNest # - Generic parameters weights = np.array([[-0.1, 0.02, 0.4], [0.2, -0.3, -0.15]]) bias = 0.01 tau_mem = [0.02, 0.05, 0.1] # - Layer generation fl0 = FFIAFNest( weights=weights, dt=0.001, bias=bias, tau_mem=tau_mem, refractory=0.001, record=True, ) # - Input signal tsInCont = ts.TSContinuous(times=np.arange(15) * 0.01, samples=np.ones((15, 2))) # - Compare states before and after vStateBefore = np.copy(fl0.state) dFl0 = fl0.evolve(tsInCont, duration=0.1) dFl0 = fl0.evolve(tsInCont, duration=0.1) assert fl0.t == 0.2 assert (vStateBefore != fl0.state).any() fl0.reset_all() assert fl0.t == 0 assert (vStateBefore == fl0.state).all() fl0.terminate()
def test_reciaf(): """ Test RecIAFBrian """ from brian2 import second from rockpool import timeseries as ts from rockpool.layers import RecIAFBrian # - Generic parameters np.random.seed(1) weights = 2 * np.random.rand(3, 3) - 1 bias = 2 * np.random.rand(3) - 1 tau_mem, tau_syn_r = np.clip(np.random.rand(2, 3), 0.01, None) # - Layer generation rl0 = RecIAFBrian( weights=weights, bias=bias, tau_mem=tau_mem, tau_syn_r=tau_syn_r, dt=0.01, noise_std=0.1, refractory=0.001 * second, ) # - Input signal tsInCont = ts.TSContinuous(times=np.arange(15) * 0.01, samples=np.ones((15, 3))) # - Compare states and time before and after vStateBefore = np.copy(rl0.state) rl0.evolve(tsInCont, duration=0.1) assert rl0.t == 0.1 assert (vStateBefore != rl0.state).any() rl0.reset_all() assert rl0.t == 0 assert (vStateBefore == rl0.state).all()
def test_delays(): """ test delays """ from rockpool import timeseries as ts from rockpool.layers import FFIAFNest, RecIAFSpkInNest from rockpool.networks import network as nw import numpy as np weights = [[0.001, 0.0, 0.0, 0.0]] bias = 0.375 tau_mem = 0.01 vReset = -0.07 vRest = -0.07 vTh = -0.055 fC = 0.25 dt = 0.001 refractory = 0.002 fl0 = FFIAFNest( weights=weights, dt=dt, bias=bias, tau_mem=tau_mem, v_reset=vReset, v_rest=vRest, v_thresh=vTh, capacity=fC, refractory=refractory, record=True, name="FF", ) weights_in = [ [0.015, 0.015, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], ] weights_rec = [ [0.0, 0.0, 0.015, 0.015], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0014], ] delay_in = [ [0.001, 0.011, 0.001, 0.001], [0.001, 0.001, 0.001, 0.001], [0.001, 0.001, 0.001, 0.001], [0.001, 0.001, 0.001, 0.001], ] delay_rec = [ [0.001, 0.001, 0.001, 0.011], [0.001, 0.001, 0.001, 0.001], [0.001, 0.001, 0.001, 0.001], [0.001, 0.001, 0.001, 0.001], ] vfBiasRec = 0.0 vtTauNRec = [0.2, 0.2, 0.2, 0.2] tau_syn_exc_rec = [0.2, 0.2, 0.2, 0.2] tau_syn_inh_rec = tau_syn_exc_rec # - Layer generation fl1 = RecIAFSpkInNest( weights_in=weights_in, weights_rec=weights_rec, delay_in=delay_in, delay_rec=delay_rec, dt=0.001, bias=vfBiasRec, tau_mem=vtTauNRec, capacity=0.1, tau_syn_exc=tau_syn_exc_rec, tau_syn_inh=tau_syn_inh_rec, refractory=0.001, record=True, name="Rec", ) net = nw.Network(fl0, fl1) # - Input signal vTime = np.arange(0, 1, dt) vVal = np.zeros([len(vTime), 1]) vVal[500] = 0.01 tsInCont = ts.TSContinuous(vTime, vVal) dAct = net.evolve(tsInCont, duration=1.0) times = dAct["Rec"].times eps = 0.000001 assert times[1] - times[0] - 0.01 < eps assert times[3] - times[2] - 0.01 < eps fl0.terminate() fl1.terminate()
def test_timeconstants(): """ test delays """ from rockpool import timeseries as ts from rockpool.layers import FFIAFNest, RecIAFSpkInNest from rockpool.networks import network as nw import numpy as np weights = [[0.001]] bias = 0.375 tau_mem = 0.01 vReset = -0.07 vRest = -0.07 vTh = -0.055 fC = 0.25 dt = 0.001 refractory = 0.002 fl0 = FFIAFNest( weights=weights, dt=dt, bias=bias, tau_mem=tau_mem, v_reset=vReset, v_rest=vRest, v_thresh=vTh, capacity=fC, refractory=refractory, record=True, name="FF", ) weights_in = [[0.001, -0.001]] weights_rec = [[0, 0], [0, 0]] vfBiasRec = 0.0 vtTauNRec = [0.2, 0.2] tau_syn_exc_rec = [0.1, 0.1] tau_syn_inh_rec = [0.01, 0.01] # - Layer generation fl1 = RecIAFSpkInNest( weights_in=weights_in, weights_rec=weights_rec, dt=0.001, bias=vfBiasRec, tau_mem=vtTauNRec, tau_syn_exc=tau_syn_exc_rec, tau_syn_inh=tau_syn_inh_rec, refractory=0.001, v_reset=vReset, v_rest=vRest, v_thresh=vTh, record=True, name="Rec", ) net = nw.Network(fl0, fl1) # - Input signal vTime = np.arange(0, 1, dt) vVal = np.zeros([len(vTime), 1]) vVal[500] = 0.01 tsInCont = ts.TSContinuous(vTime, vVal) dAct = net.evolve(tsInCont, duration=1.0) exc_input = np.abs(fl1.recorded_states.samples[:, 0] - vRest) inh_input = np.abs(fl1.recorded_states.samples[:, 1] - vRest) # excitatory input peak should be later than inhibitory as the synaptic TC is longer assert np.argmax(exc_input) > np.argmax(inh_input) fl0.terminate() fl1.terminate()
def test_DefaultParams(): """ Test RecIAFNest""" from rockpool.layers import RecIAFSpkInNest, FFIAFNest from rockpool import timeseries as ts from rockpool.networks import network as nw import numpy as np # - Generic parameters weights = np.ones([1, 2]) * 0.01 weights_in = np.array([[-0.1, 0.02, 0.4], [0.2, -0.3, -0.15]]) weights_rec = np.random.rand(3, 3) * 0.01 bias = 0.0 dt = 0.0001 tau_mem = 0.02 tau_syn_exc = 0.05 tau_syn_inh = 0.05 v_thresh = -0.055 v_reset = -0.065 v_rest = -0.065 capacity = tau_mem refractory = 0.001 fl0 = FFIAFNest( weights=weights, dt=dt, bias=bias, tau_mem=tau_mem, v_reset=v_reset, v_rest=v_rest, v_thresh=v_thresh, capacity=capacity, refractory=refractory, record=True, name="FF", ) fl1 = RecIAFSpkInNest( weights_in=weights_in, weights_rec=weights_rec, dt=dt, bias=bias, tau_mem=tau_mem, tau_syn_exc=tau_syn_exc, tau_syn_inh=tau_syn_inh, v_thresh=v_thresh, v_reset=v_reset, v_rest=v_rest, capacity=capacity, refractory=refractory, record=True, name="Rec", ) net0 = nw.Network(fl0, fl1) fl2 = FFIAFNest(weights=weights, record=True, name="FF") fl3 = RecIAFSpkInNest( weights_in=weights_in, weights_rec=weights_rec, record=True, name="Rec" ) net1 = nw.Network(fl2, fl3) # - Input signal vTime = np.arange(0, 1, dt) vVal = np.zeros([len(vTime), 1]) vVal[2000:7000] = 0.01 tsInCont = ts.TSContinuous(vTime, vVal) eps = 1e-6 assert (np.abs(fl0.state - fl2.state) < eps).all() assert (np.abs(fl1.state - fl3.state) < eps).all() # - Compare states before and after dAct0 = net0.evolve(tsInCont, duration=1.0) dAct1 = net1.evolve(tsInCont, duration=1.0) assert (np.abs(fl0.state - fl2.state) < eps).all() assert (np.abs(fl1.state - fl3.state) < eps).all() fl0.terminate() fl1.terminate()
def test_FFToRecLayerRepeat(): """ Test FFToRecNest""" from rockpool import timeseries as ts from rockpool.layers import FFIAFNest, RecIAFSpkInNest from rockpool.networks import network as nw import numpy as np weights = [[0.0, 0.001, 0.0]] bias = 0.375 tau_mem = 0.01 vReset = -0.07 vRest = -0.07 vTh = -0.055 fC = 0.25 dt = 0.001 refractory = 0.002 fl0 = FFIAFNest( weights=weights, dt=dt, bias=bias, tau_mem=tau_mem, v_reset=vReset, v_rest=vRest, v_thresh=vTh, capacity=fC, refractory=refractory, record=True, name="FF", ) weights_in = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.6], [0.0, 0.0, 0.0]] weights_rec = np.random.rand(3, 3) * 0.001 vfBiasRec = 0.0 vtTauNRec = [0.02, 0.05, 0.1] tau_syn_exc_rec = [0.2, 0.01, 0.01] tau_syn_inh_rec = tau_syn_exc_rec # - Layer generation fl1 = RecIAFSpkInNest( weights_in=weights_in, weights_rec=weights_rec, dt=0.001, bias=vfBiasRec, tau_mem=vtTauNRec, tau_syn_exc=tau_syn_exc_rec, tau_syn_inh=tau_syn_inh_rec, refractory=0.001, record=True, name="Rec", ) net = nw.Network(fl0, fl1) # - Input signal vTime = np.arange(0, 1, dt) vVal = np.zeros([len(vTime), 1]) vVal[500] = 0.01 tsInCont = ts.TSContinuous(vTime, vVal) # - Compare states before and after vStateBefore = np.copy(fl1.state) for _ in range(10): dAct = net.evolve(tsInCont, duration=1.0 / 10) assert fl0.t == 1.0 assert fl1.t == 1.0 assert (vStateBefore != fl1.state).any() net.reset_all() assert fl0.t == 0 assert fl1.t == 0 assert (vStateBefore == fl1.state).all() fl0.terminate()
def test_save_load(): """ Test RecIAFNest""" from rockpool.layers import RecIAFSpkInNest, FFIAFNest, FFExpSynTorch from rockpool import timeseries as ts from rockpool.networks import network as nw from rockpool.networks import Network as nws import numpy as np import pylab as plt # - Generic parameters weights = np.ones([1, 1]) * 0.01 weights_in = [[0.1, 0, 0]] weights_rec = [[0, 0.1, 0], [0, 0, 0.1], [0.0, 0, 0]] mfWOut = [[1], [1], [1]] bias = 0.0 dt = 0.001 tau_mem = 0.02 tau_syn = 0.05 v_thresh = -0.055 v_reset = -0.065 v_rest = -0.065 capacity = 100.0 refractory = 0.001 np.random.seed(0) fl0 = FFIAFNest( weights=weights, dt=dt, bias=bias, tau_mem=tau_mem, v_reset=v_reset, v_rest=v_rest, v_thresh=v_thresh, capacity=capacity, refractory=refractory, num_cores=1, record=True, name="FF", ) fl1 = RecIAFSpkInNest( weights_in=weights_in, weights_rec=weights_rec, dt=dt, bias=bias, tau_mem=tau_mem, tau_syn_exc=tau_syn, tau_syn_inh=tau_syn, v_thresh=v_thresh, v_reset=v_reset, v_rest=v_rest, capacity=capacity, refractory=refractory, num_cores=1, record=True, name="Rec", ) net0 = nw.Network(fl0, fl1) net0.save("test_nw.json") net1 = nws.load("test_nw.json") fl2 = net1.FF fl3 = net1.Rec np.random.seed(0) # - Input signal vTime = np.arange(0, 1, dt) vVal = np.zeros([len(vTime), 1]) vVal[200:201] = 0.25 tsInCont = ts.TSContinuous(vTime, vVal) epsilon = 0.0000001 # - Compare states before and after np.random.seed(0) dAct0 = net0.evolve(tsInCont, duration=1.0) np.random.seed(0) dAct1 = net1.evolve(tsInCont, duration=1.0) assert ( np.abs(fl0.recorded_states.samples - fl2.recorded_states.samples) < epsilon ).all() assert ( np.abs(fl1.recorded_states.samples - fl3.recorded_states.samples) < epsilon ).all()