Ejemplo n.º 1
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
Ejemplo n.º 2
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
Ejemplo n.º 3
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(sirsim.Network.context) > 0:
            warnings.warn(NotAddedToNetworkWarning(self))
Ejemplo n.º 4
0
    def learning_rule(self):
        """(LearningRule or iterable) Connectable learning rule object(s)."""
        if self.learning_rule_type is None:
            return None

        types = self.learning_rule_type
        if isinstance(types, dict):
            learning_rule = type(types)()  # dict of same type
            for k, v in iteritems(types):
                learning_rule[k] = LearningRule(self, v)
        elif is_iterable(types):
            learning_rule = [LearningRule(self, v) for v in types]
        elif isinstance(types, LearningRuleType):
            learning_rule = LearningRule(self, types)
        else:
            raise ValidationError("Invalid type %r" % type(types).__name__,
                                  attr='learning_rule_type',
                                  obj=self)

        return learning_rule
Ejemplo n.º 5
0
 def __setstate__(self, state):
     for k, v in iteritems(state):
         setattr(self, k, v)
     if len(Network.context) > 0:
         warnings.warn(NotAddedToNetworkWarning(self))
Ejemplo n.º 6
0
 def size_out(self):
     time, value = next(iteritems(self.data))
     value = np.ravel(value(time)) if callable(value) else value
     return value.size