Skip to content

Commit 6512fdd

Browse files
authored
Merge pull request #445 from Labelbox/al-1555
[AL-1555] Updating Test Cases
2 parents 65ec3b6 + 03d871c commit 6512fdd

22 files changed

+352
-538
lines changed

examples/model_assisted_labeling/mal_basics.ipynb

Lines changed: 0 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -1,146 +1,4 @@
11
{
2-
"cells": [
3-
{
4-
"cell_type": "markdown",
5-
"id": "db768cda",
6-
"metadata": {
7-
"id": "db768cda"
8-
},
9-
"source": [
10-
"<td>\n",
11-
" <a target=\"_blank\" href=\"https://labelbox.com\" ><img src=\"https://labelbox.com/blog/content/images/2021/02/logo-v4.svg\" width=256/></a>\n",
12-
"</td>"
13-
]
14-
},
15-
{
16-
"cell_type": "markdown",
17-
"id": "cb5611d0",
18-
"metadata": {
19-
"id": "cb5611d0"
20-
},
21-
"source": [
22-
"<td>\n",
23-
"<a href=\"https://colab.research.google.com/github/Labelbox/labelbox-python/blob/develop/examples/model_assisted_labeling/mal_basics.ipynb\" target=\"_blank\"><img\n",
24-
"src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"></a>\n",
25-
"</td>\n",
26-
"\n",
27-
"<td>\n",
28-
"<a href=\"https://github.com/Labelbox/labelbox-python/tree/develop/examples/model_assisted_labeling/mal_basics.ipynb\" target=\"_blank\"><img\n",
29-
"src=\"https://img.shields.io/badge/GitHub-100000?logo=github&logoColor=white\" alt=\"GitHub\"></a>\n",
30-
"</td>"
31-
]
32-
},
33-
{
34-
"cell_type": "markdown",
35-
"id": "fundamental-failure",
36-
"metadata": {
37-
"id": "fundamental-failure"
38-
},
39-
"source": [
40-
"# Annotation Imports\n",
41-
"* This notebook is a high level introduction demonstrating multiple ways to upload your annotations. This will cover the following:\n",
42-
" * Model-assisted labeling - used to provide pre-annotated data for your labelers. This will enable a reduction in the total amount of time to properly label your assets. Model-assisted labeling does not submit the labels automatically, and will need to be reviewed by a labeler for submission.\n",
43-
" * Label Import - used to provide ground truth labels. These can in turn be used and compared against prediction labels, or used as benchmarks to see how your labelers are doing.\n",
44-
"\n",
45-
"\n",
46-
"* For complete examples see image_mal.ipynb or ner_mal.ipynb"
47-
]
48-
},
49-
{
50-
"cell_type": "markdown",
51-
"id": "registered-parts",
52-
"metadata": {
53-
"id": "registered-parts"
54-
},
55-
"source": [
56-
"* For information on what types of annotations are supported per data type, refer to this documentation:\n",
57-
" * https://docs.labelbox.com/docs/model-assisted-labeling#option-1-import-via-python-annotation-types-recommended"
58-
]
59-
},
60-
{
61-
"cell_type": "markdown",
62-
"id": "legislative-violence",
63-
"metadata": {
64-
"id": "legislative-violence"
65-
},
66-
"source": [
67-
"* Notes:\n",
68-
" * If you are importing more than 1,000 mask annotations at a time, consider submitting separate jobs, as they can take longer than other annotation types to import.\n",
69-
" * Wait until the import job is complete before opening the Editor to make sure all annotations are imported properly."
70-
]
71-
},
72-
{
73-
"cell_type": "markdown",
74-
"id": "70072299-2ffe-4ea3-9af1-410d9bfd18cc",
75-
"metadata": {
76-
"id": "70072299-2ffe-4ea3-9af1-410d9bfd18cc"
77-
},
78-
"source": [
79-
"# Installs"
80-
]
81-
},
82-
{
83-
"cell_type": "code",
84-
"execution_count": null,
85-
"id": "pointed-disability",
86-
"metadata": {
87-
"id": "pointed-disability"
88-
},
89-
"outputs": [],
90-
"source": [
91-
"!pip install -q 'labelbox[data]'"
92-
]
93-
},
94-
{
95-
"cell_type": "markdown",
96-
"id": "a5c271de-1006-400e-a5bb-d466b833b734",
97-
"metadata": {
98-
"id": "a5c271de-1006-400e-a5bb-d466b833b734"
99-
},
100-
"source": [
101-
"# Imports"
102-
]
103-
},
104-
{
105-
"cell_type": "code",
106-
"execution_count": null,
107-
"id": "guided-arthritis",
108-
"metadata": {
109-
"id": "guided-arthritis"
110-
},
111-
"outputs": [],
112-
"source": [
113-
"from labelbox.schema.ontology import OntologyBuilder, Tool\n",
114-
"from labelbox import Client, LabelingFrontend, LabelImport, MALPredictionImport\n",
115-
"from labelbox.data.annotation_types import (\n",
116-
" Label, ImageData, ObjectAnnotation, Rectangle, Point, Radio,\n",
117-
" ClassificationAnnotation, ClassificationAnswer\n",
118-
")\n",
119-
"from labelbox.data.serialization import NDJsonConverter\n",
120-
"import uuid\n",
121-
"import json"
122-
]
123-
},
124-
{
125-
"cell_type": "markdown",
126-
"id": "7ff330d7",
127-
"metadata": {
128-
"id": "7ff330d7"
129-
},
130-
"source": [
131-
"# API Key and Client\n",
132-
"Provide a valid api key below in order to properly connect to the Labelbox Client."
133-
]
134-
},
135-
{
136-
"cell_type": "code",
137-
"execution_count": null,
138-
"id": "preliminary-benchmark",
139-
"metadata": {
140-
"id": "preliminary-benchmark",
141-
"outputId": "8cf16a44-d0b8-477f-b361-43865b2fc572"
142-
},
143-
"outputs": [
1442
{
1453
"name": "stderr",
1464
"output_type": "stream",

tests/conftest.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from datetime import datetime
2+
from random import randint
3+
from string import ascii_letters
4+
5+
import pytest
6+
7+
8+
@pytest.fixture
9+
def rand_gen():
10+
11+
def gen(field_type):
12+
if field_type is str:
13+
return "".join(ascii_letters[randint(0,
14+
len(ascii_letters) - 1)]
15+
for _ in range(16))
16+
17+
if field_type is datetime:
18+
return datetime.now()
19+
20+
raise Exception("Can't random generate for field type '%r'" %
21+
field_type)
22+
23+
return gen

tests/integration/annotation_import/test_bulk_import_request.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import uuid
22
import ndjson
33
import pytest
4-
import requests
54

65
from labelbox.exceptions import MALValidationError, UuidError
76
from labelbox.schema.bulk_import_request import BulkImportRequest
@@ -29,7 +28,7 @@ def test_create_from_url(configured_project):
2928
assert bulk_import_request.state == BulkImportRequestState.RUNNING
3029

3130

32-
def test_validate_file(client, configured_project):
31+
def test_validate_file(configured_project):
3332
name = str(uuid.uuid4())
3433
url = "https://storage.googleapis.com/labelbox-public-bucket/predictions_test_v2.ndjson"
3534
with pytest.raises(MALValidationError):
@@ -151,7 +150,7 @@ def test_wait_till_done(rectangle_inference, configured_project):
151150
'uuid']
152151

153152

154-
def test_project_bulk_import_requests(client, configured_project, predictions):
153+
def test_project_bulk_import_requests(configured_project, predictions):
155154
result = configured_project.bulk_import_requests()
156155
assert len(list(result)) == 0
157156

@@ -174,7 +173,7 @@ def test_project_bulk_import_requests(client, configured_project, predictions):
174173
assert len(list(result)) == 3
175174

176175

177-
def test_delete(client, configured_project, predictions):
176+
def test_delete(configured_project, predictions):
178177
name = str(uuid.uuid4())
179178

180179
bulk_import_request = configured_project.upload_annotations(

tests/integration/annotation_import/test_label_import.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import uuid
2-
import ndjson
32
import pytest
4-
import requests
53

64
from labelbox.schema.annotation_import import AnnotationImportState, LabelImport
75
"""

tests/integration/annotation_import/test_model_run.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33

44
def test_model_run(client, configured_project_with_label, rand_gen):
5-
project, label_id = configured_project_with_label
5+
project, _, _, label = configured_project_with_label
6+
label_id = label.uid
67
ontology = project.ontology()
78
data = {"name": rand_gen(str), "ontology_id": ontology.uid}
89
model = client.create_model(data["name"], data["ontology_id"])

tests/integration/conftest.py

Lines changed: 26 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import re
33
import uuid
44
import time
5-
from collections import namedtuple
65
from datetime import datetime
76
from enum import Enum
87
from random import randint
@@ -15,8 +14,6 @@
1514
from labelbox import Client
1615
from labelbox import LabelingFrontend
1716
from labelbox.orm import query
18-
from labelbox.schema.annotation_import import MALPredictionImport
19-
from labelbox.orm.db_object import Entity, DbObject
2017
from labelbox.pagination import PaginatedCollection
2118
from labelbox.schema.invite import Invite
2219
from labelbox.schema.user import User
@@ -137,49 +134,9 @@ def image_url(client):
137134
return client.upload_data(requests.get(IMG_URL).content, sign=True)
138135

139136

140-
@pytest.fixture
141-
def rand_gen():
142-
143-
def gen(field_type):
144-
if field_type is str:
145-
return "".join(ascii_letters[randint(0,
146-
len(ascii_letters) - 1)]
147-
for _ in range(16))
148-
149-
if field_type is datetime:
150-
return datetime.now()
151-
152-
raise Exception("Can't random generate for field type '%r'" %
153-
field_type)
154-
155-
return gen
156-
157-
158137
@pytest.fixture
159138
def project(client, rand_gen):
160139
project = client.create_project(name=rand_gen(str))
161-
162-
def create_label(**kwargs):
163-
""" Creates a label on a Legacy Editor project. Not supported in the new Editor.
164-
Args:
165-
**kwargs: Label attributes. At minimum, the label `DataRow`.
166-
"""
167-
Label = Entity.Label
168-
kwargs[Label.project] = project
169-
kwargs[Label.seconds_to_label] = kwargs.get(Label.seconds_to_label.name,
170-
0.0)
171-
data = {
172-
Label.attribute(attr) if isinstance(attr, str) else attr:
173-
value.uid if isinstance(value, DbObject) else value
174-
for attr, value in kwargs.items()
175-
}
176-
query_str, params = query.create(Label, data)
177-
query_str = query_str.replace(
178-
"data: {", "data: {type: {connect: {name: \"Any\"}} ")
179-
res = project.client.execute(query_str, params)
180-
return Label(project.client, res["createLabel"])
181-
182-
project.create_label = create_label
183140
yield project
184141
project.delete()
185142

@@ -205,21 +162,6 @@ def datarow(dataset, image_url):
205162
dr.delete()
206163

207164

208-
LabelPack = namedtuple("LabelPack", "project dataset data_row label")
209-
210-
211-
@pytest.fixture
212-
def label_pack(project, rand_gen, image_url):
213-
client = project.client
214-
dataset = client.create_dataset(name=rand_gen(str))
215-
project.datasets.connect(dataset)
216-
data_row = dataset.create_data_row(row_data=IMG_URL)
217-
label = project.create_label(data_row=data_row, label=rand_gen(str))
218-
time.sleep(10)
219-
yield LabelPack(project, dataset, data_row, label)
220-
dataset.delete()
221-
222-
223165
@pytest.fixture
224166
def iframe_url(environ) -> str:
225167
if environ in [Environ.PROD, Environ.LOCAL]:
@@ -298,10 +240,14 @@ def configured_project(project, client, rand_gen, image_url):
298240

299241

300242
@pytest.fixture
301-
def configured_project_with_label(client, rand_gen, image_url):
302-
project = client.create_project(name=rand_gen(str))
303-
dataset = client.create_dataset(name=rand_gen(str), projects=project)
304-
data_row = dataset.create_data_row(row_data=image_url)
243+
def configured_project_with_label(client, rand_gen, image_url, project, dataset,
244+
datarow):
245+
"""Project with a connected dataset, having one datarow
246+
Project contains an ontology with 1 bbox tool
247+
Additionally includes a create_label method for any needed extra labels
248+
One label is already created and yielded when using fixture
249+
"""
250+
project.datasets.connect(dataset)
305251
editor = list(
306252
project.client.get_labeling_frontends(
307253
where=LabelingFrontend.name == "editor"))[0]
@@ -310,13 +256,12 @@ def configured_project_with_label(client, rand_gen, image_url):
310256
Tool(tool=Tool.Type.BBOX, name="test-bbox-class"),
311257
])
312258
project.setup(editor, ontology_builder.asdict())
313-
project.enable_model_assisted_labeling()
314259
ontology = ontology_builder.from_project(project)
315260
predictions = [{
316261
"uuid": str(uuid.uuid4()),
317262
"schemaId": ontology.tools[0].feature_schema_id,
318263
"dataRow": {
319-
"id": data_row.uid
264+
"id": datarow.uid
320265
},
321266
"bbox": {
322267
"top": 20,
@@ -325,13 +270,23 @@ def configured_project_with_label(client, rand_gen, image_url):
325270
"width": 50
326271
}
327272
}]
328-
upload_task = LabelImport.create_from_objects(
329-
client, project.uid, f'label-import-{uuid.uuid4()}', predictions)
330-
upload_task.wait_until_done()
331-
label = next(project.labels()).uid
332-
yield [project, label]
333-
dataset.delete()
334-
project.delete()
273+
274+
def create_label():
275+
""" Ad-hoc function to create a LabelImport
276+
277+
Creates a LabelImport task which will create a label
278+
"""
279+
upload_task = LabelImport.create_from_objects(
280+
client, project.uid, f'label-import-{uuid.uuid4()}', predictions)
281+
upload_task.wait_until_done(sleep_time_seconds=5)
282+
283+
project.create_label = create_label
284+
project.create_label()
285+
label = next(project.labels())
286+
yield [project, dataset, datarow, label]
287+
288+
for label in project.labels():
289+
label.delete()
335290

336291

337292
@pytest.fixture

tests/integration/test_batch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def big_dataset(dataset: Dataset):
2020
dataset.delete()
2121

2222

23-
def test_submit_batch(configured_project: Project, big_dataset):
23+
def test_submit_batch(configured_project: Project, big_dataset: Dataset):
2424
configured_project.update(queue_mode=QueueMode.Batch)
2525

2626
data_rows = [dr.uid for dr in list(big_dataset.export_data_rows())]

tests/integration/test_benchmark.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
def test_benchmark(label_pack):
2-
project, _, data_row, label = label_pack
1+
def test_benchmark(configured_project_with_label):
2+
project, _, data_row, label = configured_project_with_label
33
assert set(project.benchmarks()) == set()
44
assert label.is_benchmark_reference == False
55

0 commit comments

Comments
 (0)