[Numpy-discussion] using assertEqual in unittest to test two np.ndarray?
josef.pktd@gmai...
josef.pktd@gmai...
Fri Mar 20 16:03:40 CDT 2009
On Fri, Mar 20, 2009 at 4:49 PM, Pierre GM <pgmdevlist@gmail.com> wrote:
>
> On Mar 20, 2009, at 4:39 PM, Christopher Barker wrote:
>
>> Grissiom wrote:
>>> I know I should use array_equal to test two arrays
>>
>> Not answering your question, but I hadn't known about array_equal, so
>> when I saw this, I thought: great! I can get rid of a bunch of ugly
>> code
>> in my tests. However, it doesn't work as I would like for NaNs:
>
> Chris, have you tried assert_equal redefined in numpy.ma.testutils ?
> It was deisgned for masked arrays, but automatically deals with NaNs
> >>> from numpy.ma.testutils import assert_equal
> >>> a = np.array([1,2,np.nan,4])
> >>> assert_equal(a,a)
for testing purposes it is available in numpy testing:
from numpy.testing import assert_equal, assert_almost_equal, assert_array_equal
>>> a = np.array([ 1., 2., np.NaN, 4.])
>>> assert_array_equal(a,a)
does not raise AssertionError
>>> assert_array_equal(a,a+1)
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
assert_array_equal(a,a+1)
File "C:\Programs\Python25\lib\site-packages\numpy\testing\utils.py",
line 303, in assert_array_equal
verbose=verbose, header='Arrays are not equal')
File "C:\Programs\Python25\lib\site-packages\numpy\testing\utils.py",
line 295, in assert_array_compare
raise AssertionError(msg)
AssertionError:
Arrays are not equal
(mismatch 100.0%)
x: array([ 1., 2., NaN, 4.])
y: array([ 2., 3., NaN, 5.])
More information about the Numpy-discussion
mailing list