snaptol.snapshot.Snapshot#

class snaptol.snapshot.Snapshot(nodeid, snapshot_file, snapshot_dir, snaptol_update=False, snapshot_found=False, show_diff=False, rtol=1e-05, atol=1e-08, equal_nan=False, cache=None, config=None)[source]#

Bases: object

Parameters:
  • nodeid (str)

  • snapshot_file (Path)

  • snapshot_dir (Path)

  • snaptol_update (bool)

  • snapshot_found (bool)

  • show_diff (bool)

  • rtol (float)

  • atol (float)

  • equal_nan (bool)

  • cache (Cache)

  • config (Config)

__init__(nodeid, snapshot_file, snapshot_dir, snaptol_update=False, snapshot_found=False, show_diff=False, rtol=1e-05, atol=1e-08, equal_nan=False, cache=None, config=None)#
Parameters:
  • nodeid (str)

  • snapshot_file (Path)

  • snapshot_dir (Path)

  • snaptol_update (bool)

  • snapshot_found (bool)

  • show_diff (bool)

  • rtol (float)

  • atol (float)

  • equal_nan (bool)

  • cache (Cache)

  • config (Config)

Return type:

None

Methods

__init__(nodeid, snapshot_file, snapshot_dir)

assert_allclose(desired[, rtol, atol, ...])

Raises an AssertionError if two objects are not equal up to desired tolerance.

assert_array_almost_equal_nulp(y[, nulp])

Compare two arrays relatively to their spacing.

assert_array_equal(desired[, err_msg, ...])

Raises an AssertionError if two array_like objects are not equal.

assert_array_max_ulp(b[, maxulp, dtype])

Check that all items of arrays differ in at most N Units in the Last Place.

assert_equal(desired[, err_msg, verbose, strict])

Raises an AssertionError if two objects are not equal.

assert_string_equal(desired)

Test if two strings are equal.

from_request(request)

Create a Snapshot instance from a pytest request object.

match(value, *[, rtol, atol])

Compare a value with the stored snapshot.

matches(*args, **kwargs)

Alias for match() method.

Attributes

assert_allclose(desired, rtol=1e-07, atol=0, equal_nan=True, err_msg='', verbose=True, *, strict=False)#

Raises an AssertionError if two objects are not equal up to desired tolerance.

Given two array_like objects, check that their shapes and all elements are equal (but see the Notes for the special handling of a scalar). An exception is raised if the shapes mismatch or any values conflict. In contrast to the standard usage in numpy, NaNs are compared like numbers, no assertion is raised if both objects have NaNs in the same positions.

The test is equivalent to allclose(actual, desired, rtol, atol), except that it is stricter: it doesn’t broadcast its operands, and has tighter default tolerance values. It compares the difference between actual and desired to atol + rtol * abs(desired).

Parameters:
  • actual (array_like) – Array obtained.

  • desired (array_like) – Array desired.

  • rtol (float, optional) – Relative tolerance.

  • atol (float, optional) – Absolute tolerance.

  • equal_nan (bool, optional.) – If True, NaNs will compare equal.

  • err_msg (str, optional) – The error message to be printed in case of failure.

  • verbose (bool, optional) – If True, the conflicting values are appended to the error message.

  • strict (bool, optional) –

    If True, raise an AssertionError when either the shape or the data type of the arguments does not match. The special handling of scalars mentioned in the Notes section is disabled.

    Added in version 2.0.0.

Raises:

AssertionError – If actual and desired are not equal up to specified precision.

Notes

When one of actual and desired is a scalar and the other is array_like, the function performs the comparison as if the scalar were broadcasted to the shape of the array. Note that empty arrays are therefore considered equal to scalars. This behaviour can be disabled by setting strict==True.

Examples

>>> x = [1e-5, 1e-3, 1e-1]
>>> y = np.arccos(np.cos(x))
>>> np.testing.assert_allclose(x, y, rtol=1e-5, atol=0)

As mentioned in the Notes section, assert_allclose has special handling for scalars. Here, the test checks that the value of numpy.sin is nearly zero at integer multiples of π.

>>> x = np.arange(3) * np.pi
>>> np.testing.assert_allclose(np.sin(x), 0, atol=1e-15)

Use strict to raise an AssertionError when comparing an array with one or more dimensions against a scalar.

>>> np.testing.assert_allclose(np.sin(x), 0, atol=1e-15, strict=True)
Traceback (most recent call last):
    ...
AssertionError:
Not equal to tolerance rtol=1e-07, atol=1e-15

(shapes (3,), () mismatch)
 ACTUAL: array([ 0.000000e+00,  1.224647e-16, -2.449294e-16])
 DESIRED: array(0)

The strict parameter also ensures that the array data types match:

>>> y = np.zeros(3, dtype=np.float32)
>>> np.testing.assert_allclose(np.sin(x), y, atol=1e-15, strict=True)
Traceback (most recent call last):
    ...
AssertionError:
Not equal to tolerance rtol=1e-07, atol=1e-15

(dtypes float64, float32 mismatch)
 ACTUAL: array([ 0.000000e+00,  1.224647e-16, -2.449294e-16])
 DESIRED: array([0., 0., 0.], dtype=float32)
assert_array_almost_equal_nulp(y, nulp=1)#

Compare two arrays relatively to their spacing.

This is a relatively robust method to compare two arrays whose amplitude is variable.

Parameters:
  • x (array_like) – Input arrays.

  • y (array_like) – Input arrays.

  • nulp (int, optional) – The maximum number of unit in the last place for tolerance (see Notes). Default is 1.

Return type:

None

Raises:

AssertionError – If the spacing between x and y for one or more elements is larger than nulp.

See also

assert_array_max_ulp

Check that all items of arrays differ in at most N Units in the Last Place.

spacing

Return the distance between x and the nearest adjacent number.

Notes

An assertion is raised if the following condition is not met:

abs(x - y) <= nulp * spacing(maximum(abs(x), abs(y)))

Examples

>>> x = np.array([1., 1e-10, 1e-20])
>>> eps = np.finfo(x.dtype).eps
>>> np.testing.assert_array_almost_equal_nulp(x, x*eps/2 + x)
>>> np.testing.assert_array_almost_equal_nulp(x, x*eps + x)
Traceback (most recent call last):
  ...
AssertionError: Arrays are not equal to 1 ULP (max is 2)
assert_array_equal(desired, err_msg='', verbose=True, *, strict=False)#

Raises an AssertionError if two array_like objects are not equal.

Given two array_like objects, check that the shape is equal and all elements of these objects are equal (but see the Notes for the special handling of a scalar). An exception is raised at shape mismatch or conflicting values. In contrast to the standard usage in numpy, NaNs are compared like numbers, no assertion is raised if both objects have NaNs in the same positions.

The usual caution for verifying equality with floating point numbers is advised.

Note

When either actual or desired is already an instance of numpy.ndarray and desired is not a dict, the behavior of assert_equal(actual, desired) is identical to the behavior of this function. Otherwise, this function performs np.asanyarray on the inputs before comparison, whereas assert_equal defines special comparison rules for common Python types. For example, only assert_equal can be used to compare nested Python lists. In new code, consider using only assert_equal, explicitly converting either actual or desired to arrays if the behavior of assert_array_equal is desired.

Parameters:
  • actual (array_like) – The actual object to check.

  • desired (array_like) – The desired, expected object.

  • err_msg (str, optional) – The error message to be printed in case of failure.

  • verbose (bool, optional) – If True, the conflicting values are appended to the error message.

  • strict (bool, optional) –

    If True, raise an AssertionError when either the shape or the data type of the array_like objects does not match. The special handling for scalars mentioned in the Notes section is disabled.

    Added in version 1.24.0.

Raises:

AssertionError – If actual and desired objects are not equal.

See also

assert_allclose

Compare two array_like objects for equality with desired relative and/or absolute precision.

assert_array_almost_equal_nulp, assert_array_max_ulp, assert_equal

Notes

When one of actual and desired is a scalar and the other is array_like, the function checks that each element of the array_like is equal to the scalar. Note that empty arrays are therefore considered equal to scalars. This behaviour can be disabled by setting strict==True.

Examples

The first assert does not raise an exception:

>>> np.testing.assert_array_equal([1.0,2.33333,np.nan],
...                               [np.exp(0),2.33333, np.nan])

Assert fails with numerical imprecision with floats:

>>> np.testing.assert_array_equal([1.0,np.pi,np.nan],
...                               [1, np.sqrt(np.pi)**2, np.nan])
Traceback (most recent call last):
    ...
AssertionError:
Arrays are not equal

Mismatched elements: 1 / 3 (33.3%)
Mismatch at index:
 [1]: 3.141592653589793 (ACTUAL), 3.1415926535897927 (DESIRED)
Max absolute difference among violations: 4.4408921e-16
Max relative difference among violations: 1.41357986e-16
 ACTUAL: array([1.      , 3.141593,      nan])
 DESIRED: array([1.      , 3.141593,      nan])

Use assert_allclose or one of the nulp (number of floating point values) functions for these cases instead:

>>> np.testing.assert_allclose([1.0,np.pi,np.nan],
...                            [1, np.sqrt(np.pi)**2, np.nan],
...                            rtol=1e-10, atol=0)

As mentioned in the Notes section, assert_array_equal has special handling for scalars. Here the test checks that each value in x is 3:

>>> x = np.full((2, 5), fill_value=3)
>>> np.testing.assert_array_equal(x, 3)

Use strict to raise an AssertionError when comparing a scalar with an array:

>>> np.testing.assert_array_equal(x, 3, strict=True)
Traceback (most recent call last):
    ...
AssertionError:
Arrays are not equal

(shapes (2, 5), () mismatch)
 ACTUAL: array([[3, 3, 3, 3, 3],
       [3, 3, 3, 3, 3]])
 DESIRED: array(3)

The strict parameter also ensures that the array data types match:

>>> x = np.array([2, 2, 2])
>>> y = np.array([2., 2., 2.], dtype=np.float32)
>>> np.testing.assert_array_equal(x, y, strict=True)
Traceback (most recent call last):
    ...
AssertionError:
Arrays are not equal

(dtypes int64, float32 mismatch)
 ACTUAL: array([2, 2, 2])
 DESIRED: array([2., 2., 2.], dtype=float32)
assert_array_max_ulp(b, maxulp=1, dtype=None)#

Check that all items of arrays differ in at most N Units in the Last Place.

Parameters:
  • a (array_like) – Input arrays to be compared.

  • b (array_like) – Input arrays to be compared.

  • maxulp (int, optional) – The maximum number of units in the last place that elements of a and b can differ. Default is 1.

  • dtype (dtype, optional) – Data-type to convert a and b to if given. Default is None.

Returns:

ret – Array containing number of representable floating point numbers between items in a and b.

Return type:

ndarray

Raises:

AssertionError – If one or more elements differ by more than maxulp.

Notes

For computing the ULP difference, this API does not differentiate between various representations of NAN (ULP difference between 0x7fc00000 and 0xffc00000 is zero).

See also

assert_array_almost_equal_nulp

Compare two arrays relatively to their spacing.

Examples

>>> a = np.linspace(0., 1., 100)
>>> res = np.testing.assert_array_max_ulp(a, np.arcsin(np.sin(a)))
assert_equal(desired, err_msg='', verbose=True, *, strict=False)#

Raises an AssertionError if two objects are not equal.

Given two objects (scalars, lists, tuples, dictionaries or numpy arrays), check that all elements of these objects are equal. An exception is raised at the first conflicting values.

This function handles NaN comparisons as if NaN was a “normal” number. That is, AssertionError is not raised if both objects have NaNs in the same positions. This is in contrast to the IEEE standard on NaNs, which says that NaN compared to anything must return False.

Parameters:
  • actual (array_like) – The object to check.

  • desired (array_like) – The expected object.

  • err_msg (str, optional) – The error message to be printed in case of failure.

  • verbose (bool, optional) – If True, the conflicting values are appended to the error message.

  • strict (bool, optional) –

    If True and either of the actual and desired arguments is an array, raise an AssertionError when either the shape or the data type of the arguments does not match. If neither argument is an array, this parameter has no effect.

    Added in version 2.0.0.

Raises:

AssertionError – If actual and desired are not equal.

Notes

When one of actual and desired is a scalar and the other is array_like, the function checks that each element of the array_like is equal to the scalar. Note that empty arrays are therefore considered equal to scalars. This behaviour can be disabled by setting strict==True.

Examples

>>> np.testing.assert_equal([4, 5], [4, 6])
Traceback (most recent call last):
    ...
AssertionError:
Items are not equal:
item=1
 ACTUAL: 5
 DESIRED: 6

The following comparison does not raise an exception. There are NaNs in the inputs, but they are in the same positions.

>>> np.testing.assert_equal(np.array([1.0, 2.0, np.nan]), [1, 2, np.nan])

As mentioned in the Notes section, assert_equal has special handling for scalars when one of the arguments is an array. Here, the test checks that each value in x is 3:

>>> x = np.full((2, 5), fill_value=3)
>>> np.testing.assert_equal(x, 3)

Use strict to raise an AssertionError when comparing a scalar with an array of a different shape:

>>> np.testing.assert_equal(x, 3, strict=True)
Traceback (most recent call last):
    ...
AssertionError:
Arrays are not equal

(shapes (2, 5), () mismatch)
 ACTUAL: array([[3, 3, 3, 3, 3],
       [3, 3, 3, 3, 3]])
 DESIRED: array(3)

The strict parameter also ensures that the array data types match:

>>> x = np.array([2, 2, 2])
>>> y = np.array([2., 2., 2.], dtype=np.float32)
>>> np.testing.assert_equal(x, y, strict=True)
Traceback (most recent call last):
    ...
AssertionError:
Arrays are not equal

(dtypes int64, float32 mismatch)
 ACTUAL: array([2, 2, 2])
 DESIRED: array([2., 2., 2.], dtype=float32)
assert_string_equal(desired)#

Test if two strings are equal.

If the given strings are equal, assert_string_equal does nothing. If they are not equal, an AssertionError is raised, and the diff between the strings is shown.

Parameters:
  • actual (str) – The string to test for equality against the expected string.

  • desired (str) – The expected string.

Examples

>>> np.testing.assert_string_equal('abc', 'abc')
>>> np.testing.assert_string_equal('abc', 'abcd')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
...
AssertionError: Differences in strings:
- abc+ abcd?    +
atol: float = 1e-08#
cache: Cache = None#
config: Config = None#
equal_nan: bool = False#
expected: Any#
classmethod from_request(request)[source]#

Create a Snapshot instance from a pytest request object. Returns the instansiated Snapshot object.

Parameters:

request – The pytest request fixture containing test information.

Return type:

Snapshot

match(value, *, rtol=1e-05, atol=1e-08)[source]#

Compare a value with the stored snapshot. Returns True if the values match, False otherwise.

Parameters:
  • value – The value to compare with the snapshot.

  • rtol (float) – Relative tolerance for comparison.

  • atol (float) – Absolute tolerance for comparison.

Return type:

bool

matches(*args, **kwargs)[source]#

Alias for match() method. Compare a value with the stored snapshot.

Return type:

bool

nodeid: str#
rtol: float = 1e-05#
show_diff: bool = False#
snapshot_dir: Path#
snapshot_file: Path#
snapshot_found: bool = False#
snaptol_update: bool = False#