"""Test runner (implements the script ``tests`` and provides frequently used test data). """ from __future__ import division import doctest import optparse import random from diles.util import printmemoizestats, modules from diles.learn import Label # ============================================================================= # test sets for ... tests # ============================================================================= random.seed(2010) TSETS = {} TSETS['abc'] = [ ["abcd", "abc-", "---d", "a-cd", "-bcd", "--cd", "-b--"], ['012', '01', '', '02', '12', '2', '1'] ] TSETS['abc-2'] = [ TSETS['abc'][0] * 2, TSETS['abc'][1] * 2, ] TSETS['abc-2-noised'] = [ [x + "".join(random.sample("-abcd", 2)) for x in TSETS['abc-2'][0]], TSETS['abc-2'][1], ] TSETS['easy'] = [ ["a"] * 4 + ["b"] * 4, ['0'] * 4 + ['1'] * 4 ] TSETS['moderate'] = [ ["aa", "ab", "ac", "bc", "ba", "bb", "xd", "bd"], ['x', 'x', 'x', 'y', 'y', 'y', 'x', 'y'] ] TSETS['impossible'] = [ ['a'] * 4 + ['b'] * 4, (['0'] * 2 + ['1'] * 2) * 2 ] def gentricky(labelpop, p): samples, labels = [], [] random.seed(2010) for _ in range(30): sample = random.sample("abcdefghijkl", 5) label = random.sample(labelpop, random.randint(1, 3)) sample[0] = random.random() < p and 'x' in label and 'x' or sample[0] sample[1] = random.random() < p and 'y' in label and 'y' or sample[1] sample[2] = random.random() < p and 'z' in label and 'z' or sample[2] samples.append("".join(sample)) labels.append(label) return [samples, labels] TSETS['tricky-09-balanced'] = gentricky("xyz", 0.9) TSETS['tricky-09-unbalanced'] = gentricky("xxxyyz", 0.9) TSETS['tricky-07-balanced'] = gentricky("xyz", 0.7) TSETS['tricky-07-unbalanced'] = gentricky("xxxyyz", 0.7) random.seed(None) for k, v in TSETS.items(): v[1] = [Label(x) for x in v[1]] # ============================================================================= # command line interface # ============================================================================= def options(): op = optparse.OptionParser("[-m MOD [-o OBJ]]") op.add_option("-m", "--module", action="append", help="test this module [+]", metavar="MOD") op.add_option("-o", "--object", action="append", help="test this object [+]", metavar="OBJ") opts, _ = op.parse_args() return opts def main(): opts = options() runner = doctest.DocTestRunner(optionflags=doctest.ELLIPSIS) if not opts.module: allmods = modules("diles", recursive=True) opts.module = sorted(n for _l, n, _m in allmods) for modn in sorted(opts.module): modi = __import__(modn, globals(), locals(), ["*"]) if not opts.object: print((" %s " % modn).center(79, "-")) doctest.testmod(modi, optionflags=doctest.ELLIPSIS) else: tests = doctest.DocTestFinder().find(modi) tests = sorted(tests, key=lambda t: t.name) tnames = ["%s.%s" % (modn, objn) for objn in opts.object] tests = [t for t in tests if t.name in tnames] if len(tests) != len(tnames): raise Exception("at least one test object does not exist") for t in tests: if t in tests: print((" %s " % t.name).center(79, "-")) runner.run(t) printmemoizestats() if __name__ == '__main__': main()