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.