[Numpy-discussion] numpy and unittests

Robert Kern robert.kern at gmail.com
Mon Aug 7 13:04:24 CDT 2006


Christian Meesters wrote:
> Hi,
> 
> I used to work with some unittest scripts for a bigger project of mine. Now 
> that I started the project again the tests don't work anymore, using numpy 
> version '0.9.5.2100' .
> 
> The errors I get look are like this:
> 
> ERROR: _normalize() should return dataset scaled between 0 and 1
> ----------------------------------------------------------------------
> Traceback (most recent call last):
>   File "testingSAXS.py", line 265, in testNormalization
>     
> self.assertEqual(self.test1._normalize(minimum=0.0,maximum=1.0),self.test5)
>   File "/usr/lib64/python2.4/unittest.py", line 332, in failUnlessEqual
>     if not first == second:
>   File 
> "/home/cm/Documents/Informatics/Python/python_programming/biophysics/SAXS/lib/Data.py", 
> line 174, in __eq__
>     if self.intensity == other.intensity:
> ValueError: The truth value of an array with more than one element is 
> ambiguous. Use a.any() or a.all()
> 
> The 'self.intensity' objects are 1D-arrays containing integers <= 1E6.
> 
> The unittest script looks like:
> 
> if __name__=='__main__':
>     from Data import *
>     from Utils import *
>     import unittest
> 
> <snip>
>     def test__eq__(self):
>             """__eq__ should return True with identical array data"""
>             self.assert_(self.test1 == self.test2)
> <snip>
>     suite = unittest.TestSuite()
>     suite.addTest(unittest.makeSuite(Test_SAXS_Sanity))
> <snip>
>     unittest.TextTestRunner(verbosity=1).run(suite) 
> 
> Any ideas what I have to change? (Possibly trivial, but I have no clue.)

self.assert_((self.test1 == self.test2).all())

I'm afraid that your test was always broken. Numeric used the convention that if 
*any* value in a boolean array was True, then the array would evaluate to True 
when used as a truth value in an if: clause. However, you almost certainly 
wanted to test that *all* of the values were True. This is why we now raise an 
exception; lots of people got tripped up over that.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco





More information about the Numpy-discussion mailing list