Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_single_condition():
estimator = tree.DecisionTreeRegressor()
estimator.fit([[1], [2]], [1, 2])
assembler = assemblers.TreeModelAssembler(estimator)
actual = assembler.assemble()
expected = ast.IfExpr(
ast.CompExpr(
ast.FeatureRef(0),
ast.NumVal(1.5),
ast.CompOpType.LTE),
ast.NumVal(1.0),
ast.NumVal(2.0))
assert utils.cmp_exprs(actual, expected)
ast.BinNumExpr(
ast.NumVal(base_score),
ast.IfExpr(
ast.CompExpr(
ast.FeatureRef(12),
ast.NumVal(9.72500038),
ast.CompOpType.GTE),
ast.NumVal(1.67318344),
ast.NumVal(2.92757893)),
ast.BinNumOpType.ADD),
ast.IfExpr(
ast.CompExpr(
ast.FeatureRef(5),
ast.NumVal(6.94099998),
ast.CompOpType.GTE),
ast.NumVal(3.3400948),
ast.NumVal(1.72118247)),
ast.BinNumOpType.ADD))
assert utils.cmp_exprs(actual, expected)
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 = """
public class Model {
public static double score(double[] input) {
double var0;
double var1;
if ((1) == (1)) {
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)
expected_code = """
Module Model
Function score(ByRef input_vector() As Double) As Double
score = ((input_vector(0)) / (-2)) * (2)
End Function
End Module
"""
interpreter = VisualBasicInterpreter()
utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def test_if_expr():
expr = ast.IfExpr(
ast.CompExpr(ast.NumVal(1), ast.FeatureRef(0), ast.CompOpType.EQ),
ast.NumVal(2),
ast.NumVal(3))
expected_code = """
Module Model
Function score(ByRef input_vector() As Double) As Double
Dim var0 As Double
If (1) == (input_vector(0)) Then
var0 = 2
Else
var0 = 3
End If
score = var0
End Function
End Module
"""
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)
def test_single_feature():
estimator = linear_model.LinearRegression()
estimator.coef_ = [1]
estimator.intercept_ = 3
assembler = assemblers.LinearModelAssembler(estimator)
actual = assembler.assemble()
expected = ast.BinNumExpr(
ast.NumVal(3),
ast.BinNumExpr(
ast.FeatureRef(0),
ast.NumVal(1),
ast.BinNumOpType.MUL),
ast.BinNumOpType.ADD)
assert utils.cmp_exprs(actual, expected)