def _test_normalization(Simulator, sys, rng, normalizer, l1_lower, lower, radius=5.0, dt=0.0001, T=1.0): l1_norms = np.empty(len(sys)) for i, sub in enumerate(decompose_states(sys)): response = impulse(sub, dt=dt, length=int(T / dt)) assert np.allclose(response[-10:], 0) l1_norms[i] = radius * np.sum(abs(response * dt)) with Network() as model: stim = nengo.Node(output=lambda t: rng.choice([-radius, radius]) if t < T/2 else radius) tau = 0.02 subnet = LinearNetwork(sys, n_neurons=1, synapse=tau, dt=dt, input_synapse=tau, radii=radius, normalizer=normalizer, neuron_type=nengo.neurons.Direct()) nengo.Connection(stim, subnet.input, synapse=None) p = nengo.Probe(subnet.x.output, synapse=None) assert ((l1_lower*subnet.info['radii'] <= l1_norms) | ((l1_norms <= 1e-6) & (subnet.info['radii'] <= 1e-6))).all() assert (l1_norms <= subnet.info['radii'] + 1e-4).all() with Simulator(model, dt=dt) as sim: sim.run(T) # lower bound includes both approximation error and the gap between # random {-1, 1} flip-flop inputs and the true worst-case input worst_x = np.max(abs(sim.data[p]), axis=0) assert (lower <= worst_x+1e-6).all() assert (worst_x <= 1+1e-6).all()
def test_radii(Simulator, seed, plt): sys = canonical(PureDelay(0.1, order=4)) dt = 0.001 T = 0.3 plt.figure() # Precompute the exact bounds for an impulse stimulus radii = [] for sub in decompose_states(sys): response = impulse(sub, dt=dt, length=int(T / dt)) amplitude = np.max(abs(response)) assert amplitude >= 1e-6 # otherwise numerical issues radii.append(amplitude) plt.plot(response / amplitude, linestyle='--') with Network(seed=seed) as model: # Impulse stimulus stim = nengo.Node(output=lambda t: 1 / dt if t <= dt else 0) # Set explicit radii for controllable realization subnet = LinearNetwork(sys, n_neurons=1, synapse=0.2, input_synapse=0.2, dt=dt, radii=radii, normalizer=Controllable(), neuron_type=nengo.neurons.Direct()) nengo.Connection(stim, subnet.input, synapse=None) p = nengo.Probe(subnet.x.output, synapse=None) assert subnet.info == {} sim = nengo.Simulator(model, dt=dt) sim.run(T) plt.plot(sim.data[p], lw=5, alpha=0.5) assert np.allclose(np.max(abs(sim.data[p]), axis=0), 1, atol=1e-3)