IMP logo
IMP Reference Guide  2.6.0
The Integrative Modeling Platform
test/__init__.py
1 # This file was automatically generated by SWIG (http://www.swig.org).
2 # Version 3.0.7
3 #
4 # Do not make changes to this file unless you know what you are doing--modify
5 # the SWIG interface file instead.
6 
7 # This wrapper is part of IMP,
8 # Copyright 2007-2016 IMP Inventors. All rights reserved.
9 
10 from __future__ import print_function, division, absolute_import
11 
12 
13 
14 
15 from sys import version_info
16 if version_info >= (2, 6, 0):
17  def swig_import_helper():
18  from os.path import dirname
19  import imp
20  fp = None
21  try:
22  fp, pathname, description = imp.find_module('_IMP_test', [dirname(__file__)])
23  except ImportError:
24  import _IMP_test
25  return _IMP_test
26  if fp is not None:
27  try:
28  _mod = imp.load_module('_IMP_test', fp, pathname, description)
29  finally:
30  fp.close()
31  return _mod
32  _IMP_test = swig_import_helper()
33  del swig_import_helper
34 else:
35  import _IMP_test
36 del version_info
37 try:
38  _swig_property = property
39 except NameError:
40  pass # Python < 2.2 doesn't have 'property'.
41 
42 
43 def _swig_setattr_nondynamic(self, class_type, name, value, static=1):
44  if (name == "thisown"):
45  return self.this.own(value)
46  if (name == "this"):
47  if type(value).__name__ == 'SwigPyObject':
48  self.__dict__[name] = value
49  return
50  method = class_type.__swig_setmethods__.get(name, None)
51  if method:
52  return method(self, value)
53  if (not static):
54  object.__setattr__(self, name, value)
55  else:
56  raise AttributeError("You cannot add attributes to %s" % self)
57 
58 
59 def _swig_setattr(self, class_type, name, value):
60  return _swig_setattr_nondynamic(self, class_type, name, value, 0)
61 
62 
63 def _swig_getattr_nondynamic(self, class_type, name, static=1):
64  if (name == "thisown"):
65  return self.this.own()
66  method = class_type.__swig_getmethods__.get(name, None)
67  if method:
68  return method(self)
69  if (not static):
70  return object.__getattr__(self, name)
71  else:
72  raise AttributeError(name)
73 
74 def _swig_getattr(self, class_type, name):
75  return _swig_getattr_nondynamic(self, class_type, name, 0)
76 
77 
78 def _swig_repr(self):
79  try:
80  strthis = "proxy of " + self.this.__repr__()
81  except:
82  strthis = ""
83  return "<%s.%s; %s >" % (self.__class__.__module__, self.__class__.__name__, strthis,)
84 
85 try:
86  _object = object
87  _newclass = 1
88 except AttributeError:
89  class _object:
90  pass
91  _newclass = 0
92 
93 
94 
95 def _swig_setattr_nondynamic_method(set):
96  def set_attr(self, name, value):
97  if (name == "thisown"):
98  return self.this.own(value)
99  if hasattr(self, name) or (name == "this"):
100  set(self, name, value)
101  else:
102  raise AttributeError("You cannot add attributes to %s" % self)
103  return set_attr
104 
105 
106 try:
107  import weakref
108  weakref_proxy = weakref.proxy
109 except:
110  weakref_proxy = lambda x: x
111 
112 
113 class IMP_TEST_SwigPyIterator(object):
114  """Proxy of C++ swig::IMP_TEST_SwigPyIterator class"""
115  thisown = _swig_property(lambda x: x.this.own(), lambda x, v: x.this.own(v), doc='The membership flag')
116 
117  def __init__(self, *args, **kwargs):
118  raise AttributeError("No constructor defined - class is abstract")
119  __repr__ = _swig_repr
120  __swig_destroy__ = _IMP_test.delete_IMP_TEST_SwigPyIterator
121  __del__ = lambda self: None
122 
123  def value(self):
124  """value(IMP_TEST_SwigPyIterator self) -> PyObject *"""
125  return _IMP_test.IMP_TEST_SwigPyIterator_value(self)
126 
127 
128  def incr(self, n=1):
129  """
130  incr(IMP_TEST_SwigPyIterator self, size_t n=1) -> IMP_TEST_SwigPyIterator
131  incr(IMP_TEST_SwigPyIterator self) -> IMP_TEST_SwigPyIterator
132  """
133  return _IMP_test.IMP_TEST_SwigPyIterator_incr(self, n)
134 
135 
136  def decr(self, n=1):
137  """
138  decr(IMP_TEST_SwigPyIterator self, size_t n=1) -> IMP_TEST_SwigPyIterator
139  decr(IMP_TEST_SwigPyIterator self) -> IMP_TEST_SwigPyIterator
140  """
141  return _IMP_test.IMP_TEST_SwigPyIterator_decr(self, n)
142 
143 
144  def distance(self, x):
145  """distance(IMP_TEST_SwigPyIterator self, IMP_TEST_SwigPyIterator x) -> ptrdiff_t"""
146  return _IMP_test.IMP_TEST_SwigPyIterator_distance(self, x)
147 
148 
149  def equal(self, x):
150  """equal(IMP_TEST_SwigPyIterator self, IMP_TEST_SwigPyIterator x) -> bool"""
151  return _IMP_test.IMP_TEST_SwigPyIterator_equal(self, x)
152 
153 
154  def copy(self):
155  """copy(IMP_TEST_SwigPyIterator self) -> IMP_TEST_SwigPyIterator"""
156  return _IMP_test.IMP_TEST_SwigPyIterator_copy(self)
157 
158 
159  def next(self):
160  """next(IMP_TEST_SwigPyIterator self) -> PyObject *"""
161  return _IMP_test.IMP_TEST_SwigPyIterator_next(self)
162 
163 
164  def __next__(self):
165  """__next__(IMP_TEST_SwigPyIterator self) -> PyObject *"""
166  return _IMP_test.IMP_TEST_SwigPyIterator___next__(self)
167 
168 
169  def previous(self):
170  """previous(IMP_TEST_SwigPyIterator self) -> PyObject *"""
171  return _IMP_test.IMP_TEST_SwigPyIterator_previous(self)
172 
173 
174  def advance(self, n):
175  """advance(IMP_TEST_SwigPyIterator self, ptrdiff_t n) -> IMP_TEST_SwigPyIterator"""
176  return _IMP_test.IMP_TEST_SwigPyIterator_advance(self, n)
177 
178 
179  def __eq__(self, x):
180  """__eq__(IMP_TEST_SwigPyIterator self, IMP_TEST_SwigPyIterator x) -> bool"""
181  return _IMP_test.IMP_TEST_SwigPyIterator___eq__(self, x)
182 
183 
184  def __ne__(self, x):
185  """__ne__(IMP_TEST_SwigPyIterator self, IMP_TEST_SwigPyIterator x) -> bool"""
186  return _IMP_test.IMP_TEST_SwigPyIterator___ne__(self, x)
187 
188 
189  def __iadd__(self, n):
190  """__iadd__(IMP_TEST_SwigPyIterator self, ptrdiff_t n) -> IMP_TEST_SwigPyIterator"""
191  return _IMP_test.IMP_TEST_SwigPyIterator___iadd__(self, n)
192 
193 
194  def __isub__(self, n):
195  """__isub__(IMP_TEST_SwigPyIterator self, ptrdiff_t n) -> IMP_TEST_SwigPyIterator"""
196  return _IMP_test.IMP_TEST_SwigPyIterator___isub__(self, n)
197 
198 
199  def __add__(self, n):
200  """__add__(IMP_TEST_SwigPyIterator self, ptrdiff_t n) -> IMP_TEST_SwigPyIterator"""
201  return _IMP_test.IMP_TEST_SwigPyIterator___add__(self, n)
202 
203 
204  def __sub__(self, *args):
205  """
206  __sub__(IMP_TEST_SwigPyIterator self, ptrdiff_t n) -> IMP_TEST_SwigPyIterator
207  __sub__(IMP_TEST_SwigPyIterator self, IMP_TEST_SwigPyIterator x) -> ptrdiff_t
208  """
209  return _IMP_test.IMP_TEST_SwigPyIterator___sub__(self, *args)
210 
211  def __iter__(self):
212  return self
213 IMP_TEST_SwigPyIterator_swigregister = _IMP_test.IMP_TEST_SwigPyIterator_swigregister
214 IMP_TEST_SwigPyIterator_swigregister(IMP_TEST_SwigPyIterator)
215 
216 
217 _value_types=[]
218 _object_types=[]
219 _raii_types=[]
220 _plural_types=[]
221 
222 
223 _IMP_test.IMP_DEBUG_swigconstant(_IMP_test)
224 IMP_DEBUG = _IMP_test.IMP_DEBUG
225 
226 _IMP_test.IMP_RELEASE_swigconstant(_IMP_test)
227 IMP_RELEASE = _IMP_test.IMP_RELEASE
228 
229 _IMP_test.IMP_SILENT_swigconstant(_IMP_test)
230 IMP_SILENT = _IMP_test.IMP_SILENT
231 
232 _IMP_test.IMP_PROGRESS_swigconstant(_IMP_test)
233 IMP_PROGRESS = _IMP_test.IMP_PROGRESS
234 
235 _IMP_test.IMP_TERSE_swigconstant(_IMP_test)
236 IMP_TERSE = _IMP_test.IMP_TERSE
237 
238 _IMP_test.IMP_VERBOSE_swigconstant(_IMP_test)
239 IMP_VERBOSE = _IMP_test.IMP_VERBOSE
240 
241 _IMP_test.IMP_MEMORY_swigconstant(_IMP_test)
242 IMP_MEMORY = _IMP_test.IMP_MEMORY
243 
244 _IMP_test.IMP_NONE_swigconstant(_IMP_test)
245 IMP_NONE = _IMP_test.IMP_NONE
246 
247 _IMP_test.IMP_USAGE_swigconstant(_IMP_test)
248 IMP_USAGE = _IMP_test.IMP_USAGE
249 
250 _IMP_test.IMP_INTERNAL_swigconstant(_IMP_test)
251 IMP_INTERNAL = _IMP_test.IMP_INTERNAL
252 
253 _IMP_test.IMP_KERNEL_HAS_LOG4CXX_swigconstant(_IMP_test)
254 IMP_KERNEL_HAS_LOG4CXX = _IMP_test.IMP_KERNEL_HAS_LOG4CXX
255 
256 _IMP_test.IMP_COMPILER_HAS_AUTO_swigconstant(_IMP_test)
257 IMP_COMPILER_HAS_AUTO = _IMP_test.IMP_COMPILER_HAS_AUTO
258 
259 _IMP_test.IMP_COMPILER_HAS_DEBUG_VECTOR_swigconstant(_IMP_test)
260 IMP_COMPILER_HAS_DEBUG_VECTOR = _IMP_test.IMP_COMPILER_HAS_DEBUG_VECTOR
261 
262 _IMP_test.IMP_COMPILER_HAS_UNIQUE_PTR_swigconstant(_IMP_test)
263 IMP_COMPILER_HAS_UNIQUE_PTR = _IMP_test.IMP_COMPILER_HAS_UNIQUE_PTR
264 
265 _IMP_test.IMP_KERNEL_HAS_BOOST_RANDOM_swigconstant(_IMP_test)
266 IMP_KERNEL_HAS_BOOST_RANDOM = _IMP_test.IMP_KERNEL_HAS_BOOST_RANDOM
267 
268 _IMP_test.IMP_KERNEL_HAS_GPERFTOOLS_swigconstant(_IMP_test)
269 IMP_KERNEL_HAS_GPERFTOOLS = _IMP_test.IMP_KERNEL_HAS_GPERFTOOLS
270 
271 _IMP_test.IMP_KERNEL_HAS_TCMALLOC_HEAPCHECKER_swigconstant(_IMP_test)
272 IMP_KERNEL_HAS_TCMALLOC_HEAPCHECKER = _IMP_test.IMP_KERNEL_HAS_TCMALLOC_HEAPCHECKER
273 
274 _IMP_test.IMP_KERNEL_HAS_TCMALLOC_HEAPPROFILER_swigconstant(_IMP_test)
275 IMP_KERNEL_HAS_TCMALLOC_HEAPPROFILER = _IMP_test.IMP_KERNEL_HAS_TCMALLOC_HEAPPROFILER
276 
277 _IMP_test.IMPKERNEL_SHOW_WARNINGS_swigconstant(_IMP_test)
278 IMPKERNEL_SHOW_WARNINGS = _IMP_test.IMPKERNEL_SHOW_WARNINGS
279 
280 import sys
281 class _DirectorObjects(object):
282  """@internal Simple class to keep references to director objects
283  to prevent premature deletion."""
284  def __init__(self):
285  self._objects = []
286  def register(self, obj):
287  """Take a reference to a director object; will only work for
288  refcounted C++ classes"""
289  if hasattr(obj, 'get_ref_count'):
290  self._objects.append(obj)
291  def cleanup(self):
292  """Only drop our reference and allow cleanup by Python if no other
293  Python references exist (we hold 3 references: one in self._objects,
294  one in x, and one in the argument list for getrefcount) *and* no
295  other C++ references exist (the Python object always holds one)"""
296  objs = [x for x in self._objects if sys.getrefcount(x) > 3 \
297  or x.get_ref_count() > 1]
298 # Do in two steps so the references are kept until the end of the
299 # function (deleting references may trigger a fresh call to this method)
300  self._objects = objs
301  def get_object_count(self):
302  """Get number of director objects (useful for testing only)"""
303  return len(self._objects)
304 _director_objects = _DirectorObjects()
305 
306 class _ostream(object):
307  """Proxy of C++ std::ostream class"""
308  thisown = _swig_property(lambda x: x.this.own(), lambda x, v: x.this.own(v), doc='The membership flag')
309 
310  def __init__(self, *args, **kwargs):
311  raise AttributeError("No constructor defined")
312  __repr__ = _swig_repr
313 
314  def write(self, osa_buf):
315  """write(_ostream self, char const * osa_buf)"""
316  return _IMP_test._ostream_write(self, osa_buf)
317 
318 _ostream_swigregister = _IMP_test._ostream_swigregister
319 _ostream_swigregister(_ostream)
320 
321 
322 _IMP_test.IMP_COMPILER_HAS_OVERRIDE_swigconstant(_IMP_test)
323 IMP_COMPILER_HAS_OVERRIDE = _IMP_test.IMP_COMPILER_HAS_OVERRIDE
324 
325 _IMP_test.IMP_COMPILER_HAS_FINAL_swigconstant(_IMP_test)
326 IMP_COMPILER_HAS_FINAL = _IMP_test.IMP_COMPILER_HAS_FINAL
327 
328 _IMP_test.IMP_HAS_NOEXCEPT_swigconstant(_IMP_test)
329 IMP_HAS_NOEXCEPT = _IMP_test.IMP_HAS_NOEXCEPT
330 import IMP
331 
332 _IMP_test.IMP_TEST_HAS_BOOST_FILESYSTEM_swigconstant(_IMP_test)
333 IMP_TEST_HAS_BOOST_FILESYSTEM = _IMP_test.IMP_TEST_HAS_BOOST_FILESYSTEM
334 
335 _IMP_test.IMP_TEST_HAS_BOOST_PROGRAMOPTIONS_swigconstant(_IMP_test)
336 IMP_TEST_HAS_BOOST_PROGRAMOPTIONS = _IMP_test.IMP_TEST_HAS_BOOST_PROGRAMOPTIONS
337 
338 _IMP_test.IMP_TEST_HAS_BOOST_RANDOM_swigconstant(_IMP_test)
339 IMP_TEST_HAS_BOOST_RANDOM = _IMP_test.IMP_TEST_HAS_BOOST_RANDOM
340 
341 _IMP_test.IMP_TEST_HAS_BOOST_SYSTEM_swigconstant(_IMP_test)
342 IMP_TEST_HAS_BOOST_SYSTEM = _IMP_test.IMP_TEST_HAS_BOOST_SYSTEM
343 
344 _IMP_test.IMPTEST_SHOW_WARNINGS_swigconstant(_IMP_test)
345 IMPTEST_SHOW_WARNINGS = _IMP_test.IMPTEST_SHOW_WARNINGS
346 
347 
348 """@namespace IMP::test
349  @brief Methods and classes for testing the IMP kernel and modules.
350  @ingroup python
351 """
352 
353 import re, math
354 import sys
355 import os
356 import re
357 import tempfile
358 import random
359 import IMP
360 import time
361 import types
362 import shutil
363 from . import _compat_python
364 from ._compat_python import unittest2
365 import datetime
366 import pickle
367 
368 # Fall back to the sets.Set class on older Pythons that don't have
369 # the 'set' builtin type.
370 try:
371  set = set
372 except NameError:
373  import sets
374  set = sets.Set
375 
376 # Load a new enough unittest package (should have the 'skip' decorator)
377 # - On Python 2.7 or 3.2, the standard 'unittest' package will work.
378 # - On older Pythons, use the 'unittest2' package if available, otherwise use
379 # our bundled version of this package.
380 def __load_unittest_package():
381  errors = []
382  for modname, fromlist in (('unittest', []),
383  ('unittest2', []),
384  ):
385  try:
386  u = __import__(modname, {}, {}, fromlist)
387  if hasattr(u, 'skip'):
388  return u
389  else:
390  errors.append("'%s' does not have the 'skip' decorator" \
391  % modname)
392  except ImportError as e:
393  errors.append(str(e))
394 #u = __import__("_compat_python.unittest2
395  return _compat_python.unittest2
396  raise ImportError("IMP.test requires a newer version of Python's unittest "
397  "package than is available. Either upgrade to a new "
398  "enough Python (at least 2.7 or 3.2) or install the "
399  "unittest2 package. Encountered errors: %s" \
400  % "; ".join(errors))
401 unittest = __load_unittest_package()
402 
403 # Expose some unittest decorators for convenience
404 expectedFailure = unittest.expectedFailure
405 skip = unittest.skip
406 skipIf = unittest.skipIf
407 skipUnless = unittest.skipUnless
408 
409 class RunInTempDir(object):
410  """Simple RAII-style class to run in a temporary directory.
411  When the object is created, the temporary directory is created
412  and becomes the current working directory. When the object goes out
413  of scope, the working directory is reset and the temporary directory
414  deleted."""
415  def __init__(self):
416  self.origdir = os.getcwd()
417  self.tmpdir = tempfile.mkdtemp()
418  os.chdir(self.tmpdir)
419  def __del__(self):
420  os.chdir(self.origdir)
421  shutil.rmtree(self.tmpdir, ignore_errors=True)
422 
423 
424 class TempDir(object):
425  """Simple RAII-style class to make a temporary directory. When the object
426  is created, the temporary directory is created. When the object goes
427  out of scope, the temporary directory is deleted."""
428  def __init__(self, dir=None):
429  self.tmpdir = tempfile.mkdtemp(dir=dir)
430  def __del__(self):
431  shutil.rmtree(self.tmpdir, ignore_errors=True)
432 
433 
434 def numerical_derivative(func, val, step):
435  """Calculate the derivative of the single-value function `func` at
436  point `val`. The derivative is calculated using simple finite
437  differences starting with the given `step`; Richardson extrapolation
438  is then used to extrapolate the derivative at step=0."""
439  maxsteps = 50
440  con = 1.4
441  safe = 2.0
442  err = 1.0e30
443  f1 = func(val + step)
444  f2 = func(val - step)
445 # create first element in triangular matrix d of derivatives
446  d = [[(f1 - f2) / (2.0 * step)]]
447  retval = None
448  for i in range(1, maxsteps):
449  d.append([0.] * (i + 1))
450  step /= con
451  f1 = func(val + step)
452  f2 = func(val - step)
453  d[i][0] = (f1 - f2) / (2.0 * step)
454  fac = con * con
455  for j in range(1, i + 1):
456  d[i][j] = (d[i][j-1] * fac - d[i-1][j-1]) / (fac - 1.)
457  fac *= con * con
458  errt = max(abs(d[i][j] - d[i][j-1]),
459  abs(d[i][j] - d[i-1][j-1]))
460  if errt <= err:
461  err = errt
462  retval = d[i][j]
463  if abs(d[i][i] - d[i-1][i-1]) >= safe * err:
464  break
465  if retval is None:
466  raise ValueError("Cannot calculate numerical derivative")
467  return retval
468 
469 
470 def xyz_numerical_derivatives(sf, xyz, step):
471  """Calculate the x,y and z derivatives of the scoring function `sf`
472  on the `xyz` particle. The derivatives are approximated numerically
473  using the numerical_derivatives() function."""
474  class _XYZDerivativeFunc(object):
475  def __init__(self, sf, xyz, basis_vector):
476  self._xyz = xyz
477  self._sf = sf
478  self._basis_vector = basis_vector
479  self._starting_coordinates = xyz.get_coordinates()
480 
481  def __call__(self, val):
482  self._xyz.set_coordinates(self._starting_coordinates + \
483  self._basis_vector * val)
484  return self._sf.evaluate(False)
485 
486  return tuple([IMP.test.numerical_derivative(_XYZDerivativeFunc(sf, xyz,
487  IMP.algebra.Vector3D(*x)), 0, 0.01) \
488  for x in ((1,0,0), (0,1,0), (0,0,1))])
489 
490 
491 class TestCase(unittest.TestCase):
492  """Super class for IMP test cases"""
493 
494  def __init__(self, *args, **keys):
495  unittest.TestCase.__init__(self, *args, **keys)
496  self._progname = os.path.abspath(sys.argv[0])
497 
498  def setUp(self):
499  self.__check_level = IMP.get_check_level()
500 # Turn on expensive runtime checks while running the test suite:
501  IMP.set_check_level(IMP.USAGE_AND_INTERNAL)
502 # python ints are bigger than C++ ones, so we need to make sure it fits
503 # otherwise python throws fits
504  IMP.random_number_generator.seed(hash(time.time())%2**30)
505 
506  def tearDown(self):
507 # Restore original check level
508  IMP.set_check_level(self.__check_level)
509 
510  def get_input_file_name(self, filename):
511  """Get the full name of an input file in the top-level
512  test directory."""
513  testdir = os.path.dirname(self._progname)
514  dirs = testdir.split(os.path.sep)
515  for i in range(len(dirs), 0, -1):
516  input = os.path.sep.join(dirs[:i] + ['input'])
517  if os.path.isdir(input):
518  ret = os.path.join(input, filename)
519  if not os.path.exists(ret):
520  raise IOError("Test input file "+ret+" does not exist")
521  return ret
522  raise IOError("No test input directory found")
523 
524  def open_input_file(self, filename, mode='rb'):
525  """Open and return an input file in the top-level test directory."""
526  return open(self.get_input_file_name(filename), mode)
527 
528  def get_tmp_file_name(self, filename):
529  """Get the full name of an output file in the tmp directory."""
530  if not hasattr(self, '_tmpdir'):
531  self._tmpdir = TempDir(os.environ['IMP_TMP_DIR'])
532  tmpdir = self._tmpdir.tmpdir
533  return os.path.join(tmpdir, filename)
534 
535  def get_magnitude(self, vector):
536  return sum([x*x for x in vector], 0)**.5
537 
538  def assertRaisesUsageException(self, c, *args, **keys):
539  """Assert that the given callable object raises UsageException.
540  This differs from unittest's assertRaises in that the test
541  is skipped in fast mode (where usage checks are turned off)."""
542  if IMP.get_check_level() >= IMP.USAGE:
543  return self.assertRaises(IMP.UsageException, c, *args, **keys)
544 
545  def assertRaisesInternalException(self, c, *args, **keys):
546  """Assert that the given callable object raises InternalException.
547  This differs from unittest's assertRaises in that the test
548  is skipped in fast mode (where internal checks are turned off)."""
549  if IMP.get_check_level() >= IMP.USAGE_AND_INTERNAL:
550  return self.assertRaises(IMP.InternalException, c, *args, **keys)
551 
552  def assertNotImplemented(self, c, *args, **keys):
553  """Assert that the given callable object is not implemented."""
554  return self.assertRaises(IMP.InternalException, c, *args, **keys)
555 
556  def assertXYZDerivativesInTolerance(self, sf, xyz, tolerance=0,
557  percentage=0):
558  """Assert that x,y,z analytical derivatives match numerical within
559  a tolerance, or a percentage (of the analytical value), whichever
560  is larger. `sf` should be a ScoringFunction or Restraint,
561  although for backwards compatibility a Model is also accepted."""
562  sf.evaluate(True)
563  derivs = xyz.get_derivatives()
564  num_derivs = xyz_numerical_derivatives(sf, xyz, 0.01)
565  pct = percentage / 100.0
566  self.assertAlmostEqual(self.get_magnitude(derivs-num_derivs),0,
567  delta=tolerance+percentage*self.get_magnitude(num_derivs),
568  msg="Don't match "+str(derivs) + str(num_derivs))
569  self.assertAlmostEqual(derivs[0], num_derivs[0],
570  delta=max(tolerance, abs(derivs[0]) * pct))
571  self.assertAlmostEqual(derivs[1], num_derivs[1],
572  delta=max(tolerance, abs(derivs[1]) * pct))
573  self.assertAlmostEqual(derivs[2], num_derivs[2],
574  delta=max(tolerance, abs(derivs[2]) * pct))
575 
576  def create_point_particle(self, model, x, y, z):
577  """Make a particle with optimizable x, y and z attributes, and
578  add it to the model."""
579  p = IMP.Particle(model)
580  p.add_attribute(IMP.FloatKey("x"), x, True)
581  p.add_attribute(IMP.FloatKey("y"), y, True)
582  p.add_attribute(IMP.FloatKey("z"), z, True)
583  return p
584 
585  def probabilistic_test(self, testcall, chance_of_failure):
586  """Help handle a test which is expected to fail some fraction of
587  the time. The test is run multiple times and an exception
588  is thrown only if it fails too many times."""
589  prob=chance_of_failure
590  tries=1
591  while prob > .001:
592  tries=tries+1
593  prob= prob*chance_of_failure
594  for i in range(0, tries):
595  try:
596  eval(testcall)
597  except:
598  pass
599  else:
600  return
601  eval(testcall)
602  raise AssertError("Too many failures")
603 
604  def failure_probability(self, testcall):
605  """Estimate how likely a given block of code is to raise an
606  AssertionError."""
607  failures=0
608  tries=0.0
609  while failures < 10 and tries <1000:
610  try:
611  eval(testcall)
612  except:
613  failures=failures+1
614  tries=tries+1
615  return failures/tries
616 
617  def randomize_particles(self, particles, deviation):
618  """Randomize the xyz coordinates of a list of particles"""
619 # Note: cannot use XYZ here since that pulls in IMP.core
620  xkey = IMP.FloatKey("x")
621  ykey = IMP.FloatKey("y")
622  zkey = IMP.FloatKey("z")
623  for p in particles:
624  p.set_value(xkey, random.uniform(-deviation, deviation))
625  p.set_value(ykey, random.uniform(-deviation, deviation))
626  p.set_value(zkey, random.uniform(-deviation, deviation))
627 
628  def particle_distance(self, p1, p2):
629  """Return distance between two given particles"""
630  xkey = IMP.FloatKey("x")
631  ykey = IMP.FloatKey("y")
632  zkey = IMP.FloatKey("z")
633  dx = p1.get_value(xkey) - p2.get_value(xkey)
634  dy = p1.get_value(ykey) - p2.get_value(ykey)
635  dz = p1.get_value(zkey) - p2.get_value(zkey)
636  return math.sqrt(dx*dx + dy*dy + dz*dz)
637 
638  def check_unary_function_deriv(self, func, lb, ub, step):
639  """Check the unary function func's derivatives against numerical
640  approximations between lb and ub"""
641  for f in [lb + i * step for i in range(1, int((ub-lb)/step))]:
642  (v,d)= func.evaluate_with_derivative(f)
643  da = numerical_derivative(func.evaluate, f, step / 10.)
644  self.assertAlmostEqual(d, da, delta=max(abs(.1 *d), 0.01))
645 
646  def check_unary_function_min(self, func, lb, ub, step, expected_fmin):
647  """Make sure that the minimum of the unary function func over the
648  range between lb and ub is at expected_fmin"""
649  fmin, vmin = lb, func.evaluate(lb)
650  for f in [lb + i * step for i in range(1, int((ub-lb)/step))]:
651  v = func.evaluate(f)
652  if v < vmin:
653  fmin, vmin = f, v
654  self.assertAlmostEqual(fmin, expected_fmin, delta=step)
655 
656  def check_get_from(self, obj):
657  """Check that the get_from() static method works correctly"""
658  cls = type(obj)
659  self.assertIsNotNone(cls.get_from(obj))
660  self.assertRaises(ValueError, cls.get_from, IMP.Model())
661 
662  def create_particles_in_box(self, model, num=10,
663  lb= [0,0,0],
664  ub= [10,10,10]):
665  """Create a bunch of particles in a box"""
666  import IMP.algebra
667  lbv=IMP.algebra.Vector3D(lb[0],lb[1],lb[2])
668  ubv=IMP.algebra.Vector3D(ub[0],ub[1],ub[2])
669  ps= []
670  for i in range(0,num):
672  p = self.create_point_particle(model, v[0], v[1], v[2])
673  ps.append(p)
674  return ps
675  def _get_type(self, module, name):
676  return eval('type('+module+"."+name+')')
677  def assertValueObjects(self, module, exceptions_list):
678  "Check that all the C++ classes in the module are values or objects."
679  all= dir(module)
680  ok = set(exceptions_list + module._value_types + module._object_types + module._raii_types + module._plural_types)
681 
682  bad=[]
683  for name in all:
684  if self._get_type(module.__name__, name)==type and not name.startswith("_"):
685  if name.find("SwigPyIterator") != -1:
686  continue
687 # Exclude Python-only classes
688  if not eval('hasattr(%s.%s, "__swig_destroy__")' \
689  % (module.__name__, name)):
690  continue
691  if name in ok:
692  continue
693  bad.append(name)
694  message="All IMP classes should be labeled as values or objects to get memory management correct in Python. The following are not:\n%s\nPlease add an IMP_SWIG_OBJECT or IMP_SWIG_VALUE call to the Python wrapper, or if the class has a good reason to be neither, add the name to the value_object_exceptions list in the IMPModuleTest call." \
695  % (str(bad))
696  self.assertEqual(len(bad), 0, message)
697  for e in exceptions_list:
698  self.assertTrue(e not in module._value_types
699  + module._object_types
700  + module._raii_types
701  + module._plural_types,
702  "Value/Object exception "+e+" is not an exception")
703 
704  def _check_spelling(self, word, words):
705  """Check that the word is spelled correctly"""
706  if "words" not in dir(self):
707  with open(IMP.test.get_data_path("linux.words"), "r") as fh:
708  wordlist= fh.read().split("\n")
709 # why is "all" missing on my mac?
710  custom_words=["info", "prechange", "int", "ints", "optimizeds", "graphviz",
711  "voxel", "voxels", "endian", 'rna', 'dna',
712  "xyzr", "pdbs", "fft", "ccc", "gaussian"]
713 # Exclude some common alternative spellings - we want to
714 # be consistent
715  exclude_words = set(["adapter", "grey"])
716  self.words=set(wordlist+custom_words) - exclude_words
717  if self.words:
718  for i in "0123456789":
719  if i in word:
720  return True
721  if word in words:
722  return True
723  if word in self.words:
724  return True
725  else:
726  return False
727  else:
728  return True
729  def assertClassNames(self, module, exceptions, words):
730  """Check that all the classes in the module follow the imp naming conventions."""
731  all= dir(module)
732  misspelled = []
733  bad=[]
734  cc=re.compile("([A-Z][a-z]*)")
735  for name in all:
736  if self._get_type(module.__name__, name)==type and not name.startswith("_"):
737  if name.find("SwigPyIterator") != -1:
738  continue
739  for t in re.findall(cc, name):
740  if not self._check_spelling(t.lower(), words):
741  misspelled.append(t.lower())
742  bad.append(name)
743 
744  self.assertEqual(len(bad), 0,
745  "All IMP classes should be properly spelled. The following are not: %s.\nMisspelled words: %s. Add words to the spelling_exceptions variable of the IMPModuleTest if needed." \
746  % (str(bad), ", ".join(set(misspelled))))
747 
748  for name in all:
749  if self._get_type(module.__name__, name)==type and not name.startswith("_"):
750  if name.find("SwigPyIterator") != -1:
751  continue
752  if name.find('_') != -1:
753  bad.append(name)
754  if name.lower== name:
755  bad.append(name)
756  for t in re.findall(cc, name):
757  if not self._check_spelling(t.lower(), words):
758  print("misspelled %s in %s" % (t, name))
759  bad.append(name)
760  message="All IMP classes should have CamelCase names. The following do not: %s." \
761  % ("\n".join(bad))
762  self.assertEqual(len(bad), 0, message)
763 
764  def _check_function_name(self, prefix, name, verbs, all, exceptions, words,
765  misspelled):
766  if prefix:
767  fullname=prefix+"."+name
768  else:
769  fullname=name
770  old_exceptions=['unprotected_evaluate', "unprotected_evaluate_if_good",
771  "unprotected_evaluate_if_below",
772  "after_evaluate", "before_evaluate", "has_attribute",
773  "decorate_particle","particle_is_instance"]
774  if name in old_exceptions:
775  return []
776 #print "name", fullname
777  if fullname in exceptions:
778  return []
779  if name.endswith("swigregister"):
780  return []
781  if name.lower() != name:
782  if name[0].lower() != name[0] and name.split('_')[0] in all:
783 # static methods
784  return []
785  else:
786  return [fullname]
787  tokens= name.split("_")
788  if tokens[0] not in verbs:
789  return [fullname]
790  for t in tokens:
791  if not self._check_spelling(t, words):
792  misspelled.append(t)
793  print("misspelled %s in %s" % (t, name))
794  return [fullname]
795  return []
796 
797  def _static_method(self, module, prefix, name):
798  """For static methods of the form Foo.bar SWIG creates free functions
799  named Foo_bar. Exclude these from spelling checks since the method
800  Foo.bar has already been checked."""
801  if prefix is None and '_' in name:
802  modobj = eval(module)
803  cls, meth = name.split('_', 1)
804  if hasattr(modobj, cls):
805  clsobj = eval(module + '.' + cls)
806  if hasattr(clsobj, meth):
807  return True
808 
809  def _check_function_names(self, module, prefix, names, verbs, all,
810  exceptions, words, misspelled):
811  bad=[]
812  for name in names:
813  typ = self._get_type(module, name)
814  if name.startswith("_") or name =="weakref_proxy":
815  continue
816  if typ in (types.BuiltinMethodType, types.MethodType) \
817  or (typ == types.FunctionType and \
818  not self._static_method(module, prefix, name)):
819  bad.extend(self._check_function_name(prefix, name, verbs, all,
820  exceptions, words,
821  misspelled))
822  if typ == type and "SwigPyIterator" not in name:
823  members=eval("dir("+module+"."+name+")")
824  bad.extend(self._check_function_names(module+"."+name,
825  name, members, verbs, [],
826  exceptions, words,
827  misspelled))
828  return bad
829 
830  def assertFunctionNames(self, module, exceptions, words):
831  """Check that all the functions in the module follow the IMP
832  naming conventions."""
833  all= dir(module)
834  verbs=set(["add", "remove", "get", "set", "evaluate", "compute", "show", "create", "destroy",
835  "push", "pop", "write", "read", "do", "show", "load", "save", "reset",
836  "accept", "reject",
837  "clear", "handle", "update", "apply", "optimize", "reserve", "dump",
838  "propose", "setup", "teardown", "visit", "find", "run", "swap", "link",
839  "validate"])
840  misspelled = []
841  bad=self._check_function_names(module.__name__, None, all, verbs, all, exceptions, words, misspelled)
842  message="All IMP methods and functions should have lower case names separated by underscores and beginning with a verb, preferably one of ['add', 'remove', 'get', 'set', 'create', 'destroy']. Each of the words should be a properly spelled English word. The following do not (given our limited list of verbs that we check for):\n%(bad)s\nIf there is a good reason for them not to (eg it does start with a verb, just one with a meaning that is not covered by the normal list), add them to the function_name_exceptions variable in the standards_exceptions file. Otherwise, please fix. The current verb list is %(verbs)s" \
843  % {"bad":"\n".join(bad), "verbs":verbs}
844  if len(misspelled) > 0:
845  message += "\nMisspelled words: " + ", ".join(set(misspelled)) \
846  + ". Add words to the spelling_exceptions variable " \
847  + "of the standards_exceptions file if needed."
848  self.assertEqual(len(bad), 0, message)
849 
850 
851  def assertShow(self, modulename, exceptions):
852  """Check that all the classes in modulename have a show method"""
853  all= dir(modulename)
854  not_found=[]
855  for f in all:
856 # Exclude SWIG C global variables object
857  if f == 'cvar':
858  continue
859 # Exclude Python-only classes; they are all showable
860  if not eval('hasattr(%s.%s, "__swig_destroy__")' \
861  % (modulename.__name__, f)):
862  continue
863  if self._get_type(modulename.__name__, f) == type \
864  and not f.startswith("_") \
865  and not f.endswith("_swigregister")\
866  and f not in exceptions\
867  and not f.endswith("Temp") and not f.endswith("Iterator")\
868  and not f.endswith("Exception") and\
869  f not in modulename._raii_types and \
870  f not in modulename._plural_types:
871  if not hasattr(getattr(modulename, f), 'show'):
872  not_found.append(f)
873  message="All IMP classes should support show and __str__. The following do not:\n%s\n If there is a good reason for them not to, add them to the show_exceptions variable in the IMPModuleTest call. Otherwise, please fix." \
874  % "\n".join(not_found)
875  self.assertEqual(len(not_found), 0, message)
876  for e in exceptions:
877  self.assertIn(e, all, "Show exception "+e+" is not a class in module")
878  self.assertTrue(not hasattr(getattr(modulename, e), 'show'),
879  "Exception "+e+" is not really a show exception")
880 
881  def run_example(self, filename):
882  """Run the named example script.
883  A dictionary of all the script's global variables is returned.
884  This can be queried in a test case to make sure the example
885  performed correctly."""
886  class _FatalError(Exception): pass
887 
888 # Add directory containing the example to sys.path, so it can import
889 # other Python modules in the same directory
890  path, name = os.path.split(filename)
891  oldsyspath = sys.path[:]
892  olssysargv= sys.argv[:]
893  sys.path.insert(0, path)
894  sys.argv=[filename]
895  vars = {}
896  try:
897  try:
898  exec(open(filename).read(), vars)
899 # Catch sys.exit() called from within the example; a non-zero exit
900 # value should cause the test case to fail
901  except SystemExit as e:
902  if e.code != 0 and e.code is not None:
903  raise _FatalError("Example exit with code %s" % str(e.code))
904  finally:
905 # Restore sys.path (note that Python 2.3 does not allow
906 # try/except/finally, so we need to use nested trys)
907  sys.path = oldsyspath
908  sys.argv= olssysargv
909 
910  return _ExecDictProxy(vars)
911 
912  def run_python_module(self, module, args):
913  """Run a Python module as if with "python -m <modname>",
914  with the given list of arguments as sys.argv.
915 
916  If module is an already-imported Python module, run its 'main'
917  function and return the result.
918 
919  If module is a string, run the module in a subprocess and return
920  a subprocess.Popen-like object containing the child stdin,
921  stdout and stderr.
922  """
923  if type(module) == type(os):
924  mod = module
925  else:
926  mod = __import__(module, {}, {}, [''])
927  modpath = mod.__file__
928  if modpath.endswith('.pyc'):
929  modpath = modpath[:-1]
930  if type(module) == type(os):
931  old_sys_argv = sys.argv
932 # boost parser doesn't like being called multiple times per process
933  IMP.OptionParser._use_boost_parser = False
934  try:
935  sys.argv = [modpath] + args
936  return module.main()
937  finally:
938  IMP.OptionParser._use_boost_parser = True
939  sys.argv = old_sys_argv
940  else:
941  return _SubprocessWrapper(sys.executable, [modpath] + args)
942 
943  def check_runnable_python_module(self, module):
944  """Check a Python module designed to be runnable with 'python -m'
945  to make sure it supports standard command line options."""
946 # --help should return with exit 0, no errors
947  r = self.run_python_module(module, ['--help'])
948  out, err = r.communicate()
949  self.assertEqual(r.returncode, 0)
950  self.assertNotEqual(err, "")
951  self.assertEqual(out, "")
952 
953 
954 class _ExecDictProxy(object):
955  """exec returns a Python dictionary, which contains IMP objects, other
956  Python objects, as well as base Python modules (such as sys and
957  __builtins__). If we just delete this dictionary, it is entirely
958  possible that base Python modules are removed from the dictionary
959  *before* some IMP objects. This will prevent the IMP objects' Python
960  destructors from running properly, so C++ objects will not be
961  cleaned up. This class proxies the base dict class, and on deletion
962  attempts to remove keys from the dictionary in an order that allows
963  IMP destructors to fire."""
964  def __init__(self, d):
965  self._d = d
966  def __del__(self):
967 # Try to release example objects in a sensible order
968  module_type = type(IMP)
969  d = self._d
970  for k in d.keys():
971  if type(d[k]) != module_type:
972  del d[k]
973 
974  for meth in ['__contains__', '__getitem__', '__iter__', '__len__',
975  'get', 'has_key', 'items', 'keys', 'values']:
976  exec("def %s(self, *args, **keys): "
977  "return self._d.%s(*args, **keys)" % (meth, meth))
978 
979 
980 class _TestResult(unittest.TextTestResult):
981 
982  def __init__(self, stream=None, descriptions=None, verbosity=None):
983  super(_TestResult, self).__init__(stream, descriptions, verbosity)
984  self.all_tests = []
985 
986  def stopTestRun(self):
987  if 'IMP_TEST_DETAIL_DIR' in os.environ:
988  fname = os.path.join(os.environ['IMP_TEST_DETAIL_DIR'],
989  os.path.basename(sys.argv[0]))
990  with open(fname, 'wb') as fh:
991  pickle.dump(self.all_tests, fh, -1)
992  super(_TestResult, self).stopTestRun()
993 
994  def startTest(self, test):
995  super(_TestResult, self).startTest(test)
996  test.start_time=datetime.datetime.now()
997 
998  def _test_finished(self, test, state, detail=None):
999  delta = datetime.datetime.now() - test.start_time
1000  try:
1001  pv= delta.total_seconds()
1002  except AttributeError:
1003  pv = (float(delta.microseconds) \
1004  + (delta.seconds + delta.days * 24 * 3600) * 10**6) / 10**6
1005  if pv > 1:
1006  self.stream.write("in %.3fs ... " % pv)
1007  if detail is not None and not isinstance(detail, str):
1008  detail = self._exc_info_to_string(detail, test)
1009  test_doc = self.getDescription(test)
1010  test_name = test.id()
1011  if test_name.startswith('__main__.'):
1012  test_name = test_name[9:]
1013  self.all_tests.append({'name': test_name,
1014  'docstring': test_doc,
1015  'time': pv, 'state': state, 'detail': detail})
1016 
1017  def addSuccess(self, test):
1018  self._test_finished(test, 'OK')
1019  super(_TestResult, self).addSuccess(test)
1020 
1021  def addError(self, test, err):
1022  self._test_finished(test, 'ERROR', err)
1023  super(_TestResult, self).addError(test, err)
1024 
1025  def addFailure(self, test, err):
1026  self._test_finished(test, 'FAIL', err)
1027  super(_TestResult, self).addFailure(test, err)
1028 
1029  def addSkip(self, test, reason):
1030  self._test_finished(test, 'SKIP', reason)
1031  super(_TestResult, self).addSkip(test, reason)
1032 
1033  def addExpectedFailure(self, test, err):
1034  self._test_finished(test, 'EXPFAIL', err)
1035  super(_TestResult, self).addExpectedFailure(test, err)
1036 
1037  def addUnexpectedSuccess(self, test):
1038  self._test_finished(test, 'UNEXPSUC')
1039  super(_TestResult, self).addUnexpectedSuccess(test)
1040 
1041  def getDescription(self, test):
1042  doc_first_line = test.shortDescription()
1043  if self.descriptions and doc_first_line:
1044  return doc_first_line
1045  else:
1046  return str(test)
1047 
1048 
1049 class _TestRunner(unittest.TextTestRunner):
1050  def _makeResult(self):
1051  return _TestResult(self.stream, self.descriptions, self.verbosity)
1052 
1053 
1054 def main(*args, **keys):
1055  """Run a set of tests; similar to unittest.main().
1056  Obviates the need to separately import the 'unittest' module, and
1057  ensures that main() is from the same unittest module that the
1058  IMP.test testcases are. In addition, turns on some extra checks
1059  (e.g. trying to use deprecated code will cause an exception
1060  to be thrown)."""
1061  import IMP
1063  return unittest.main(testRunner=_TestRunner, *args, **keys)
1064 
1065 import subprocess
1066 class _SubprocessWrapper(subprocess.Popen):
1067  def __init__(self, app, args):
1068 # For (non-Python) applications to work on Windows, the
1069 # PATH must include the directory containing built DLLs
1070  if sys.platform == 'win32' and app != sys.executable:
1071 # Hack to find the location of build/lib/
1072  libdir = os.environ['PYTHONPATH'].split(';')[0]
1073  env = os.environ.copy()
1074  env['PATH'] += ';' + libdir
1075  else:
1076  env = None
1077  subprocess.Popen.__init__(self, [app]+list(args),
1078  stdin=subprocess.PIPE,
1079  stdout=subprocess.PIPE,
1080  stderr=subprocess.PIPE, env=env,
1081  universal_newlines=True)
1082 
1083 
1085  """Super class for simple IMP application test cases"""
1086  def _get_application_file_name(self, filename):
1087 # If we ran from run-all-tests.py, it set an env variable for us with
1088 # the top-level test directory
1089  if sys.platform == 'win32':
1090  filename += '.exe'
1091 #if 'IMP_BUILD_ROOT' in os.environ:
1092 # testdir = os.environ['IMP_BUILD_ROOT']
1093 # return os.path.join(testdir, "build", "bin", filename)
1094  return filename
1095 
1096  def run_application(self, app, args):
1097  """Run an application with the given list of arguments.
1098  @return a subprocess.Popen-like object containing the child stdin,
1099  stdout and stderr.
1100  """
1101  filename = self._get_application_file_name(app)
1102  if sys.platform == 'win32':
1103 # Cannot rely on PATH on wine builds, so use full pathname
1104  return _SubprocessWrapper(os.path.join(os.environ['IMP_BIN_DIR'],
1105  filename), args)
1106  else:
1107  return _SubprocessWrapper(filename, args)
1108 
1109  def run_python_application(self, app, args):
1110  """Run a Python application with the given list of arguments.
1111  The Python application should be self-runnable (i.e. it should
1112  be executable and with a #! on the first line).
1113  @return a subprocess.Popen-like object containing the child stdin,
1114  stdout and stderr.
1115  """
1116 # Handle platforms where /usr/bin/python doesn't work
1117  if sys.executable != '/usr/bin/python' and 'IMP_BIN_DIR' in os.environ:
1118  return _SubprocessWrapper(sys.executable,
1119  [os.path.join(os.environ['IMP_BIN_DIR'], app)] + args)
1120  else:
1121  return _SubprocessWrapper(app, args)
1122 
1124  """Import an installed Python application, rather than running it.
1125  This is useful to directly test components of the application.
1126  @return the Python module object."""
1127  try:
1128  import importlib.machinery
1129  imp = None
1130  except ImportError:
1131  import imp
1132  name = os.path.splitext(app)[0]
1133  pathname = os.path.join(os.environ['IMP_BIN_DIR'], app)
1134  if imp is None:
1135  return importlib.machinery.SourceFileLoader(name,
1136  pathname).load_module()
1137  else:
1138  return imp.load_source(name, pathname)
1139 
1140  def run_script(self, app, args):
1141  """Run an application with the given list of arguments.
1142  @return a subprocess.Popen-like object containing the child stdin,
1143  stdout and stderr.
1144  """
1145  return _SubprocessWrapper(sys.executable, [app]+args)
1146 
1147  def assertApplicationExitedCleanly(self, ret, error):
1148  """Assert that the application exited cleanly (return value = 0)."""
1149  if ret < 0:
1150  raise OSError("Application exited with signal %d\n" % -ret\
1151  +error)
1152  else:
1153  self.assertEqual(ret, 0,
1154  "Application exited uncleanly, with exit code %d\n" % ret\
1155  + error)
1156 
1157  def read_shell_commands(self, doxfile):
1158  """Read and return a set of shell commands from a doxygen file.
1159  Each command is assumed to be in a \\code{.sh}...\\endcode block.
1160  The doxygen file is specified relative to the test file itself.
1161  This is used to make sure the commands shown in an application
1162  example actually work (the testcase can also check the resulting
1163  files for correctness)."""
1164  def win32_normpath(p):
1165 # Sometimes Windows can read Unix-style paths, but sometimes it
1166 # gets confused... so normalize all paths to be sure
1167  return " ".join([os.path.normpath(x) for x in p.split()])
1168  def fix_win32_command(cmd):
1169 # Make substitutions so a Unix shell command works on Windows
1170  if cmd.startswith('cp -r '):
1171  return 'xcopy /E ' + win32_normpath(cmd[6:])
1172  elif cmd.startswith('cp '):
1173  return 'copy ' + win32_normpath(cmd[3:])
1174  else:
1175  return cmd
1176  d = os.path.dirname(sys.argv[0])
1177  doc = os.path.join(d, doxfile)
1178  inline = False
1179  cmds = []
1180  example_path = os.path.abspath(IMP.get_example_path('..'))
1181  with open(doc) as fh:
1182  for line in fh.readlines():
1183  if '\code{.sh}' in line:
1184  inline = True
1185  elif '\endcode' in line:
1186  inline = False
1187  elif inline:
1188  cmds.append(line.rstrip('\r\n').replace(
1189  '<imp_example_path>', example_path))
1190  if sys.platform == 'win32':
1191  cmds = [fix_win32_command(x) for x in cmds]
1192  return cmds
1193 
1194  def run_shell_command(self, cmd):
1195  "Print and run a shell command, as returned by read_shell_commands()"
1196  import subprocess
1197  print(cmd)
1198  p = subprocess.call(cmd, shell=True)
1199  if p != 0:
1200  raise OSError("%s failed with exit value %d" % (cmd, p))
1201 
1202 
1203 class RefCountChecker(object):
1204  """Check to make sure the number of C++ object references is as expected"""
1205 
1206  def __init__(self, testcase):
1207 # Make sure no director objects are hanging around; otherwise these
1208 # may be unexpectedly garbage collected later, decreasing the
1209 # live object count
1210  IMP._director_objects.cleanup()
1211  self.__testcase = testcase
1212  if IMP.get_check_level() >= IMP.USAGE_AND_INTERNAL:
1213  self.__basenum = IMP.Object.get_number_of_live_objects()
1214  self.__names= IMP.get_live_object_names()
1215 
1216  def assert_number(self, expected):
1217  "Make sure that the number of references matches the expected value."
1218  t = self.__testcase
1219  IMP._director_objects.cleanup()
1220  if IMP.get_check_level() >= IMP.USAGE_AND_INTERNAL:
1221  newnames=[x for x in IMP.get_live_object_names() if x not in self.__names]
1222  newnum=IMP.Object.get_number_of_live_objects()-self.__basenum
1223  t.assertEqual(newnum, expected,
1224  "Number of objects don't match: "\
1225  +str(newnum)\
1226  +" != "+ str(expected) +" found "+\
1227  str(newnames))
1228 
1229 
1230 
1232  """Check to make sure the number of director references is as expected"""
1233 
1234  def __init__(self, testcase):
1235  IMP._director_objects.cleanup()
1236  self.__testcase = testcase
1237  self.__basenum = IMP._director_objects.get_object_count()
1238 
1239  def assert_number(self, expected, force_cleanup=True):
1240  """Make sure that the number of references matches the expected value.
1241  If force_cleanup is set, clean up any unused references first before
1242  doing the assertion.
1243  """
1244  t = self.__testcase
1245  if force_cleanup:
1246  IMP._director_objects.cleanup()
1247  t.assertEqual(IMP._director_objects.get_object_count() \
1248  - self.__basenum, expected)
1249 
1250 # Make sure that the IMP binary directory (build/bin) is in the PATH, if
1251 # we're running under wine (the imppy.sh script normally ensures this, but
1252 # wine overrides the PATH). This is needed so that tests of imported Python
1253 # applications can successfully spawn C++ applications (e.g. idock.py tries
1254 # to run recompute_zscore.exe). build/lib also needs to be in the PATH, since
1255 # that's how Windows locates dependent DLLs such as libimp.dll.
1256 if sys.platform == 'win32' and 'PYTHONPATH' in os.environ \
1257  and 'IMP_BIN_DIR' in os.environ:
1258  libdir = os.environ['PYTHONPATH'].split(';')[0]
1259  bindir = os.environ['IMP_BIN_DIR']
1260  path = os.environ['PATH']
1261  if libdir not in path or bindir not in path:
1262  os.environ['PATH'] = bindir + ';' + libdir + ';' + path
1263 
1264 
1265 def get_module_version():
1266  """get_module_version() -> std::string const"""
1267  return _IMP_test.get_module_version()
1268 
1269 def get_example_path(fname):
1270  """get_example_path(std::string fname) -> std::string"""
1271  return _IMP_test.get_example_path(fname)
1272 
1273 def get_data_path(fname):
1274  """get_data_path(std::string fname) -> std::string"""
1275  return _IMP_test.get_data_path(fname)
1276 
1277 from . import _version_check
1278 _version_check.check_version(get_module_version())
1279 __version__ = get_module_version()
1280 
1281 
1282 
def run_python_module
Run a Python module as if with "python -m <modname>", with the given list of arguments as sys...
def assertApplicationExitedCleanly
Assert that the application exited cleanly (return value = 0).
def check_get_from
Check that the get_from() static method works correctly.
CheckLevel get_check_level()
Get the current audit mode.
Definition: exception.h:81
def import_python_application
Import an installed Python application, rather than running it.
def open_input_file
Open and return an input file in the top-level test directory.
def run_application
Run an application with the given list of arguments.
def randomize_particles
Randomize the xyz coordinates of a list of particles.
A general exception for an internal error in IMP.
Definition: exception.h:102
def main
Run a set of tests; similar to unittest.main().
An exception for an invalid usage of IMP.
Definition: exception.h:123
Super class for simple IMP application test cases.
def assertRaisesInternalException
Assert that the given callable object raises InternalException.
std::string get_example_path(std::string file_name)
Return the full path to one of this module's example files.
def assert_number
Make sure that the number of references matches the expected value.
Check to make sure the number of director references is as expected.
def assertShow
Check that all the classes in modulename have a show method.
def run_shell_command
Print and run a shell command, as returned by read_shell_commands()
def assertRaisesUsageException
Assert that the given callable object raises UsageException.
Vector3D get_random_vector_in(const Cylinder3D &c)
Generate a random vector in a cylinder with uniform density.
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:72
def run_python_application
Run a Python application with the given list of arguments.
def assert_number
Make sure that the number of references matches the expected value.
Simple RAII-style class to run in a temporary directory.
Strings get_live_object_names()
Return the names of all live objects.
def particle_distance
Return distance between two given particles.
def check_unary_function_deriv
Check the unary function func's derivatives against numerical approximations between lb and ub...
def get_tmp_file_name
Get the full name of an output file in the tmp directory.
def run_example
Run the named example script.
void set_deprecation_exceptions(bool tf)
Toggle whether an exception is thrown when a deprecated method is used.
def check_unary_function_min
Make sure that the minimum of the unary function func over the range between lb and ub is at expected...
def probabilistic_test
Help handle a test which is expected to fail some fraction of the time.
def create_particles_in_box
Create a bunch of particles in a box.
General purpose algebraic and geometric methods that are expected to be used by a wide variety of IMP...
def assertNotImplemented
Assert that the given callable object is not implemented.
The general base class for IMP exceptions.
Definition: exception.h:49
def numerical_derivative
Calculate the derivative of the single-value function func at point val.
std::string get_example_path(std::string file_name)
Return the full path to one of this module's example files.
def xyz_numerical_derivatives
Calculate the x,y and z derivatives of the scoring function sf on the xyz particle.
def failure_probability
Estimate how likely a given block of code is to raise an AssertionError.
VectorD< 3 > Vector3D
Definition: VectorD.h:395
def assertClassNames
Check that all the classes in the module follow the imp naming conventions.
def create_point_particle
Make a particle with optimizable x, y and z attributes, and add it to the model.
Class to handle individual model particles.
Definition: Particle.h:37
def read_shell_commands
Read and return a set of shell commands from a doxygen file.
std::string get_data_path(std::string file_name)
Return the full path to one of this module's data files.
Check to make sure the number of C++ object references is as expected.
Simple RAII-style class to make a temporary directory.
def assertFunctionNames
Check that all the functions in the module follow the IMP naming conventions.
def assertValueObjects
Check that all the C++ classes in the module are values or objects.
Super class for IMP test cases.
def assertXYZDerivativesInTolerance
Assert that x,y,z analytical derivatives match numerical within a tolerance, or a percentage (of the ...
def run_script
Run an application with the given list of arguments.
def get_input_file_name
Get the full name of an input file in the top-level test directory.
def check_runnable_python_module
Check a Python module designed to be runnable with 'python -m' to make sure it supports standard comm...
void set_check_level(CheckLevel tf)
Control runtime checks in the code.
Definition: exception.h:73