Beispiel #1
0
def test_dependable_condition():
    left = ast.BinNumExpr(
        ast.IfExpr(
            ast.CompExpr(ast.NumVal(1), ast.NumVal(1), ast.CompOpType.EQ),
            ast.NumVal(1), ast.NumVal(2)), ast.NumVal(2), ast.BinNumOpType.ADD)

    right = ast.BinNumExpr(ast.NumVal(1), ast.NumVal(2), ast.BinNumOpType.DIV)
    bool_test = ast.CompExpr(left, right, ast.CompOpType.GTE)

    expr = ast.IfExpr(bool_test, ast.NumVal(1), ast.FeatureRef(0))

    expected_code = """
def score(input):
    if (1) == (1):
        var1 = 1
    else:
        var1 = 2
    if ((var1) + (2)) >= ((1) / (2)):
        var0 = 1
    else:
        var0 = input[0]
    return var0
    """

    interpreter = interpreters.PythonInterpreter()

    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #2
0
def test_nested_condition():
    left = ast.BinNumExpr(
        ast.IfExpr(
            ast.CompExpr(ast.NumVal(1), ast.NumVal(1), ast.CompOpType.EQ),
            ast.NumVal(1), ast.NumVal(2)), ast.NumVal(2), ast.BinNumOpType.ADD)

    bool_test = ast.CompExpr(ast.NumVal(1), left, ast.CompOpType.EQ)

    expr_nested = ast.IfExpr(bool_test, ast.FeatureRef(2), ast.NumVal(2))

    expr = ast.IfExpr(bool_test, expr_nested, ast.NumVal(2))

    expected_code = """
def score(input):
    if (1) == (1):
        var1 = 1
    else:
        var1 = 2
    if (1) == ((var1) + (2)):
        if (1) == (1):
            var2 = 1
        else:
            var2 = 2
        if (1) == ((var2) + (2)):
            var0 = input[2]
        else:
            var0 = 2
    else:
        var0 = 2
    return var0
    """

    interpreter = interpreters.PythonInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #3
0
def test_raw_array():
    expr = ast.VectorVal([ast.NumVal(3), ast.NumVal(4)])

    expected_code = """
def score(input):
    return [3.0, 4.0]
    """

    interpreter = interpreters.PythonInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #4
0
def test_tanh_expr():
    expr = ast.TanhExpr(ast.NumVal(2.0))

    interpreter = interpreters.PythonInterpreter()

    expected_code = """
import numpy as np
def score(input):
    return np.tanh(2.0)"""

    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #5
0
def test_pow_expr():
    expr = ast.PowExpr(ast.NumVal(2.0), ast.NumVal(3.0))

    interpreter = interpreters.PythonInterpreter()

    expected_code = """
import numpy as np
def score(input):
    return np.power(2.0, 3.0)"""

    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #6
0
def test_abs_expr():
    expr = ast.AbsExpr(ast.NumVal(-1.0))

    interpreter = interpreters.PythonInterpreter()

    expected_code = """
def score(input):
    return abs(-1.0)
    """

    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #7
0
def test_atan_expr():
    expr = ast.AtanExpr(ast.NumVal(2.0))

    interpreter = interpreters.PythonInterpreter()

    expected_code = """
import math
def score(input):
    return math.atan(2.0)
    """

    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #8
0
def test_reused_expr():
    reused_expr = ast.ExpExpr(ast.NumVal(1.0), to_reuse=True)
    expr = ast.BinNumExpr(reused_expr, reused_expr, ast.BinNumOpType.DIV)

    interpreter = interpreters.PythonInterpreter()

    expected_code = """
import numpy as np
def score(input):
    var0 = np.exp(1.0)
    return (var0) / (var0)"""

    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #9
0
def test_bin_vector_num_expr():
    expr = ast.BinVectorNumExpr(ast.VectorVal([ast.NumVal(1),
                                               ast.NumVal(2)]), ast.NumVal(1),
                                ast.BinNumOpType.MUL)

    interpreter = interpreters.PythonInterpreter()

    expected_code = """
import numpy as np
def score(input):
    return (np.asarray([1, 2])) * (1)
"""
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #10
0
def test_bin_num_expr():
    expr = ast.BinNumExpr(
        ast.BinNumExpr(ast.FeatureRef(0), ast.NumVal(-2),
                       ast.BinNumOpType.DIV), ast.NumVal(2),
        ast.BinNumOpType.MUL)

    interpreter = interpreters.PythonInterpreter()

    expected_code = """
def score(input):
    return ((input[0]) / (-2)) * (2)
    """

    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #11
0
def test_deep_expression():
    expr = ast.NumVal(1)
    for i in range(120):
        expr = ast.BinNumExpr(expr, ast.NumVal(1), ast.BinNumOpType.ADD)

    interpreter = interpreters.PythonInterpreter()

    result_code = interpreter.interpret(expr)
    result_code += """
result = score(None)
"""

    scope = {}
    exec(result_code, scope)

    assert scope["result"] == 121
Beispiel #12
0
def export_to_python(model, indent=4):
    """
    Generates a Python code representation of the given model.

    Parameters
    ----------
    model : object
        The model object that should be transpiled into code.
    indent : int, optional
        The size of indents in the generated code.

    Returns
    -------
    code : string
    """
    interpreter = interpreters.PythonInterpreter(indent=indent)
    return _export(model, interpreter)
Beispiel #13
0
def test_if_expr():
    expr = ast.IfExpr(
        ast.CompExpr(ast.NumVal(1), ast.FeatureRef(0), ast.CompOpType.EQ),
        ast.NumVal(2), ast.NumVal(3))

    interpreter = interpreters.PythonInterpreter()

    expected_code = """
def score(input):
    if (1) == (input[0]):
        var0 = 2
    else:
        var0 = 3
    return var0
"""

    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #14
0
def test_bin_vector_num_expr():
    expr = ast.BinVectorNumExpr(ast.VectorVal([ast.NumVal(1),
                                               ast.NumVal(2)]), ast.NumVal(1),
                                ast.BinNumOpType.MUL)

    interpreter = interpreters.PythonInterpreter()

    expected_code = """
def add_vectors(v1, v2):
    return [sum(i) for i in zip(v1, v2)]
def mul_vector_number(v1, num):
    return [i * num for i in v1]
def score(input):
    return mul_vector_number([1.0, 2.0], 1.0)
    """

    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #15
0
def test_multi_output():
    expr = ast.IfExpr(
        ast.CompExpr(ast.NumVal(1), ast.NumVal(1), ast.CompOpType.EQ),
        ast.VectorVal([ast.NumVal(1), ast.NumVal(2)]),
        ast.VectorVal([ast.NumVal(3), ast.NumVal(4)]))

    expected_code = """
def score(input):
    if (1.0) == (1.0):
        var0 = [1.0, 2.0]
    else:
        var0 = [3.0, 4.0]
    return var0
    """

    interpreter = interpreters.PythonInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #16
0
def test_multi_output():
    expr = ast.SubroutineExpr(
        ast.IfExpr(
            ast.CompExpr(ast.NumVal(1), ast.NumVal(1), ast.CompOpType.EQ),
            ast.VectorVal([ast.NumVal(1), ast.NumVal(2)]),
            ast.VectorVal([ast.NumVal(3), ast.NumVal(4)])))

    expected_code = """
import numpy as np
def score(input):
    if (1) == (1):
        var0 = np.asarray([1, 2])
    else:
        var0 = np.asarray([3, 4])
    return var0
"""

    interpreter = interpreters.PythonInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)