API reference
np.testing (assertions)
NumPy's array assertion helpers. They throw AssertionError (a NativpyError subclass) with NumPy-style messages.
| Name | Summary |
|---|---|
np.testing | The numpy.testing assertion functions. |
np.testing.assertArrayEqual | assertArrayEqual: equal shapes (a 0-d side broadcasts unless strict, which also requires equal dtypes) and equal elements; NaNs and Infs must sit at the same positions. |
np.testing.assertAllclose | assertAllclose checks |actual - desired| <= atol + rtol * |desired| (np.isclose). |
np.testing.assertEqual | assertEqual compares plain objects and JS arrays recursively (key=/item= lines in the message), NDArrays with assertArrayEqual, and scalars with NaN equal to NaN and 0 different from -0. |
np.testing.assertArrayMaxUlp | Units-in-the-last-place checks for float arrays. |
np.testing.assertRaises | assertRaises calls fn(...args) and returns the error if it is an instance of ErrorClass; other errors propagate, and no error raises AssertionError("<Class> not raised by <fn>"). |
np.testing.assertWarns | Node has no Python warnings module; these watch process.emitWarning while fn runs (numera emits RuntimeWarnings there under np.seterr "warn"). |
np.testing.assertStringEqual | assertStringEqual reports a line diff (- actual, + desired). |
np.testing
#np.testing.{assertEqual, assertArrayEqual, assertAllclose, ...} · AssertionError
The numpy.testing assertion functions. A failing assertion throws AssertionError (also a named export) with NumPy's message layout: header, mismatch counts, the first mismatching indices, the largest absolute and relative differences, and the reprs of ACTUAL and DESIRED.
Returns
namespace object
Example
np.testing.assertArrayEqual([1, 2], [1, 2]); // passes
let msg = "";
try { np.testing.assertArrayEqual([1, 2, 3], [1, 2, 4]); } catch (e) { msg = e.message; }
msg.split("\n")[1]; // => "Arrays are not equal"
msg.split("\n")[3]; // => "Mismatched elements: 1 / 3 (33.3%)"
msg.split("\n")[5]; // => " [2]: 3 (ACTUAL), 4 (DESIRED)"
msg.split("\n").at(-1); // => " DESIRED: array([1, 2, 4])"np.testing.assertArrayEqual
#np.testing.assertArrayEqual(actual, desired, { errMsg?, verbose?, strict? }) · assertArrayLess(x, y, opts?) · assertArrayCompare(comparison, x, y, opts?)
assertArrayEqual: equal shapes (a 0-d side broadcasts unless strict, which also requires equal dtypes) and equal elements; NaNs and Infs must sit at the same positions. assertArrayLess checks x < y. assertArrayCompare is the shared driver: it takes any element-wise comparison returning a bool array, plus header, precision, equalNan, equalInf, names.
Parameters
actualArrayLike- An
NDArray, nested JS array or scalar. desiredArrayLike- An
NDArray, nested JS array or scalar.
Returns
void (throws AssertionError)
Example
np.testing.assertArrayEqual([1, NaN], [1, NaN]); // passes
np.testing.assertArrayLess([1, 2], [2, 3]); // passes
np.testing.assertArrayCompare((x, y) => np.lessEqual(x, y), [1, 2], [1, 3]); // passes
np.testing.assertRaises(np.testing.AssertionError, () => np.testing.assertArrayEqual([1, 2], 1, { strict: true })).message.split("\n")[3]; // => "(shapes (2,), () mismatch)"TypeScript declaration
np.testing.assertArrayEqual(actual: ArrayLike, desired: ArrayLike, opts?: AssertOptions | undefined): voidnp.testing.assertAllclose
#np.testing.assertAllclose(actual, desired, { rtol=1e-7, atol=0, equalNan=true, errMsg?, verbose?, strict? }) · assertArrayAlmostEqual(actual, desired, { decimal=6 })
assertAllclose checks |actual - desired| <= atol + rtol * |desired| (np.isclose). assertArrayAlmostEqual checks |desired - actual| < 1.5 * 10**-decimal.
Parameters
actualArrayLike- An
NDArray, nested JS array or scalar. desiredArrayLike- An
NDArray, nested JS array or scalar.
Returns
void (throws AssertionError)
Example
np.testing.assertAllclose([1, 2], [1, 2 + 1e-8]); // passes
np.testing.assertArrayAlmostEqual([1, 2], [1, 2.0000001]); // passes
np.testing.assertRaises(np.testing.AssertionError, () => np.testing.assertAllclose([1, 2], [1, 2.1])).message.split("\n")[1]; // => "Not equal to tolerance rtol=1e-07, atol=0"TypeScript declaration
np.testing.assertAllclose(actual: ArrayLike, desired: ArrayLike, opts?: AllcloseOptions | undefined): voidnp.testing.assertEqual
#np.testing.assertEqual(actual, desired, opts?) · assertAlmostEqual(actual, desired, { decimal=7 }) · assertApproxEqual(actual, desired, { significant=7 })
assertEqual compares plain objects and JS arrays recursively (key=/item= lines in the message), NDArrays with assertArrayEqual, and scalars with NaN equal to NaN and 0 different from -0. assertAlmostEqual is the scalar/array decimal check; assertApproxEqual compares scalars to a number of significant digits.
Parameters
actualArrayLike- An
NDArray, nested JS array or scalar. desiredArrayLike- An
NDArray, nested JS array or scalar.
Returns
void (throws AssertionError)
Example
np.testing.assertEqual({ a: [1, NaN] }, { a: [1, NaN] }); // passes
np.testing.assertAlmostEqual(1, 1 + 1e-8); // passes
np.testing.assertApproxEqual(1234.5, 1234.6, { significant: 4 }); // passes
np.testing.assertRaises(np.testing.AssertionError, () => np.testing.assertEqual([1, 2], [1, 3])).message; // => "\nItems are not equal:\nitem=1\n\n ACTUAL: 2\n DESIRED: 3"TypeScript declaration
np.testing.assertEqual(actual: unknown, desired: unknown, opts?: AssertOptions | undefined): voidnp.testing.assertArrayMaxUlp
#np.testing.assertArrayMaxUlp(a, b, { maxulp=1, dtype? }) · assertArrayAlmostEqualNulp(x, y, nulp=1)
Units-in-the-last-place checks for float arrays. assertArrayMaxUlp returns the ULP distances (shape [1, ...shape], common float dtype) and fails when any exceeds maxulp. assertArrayAlmostEqualNulp checks |x - y| <= nulp * spacing(max(|x|, |y|)). Complex input to assertArrayMaxUlp throws NotImplementedError.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. bArrayLike- An
NDArray, nested JS array or scalar.
Returns
NDArray / void
Example
np.testing.assertArrayMaxUlp([1], [1 + 2 ** -52]); // => [[1]]
np.testing.assertArrayAlmostEqualNulp([1], [1 + 2 ** -52]); // passes
np.testing.assertRaises(np.testing.AssertionError, () => np.testing.assertArrayMaxUlp([1], [1 + 1e-15])).message; // => "Arrays are not almost equal up to 1 ULP (max difference is 5 ULP)"TypeScript declaration
np.testing.assertArrayMaxUlp(a: ArrayLike, b: ArrayLike, opts?: MaxUlpOptions | undefined): NDArraynp.testing.assertRaises
#np.testing.assertRaises(ErrorClass, fn, ...args) · assertRaisesRegex(ErrorClass, pattern, fn, ...args) · assert_(val, msg?)
assertRaises calls fn(...args) and returns the error if it is an instance of ErrorClass; other errors propagate, and no error raises AssertionError("<Class> not raised by <fn>"). assertRaisesRegex also searches the message for pattern. assert_ throws AssertionError(msg) for a falsy value (msg may be a function). Async functions are not awaited.
Parameters
ErrorClassclass- Expected error class, e.g.
np.ValueError. fnfunction- Called with
args.
Returns
Error
Example
np.testing.assertRaises(np.ShapeError, () => np.zeros(3).reshape([2])).name; // => "ShapeError"
np.testing.assertRaisesRegex(np.ShapeError, "reshape", () => np.zeros(3).reshape([2])).name; // => "ShapeError"
np.testing.assert_(true); // passesTypeScript declaration
np.testing.assertRaises<A extends unknown[]>(errorClass: ErrorCtor, fn: (...args: A) => unknown, ...args: A): Errornp.testing.assertWarns
#np.testing.assertWarns(warningType | null, fn, ...args) · assertNoWarnings(fn, ...args)
Node has no Python warnings module; these watch process.emitWarning while fn runs (numera emits RuntimeWarnings there under np.seterr "warn"). Captured warnings are not printed. assertWarns requires at least one warning of warningType (null: any) and returns fn's result; assertNoWarnings requires none.
Parameters
warningTypestring | null- Warning type name, e.g.
"RuntimeWarning". fnfunction- Called with
args.
Returns
fn's result
Example
np.testing.assertWarns("RuntimeWarning", () => np.divide([1], [0])).dtype.name; // => "float64"
np.testing.assertNoWarnings(() => np.divide([1], [2])); // => [0.5]np.testing.assertStringEqual
#np.testing.assertStringEqual(actual, desired) · buildErrMsg(arrays, errMsg?, { header, verbose, names, precision }) · printAssertEqual(testString, actual, desired)
assertStringEqual reports a line diff (- actual, + desired). buildErrMsg builds the standard header + ACTUAL/DESIRED message. printAssertEqual deep-compares JS values.
Parameters
actualstring- Actual string.
desiredstring- Desired string.
Returns
void / string
Example
np.testing.buildErrMsg([np.array([1, 2]), 3], "msg"); // => "\nItems are not equal: msg\n ACTUAL: array([1, 2])\n DESIRED: 3"
np.testing.assertStringEqual("abc", "abc"); // passesTypeScript declaration
np.testing.assertStringEqual(actual: string, desired: string): void