def test_signaldict_reset(): """Tests SignalDict's reset function.""" signaldict = SignalDict() two_d = Signal([[1], [1]]) signaldict.init(two_d) two_d_view = two_d[0, :] signaldict[two_d_view] = -0.5 assert np.allclose(signaldict[two_d], np.array([[-0.5], [1]])) signaldict[two_d] = np.array([[-1], [-1]]) assert np.allclose(signaldict[two_d], np.array([[-1], [-1]])) assert np.allclose(signaldict[two_d_view], np.array([-1])) signaldict.reset(two_d_view) assert np.allclose(signaldict[two_d_view], np.array([1])) assert np.allclose(signaldict[two_d], np.array([[1], [-1]])) signaldict.reset(two_d) assert np.allclose(signaldict[two_d], np.array([[1], [1]]))
class Simulator(object): """Reference simulator for Nengo models.""" def __init__(self, network, dt=0.001, seed=None, model=None): """Initialize the simulator with a network and (optionally) a model. Most of the time, you will pass in a network and sometimes a dt:: sim1 = nengo.Simulator(my_network) # Uses default 0.001s dt sim2 = nengo.Simulator(my_network, dt=0.01) # Uses 0.01s dt For more advanced use cases, you can initialize the model yourself, and also pass in a network that will be built into the same model that you pass in:: sim = nengo.Simulator(my_network, model=my_model) If you want full control over the build process, then you can build your network into the model manually. If you do this, then you must explicitly pass in ``None`` for the network:: sim = nengo.Simulator(None, model=my_model) Parameters ---------- network : nengo.Network instance or None A network object to the built and then simulated. If a fully built ``model`` is passed in, then you can skip building the network by passing in network=None. dt : float, optional The length of a simulator timestep, in seconds. seed : int, optional A seed for all stochastic operators used in this simulator. model : nengo.builder.Model instance or None, optional A model object that contains build artifacts to be simulated. Usually the simulator will build this model for you; however, if you want to build the network manually, or to inject some build artifacts in the Model before building the network, then you can pass in a ``nengo.builder.Model`` instance. """ if model is None: dt = float(dt) # make sure it's a float (for division purposes) self.model = Model(dt=dt, label="%s, dt=%f" % (network, dt), decoder_cache=get_default_decoder_cache()) else: self.model = model if network is not None: # Build the network into the model self.model.build(network) self.model.decoder_cache.shrink() # -- map from Signal.base -> ndarray self.signals = SignalDict(__time__=np.asarray(0.0, dtype=np.float64)) for op in self.model.operators: op.init_signals(self.signals) # Order the steps (they are made in `Simulator.reset`) self.dg = operator_depencency_graph(self.model.operators) self._step_order = [op for op in toposort(self.dg) if hasattr(op, 'make_step')] # Add built states to the probe dictionary self._probe_outputs = self.model.params # Provide a nicer interface to probe outputs self.data = ProbeDict(self._probe_outputs) seed = np.random.randint(npext.maxint) if seed is None else seed self.reset(seed=seed) @property def dt(self): """The time step of the simulator""" return self.model.dt @dt.setter def dt(self, dummy): raise AttributeError("Cannot change simulator 'dt'. Please file " "an issue at http://github.com/nengo/nengo" "/issues and describe your use case.") @property def time(self): """The current time of the simulator""" return self.signals['__time__'].copy() def trange(self, dt=None): """Create a range of times matching probe data. Note that the range does not start at 0 as one might expect, but at the first timestep (i.e., dt). Parameters ---------- dt : float (optional) The sampling period of the probe to create a range for. If empty, will use the default probe sampling period. """ dt = self.dt if dt is None else dt n_steps = int(self.n_steps * (self.dt / dt)) return dt * np.arange(1, n_steps + 1) def _probe(self): """Copy all probed signals to buffers""" for probe in self.model.probes: period = (1 if probe.sample_every is None else probe.sample_every / self.dt) if self.n_steps % period < 1: tmp = self.signals[self.model.sig[probe]['in']].copy() self._probe_outputs[probe].append(tmp) def step(self): """Advance the simulator by `self.dt` seconds. """ self.n_steps += 1 self.signals['__time__'][...] = self.n_steps * self.dt old_err = np.seterr(invalid='raise', divide='ignore') try: for step_fn in self._steps: step_fn() finally: np.seterr(**old_err) self._probe() def run(self, time_in_seconds, progress_bar=True): """Simulate for the given length of time. Parameters ---------- steps : int Number of steps to run the simulation for. progress_bar : bool or ``ProgressBar`` or ``ProgressUpdater``, optional Progress bar for displaying the progress. By default, ``progress_bar=True``, which uses the default progress bar (text in most situations, or an HTML version in recent IPython notebooks). To disable the progress bar, use ``progress_bar=False``. For more control over the progress bar, pass in a :class:`nengo.utils.progress.ProgressBar`, or :class:`nengo.utils.progress.ProgressUpdater` instance. """ steps = int(np.round(float(time_in_seconds) / self.dt)) logger.debug("Running %s for %f seconds, or %d steps", self.model.label, time_in_seconds, steps) self.run_steps(steps, progress_bar=progress_bar) def run_steps(self, steps, progress_bar=True): """Simulate for the given number of `dt` steps. Parameters ---------- steps : int Number of steps to run the simulation for. progress_bar : bool or ``ProgressBar`` or ``ProgressUpdater``, optional Progress bar for displaying the progress. By default, ``progress_bar=True``, which uses the default progress bar (text in most situations, or an HTML version in recent IPython notebooks). To disable the progress bar, use ``progress_bar=False``. For more control over the progress bar, pass in a :class:`nengo.utils.progress.ProgressBar`, or :class:`nengo.utils.progress.ProgressUpdater` instance. """ with ProgressTracker(steps, progress_bar) as progress: for i in range(steps): self.step() progress.step() def reset(self, seed=None): """Reset the simulator state. Parameters ---------- seed : int, optional A seed for all stochastic operators used in the simulator. This will change the random sequences generated for noise or inputs (e.g. from Processes), but not the built objects (e.g. ensembles, connections). """ if seed is not None: self.seed = seed self.n_steps = 0 self.signals['__time__'][...] = 0 # reset signals for key in self.signals: if key != '__time__': self.signals.reset(key) # rebuild steps (resets ops with their own state, like Processes) self.rng = np.random.RandomState(self.seed) self._steps = [op.make_step(self.signals, self.dt, self.rng) for op in self._step_order] # clear probe data for probe in self.model.probes: self._probe_outputs[probe] = []
class Simulator(object): """Reference simulator for Nengo models.""" def __init__(self, network, dt=0.001, seed=None, model=None): """Initialize the simulator with a network and (optionally) a model. Most of the time, you will pass in a network and sometimes a dt:: sim1 = nengo.Simulator(my_network) # Uses default 0.001s dt sim2 = nengo.Simulator(my_network, dt=0.01) # Uses 0.01s dt For more advanced use cases, you can initialize the model yourself, and also pass in a network that will be built into the same model that you pass in:: sim = nengo.Simulator(my_network, model=my_model) If you want full control over the build process, then you can build your network into the model manually. If you do this, then you must explicitly pass in ``None`` for the network:: sim = nengo.Simulator(None, model=my_model) Parameters ---------- network : nengo.Network instance or None A network object to the built and then simulated. If a fully built ``model`` is passed in, then you can skip building the network by passing in network=None. dt : float The length of a simulator timestep, in seconds. seed : int A seed for all stochastic operators used in this simulator. Note that there are not stochastic operators implemented currently, so this parameters does nothing. model : nengo.builder.Model instance or None A model object that contains build artifacts to be simulated. Usually the simulator will build this model for you; however, if you want to build the network manually, or to inject some build artifacts in the Model before building the network, then you can pass in a ``nengo.builder.Model`` instance. """ dt = float(dt) # make sure it's a float (for division purposes) if model is None: self.model = Model(dt=dt, label="%s, dt=%f" % (network, dt), decoder_cache=get_default_decoder_cache()) else: self.model = model if network is not None: # Build the network into the model self.model.build(network) self.model.decoder_cache.shrink() self.seed = np.random.randint(npext.maxint) if seed is None else seed self.rng = np.random.RandomState(self.seed) # -- map from Signal.base -> ndarray self.signals = SignalDict(__time__=np.asarray(0.0, dtype=np.float64)) for op in self.model.operators: op.init_signals(self.signals) self.dg = operator_depencency_graph(self.model.operators) self._step_order = [node for node in toposort(self.dg) if hasattr(node, 'make_step')] self._steps = [node.make_step(self.signals, dt, self.rng) for node in self._step_order] # Add built states to the probe dictionary self._probe_outputs = self.model.params # Provide a nicer interface to probe outputs self.data = ProbeDict(self._probe_outputs) self.reset() @property def dt(self): """The time step of the simulator""" return self.model.dt @dt.setter def dt(self, dummy): raise AttributeError("Cannot change simulator 'dt'. Please file " "an issue at http://github.com/ctn-waterloo/nengo" "/issues and describe your use case.") @property def time(self): """The current time of the simulator""" return self.signals['__time__'].copy() def trange(self, dt=None): """Create a range of times matching probe data. Parameters ---------- dt : float (optional) The sampling period of the probe to create a range for. If empty, will use the default probe sampling period. """ dt = self.dt if dt is None else dt n_steps = int(self.n_steps * (self.dt / dt)) return dt * np.arange(1, n_steps + 1) def _probe(self): """Copy all probed signals to buffers""" for probe in self.model.probes: period = (1 if probe.sample_every is None else probe.sample_every / self.dt) if self.n_steps % period < 1: tmp = self.signals[self.model.sig[probe]['in']].copy() self._probe_outputs[probe].append(tmp) def step(self): """Advance the simulator by `self.dt` seconds. """ self.n_steps += 1 self.signals['__time__'][...] = self.n_steps * self.dt old_err = np.seterr(invalid='raise', divide='ignore') try: for step_fn in self._steps: step_fn() finally: np.seterr(**old_err) self._probe() def run(self, time_in_seconds): """Simulate for the given length of time.""" steps = int(np.round(float(time_in_seconds) / self.dt)) logger.debug("Running %s for %f seconds, or %d steps", self.model.label, time_in_seconds, steps) self.run_steps(steps) def run_steps(self, steps): """Simulate for the given number of `dt` steps.""" for i in range(steps): if i % 1000 == 0: logger.debug("Step %d", i) self.step() def reset(self): """Reset the simulator state.""" self.n_steps = 0 self.signals['__time__'][...] = 0 for key in self.signals: if key != '__time__': self.signals.reset(key) for probe in self.model.probes: self._probe_outputs[probe] = []
class Simulator(object): """Reference simulator for Nengo models. The simulator takes a `.Network` and builds internal data structures to run the model defined by that network. Run the simulator with the `~.Simulator.run` method, and access probed data through the ``data`` attribute. Building and running the simulation may allocate resources like files and sockets. To properly free these resources, call the `.Simulator.close` method. Alternatively, `.Simulator.close` will automatically be called if you use the ``with`` syntax:: with nengo.Simulator(my_network) as sim: sim.run(0.1) print(sim.data[my_probe]) Note that the ``data`` attribute is still accessible even when a simulator has been closed. Running the simulator, however, will raise an error. Parameters ---------- network : Network or None A network object to be built and then simulated. If None, then a `.Model` with the build model must be provided instead. dt : float, optional (Default: 0.001) The length of a simulator timestep, in seconds. seed : int, optional (Default: None) A seed for all stochastic operators used in this simulator. model : Model, optional (Default: None) A `.Model` that contains build artifacts to be simulated. Usually the simulator will build this model for you; however, if you want to build the network manually, or you want to inject build artifacts in the model before building the network, then you can pass in a `.Model` instance. Attributes ---------- closed : bool Whether the simulator has been closed. Once closed, it cannot be reopened. data : ProbeDict The `.ProbeDict` mapping from Nengo objects to the data associated with those objects. In particular, each `.Probe` maps to the data probed while running the simulation. dg : dict A dependency graph mapping from each `.Operator` to the operators that depend on that operator. model : Model The `.Model` containing the signals and operators necessary to simulate the network. signals : SignalDict The `.SignalDict` mapping from `.Signal` instances to NumPy arrays. """ # 'unsupported' defines features unsupported by a simulator. # The format is a list of tuples of the form `(test, reason)` with `test` # being a string with wildcards (*, ?, [abc], [!abc]) matched against Nengo # test paths and names, and `reason` is a string describing why the feature # is not supported by the backend. For example: # unsupported = [('test_pes*', 'PES rule not implemented')] # would skip all test whose names start with 'test_pes'. unsupported = [] def __init__(self, network, dt=0.001, seed=None, model=None): self.closed = False if model is None: dt = float(dt) # make sure it's a float (for division purposes) self.model = Model(dt=dt, label="%s, dt=%f" % (network, dt), decoder_cache=get_default_decoder_cache()) else: self.model = model if network is not None: # Build the network into the model self.model.build(network) self.model.decoder_cache.shrink() # -- map from Signal.base -> ndarray self.signals = SignalDict() for op in self.model.operators: op.init_signals(self.signals) # Order the steps (they are made in `Simulator.reset`) self.dg = operator_depencency_graph(self.model.operators) self._step_order = [op for op in toposort(self.dg) if hasattr(op, 'make_step')] # Add built states to the probe dictionary self._probe_outputs = self.model.params # Provide a nicer interface to probe outputs self.data = ProbeDict(self._probe_outputs) seed = np.random.randint(npext.maxint) if seed is None else seed self.reset(seed=seed) def __del__(self): """Raise a ResourceWarning if we are deallocated while open.""" if not self.closed: warnings.warn( "Simulator with model=%s was deallocated while open. Please " "close simulators manually to ensure resources are properly " "freed." % self.model, ResourceWarning) def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): self.close() @property def dt(self): """(float) The time step of the simulator.""" return self.model.dt @dt.setter def dt(self, dummy): raise ReadonlyError(attr='dt', obj=self) @property def n_steps(self): """(int) The current time step of the simulator.""" return self._n_steps @property def time(self): """(float) The current time of the simulator.""" return self._time def close(self): """Closes the simulator. Any call to `.Simulator.run`, `.Simulator.run_steps`, `.Simulator.step`, and `.Simulator.reset` on a closed simulator raises a `.SimulatorClosed` exception. """ self.closed = True self.signals = None # signals may no longer exist on some backends def _probe(self): """Copy all probed signals to buffers.""" self._probe_step_time() for probe in self.model.probes: period = (1 if probe.sample_every is None else probe.sample_every / self.dt) if self.n_steps % period < 1: tmp = self.signals[self.model.sig[probe]['in']].copy() self._probe_outputs[probe].append(tmp) def _probe_step_time(self): self._n_steps = self.signals[self.model.step].copy() self._time = self.signals[self.model.time].copy() def reset(self, seed=None): """Reset the simulator state. Parameters ---------- seed : int, optional A seed for all stochastic operators used in the simulator. This will change the random sequences generated for noise or inputs (e.g. from processes), but not the built objects (e.g. ensembles, connections). """ if self.closed: raise SimulatorClosed("Cannot reset closed Simulator.") if seed is not None: self.seed = seed # reset signals for key in self.signals: self.signals.reset(key) # rebuild steps (resets ops with their own state, like Processes) self.rng = np.random.RandomState(self.seed) self._steps = [op.make_step(self.signals, self.dt, self.rng) for op in self._step_order] # clear probe data for probe in self.model.probes: self._probe_outputs[probe] = [] self._probe_step_time() def run(self, time_in_seconds, progress_bar=True): """Simulate for the given length of time. Parameters ---------- time_in_seconds : float Amount of time to run the simulation for. progress_bar : bool or `.ProgressBar` or `.ProgressUpdater`, optional \ (Default: True) Progress bar for displaying the progress of the simulation run. If True, the default progress bar will be used. If False, the progress bar will be disabled. For more control over the progress bar, pass in a `.ProgressBar` or `.ProgressUpdater` instance. """ steps = int(np.round(float(time_in_seconds) / self.dt)) logger.info("Running %s for %f seconds, or %d steps", self.model.label, time_in_seconds, steps) self.run_steps(steps, progress_bar=progress_bar) def run_steps(self, steps, progress_bar=True): """Simulate for the given number of ``dt`` steps. Parameters ---------- steps : int Number of steps to run the simulation for. progress_bar : bool or `.ProgressBar` or `.ProgressUpdater`, optional \ (Default: True) Progress bar for displaying the progress of the simulation run. If True, the default progress bar will be used. If False, the progress bar will be disabled. For more control over the progress bar, pass in a `.ProgressBar` or `.ProgressUpdater` instance. """ with ProgressTracker(steps, progress_bar) as progress: for i in range(steps): self.step() progress.step() def step(self): """Advance the simulator by 1 step (``dt`` seconds).""" if self.closed: raise SimulatorClosed("Simulator cannot run because it is closed.") old_err = np.seterr(invalid='raise', divide='ignore') try: for step_fn in self._steps: step_fn() finally: np.seterr(**old_err) self._probe() def trange(self, dt=None): """Create a vector of times matching probed data. Note that the range does not start at 0 as one might expect, but at the first timestep (i.e., ``dt``). Parameters ---------- dt : float, optional (Default: None) The sampling period of the probe to create a range for. If None, the simulator's ``dt`` will be used. """ dt = self.dt if dt is None else dt n_steps = int(self.n_steps * (self.dt / dt)) return dt * np.arange(1, n_steps + 1)
class Simulator: r"""Reference simulator for Nengo models. The simulator takes a `.Network` and builds internal data structures to run the model defined by that network. Run the simulator with the `~.Simulator.run` method, and access probed data through the ``data`` attribute. Building and running the simulation may allocate resources like files and sockets. To properly free these resources, call the `.Simulator.close` method. Alternatively, `.Simulator.close` will automatically be called if you use the ``with`` syntax: .. testcode:: with nengo.Network() as net: ensemble = nengo.Ensemble(10, 1) with nengo.Simulator(net, progress_bar=False) as sim: sim.run(0.1) Note that the ``data`` attribute is still accessible even when a simulator has been closed. Running the simulator, however, will raise an error. When debugging or comparing models, it can be helpful to see the full ordered list of operators that the simulator will execute on each timestep. .. testcode:: with nengo.Simulator(nengo.Network(), progress_bar=False) as sim: print('\n'.join(f"* {op}" for op in sim.step_order)) .. testoutput:: * TimeUpdate{} The diff of two simulators' sorted ops tells us how two built models differ. We can use ``difflib`` in the Python standard library to see the differences. .. testcode:: # Original model with nengo.Network() as net: ensemble = nengo.Ensemble(10, 1, label="Ensemble") sim1 = nengo.Simulator(net, progress_bar=False) # Add a node with net: node = nengo.Node(output=0, label="Node") nengo.Connection(node, ensemble) sim2 = nengo.Simulator(net, progress_bar=False) import difflib print("".join(difflib.unified_diff( sorted(f"{type(op).__name__}: {op.tag}\n" for op in sim1.step_order), sorted(f"{type(op).__name__}: {op.tag}\n" for op in sim2.step_order), fromfile="sim1", tofile="sim2", n=0, )).strip()) sim1.close() sim2.close() .. testoutput:: :options: --- sim1 +++ sim2 @@ -0,0 +1 @@ +Copy: <Connection from <Node 'Node'> to <Ensemble 'Ensemble'>> @@ -4,0 +6 @@ +SimProcess: Lowpass(tau=0.005) Parameters ---------- network : Network or None A network object to be built and then simulated. If None, then a `.Model` with the build model must be provided instead. dt : float, optional The length of a simulator timestep, in seconds. seed : int, optional A seed for all stochastic operators used in this simulator. Will be set to ``network.seed + 1`` if not given. model : Model, optional A `.Model` that contains build artifacts to be simulated. Usually the simulator will build this model for you; however, if you want to build the network manually, or you want to inject build artifacts in the model before building the network, then you can pass in a `.Model` instance. progress_bar : bool or ProgressBar, optional Progress bar for displaying build and simulation progress. If ``True``, the default progress bar will be used. If ``False``, the progress bar will be disabled. For more control over the progress bar, pass in a ``ProgressBar`` instance. optimize : bool, optional If ``True``, the builder will run an additional optimization step that can speed up simulations significantly at the cost of slower builds. If running models for very small amounts of time, pass ``False`` to disable the optimizer. Attributes ---------- closed : bool Whether the simulator has been closed. Once closed, it cannot be reopened. data : SimulationData The `.SimulationData` mapping from Nengo objects to the data associated with those objects. In particular, each `.Probe` maps to the data probed while running the simulation. dg : dict A dependency graph mapping from each `.Operator` to the operators that depend on that operator. model : Model The `.Model` containing the signals and operators necessary to simulate the network. signals : SignalDict The `.SignalDict` mapping from `.Signal` instances to NumPy arrays. """ def __init__(self, network, dt=0.001, seed=None, model=None, progress_bar=True, optimize=True): self.closed = True # Start closed in case constructor raises exception self.progress_bar = progress_bar self.optimize = optimize if model is None: self.model = Model( dt=float(dt), label=f"{network}, dt={dt:f}", decoder_cache=get_default_decoder_cache(), ) else: self.model = model pt = ProgressTracker(progress_bar, Progress("Building", "Build")) with pt: if network is not None: # Build the network into the model self.model.build(network, progress=pt.next_stage("Building", "Build")) # Order the steps (they are made in `Simulator.reset`) self.dg = operator_dependency_graph(self.model.operators) if optimize: with pt.next_stage("Building (running optimizer)", "Optimization"): opmerge_optimize(self.model, self.dg) self._step_order = [ op for op in toposort(self.dg) if hasattr(op, "make_step") ] # -- map from Signal.base -> ndarray self.signals = SignalDict() for op in self.model.operators: op.init_signals(self.signals) # Add built states to the raw simulation data dictionary self._sim_data = self.model.params # Provide a nicer interface to simulation data self.data = SimulationData(self._sim_data) if seed is None: if network is not None and network.seed is not None: seed = network.seed + 1 else: seed = np.random.randint(npext.maxint) self.closed = False self.reset(seed=seed) def __del__(self): """Raise a ResourceWarning if we are deallocated while open.""" if not self.closed: warnings.warn( f"Simulator with model={self.model} was deallocated while open. Please " "close simulators manually to ensure resources are properly " "freed.", ResourceWarning, ) def __enter__(self): if self.closed: raise SimulatorClosed("Cannot re-open after simulator is closed") return self def __exit__(self, exc_type, exc_value, traceback): self.close() def __getstate__(self): signals = ({k: v for k, v in self.signals.items() if not k.readonly} if self.signals is not None else {}) probe_outputs = { probe: self._sim_data[probe] for probe in self.model.probes } return dict( model=self.model, signals=signals, probe_outputs=probe_outputs, dt=self.dt, seed=self.seed, progress_bar=self.progress_bar, optimize=self.optimize, closed=self.closed, ) def __setstate__(self, state): self.__init__( network=None, model=state["model"], dt=state["dt"], seed=state["seed"], progress_bar=state["progress_bar"], optimize=False, # The pickled Sim will have already been optimized ) for key, value in state["signals"].items(): self.signals[key] = value for key, value in state["probe_outputs"].items(): self._sim_data[key].extend(value) # Set whether it had originally been optimized self.optimize = state["optimize"] if state["closed"]: self.close() @property def dt(self): """(float) The time step of the simulator.""" return self.model.dt @dt.setter def dt(self, dummy): raise ReadonlyError(attr="dt", obj=self) @property def n_steps(self): """(int) The current time step of the simulator.""" return self._n_steps @property def step_order(self): """(list) The ordered list of step functions run by this simulator.""" return self._step_order @property def time(self): """(float) The current time of the simulator.""" return self._time def clear_probes(self): """Clear all probe histories. .. versionadded:: 3.0.0 """ for probe in self.model.probes: self._sim_data[probe] = [] self.data.reset() # clear probe cache def close(self): """Closes the simulator. Any call to `.Simulator.run`, `.Simulator.run_steps`, `.Simulator.step`, and `.Simulator.reset` on a closed simulator raises a `.SimulatorClosed` exception. """ self.closed = True self.signals = None # signals may no longer exist on some backends def _probe(self): """Copy all probed signals to buffers.""" self._probe_step_time() for probe in self.model.probes: period = 1 if probe.sample_every is None else probe.sample_every / self.dt if self.n_steps % period < 1: tmp = self.signals[self.model.sig[probe]["in"]].copy() self._sim_data[probe].append(tmp) def _probe_step_time(self): self._n_steps = self.signals[self.model.step].item() self._time = self.signals[self.model.time].item() def reset(self, seed=None): """Reset the simulator state. Parameters ---------- seed : int, optional A seed for all stochastic operators used in the simulator. This will change the random sequences generated for noise or inputs (e.g. from processes), but not the built objects (e.g. ensembles, connections). """ if self.closed: raise SimulatorClosed("Cannot reset closed Simulator.") if seed is not None: self.seed = seed # reset signals for key in self.signals: self.signals.reset(key) # rebuild steps (resets ops with their own state, like Processes) self.rng = np.random.RandomState(self.seed) self._steps = [ op.make_step(self.signals, self.dt, self.rng) for op in self._step_order ] self.clear_probes() self._probe_step_time() def run(self, time_in_seconds, progress_bar=None): """Simulate for the given length of time. If the given length of time is not a multiple of ``dt``, it will be rounded to the nearest ``dt``. For example, if ``dt`` is 0.001 and ``run`` is called with ``time_in_seconds=0.0006``, the simulator will advance one timestep, resulting in the actual simulator time being 0.001. The given length of time must be positive. The simulator cannot be run backwards. Parameters ---------- time_in_seconds : float Amount of time to run the simulation for. Must be positive. progress_bar : bool or ProgressBar, optional Progress bar for displaying the progress of the simulation run. If True, the default progress bar will be used. If False, the progress bar will be disabled. For more control over the progress bar, pass in a ``ProgressBar`` instance. """ if time_in_seconds < 0: raise ValidationError( f"Must be positive (got {time_in_seconds:g})", attr="time_in_seconds") steps = int(np.round(float(time_in_seconds) / self.dt)) if steps == 0: warnings.warn( f"{time_in_seconds} results in running for 0 timesteps. Simulator " f"still at time {self.time}.") else: logger.info( "Running %s for %f seconds, or %d steps", self.model.label, time_in_seconds, steps, ) self.run_steps(steps, progress_bar=progress_bar) def run_steps(self, steps, progress_bar=None): """Simulate for the given number of ``dt`` steps. Parameters ---------- steps : int Number of steps to run the simulation for. progress_bar : bool or ProgressBar, optional Progress bar for displaying the progress of the simulation run. If True, the default progress bar will be used. If False, the progress bar will be disabled. For more control over the progress bar, pass in a ``ProgressBar`` instance. """ if progress_bar is None: progress_bar = self.progress_bar with ProgressTracker(progress_bar, Progress("Simulating", "Simulation", steps)) as pt: for _ in range(steps): self.step() pt.total_progress.step() def step(self): """Advance the simulator by 1 step (``dt`` seconds).""" if self.closed: raise SimulatorClosed("Simulator cannot run because it is closed.") old_err = np.seterr(invalid="raise", divide="ignore") try: for step_fn in self._steps: step_fn() finally: np.seterr(**old_err) self._probe() def trange(self, dt=None, sample_every=None): """Create a vector of times matching probed data. Note that the range does not start at 0 as one might expect, but at the first timestep (i.e., ``dt``). Parameters ---------- sample_every : float, optional The sampling period of the probe to create a range for. If None, a time value for every ``dt`` will be produced. .. versionchanged:: 3.0.0 Renamed from dt to sample_every """ if dt is not None: if sample_every is not None: raise ValidationError( "Cannot specify both `dt` and `sample_every`. " "Use `sample_every` only.", attr="dt", obj=self, ) warnings.warn("`dt` is deprecated. Use `sample_every` instead.", DeprecationWarning) sample_every = dt period = 1 if sample_every is None else sample_every / self.dt steps = np.arange(1, self.n_steps + 1) return self.dt * steps[steps % period < 1]
class Simulator(object): """Reference simulator for Nengo models. The simulator takes a `.Network` and builds internal data structures to run the model defined by that network. Run the simulator with the `~.Simulator.run` method, and access probed data through the ``data`` attribute. Building and running the simulation may allocate resources like files and sockets. To properly free these resources, call the `.Simulator.close` method. Alternatively, `.Simulator.close` will automatically be called if you use the ``with`` syntax:: with nengo.Simulator(my_network) as sim: sim.run(0.1) print(sim.data[my_probe]) Note that the ``data`` attribute is still accessible even when a simulator has been closed. Running the simulator, however, will raise an error. Parameters ---------- network : Network or None A network object to be built and then simulated. If None, then a `.Model` with the build model must be provided instead. dt : float, optional (Default: 0.001) The length of a simulator timestep, in seconds. seed : int, optional (Default: None) A seed for all stochastic operators used in this simulator. model : Model, optional (Default: None) A `.Model` that contains build artifacts to be simulated. Usually the simulator will build this model for you; however, if you want to build the network manually, or you want to inject build artifacts in the model before building the network, then you can pass in a `.Model` instance. Attributes ---------- closed : bool Whether the simulator has been closed. Once closed, it cannot be reopened. data : ProbeDict The `.ProbeDict` mapping from Nengo objects to the data associated with those objects. In particular, each `.Probe` maps to the data probed while running the simulation. dg : dict A dependency graph mapping from each `.Operator` to the operators that depend on that operator. model : Model The `.Model` containing the signals and operators necessary to simulate the network. signals : SignalDict The `.SignalDict` mapping from `.Signal` instances to NumPy arrays. """ # 'unsupported' defines features unsupported by a simulator. # The format is a list of tuples of the form `(test, reason)` with `test` # being a string with wildcards (*, ?, [abc], [!abc]) matched against Nengo # test paths and names, and `reason` is a string describing why the feature # is not supported by the backend. For example: # unsupported = [('test_pes*', 'PES rule not implemented')] # would skip all test whose names start with 'test_pes'. unsupported = [] def __init__(self, network, dt=0.001, seed=None, model=None): self.closed = False if model is None: dt = float(dt) # make sure it's a float (for division purposes) self.model = Model(dt=dt, label="%s, dt=%f" % (network, dt), decoder_cache=get_default_decoder_cache()) else: self.model = model if network is not None: # Build the network into the model self.model.build(network) self.model.decoder_cache.shrink() # -- map from Signal.base -> ndarray self.signals = SignalDict() for op in self.model.operators: op.init_signals(self.signals) # Order the steps (they are made in `Simulator.reset`) self.dg = operator_depencency_graph(self.model.operators) self._step_order = [ op for op in toposort(self.dg) if hasattr(op, 'make_step') ] # Add built states to the probe dictionary self._probe_outputs = self.model.params # Provide a nicer interface to probe outputs self.data = ProbeDict(self._probe_outputs) seed = np.random.randint(npext.maxint) if seed is None else seed self.reset(seed=seed) def __del__(self): """Raise a ResourceWarning if we are deallocated while open.""" if not self.closed: warnings.warn( "Simulator with model=%s was deallocated while open. Please " "close simulators manually to ensure resources are properly " "freed." % self.model, ResourceWarning) def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): self.close() @property def dt(self): """(float) The time step of the simulator.""" return self.model.dt @dt.setter def dt(self, dummy): raise ReadonlyError(attr='dt', obj=self) @property def n_steps(self): """(int) The current time step of the simulator.""" return self._n_steps @property def time(self): """(float) The current time of the simulator.""" return self._time def close(self): """Closes the simulator. Any call to `.Simulator.run`, `.Simulator.run_steps`, `.Simulator.step`, and `.Simulator.reset` on a closed simulator raises a `.SimulatorClosed` exception. """ self.closed = True self.signals = None # signals may no longer exist on some backends def _probe(self): """Copy all probed signals to buffers.""" self._probe_step_time() for probe in self.model.probes: period = (1 if probe.sample_every is None else probe.sample_every / self.dt) if self.n_steps % period < 1: tmp = self.signals[self.model.sig[probe]['in']].copy() self._probe_outputs[probe].append(tmp) def _probe_step_time(self): self._n_steps = self.signals[self.model.step].copy() self._time = self.signals[self.model.time].copy() def reset(self, seed=None): """Reset the simulator state. Parameters ---------- seed : int, optional A seed for all stochastic operators used in the simulator. This will change the random sequences generated for noise or inputs (e.g. from processes), but not the built objects (e.g. ensembles, connections). """ if self.closed: raise SimulatorClosed("Cannot reset closed Simulator.") if seed is not None: self.seed = seed # reset signals for key in self.signals: self.signals.reset(key) # rebuild steps (resets ops with their own state, like Processes) self.rng = np.random.RandomState(self.seed) self._steps = [ op.make_step(self.signals, self.dt, self.rng) for op in self._step_order ] # clear probe data for probe in self.model.probes: self._probe_outputs[probe] = [] self._probe_step_time() def run(self, time_in_seconds, progress_bar=True): """Simulate for the given length of time. Parameters ---------- time_in_seconds : float Amount of time to run the simulation for. progress_bar : bool or `.ProgressBar` or `.ProgressUpdater`, optional \ (Default: True) Progress bar for displaying the progress of the simulation run. If True, the default progress bar will be used. If False, the progress bar will be disabled. For more control over the progress bar, pass in a `.ProgressBar` or `.ProgressUpdater` instance. """ steps = int(np.round(float(time_in_seconds) / self.dt)) logger.info("Running %s for %f seconds, or %d steps", self.model.label, time_in_seconds, steps) self.run_steps(steps, progress_bar=progress_bar) def run_steps(self, steps, progress_bar=True): """Simulate for the given number of ``dt`` steps. Parameters ---------- steps : int Number of steps to run the simulation for. progress_bar : bool or `.ProgressBar` or `.ProgressUpdater`, optional \ (Default: True) Progress bar for displaying the progress of the simulation run. If True, the default progress bar will be used. If False, the progress bar will be disabled. For more control over the progress bar, pass in a `.ProgressBar` or `.ProgressUpdater` instance. """ with ProgressTracker(steps, progress_bar) as progress: for i in range(steps): self.step() progress.step() def step(self): """Advance the simulator by 1 step (``dt`` seconds).""" if self.closed: raise SimulatorClosed("Simulator cannot run because it is closed.") old_err = np.seterr(invalid='raise', divide='ignore') try: for step_fn in self._steps: step_fn() finally: np.seterr(**old_err) self._probe() def trange(self, dt=None): """Create a vector of times matching probed data. Note that the range does not start at 0 as one might expect, but at the first timestep (i.e., ``dt``). Parameters ---------- dt : float, optional (Default: None) The sampling period of the probe to create a range for. If None, the simulator's ``dt`` will be used. """ dt = self.dt if dt is None else dt n_steps = int(self.n_steps * (self.dt / dt)) return dt * np.arange(1, n_steps + 1)
class Simulator(object): """Reference simulator for Nengo models.""" # 'unsupported' defines features unsupported by a simulator. # The format is a list of tuples of the form `(test, reason)` with `test` # being a string with wildcards (*, ?, [abc], [!abc]) matched against Nengo # test paths and names, and `reason` is a string describing why the feature # is not supported by the backend. For example: # unsupported = [('test_pes*', 'PES rule not implemented')] # would skip all test whose names start with 'test_pes'. unsupported = [] def __init__(self, network, dt=0.001, seed=None, model=None): """Initialize the simulator with a network and (optionally) a model. Most of the time, you will pass in a network and sometimes a dt:: sim1 = nengo.Simulator(my_network) # Uses default 0.001s dt sim2 = nengo.Simulator(my_network, dt=0.01) # Uses 0.01s dt For more advanced use cases, you can initialize the model yourself, and also pass in a network that will be built into the same model that you pass in:: sim = nengo.Simulator(my_network, model=my_model) If you want full control over the build process, then you can build your network into the model manually. If you do this, then you must explicitly pass in ``None`` for the network:: sim = nengo.Simulator(None, model=my_model) Parameters ---------- network : nengo.Network instance or None A network object to the built and then simulated. If a fully built ``model`` is passed in, then you can skip building the network by passing in network=None. dt : float, optional The length of a simulator timestep, in seconds. seed : int, optional A seed for all stochastic operators used in this simulator. model : nengo.builder.Model instance or None, optional A model object that contains build artifacts to be simulated. Usually the simulator will build this model for you; however, if you want to build the network manually, or to inject some build artifacts in the Model before building the network, then you can pass in a ``nengo.builder.Model`` instance. """ self.closed = False if model is None: dt = float(dt) # make sure it's a float (for division purposes) self.model = Model(dt=dt, label="%s, dt=%f" % (network, dt), decoder_cache=get_default_decoder_cache()) else: self.model = model if network is not None: # Build the network into the model self.model.build(network) self.model.decoder_cache.shrink() # -- map from Signal.base -> ndarray self.signals = SignalDict() for op in self.model.operators: op.init_signals(self.signals) # Order the steps (they are made in `Simulator.reset`) self.dg = operator_depencency_graph(self.model.operators) self._step_order = [op for op in toposort(self.dg) if hasattr(op, 'make_step')] # Add built states to the probe dictionary self._probe_outputs = self.model.params # Provide a nicer interface to probe outputs self.data = ProbeDict(self._probe_outputs) seed = np.random.randint(npext.maxint) if seed is None else seed self.reset(seed=seed) def __del__(self): """Raise a ResourceWarning if we are deallocated while open.""" if not self.closed: warnings.warn( "Simulator with model=%s was deallocated while open. Please " "close simulators manually to ensure resources are properly " "freed." % self.model, ResourceWarning) def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): self.close() @property def dt(self): """The time step of the simulator""" return self.model.dt @dt.setter def dt(self, dummy): raise ReadonlyError(attr='dt', obj=self) def _probe_step_time(self): self._n_steps = self.signals[self.model.step].copy() self._time = self.signals[self.model.time].copy() @property def n_steps(self): """The current time step of the simulator""" return self._n_steps @property def time(self): """The current time of the simulator""" return self._time def trange(self, dt=None): """Create a range of times matching probe data. Note that the range does not start at 0 as one might expect, but at the first timestep (i.e., dt). Parameters ---------- dt : float (optional) The sampling period of the probe to create a range for. If empty, will use the default probe sampling period. """ dt = self.dt if dt is None else dt n_steps = int(self.n_steps * (self.dt / dt)) return dt * np.arange(1, n_steps + 1) def _probe(self): """Copy all probed signals to buffers""" self._probe_step_time() for probe in self.model.probes: period = (1 if probe.sample_every is None else probe.sample_every / self.dt) if self.n_steps % period < 1: tmp = self.signals[self.model.sig[probe]['in']].copy() self._probe_outputs[probe].append(tmp) def step(self): """Advance the simulator by `self.dt` seconds. """ if self.closed: raise SimulatorClosed("Simulator cannot run because it is closed.") old_err = np.seterr(invalid='raise', divide='ignore') try: for step_fn in self._steps: step_fn() finally: np.seterr(**old_err) self._probe() def run(self, time_in_seconds, progress_bar=True): """Simulate for the given length of time. Parameters ---------- steps : int Number of steps to run the simulation for. progress_bar : bool or ``ProgressBar`` or ``ProgressUpdater``, optional Progress bar for displaying the progress. By default, ``progress_bar=True``, which uses the default progress bar (text in most situations, or an HTML version in recent IPython notebooks). To disable the progress bar, use ``progress_bar=False``. For more control over the progress bar, pass in a :class:`nengo.utils.progress.ProgressBar`, or :class:`nengo.utils.progress.ProgressUpdater` instance. """ steps = int(np.round(float(time_in_seconds) / self.dt)) logger.info("Running %s for %f seconds, or %d steps", self.model.label, time_in_seconds, steps) self.run_steps(steps, progress_bar=progress_bar) def run_steps(self, steps, progress_bar=True): """Simulate for the given number of `dt` steps. Parameters ---------- steps : int Number of steps to run the simulation for. progress_bar : bool or ``ProgressBar`` or ``ProgressUpdater``, optional Progress bar for displaying the progress. By default, ``progress_bar=True``, which uses the default progress bar (text in most situations, or an HTML version in recent IPython notebooks). To disable the progress bar, use ``progress_bar=False``. For more control over the progress bar, pass in a :class:`nengo.utils.progress.ProgressBar`, or :class:`nengo.utils.progress.ProgressUpdater` instance. """ with ProgressTracker(steps, progress_bar) as progress: for i in range(steps): self.step() progress.step() def reset(self, seed=None): """Reset the simulator state. Parameters ---------- seed : int, optional A seed for all stochastic operators used in the simulator. This will change the random sequences generated for noise or inputs (e.g. from Processes), but not the built objects (e.g. ensembles, connections). """ if self.closed: raise SimulatorClosed("Cannot reset closed Simulator.") if seed is not None: self.seed = seed # reset signals for key in self.signals: self.signals.reset(key) # rebuild steps (resets ops with their own state, like Processes) self.rng = np.random.RandomState(self.seed) self._steps = [op.make_step(self.signals, self.dt, self.rng) for op in self._step_order] # clear probe data for probe in self.model.probes: self._probe_outputs[probe] = [] self._probe_step_time() def close(self): """Closes the simulator. Any call to ``run``, ``run_steps``, ``step``, and ``reset`` on a closed simulator will raise ``SimulatorClosed``. """ self.closed = True self.signals = None # signals may no longer exist on some backends
class Simulator(object): """Reference simulator for Nengo models.""" def __init__(self, network, dt=0.001, seed=None, model=None): """Initialize the simulator with a network and (optionally) a model. Most of the time, you will pass in a network and sometimes a dt:: sim1 = nengo.Simulator(my_network) # Uses default 0.001s dt sim2 = nengo.Simulator(my_network, dt=0.01) # Uses 0.01s dt For more advanced use cases, you can initialize the model yourself, and also pass in a network that will be built into the same model that you pass in:: sim = nengo.Simulator(my_network, model=my_model) If you want full control over the build process, then you can build your network into the model manually. If you do this, then you must explicitly pass in ``None`` for the network:: sim = nengo.Simulator(None, model=my_model) Parameters ---------- network : nengo.Network instance or None A network object to the built and then simulated. If a fully built ``model`` is passed in, then you can skip building the network by passing in network=None. dt : float, optional The length of a simulator timestep, in seconds. seed : int, optional A seed for all stochastic operators used in this simulator. model : nengo.builder.Model instance or None, optional A model object that contains build artifacts to be simulated. Usually the simulator will build this model for you; however, if you want to build the network manually, or to inject some build artifacts in the Model before building the network, then you can pass in a ``nengo.builder.Model`` instance. """ self.closed = False if model is None: dt = float(dt) # make sure it's a float (for division purposes) self.model = Model(dt=dt, label="%s, dt=%f" % (network, dt), decoder_cache=get_default_decoder_cache()) else: self.model = model if network is not None: # Build the network into the model self.model.build(network) self.model.decoder_cache.shrink() # -- map from Signal.base -> ndarray self.signals = SignalDict() for op in self.model.operators: op.init_signals(self.signals) # Order the steps (they are made in `Simulator.reset`) self.dg = operator_depencency_graph(self.model.operators) self._step_order = [op for op in toposort(self.dg) if hasattr(op, 'make_step')] # Add built states to the probe dictionary self._probe_outputs = self.model.params # Provide a nicer interface to probe outputs self.data = ProbeDict(self._probe_outputs) seed = np.random.randint(npext.maxint) if seed is None else seed self.reset(seed=seed) def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): self.close() @property def dt(self): """The time step of the simulator""" return self.model.dt @dt.setter def dt(self, dummy): raise ReadonlyError(attr='dt', obj=self) def _probe_step_time(self): self._n_steps = self.signals[self.model.step].copy() self._time = self.signals[self.model.time].copy() @property def n_steps(self): """The current time step of the simulator""" return self._n_steps @property def time(self): """The current time of the simulator""" return self._time def trange(self, dt=None): """Create a range of times matching probe data. Note that the range does not start at 0 as one might expect, but at the first timestep (i.e., dt). Parameters ---------- dt : float (optional) The sampling period of the probe to create a range for. If empty, will use the default probe sampling period. """ dt = self.dt if dt is None else dt n_steps = int(self.n_steps * (self.dt / dt)) return dt * np.arange(1, n_steps + 1) def _probe(self): """Copy all probed signals to buffers""" self._probe_step_time() for probe in self.model.probes: period = (1 if probe.sample_every is None else probe.sample_every / self.dt) if self.n_steps % period < 1: tmp = self.signals[self.model.sig[probe]['in']].copy() self._probe_outputs[probe].append(tmp) def step(self): """Advance the simulator by `self.dt` seconds. """ if self.closed: raise SimulatorClosed("Simulator cannot run because it is closed.") old_err = np.seterr(invalid='raise', divide='ignore') try: for step_fn in self._steps: step_fn() finally: np.seterr(**old_err) self._probe() def run(self, time_in_seconds, progress_bar=True): """Simulate for the given length of time. Parameters ---------- steps : int Number of steps to run the simulation for. progress_bar : bool or ``ProgressBar`` or ``ProgressUpdater``, optional Progress bar for displaying the progress. By default, ``progress_bar=True``, which uses the default progress bar (text in most situations, or an HTML version in recent IPython notebooks). To disable the progress bar, use ``progress_bar=False``. For more control over the progress bar, pass in a :class:`nengo.utils.progress.ProgressBar`, or :class:`nengo.utils.progress.ProgressUpdater` instance. """ steps = int(np.round(float(time_in_seconds) / self.dt)) logger.info("Running %s for %f seconds, or %d steps", self.model.label, time_in_seconds, steps) self.run_steps(steps, progress_bar=progress_bar) def run_steps(self, steps, progress_bar=True): """Simulate for the given number of `dt` steps. Parameters ---------- steps : int Number of steps to run the simulation for. progress_bar : bool or ``ProgressBar`` or ``ProgressUpdater``, optional Progress bar for displaying the progress. By default, ``progress_bar=True``, which uses the default progress bar (text in most situations, or an HTML version in recent IPython notebooks). To disable the progress bar, use ``progress_bar=False``. For more control over the progress bar, pass in a :class:`nengo.utils.progress.ProgressBar`, or :class:`nengo.utils.progress.ProgressUpdater` instance. """ with ProgressTracker(steps, progress_bar) as progress: for i in range(steps): self.step() progress.step() def reset(self, seed=None): """Reset the simulator state. Parameters ---------- seed : int, optional A seed for all stochastic operators used in the simulator. This will change the random sequences generated for noise or inputs (e.g. from Processes), but not the built objects (e.g. ensembles, connections). """ if self.closed: raise SimulatorClosed("Cannot reset closed Simulator.") if seed is not None: self.seed = seed # reset signals for key in self.signals: self.signals.reset(key) # rebuild steps (resets ops with their own state, like Processes) self.rng = np.random.RandomState(self.seed) self._steps = [op.make_step(self.signals, self.dt, self.rng) for op in self._step_order] # clear probe data for probe in self.model.probes: self._probe_outputs[probe] = [] self._probe_step_time() def close(self): """Closes the simulator. Any call to ``run``, ``run_steps``, ``step``, and ``reset`` on a closed simulator will raise ``SimulatorClosed``. """ self.closed = True self.signals = None # signals may no longer exist on some backends
class Simulator(object): """Reference simulator for Nengo models.""" def __init__(self, network, dt=0.001, seed=None, model=None): """Initialize the simulator with a network and (optionally) a model. Most of the time, you will pass in a network and sometimes a dt:: sim1 = nengo.Simulator(my_network) # Uses default 0.001s dt sim2 = nengo.Simulator(my_network, dt=0.01) # Uses 0.01s dt For more advanced use cases, you can initialize the model yourself, and also pass in a network that will be built into the same model that you pass in:: sim = nengo.Simulator(my_network, model=my_model) If you want full control over the build process, then you can build your network into the model manually. If you do this, then you must explicitly pass in ``None`` for the network:: sim = nengo.Simulator(None, model=my_model) Parameters ---------- network : nengo.Network instance or None A network object to the built and then simulated. If a fully built ``model`` is passed in, then you can skip building the network by passing in network=None. dt : float The length of a simulator timestep, in seconds. seed : int A seed for all stochastic operators used in this simulator. Note that there are not stochastic operators implemented currently, so this parameters does nothing. model : nengo.builder.Model instance or None A model object that contains build artifacts to be simulated. Usually the simulator will build this model for you; however, if you want to build the network manually, or to inject some build artifacts in the Model before building the network, then you can pass in a ``nengo.builder.Model`` instance. """ dt = float(dt) # make sure it's a float (for division purposes) if model is None: self.model = Model(dt=dt, label="%s, dt=%f" % (network, dt), decoder_cache=get_default_decoder_cache()) else: self.model = model if network is not None: # Build the network into the model self.model.build(network) self.model.decoder_cache.shrink() self.seed = np.random.randint(npext.maxint) if seed is None else seed self.rng = np.random.RandomState(self.seed) # -- map from Signal.base -> ndarray self.signals = SignalDict(__time__=np.asarray(0.0, dtype=np.float64)) for op in self.model.operators: op.init_signals(self.signals) self.dg = operator_depencency_graph(self.model.operators) self._step_order = [ node for node in toposort(self.dg) if hasattr(node, 'make_step') ] self._steps = [ node.make_step(self.signals, dt, self.rng) for node in self._step_order ] # Add built states to the probe dictionary self._probe_outputs = self.model.params # Provide a nicer interface to probe outputs self.data = ProbeDict(self._probe_outputs) self.reset() @property def dt(self): """The time step of the simulator""" return self.model.dt @dt.setter def dt(self, dummy): raise AttributeError("Cannot change simulator 'dt'. Please file " "an issue at http://github.com/nengo/nengo" "/issues and describe your use case.") @property def time(self): """The current time of the simulator""" return self.signals['__time__'].copy() def trange(self, dt=None): """Create a range of times matching probe data. Note that the range does not start at 0 as one might expect, but at the first timestep (i.e., dt). Parameters ---------- dt : float (optional) The sampling period of the probe to create a range for. If empty, will use the default probe sampling period. """ dt = self.dt if dt is None else dt n_steps = int(self.n_steps * (self.dt / dt)) return dt * np.arange(1, n_steps + 1) def _probe(self): """Copy all probed signals to buffers""" for probe in self.model.probes: period = (1 if probe.sample_every is None else probe.sample_every / self.dt) if self.n_steps % period < 1: tmp = self.signals[self.model.sig[probe]['in']].copy() self._probe_outputs[probe].append(tmp) def step(self): """Advance the simulator by `self.dt` seconds. """ self.n_steps += 1 self.signals['__time__'][...] = self.n_steps * self.dt old_err = np.seterr(invalid='raise', divide='ignore') try: for step_fn in self._steps: step_fn() finally: np.seterr(**old_err) self._probe() def run(self, time_in_seconds, progress_bar=True): """Simulate for the given length of time. Parameters ---------- steps : int Number of steps to run the simulation for. progress_bar : bool or ``ProgressBar`` or ``ProgressUpdater``, optional Progress bar for displaying the progress. By default, ``progress_bar=True``, which uses the default progress bar (text in most situations, or an HTML version in recent IPython notebooks). To disable the progress bar, use ``progress_bar=False``. For more control over the progress bar, pass in a :class:`nengo.utils.progress.ProgressBar`, or :class:`nengo.utils.progress.ProgressUpdater` instance. """ steps = int(np.round(float(time_in_seconds) / self.dt)) logger.debug("Running %s for %f seconds, or %d steps", self.model.label, time_in_seconds, steps) self.run_steps(steps, progress_bar=progress_bar) def run_steps(self, steps, progress_bar=True): """Simulate for the given number of `dt` steps. Parameters ---------- steps : int Number of steps to run the simulation for. progress_bar : bool or ``ProgressBar`` or ``ProgressUpdater``, optional Progress bar for displaying the progress. By default, ``progress_bar=True``, which uses the default progress bar (text in most situations, or an HTML version in recent IPython notebooks). To disable the progress bar, use ``progress_bar=False``. For more control over the progress bar, pass in a :class:`nengo.utils.progress.ProgressBar`, or :class:`nengo.utils.progress.ProgressUpdater` instance. """ with ProgressTracker(steps, progress_bar) as progress: for i in range(steps): self.step() progress.step() def reset(self): """Reset the simulator state.""" self.n_steps = 0 self.signals['__time__'][...] = 0 for key in self.signals: if key != '__time__': self.signals.reset(key) for probe in self.model.probes: self._probe_outputs[probe] = []