def determine_size(self, instance, function): args = self.function_args(instance, function) value, invoked = checked_call(function, *args) if not invoked: raise TypeError("function '%s' must accept a single " "np.array argument" % function) return np.asarray(value).size
def coerce_callable(self, obj, fn): t = 0.0 if obj.input_vocab is not None: args = ( t, SemanticPointer( np.zeros(obj.input_vocab.dimensions), vocab=obj.input_vocab ), ) elif obj.size_in is not None: args = (t, np.zeros(obj.size_in)) else: args = (t,) _, invoked = checked_call(fn, *args) fn(*args) if not invoked: if obj.input_vocab is not None: raise ValidationError( "Transcode function %r is expected to accept exactly 2 " "arguments: time as a float, and a SemanticPointer", attr=self.name, obj=obj, ) else: raise ValidationError( "Transcode function %r is expected to accept exactly 1 or " "2 arguments: time as a float, and optionally the input " "data as NumPy array.", attr=self.name, obj=obj, ) return fn
def determine_size(self, instance, function): args = self.function_args(instance, function) value, invoked = checked_call(function, *args) if not invoked: raise ValidationError( "function '%s' must accept a single np.array argument" % function, attr=self.name, obj=instance, ) return np.asarray(value).size
def validate_callable(self, node, output): t, x = np.asarray(0.0), np.zeros(node.size_in) args = (t, x) if node.size_in > 0 else (t,) result, invoked = checked_call(output, *args) if not invoked: raise TypeError("output function '%s' must accept %d argument%s" % (output, len(args), 's' if len(args) > 1 else '')) if result is not None: result = np.asarray(result) if len(result.shape) > 1: raise ValueError("Node output must be a vector (got shape %s)" % (result.shape,)) return result
def check_callable_output(self, node, output): t, x = 0.0, np.zeros(node.size_in) args = (t, x) if node.size_in > 0 else (t,) result, invoked = checked_call(output, *args) if not invoked: raise self._fn_args_validation_error(output, self.name, node) if result is not None: result = np.asarray(result) if len(result.shape) > 1: raise ValidationError("Node output must be a vector " "(got shape %s)" % (result.shape,), attr=self.name, obj=node) # return callable output size return 0 if result is None else result.size
def validate_callable(self, node, output): t, x = np.asarray(0.0), np.zeros(node.size_in) args = (t, x) if node.size_in > 0 else (t, ) result, invoked = checked_call(output, *args) if not invoked: raise TypeError("output function '%s' must accept %d argument%s" % (output, len(args), 's' if len(args) > 1 else '')) if result is not None: result = np.asarray(result) if len(result.shape) > 1: raise ValueError( "Node output must be a vector (got shape %s)" % (result.shape, )) return result
def validate_callable(self, node, output): t, x = 0.0, np.zeros(node.size_in) args = (t, x) if node.size_in > 0 else (t,) result, invoked = checked_call(output, *args) if not invoked: msg = "output function '%s' is expected to accept exactly " "%d argument" % (output, len(args)) msg += " (time, as a float)" if len(args) == 1 else "s (time, as a float and data, as a NumPy array)" raise ValidationError(msg, attr=self.name, obj=node) if result is not None: result = np.asarray(result) if len(result.shape) > 1: raise ValidationError( "Node output must be a vector (got shape" " %s)" % (result.shape,), attr=self.name, obj=node ) return result
def validate_callable(self, node, output): t, x = np.asarray(0.0), np.zeros(node.size_in) args = (t, x) if node.size_in > 0 else (t,) result, invoked = checked_call(output, *args) if not invoked: msg = ("output function '%s' is expected to accept exactly " "%d argument" % (output, len(args))) msg += (' (time, as a float)' if len(args) == 1 else 's (time, as a float and data, as a NumPy array)') raise TypeError(msg) if result is not None: result = np.asarray(result) if len(result.shape) > 1: raise ValueError("Node output must be a vector (got shape %s)" % (result.shape,)) return result
def validate_callable(self, node, output): t, x = 0.0, np.zeros(node.size_in) args = (t, x) if node.size_in > 0 else (t,) result, invoked = checked_call(output, *args) if not invoked: msg = ("output function '%s' is expected to accept exactly " "%d argument" % (output, len(args))) msg += (' (time, as a float)' if len(args) == 1 else 's (time, as a float and data, as a NumPy array)') raise TypeError(msg) if result is not None: result = np.asarray(result) if len(result.shape) > 1: raise ValueError("Node output must be a vector (got shape %s)" % (result.shape,)) return result
def test_checked_call_errors(): class A(object): def __call__(self, a): raise NotImplementedError() assert checked_call(A()) == (None, False) with pytest.raises(NotImplementedError): checked_call(A(), 1) assert checked_call(np.sin, 1, 2, 3) == (None, False) with pytest.raises(ValueError): checked_call(lambda x: np.sin(1, 2, 3), 1)
def test_checked_call(): def func1(a): return a def func2(a, b=0, **kwargs): return a+b def func3(a, b=0, c=0, *args, **kwargs): return a+b+c+sum(args) func4 = lambda x=[0]: sum(x) class A(object): def __call__(self, a, b): return a + b assert checked_call(func1) == (None, False) assert checked_call(func1, 1) == (1, True) assert checked_call(func1, 1, 2) == (None, False) assert checked_call(func1, 1, two=2) == (None, False) assert checked_call(func2) == (None, False) assert checked_call(func2, 1) == (1, True) assert checked_call(func2, 1, 2) == (3, True) assert checked_call(func2, 1, 2, three=3) == (3, True) assert checked_call(func2, 1, 2, 3) == (None, False) assert checked_call(func3) == (None, False) assert checked_call(func3, 1) == (1, True) assert checked_call(func3, 1, 2) == (3, True) assert checked_call(func3, 1, 2, 3) == (6, True) assert checked_call(func3, 1, 2, 3, 4, 5, 6) == (21, True) assert checked_call(func4) == (0, True) assert checked_call(func4, [1, 2]) == (3, True) assert checked_call(func4, [1], 2) == (None, False) assert checked_call(A(), 1) == (None, False) assert checked_call(A(), 1, 2) == (3, True) assert checked_call(A(), 1, 2, 3) == (None, False) assert checked_call(np.sin) == (None, False) assert checked_call(np.sin, 0) == (0, True) assert checked_call(np.sin, 0, np.array([1])) == (np.array([0]), True) assert checked_call(np.sin, 0, np.array([1]), 1) == (None, False)
def test_checked_call(): def func1(a): return a def func2(a, b=0, **kwargs): return a + b def func3(a, b=0, c=0, *args, **kwargs): return a + b + c + sum(args) func4 = lambda x=[0]: sum(x) class A(object): def __call__(self, a, b): return a + b assert checked_call(func1) == (None, False) assert checked_call(func1, 1) == (1, True) assert checked_call(func1, 1, 2) == (None, False) assert checked_call(func1, 1, two=2) == (None, False) assert checked_call(func2) == (None, False) assert checked_call(func2, 1) == (1, True) assert checked_call(func2, 1, 2) == (3, True) assert checked_call(func2, 1, 2, three=3) == (3, True) assert checked_call(func2, 1, 2, 3) == (None, False) assert checked_call(func3) == (None, False) assert checked_call(func3, 1) == (1, True) assert checked_call(func3, 1, 2) == (3, True) assert checked_call(func3, 1, 2, 3) == (6, True) assert checked_call(func3, 1, 2, 3, 4, 5, 6) == (21, True) assert checked_call(func4) == (0, True) assert checked_call(func4, [1, 2]) == (3, True) assert checked_call(func4, [1], 2) == (None, False) assert checked_call(A(), 1) == (None, False) assert checked_call(A(), 1, 2) == (3, True) assert checked_call(A(), 1, 2, 3) == (None, False) assert checked_call(np.sin) == (None, False) assert checked_call(np.sin, 0) == (0, True) assert checked_call(np.sin, 0, np.array([1.])) == (np.array([0.]), True) assert checked_call(np.sin, 0, np.array([1.]), 1) == (None, False)