If you want the function of the node to have a stateful value, you should use a class. Here’s a basic example of a counter that increments when above a threshold.
import nengo
import matplotlib.pyplot as plt
dt = 0.001
class Counter(object):
def __init__(self):
self.count = 0
def state_func(self, t, x):
if x > 0.8:
self.count += 1
return self.count
def in_func(t):
if ((t - dt) % 1) > 0.5:
return 1
else:
return 0
counter = Counter()
with nengo.Network() as model:
in_nd = nengo.Node(in_func, size_out=1)
state_nd = nengo.Node(counter.state_func, size_in=1, size_out=1)
nengo.Connection(in_nd, state_nd, synapse=None)
p_in = nengo.Probe(in_nd)
p_state = nengo.Probe(state_nd)
with nengo.Simulator(model) as sim:
sim.run(3.0)
plt.plot(sim.data[p_in])
plt.show()
plt.plot(sim.data[p_state])
plt.show()
To complete your use case, you’re going to need a bit more complex logic in the state_func
.