def test_should_return_all_frames_when_no_predicate_is_applied(self): dataframe = create_dataframe(3) outcome_1 = Outcome( pd.DataFrame({ 'labels': ["car", "bus"], 'scores': [0.5, 0.6] }), 'labels') outcome_2 = Outcome(pd.DataFrame({ 'labels': ["bus"], 'scores': [0.5] }), 'labels') outcome_3 = Outcome( pd.DataFrame({ 'labels': ["car", "train"], 'scores': [0.5, 0.6] }), 'labels') batch = Batch(frames=dataframe, outcomes={"test": [outcome_1, outcome_2, outcome_3]}) plan = type("ScanPlan", (), {"predicate": None}) predicate_executor = SequentialScanExecutor(plan) predicate_executor.append_child(DummyExecutor([batch])) filtered = list(predicate_executor.exec())[0] self.assertEqual(batch, filtered)
def test_should_return_only_frames_satisfy_predicate(self): dataframe = create_dataframe(3) outcome_1 = Outcome( pd.DataFrame({ 'labels': ["car", "bus"], 'scores': [0.5, 0.6] }), 'labels') outcome_2 = Outcome(pd.DataFrame({ 'labels': ["bus"], 'scores': [0.5] }), 'labels') outcome_3 = Outcome( pd.DataFrame({ 'labels': ["car", "train"], 'scores': [0.5, 0.6] }), 'labels') batch = Batch(frames=dataframe, outcomes={"test": [outcome_1, outcome_2, outcome_3]}) expression = type("AbstractExpression", (), {"evaluate": lambda x: [False, False, True]}) plan = type("ScanPlan", (), {"predicate": expression}) predicate_executor = SequentialScanExecutor(plan) predicate_executor.append_child(DummyExecutor([batch])) expected = batch[[2]] filtered = list(predicate_executor.exec())[0] self.assertEqual(expected, filtered)
def test_func_expr_with_cmpr_and_const_expr_should_work(self): frames = create_dataframe(2) outcome_1 = Outcome(pd.DataFrame( {'labels': ["car", "bus"], 'scores': [0.5, 0.6]}), 'labels') outcome_2 = Outcome(pd.DataFrame( {'labels': ["bus"], 'scores': [0.6]}), 'labels') func = FunctionExpression(lambda x: [outcome_1, outcome_2]) value_expr = ConstantValueExpression("car") expression_tree = ComparisonExpression(ExpressionType.COMPARE_EQUAL, func, value_expr) batch = Batch(frames=frames) self.assertEqual([True, False], expression_tree.evaluate(batch))
def _get_predictions(self, frames: np.ndarray) -> List[Outcome]: """ Performs predictions on input frames Arguments: frames (np.ndarray): Frames on which predictions need to be performed Returns: tuple containing predicted_classes (List[List[str]]), predicted_boxes (List[List[BoundingBox]]), predicted_scores (List[List[float]]) """ transform = transforms.Compose([transforms.ToTensor()]) images = [transform(frame) for frame in frames] predictions = self.model(images) prediction_df_list = [] for prediction in predictions: pred_class = [str(self.labels[i]) for i in list(prediction['labels'].detach().numpy())] pred_boxes = [[[i[0], i[1]], [i[2], i[3]]] for i in list(prediction['boxes'].detach().numpy())] pred_score = list(prediction['scores'].detach().numpy()) pred_t = \ [pred_score.index(x) for x in pred_score if x > self.threshold][-1] pred_boxes = list(pred_boxes[:pred_t + 1]) pred_class = list(pred_class[:pred_t + 1]) pred_score = list(pred_score[:pred_t + 1]) prediction_df_list.append( Outcome(pd.DataFrame( {"label": pred_class, "pred_score": pred_score, "pred_boxes": pred_boxes}), 'label')) return prediction_df_list
def test_ne_should_return_true_if_value_not_present(self): data = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) outcome = Outcome(data, identifier='A') self.assertTrue(outcome != 4)
def test_ne_should_return_false_if_value_present_atleast_once(self): data = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) outcome = Outcome(data, identifier='A') self.assertFalse(outcome.A != 1)
def test_lte_should_return_true_if_atleast_one_value_lte_given(self): data = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) outcome = Outcome(data, identifier='A') self.assertTrue(outcome.A <= 3)
def test_lte_should_return_false_if_no_value_lte_given(self): data = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) outcome = Outcome(data, identifier='A') self.assertFalse(outcome.A < 1)
def test_equals_should_return_false_frame_if_no_value_in_data_frame_equal( self): data = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) outcome = Outcome(data, identifier='A') self.assertFalse(outcome.A == 4)
def test_equals_should_return_true_if_alreast_one_value_equal(self): data = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) outcome = Outcome(data, identifier='A') self.assertTrue(getattr(outcome, 'A') == 1)
def test_get_attribute_should_return_new_outcome(self): data = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) outcome = Outcome(data, identifier='A') expected = Outcome(data[["A"]], identifier='A') self.assertEqual(outcome.A, expected)