Skip to content

Commit

Permalink
feat: filter labels when label_type_in is provided (#1783)
Browse files Browse the repository at this point in the history
  • Loading branch information
florianlega authored Oct 1, 2024
1 parent 6a9b516 commit 4d1e2fe
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/kili/services/export/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,31 @@ def fetch_assets(
kili.kili_api_gateway, download_media, fields, ProjectId(project_id), local_media_dir
)
assets_gen = kili.kili_api_gateway.list_assets(filters, fields, options)

if (label_type_in is not None) and (len(label_type_in) > 0):
if export_type == "latest":
assets_gen = filter(
lambda asset: asset["latestLabel"].get("labelType") in label_type_in, assets_gen
)
else:
assets_gen = filter(
lambda asset: any(
label.get("labelType") in label_type_in for label in asset["labels"]
),
assets_gen,
)
assets_gen = (
{
**asset,
"labels": [
label
for label in asset["labels"]
if label.get("labelType") in label_type_in
],
}
for asset in assets_gen
)

if download_media_function is not None:
assets: List[Dict] = []
# TODO: modify download_media function so it can take a generator of assets
Expand Down
7 changes: 7 additions & 0 deletions tests/fakes/fake_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"latestLabel": {
"jsonResponse": job_object_detection,
"author": {"firstname": "Jean-Pierre", "lastname": "Dupont"},
"labelType": "DEFAULT",
},
"externalId": "car_1",
"content": "https://storage.googleapis.com/label-public-staging/car/car_1.jpg",
Expand All @@ -77,6 +78,7 @@
"latestLabel": {
"jsonResponse": job_object_detection_with_classification,
"author": {"firstname": "Jean-Pierre", "lastname": "Dupont"},
"labelType": "DEFAULT",
},
"externalId": "car_1",
"content": "https://storage.googleapis.com/label-public-staging/car/car_1.jpg",
Expand All @@ -87,6 +89,7 @@
"latestLabel": {
"jsonResponse": {},
"author": {"firstname": "Jean-Pierre", "lastname": "Dupont"},
"labelType": "DEFAULT",
},
"externalId": "car_1",
"content": "https://storage.googleapis.com/label-public-staging/car/car_1.jpg",
Expand All @@ -97,6 +100,7 @@
"latestLabel": {
"jsonResponse": job_object_detection,
"author": {"firstname": "Jean-Pierre", "lastname": "Dupont"},
"labelType": "DEFAULT",
},
"externalId": "car_2",
"content": "https://storage.googleapis.com/label-public-staging/car/car_2.jpg",
Expand All @@ -107,6 +111,7 @@
"latestLabel": {
"jsonResponse": job_object_detection,
"author": {"firstname": "Jean-Pierre", "lastname": "Dupont"},
"labelType": "DEFAULT",
},
"externalId": "car_3",
"content": "",
Expand Down Expand Up @@ -134,6 +139,7 @@
"1": job_object_detection,
**{str(i): {} for i in range(2, 28)},
},
"labelType": "DEFAULT",
"author": {"firstname": "Jean-Pierre", "lastname": "Dupont"},
},
"externalId": "short_video",
Expand All @@ -148,6 +154,7 @@
"1": job_object_detection,
**{str(i): {} for i in range(2, 130)},
},
"labelType": "DEFAULT",
"author": {"firstname": "Jean-Pierre", "lastname": "Dupont"},
},
"externalId": "video2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"isLatestLabelForUser": True,
"isSentBackToQueue": False,
"jsonResponse": json_response,
"labelType": "DEFAULT",
},
"priority": 0,
"skipped": False,
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/services/export/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ def test_export_with_asset_filter_kwargs(mocker):
"inference_mark_gte": 0.7,
"inference_mark_lte": 0.8,
},
label_type_in=["PREDICTION"],
label_type_in=["PREDICTION", "DEFAULT", "REVIEW"],
)
expected_where = AssetFilters(
project_id=ProjectId("fake_proj_id"),
Expand All @@ -837,7 +837,7 @@ def test_export_with_asset_filter_kwargs(mocker):
honeypot_mark_lte=0.4,
created_at_gte="2022-02-03",
created_at_lte="2023-02-03",
label_type_in=["PREDICTION"],
label_type_in=["PREDICTION", "DEFAULT", "REVIEW"],
inference_mark_gte=0.7,
inference_mark_lte=0.8,
issue_type="QUESTION",
Expand Down

0 comments on commit 4d1e2fe

Please sign in to comment.