Skip to content

Commit 4d1e2fe

Browse files
authored
feat: filter labels when label_type_in is provided (#1783)
1 parent 6a9b516 commit 4d1e2fe

4 files changed

Lines changed: 35 additions & 2 deletions

File tree

src/kili/services/export/tools.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,31 @@ def fetch_assets(
165165
kili.kili_api_gateway, download_media, fields, ProjectId(project_id), local_media_dir
166166
)
167167
assets_gen = kili.kili_api_gateway.list_assets(filters, fields, options)
168+
169+
if (label_type_in is not None) and (len(label_type_in) > 0):
170+
if export_type == "latest":
171+
assets_gen = filter(
172+
lambda asset: asset["latestLabel"].get("labelType") in label_type_in, assets_gen
173+
)
174+
else:
175+
assets_gen = filter(
176+
lambda asset: any(
177+
label.get("labelType") in label_type_in for label in asset["labels"]
178+
),
179+
assets_gen,
180+
)
181+
assets_gen = (
182+
{
183+
**asset,
184+
"labels": [
185+
label
186+
for label in asset["labels"]
187+
if label.get("labelType") in label_type_in
188+
],
189+
}
190+
for asset in assets_gen
191+
)
192+
168193
if download_media_function is not None:
169194
assets: List[Dict] = []
170195
# TODO: modify download_media function so it can take a generator of assets

tests/fakes/fake_data.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"latestLabel": {
6666
"jsonResponse": job_object_detection,
6767
"author": {"firstname": "Jean-Pierre", "lastname": "Dupont"},
68+
"labelType": "DEFAULT",
6869
},
6970
"externalId": "car_1",
7071
"content": "https://storage.googleapis.com/label-public-staging/car/car_1.jpg",
@@ -77,6 +78,7 @@
7778
"latestLabel": {
7879
"jsonResponse": job_object_detection_with_classification,
7980
"author": {"firstname": "Jean-Pierre", "lastname": "Dupont"},
81+
"labelType": "DEFAULT",
8082
},
8183
"externalId": "car_1",
8284
"content": "https://storage.googleapis.com/label-public-staging/car/car_1.jpg",
@@ -87,6 +89,7 @@
8789
"latestLabel": {
8890
"jsonResponse": {},
8991
"author": {"firstname": "Jean-Pierre", "lastname": "Dupont"},
92+
"labelType": "DEFAULT",
9093
},
9194
"externalId": "car_1",
9295
"content": "https://storage.googleapis.com/label-public-staging/car/car_1.jpg",
@@ -97,6 +100,7 @@
97100
"latestLabel": {
98101
"jsonResponse": job_object_detection,
99102
"author": {"firstname": "Jean-Pierre", "lastname": "Dupont"},
103+
"labelType": "DEFAULT",
100104
},
101105
"externalId": "car_2",
102106
"content": "https://storage.googleapis.com/label-public-staging/car/car_2.jpg",
@@ -107,6 +111,7 @@
107111
"latestLabel": {
108112
"jsonResponse": job_object_detection,
109113
"author": {"firstname": "Jean-Pierre", "lastname": "Dupont"},
114+
"labelType": "DEFAULT",
110115
},
111116
"externalId": "car_3",
112117
"content": "",
@@ -134,6 +139,7 @@
134139
"1": job_object_detection,
135140
**{str(i): {} for i in range(2, 28)},
136141
},
142+
"labelType": "DEFAULT",
137143
"author": {"firstname": "Jean-Pierre", "lastname": "Dupont"},
138144
},
139145
"externalId": "short_video",
@@ -148,6 +154,7 @@
148154
"1": job_object_detection,
149155
**{str(i): {} for i in range(2, 130)},
150156
},
157+
"labelType": "DEFAULT",
151158
"author": {"firstname": "Jean-Pierre", "lastname": "Dupont"},
152159
},
153160
"externalId": "video2",

tests/unit/adapters/kili_api_gateway/label/test_data/test_case_13.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
"isLatestLabelForUser": True,
107107
"isSentBackToQueue": False,
108108
"jsonResponse": json_response,
109+
"labelType": "DEFAULT",
109110
},
110111
"priority": 0,
111112
"skipped": False,

tests/unit/services/export/test_export.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ def test_export_with_asset_filter_kwargs(mocker):
826826
"inference_mark_gte": 0.7,
827827
"inference_mark_lte": 0.8,
828828
},
829-
label_type_in=["PREDICTION"],
829+
label_type_in=["PREDICTION", "DEFAULT", "REVIEW"],
830830
)
831831
expected_where = AssetFilters(
832832
project_id=ProjectId("fake_proj_id"),
@@ -837,7 +837,7 @@ def test_export_with_asset_filter_kwargs(mocker):
837837
honeypot_mark_lte=0.4,
838838
created_at_gte="2022-02-03",
839839
created_at_lte="2023-02-03",
840-
label_type_in=["PREDICTION"],
840+
label_type_in=["PREDICTION", "DEFAULT", "REVIEW"],
841841
inference_mark_gte=0.7,
842842
inference_mark_lte=0.8,
843843
issue_type="QUESTION",

0 commit comments

Comments
 (0)