Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions libs/labelbox/src/labelbox/schema/batch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, List, Optional

from lbox.exceptions import ResourceNotFoundError

Expand Down Expand Up @@ -62,12 +62,54 @@ def project(self) -> "Project": # type: ignore

return Entity.Project(self.client, response["project"])

def remove_queued_data_rows(self) -> None:
"""Removes remaining queued data rows from the batch and labeling queue.
def remove_queued_data_rows(
self, data_row_ids: Optional[List[str]] = None
) -> None:
"""Removes unlabeled data rows from the batch and the project's labeling queue.

The data rows stay in Catalog and in any other projects. Labeled data
rows are never removed; delete their labels first if you want them out.

Args:
batch (Batch): Batch to remove queued data rows from
data_row_ids: Ids of the data rows to remove, at most 10,000 per
call. If omitted, every unlabeled data row in the batch is
removed. If given, only these rows are removed, and if any of
them is labeled or not in this batch, nothing is removed.

Raises:
ValueError: if `data_row_ids` is empty or not a list of ids.
OperationNotAllowedException: if a row in `data_row_ids` is labeled
or not in this batch (the message names them), or the batch
has no unlabeled data rows left.

>>> batch.remove_queued_data_rows(data_row_ids=[data_row.uid])
"""
if data_row_ids is not None:
if isinstance(data_row_ids, str):
raise ValueError("data_row_ids must be a list of data row ids")
# Copy first so a one-shot iterable isn't consumed by the check.
data_row_ids = list(data_row_ids)
if not all(
isinstance(data_row_id, str) for data_row_id in data_row_ids
):
raise ValueError("data_row_ids must be a list of data row ids")
if not data_row_ids:
raise ValueError(
"data_row_ids must not be empty. Omit it to remove every "
"unlabeled data row in the batch."
)
self.client.execute(
"""mutation RemoveQueuedDataRowsFromBatchPyApi($projectId: ID!, $batchId: ID!, $dataRowIds: [ID!]) {
project(where: {id: $projectId}) { removeQueuedDataRowsFromBatch(batchId: $batchId, dataRowIds: $dataRowIds) { id } }
}""",
{
"projectId": self.project_id,
"batchId": self.uid,
"dataRowIds": data_row_ids,
},
experimental=True,
)
return

project_id_param = "projectId"
batch_id_param = "batchId"
Expand Down
21 changes: 21 additions & 0 deletions libs/labelbox/tests/integration/test_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from lbox.exceptions import (
LabelboxError,
MalformedQueryException,
OperationNotAllowedException,
ProcessingWaitTimeout,
ResourceConflict,
)
Expand Down Expand Up @@ -187,6 +188,26 @@ def test_archive_batch(project: Project, small_dataset: Dataset):
assert overview.to_label == 0


def test_remove_specific_queued_data_rows(
project: Project, small_dataset: Dataset
):
data_rows = get_data_row_ids(small_dataset)
assert len(data_rows) >= 2
removed, kept = data_rows[0], data_rows[1:]

batch = project.create_batch("batch to remove one row from", data_rows)
batch.remove_queued_data_rows(data_row_ids=[removed])

assert project.get_overview().to_label == len(kept)
# removed from the project only, not from Catalog
assert project.client.get_data_row(removed).uid == removed

# the row is no longer in the batch, so nothing is removed
with pytest.raises(OperationNotAllowedException, match=removed):
batch.remove_queued_data_rows(data_row_ids=[removed, kept[0]])
assert project.get_overview().to_label == len(kept)


def test_delete(project: Project, small_dataset: Dataset):
export_task = small_dataset.export()
export_task.wait_till_done()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from unittest.mock import MagicMock

import pytest

from labelbox.schema.batch import Batch


@pytest.fixture
def mock_client():
return MagicMock()


@pytest.fixture
def batch(mock_client):
return Batch(
mock_client,
"test_project_id",
{
"id": "test_batch_id",
"name": "test",
"createdAt": "2021-06-01T00:00:00.000Z",
"updatedAt": "2021-06-01T00:00:00.000Z",
"size": 3,
"consensusSettingsJson": None,
},
)


def test_without_ids_removes_every_queued_row(batch, mock_client):
batch.remove_queued_data_rows()

query, params = mock_client.execute.call_args.args
assert "dataRowIds" not in query
assert params == {
"projectId": "test_project_id",
"batchId": "test_batch_id",
}
assert mock_client.execute.call_args.kwargs == {"experimental": True}


def test_with_ids_sends_only_those_ids(batch, mock_client):
batch.remove_queued_data_rows(data_row_ids=("dr_1", "dr_2"))

query, params = mock_client.execute.call_args.args
assert (
"removeQueuedDataRowsFromBatch(batchId: $batchId, dataRowIds: $dataRowIds)"
in query
)
assert params == {
"projectId": "test_project_id",
"batchId": "test_batch_id",
"dataRowIds": ["dr_1", "dr_2"],
}
assert mock_client.execute.call_args.kwargs == {"experimental": True}


def test_one_shot_iterable_of_ids_is_sent_in_full(batch, mock_client):
batch.remove_queued_data_rows(
data_row_ids=(data_row_id for data_row_id in ["dr_1", "dr_2"])
)

_, params = mock_client.execute.call_args.args
assert params["dataRowIds"] == ["dr_1", "dr_2"]


def test_empty_ids_are_rejected_before_any_request(batch, mock_client):
with pytest.raises(ValueError, match="must not be empty"):
batch.remove_queued_data_rows(data_row_ids=[])

mock_client.execute.assert_not_called()


@pytest.mark.parametrize("bad_ids", ["dr_1", ["dr_1", 2]])
def test_non_list_of_ids_is_rejected(batch, mock_client, bad_ids):
with pytest.raises(ValueError, match="list of data row ids"):
batch.remove_queued_data_rows(data_row_ids=bad_ids)

mock_client.execute.assert_not_called()
Loading