LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
LSSTDataManagementBasePackage
Classes | Functions | Variables
lsst.utils.tests Namespace Reference

Classes

class  MemoryTestCase
 
class  temporaryFile
 
class  TestCase
 

Functions

def init
 
def run
 
def findFileFromRoot
 
def inTestCase
 
def assertRaisesLsstCpp
 
def debugger
 
def plotImageDiff
 
def assertClose
 
def assertNotClose
 

Variables

 dafBase = None
 
int memId0 = 0
 
int nleakPrintMax = 20
 

Function Documentation

def lsst.utils.tests.assertClose (   testCase,
  lhs,
  rhs,
  rtol = sys.float_info.epsilon,
  atol = sys.float_info.epsilon,
  relTo = None,
  printFailures = True,
  plotOnFailure = False,
  plotFileName = None,
  invert = False 
)
Highly-configurable floating point comparisons for scalars and arrays.

The test assertion will fail if all elements lhs and rhs are not equal to within the tolerances
specified by rtol and atol.  More precisely, the comparison is:

abs(lhs - rhs) <= relTo*rtol OR abs(lhs - rhs) <= atol

If rtol or atol is None, that term in the comparison is not performed at all.

When not specified, relTo is the elementwise maximum of the absolute values of lhs and rhs.  If
set manually, it should usually be set to either lhs or rhs, or a scalar value typical of what
is expected.

@param[in]  testCase       unittest.TestCase instance the test is part of
@param[in]  lhs            LHS value(s) to compare; may be a scalar or a numpy array of any dimension
@param[in]  rhs            RHS value(s) to compare; may be a scalar or a numpy array of any dimension
@param[in]  rtol           Relative tolerance for comparison; defaults to double-precision epsilon.
@param[in]  atol           Absolute tolerance for comparison; defaults to double-precision epsilon.
@param[in]  relTo          Value to which comparison with rtol is relative.
@param[in]  printFailures  Upon failure, print all inequal elements as part of the message.
@param[in]  plotOnFailure  Upon failure, plot the originals and their residual with matplotlib.
                           Only 2-d arrays are supported.
@param[in]  plotFileName   Filename to save the plot to.  If None, the plot will be displayed in a
                           a window.
@param[in]  invert         If True, invert the comparison and fail only if any elements *are* equal.
                           Used to implement assertNotClose, which should generally be used instead
                           for clarity.

Definition at line 245 of file tests.py.

246  printFailures=True, plotOnFailure=False, plotFileName=None, invert=False):
247  """Highly-configurable floating point comparisons for scalars and arrays.
248 
249  The test assertion will fail if all elements lhs and rhs are not equal to within the tolerances
250  specified by rtol and atol. More precisely, the comparison is:
251 
252  abs(lhs - rhs) <= relTo*rtol OR abs(lhs - rhs) <= atol
253 
254  If rtol or atol is None, that term in the comparison is not performed at all.
255 
256  When not specified, relTo is the elementwise maximum of the absolute values of lhs and rhs. If
257  set manually, it should usually be set to either lhs or rhs, or a scalar value typical of what
258  is expected.
259 
260  @param[in] testCase unittest.TestCase instance the test is part of
261  @param[in] lhs LHS value(s) to compare; may be a scalar or a numpy array of any dimension
262  @param[in] rhs RHS value(s) to compare; may be a scalar or a numpy array of any dimension
263  @param[in] rtol Relative tolerance for comparison; defaults to double-precision epsilon.
264  @param[in] atol Absolute tolerance for comparison; defaults to double-precision epsilon.
265  @param[in] relTo Value to which comparison with rtol is relative.
266  @param[in] printFailures Upon failure, print all inequal elements as part of the message.
267  @param[in] plotOnFailure Upon failure, plot the originals and their residual with matplotlib.
268  Only 2-d arrays are supported.
269  @param[in] plotFileName Filename to save the plot to. If None, the plot will be displayed in a
270  a window.
271  @param[in] invert If True, invert the comparison and fail only if any elements *are* equal.
272  Used to implement assertNotClose, which should generally be used instead
273  for clarity.
274  """
275  if not numpy.isfinite(lhs).all():
276  testCase.fail("Non-finite values in lhs")
277  if not numpy.isfinite(rhs).all():
278  testCase.fail("Non-finite values in rhs")
279  diff = lhs - rhs
280  absDiff = numpy.abs(lhs - rhs)
281  if rtol is not None:
282  if relTo is None:
283  relTo = numpy.maximum(numpy.abs(lhs), numpy.abs(rhs))
284  else:
285  relTo = numpy.abs(relTo)
286  bad = absDiff > rtol*relTo
287  if atol is not None:
288  bad = numpy.logical_and(bad, absDiff > atol)
289  else:
290  if atol is None:
291  raise ValueError("rtol and atol cannot both be None")
292  bad = absDiff > atol
293  failed = numpy.any(bad)
294  if invert:
295  failed = not failed
296  bad = numpy.logical_not(bad)
297  cmpStr = "=="
298  failStr = "are the same"
299  else:
300  cmpStr = "!="
301  failStr = "differ"
302  msg = []
303  if failed:
304  if numpy.isscalar(bad):
305  msg = ["%s %s %s; diff=%s/%s=%s with rtol=%s, atol=%s"
306  % (lhs, cmpStr, rhs, absDiff, relTo, absDiff/relTo, rtol, atol)]
307  else:
308  msg = ["%d/%d elements %s with rtol=%s, atol=%s"
309  % (bad.sum(), bad.size, failStr, rtol, atol)]
310  if plotOnFailure:
311  if len(lhs.shape) != 2 or len(rhs.shape) != 2:
312  raise ValueError("plotOnFailure is only valid for 2-d arrays")
313  try:
314  plotImageDiff(lhs, rhs, bad, diff=diff, plotFileName=plotFileName)
315  except ImportError:
316  msg.append("Failure plot requested but matplotlib could not be imported.")
317  if printFailures:
318  # Make sure everything is an array if any of them are, so we can treat
319  # them the same (diff and absDiff are arrays if either rhs or lhs is),
320  # and we don't get here if neither is.
321  if numpy.isscalar(relTo):
322  relTo = numpy.ones(bad.shape, dtype=float) * relTo
323  if numpy.isscalar(lhs):
324  lhs = numpy.ones(bad.shape, dtype=float) * lhs
325  if numpy.isscalar(rhs):
326  rhs = numpy.ones(bad.shape, dtype=float) * rhs
327  for a, b, diff, rel in zip(lhs[bad], rhs[bad], absDiff[bad], relTo[bad]):
328  msg.append("%s %s %s (diff=%s/%s=%s)" % (a, cmpStr, b, diff, rel, diff/rel))
329  testCase.assertFalse(failed, msg="\n".join(msg))
330 
@inTestCase
def plotImageDiff
Definition: tests.py:190
boost::enable_if< typename ExpressionTraits< Scalar >::IsScalar, bool >::type all(Scalar const &scalar)
Definition: operators.h:1186
def lsst.utils.tests.assertNotClose (   testCase,
  lhs,
  rhs,
  kwds 
)
Fail a test if the given floating point values are completely equal to within the given tolerances.

See assertClose for more information.

Definition at line 331 of file tests.py.

332 def assertNotClose(testCase, lhs, rhs, **kwds):
333  """Fail a test if the given floating point values are completely equal to within the given tolerances.
334 
335  See assertClose for more information.
336  """
337  return assertClose(testCase, lhs, rhs, invert=True, **kwds)
def assertNotClose
Definition: tests.py:331
def lsst.utils.tests.assertRaisesLsstCpp (   testcase,
  excClass,
  callableObj,
  args,
  kwargs 
)

Definition at line 155 of file tests.py.

156 def assertRaisesLsstCpp(testcase, excClass, callableObj, *args, **kwargs):
157  warnings.warn("assertRaisesLsstCpp is deprecated; please just use TestCase.assertRaises",
158  DeprecationWarning)
159  return testcase.assertRaises(excClass, callableObj, *args, **kwargs)
160 
161 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
def assertRaisesLsstCpp
Definition: tests.py:155
def lsst.utils.tests.debugger (   exceptions)
Decorator to enter the debugger when there's an uncaught exception

To use, just slap a "@debugger()" on your function.

You may provide specific exception classes to catch as arguments to
the decorator function, e.g., "@debugger(RuntimeError, NotImplementedError)".
This defaults to just 'AssertionError', for use on unittest.TestCase methods.

Code provided by "Rosh Oxymoron" on StackOverflow:
http://stackoverflow.com/questions/4398967/python-unit-testing-automatically-running-the-debugger-when-a-test-fails

Definition at line 163 of file tests.py.

164 def debugger(*exceptions):
165  """Decorator to enter the debugger when there's an uncaught exception
166 
167  To use, just slap a "@debugger()" on your function.
168 
169  You may provide specific exception classes to catch as arguments to
170  the decorator function, e.g., "@debugger(RuntimeError, NotImplementedError)".
171  This defaults to just 'AssertionError', for use on unittest.TestCase methods.
172 
173  Code provided by "Rosh Oxymoron" on StackOverflow:
174  http://stackoverflow.com/questions/4398967/python-unit-testing-automatically-running-the-debugger-when-a-test-fails
175  """
176  if not exceptions:
177  exceptions = (AssertionError, )
178  def decorator(f):
179  @functools.wraps(f)
180  def wrapper(*args, **kwargs):
181  try:
182  return f(*args, **kwargs)
183  except exceptions:
184  import sys, pdb
185  pdb.post_mortem(sys.exc_info()[2])
186  return wrapper
187  return decorator
188 
189 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
def lsst.utils.tests.findFileFromRoot (   ifile)
Find file which is specified as a path relative to the toplevel directory;
we start in $cwd and walk up until we find the file (or throw IOError if it doesn't exist)

This is useful for running tests that may be run from <dir>/tests or <dir>

Definition at line 90 of file tests.py.

90 
91 def findFileFromRoot(ifile):
92  """Find file which is specified as a path relative to the toplevel directory;
93  we start in $cwd and walk up until we find the file (or throw IOError if it doesn't exist)
94 
95  This is useful for running tests that may be run from <dir>/tests or <dir>"""
96 
97  if os.path.isfile(ifile):
98  return ifile
99 
100  ofile = None
101  file = ifile
102  while file != "":
103  dirname, basename = os.path.split(file)
104  if ofile:
105  ofile = os.path.join(basename, ofile)
106  else:
107  ofile = basename
108 
109  if os.path.isfile(ofile):
110  return ofile
111 
112  file = dirname
113 
114  raise IOError, "Can't find %s" % ifile
115 
116 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
def findFileFromRoot
Definition: tests.py:90
def lsst.utils.tests.init ( )

Definition at line 44 of file tests.py.

44 
45 def init():
46  global memId0
47  if dafBase:
48  memId0 = dafBase.Citizen_getNextMemId() # used by MemoryTestCase
def lsst.utils.tests.inTestCase (   func)
A decorator to add a free function to our custom TestCase class, while also
making it available as a free function.

Definition at line 145 of file tests.py.

146 def inTestCase(func):
147  """A decorator to add a free function to our custom TestCase class, while also
148  making it available as a free function.
149  """
150  setattr(TestCase, func.__name__, func)
151  return func
152 
153 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
154 
@inTestCase
def lsst.utils.tests.plotImageDiff (   lhs,
  rhs,
  bad = None,
  diff = None,
  plotFileName = None 
)
Plot the comparison of two 2-d NumPy arrays.

NOTE: this method uses matplotlib and imports it internally; it should be
wrapped in a try/except block within packages that do not depend on
matplotlib (including utils).

@param[in]  lhs            LHS values to compare; a 2-d NumPy array
@param[in]  rhs            RHS values to compare; a 2-d NumPy array
@param[in]  bad            A 2-d boolean NumPy array of values to emphasize in the plots
@param[in]  plotFileName   Filename to save the plot to.  If None, the plot will be displayed in a
                           a window.

Definition at line 190 of file tests.py.

191 def plotImageDiff(lhs, rhs, bad=None, diff=None, plotFileName=None):
192  """Plot the comparison of two 2-d NumPy arrays.
193 
194  NOTE: this method uses matplotlib and imports it internally; it should be
195  wrapped in a try/except block within packages that do not depend on
196  matplotlib (including utils).
197 
198  @param[in] lhs LHS values to compare; a 2-d NumPy array
199  @param[in] rhs RHS values to compare; a 2-d NumPy array
200  @param[in] bad A 2-d boolean NumPy array of values to emphasize in the plots
201  @param[in] plotFileName Filename to save the plot to. If None, the plot will be displayed in a
202  a window.
203  """
204  from matplotlib import pyplot
205  if diff is None:
206  diff = lhs - rhs
207  pyplot.figure()
208  if bad is not None:
209  # make an rgba image that's red and transparent where not bad
210  badImage = numpy.zeros(bad.shape + (4,), dtype=numpy.uint8)
211  badImage[:,:,0] = 255
212  badImage[:,:,1] = 0
213  badImage[:,:,2] = 0
214  badImage[:,:,3] = 255*bad
215  vmin1 = numpy.minimum(numpy.min(lhs), numpy.min(rhs))
216  vmax1 = numpy.maximum(numpy.max(lhs), numpy.max(rhs))
217  vmin2 = numpy.min(diff)
218  vmax2 = numpy.max(diff)
219  for n, (image, title) in enumerate([(lhs, "lhs"), (rhs, "rhs"), (diff, "diff")]):
220  pyplot.subplot(2,3,n+1)
221  im1 = pyplot.imshow(image, cmap=pyplot.cm.gray, interpolation='nearest', origin='lower',
222  vmin=vmin1, vmax=vmax1)
223  if bad is not None:
224  pyplot.imshow(badImage, alpha=0.2, interpolation='nearest', origin='lower')
225  pyplot.axis("off")
226  pyplot.title(title)
227  pyplot.subplot(2,3,n+4)
228  im2 = pyplot.imshow(image, cmap=pyplot.cm.gray, interpolation='nearest', origin='lower',
229  vmin=vmin2, vmax=vmax2)
230  if bad is not None:
231  pyplot.imshow(badImage, alpha=0.2, interpolation='nearest', origin='lower')
232  pyplot.axis("off")
233  pyplot.title(title)
234  pyplot.subplots_adjust(left=0.05, bottom=0.05, top=0.92, right=0.75, wspace=0.05, hspace=0.05)
235  cax1 = pyplot.axes([0.8, 0.55, 0.05, 0.4])
236  pyplot.colorbar(im1, cax=cax1)
237  cax2 = pyplot.axes([0.8, 0.05, 0.05, 0.4])
238  pyplot.colorbar(im2, cax=cax2)
239  if plotFileName:
240  pyplot.savefig(plotFileName)
241  else:
242  pyplot.show()
243 
@inTestCase
def plotImageDiff
Definition: tests.py:190
def lsst.utils.tests.run (   suite,
  exit = True 
)
Exit with the status code resulting from running the provided test suite

Definition at line 49 of file tests.py.

49 
50 def run(suite, exit=True):
51  """Exit with the status code resulting from running the provided test suite"""
52 
53  if unittest.TextTestRunner().run(suite).wasSuccessful():
54  status = 0
55  else:
56  status = 1
57 
58  if exit:
59  sys.exit(status)
60  else:
61  return status
62 
63 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Variable Documentation

lsst.utils.tests.dafBase = None

Definition at line 31 of file tests.py.

int lsst.utils.tests.memId0 = 0

Definition at line 41 of file tests.py.

int lsst.utils.tests.nleakPrintMax = 20

Definition at line 42 of file tests.py.