|
1 | 1 | import numpy as np |
| 2 | +import pyarrow.compute as pc |
2 | 3 | import pytest |
3 | 4 |
|
| 5 | +from valor_lite.cache import DataType |
4 | 6 | from valor_lite.exceptions import EmptyFilterError |
5 | 7 | from valor_lite.semantic_segmentation import DataLoader, Segmentation |
| 8 | +from valor_lite.semantic_segmentation.evaluator import Filter |
6 | 9 |
|
7 | 10 |
|
8 | | -def test_filtering(segmentations_from_boxes: list[Segmentation]): |
| 11 | +def test_filtering_raises(segmentations_from_boxes: list[Segmentation]): |
| 12 | + |
| 13 | + loader = DataLoader() |
| 14 | + loader.add_data(segmentations_from_boxes) |
| 15 | + evaluator = loader.finalize() |
| 16 | + assert evaluator._confusion_matrix.shape == (3, 3) |
| 17 | + |
| 18 | + with pytest.raises(EmptyFilterError): |
| 19 | + evaluator.create_filter(datums=[]) |
| 20 | + assert evaluator._confusion_matrix.shape == (3, 3) |
| 21 | + |
| 22 | + |
| 23 | +def test_filtering_by_datum(segmentations_from_boxes: list[Segmentation]): |
9 | 24 |
|
10 | 25 | loader = DataLoader() |
11 | 26 | loader.add_data(segmentations_from_boxes) |
@@ -51,13 +66,105 @@ def test_filtering(segmentations_from_boxes: list[Segmentation]): |
51 | 66 | evaluator.create_filter(datums=[]) |
52 | 67 |
|
53 | 68 |
|
54 | | -def test_filtering_raises(segmentations_from_boxes: list[Segmentation]): |
| 69 | +def test_filtering_by_annotation_metadata( |
| 70 | + segmentations_from_boxes: list[Segmentation], |
| 71 | +): |
55 | 72 |
|
56 | | - loader = DataLoader() |
| 73 | + loader = DataLoader( |
| 74 | + groundtruth_metadata_types={ |
| 75 | + "gt_xmin": DataType.FLOAT, |
| 76 | + }, |
| 77 | + prediction_metadata_types={ |
| 78 | + "pd_xmin": DataType.FLOAT, |
| 79 | + }, |
| 80 | + ) |
57 | 81 | loader.add_data(segmentations_from_boxes) |
58 | 82 | evaluator = loader.finalize() |
59 | | - assert evaluator._confusion_matrix.shape == (3, 3) |
60 | 83 |
|
61 | | - with pytest.raises(EmptyFilterError): |
62 | | - evaluator.create_filter(datums=[]) |
63 | | - assert evaluator._confusion_matrix.shape == (3, 3) |
| 84 | + total_pixels = 540_000 |
| 85 | + assert evaluator.metadata.number_of_datums == 2 |
| 86 | + assert evaluator.metadata.number_of_labels == 2 |
| 87 | + assert evaluator.metadata.number_of_ground_truths == 25000 |
| 88 | + assert evaluator.metadata.number_of_predictions == 15000 |
| 89 | + assert evaluator.metadata.number_of_pixels == total_pixels |
| 90 | + |
| 91 | + # test groundtruth filtering |
| 92 | + filter_ = Filter(groundtruths=pc.field("gt_xmin") < 100) |
| 93 | + filtered_evaluator = evaluator.filter(filter_) |
| 94 | + confusion_matrix = filtered_evaluator._confusion_matrix |
| 95 | + assert np.all( |
| 96 | + confusion_matrix |
| 97 | + == np.array( |
| 98 | + [ |
| 99 | + [520000, 5000, 5000], |
| 100 | + [5000, 5000, 0], |
| 101 | + [0, 0, 0], |
| 102 | + ] |
| 103 | + ) |
| 104 | + ) |
| 105 | + assert confusion_matrix.sum() == total_pixels |
| 106 | + |
| 107 | + filter_ = Filter(groundtruths=pc.field("gt_xmin") > 100) |
| 108 | + filtered_evaluator = evaluator.filter(filter_) |
| 109 | + confusion_matrix = filtered_evaluator._confusion_matrix |
| 110 | + assert np.all( |
| 111 | + confusion_matrix |
| 112 | + == np.array( |
| 113 | + [ |
| 114 | + [510001, 10000, 4999], |
| 115 | + [0, 0, 0], |
| 116 | + [14999, 0, 1], |
| 117 | + ] |
| 118 | + ) |
| 119 | + ) |
| 120 | + assert confusion_matrix.sum() == total_pixels |
| 121 | + |
| 122 | + # test prediction filtering |
| 123 | + filter_ = Filter(predictions=pc.field("pd_xmin") < 100) |
| 124 | + filtered_evaluator = evaluator.filter(filter_) |
| 125 | + confusion_matrix = filtered_evaluator._confusion_matrix |
| 126 | + assert np.all( |
| 127 | + confusion_matrix |
| 128 | + == np.array( |
| 129 | + [ |
| 130 | + [510000, 5000, 0], |
| 131 | + [5000, 5000, 0], |
| 132 | + [15000, 0, 0], |
| 133 | + ] |
| 134 | + ) |
| 135 | + ) |
| 136 | + assert confusion_matrix.sum() == total_pixels |
| 137 | + |
| 138 | + filter_ = Filter(predictions=pc.field("pd_xmin") > 100) |
| 139 | + filtered_evaluator = evaluator.filter(filter_) |
| 140 | + confusion_matrix = filtered_evaluator._confusion_matrix |
| 141 | + assert np.all( |
| 142 | + confusion_matrix |
| 143 | + == np.array( |
| 144 | + [ |
| 145 | + [510001, 0, 4999], |
| 146 | + [10000, 0, 0], |
| 147 | + [14999, 0, 1], |
| 148 | + ] |
| 149 | + ) |
| 150 | + ) |
| 151 | + assert confusion_matrix.sum() == total_pixels |
| 152 | + |
| 153 | + # filter out all gts and pds |
| 154 | + filter_ = Filter( |
| 155 | + groundtruths=pc.field("gt_xmin") > 1000, |
| 156 | + predictions=pc.field("pd_xmin") > 1000, |
| 157 | + ) |
| 158 | + filtered_evaluator = evaluator.filter(filter_) |
| 159 | + confusion_matrix = filtered_evaluator._confusion_matrix |
| 160 | + assert np.all( |
| 161 | + confusion_matrix |
| 162 | + == np.array( |
| 163 | + [ |
| 164 | + [total_pixels, 0, 0], |
| 165 | + [0, 0, 0], |
| 166 | + [0, 0, 0], |
| 167 | + ] |
| 168 | + ) |
| 169 | + ) |
| 170 | + assert confusion_matrix.sum() == total_pixels |
0 commit comments