Esempio n. 1
0
    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="%s, dt=%f" % (network, dt),
                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)
Esempio n. 2
0
    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

        if model is None:
            self.model = Model(dt=float(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, progress_bar=self.progress_bar)

        # Order the steps (they are made in `Simulator.reset`)
        self.dg = operator_dependency_graph(self.model.operators)

        if optimize:
            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 probe dictionary
        self._probe_outputs = self.model.params

        # Provide a nicer interface to probe outputs
        self.data = ProbeDict(self._probe_outputs)

        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)