#NEWLINE# @ staticmethod #NEWLINE# def arithmetics( tree ) : #NEWLINE# #INDENT# '\n        Takes an arithmetic operations tree (Lisp-styled) as an input\n        ' #NEWLINE# def basic_function( func_name , args ) : #NEWLINE# #INDENT# expr = pmml . Apply ( function = func_name ) #NEWLINE# for a in args : #NEWLINE# #INDENT# expr . append ( a ) #NEWLINE# #UNINDENT# return expr #NEWLINE# #UNINDENT# def mod_function( args ) : #NEWLINE# #INDENT# expr = pmml . Apply ( function = '-' ) #NEWLINE# expr . append ( args [ 0 ] ) #NEWLINE# mul = pmml . Apply ( function = '*' ) #NEWLINE# mul . append ( args [ 1 ] ) #NEWLINE# floor = pmml . Apply ( function = 'floor' ) #NEWLINE# mul . append ( floor ) #NEWLINE# div = pmml . Apply ( function = '/' ) #NEWLINE# floor . append ( div ) #NEWLINE# div . append ( args [ 0 ] ) #NEWLINE# div . append ( args [ 1 ] ) #NEWLINE# return expr #NEWLINE# #UNINDENT# def greedy_evaluation( node ) : #NEWLINE# #INDENT# if isinstance ( node , str ) : #NEWLINE# #INDENT# return ( ( lambda df : df [ node ] ) , pmml . FieldRef ( field = node ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( node , ( tuple , list ) ) : #NEWLINE# #INDENT# args = map ( greedy_evaluation , node [ 1 : ] ) #NEWLINE# functions = { #NEWLINE# #INDENT# '*' : ( lambda df : np . multiply ( * [ _ [ 0 ] ( df ) for _ in args ] ) ) , #NEWLINE# '-' : ( lambda df : np . subtract ( * [ _ [ 0 ] ( df ) for _ in args ] ) ) , #NEWLINE# '+' : ( lambda df : np . add ( * [ _ [ 0 ] ( df ) for _ in args ] ) ) , #NEWLINE# '/' : ( lambda df : np . divide ( * [ _ [ 0 ] ( df ) for _ in args ] ) ) , #NEWLINE# '%' : ( lambda df : np . mod ( * [ _ [ 0 ] ( df ) for _ in args ] ) ) , #NEWLINE# #UNINDENT# } #NEWLINE# assert isinstance ( node [ 0 ] , str ) , 'First element should be a code of operation' #NEWLINE# assert ( node [ 0 ] in functions ) , 'Unknown function code {}. Supported codes: {}' . format ( node [ 0 ] , functions . keys ( ) ) #NEWLINE# expr = { #NEWLINE# #INDENT# '*' : partial ( basic_function , '*' ) , #NEWLINE# '-' : partial ( basic_function , '-' ) , #NEWLINE# '+' : partial ( basic_function , '+' ) , #NEWLINE# '/' : partial ( basic_function , '/' ) , #NEWLINE# '%' : mod_function , #NEWLINE# #UNINDENT# } . get ( node [ 0 ] ) ( [ a [ 1 ] for a in args ] ) #NEWLINE# func = functions [ node [ 0 ] ] #NEWLINE# return ( func , expr ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return ( ( lambda df : node ) , pmml . Constant ( node , dataType = 'double' ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# ( function , transformation ) = greedy_evaluation ( tree ) #NEWLINE# return { #NEWLINE# #INDENT# DerivedFeatureTransformations . TRANSFORMATION : transformation , #NEWLINE# DerivedFeatureTransformations . FUNCTION : function , #NEWLINE# #UNINDENT# }