Testing
Utility functions for evotorch-related unit testing.
TestingError
¶
assert_allclose(actual, desired, *, rtol=None, atol=None, equal_nan=True)
¶
This function is similar to numpy.testing.assert_allclose(...)
except
that atol
and rtol
are keyword-only arguments (which encourages
one to be more explicit when writing tests) and that the default dtype
is "float32" when the provided arguments are neither numpy arrays nor
torch tensors. Having "float32" as the default target dtype is a behavior
that is compatible with PyTorch.
This function first casts actual
into the dtype of desired
, then
uses numpy's assert_allclose(...)
for testing the closeness of the
values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
actual
|
Iterable
|
An iterable of numbers. |
required |
desired
|
Iterable
|
An iterable of numbers. These numbers represent the values
that we expect the |
required |
rtol
|
Optional[float]
|
Relative tolerance.
Can be left as None if only |
None
|
atol
|
Optional[float]
|
Absolute tolerance.
Can be left as None if only |
None
|
equal_nan
|
bool
|
If True, |
True
|
Source code in evotorch/testing.py
assert_almost_between(x, lb, ub, *, atol=None)
¶
Assert that the given Iterable has its values between the desired bounds.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Iterable
|
An Iterable containing numeric (float) values. |
required |
lb
|
Union[float, Iterable]
|
Lower bound for the desired interval. Can be a scalar or an iterable of values. |
required |
ub
|
Union[float, Iterable]
|
Upper bound for the desired interval. Can be a scalar or an iterable of values. |
required |
atol
|
Optional[float]
|
Absolute tolerance. If given, then the effective interval will
be |
None
|
Source code in evotorch/testing.py
assert_dtype_matches(x, dtype)
¶
Assert that the dtype of x
is compatible with the given dtype
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Iterable
|
An object with |
required |
dtype
|
Union[str, Type, dtype, dtype]
|
The dtype which |
required |
Source code in evotorch/testing.py
assert_eachclose(x, value, *, rtol=None, atol=None)
¶
Assert that the given tensor or array consists of a single value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Iterable
|
The tensor in which each value will be compared against |
required |
value
|
Any
|
A scalar |
required |
Source code in evotorch/testing.py
assert_shape_matches(x, shape)
¶
Assert that the dtype of x
matches the given shape
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Iterable
|
An object which can be converted to a PyTorch tensor. |
required |
shape
|
Union[tuple, int]
|
A tuple, or a torch.Size, or an integer. |
required |