Skip to content

Commit 0fa39ed

Browse files
Add Benchmark
1 parent c56e564 commit 0fa39ed

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

labelbox/orm/query.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,3 +603,20 @@ def project_review_metrics(project, net_score):
603603
}""" % (project_id_param, project_id_param, net_score_literal)
604604

605605
return query_str, {project_id_param: project.uid}
606+
607+
608+
def create_benchmark(label):
609+
label_id_param = "labelId"
610+
query_str = """mutation CreateBenchmarkPyApi($%s: ID!) {
611+
createBenchmark(data: {labelId: $%s}) {%s}} """ % (
612+
label_id_param, label_id_param,
613+
results_query_part(Entity.named("Benchmark")))
614+
return query_str, {label_id_param: label.uid}
615+
616+
617+
def delete_benchmark(label):
618+
label_id_param = "labelId"
619+
query_str = """mutation DeleteBenchmarkPyApi($%s: ID!) {
620+
deleteBenchmark(where: {labelId: $%s}) {id}} """ % (
621+
label_id_param, label_id_param)
622+
return query_str, {label_id_param: label.uid}

labelbox/schema.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class Project(DbObject, Updateable, Deletable):
4444
labeling_parameter_overrides = Relationship.ToMany(
4545
"LabelingParameterOverride", False, "labeling_parameter_overrides")
4646
webhooks = Relationship.ToMany("Webhook", False)
47+
benchmarks = Relationship.ToMany("Benchmark", False)
4748

4849
def create_label(self, **kwargs):
4950
""" Creates a label on this project.
@@ -408,6 +409,15 @@ def create_review(self, **kwargs):
408409
kwargs[Review.project.name] = self.project()
409410
return self.client._create(Review, kwargs)
410411

412+
def create_benchmark(self):
413+
""" Creates a Benchmark for this Label.
414+
Return:
415+
The newly created Benchmark.
416+
"""
417+
query_str, params = query.create_benchmark(self)
418+
res = self.client.execute(query_str, params)
419+
res = res["data"]["createBenchmark"]
420+
return Benchmark(self.client, res)
411421

412422
class Review(DbObject, Deletable, Updateable):
413423

@@ -425,6 +435,26 @@ class NetScore(Enum):
425435
project = Relationship.ToOne("Project", False)
426436
label = Relationship.ToOne("Label", False)
427437

438+
class Benchmark(DbObject):
439+
""" Benchmarks (also known as Golden Standard) is a quality assurance tool
440+
for training data. Training data quality is the measure of accuracy and
441+
consistency of the training data. Benchmarks works by interspersing data
442+
to be labeled, for which there is a benchmark label, to each person labeling.
443+
These labeled data are compared against their respective benchmark and an
444+
accuracy score between 0 and 100 percent is calculated.
445+
"""
446+
created_at = Field.DateTime("created_at")
447+
created_by = Relationship.ToOne("User", False, "created_by")
448+
last_activity = Field.DateTime("last_activity")
449+
average_agreement = Field.Float("average_agreement")
450+
completed_count = Field.Int("completed_count")
451+
452+
reference_label = Relationship.ToOne("Label", False, "reference_label")
453+
454+
def delete(self):
455+
query_str, params = query.delete_benchmark(self.reference_label())
456+
self.client.execute(query_str, params)
457+
428458

429459
class AssetMetadata(DbObject):
430460
""" AssetMetadata is a datatype to provide extra context about an asset
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
IMG_URL = "https://picsum.photos/200/300"
2+
3+
4+
def test_benchmark(client, rand_gen):
5+
project = client.create_project(name=rand_gen(str))
6+
dataset = client.create_dataset(name=rand_gen(str), projects=project)
7+
data_row = dataset.create_data_row(row_data=IMG_URL)
8+
label = project.create_label(data_row=data_row, label="test",
9+
seconds_to_label=0.0)
10+
assert set(project.benchmarks()) == set()
11+
assert label.is_benchmark_reference == False
12+
13+
benchmark = label.create_benchmark()
14+
assert set(project.benchmarks()) == {benchmark}
15+
assert benchmark.reference_label() == label
16+
# Refresh label data to check it's benchmark reference
17+
label = list(data_row.labels())[0]
18+
assert label.is_benchmark_reference == True
19+
20+
benchmark.delete()
21+
assert set(project.benchmarks()) == set()
22+
# Refresh label data to check it's benchmark reference
23+
label = list(data_row.labels())[0]
24+
assert label.is_benchmark_reference == False
25+
26+
dataset.delete()
27+
project.delete()

0 commit comments

Comments
 (0)