Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unittest - how to assert if the two possibly NaN values are equal

In my test case, I assume that if two values are NaN then they are equal. What is the way to express it using unittest assertions? The two common functions presented below are not handling this case.

v1 = np.nan
v2 = np.nan
self.assertEquals(v1, v2)
self.assertTrue(v1 == v2)

A solution that is working for me right now is using a boolean expression inside assertTrue:

self.assertTrue(v1 == v2 or (np.isnan(v1) and np.isnan(v2))
like image 302
Krzysztof Słowiński Avatar asked Aug 07 '18 13:08

Krzysztof Słowiński


4 Answers

You could use:

numpy.testing.assert_equal(v1, v2)

From docs:

This function handles NaN comparisons as if NaN was a “normal” number. That is, no assertion is 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.

It throws AssertionError when the values are not equal and it should work fine with pytest, but it may not be a good fit for unittest tests.

Another option is:

numpy.isclose(v1, v2, equal_nan=True)

but obviously it's a replacement for math.isclose, not for ==.

like image 183
marcin Avatar answered Nov 08 '22 15:11

marcin


It does not make sense to check two NaNs for equality. The best you could do is to check it in a different way, for example:

check = numpy.isnan(v1) and numpy.isnan(v2)
self.assertTrue(check)
like image 4
toti08 Avatar answered Nov 08 '22 15:11

toti08


You can use math.isnan

self.assertTrue(math.isnan(v1) and math.isnan(v2))
like image 2
haklir Avatar answered Nov 08 '22 15:11

haklir


You could check if each of them is NaN separately. To do that, I suggest using the following class:

import math


class NumericAssertions:
    """
    This class is following the UnitTest naming conventions.
    It is meant to be used along with unittest.TestCase like so :
    class MyTest(unittest.TestCase, NumericAssertions):
        ...
    It needs python >= 2.6
    """

    def assertIsNaN(self, value, msg=None):
        """Fail if provided value is not NaN"""
        standardMsg = "%s is not NaN" % str(value)
        try:
            if not math.isnan(value):
                self.fail(self._formatMessage(msg, standardMsg))
        except:
            self.fail(self._formatMessage(msg, standardMsg))

    def assertIsNotNaN(self, value, msg=None):
        """Fail if provided value is NaN"""
        standardMsg = "Provided value is NaN"
        try:
            if math.isnan(value):
                self.fail(self._formatMessage(msg, standardMsg))
        except:
            pass

It would be as easy as:

v1 = np.nan
v2 = np.nan
self.assertIsNaN(v1)
self.assertIsNaN(v2)
like image 1
ibarrond Avatar answered Nov 08 '22 13:11

ibarrond