Re: Numeric array in unittest problem

2005-11-22 Thread [EMAIL PROTECTED]
Thanks all,

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

Cheers,
pujo

-- 
http://mail.python.org/mailman/listinfo/python-list


Numeric array in unittest problem

2005-11-21 Thread [EMAIL PROTECTED]
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Numeric array in unittest problem

2005-11-21 Thread Peter Hansen
[EMAIL PROTECTED] wrote:
 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Numeric array in unittest problem

2005-11-21 Thread [EMAIL PROTECTED]
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Numeric array in unittest problem

2005-11-21 Thread Robert Kern
[EMAIL PROTECTED] wrote:
 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
[EMAIL PROTECTED]

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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Numeric array in unittest problem

2005-11-21 Thread Alex Martelli
[EMAIL PROTECTED] [EMAIL PROTECTED] wrote:

 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Numeric array in unittest problem

2005-11-21 Thread Robert Kern
Alex Martelli wrote:

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.

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
[EMAIL PROTECTED]

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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Numeric array in unittest problem

2005-11-21 Thread Roman Bertle
* Alex Martelli [EMAIL PROTECTED]:
  [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
 
  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)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Numeric array in unittest problem

2005-11-21 Thread Peter Hansen
[EMAIL PROTECTED] wrote:
 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
-- 
http://mail.python.org/mailman/listinfo/python-list