Beispiel #1
0
def test_SaveLoad():
    """ Test save and load RecAEIFNest """
    from rockpool.layers import RecIAFSpkInNest
    from rockpool.layers import RecAEIFSpkInNest
    from rockpool import timeseries as ts
    from rockpool.networks import network as nw
    import numpy as np

    # - Generic parameters
    weights_in = np.array([[-0.001, 0.002, 0.004], [0.03, -0.003, -0.0015]])
    weights_rec = np.random.rand(3, 3) * 0.001
    bias = 0.0
    tau_mem = [0.02, 0.05, 0.1]
    tau_syn_exc = [0.2, 0.01, 0.01]
    tau_syn_inh = tau_syn_exc
    dt = 0.001
    vThresh = -0.055
    vRest = -0.065

    # - Layer generation
    fl0 = RecAEIFSpkInNest(
        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=vThresh,
        v_reset=vRest,
        v_rest=vRest,
        subthresh_adapt=0.0,
        spike_adapt=0.001,
        delta_t=0.0,
        refractory=0.001,
        record=True,
        name="lyrNest",
    )

    net0 = nw.Network(fl0)
    net0.save("tmp.model")

    net1 = nw.Network.load("tmp.model")

    # - Input signal
    spikeTimes = np.arange(0, 1, dt)
    spikeTrain0 = np.sort(np.random.choice(spikeTimes, size=20, replace=False))
    channels = np.random.choice([0, 1], 20, replace=True).astype(int)

    tsInEvent = ts.TSEvent(spikeTrain0, channels)

    # - Compare states before and after

    assert (np.abs(net0.input_layer.state - net1.input_layer.state) < 0.00001).all()

    dFl0 = net0.evolve(tsInEvent, duration=1.0)
    dFl1 = net1.evolve(tsInEvent, duration=1.0)

    assert (np.abs(net0.input_layer.state - net1.input_layer.state) < 0.00001).all()

    fl0.terminate()
    net1.lyrNest.terminate()
Beispiel #2
0
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()
Beispiel #3
0
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()
Beispiel #4
0
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()
Beispiel #5
0
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()
Beispiel #6
0
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()