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)' )
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))))
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)
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'))))
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))
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'))
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))
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)
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'), ))), ))
def sym_identity(estimator): return RealVariable(estimator.column)
def __call__(self): result = self.base + str(self.current_n) self.current_n += 1 return RealVariable(result)
def safe_symbol(s): if isinstance(s, Variable): return s return RealVariable(s)
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)
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)