Ejemplo n.º 1
0
def main(args=None):
    if args is None:
        args = sys.argv[1:]

    parser = argparse.ArgumentParser(
        prog=__package__,
        description="Generates a Python module in directory OUTPUT_DIR "
        "from each decision tree given in SOURCE_FILE. If OUTPUT_DIR "
        "is not given, Python modules are created next to their "
        "SOURCE_FILE.")
    parser.add_argument(
        "src",
        metavar='SOURCE_FILE',
        nargs='+',
        help="source file containing a decision tree (YAML format)")
    parser.add_argument("-o",
                        "--out",
                        metavar='OUTPUT_DIR',
                        help="target directory for generated Python files")

    for option_name, option_def in CONFIG_DEFAULTS.items():
        default, help_pattern, choices = option_def
        if choices:
            parser.add_argument('--' + option_name,
                                default=default,
                                help=help_pattern.format(default=default),
                                choices=choices)
        else:
            parser.add_argument(
                '--' + option_name,
                default=default,
                action='store_true' if isinstance(default, bool) else None,
                help=help_pattern.format(default=default))

    args = parser.parse_args(args=args)
    options = {
        k: v
        for k, v in vars(args).items()
        if k in CONFIG_DEFAULTS and v != CONFIG_DEFAULTS[k][0]
    }

    if args.no_jit and args.vectorize == VECTORIZE_PROP:
        print('error: --no_jit is illegal because --vectorize "' +
              VECTORIZE_PROP + '" requires JIT')
        exit(1)

    out_dir = args.out
    if out_dir is not None:
        os.makedirs(out_dir, exist_ok=True)

    for src_file in args.src:
        out_file = None
        if out_dir is not None:
            basename = os.path.splitext(os.path.basename(src_file))[0] + '.py'
            out_file = os.path.join(out_dir, basename)
        out_file = transpile(src_file, out_file=out_file, **options)
        print('generated', out_file)
Ejemplo n.º 2
0
    def test_transpile_vectorized(self):
        src_file = os.path.join(os.path.dirname(__file__), 'dectree_test.yml')
        out_file = os.path.join(os.path.dirname(__file__), 'dectree_test_v.py')
        if os.path.exists(out_file):
            os.remove(out_file)
        transpile(src_file, out_file=out_file, vectorize=VECTORIZE_PROP)
        self.assertTrue(os.path.exists(out_file))
        m = __import__('test.dectree_test_v')
        self.assertTrue(hasattr(m, 'dectree_test_v'))
        self.assertTrue(hasattr(m.dectree_test_v, 'Inputs'))
        self.assertTrue(hasattr(m.dectree_test_v, 'Outputs'))
        self.assertTrue(hasattr(m.dectree_test_v, 'apply_rules'))
        inputs = m.dectree_test_v.Inputs()
        outputs = m.dectree_test_v.Outputs()

        inputs.glint = np.array([0.2, 0.3])
        inputs.radiance = np.array([60.0, 10.0])
        m.dectree_test_v.apply_rules(inputs, outputs)
        np.testing.assert_almost_equal(outputs.cloudy, np.array([0.6, 0.0]))
        np.testing.assert_almost_equal(outputs.certain, np.array([1.0, 1.0]))
Ejemplo n.º 3
0
    def test_transpile_with_defaults(self):
        src_file = os.path.join(os.path.dirname(__file__), 'dectree_test.yml')
        out_file = os.path.join(os.path.dirname(__file__), 'dectree_test.py')
        if os.path.exists(out_file):
            os.remove(out_file)
        transpile(src_file)
        self.assertTrue(os.path.exists(out_file))
        m = __import__('test.dectree_test')
        self.assertTrue(hasattr(m, 'dectree_test'))
        self.assertTrue(hasattr(m.dectree_test, 'Inputs'))
        self.assertTrue(hasattr(m.dectree_test, 'Outputs'))
        self.assertTrue(hasattr(m.dectree_test, 'apply_rules'))
        inputs = m.dectree_test.Inputs()
        outputs = m.dectree_test.Outputs()

        inputs.glint = 0.2
        inputs.radiance = 60.
        m.dectree_test.apply_rules(inputs, outputs)
        self.assertAlmostEqual(outputs.cloudy, 0.6)
        self.assertAlmostEqual(outputs.certain, 1.0)
Ejemplo n.º 4
0
    def test_transpile_parameterized(self):
        src_file = os.path.join(os.path.dirname(__file__), 'dectree_test.yml')
        out_file = os.path.join(os.path.dirname(__file__), 'dectree_test_p.py')
        if os.path.exists(out_file):
            os.remove(out_file)
        transpile(src_file, out_file=out_file, parameterize=True)
        self.assertTrue(os.path.exists(out_file))
        m = __import__('test.dectree_test_p')
        self.assertTrue(hasattr(m, 'dectree_test_p'))
        self.assertTrue(hasattr(m.dectree_test_p, 'Inputs'))
        self.assertTrue(hasattr(m.dectree_test_p, 'Outputs'))
        self.assertTrue(hasattr(m.dectree_test_p, 'Params'))
        self.assertTrue(hasattr(m.dectree_test_p, 'apply_rules'))
        inputs = m.dectree_test_p.Inputs()
        outputs = m.dectree_test_p.Outputs()
        params = m.dectree_test_p.Params()

        inputs.glint = 0.2
        inputs.radiance = 60.
        m.dectree_test_p.apply_rules(inputs, outputs, params)
        self.assertAlmostEqual(outputs.cloudy, 0.6)
        self.assertAlmostEqual(outputs.certain, 1.0)
Ejemplo n.º 5
0
    def test_transpile_failures(self):
        src_file = StringIO("")
        with self.assertRaises(ValueError) as cm:
            transpile(src_file)
        self.assertEqual(str(cm.exception), 'Empty decision tree definition')

        src_file = StringIO("")
        out_file = StringIO()
        with self.assertRaises(ValueError) as cm:
            transpile(src_file, out_file=out_file)
        self.assertEqual(str(cm.exception), 'Empty decision tree definition')

        src_file = StringIO("types:\n    ")
        out_file = StringIO()
        with self.assertRaises(ValueError) as cm:
            transpile(src_file, out_file=out_file)
        self.assertEqual(
            str(cm.exception),
            "Invalid decision tree definition: missing section "
            "('types', 'inputs', 'outputs', 'rules') or all of them")

        src_file = StringIO(
            "types: null\ninputs: null\noutputs: null\nrules: null\n")
        out_file = StringIO()
        with self.assertRaises(ValueError) as cm:
            transpile(src_file, out_file=out_file)
        self.assertEqual(
            str(cm.exception),
            "Invalid decision tree definition: section 'types' is empty")

        src_file = StringIO(get_src(a='u'))
        out_file = StringIO()
        with self.assertRaises(ValueError) as cm:
            transpile(src_file, out_file=out_file)
        self.assertEqual(str(cm.exception), 'Variable "a" is undefined')

        src_file = StringIO(get_src(b='u'))
        out_file = StringIO()
        with self.assertRaises(ValueError) as cm:
            transpile(src_file, out_file=out_file)
        self.assertEqual(str(cm.exception), 'Variable "u" is undefined')

        src_file = StringIO(get_src(no1='false'))
        out_file = StringIO()
        with self.assertRaises(ValueError) as cm:
            transpile(src_file, out_file=out_file)
        self.assertEqual(
            str(cm.exception),
            'Illegal value for property "NO" of type "P2": False')

        src_file = StringIO(get_src(p1='Radiance'))
        out_file = StringIO()
        with self.assertRaises(ValueError) as cm:
            transpile(src_file, out_file=out_file)
        self.assertEqual(str(cm.exception),
                         'Type "Radiance" of variable "a" is undefined')

        src_file = StringIO(get_src(no2='Radiance'))
        out_file = StringIO()
        with self.assertRaises(ValueError) as cm:
            transpile(src_file, out_file=out_file)
        self.assertEqual(
            str(cm.exception),
            '"Radiance" is not a property of type "P2" of variable "b"')
Ejemplo n.º 6
0
 def test_transpile_success(self):
     src_file = StringIO(get_src())
     out_file = StringIO()
     transpile(src_file, out_file=out_file)
     self.assertIsNotNone(out_file.getvalue())