1 """@namespace IMP::test
2 @brief Methods and classes for testing the IMP kernel and modules.
19 from unittest.util
import safe_repr
25 from pathlib
import Path
27 from IMP._compat_pathlib
import Path
31 expectedFailure = unittest.expectedFailure
33 skipIf = unittest.skipIf
34 skipUnless = unittest.skipUnless
37 class _TempDir(object):
38 def __init__(self, dir=None):
39 self.tmpdir = tempfile.mkdtemp(dir=dir)
42 shutil.rmtree(self.tmpdir, ignore_errors=
True)
45 @contextlib.contextmanager
47 """Simple context manager to run in a temporary directory.
48 While the context manager is active (within the 'with' block)
49 the current working directory is set to a temporary directory.
50 When the context manager exists, the working directory is reset
51 and the temporary directory deleted."""
53 tmpdir = tempfile.mkdtemp()
57 shutil.rmtree(tmpdir, ignore_errors=
True)
60 @contextlib.contextmanager
62 """Simple context manager to make a temporary directory.
63 The temporary directory has the same lifetime as the context manager
64 (i.e. it is created at the start of the 'with' block, and deleted
65 at the end of the block).
66 @param dir If given, the temporary directory is made as a subdirectory
67 of that directory, rather than in the default temporary
68 directory location (e.g. /tmp)
69 @return the full path to the temporary directory.
71 tmpdir = tempfile.mkdtemp(dir=dir)
73 shutil.rmtree(tmpdir, ignore_errors=
True)
77 """Calculate the derivative of the single-value function `func` at
78 point `val`. The derivative is calculated using simple finite
79 differences starting with the given `step`; Richardson extrapolation
80 is then used to extrapolate the derivative at step=0."""
88 d = [[(f1 - f2) / (2.0 * step)]]
90 for i
in range(1, maxsteps):
91 d.append([0.] * (i + 1))
95 d[i][0] = (f1 - f2) / (2.0 * step)
97 for j
in range(1, i + 1):
98 d[i][j] = (d[i][j-1] * fac - d[i-1][j-1]) / (fac - 1.)
100 errt = max(abs(d[i][j] - d[i][j-1]),
101 abs(d[i][j] - d[i-1][j-1]))
105 if abs(d[i][i] - d[i-1][i-1]) >= safe * err:
108 raise ValueError(
"Cannot calculate numerical derivative")
113 """Calculate the x,y and z derivatives of the scoring function `sf`
114 on the `xyz` particle. The derivatives are approximated numerically
115 using the numerical_derivatives() function."""
116 class _XYZDerivativeFunc(object):
117 def __init__(self, sf, xyz, basis_vector):
120 self._basis_vector = basis_vector
121 self._starting_coordinates = xyz.get_coordinates()
123 def __call__(self, val):
124 self._xyz.set_coordinates(self._starting_coordinates +
125 self._basis_vector * val)
126 return self._sf.evaluate(
False)
131 for x
in ((1, 0, 0), (0, 1, 0), (0, 0, 1))])
135 """Super class for IMP test cases.
136 This provides a number of useful IMP-specific methods on top of
137 the standard Python `unittest.TestCase` class.
138 Test scripts should generally contain a subclass of this class,
139 conventionally called `Tests` (this makes it easier to run an
140 individual test from the command line) and use IMP::test::main()
141 as their main function."""
145 if not hasattr(unittest.TestCase,
'assertRegex'):
146 assertRegex = unittest.TestCase.assertRegexpMatches
147 assertNotRegex = unittest.TestCase.assertNotRegexpMatches
149 def __init__(self, *args, **keys):
150 unittest.TestCase.__init__(self, *args, **keys)
151 self._progname = Path(sys.argv[0]).absolute()
159 IMP.random_number_generator.seed(hash(time.time()) % 2**30)
165 if hasattr(self,
'_tmpdir'):
169 """Get the full name of an input file in the top-level
171 if self.__module__ ==
'__main__':
172 testdir = self._progname
174 testdir = Path(sys.modules[self.__module__].__file__)
175 for p
in testdir.parents:
178 ret = input / filename
180 raise IOError(
"Test input file %s does not exist" % ret)
182 raise IOError(
"No test input directory found")
185 """Open and return an input file in the top-level test directory."""
189 """Get the full name of an output file in the tmp directory.
190 The directory containing this file will be automatically
191 cleaned up when the test completes."""
192 if not hasattr(self,
'_tmpdir'):
193 self._tmpdir = _TempDir(os.environ.get(
'IMP_TMP_DIR'))
194 tmpdir = self._tmpdir.tmpdir
195 return str(Path(tmpdir) / filename)
198 """Get the magnitude of a list of floats"""
199 return sum(x*x
for x
in vector)**.5
202 """Assert that the given callable object raises UsageException.
203 This differs from unittest's assertRaises in that the test
204 is skipped in fast mode (where usage checks are turned off)."""
209 """Assert that the given callable object raises InternalException.
210 This differs from unittest's assertRaises in that the test
211 is skipped in fast mode (where internal checks are turned off)."""
216 """Assert that the given callable object is not implemented."""
221 """Assert that x,y,z analytical derivatives match numerical within
222 a tolerance, or a percentage (of the analytical value), whichever
223 is larger. `sf` should be a ScoringFunction or Restraint."""
225 derivs = xyz.get_derivatives()
227 pct = percentage / 100.0
228 self.assertAlmostEqual(
231 msg=
"Don't match "+str(derivs) + str(num_derivs))
232 self.assertAlmostEqual(derivs[0], num_derivs[0],
233 delta=max(tolerance, abs(derivs[0]) * pct))
234 self.assertAlmostEqual(derivs[1], num_derivs[1],
235 delta=max(tolerance, abs(derivs[1]) * pct))
236 self.assertAlmostEqual(derivs[2], num_derivs[2],
237 delta=max(tolerance, abs(derivs[2]) * pct))
240 """Fail if the given numpy array doesn't match expected"""
241 if IMP.IMP_KERNEL_HAS_NUMPY:
243 self.assertIsInstance(numpy_array, numpy.ndarray)
244 numpy.testing.assert_array_equal(numpy_array, exp_array)
246 self.assertEqual(numpy_array, exp_array)
250 """Fail if the difference between any two items in the two sequences
251 are exceed the specified number of places or delta. See
254 if delta
is not None and places
is not None:
255 raise TypeError(
"specify delta or places not both")
258 ftypename = ftype.__name__
260 stypename = stype.__name__
262 raise self.failureException(
263 'Sequences are of different types: %s != %s' % (
264 ftypename, stypename))
268 except (NotImplementedError, TypeError):
269 raise self.failureException(
270 'First %s has no length' % (ftypename))
273 except (NotImplementedError, TypeError):
274 raise self.failureException(
275 'Second %s has no length' % (stypename))
278 raise self.failureException(
279 'Sequences have non equal lengths: %d != %d' % (flen, slen))
282 for i
in range(min(flen, slen)):
283 differing =
'%ss differ: %s != %s\n' % (
284 ftypename.capitalize(), safe_repr(first),
289 except (TypeError, IndexError, NotImplementedError):
290 differing += (
'\nUnable to index element %d of first %s\n' %
296 except (TypeError, IndexError, NotImplementedError):
297 differing += (
'\nUnable to index element %d of second %s\n' %
302 self.assertAlmostEqual(
303 f, s, places=places, msg=msg, delta=delta)
304 except (TypeError, ValueError, NotImplementedError,
307 "\nFirst differing element "
308 "%d:\n%s\n%s\n") % (i, safe_repr(f), safe_repr(s))
313 standardMsg = differing
314 diffMsg =
'\n' +
'\n'.join(
315 difflib.ndiff(pprint.pformat(first).splitlines(),
316 pprint.pformat(second).splitlines()))
317 standardMsg = self._truncateMessage(standardMsg, diffMsg)
318 msg = self._formatMessage(msg, standardMsg)
319 raise self.failureException(msg)
322 """Make a particle with optimizable x, y and z attributes, and
323 add it to the model."""
331 """Help handle a test which is expected to fail some fraction of
332 the time. The test is run multiple times and an exception
333 is thrown only if it fails too many times.
334 @note Use of this function should be avoided. If there is a corner
335 case that results in a test 'occasionally' failing, write a
336 new test specifically for that corner case and assert that
337 it fails consistently (and remove the corner case from the
340 prob = chance_of_failure
344 prob = prob*chance_of_failure
345 for i
in range(0, tries):
353 raise AssertionError(
"Too many failures")
356 """Estimate how likely a given block of code is to raise an
360 while failures < 10
and tries < 1000:
366 return failures/tries
369 """Randomize the xyz coordinates of a list of particles"""
375 p.set_value(xkey, random.uniform(-deviation, deviation))
376 p.set_value(ykey, random.uniform(-deviation, deviation))
377 p.set_value(zkey, random.uniform(-deviation, deviation))
380 """Return distance between two given particles"""
384 dx = p1.get_value(xkey) - p2.get_value(xkey)
385 dy = p1.get_value(ykey) - p2.get_value(ykey)
386 dz = p1.get_value(zkey) - p2.get_value(zkey)
387 return math.sqrt(dx*dx + dy*dy + dz*dz)
390 """Check the unary function func's derivatives against numerical
391 approximations between lb and ub"""
392 for f
in [lb + i * step
for i
in range(1, int((ub-lb)/step))]:
393 (v, d) = func.evaluate_with_derivative(f)
395 self.assertAlmostEqual(d, da, delta=max(abs(.1 * d), 0.01))
398 """Make sure that the minimum of the unary function func over the
399 range between lb and ub is at expected_fmin"""
400 fmin, vmin = lb, func.evaluate(lb)
401 for f
in [lb + i * step
for i
in range(1, int((ub-lb)/step))]:
405 self.assertAlmostEqual(fmin, expected_fmin, delta=step)
408 """Check methods that every IMP::Object class should have"""
409 obj.set_was_used(
True)
412 self.assertIsNotNone(cls.get_from(obj))
413 self.assertRaises(ValueError, cls.get_from,
IMP.Model())
415 self.assertIsInstance(str(obj), str)
416 self.assertIsInstance(repr(obj), str)
418 verinf = obj.get_version_info()
427 """Create a bunch of particles in a box"""
432 for i
in range(0, num):
439 def _get_type(self, module, name):
440 return eval(
'type('+module+
"."+name+
')')
443 "Check that all the C++ classes in the module are values or objects."
445 ok = set(exceptions_list + module._value_types + module._object_types
446 + module._raii_types + module._plural_types)
450 if self._get_type(module.__name__, name) == type \
451 and not name.startswith(
"_"):
452 if name.find(
"SwigPyIterator") != -1:
455 if not eval(
'hasattr(%s.%s, "__swig_destroy__")'
456 % (module.__name__, name)):
463 "All IMP classes should be labeled as values or objects to get "
464 "memory management correct in Python. The following are not:\n%s\n"
465 "Please add an IMP_SWIG_OBJECT or IMP_SWIG_VALUE call to the "
466 "Python wrapper, or if the class has a good reason to be "
467 "neither, add the name to the value_object_exceptions list in "
468 "the IMPModuleTest call." % str(bad))
469 for e
in exceptions_list:
471 e
not in module._value_types + module._object_types
472 + module._raii_types + module._plural_types,
473 "Value/Object exception "+e+
" is not an exception")
475 def _check_spelling(self, word, words):
476 """Check that the word is spelled correctly"""
477 if "words" not in dir(self):
479 wordlist = fh.read().split("\n")
481 custom_words = [
"info",
"prechange",
"int",
"ints",
"optimizeds",
482 "graphviz",
"voxel",
"voxels",
"endian",
'rna',
483 'dna',
"xyzr",
"pdbs",
"fft",
"ccc",
"gaussian"]
486 exclude_words = set([
"adapter",
"grey"])
487 self.words = set(wordlist+custom_words) - exclude_words
489 for i
in "0123456789":
494 if word
in self.words:
502 """Check that all the classes in the module follow the IMP
503 naming conventions."""
507 cc = re.compile(
"([A-Z][a-z]*)")
509 if self._get_type(module.__name__, name) == type \
510 and not name.startswith(
"_"):
511 if name.find(
"SwigPyIterator") != -1:
513 for t
in re.findall(cc, name):
514 if not self._check_spelling(t.lower(), words):
515 misspelled.append(t.lower())
520 "All IMP classes should be properly spelled. The following "
521 "are not: %s.\nMisspelled words: %s. Add words to the "
522 "spelling_exceptions variable of the IMPModuleTest if needed."
523 % (str(bad),
", ".join(set(misspelled))))
526 if self._get_type(module.__name__, name) == type \
527 and not name.startswith(
"_"):
528 if name.find(
"SwigPyIterator") != -1:
530 if name.find(
'_') != -1:
532 if name.lower == name:
534 for t
in re.findall(cc, name):
535 if not self._check_spelling(t.lower(), words):
536 print(
"misspelled %s in %s" % (t, name))
540 "All IMP classes should have CamelCase names. The following "
541 "do not: %s." %
"\n".join(bad))
543 def _check_function_name(self, prefix, name, verbs, all, exceptions, words,
546 fullname = prefix+
"."+name
550 'unprotected_evaluate',
"unprotected_evaluate_if_good",
551 "unprotected_evaluate_if_below",
552 'unprotected_evaluate_moved',
"unprotected_evaluate_moved_if_good",
553 "unprotected_evaluate_moved_if_below",
554 "after_evaluate",
"before_evaluate",
"has_attribute",
555 "decorate_particle",
"particle_is_instance"]
556 if name
in old_exceptions:
558 if fullname
in exceptions:
560 if name.endswith(
"swigregister"):
562 if name.lower() != name:
563 if name[0].lower() != name[0]
and name.split(
'_')[0]
in all:
568 tokens = name.split(
"_")
569 if tokens[0]
not in verbs:
572 if not self._check_spelling(t, words):
574 print(
"misspelled %s in %s" % (t, name))
578 def _static_method(self, module, prefix, name):
579 """For static methods of the form Foo.bar SWIG creates free functions
580 named Foo_bar. Exclude these from spelling checks since the method
581 Foo.bar has already been checked."""
582 if prefix
is None and '_' in name:
583 modobj = eval(module)
584 cls, meth = name.split(
'_', 1)
585 if hasattr(modobj, cls):
586 clsobj = eval(module +
'.' + cls)
587 if hasattr(clsobj, meth):
590 def _check_function_names(self, module, prefix, names, verbs, all,
591 exceptions, words, misspelled):
594 typ = self._get_type(module, name)
595 if name.startswith(
"_")
or name ==
"weakref_proxy":
597 if typ
in (types.BuiltinMethodType, types.MethodType) \
598 or (typ == types.FunctionType
and
599 not self._static_method(module, prefix, name)):
600 bad.extend(self._check_function_name(prefix, name, verbs, all,
603 if typ == type
and "SwigPyIterator" not in name:
604 members = eval(
"dir("+module+
"."+name+
")")
605 bad.extend(self._check_function_names(module+
"."+name,
606 name, members, verbs, [],
612 """Check that all the functions in the module follow the IMP
613 naming conventions."""
615 verbs = set([
"add",
"remove",
"get",
"set",
"evaluate",
"compute",
616 "show",
"create",
"destroy",
"push",
"pop",
"write",
617 "read",
"do",
"show",
"load",
"save",
"reset",
"accept",
618 "reject",
"clear",
"handle",
"update",
"apply",
619 "optimize",
"reserve",
"dump",
"propose",
"setup",
620 "teardown",
"visit",
"find",
"run",
"swap",
"link",
621 "validate",
"erase"])
623 bad = self._check_function_names(module.__name__,
None, all, verbs,
624 all, exceptions, words, misspelled)
625 message = (
"All IMP methods and functions should have lower case "
626 "names separated by underscores and beginning with a "
627 "verb, preferably one of ['add', 'remove', 'get', 'set', "
628 "'create', 'destroy']. Each of the words should be a "
629 "properly spelled English word. The following do not "
630 "(given our limited list of verbs that we check for):\n"
631 "%(bad)s\nIf there is a good reason for them not to "
632 "(eg it does start with a verb, just one with a meaning "
633 "that is not covered by the normal list), add them to the "
634 "function_name_exceptions variable in the "
635 "standards_exceptions file. Otherwise, please fix. "
636 "The current verb list is %(verbs)s"
637 % {
"bad":
"\n".join(bad),
"verbs": verbs})
638 if len(misspelled) > 0:
639 message +=
"\nMisspelled words: " +
", ".join(set(misspelled)) \
640 +
". Add words to the spelling_exceptions variable " \
641 +
"of the standards_exceptions file if needed."
642 self.assertEqual(len(bad), 0, message)
645 """Check that all the classes in modulename have a show method"""
646 all = dir(modulename)
653 if not eval(
'hasattr(%s.%s, "__swig_destroy__")'
654 % (modulename.__name__, f)):
656 if self._get_type(modulename.__name__, f) == type \
657 and not f.startswith(
"_") \
658 and not f.endswith(
"_swigregister")\
659 and f
not in exceptions\
660 and not f.endswith(
"Temp")
and not f.endswith(
"Iterator")\
661 and not f.endswith(
"Exception")
and\
662 f
not in modulename._raii_types
and \
663 f
not in modulename._plural_types:
664 if not hasattr(getattr(modulename, f),
'show'):
668 "All IMP classes should support show and __str__. The following "
669 "do not:\n%s\n If there is a good reason for them not to, add "
670 "them to the show_exceptions variable in the IMPModuleTest "
671 "call. Otherwise, please fix." %
"\n".join(not_found))
673 self.assertIn(e, all,
674 "Show exception "+e+
" is not a class in module")
675 self.assertTrue(
not hasattr(getattr(modulename, e),
'show'),
676 "Exception "+e+
" is not really a show exception")
679 """Run the named example script.
680 @return a dictionary of all the script's global variables.
681 This can be queried in a test case to make sure
682 the example performed correctly."""
688 path, name = os.path.split(filename)
689 oldsyspath = sys.path[:]
690 olssysargv = sys.argv[:]
691 sys.path.insert(0, path)
692 sys.argv = [filename]
695 exec(open(filename).read(), vars)
698 except SystemExit
as e:
699 if e.code != 0
and e.code
is not None:
701 "Example exit with code %s" % str(e.code))
704 sys.path = oldsyspath
705 sys.argv = olssysargv
707 return _ExecDictProxy(vars)
710 """Run a Python module as if with "python -m <modname>",
711 with the given list of arguments as sys.argv.
713 If module is an already-imported Python module, run its 'main'
714 function and return the result.
716 If module is a string, run the module in a subprocess and return
717 a subprocess.Popen-like object containing the child stdin,
720 def mock_setup_from_argv(*args, **kwargs):
723 if type(module) == type(os):
726 mod = __import__(module, {}, {}, [
''])
727 modpath = mod.__file__
728 if modpath.endswith(
'.pyc'):
729 modpath = modpath[:-1]
730 if type(module) == type(os):
731 old_sys_argv = sys.argv
733 old_setup = IMP.setup_from_argv
734 IMP.setup_from_argv = mock_setup_from_argv
736 sys.argv = [modpath] + args
739 IMP.setup_from_argv = old_setup
740 sys.argv = old_sys_argv
742 return _SubprocessWrapper(sys.executable, [modpath] + args)
745 """Check a Python module designed to be runnable with 'python -m'
746 to make sure it supports standard command line options."""
749 out, err = r.communicate()
750 self.assertEqual(r.returncode, 0)
751 self.assertNotEqual(err,
"")
752 self.assertEqual(out,
"")
755 class _ExecDictProxy(object):
756 """exec returns a Python dictionary, which contains IMP objects, other
757 Python objects, as well as base Python modules (such as sys and
758 __builtins__). If we just delete this dictionary, it is entirely
759 possible that base Python modules are removed from the dictionary
760 *before* some IMP objects. This will prevent the IMP objects' Python
761 destructors from running properly, so C++ objects will not be
762 cleaned up. This class proxies the base dict class, and on deletion
763 attempts to remove keys from the dictionary in an order that allows
764 IMP destructors to fire."""
765 def __init__(self, d):
770 module_type = type(IMP)
773 if type(d[k]) != module_type:
776 for meth
in [
'__contains__',
'__getitem__',
'__iter__',
'__len__',
777 'get',
'has_key',
'items',
'keys',
'values']:
778 exec(
"def %s(self, *args, **keys): "
779 "return self._d.%s(*args, **keys)" % (meth, meth))
782 class _TestResult(unittest.TextTestResult):
784 def __init__(self, stream=None, descriptions=None, verbosity=None):
785 super(_TestResult, self).__init__(stream, descriptions, verbosity)
788 def stopTestRun(self):
789 if 'IMP_TEST_DETAIL_DIR' in os.environ:
792 protocol = min(pickle.HIGHEST_PROTOCOL, 4)
793 fname = (Path(os.environ[
'IMP_TEST_DETAIL_DIR'])
794 / Path(sys.argv[0]).name)
795 with open(str(fname),
'wb')
as fh:
796 pickle.dump(self.all_tests, fh, protocol)
797 super(_TestResult, self).stopTestRun()
799 def startTest(self, test):
800 super(_TestResult, self).startTest(test)
801 test.start_time = datetime.datetime.now()
803 def _test_finished(self, test, state, detail=None):
804 if hasattr(test,
'start_time'):
805 delta = datetime.datetime.now() - test.start_time
807 pv = delta.total_seconds()
808 except AttributeError:
809 pv = (float(delta.microseconds)
811 + delta.days * 24 * 3600) * 10**6) / 10**6
813 self.stream.write(
"in %.3fs ... " % pv)
818 if detail
is not None and not isinstance(detail, str):
819 detail = self._exc_info_to_string(detail, test)
820 test_doc = self.getDescription(test)
821 test_name = test.id()
822 if test_name.startswith(
'__main__.'):
823 test_name = test_name[9:]
824 self.all_tests.append({
'name': test_name,
825 'docstring': test_doc,
826 'time': pv,
'state': state,
'detail': detail})
828 def addSuccess(self, test):
829 self._test_finished(test,
'OK')
830 super(_TestResult, self).addSuccess(test)
832 def addError(self, test, err):
833 self._test_finished(test,
'ERROR', err)
834 super(_TestResult, self).addError(test, err)
836 def addFailure(self, test, err):
837 self._test_finished(test,
'FAIL', err)
838 super(_TestResult, self).addFailure(test, err)
840 def addSkip(self, test, reason):
841 self._test_finished(test,
'SKIP', reason)
842 super(_TestResult, self).addSkip(test, reason)
844 def addExpectedFailure(self, test, err):
845 self._test_finished(test,
'EXPFAIL', err)
846 super(_TestResult, self).addExpectedFailure(test, err)
848 def addUnexpectedSuccess(self, test):
849 self._test_finished(test,
'UNEXPSUC')
850 super(_TestResult, self).addUnexpectedSuccess(test)
852 def getDescription(self, test):
853 doc_first_line = test.shortDescription()
854 if self.descriptions
and doc_first_line:
855 return doc_first_line
860 class _TestRunner(unittest.TextTestRunner):
861 def _makeResult(self):
862 return _TestResult(self.stream, self.descriptions, self.verbosity)
866 """Run a set of tests; similar to unittest.main().
867 Obviates the need to separately import the 'unittest' module, and
868 ensures that main() is from the same unittest module that the
869 IMP.test testcases are. In addition, turns on some extra checks
870 (e.g. trying to use deprecated code will cause an exception
874 return unittest.main(testRunner=_TestRunner, *args, **keys)
877 class _SubprocessWrapper(subprocess.Popen):
878 def __init__(self, app, args, cwd=None):
881 if sys.platform ==
'win32' and app != sys.executable:
883 libdir = os.environ[
'PYTHONPATH'].split(
';')[0]
884 env = os.environ.copy()
885 env[
'PATH'] +=
';' + libdir
888 subprocess.Popen.__init__(self, [app]+list(args),
889 stdin=subprocess.PIPE,
890 stdout=subprocess.PIPE,
891 stderr=subprocess.PIPE, env=env, cwd=cwd,
892 universal_newlines=
True)
896 """Super class for simple IMP application test cases"""
897 def _get_application_file_name(self, filename):
900 if sys.platform ==
'win32':
905 """Run an application with the given list of arguments.
906 @return a subprocess.Popen-like object containing the child stdin,
909 filename = self._get_application_file_name(app)
910 if sys.platform ==
'win32':
912 return _SubprocessWrapper(os.path.join(os.environ[
'IMP_BIN_DIR'],
913 filename), args, cwd=cwd)
915 return _SubprocessWrapper(filename, args, cwd=cwd)
918 """Run a Python application with the given list of arguments.
919 The Python application should be self-runnable (i.e. it should
920 be executable and with a #! on the first line).
921 @return a subprocess.Popen-like object containing the child stdin,
925 if sys.executable !=
'/usr/bin/python' and 'IMP_BIN_DIR' in os.environ:
926 return _SubprocessWrapper(
928 [os.path.join(os.environ[
'IMP_BIN_DIR'], app)] + args)
930 return _SubprocessWrapper(app, args)
933 """Import an installed Python application, rather than running it.
934 This is useful to directly test components of the application.
935 @return the Python module object."""
937 import importlib.machinery
941 name = os.path.splitext(app)[0]
942 pathname = os.path.join(os.environ[
'IMP_BIN_DIR'], app)
944 return importlib.machinery.SourceFileLoader(name,
945 pathname).load_module()
947 return imp.load_source(name, pathname)
950 """Run an application with the given list of arguments.
951 @return a subprocess.Popen-like object containing the child stdin,
954 return _SubprocessWrapper(sys.executable, [app]+args)
957 """Assert that the application exited cleanly (return value = 0)."""
959 raise OSError(
"Application exited with signal %d\n" % -ret
964 "Application exited uncleanly, with exit code %d\n" % ret
968 """Read and return a set of shell commands from a doxygen file.
969 Each command is assumed to be in a \code{.sh}...\endcode block.
970 The doxygen file is specified relative to the test file itself.
971 This is used to make sure the commands shown in an application
972 example actually work (the testcase can also check the resulting
973 files for correctness)."""
974 def win32_normpath(p):
977 return " ".join([os.path.normpath(x)
for x
in p.split()])
979 def fix_win32_command(cmd):
981 if cmd.startswith(
'cp -r '):
982 return 'xcopy /E ' + win32_normpath(cmd[6:])
983 elif cmd.startswith(
'cp '):
984 return 'copy ' + win32_normpath(cmd[3:])
987 d = os.path.dirname(sys.argv[0])
988 doc = os.path.join(d, doxfile)
992 with open(doc)
as fh:
993 for line
in fh.readlines():
994 if '\code{.sh}' in line:
996 elif '\endcode' in line:
999 cmds.append(line.rstrip(
'\r\n').replace(
1000 '<imp_example_path>', example_path))
1001 if sys.platform ==
'win32':
1002 cmds = [fix_win32_command(x)
for x
in cmds]
1006 "Print and run a shell command, as returned by read_shell_commands()"
1008 p = subprocess.call(cmd, shell=
True)
1010 raise OSError(
"%s failed with exit value %d" % (cmd, p))
1014 """Check to make sure the number of C++ object references is as expected"""
1016 def __init__(self, testcase):
1020 IMP._director_objects.cleanup()
1021 self.__testcase = testcase
1023 self.__basenum = IMP.Object.get_number_of_live_objects()
1027 "Make sure that the number of references matches the expected value."
1029 IMP._director_objects.cleanup()
1032 if x
not in self.__names]
1033 newnum = IMP.Object.get_number_of_live_objects()-self.__basenum
1034 t.assertEqual(newnum, expected,
1035 "Number of objects don't match: "
1036 + str(newnum) +
" != " + str(expected) +
" found "
1041 """Check to make sure the number of director references is as expected"""
1043 def __init__(self, testcase):
1044 IMP._director_objects.cleanup()
1045 self.__testcase = testcase
1046 self.__basenum = IMP._director_objects.get_object_count()
1049 """Make sure that the number of references matches the expected value.
1050 If force_cleanup is set, clean up any unused references first before
1051 doing the assertion.
1055 IMP._director_objects.cleanup()
1056 t.assertEqual(IMP._director_objects.get_object_count()
1057 - self.__basenum, expected)
1066 if sys.platform ==
'win32' and 'PYTHONPATH' in os.environ \
1067 and 'IMP_BIN_DIR' in os.environ:
1068 libdir = os.environ[
'PYTHONPATH'].split(
';')[0]
1069 bindir = os.environ[
'IMP_BIN_DIR']
1070 path = os.environ[
'PATH']
1071 if libdir
not in path
or bindir
not in path:
1072 os.environ[
'PATH'] = bindir +
';' + libdir +
';' + path
1075 __version__ =
"2.20.1"
1078 '''Return the version of this module, as a string'''
1082 '''Return the fully-qualified name of this module'''
1086 '''Return the full path to one of this module's data files'''
1088 return IMP._get_module_data_path(
"test", fname)
1091 '''Return the full path to one of this module's example files'''
1093 return IMP._get_module_example_path(
"test", fname)
def run_python_module
Run a Python module as if with "python -m <modname>", with the given list of arguments as sys...
def temporary_working_directory
Simple context manager to run in a temporary directory.
def assertApplicationExitedCleanly
Assert that the application exited cleanly (return value = 0).
CheckLevel get_check_level()
Get the current audit mode.
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.
def get_module_version
Return the version of this module, as a string.
A general exception for an internal error in IMP.
def main
Run a set of tests; similar to unittest.main().
An exception for an invalid usage of IMP.
Super class for simple IMP application test cases.
def assertRaisesInternalException
Assert that the given callable object raises InternalException.
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 get_data_path
Return the full path to one of this module's data files.
def get_example_path
Return the full path to one of this module's example files.
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.
def assertSequenceAlmostEqual
Fail if the difference between any two items in the two sequences are exceed the specified number of ...
Class for storing model, its restraints, constraints, and particles.
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.
Strings get_live_object_names()
Return the names of all live objects.
def check_standard_object_methods
Check methods that every IMP::Object class should have.
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.
Version and module information for Objects.
def run_example
Run the named example script.
def get_magnitude
Get the magnitude of a list of floats.
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_check
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.
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.
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 particles of a Model object.
def read_shell_commands
Read and return a set of shell commands from a doxygen file.
Check to make sure the number of C++ object references is as expected.
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.
def assertNumPyArrayEqual
Fail if the given numpy array doesn't match expected.
def get_module_name
Return the fully-qualified name of this module.
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 temporary_directory
Simple context manager to make a temporary directory.
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.