Home > other >  how do print the full array when using np.testing with 2D arrays?
how do print the full array when using np.testing with 2D arrays?

Time:06-13

I am using assertions like np.testing.assert_all_equal to check whether arrays are as expected. However, for 2D arrays I only see up to 3 elements of axis 0. e.g.:

import numpy as np

x1 = np.expand_dims(np.arange(10), 1)
x2 = np.expand_dims(np.arange(10), 1)
x2[8] = 0

np.testing.assert_almost_equal(x1, x2)

This results in:

AssertionError: 
Arrays are not almost equal to 7 decimals

Mismatched elements: 1 / 10 (10%)
Max absolute difference: 8
Max relative difference: 0.
 x: array([[0],
       [1],
       [2],...
 y: array([[0],
       [1],
       [2],...

This is less than helpful since I can't see where the mismatch is. I've tried using np.set_printoptions(edgeitems=10) to no avail.

How can I set it up so that my asserts print the full contents of the arrays on failure?

CodePudding user response:

I guess you can't do anything about it as the maximum of 3 elements is hard-coded in numpy.testing._private.utils.build_err_msg:

        if r.count('\n') > 3:
            r = '\n'.join(r.splitlines()[:3])
            r  = '...'

(unless you mock out r.count())

  • Related