Coverage for mlprodict/grammar/grammar_sklearn/grammar/gactions_num.py: 100%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

36 statements  

1""" 

2@file 

3@brief Action definition. 

4""" 

5from .gtypes import MLNumTypeFloat32, MLNumTypeFloat64, MLNumTypeBool 

6from .gactions import MLActionBinary, MLActionFunctionCall 

7 

8 

9class MLActionAdd(MLActionBinary): 

10 """ 

11 Addition 

12 """ 

13 

14 def __init__(self, act1, act2): 

15 """ 

16 @param act1 first element 

17 @param act2 second element 

18 """ 

19 MLActionBinary.__init__(self, act1, act2, "+") 

20 if type(act1.output) != type(act2.output): 

21 raise TypeError( # pragma: no cover 

22 "Not the same input type {0} != {1}".format( 

23 type(act1.output), type(act2.output))) 

24 

25 def execute(self, **kwargs): 

26 MLActionBinary.execute(self, **kwargs) 

27 res = self.ChildrenResults 

28 return self.output.validate(res[0] + res[1]) 

29 

30 

31class MLActionSign(MLActionFunctionCall): 

32 """ 

33 Sign of an expression: 1=positive, 0=negative. 

34 """ 

35 

36 def __init__(self, act1): 

37 """ 

38 @param act1 first element 

39 """ 

40 MLActionFunctionCall.__init__(self, "sign", act1.output, act1) 

41 if not isinstance(act1.output, (MLNumTypeFloat32, MLNumTypeFloat64)): 

42 raise TypeError( # pragma: no cover 

43 "The input action must produce float32 or float64 not '{0}'".format(type(act1.output))) 

44 

45 def execute(self, **kwargs): 

46 MLActionFunctionCall.execute(self, **kwargs) 

47 res = self.ChildrenResults 

48 return self.output.validate(self.output.softcast(1 if res[0] >= 0 else 0)) 

49 

50 

51class MLActionTestInf(MLActionBinary): 

52 """ 

53 Operator ``<``. 

54 """ 

55 

56 def __init__(self, act1, act2): 

57 """ 

58 @param act1 first element 

59 @param act2 second element 

60 """ 

61 MLActionBinary.__init__(self, act1, act2, "<=") 

62 if type(act1.output) != type(act2.output): 

63 raise TypeError( # pragma: no cover 

64 "Not the same input type {0} != {1}".format( 

65 type(act1.output), type(act2.output))) 

66 self.output = MLNumTypeBool() 

67 

68 def execute(self, **kwargs): 

69 MLActionBinary.execute(self, **kwargs) 

70 res = self.ChildrenResults 

71 return self.output.validate(self.output.softcast(res[0] <= res[1])) 

72 

73 

74class MLActionTestEqual(MLActionBinary): 

75 """ 

76 Operator ``==``. 

77 """ 

78 

79 def __init__(self, act1, act2): 

80 """ 

81 @param act1 first element 

82 @param act2 second element 

83 """ 

84 MLActionBinary.__init__(self, act1, act2, "==") 

85 if type(act1.output) != type(act2.output): 

86 raise TypeError( # pragma: no cover 

87 "Not the same input type {0} != {1}".format( 

88 type(act1.output), type(act2.output))) 

89 self.output = MLNumTypeBool() 

90 

91 def execute(self, **kwargs): 

92 MLActionBinary.execute(self, **kwargs) 

93 res = self.ChildrenResults 

94 return self.output.validate(self.output.softcast(res[0] == res[1]))