Skip to content

Commit 67dc7a9

Browse files
author
Kevin Kim
committed
Address PR comments and fix build failure
1 parent b87860a commit 67dc7a9

File tree

15 files changed

+1242
-94
lines changed

15 files changed

+1242
-94
lines changed

labelbox/data/serialization/ndjson/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from uuid import uuid4
33
from pydantic import BaseModel, root_validator, validator, Field
44

5-
from labelbox.utils import camel_case
5+
from labelbox.utils import camel_case, is_exactly_one_set
66
from ...annotation_types.types import Cuid
77

88

@@ -12,7 +12,7 @@ class DataRow(BaseModel):
1212

1313
@root_validator()
1414
def must_set_one(cls, values):
15-
if bool(values.get('id')) == bool(values.get('global_key')):
15+
if is_exactly_one_set(values.get('id'), values.get('global_key')):
1616
raise ValueError("Must set either id or global_key")
1717
return values
1818

labelbox/schema/annotation_import.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from labelbox.orm import query
1515
from labelbox.orm.db_object import DbObject
1616
from labelbox.orm.model import Field, Relationship
17+
from labelbox.utils import is_exactly_one_set
1718
from labelbox.schema.confidence_presence_checker import LabelsConfidencePresenceChecker
1819
from labelbox.schema.enums import AnnotationImportState
1920
from labelbox.schema.serialization import serialize_labels
@@ -155,7 +156,7 @@ def _get_ndjson_from_objects(cls, objects: Union[List[Dict[str, Any]],
155156
)
156157

157158
objects = serialize_labels(objects)
158-
cls.validate_data_rows(objects)
159+
cls._validate_data_rows(objects)
159160

160161
data_str = ndjson.dumps(objects)
161162
if not data_str:
@@ -174,7 +175,7 @@ def refresh(self) -> None:
174175
self._set_field_values(res)
175176

176177
@classmethod
177-
def validate_data_rows(cls, objects: List[Dict[str, Any]]):
178+
def _validate_data_rows(cls, objects: List[Dict[str, Any]]):
178179
"""
179180
Validates annotations by checking 'dataRow' is provided
180181
and only one of 'id' or 'globalKey' is provided.
@@ -187,7 +188,8 @@ def validate_data_rows(cls, objects: List[Dict[str, Any]]):
187188
for object in objects:
188189
if 'dataRow' not in object:
189190
errors.append(f"'dataRow' is missing in {object}")
190-
elif 'id' in object['dataRow'] and 'globalKey' in object['dataRow']:
191+
elif not is_exactly_one_set(object['dataRow'].get('id'),
192+
object['dataRow'].get('globalKey')):
191193
errors.append(
192194
f"Must provide only one of 'id' or 'globalKey' for 'dataRow' in {object}"
193195
)

labelbox/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ def snake_case(s):
2626
return _convert(s, "_", lambda i: False)
2727

2828

29+
def is_exactly_one_set(x, y):
30+
return not (bool(x) == bool(y))
31+
32+
2933
class _CamelCaseMixin(BaseModel):
3034

3135
class Config:

tests/data/annotation_types/test_metrics.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def test_legacy_scalar_metric():
1818
'data': {
1919
'external_id': None,
2020
'uid': 'ckrmd9q8g000009mg6vej7hzg',
21+
'global_key': None,
2122
'im_bytes': None,
2223
'file_path': None,
2324
'url': None,
@@ -68,6 +69,7 @@ def test_custom_scalar_metric(feature_name, subclass_name, aggregation, value):
6869
'data': {
6970
'external_id': None,
7071
'uid': 'ckrmd9q8g000009mg6vej7hzg',
72+
'global_key': None,
7173
'im_bytes': None,
7274
'file_path': None,
7375
'url': None,
@@ -124,6 +126,7 @@ def test_custom_confusison_matrix_metric(feature_name, subclass_name,
124126
'data': {
125127
'external_id': None,
126128
'uid': 'ckrmd9q8g000009mg6vej7hzg',
129+
'global_key': None,
127130
'im_bytes': None,
128131
'file_path': None,
129132
'url': None,
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[
2+
{
3+
"answer": {
4+
"schemaId": "ckrb1sfl8099g0y91cxbd5ftb",
5+
"confidence": 0.8
6+
},
7+
"schemaId": "c123",
8+
"dataRow": {
9+
"globalKey": "05e8ee85-072e-4eb2-b30a-501dee9b0d9d"
10+
},
11+
"uuid": "f6879f59-d2b5-49c2-aceb-d9e8dc478673"
12+
},
13+
{
14+
"answer": [
15+
{
16+
"schemaId": "ckrb1sfl8099e0y919v260awv",
17+
"confidence": 0.82
18+
}
19+
],
20+
"schemaId": "ckrb1sfkn099c0y910wbo0p1a",
21+
"dataRow": {
22+
"globalKey": "05e8ee85-072e-4eb2-b30a-501dee9b0d9d"
23+
},
24+
"uuid": "d009925d-91a3-4f67-abd9-753453f5a584"
25+
},
26+
{
27+
"answer": "a value",
28+
"schemaId": "ckrb1sfkn099c0y910wbo0p1a",
29+
"dataRow": {
30+
"globalKey": "05e8ee85-072e-4eb2-b30a-501dee9b0d9d"
31+
},
32+
"uuid": "d009925d-91a3-4f67-abd9-753453f5a584"
33+
}
34+
]

0 commit comments

Comments
 (0)