-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlabel_grouper.py
More file actions
43 lines (37 loc) · 1.26 KB
/
label_grouper.py
File metadata and controls
43 lines (37 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from typing import Any, List
import numpy as np
import pandas as pd
class LabelsGrouper:
def __init__(self, annotations_or_predictions_list: List[Any]):
self.items = annotations_or_predictions_list
if len(self.items) > 0:
assert hasattr(
self.items[0], "label"
), f"Expected items to have attribute 'label' found none on {repr(self.items[0])}"
self.codes, self.labels = pd.factorize(
[item.label for item in self.items]
)
self.group_idx = 0
def __iter__(self):
self.group_idx = 0
return self
def __next__(self):
if self.group_idx >= len(self.labels):
raise StopIteration
label = self.labels[self.group_idx]
label_items = list(
np.take(self.items, np.where(self.codes == self.group_idx)[0])
)
self.group_idx += 1
return label, label_items
def label_group(self, label: str) -> List[Any]:
if len(self.items) == 0:
return []
idx = np.where(self.labels == label)[0]
if idx >= 0:
label_items = list(
np.take(self.items, np.where(self.codes == idx)[0])
)
return label_items
else:
return []