Example #1
0
def test_numpy_printer():
    x = RealVariable('x')
    y = RealVariable('y')
    numpy_print = NumpyPrinter()
    expr = Piecewise((-Log(x), x > y), (y, true))
    assert_equal(
        numpy_print(expr),
        'select([greater(x, y), True], [-log(x), y], default=nan).astype(float)'
    )
Example #2
0
def test_compose():
    fun0 = Function('x', tuple(),
                    (RealVariable('x'), RealNumber(1) - RealVariable('x')))
    fun = Function(('x', 'y'), tuple(),
                   (RealVariable('x') / RealVariable('y'), ))
    composed_fun = fun.compose(fun0)
    assert_equal(composed_fun.calls[0][1][0], fun0)
    assert_equal(composed_fun.inputs, fun0.inputs)
    assert_equal(
        fun.outputs,
        composed_fun.map_output_symbols(
            dict(zip(composed_fun.calls[0][0], fun.inputs))))
Example #3
0
def sym_transform_variable_transformer(estimator):
    inputs = s2c_syms(estimator)
    label_to_idxs = defaultdict(list)
    for i, label in enumerate(estimator.xlabels_):
        label_to_idxs[label].append(i)

    label_to_expr = dict(
        map(lambda label: (label, RealVariable(label)), estimator.xlabels_))
    label_to_expr.update(estimator.clean_transformations_)

    outputs = list(inputs)
    for label, expr in estimator.clean_transformations_.items():
        if label in label_to_idxs:
            idxs = label_to_idxs[label]
            for idx in idxs:
                outputs[idx] = sym_col_trans(expr)
        else:
            outputs.append(sym_col_trans(expr))

    outputs = tuple(outputs)

    #     outputs_dict = OrderedDict(map(lambda label: (label, RealVariable(label)), estimator.xlabels_))
    #     print(len(outputs_dict))
    #     print(len(estimator.xlabels_))
    #     print(len(estimator.clean_transformations_))
    #     for k, v in estimator.clean_transformations_.items():
    #         outputs_dict[k] = sym_col_trans(v)
    #     print(len(outputs_dict))
    #     outputs = tuple(outputs_dict.values())#tuple(valmap(sym_col_trans, estimator.clean_transformations_).values())
    #     print(len(outputs))
    return Function(inputs, tuple(), outputs)
Example #4
0
def test_from_expressions():
    fun = Function.from_expressions(
        (RealVariable('x'), RealVariable('x') + RealVariable('y')))
    assert_equal(
        fun,
        Function(('x', 'y'), tuple(),
                 (RealVariable('x'), RealVariable('x') + RealVariable('y'))))
Example #5
0
 def expression(self):
     x = RealVariable(self.variable)
     t = RealNumber(self.threshold)
     if isinstance(self.missing, Node):
         missing = self.missing.expression()
     else:
         missing = RealNumber(self.missing)
     if isinstance(self.yes, Node):
         yes = self.yes.expression()
     else:
         yes = RealNumber(self.yes)
     if isinstance(self.no, Node):
         no = self.no.expression()
     else:
         no = RealNumber(self.no)
     return Piecewise((missing, IsNan(x)), (yes, x < t), (no, true))
Example #6
0
def test_map_symbols():
    fun0 = Function(('x', 'y'), tuple(),
                    (RealVariable('x') + RealVariable('y'), ))
    fun = Function(('x', 'y'), (((('z', ), (fun0, ('x', 'y')))), ),
                   (RealVariable('x') / RealVariable('z'), ))
    mapped_fun = fun.map_symbols({'x': 'q'})
    assert_list_equal(list(mapped_fun.inputs),
                      list(map(RealVariable, ('q', 'y'))))
    assert_equal(set(mapped_fun.calls[0][1][1]),
                 set(map(RealVariable, ('q', 'y'))))
    assert_equal(mapped_fun.outputs[0], RealVariable('q') / RealVariable('z'))
Example #7
0
def sym_predict_classifier(estimator):
    x = RealVariable('x')
    return Function.from_expression(Piecewise((Integer(1), x >= RealNumber(.5)), (Integer(0), true))).compose(sym_predict_proba(estimator).select_outputs(1))
Example #8
0
 def test_op(self):
     fun0 = Function(('x', 'y'), tuple(),
                     (RealVariable('x') + RealVariable('y'), ))
     fun = Function(('x', 'y'), (((('z', ), (fun0, ('x', 'y')))), ),
                    (RealVariable('x') / RealVariable('z'), ))
     fun_op_two = op(fun, RealNumber(2))
     assert_equal(fun_op_two.outputs[0],
                  op(RealVariable('x') / RealVariable('z'), RealNumber(2)))
     two_op_fun = op(RealNumber(2), fun)
     assert_equal(two_op_fun.outputs[0],
                  op(RealNumber(2),
                     RealVariable('x') / RealVariable('z')))
     fun_op_fun = op(fun, fun)
     assert_equal(
         fun_op_fun.outputs[0],
         op(
             RealVariable('x') / RealVariable('z'),
             RealVariable('x') / RealVariable('z')))
     assert_equal(fun_op_fun.inputs, fun.inputs)
     assert_equal(fun_op_fun.calls, fun.calls)
Example #9
0
def test_trim():
    fun0 = Function(
        'x',
        ((('u', ),
          (Function.from_expression(RealVariable('x0') + RealVariable('x1')),
           ('x', 'x'))), ),
        (RealVariable('u'), RealNumber(1) - RealVariable('x')))
    fun = Function(('x', 'y'), ((('z', 'w'), (fun0, ('y', ))), ),
                   (RealVariable('x') / RealVariable('w'), )).trim()
    assert_equal(fun.inputs, (RealVariable('x'), RealVariable('y')))
    assert_equal(fun.outputs, (RealVariable('x') / RealVariable('w'), ))
    assert_equal(fun.calls, (((RealVariable('w'), ), (Function(
        ('x', ), tuple(),
        (RealNumber(1) - RealVariable('x'), )), (RealVariable('y'), ))), ))
Example #10
0
def sym_identity(estimator):
    return RealVariable(estimator.column)
Example #11
0
 def __call__(self):
     result = self.base + str(self.current_n)
     self.current_n += 1
     return RealVariable(result)
Example #12
0
def safe_symbol(s):
    if isinstance(s, Variable):
        return s
    return RealVariable(s)
Example #13
0
def test_piecewise():
    x = RealVariable('x')
    y = RealVariable('y')
    expr = Piecewise((-Log(x), x > y), (y, true))
    assert_equal(str(expr), '(-Log(x) if (x > y) else (y if True))')
    assert_is_instance(expr, RealPiecewise)
Example #14
0
def test_max():
    x = RealVariable('x')
    y = RealVariable('y')
    expr = Max(x, y)
    assert_equal(str(expr), 'Max(x, y)')
    assert_is_instance(expr, MaxReal)