Skip to content

Commit

Permalink
feat: add project_id in append_labels (#1596)
Browse files Browse the repository at this point in the history
Co-authored-by: theodu <[email protected]>
  • Loading branch information
theodu and theodu authored Nov 23, 2023
1 parent d962780 commit 923317f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
18 changes: 15 additions & 3 deletions src/kili/adapters/kili_api_gateway/label/operations_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from kili.core.utils.pagination import batcher
from kili.domain.asset import AssetId
from kili.domain.label import LabelFilters, LabelId
from kili.domain.project import ProjectId
from kili.domain.types import ListOrTuple
from kili.utils.tqdm import tqdm

Expand Down Expand Up @@ -109,7 +110,11 @@ def delete_labels(
return delete_label_ids

def append_many_labels(
self, data: AppendManyLabelsData, fields: ListOrTuple[str], disable_tqdm: Optional[bool]
self,
data: AppendManyLabelsData,
fields: ListOrTuple[str],
disable_tqdm: Optional[bool],
project_id: Optional[ProjectId],
) -> List[Dict]:
"""Append many labels."""
nb_labels_to_add = len(data.labels_data)
Expand All @@ -128,8 +133,12 @@ def append_many_labels(
append_label_data_mapper(label) for label in batch_of_label_data
],
},
"where": {"idIn": [label.asset_id for label in batch_of_label_data]},
"where": {
"idIn": [label.asset_id for label in batch_of_label_data],
},
}
if project_id is not None:
variables["where"]["project"] = {"id": project_id}

# we increase the timeout because the import can take a long time
batch_result = self.graphql_client.execute(query, variables, timeout=60)
Expand All @@ -144,7 +153,10 @@ def append_to_labels(
"""Append to labels."""
fragment = fragment_builder(fields)
query = get_append_to_labels_mutation(fragment)
variables = {"where": {"id": asset_id}, "data": append_to_labels_data_mapper(data)}
variables = {
"where": {"id": asset_id},
"data": append_to_labels_data_mapper(data),
}
result = self.graphql_client.execute(query, variables)
return result["data"]

Expand Down
5 changes: 4 additions & 1 deletion src/kili/use_cases/label/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ def append_labels(
labels_data=labels_to_add,
)
return self._kili_api_gateway.append_many_labels(
fields=fields, disable_tqdm=disable_tqdm, data=data
fields=fields,
disable_tqdm=disable_tqdm,
data=data,
project_id=project_id,
)

def append_to_labels(
Expand Down
10 changes: 7 additions & 3 deletions tests/integration/adapters/kili_api_gateway/test_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@


def test_given_kili_gateway_when_querying_labels__it_calls_proper_resolver(
graphql_client: GraphQLClient, http_client: HttpClient, mocker: pytest_mock.MockerFixture
graphql_client: GraphQLClient,
http_client: HttpClient,
mocker: pytest_mock.MockerFixture,
):
# Given
mocker.patch.object(PaginatedGraphQLQuery, "get_number_of_elements_to_query", return_value=1)
Expand Down Expand Up @@ -124,6 +126,7 @@ def test_given_kili_gateway_when_adding_labels_then_it_calls_proper_resolver(
),
fields=("id",),
disable_tqdm=True,
project_id=None,
)

# Then
Expand All @@ -145,7 +148,7 @@ def test_given_kili_gateway_when_adding_labels_then_it_calls_proper_resolver(
}
],
},
"where": {"idIn": ["fake_asset_id"]},
"where": {"idIn": ["fake_asset_id"], "project": {"id": None}},
},
)

Expand Down Expand Up @@ -175,6 +178,7 @@ def test_given_kili_gateway_when_adding_labels_by_batch_then_it_calls_proper_res
),
fields=("id",),
disable_tqdm=True,
project_id=ProjectId("project_id"),
)

# Then
Expand All @@ -197,7 +201,7 @@ def test_given_kili_gateway_when_adding_labels_by_batch_then_it_calls_proper_res
}
],
},
"where": {"idIn": ["fake_asset_id_101"]},
"where": {"idIn": ["fake_asset_id_101"], "project": {"id": "project_id"}},
},
)

Expand Down
11 changes: 8 additions & 3 deletions tests/integration/use_cases/test_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

def test_import_default_labels_with_asset_id(kili_api_gateway: KiliAPIGateway):
# Given
project_id = "project_id"
label_type = "DEFAULT"
overwrite = False
model_name = None
Expand Down Expand Up @@ -53,7 +52,7 @@ def test_import_default_labels_with_asset_id(kili_api_gateway: KiliAPIGateway):
disable_tqdm=True,
overwrite=overwrite,
label_type=label_type,
project_id=ProjectId(project_id),
project_id=None,
fields=("id",),
)

Expand Down Expand Up @@ -83,6 +82,7 @@ def test_import_default_labels_with_asset_id(kili_api_gateway: KiliAPIGateway):
],
),
fields=("id",),
project_id=None,
)


Expand Down Expand Up @@ -157,6 +157,7 @@ def test_import_default_labels_with_external_id(kili_api_gateway: KiliAPIGateway
],
),
fields=("id",),
project_id=ProjectId(project_id),
)


Expand Down Expand Up @@ -208,6 +209,7 @@ def test_import_labels_with_optional_params(kili_api_gateway: KiliAPIGateway):
],
),
fields=("id",),
project_id=ProjectId(project_id),
)


Expand Down Expand Up @@ -282,6 +284,7 @@ def test_import_predictions(kili_api_gateway: KiliAPIGateway):
],
),
fields=("id",),
project_id=ProjectId(project_id),
)


Expand Down Expand Up @@ -339,6 +342,7 @@ def test_import_predictions_with_overwriting(kili_api_gateway: KiliAPIGateway):
],
),
fields=("id",),
project_id=ProjectId(project_id),
)


Expand All @@ -362,7 +366,8 @@ def test_import_predictions_without_giving_model_name(kili_api_gateway: KiliAPIG

# When Then
with pytest.raises(
ValueError, match="You must provide `model_name` when uploading `PREDICTION` labels."
ValueError,
match="You must provide `model_name` when uploading `PREDICTION` labels.",
):
LabelUseCases(kili_api_gateway).append_labels(
labels=labels,
Expand Down

0 comments on commit 923317f

Please sign in to comment.