Numeric array in unittest problem

A

ajikoe

hello,

I found that if I use Numeric.array into unittest it is not
consistance,
Is that normal ?

import Numeric
class myTest(unittest.TestCase):
def runTest(self):
a = Numeric.array([1,2])
b = Numeric.array([1,33])
self.assertEqual(a, b)
pass


This will not raise any error ???

Any idea?

Sincerely Yours,
pujo
 
P

Peter Hansen

hello,

I found that if I use Numeric.array into unittest it is not
consistance,
Is that normal ?

import Numeric
class myTest(unittest.TestCase):
def runTest(self):
a = Numeric.array([1,2])
b = Numeric.array([1,33])
self.assertEqual(a, b)
pass


This will not raise any error ???

Code that doesn't execute at all generally raises no errors...

-Peter
 
A

ajikoe

Sorry Peter,

Try this....

import unittest
import Numeric

class myTest(unittest.TestCase):
def runTest(self):
var1 = Numeric.array([1,22])
var2 = Numeric.array([1,33])
self.assertEqual(var1,var2)

if __name__ == '__main__':
unittest.main()


pujo
 
A

Alex Martelli

Sorry Peter,

Try this....

import unittest
import Numeric

class myTest(unittest.TestCase):
def runTest(self):
var1 = Numeric.array([1,22])
var2 = Numeric.array([1,33])
self.assertEqual(var1,var2)

if __name__ == '__main__':
unittest.main()

Try this interactively and you'll see why:
import Numeric
a=Numeric.array([1,22])
b=Numeric.array([1,33])
c = a==b
c array([1, 0])
assert(c)

i.e., thanks to element-by-element evaluation, == will generally return
a true value for ANY comparison of Numeric arrays, causing a very
frequent beginner's bug to be sure. Try Numeric.alltrue(c), or
Numeric.allclose(a,b) ...


Alex
 
R

Robert Kern

hello,

I found that if I use Numeric.array into unittest it is not
consistance,
Is that normal ?

import Numeric
class myTest(unittest.TestCase):
def runTest(self):
a = Numeric.array([1,2])
b = Numeric.array([1,33])
self.assertEqual(a, b)
pass


This will not raise any error ???

Any idea?

unittest.TestCase.assertEqual() uses == to compare a and b. Numeric
arrays have rich comparisons and so return arrays, not booleans. Try it
in the interpreter. To get a boolean from a==b, use Numeric.alltrue(a==b).

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
R

Robert Kern

Alex said:
import Numeric
a=Numeric.array([1,22])
b=Numeric.array([1,33])
c = a==b
c

array([1, 0])

i.e., thanks to element-by-element evaluation, == will generally return
a true value for ANY comparison of Numeric arrays, causing a very
frequent beginner's bug to be sure.

Indeed. This is why numarray and scipy_core have made arrays raise an
exception when someone tries to use them as truth values.

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
R

Roman Bertle

* Alex Martelli said:
Sorry Peter,

Try this....

import unittest
import Numeric

class myTest(unittest.TestCase):
def runTest(self):
var1 = Numeric.array([1,22])
var2 = Numeric.array([1,33])
self.assertEqual(var1,var2)

if __name__ == '__main__':
unittest.main()


i.e., thanks to element-by-element evaluation, == will generally return
a true value for ANY comparison of Numeric arrays, causing a very
frequent beginner's bug to be sure. Try Numeric.alltrue(c), or
Numeric.allclose(a,b) ...

I extend unittest.TestCase as follows (uses numarray, not Numeric):


class NumTestCase(unittest.TestCase):

"""Extends TestCase with equality tests for numarrays.
"""

def numAssertEqual(self, a1, a2):
"""Test for equality of numarray fields a1 and a2.
"""
self.assertEqual(a1.shape, a2.shape)
self.assertEqual(a1.type(), a2.type())
self.assertTrue(N.alltrue(N.equal(a1.flat, a2.flat)))

def numAssertAlmostEqual(self, a1, a2):
"""Test for approximately equality of numarray fields a1 and a2.
"""
self.assertEqual(a1.shape, a2.shape)
self.assertEqual(a1.type(), a2.type())
if a1.type() == 'Float64' or a1.type() == 'Complex64':
prec = 15
else:
prec = 7
if isinstance(a1.type(), N.ComplexType):
af1, af2 = a1.flat.real, a2.flat.real
for ind in xrange(af1.nelements()):
self.assertAlmostEqual(af1[ind], af2[ind], prec)
af1, af2 = a1.flat.imag, a2.flat.imag
for ind in xrange(af1.nelements()):
self.assertAlmostEqual(af1[ind], af2[ind], prec)
else:
af1, af2 = a1.flat, a2.flat
for ind in xrange(af1.nelements()):
self.assertAlmostEqual(af1[ind], af2[ind], prec)
 
P

Peter Hansen

Sorry Peter,
Try this....

import unittest
import Numeric

class myTest(unittest.TestCase):
def runTest(self):
var1 = Numeric.array([1,22])
var2 = Numeric.array([1,33])
self.assertEqual(var1,var2)

if __name__ == '__main__':
unittest.main()

My apologies, as I thought I was pointing out an obvious error, but it
turns out I was totally wrong about it.

My own use of module unittest has always involved defining methods whose
names start with "test", as in "def test01(self):" and "def
test_this(self):" and so forth.

I had no idea that there was a method runTest() that you could override,
so I was trying to point out that the test case wasn't even executing --
though clearly it was!

(Try defining even a single method starting with "test" in addition to
the runTest() method you have above, and you'll see that runTest() stops
executing... but obviously this isn't your problem.)

-Peter
 
A

ajikoe

Thanks all,

I will use alltrue and allclose as Alex and Robert point out..

Cheers,
pujo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top