2

I have the the following question.

I have these function:

def string_to_2Darray(flat_string):

    """converts a string of type '0,1,0,1,1,1,0,1,0'"""

    array1d = np.fromstring(flat_string, dtype=int, sep=',')
    return np.reshape(array1d, (-1,3)) 

and I wrote a unittest Class for this function which goes like that:

class StringTo2DArray(unittest.TestCase):

    def test_string_2DArray(self):
        string_example_0 = '0,1,0,1,1,1,0,1,0'
        array_example_0 = string_to_2Darray(string_example_0)
        print(array_example_0)
        print(type(array_example_0))
        self.assertEqual([[0,1,0],[1,1,1],[0,1,0]], array_example_0)

See that I am adding some print statements within the body of the test_string_2Darray module within the StringTo2DArray class in the unittest.

When I run python -m unittest then I get the following Error message:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I don't know why it happens since the string is correctly transformed to 2D numpy array and does not match the array [[0,1,0],[1,1,1],[0,1,0]] that I passed in the assert. Equal for my test.

1

3 Answers 3

3

You can check if two numpy arrays are equal using functions from the numpy.testing suite.

In your case, you could replace

self.assertEqual([[0,1,0],[1,1,1],[0,1,0]], array_example_0)

with

numpy.testing.assert_array_equal([[0,1,0],[1,1,1],[0,1,0]], array_example_0)
Sign up to request clarification or add additional context in comments.

Comments

1

@Roger Almengor,

As @lllllIIIIll replied, it's a known bug in Python's TestCase implementation. The assertEqual(a, b) implemenation assumes the a==b operation will return one True or one False, but if it returns a composited value which including multiple boolean values, it'll raise this error. E.g, if the a, b are numpy's data array or Tensorflow's tensor, this bug will be triggered.

There are two solutions to work around this bug:

  1. Converting the numpy data arry or tensors to a list, because a_list==b_list will return one boolean value, so TestCase assertEqual() could handle it correctly.
  2. Using numpy or tesorflow's customized version TestCase implementation, they have their own versions' for assertEqual() function implenentation for numpy array and tensors, e.g. assert_array_equal()# In numpy, or assertAllEqual() ## in Tensorflow.test.TestCase

1 Comment

I like to use tensorflow.test.TestCase' assertAllEqual(), assertAllClose(), assertAllXXXXX() functions to handle both numpy's data array and tensor's value comparison work. tensorflow.org/api_docs/python/tf/test/TestCase#assertAllEqual
1

In addition, 1d arrays can be asserted by assertCountEqual.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.