示例#1
0
def test_format_simple():
    assert format_expr(BinaryOp('+', 1, 2)) == '1 + 2'
    assert format_expr(BinaryOp('-', 1, 2)) == '1 - 2'
    assert format_expr(BinaryOp('*', 1, 2)) == '1 * 2'
    assert format_expr(BinaryOp('/', 1, 2)) == '1 / 2'
    assert format_expr(UnaryOp('+', 1)) == '+1'
    assert format_expr(UnaryOp('-', 1)) == '-1'
示例#2
0
文件: tests.py 项目: wjzz/polyglot
    def test_parser(self):
        e1 = "1"
        self.assertEqual(parse(e1), NumberLit(1))

        e2 = "(1)"
        self.assertEqual(parse(e2), NumberLit(1))

        e3 = "((1))"
        self.assertEqual(parse(e3), NumberLit(1))

        e4 = "(1 + 2)"
        r4 = BinaryOp(NumberLit(1), Op.Plus, NumberLit(2))
        self.assertEqual(parse(e4), r4)

        e5 = "1 + 2"
        self.assertEqual(parse(e5), r4)

        e6 = "(1 * 2)"
        r6 = BinaryOp(NumberLit(1), Op.Mult, NumberLit(2))
        self.assertEqual(parse(e6), r6)

        e7 = "1 * 2"
        self.assertEqual(parse(e7), r6)

        e8 = "1 + 2 * 3"
        r8 = BinaryOp(NumberLit(1), Op.Plus,
                      BinaryOp(NumberLit(2), Op.Mult, NumberLit(3)))
        self.assertEqual(parse(e8), r8)
示例#3
0
    def compute(self, context, expr, filter=None, skip_na=True):
        # FIXME: either take "contextual filter" into account here (by using
        # self._getfilter), or don't do it in sum & gini
        if filter is not None:
            tmpvar = self.add_tmp_var(context, filter)
            if getdtype(expr, context) is bool:
                # convert expr to int because mul_bbb is not implemented in
                # numexpr
                # expr *= 1
                expr = BinaryOp('*', expr, 1)
            # expr *= filter_values
            expr = BinaryOp('*', expr, tmpvar)
        else:
            filter = True

        values = expr_eval(expr, context)
        values = np.asarray(values)

        if skip_na:
            # we should *not* use an inplace operation because filter can be a
            # simple variable
            filter = filter & ispresent(values)

        if filter is True:
            numrows = len(values)
        else:
            numrows = np.sum(filter)

        if numrows:
            if skip_na:
                return na_sum(values) / float(numrows)
            else:
                return np.sum(values) / float(numrows)
        else:
            return float('nan')
示例#4
0
 def build_regression_expr(self, expr, mult=0.0, error_var=None):
     if error_var is not None:
         # expr += error_var
         expr = BinaryOp('+', expr, Variable(None, error_var))
     if mult:
         # expr += normal(0, 1) * mult
         expr = BinaryOp('+', expr, BinaryOp('*', Normal(0, 1), mult))
     return expr
示例#5
0
文件: tests.py 项目: wjzz/polyglot
 def test_expr_str(self):
     n = NumberLit(11)
     m = NumberLit(22)
     self.assertEqual(str(n), "11")
     self.assertEqual(str(m), "22")
     e1 = BinaryOp(n, Op.Plus, m)
     self.assertEqual(str(e1), "(11 + 22)")
     e2 = BinaryOp(n, Op.Mult, m)
     self.assertEqual(str(e2), "(11 * 22)")
     e3 = BinaryOp(e1, Op.Mult, e1)
     self.assertEqual(str(e3), "((11 + 22) * (11 + 22))")
示例#6
0
文件: tests.py 项目: wjzz/polyglot
 def test_evaluator(self):
     n = NumberLit(2)
     m = NumberLit(3)
     self.assertEqual(evaluate(n), 2)
     self.assertEqual(evaluate(m), 3)
     e1 = BinaryOp(n, Op.Plus, m)
     self.assertEqual(evaluate(e1), 5)
     e2 = BinaryOp(n, Op.Mult, m)
     self.assertEqual(evaluate(e2), 6)
     e3 = BinaryOp(e1, Op.Mult, e1)
     self.assertEqual(evaluate(e3), 25)
示例#7
0
    def compute(self, context, expr, filter=None, skip_na=True):
        filter_expr = self._getfilter(context, filter)
        if filter_expr is not None:
            expr = BinaryOp('*', expr, filter_expr)

        values = expr_eval(expr, context)
        values = np.asarray(values)

        return na_sum(values) if skip_na else np.sum(values)
示例#8
0
 def test_expr_str(self):
     n = NumberLit(11)
     m = NumberLit(22)
     self.assertEqual(str(n), "11")
     self.assertEqual(str(m), "22")
     x = Variable("x")
     self.assertEqual(str(x), "x")
     e1 = BinaryOp(n, Op.Plus, m)
     self.assertEqual(str(e1), "(11 + 22)")
     e2 = BinaryOp(n, Op.Mult, m)
     self.assertEqual(str(e2), "(11 * 22)")
     e3 = BinaryOp(e1, Op.Mult, e1)
     self.assertEqual(str(e3), "((11 + 22) * (11 + 22))")
     y = Variable("y")
     self.assertEqual(str(y), "y")
     e4 = BinaryOp(x, Op.Plus, y)
     self.assertEqual(str(e4), "(x + y)")
     e5 = LetIn("x", n, x)
     self.assertEqual(str(e5), "(let x := 11 in x)")
     e6 = LetIn("x", n, BinaryOp(x, Op.Plus, x))
     self.assertEqual(str(e6), "(let x := 11 in (x + x))")
示例#9
0
    def build_expr(self, context, expr):
        if isinstance(expr, basestring):
            # assume it is a filename
            expr = ExtExpr(expr)

        u = Uniform()
        # expr in (0, 0.0, False, '')
        if not isinstance(expr, Expr) and not expr:
            expr = u
        else:
            epsilon = Logit(u)
            # expr = logistic(expr - epsilon)
            expr = Logistic(BinaryOp('-', expr, epsilon))
        return expr
示例#10
0
文件: exprmisc.py 项目: gvk489/liam2
 def build_expr(self, context, expr):
     # log(x / (1 - x))
     return Log(DivisionOp('/', expr, BinaryOp('-', 1.0, expr)))
示例#11
0
    def test_parser(self):
        v1 = "x"
        self.assertEqual(parse(v1), Variable("x"))

        v2 = "(x)"
        self.assertEqual(parse(v2), Variable("x"))

        v3 = "thisisalongvariable"
        self.assertEqual(parse(v3), Variable(v3))

        e1 = "1"
        self.assertEqual(parse(e1), NumberLit(1))

        e2 = "(1)"
        self.assertEqual(parse(e2), NumberLit(1))

        e3 = "((1))"
        self.assertEqual(parse(e3), NumberLit(1))

        e4 = "(1 + 2)"
        r4 = BinaryOp(
                NumberLit(1),
                Op.Plus,
                NumberLit(2))
        self.assertEqual(parse(e4), r4)

        e5 = "1 + 2"
        self.assertEqual(parse(e5), r4)

        e6 = "(1 * 2)"
        r6 = BinaryOp(
                NumberLit(1),
                Op.Mult,
                NumberLit(2))
        self.assertEqual(parse(e6), r6)

        e7 = "1 * 2"
        self.assertEqual(parse(e7), r6)

        e8 = "1 + 2 * 3"
        r8 = BinaryOp(
                NumberLit(1),
                Op.Plus,
                BinaryOp(
                    NumberLit(2),
                    Op.Mult,
                    NumberLit(3)
                ))
        self.assertEqual(parse(e8), r8)

        e9 = "let x := 1 in x end"
        r9 = LetIn(
                "x",
                NumberLit(1),
                Variable("x")
                )
        self.assertEqual(parse(e9), r9)

        e10 = "2 + let x := 1 in x end"
        r10 = BinaryOp(
                NumberLit(2),
                Op.Plus,
                LetIn(
                    "x",
                    NumberLit(1),
                    Variable("x")
                ))
        self.assertEqual(parse(e10), r10)

        e11 = "let x := 1 in x + 2 end"
        r11 = LetIn(
                "x",
                NumberLit(1),
                BinaryOp(
                    Variable("x"),
                    Op.Plus,
                    NumberLit(2)
                ))
        self.assertEqual(parse(e11), r11)

        e12 = "let x := 1 in x end + 2"
        r12 = BinaryOp(
                    LetIn(
                        "x",
                        NumberLit(1),
                        Variable("x")),
                    Op.Plus,
                    NumberLit(2)
                )
        self.assertEqual(parse(e12), r12)
示例#12
0
文件: exprmisc.py 项目: gvk489/liam2
def _mul(a, b):
    return BinaryOp('*', a, b)
示例#13
0
文件: exprmisc.py 项目: gvk489/liam2
def _plus(a, b):
    return BinaryOp('+', a, b)
示例#14
0
文件: exprmisc.py 项目: gvk489/liam2
 def build_expr(self, context, expr):
     # 1 / (1 + exp(-x))
     return DivisionOp('/', 1.0, BinaryOp('+', 1.0, Exp(UnaryOp('-',
                                                                expr))))
示例#15
0
def test_simplify():
    # Constants
    assert format_expr(simplify_expr(BinaryOp('+', 1, 3))) == '4'
    assert format_expr(simplify_expr(UnaryOp('-', 1))) == '-1'

    # Variables
    assert format_expr(simplify_expr(BinaryOp('+', 2, VarExpr('x')))) == '2 + x'
    assert format_expr(simplify_expr(BinaryOp('-', 2, VarExpr('x')))) == '2 - x'
    assert format_expr(simplify_expr(BinaryOp('*', 2, VarExpr('x')))) == '2 * x'
    assert format_expr(simplify_expr(BinaryOp('/', 2, VarExpr('x')))) == '2 / x'
    assert format_expr(simplify_expr(BinaryOp('+', VarExpr('x'), 2))) == 'x + 2'
    assert format_expr(simplify_expr(BinaryOp('-', VarExpr('x'), 2))) == 'x - 2'
    assert format_expr(simplify_expr(BinaryOp('*', VarExpr('x'), 2))) == 'x * 2'
    assert format_expr(simplify_expr(BinaryOp('/', VarExpr('x'), 2))) == 'x / 2'

    # Add zero
    assert format_expr(simplify_expr(BinaryOp('+', 0, VarExpr('x')))) == 'x'
    assert format_expr(simplify_expr(BinaryOp('+', VarExpr('x'), 0))) == 'x'

    # Subtract zero
    assert format_expr(simplify_expr(BinaryOp('-', 0, VarExpr('x')))) == '-x'
    assert format_expr(simplify_expr(BinaryOp('-', VarExpr('x'), 0))) == 'x'

    # Multiply zero
    assert format_expr(simplify_expr(BinaryOp('*', 0, VarExpr('x')))) == '0'
    assert format_expr(simplify_expr(BinaryOp('*', VarExpr('x'), 0))) == '0'

    # Multiply one
    assert format_expr(simplify_expr(BinaryOp('*', 1, VarExpr('x')))) == 'x'
    assert format_expr(simplify_expr(BinaryOp('*', VarExpr('x'), 1))) == 'x'

    # Divide one
    assert format_expr(simplify_expr(BinaryOp('/', VarExpr('x'), 1))) == 'x'

    # Nested
    assert format_expr(simplify_expr(
        BinaryOp('+',
            BinaryOp('*', 1, VarExpr('x')),
            BinaryOp('+', 0, VarExpr('y'))
        ))) == 'x + y'

    # Unary plus
    assert format_expr(simplify_expr(UnaryOp('+', VarExpr('x')))) == 'x'

    # Double minus
    assert format_expr(simplify_expr(UnaryOp('-', UnaryOp('-', VarExpr('x'))))) == 'x'
示例#16
0
def test_format_complex():
    # Same operator
    assert format_expr(BinaryOp('+', BinaryOp('+', 1, 2), 3)) == '1 + 2 + 3'
    assert format_expr(BinaryOp('+', 1, BinaryOp('+', 2, 3))) == '1 + (2 + 3)'

    assert format_expr(BinaryOp('-', BinaryOp('-', 1, 2), 3)) == '1 - 2 - 3'
    assert format_expr(BinaryOp('-', 1, BinaryOp('-', 2, 3))) == '1 - (2 - 3)'

    assert format_expr(BinaryOp('*', BinaryOp('*', 1, 2), 3)) == '1 * 2 * 3'
    assert format_expr(BinaryOp('*', 1, BinaryOp('*', 2, 3))) == '1 * (2 * 3)'

    assert format_expr(BinaryOp('/', BinaryOp('/', 1, 2), 3)) == '1 / 2 / 3'
    assert format_expr(BinaryOp('/', 1, BinaryOp('/', 2, 3))) == '1 / (2 / 3)'

    # Equal precedence
    assert format_expr(BinaryOp('+', BinaryOp('-', 1, 2), 3)) == '1 - 2 + 3'
    assert format_expr(BinaryOp('-', BinaryOp('+', 1, 2), 3)) == '1 + 2 - 3'
    assert format_expr(BinaryOp('+', 1, BinaryOp('-', 2, 3))) == '1 + (2 - 3)'
    assert format_expr(BinaryOp('-', 1, BinaryOp('+', 2, 3))) == '1 - (2 + 3)'

    assert format_expr(BinaryOp('*', BinaryOp('/', 1, 2), 3)) == '1 / 2 * 3'
    assert format_expr(BinaryOp('/', BinaryOp('*', 1, 2), 3)) == '1 * 2 / 3'
    assert format_expr(BinaryOp('*', 1, BinaryOp('/', 2, 3))) == '1 * (2 / 3)'
    assert format_expr(BinaryOp('/', 1, BinaryOp('*', 2, 3))) == '1 / (2 * 3)'

    # Different precedence
    assert format_expr(BinaryOp('+', BinaryOp('*', 1, 2), 3)) == '1 * 2 + 3'
    assert format_expr(BinaryOp('*', BinaryOp('+', 1, 2), 3)) == '(1 + 2) * 3'
    assert format_expr(BinaryOp('+', 1, BinaryOp('*', 2, 3))) == '1 + 2 * 3'
    assert format_expr(BinaryOp('*', 1, BinaryOp('+', 2, 3))) == '1 * (2 + 3)'
示例#17
0
def test_eval_binary():
    assert eval_expr(BinaryOp('+', 1, 2)) == 3
    assert eval_expr(BinaryOp('-', 1, 2)) == -1
    assert eval_expr(BinaryOp('*', 1, 2)) == 2
    assert eval_expr(BinaryOp('/', 1, 2)) == 0.5