def test_aggregation_avg(self): columnName = TupleValueExpression(col_name=0) aggr_expr = AggregationExpression(ExpressionType.AGGREGATION_AVG, None, columnName) tuples = Batch(pd.DataFrame({ 0: [1, 2, 3], 1: [2, 3, 4], 2: [3, 4, 5] })) self.assertEqual(2, aggr_expr.evaluate(tuples, None))
def test_aggregation_max(self): columnName = TupleValueExpression(col_name=0) aggr_expr = AggregationExpression( ExpressionType.AGGREGATION_MAX, None, columnName ) tuples = Batch(pd.DataFrame( {0: [1, 2, 3], 1: [2, 3, 4], 2: [3, 4, 5]})) batch = aggr_expr.evaluate(tuples, None) self.assertEqual(3, batch.frames.iloc[0][0])
def test_if_expr_tree_is_equal(self): const_exp1 = ConstantValueExpression(0) const_exp2 = ConstantValueExpression(0) columnName1 = TupleValueExpression(col_name='DATA') columnName2 = TupleValueExpression(col_name='DATA') aggr_expr1 = AggregationExpression(ExpressionType.AGGREGATION_AVG, None, columnName1) aggr_expr2 = AggregationExpression(ExpressionType.AGGREGATION_AVG, None, columnName2) cmpr_exp1 = ComparisonExpression(ExpressionType.COMPARE_NEQ, aggr_expr1, const_exp1) cmpr_exp2 = ComparisonExpression(ExpressionType.COMPARE_NEQ, aggr_expr2, const_exp2) self.assertEqual(cmpr_exp1, cmpr_exp2)
def test_aggregation_min(self): columnName = TupleValueExpression(0) aggr_expr = AggregationExpression( ExpressionType.AGGREGATION_MIN, None, columnName ) frame_1 = Frame(1, np.ones((1, 1)), None) frame_2 = Frame(2, 2 * np.ones((1, 1)), None) frame_3 = Frame(3, 3 * np.ones((1, 1)), None) outcome_1 = Prediction(frame_1, ["car", "bus"], [0.5, 0.6]) outcome_2 = Prediction(frame_2, ["bus"], [0.5, 0.6]) outcome_3 = Prediction(frame_3, ["car", "train"], [0.5, 0.6]) input_batch = FrameBatch(frames=[ frame_1, frame_2, frame_3, ], info=None) expected_value = 1 output_value = aggr_expr.evaluate(input_batch) self.assertEqual(expected_value, output_value)
def test_should_return_false_for_unequal_expressions(self): const_exp1 = ConstantValueExpression(0) const_exp2 = ConstantValueExpression(1) func_expr = FunctionExpression(lambda x: x + 1) cmpr_exp = ComparisonExpression(ExpressionType.COMPARE_NEQ, const_exp1, const_exp2) tuple_expr = TupleValueExpression(col_name='id') aggr_expr = AggregationExpression(ExpressionType.AGGREGATION_MAX, None, tuple_expr) logical_expr = LogicalExpression(ExpressionType.LOGICAL_OR, cmpr_exp, cmpr_exp) self.assertNotEqual(const_exp1, const_exp2) self.assertNotEqual(cmpr_exp, const_exp1) self.assertNotEqual(func_expr, cmpr_exp) self.assertNotEqual(tuple_expr, aggr_expr) self.assertNotEqual(aggr_expr, tuple_expr) self.assertNotEqual(tuple_expr, cmpr_exp) self.assertNotEqual(logical_expr, cmpr_exp)
def test_aggregation_max(self): columnName = TupleValueExpression(col_idx=0) aggr_expr = AggregationExpression(ExpressionType.AGGREGATION_MAX, None, columnName) tuples = [[1, 2, 3], [2, 3, 4], [3, 4, 5]] self.assertEqual(3, aggr_expr.evaluate(tuples, None))