Example #1
0
    def __setattr__(self, key, value):
        """A setattr that handles Modules being added specially.

        This is so that we can use the variable name for the Module as
        the name that all of the SPA system will use to access that module.
        """
        if hasattr(self, key) and isinstance(getattr(self, key), Module):
            raise SpaModuleError("Cannot re-assign module-attribute %s to %s. "
                                 "SPA module-attributes can only be assigned "
                                 "once." % (key, value))
        super(SPA, self).__setattr__(key, value)
        if isinstance(value, Module):
            if value.label is None:
                value.label = key
            self._modules[key] = value
            for k, (obj, v) in iteritems(value.inputs):
                if type(v) == int:
                    value.inputs[k] = (obj, self.get_default_vocab(v))
                self.config[obj].vocab = value.inputs[k][1]
            for k, (obj, v) in iteritems(value.outputs):
                if type(v) == int:
                    value.outputs[k] = (obj, self.get_default_vocab(v))
                self.config[obj].vocab = value.outputs[k][1]

            value.on_add(self)
Example #2
0
    def compress(cls, operators):
        sets = OrderedDict()
        incs = OrderedDict()
        rval = []
        for op in operators:
            if isinstance(op, cls):
                if op.sets:
                    assert len(op.sets) == 1 and len(op.incs) == 0
                    sets.setdefault(op.sets[0], []).append(op)
                else:
                    assert len(op.incs) == 1 and len(op.sets) == 0
                    incs.setdefault(op.incs[0], []).append(op)
            else:
                rval.append(op)

        # combine incs into sets if on same view
        for view, set_ops in iteritems(sets):
            set_op, = set_ops
            inc_ops = incs.get(view, [])
            for inc_op in inc_ops[:]:
                set_op.As.extend(inc_op.As)
                set_op.Xs.extend(inc_op.Xs)
                inc_ops.remove(inc_op)
            rval.append(set_op)

        # combine remaining incs if on same view
        for view, inc_ops in iteritems(incs):
            if len(inc_ops) > 0:
                inc_op0 = inc_ops[0]
                for inc_op in inc_ops[1:]:
                    inc_op0.As.extend(inc_op.As)
                    inc_op0.Xs.extend(inc_op.Xs)
                rval.append(inc_op0)

        return rval
Example #3
0
    def compress(cls, operators):
        sets = OrderedDict()
        incs = OrderedDict()
        rval = []
        for op in operators:
            if isinstance(op, cls):
                if op.sets:
                    assert len(op.sets) == 1 and len(op.incs) == 0
                    sets.setdefault(op.sets[0], []).append(op)
                else:
                    assert len(op.incs) == 1 and len(op.sets) == 0
                    incs.setdefault(op.incs[0], []).append(op)
            else:
                rval.append(op)

        # combine incs into sets if on same view
        for view, set_ops in iteritems(sets):
            set_op, = set_ops
            inc_ops = incs.get(view, [])
            for inc_op in inc_ops[:]:
                set_op.As.extend(inc_op.As)
                set_op.Xs.extend(inc_op.Xs)
                inc_ops.remove(inc_op)
            rval.append(set_op)

        # combine remaining incs if on same view
        for view, inc_ops in iteritems(incs):
            if len(inc_ops) > 0:
                inc_op0 = inc_ops[0]
                for inc_op in inc_ops[1:]:
                    inc_op0.As.extend(inc_op.As)
                    inc_op0.Xs.extend(inc_op.Xs)
                rval.append(inc_op0)

        return rval
Example #4
0
 def compress(cls, operators):
     sets = OrderedDict()
     incs = OrderedDict()
     rval = []
     for op in operators:
         if isinstance(op, cls):
             if op.as_update:
                 rval.append(op)
             else:
                 assert op.sets or op.incs
                 if op.sets:
                     sets.setdefault(op.sets[0], []).append(op)
                 if op.incs:
                     incs.setdefault(op.incs[0], []).append(op)
         else:
             rval.append(op)
     done = set()
     for view, set_ops in iteritems(sets):
         set_op, = set_ops
         done.add(set_op)
         for inc_op in incs.get(view, []):
             set_op.As.extend(inc_op.As)
             set_op.Xs.extend(inc_op.Xs)
             done.add(inc_op)
         rval.append(set_op)
     for view, inc_ops in iteritems(incs):
         for inc_op in inc_ops:
             if inc_op not in done:
                 rval.append(inc_op)
     return rval
Example #5
0
 def compress(cls, operators):
     sets = OrderedDict()
     incs = OrderedDict()
     rval = []
     for op in operators:
         if isinstance(op, cls):
             if op.as_update:
                 rval.append(op)
             else:
                 assert op.sets or op.incs
                 if op.sets:
                     sets.setdefault(op.sets[0], []).append(op)
                 if op.incs:
                     incs.setdefault(op.incs[0], []).append(op)
         else:
             rval.append(op)
     done = set()
     for view, set_ops in iteritems(sets):
         set_op, = set_ops
         done.add(set_op)
         for inc_op in incs.get(view, []):
             set_op.As.extend(inc_op.As)
             set_op.Xs.extend(inc_op.Xs)
             done.add(inc_op)
         rval.append(set_op)
     for view, inc_ops in iteritems(incs):
         for inc_op in inc_ops:
             if inc_op not in done:
                 rval.append(inc_op)
     return rval
Example #6
0
    def __setattr__(self, key, value):
        """A setattr that handles Modules being added specially.

        This is so that we can use the variable name for the Module as
        the name that all of the SPA system will use to access that module.
        """
        if hasattr(self, key) and isinstance(getattr(self, key), Module):
            raise SpaModuleError("Cannot re-assign module-attribute %s to %s. "
                                 "SPA module-attributes can only be assigned "
                                 "once." % (key, value))
        super(SPA, self).__setattr__(key, value)
        if isinstance(value, Module):
            if value.label is None:
                value.label = key
            self._modules[key] = value
            for k, (obj, v) in iteritems(value.inputs):
                if type(v) == int:
                    value.inputs[k] = (obj, self.get_default_vocab(v))
                self.config[obj].vocab = value.inputs[k][1]
            for k, (obj, v) in iteritems(value.outputs):
                if type(v) == int:
                    value.outputs[k] = (obj, self.get_default_vocab(v))
                self.config[obj].vocab = value.outputs[k][1]

            value.on_add(self)
Example #7
0
def parse_ges(ges_path, speaker=None, ignore_f0=True):
    bridge = VTL(speaker)

    xml = etree.parse(ges_path)
    labels = bridge.gesture_labels()
    if ignore_f0:
        labels.remove('f0')
    gs = GestureScore(labels)
    for element in xml.iter():
        attr = element.attrib
        if element.tag == "gesture_sequence":
            seq = GestureSequence(**attr)
            if ignore_f0 and seq.type.startswith('f0'):
                continue
            gs.sequences.append(seq)
        elif element.tag == "gesture":
            gest_attr = {}
            gest_attr['neutral'] = bool(int(attr.pop('neutral')))
            gest_attr['value'] = attr.pop('value')
            if seq.numerical:
                gest_attr['value'] = float(gest_attr['value'])
            gest_attr.update({key: float(val) for key, val in iteritems(attr)})
            gest = Gesture(**gest_attr)
            seq.gestures.append(gest)
    return gs
Example #8
0
def parse_ges(ges_path, speaker=None, ignore_f0=True):
    bridge = VTL(speaker)

    xml = etree.parse(ges_path)
    labels = bridge.gesture_labels()
    if ignore_f0:
        labels.remove('f0')
    gs = GestureScore(labels)
    for element in xml.iter():
        attr = element.attrib
        if element.tag == "gesture_sequence":
            seq = GestureSequence(**attr)
            if ignore_f0 and seq.type.startswith('f0'):
                continue
            gs.sequences.append(seq)
        elif element.tag == "gesture":
            gest_attr = {}
            gest_attr['neutral'] = bool(int(attr.pop('neutral')))
            gest_attr['value'] = attr.pop('value')
            if seq.numerical:
                gest_attr['value'] = float(gest_attr['value'])
            gest_attr.update({key: float(val) for key, val in iteritems(attr)})
            gest = Gesture(**gest_attr)
            seq.gestures.append(gest)
    return gs
Example #9
0
    def plan_SimProcess(self, all_ops):
        class_groups = groupby(all_ops, lambda op: type(op.process))
        plan_groups = defaultdict(list)
        step_plans = []
        for process_class, ops in class_groups:
            for cls in process_class.__mro__:
                attrname = '_plan_' + cls.__name__
                if hasattr(self, attrname):
                    plan_groups[attrname].extend(ops)
                    break
            else:
                for op in ops:
                    shape = lambda s: s.shape if s is not None else (0, )
                    fn = op.process.make_step(shape(op.input),
                                              shape(op.output),
                                              self.model.dt,
                                              rng=op.process.get_rng(self.rng))
                    step_plans.extend(
                        self._plan_python_fn(fn, [op.t], [op.input],
                                             [op.output]))

        process_plans = [
            p for attr, ops in iteritems(plan_groups)
            for p in getattr(self, attr)(ops)
        ]
        return process_plans + step_plans
Example #10
0
    def reset(self, seed=None):
        if self.closed:
            raise SimulatorClosed("Cannot reset closed Simulator.")

        if seed is not None:
            raise NotImplementedError("Seed changing not implemented")

        # reset signals
        for base in self.all_bases:
            # TODO: copy all data on at once
            if not base.readonly:
                self.all_data[self.sidx[base]] = base.initial_value

        for clra, ra in iteritems(self._raggedarrays_to_reset):
            # TODO: copy all data on at once
            for i in range(len(clra)):
                clra[i] = ra[i]

        # clear probe data
        if self._cl_probe_plan is not None:
            self._cl_probe_plan.cl_bufpositions.fill(0)

            for probe in self.model.probes:
                del self._probe_outputs[probe][:]

        self._reset_rng()
        self._reset_cl_rngs()
        self._probe_step_time()
Example #11
0
 def _reset_cl_rngs(self):
     for rngs, seeds in iteritems(self._cl_rngs):
         seeds = [
             self.rng.randint(npext.maxint) if s is None else s
             for s in seeds
         ]
         init_rngs(self.queue, rngs, seeds)
Example #12
0
    def make_step(self, shape_in, shape_out, dt, rng):
        tp, yp = zip(*sorted(iteritems(self.data)))
        assert shape_in == (0, )
        assert shape_out == (self.size_out, )

        if self.interpolation == 'zero':

            def step_piecewise(t):
                ti = (np.searchsorted(tp, t + 0.5 * dt) - 1).clip(
                    -1,
                    len(yp) - 1)
                if ti == -1:
                    return np.zeros(shape_out)
                else:
                    return (np.ravel(yp[ti](t))
                            if callable(yp[ti]) else yp[ti])
        else:
            assert self.sp_interpolate is not None

            if self.interpolation == "cubic" and 0 not in tp:
                warnings.warn("'cubic' interpolation may fail if data not "
                              "specified for t=0.0")

            f = self.sp_interpolate.interp1d(tp,
                                             yp,
                                             axis=0,
                                             kind=self.interpolation,
                                             bounds_error=False,
                                             fill_value=0.)

            def step_piecewise(t):
                return np.ravel(f(t))

        return step_piecewise
Example #13
0
 def add_derivative(self, klass="IntermediateDeriv", **kwargs):
     deriv = globals()["%sParams" % klass]()
     for k, v in iteritems(kwargs):
         setattr(deriv, k, v)
     self.derivatives.append(deriv)
     self.mfcc.n_derivatives += 1
     return deriv
Example #14
0
    def coerce(self, instance, data):
        data = super(PiecewiseDataParam, self).coerce(instance, data)

        size_out = None
        for time, value in iteritems(data):
            if not is_number(time):
                raise ValidationError("Keys must be times (floats or ints), "
                                      "not %r" % type(time).__name__,
                                      attr='data',
                                      obj=instance)

            # figure out the length of this item
            if callable(value):
                try:
                    value = np.ravel(value(time))
                except Exception:
                    raise ValidationError(
                        "callable object for time step %.3f "
                        "should return a numerical constant" % time,
                        attr='data',
                        obj=instance)
            else:
                value = np.ravel(value)
                data[time] = value
            size = value.size

            # make sure this is the same size as previous items
            if size != size_out and size_out is not None:
                raise ValidationError("time %g has size %d instead of %d" %
                                      (time, size, size_out),
                                      attr='data',
                                      obj=instance)
            size_out = size

        return data
Example #15
0
    def on_add(self, spa):
        Module.on_add(self, spa)
        self.spa = spa

        # parse the provided class and match it up with the spa model
        self.actions.process(spa)
        for action in self.actions.actions:
            if action.condition is not None:
                raise NotImplementedError("Cortical actions do not support "
                                          "conditional expressions: %s." %
                                          action.condition)
            for name, effects in iteritems(action.effect.effect):
                for effect in effects.expression.items:
                    if isinstance(effect, Symbol):
                        self.add_direct_effect(name, effect.symbol)
                    elif isinstance(effect, Source):
                        self.add_route_effect(name, effect.name,
                                              effect.transform.symbol,
                                              effect.inverted)
                    elif isinstance(effect, Convolution):
                        self.add_conv_effect(name, effect)
                    else:
                        raise NotImplementedError(
                            "Subexpression '%s' from action '%s' is not "
                            "supported by the cortex." % (effect, action))
Example #16
0
 def add_derivative(self, klass="IntermediateDeriv", **kwargs):
     deriv = globals()["%sParams" % klass]()
     for k, v in iteritems(kwargs):
         setattr(deriv, k, v)
     self.derivatives.append(deriv)
     self.mfcc.n_derivatives += 1
     return deriv
Example #17
0
 def __call__(self):
     args = [v() if isinstance(v, Factory) else v for v in self.args]
     kwargs = {
         k: v() if isinstance(v, Factory) else v
         for k, v in iteritems(self.kwargs)
     }
     return self.klass(*args, **kwargs)
Example #18
0
 def get_module_inputs(self):
     for name, module in iteritems(self._modules):
         for input in module.inputs:
             if input == 'default':
                 yield name
             else:
                 yield '%s_%s' % (name, input)
Example #19
0
    def on_add(self, spa):
        Module.on_add(self, spa)
        self.spa = spa

        with spa:
            # connect basal ganglia to thalamus
            nengo.Connection(self.bg.output,
                             self.actions.input,
                             synapse=self.synapse_bg)

        # implement the various effects
        for i, action in enumerate(self.bg.actions.actions):
            for name, effects in iteritems(action.effect.effect):
                for effect in effects.expression.items:
                    if isinstance(effect, (int, float)):
                        effect = Symbol('%g' % effect)
                    if isinstance(effect, Symbol):
                        self.add_direct_effect(i, name, effect.symbol)
                    elif isinstance(effect, Source):
                        self.add_route_effect(i, name, effect.name,
                                              effect.transform.symbol,
                                              effect.inverted)
                    elif isinstance(effect, Convolution):
                        self.add_conv_effect(i, name, effect)
                    else:
                        raise NotImplementedError(
                            "Subexpression '%s' from action '%s' is not "
                            "supported by the Thalamus." % (effect, action))
Example #20
0
 def get_module_inputs(self):
     for name, module in iteritems(self._modules):
         for input in module.inputs:
             if input == 'default':
                 yield name
             else:
                 yield '%s_%s' % (name, input)
Example #21
0
    def sync(self):
        """Write changes to the cache index back to disk.

        The call to this function will be locked by a file lock.
        """
        try:
            with self._lock:
                try:
                    self._load_index_file()
                except IOError as err:
                    if err.errno == errno.ENOENT:
                        self._index = {}
                    else:
                        raise

                self._index.update(self._updates)
                for key in self._deletes:
                    del self._index[key]
                self._index = {k: v for k, v in iteritems(self._index)
                               if v[0] not in self._removed_files}

                self._write_index()
        except TimeoutError:
            warnings.warn(
                "Decoder cache index could not acquire lock. "
                "Cache index was not synced.")

        self._updates.clear()
        self._deletes.clear()
        self._removed_files.clear()
Example #22
0
 def get_module_outputs(self):
     for name, module in iteritems(self._modules):
         for output in module.outputs:
             if output == 'default':
                 yield name
             else:
                 yield '%s_%s' % (name, output)
Example #23
0
 def get_module_outputs(self):
     for name, module in iteritems(self._modules):
         for output in module.outputs:
             if output == 'default':
                 yield name
             else:
                 yield '%s_%s' % (name, output)
Example #24
0
    def on_add(self, spa):
        Module.on_add(self, spa)
        self.spa = spa

        with spa:
            # connect basal ganglia to thalamus
            nengo.Connection(self.bg.output, self.actions.input,
                             synapse=self.synapse_bg)

        # implement the various effects
        for i, action in enumerate(self.bg.actions.actions):
            for name, effects in iteritems(action.effect.effect):
                for effect in effects.expression.items:
                    if isinstance(effect, (int, float)):
                        effect = Symbol('%g' % effect)
                    if isinstance(effect, Symbol):
                        self.add_direct_effect(i, name, effect.symbol)
                    elif isinstance(effect, Source):
                        self.add_route_effect(i, name, effect.name,
                                              effect.transform.symbol,
                                              effect.inverted)
                    elif isinstance(effect, Convolution):
                        self.add_conv_effect(i, name, effect)
                    else:
                        raise NotImplementedError(
                            "Subexpression '%s' from action '%s' is not "
                            "supported by the Thalamus." % (effect, action))
Example #25
0
    def on_add(self, spa):
        Module.on_add(self, spa)
        self.spa = spa

        # parse the provided class and match it up with the spa model
        self.actions.process(spa)
        for action in self.actions.actions:
            if action.condition is not None:
                raise NotImplementedError("Cortical actions do not support "
                                          "conditional expressions: %s." %
                                          action.condition)
            for name, effects in iteritems(action.effect.effect):
                for effect in effects.expression.items:
                    if isinstance(effect, Symbol):
                        self.add_direct_effect(name, effect.symbol)
                    elif isinstance(effect, Source):
                        self.add_route_effect(name, effect.name,
                                              effect.transform.symbol,
                                              effect.inverted)
                    elif isinstance(effect, Convolution):
                        self.add_conv_effect(name, effect)
                    else:
                        raise NotImplementedError(
                            "Subexpression '%s' from action '%s' is not "
                            "supported by the cortex." % (effect, action))
Example #26
0
    def probe_helper(net, recursive, probe_options):
        with net:
            for obj_type, obj_list in iteritems(net.objects):

                # recursively probe subnetworks if required
                if obj_type is Network and recursive:
                    for subnet in obj_list:
                        probe_helper(subnet,
                                     recursive=recursive,
                                     probe_options=probe_options)

                # probe all probeable objects
                elif probe_options is None:
                    for obj in obj_list:
                        if hasattr(obj,
                                   'probeable') and len(obj.probeable) > 0:
                            probes[obj] = {}
                            for probeable in obj.probeable:
                                probes[obj][probeable] = Probe(
                                    obj, probeable, **probe_args)

                # probe specified objects only
                elif obj_type in probe_options:
                    for obj in obj_list:
                        if not (hasattr(obj, 'probeable')
                                and len(obj.probeable) > 0):
                            raise ValueError("'%s' is not probeable" % obj)
                        probes[obj] = {}
                        for attr in probe_options[obj_type]:
                            if attr not in obj.probeable:
                                raise ValueError(
                                    "'%s' is not probeable for '%s'" %
                                    (obj, attr))
                            probes[obj][attr] = Probe(obj, attr, **probe_args)
Example #27
0
File: probe.py Project: Ocode/nengo
    def probe_helper(net, recursive, probe_options):
        with net:
            for obj_type, obj_list in iteritems(net.objects):

                # recursively probe subnetworks if required
                if obj_type is Network and recursive:
                    for subnet in obj_list:
                        probe_helper(subnet, recursive=recursive,
                                     probe_options=probe_options)

                # probe all probeable objects
                elif probe_options is None:
                    for obj in obj_list:
                        if hasattr(obj, 'probeable') and len(
                                obj.probeable) > 0:
                            probes[obj] = {}
                            for probeable in obj.probeable:
                                probes[obj][probeable] = Probe(
                                    obj, probeable, **probe_args)

                # probe specified objects only
                elif obj_type in probe_options:
                    for obj in obj_list:
                        if not (hasattr(obj, 'probeable')
                                and len(obj.probeable) > 0):
                            raise ValueError("'%s' is not probeable" % obj)
                        probes[obj] = {}
                        for attr in probe_options[obj_type]:
                            if attr not in obj.probeable:
                                raise ValueError(
                                    "'%s' is not probeable for '%s'" %
                                    (obj, attr))
                            probes[obj][
                                attr] = Probe(obj, attr, **probe_args)
Example #28
0
    def reset(self, seed=None):
        if self.closed:
            raise SimulatorClosed("Cannot reset closed Simulator.")

        if seed is not None:
            raise NotImplementedError("Seed changing not implemented")

        # reset signals
        for base in self.all_bases:
            # TODO: copy all data on at once
            if not base.readonly:
                self.all_data[self.sidx[base]] = base.initial_value

        for clra, ra in iteritems(self._raggedarrays_to_reset):
            # TODO: copy all data on at once
            for i in range(len(clra)):
                clra[i] = ra[i]

        # clear probe data
        if self._cl_probe_plan is not None:
            self._cl_probe_plan.cl_bufpositions.fill(0)

            for probe in self.model.probes:
                del self._probe_outputs[probe][:]

        self._reset_rng()
        self._reset_cl_rngs()
        self._probe_step_time()
Example #29
0
    def sync(self):
        """Write changes to the cache index back to disk.

        The call to this function will be locked by a file lock.
        """
        try:
            with self._lock:
                try:
                    self._load_index_file()
                except IOError as err:
                    if err.errno == errno.ENOENT:
                        self._index = {}
                    else:
                        raise

                self._index.update(self._updates)
                for key in self._deletes:
                    del self._index[key]
                self._index = {
                    k: v
                    for k, v in iteritems(self._index)
                    if v[0] not in self._removed_files
                }

                self._write_index()
        except TimeoutError:
            warnings.warn("Decoder cache index could not acquire lock. "
                          "Cache index was not synced.")

        self._updates.clear()
        self._deletes.clear()
        self._removed_files.clear()
Example #30
0
def test_configure_all_nengo_parameters():

    # make up a non-default value for the parameter
    conv_func = {
        params.BoolParam:
        lambda attr: not attr.default,
        params.NumberParam:
        lambda attr: (1 if attr.default is None else attr.default + 1),
        params.StringParam:
        lambda attr: "abc",
        params.NdarrayParam:
        lambda attr: np.zeros([1] * len(attr.shape)),
        nengo.base.ProcessParam:
        lambda attr: nengo.processes.WhiteNoise(),
        nengo.node.OutputParam:
        lambda attr: lambda x: x + 1,
        nengo.synapses.SynapseParam:
        lambda attr: nengo.synapses.Alpha(0.1),
        nengo.solvers.SolverParam:
        lambda attr: nengo.solvers.LstsqL2nz(weights=isinstance(
            attr, nengo.connection.ConnectionSolverParam)),
        nengo.connection.ConnectionFunctionParam:
        lambda attr: lambda x: x + 1,
        nengo.connection.ConnectionTransformParam:
        lambda attr: 2.0,
        nengo.learning_rules.LearningRuleTypeParam:
        (lambda attr: nengo.learning_rules.PES()),
        nengo.neurons.NeuronTypeParam:
        lambda attr: nengo.AdaptiveLIF(),
    }

    net = nengo.Network()

    for obj in net.objects:
        for name, attr in obj.__dict__.items():
            if (not isinstance(attr, params.Parameter)
                    or attr.default is params.Unconfigurable):
                continue

            for param, func in iteritems(conv_func):
                if isinstance(attr, param):
                    val = func(attr)
                    break
            else:
                raise NotImplementedError

            try:
                # manually set the attribute to its default
                setattr(net.config[obj], name, attr.default)

                # set it to a non-default value
                setattr(net.config[obj], name, val)
                assert getattr(net.config[obj], name) == val

            except Exception:
                print("Error setting %s.%s" % (obj, name))
                raise
Example #31
0
 def save_network(self):
     state = []
     for obj, layout in iteritems(self.pos):
         state.append({
             'uid': self.net_graph.page.get_uid(obj),
             'pos': self.net_graph.page.config[obj].pos,
             'size': self.net_graph.page.config[obj].size,
             'obj': obj,
         })
     return state
Example #32
0
 def save_network(self):
     state = []
     for obj, layout in iteritems(self.pos):
         state.append({
             'uid': self.net_graph.page.get_uid(obj),
             'pos': self.net_graph.page.config[obj].pos,
             'size': self.net_graph.page.config[obj].size,
             'obj': obj,
         })
     return state
def test_dtype(RefSimulator, seed, dtype):
    with nengo.Network() as model:
        u = nengo.Node([0.5, -0.4])
        a = nengo.Ensemble(10, 2)
        nengo.Connection(u, a)
        nengo.Probe(a)

    sim = RefSimulator(model, dtype=dtype)
    for k, v in iteritems(sim.signals):
        assert v.dtype == dtype, "Signal '%s', wrong dtype" % k
Example #34
0
    def update_signal_indexing(self, merged_op, replaced_signals):
        for s in merged_op.all_signals:
            self.sig2ops[s].add(merged_op)
            if s.is_view:
                self.base2views[s.base].add(s)

        for from_sig, to_sig in iteritems(replaced_signals):
            self.sig2ops[to_sig] = self.sig2ops[from_sig]
            if to_sig.is_view:
                self.base2views[to_sig.base].add(to_sig)
Example #35
0
File: spa.py Project: bopo/nengo
    def __setattr__(self, key, value):
        """A setattr that handles Modules being added specially.

        This is so that we can use the variable name for the Module as
        the name that all of the SPA system will use to access that module.
        """
        super(SPA, self).__setattr__(key, value)
        if isinstance(value, Module):
            value.label = key
            self._modules[value.label] = value
            for k, (obj, v) in iteritems(value.inputs):
                if type(v) == int:
                    value.inputs[k] = (obj, self.get_default_vocab(v))
                obj.vocab = value.inputs[k][1]
            for k, (obj, v) in iteritems(value.outputs):
                if type(v) == int:
                    value.outputs[k] = (obj, self.get_default_vocab(v))
                obj.vocab = value.outputs[k][1]

            value.on_add(self)
Example #36
0
    def process(self, spa):
        """Parse the actions and generate the list of Action objects."""
        self.actions = []

        sources = list(spa.get_module_outputs())
        sinks = list(spa.get_module_inputs())

        for action in self.args:
            self.actions.append(Action(sources, sinks, action, name=None))
        for name, action in iteritems(self.kwargs):
            self.actions.append(Action(sources, sinks, action, name=name))
Example #37
0
    def process(self, spa):
        """Parse the actions and generate the list of Action objects."""
        self.actions = []

        sources = list(spa.get_module_outputs())
        sinks = list(spa.get_module_inputs())

        for action in self.args:
            self.actions.append(Action(sources, sinks, action, name=None))
        for name, action in iteritems(self.kwargs):
            self.actions.append(Action(sources, sinks, action, name=name))
Example #38
0
    def __setattr__(self, key, value):
        """A setattr that handles Modules being added specially.

        This is so that we can use the variable name for the Module as
        the name that all of the SPA system will use to access that module.
        """
        super(SPA, self).__setattr__(key, value)
        if isinstance(value, Module):
            value.label = key
            self._modules[value.label] = value
            for k, (obj, v) in iteritems(value.inputs):
                if type(v) == int:
                    value.inputs[k] = (obj, self.get_default_vocab(v))
                obj.vocab = value.inputs[k][1]
            for k, (obj, v) in iteritems(value.outputs):
                if type(v) == int:
                    value.outputs[k] = (obj, self.get_default_vocab(v))
                obj.vocab = value.outputs[k][1]

            value.on_add(self)
Example #39
0
 def save_network(self):
     state = []
     for obj, layout in iteritems(self.pos):
         state.append(
             {
                 "uid": self.net_graph.page.get_uid(obj),
                 "pos": self.net_graph.page.config[obj].pos,
                 "size": self.net_graph.page.config[obj].size,
                 "obj": obj,
             }
         )
     return state
Example #40
0
    def update(self, locals, namefinder):
        # Add any components from locals
        for name, obj in iteritems(locals):
            if isinstance(obj, components.Component):
                self.add(obj)
                # TODO: attach?
                # obj.attach(page=self, config=self.config.cfg[name], uid=name)

        # Make components for Nengo objects not in locals
        for obj, uid in iteritems(namefinder.names):
            if uid in self.by_uid:
                continue

            if isinstance(obj, nengo.Connection):
                self.add(
                    components.Connection(self.client, obj, uid, namefinder))
            elif isinstance(obj, tuple(self.NENGO_MAP)):
                for nengocls, compcls in iteritems(self.NENGO_MAP):
                    if isinstance(obj, nengocls):
                        self.add(compcls(self.client, obj, name))
                        break
Example #41
0
    def act_feedforward_layout(self):
        for obj, layout in iteritems(self.pos):
            obj_cfg = self.net_graph.page.config[obj]
            obj_cfg.pos = (layout["y"] / self.scale - self.x, layout["x"] / self.scale - self.y)
            obj_cfg.size = (layout["h"] / 2 / self.scale, layout["w"] / 2 / self.scale)

            obj_uid = self.net_graph.page.get_uid(obj)

            self.send("pos_size", uid=obj_uid, pos=obj_cfg.pos, size=obj_cfg.size)

        self.net_graph.page.config[self.network].has_layout = True
        self.net_graph.modified_config()
Example #42
0
    def __setstate__(self, state):
        for attr in self._param_init_order:
            setattr(self, attr, state.pop(attr))

        for attr in self.params:
            if attr in state:
                setattr(self, attr, state.pop(attr))

        for k, v in iteritems(state):
            setattr(self, k, v)

        self._initialized = True
        if len(nengo.Network.context) > 0:
            warnings.warn(NotAddedToNetworkWarning(self))
Example #43
0
    def __setstate__(self, state):
        for attr in self._param_init_order:
            setattr(self, attr, state.pop(attr))

        for attr in self.params:
            if attr in state:
                setattr(self, attr, state.pop(attr))

        for k, v in iteritems(state):
            setattr(self, k, v)

        self._initialized = True
        if len(nengo.Network.context) > 0:
            warnings.warn(NotAddedToNetworkWarning(self))
Example #44
0
    def act_feedforward_layout(self):
        for obj, layout in iteritems(self.pos):
            obj_cfg = self.net_graph.page.config[obj]
            obj_cfg.pos = (layout['y'] / self.scale - self.x,
                           layout['x'] / self.scale - self.y)
            obj_cfg.size = (layout['h'] / 2 / self.scale,
                            layout['w'] / 2 / self.scale)

            obj_uid = self.net_graph.page.get_uid(obj)

            self.send('pos_size',
                      uid=obj_uid, pos=obj_cfg.pos, size=obj_cfg.size)

        self.net_graph.page.config[self.network].has_layout = True
        self.net_graph.modified_config()
Example #45
0
 def learning_rule(self):
     if self.learning_rule_type is not None and self._learning_rule is None:
         types = self.learning_rule_type
         if isinstance(types, dict):
             self._learning_rule = types.__class__()  # dict of same type
             for k, v in iteritems(types):
                 self._learning_rule[k] = LearningRule(self, v)
         elif is_iterable(types):
             self._learning_rule = [LearningRule(self, v) for v in types]
         elif isinstance(types, LearningRuleType):
             self._learning_rule = LearningRule(self, types)
         else:
             raise ValueError("Invalid type for `learning_rule_type`: %s"
                              % (types.__class__.__name__))
     return self._learning_rule
Example #46
0
 def learning_rule(self):
     if self.learning_rule_type is not None and self._learning_rule is None:
         types = self.learning_rule_type
         if isinstance(types, dict):
             self._learning_rule = types.__class__()  # dict of same type
             for k, v in iteritems(types):
                 self._learning_rule[k] = LearningRule(self, v)
         elif is_iterable(types):
             self._learning_rule = [LearningRule(self, v) for v in types]
         elif isinstance(types, LearningRuleType):
             self._learning_rule = LearningRule(self, types)
         else:
             raise ValueError("Invalid type for `learning_rule_type`: %s" %
                              (types.__class__.__name__))
     return self._learning_rule
Example #47
0
def toposort(edges):
    """Topological sort algorithm by Kahn[1]

    Complexity is O(nodes + vertices).

    Parameters
    ----------
    edges : dict
        Dict of the form {a: {b, c}} where b and c depend on a

    Returns
    -------
    An ordered list of nodes that satisfy the dependencies of ``edges``

    Example
    -------

    >>> toposort({1: {2, 3}, 2: (3,)})
    [1, 2, 3]

    Notes
    -----

    Closely follows the wikipedia page [2]

    [1] Kahn, Arthur B. (1962), "Topological sorting of large networks",
    Communications of the ACM
    [2] http://en.wikipedia.org/wiki/Toposort#Algorithms
    """
    incoming_edges = reverse_edges(edges)
    incoming_edges = dict((k, set(val))
                          for k, val in iteritems(incoming_edges))
    vertices = set((v for v in edges if v not in incoming_edges))
    ordered = []

    while vertices:
        n = vertices.pop()
        ordered.append(n)
        for m in edges.get(n, ()):
            assert n in incoming_edges[m]
            incoming_edges[m].remove(n)
            if not incoming_edges[m]:
                vertices.add(m)
    if any(incoming_edges.get(v, None) for v in edges):
        raise ValueError("Input graph has cycles. This usually occurs because "
                         "too many connections have no synapses. Try setting "
                         "more synapses to '0' instead of 'None'.")
    return ordered
Example #48
0
def groupby(objects, key, hashable=None, force_list=True):
    """Group objects based on a key.

    Unlike `itertools.groupby`, this function does not require the input
    to be sorted.

    Parameters
    ----------
    objects : Iterable
        The objects to be grouped.
    key : callable
        The key function by which to group the objects. If
        `key(obj1) == key(obj2)` then `obj1` and `obj2` are in the same group,
        otherwise they are not.
    hashable : boolean (optional)
        Whether to use the key's hash to determine equality. By default, this
        will be determined by calling `key` on the first item in `objects`, and
        if it is hashable, the hash will be used. Using a hash is faster, but
        not possible for all keys.
    force_list : boolean (optional)
        Whether to force the returned `key_groups` iterator, as well as the
        `group` iterator in each `(key, group)` pair, to be lists.

    Returns
    -------
    keygroups : iterable
        An iterable of `(key, group)` pairs, where `key` is the key used for
        grouping, and `group` is an iterable of the items in the group. The
        nature of the iterables depends on the value of `force_list`.
    """
    if hashable is None:
        # get first item without advancing iterator, and see if key is hashable
        objects, objects2 = itertools.tee(iter(objects))
        item0 = next(objects2)
        hashable = isinstance(key(item0), collections.Hashable)

    if hashable:
        # use a dictionary to sort by hash (faster)
        groups = {}
        for obj in objects:
            groups.setdefault(key(obj), []).append(obj)
        return list(groups.items()) if force_list else iteritems(groups)
    else:
        keygroupers = itertools.groupby(sorted(objects, key=key), key=key)
        if force_list:
            return [(k, [v for v in g]) for k, g in keygroupers]
        else:
            return keygroupers
Example #49
0
    def plan_SimProcess(self, all_ops):
        class_groups = groupby(all_ops, lambda op: op.process.__class__)
        plan_groups = defaultdict(list)

        for process_class, ops in class_groups:
            for cls in process_class.__mro__:
                attrname = '_plan_' + cls.__name__
                if hasattr(self, attrname):
                    plan_groups[attrname].extend(ops)
                    break
            else:
                raise NotImplementedError("Unsupported process type '%s'"
                                          % process_class.__name__)

        return [p for attr, ops in iteritems(plan_groups)
                for p in getattr(self, attr)(ops)]
Example #50
0
    def update(self, names):
        nets = []
        for k, v in iteritems(names):
            if not k.startswith('_'):
                try:
                    self.names[v] = k
                    if isinstance(v, nengo.Network):
                        nets.append(v)
                except TypeError:
                    pass

        if len(nets) > 1:
            logger.info("More than one top-level model defined.")

        for net in nets:
            self._parse_network(net)
Example #51
0
def build_probe(model, probe):
    """Builds a `.Probe` object into a model.

    Under the hood, there are two types of probes:
    connection probes and signal probes.

    Connection probes are those that are built by creating a new `.Connection`
    object from the probe's target to the probe, and calling that connection's
    build function. Creating and building a connection ensure that the result
    of probing the target's attribute is the same as would result from that
    target being connected to another object.

    Signal probes are those that are built by finding the correct `.Signal`
    in the model and calling the build function corresponding to the probe's
    synapse.

    Parameters
    ----------
    model : Model
        The model to build into.
    probe : Probe
        The connection to build.

    Notes
    -----
    Sets ``model.params[probe]`` to a list.
    `.Simulator` appends to that list when running a simulation.
    """

    # find the right parent class in `objtypes`, using `isinstance`
    for nengotype, probeables in iteritems(probemap):
        if isinstance(probe.obj, nengotype):
            break
    else:
        raise BuildError(
            "Type %r is not probeable" % type(probe.obj).__name__)

    key = probeables[probe.attr] if probe.attr in probeables else probe.attr
    if key is None:
        conn_probe(model, probe)
    else:
        signal_probe(model, key, probe)

    model.probes.append(probe)

    # Simulator will fill this list with probe data during simulation
    model.params[probe] = []
Example #52
0
    def on_add(self, spa):
        """Create the connections and nodes."""
        Module.on_add(self, spa)

        for name, value in iteritems(self.kwargs):
            target, vocab = spa.get_module_input(name)
            if callable(value):
                val = make_parse_func(value, vocab)
            else:
                val = vocab.parse(value).v

            with self:
                node = nengo.Node(val, label='input_%s' % name)
            self.input_nodes[name] = node

            with spa:
                nengo.Connection(node, target, synapse=None)
Example #53
0
File: magic.py Project: bopo/nengo
 def __new__(meta, name, bases, clsdict):
     if not('__doc__' in clsdict and clsdict['__doc__']):
         for mro_cls in (
                 mro_cls for base in bases for mro_cls in base.mro()):
             doc = mro_cls.__doc__
             if doc:
                 clsdict['__doc__'] = doc
                 break
     for attr, attribute in iteritems(clsdict):
         if not attribute.__doc__:
             for mro_cls in (
                     mro_cls for base in bases for mro_cls in base.mro()
                     if hasattr(mro_cls, attr)):
                 doc = getattr(getattr(mro_cls, attr), '__doc__')
                 if doc:
                     attribute.__doc__ = doc
                     break
     return type.__new__(meta, name, bases, clsdict)
Example #54
0
    def plan_SimProcess(self, all_ops):
        class_groups = groupby(all_ops, lambda op: type(op.process))
        plan_groups = defaultdict(list)
        python_ops = []
        for process_class, ops in class_groups:
            for cls in process_class.__mro__:
                attrname = '_plan_' + cls.__name__
                if hasattr(self, attrname):
                    plan_groups[attrname].extend(ops)
                    break
            else:
                python_ops.extend(ops)

        process_plans = [p for attr, ops in iteritems(plan_groups)
                         for p in getattr(self, attr)(ops)]
        python_plans = [p for op in python_ops
                        for p in self._plan_python_process(op)]
        return process_plans + python_plans
Example #55
0
    def learning_rule(self):
        """(LearningRule or iterable) Connectable learning rule object(s)."""
        if self.learning_rule_type is not None and self._learning_rule is None:
            types = self.learning_rule_type
            if isinstance(types, dict):
                self._learning_rule = types.__class__()  # dict of same type
                for k, v in iteritems(types):
                    self._learning_rule[k] = LearningRule(self, v)
            elif is_iterable(types):
                self._learning_rule = [LearningRule(self, v) for v in types]
            elif isinstance(types, LearningRuleType):
                self._learning_rule = LearningRule(self, types)
            else:
                raise ValidationError(
                    "Invalid type %r" % types.__class__.__name__,
                    attr='learning_rule_type', obj=self)

        return self._learning_rule
Example #56
0
def build_probe(model, probe):
    # find the right parent class in `objtypes`, using `isinstance`
    for nengotype, probeables in iteritems(probemap):
        if isinstance(probe.obj, nengotype):
            break
    else:
        raise ValueError("Type '%s' is not probeable" % type(probe.obj))

    key = probeables[probe.attr] if probe.attr in probeables else probe.attr
    if key is None:
        conn_probe(model, probe)
    else:
        synapse_probe(model, key, probe)

    model.probes.append(probe)

    # Simulator will fill this list with probe data during simulation
    model.params[probe] = []
Example #57
0
    def reset(self, seed=None):
        if self.closed:
            raise SimulatorClosed("Cannot reset closed Simulator.")

        if seed is not None:
            raise NotImplementedError("Seed changing not implemented")

        # reset signals
        for base in self.all_bases:
            # TODO: copy all data on at once
            if not base.readonly:
                self.all_data[self.sidx[base]] = base.initial_value

        for clra, ra in iteritems(self._raggedarrays_to_reset):
            # TODO: copy all data on at once
            for i in range(len(clra)):
                clra[i] = ra[i]

        self._reset_rngs()
        self._reset_probes()
Example #58
0
    def plan_SimProcess(self, all_ops):
        class_groups = groupby(all_ops, lambda op: type(op.process))
        plan_groups = defaultdict(list)
        step_plans = []
        for process_class, ops in class_groups:
            for cls in process_class.__mro__:
                attrname = '_plan_' + cls.__name__
                if hasattr(self, attrname):
                    plan_groups[attrname].extend(ops)
                    break
            else:
                for op in ops:
                    shape = lambda s: s.shape if s is not None else (0,)
                    fn = op.process.make_step(
                        shape(op.input), shape(op.output), self.model.dt,
                        rng=op.process.get_rng(self.rng))
                    step_plans.extend(self._plan_python_fn(
                        fn, [op.t], [op.input], [op.output]))

        process_plans = [p for attr, ops in iteritems(plan_groups)
                         for p in getattr(self, attr)(ops)]
        return process_plans + step_plans