Saturday, November 10, 2007

Open source develop: assertAlmostEqual

I've been working on a project at work while using Python/Jython. And I'm using unittesting to drive and test the development along the way. Because I deal with things like real data, the usual assertEqual runs into problems when you use floating point computation, because you just don't test for equality in floating point. To much potential for rounding errors at the 20th digit or some other insignificant problem. So you need to use assertAlmostEqual.

And everything is working fine, until I try to run my tests in Jython. And then all my floating point tests start to fail, because assertAlmostEqual does not exist. So, did I spell it wrong, but it worked under normal Python. So a quick google search reveals that assertAlmostEqual was added a bit later, so it was not in Python 2.2 (which Jython is written against). And there is a newsgroup posting in the Python-checkins list about the adding of assertAlmostEqual to the PyUnit testing library (which is what I use for my unit tests).

http://mail.python.org/pipermail/python-checkins/2002-December/032079.html
Sat, 28 Dec 2002 22:11:50 -0600
[Python-checkins] python/nondist/sandbox/twister test_random.py,1.1,1.2

Raymond> To accomodate single precision platforms, only test to seven
Raymond> digits.

The SciPy folks have added an assertAlmostEqual method to their unit tests.
I believe it more-or-less just wraps what you've done in a callable method
(which takes a number of digits of precision). Might be a good idea to add
something like it to unittest.py so the wheel doesn't keep getting
reinvented. They actually have a few variants, coded as functions here:


http://www.scipy.org/site_content/remap?rmurl=http%3A//scipy.net/cgi-bin/viewcvsx.cgi/scipy/scipy_test/testing.py

Skip
Ok. That explains that. But something else looks oddly familiar. I remember doing something with SciPy around the same time
.

http://projects.scipy.org/pipermail/scipy-user/2002-January/008545.html

Sun Jan 20 21:38:03 CST 2002
[SciPy-user] unittests for scipy.stats: assert_almost_equal question
Hmmm, I think that I would like to be testing in terms of
significant digits as opposed to decimal places, especially when
working with floating point. Since you are asking for such a test,
here it is. The attached file has a function meant to go into the
scipy_test.py module. I wrote assert_approx_equal following the
same form as assert_almost_equal
assert_approx_equal(actual,desired,significant=7,err_msg='',verbose=1):
compares 'actual' and 'desired' and determines the first
'significant' significant digits and checks for accuracy. Let's
see, I think I counted significant digits correctly. Can anyone
tell?

Louis
> From: "eric"
> To: <scipy-user at scipy.org>
> Subject: Re: [SciPy-user] unittests for scipy.stats:
> assert_almost_equal question
> Date: Sun, 20 Jan 2002 05:36:31 -0500
> Organization: enthought
> Reply-To: scipy-user at scipy.net
>
> Hey Louis,
>
> A thousand blessing upon you. I immediately commited it to the
> CVS!
>
Oh, now I know why this problem looks so familiar. Way back when I was a grad student, I was trying to test some functions. And I ran into the problem about unittesting floating point. So I wrote unittest code to test floating point in SciPy. And it was added to SciPy. And the main Python language developers noticed (since those folks looked to the SciPy/Numpy folks about all things numerical computation related) and added it to the main language. And here I am, five years later, taking advantage of something I did as a grad student.

Add to reasons for "why contribute to open source programming" story.

No comments: