Skip to content

Commit 43404d1

Browse files
author
Ubuntu
committed
Document 3 more files
1 parent c2a99bd commit 43404d1

File tree

3 files changed

+37
-37
lines changed

3 files changed

+37
-37
lines changed

nucleus/upload_response.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@ def json_list_to_dataset_item(item_list):
1818
class UploadResponse:
1919
"""
2020
Response for long upload job
21-
# TODO refactor
21+
22+
Attributes:
23+
dataset_id: The scale-generated id for the dataset that was uploaded to
24+
new_items: How many items are new in the upload
25+
updated_items: How many items were updated
26+
ignored_items: How many items were ignored
27+
upload_errors: A list of errors encountered during upload
28+
error_codes: A set of all the error codes encountered during upload
29+
error_payload: The detailed error payload returned from the endpoint.
2230
"""
2331

2432
def __init__(self, json: dict):

nucleus/url_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ def sanitize_field(field):
66

77

88
def sanitize_string_args(function):
9+
"""Helper decorator that ensures that all arguments passed are url-safe."""
10+
911
def sanitized_function(*args, **kwargs):
1012
sanitized_args = []
1113
sanitized_kwargs = {}

nucleus/utils.py

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ def format_prediction_response(
5656
]
5757
],
5858
]:
59+
"""Helper function to convert JSON response from endpoints to python objects
60+
61+
Args:
62+
response: JSON dictionary response from REST endpoint.
63+
Returns:
64+
annotation_response: Dictionary containing a list of annotations for each type,
65+
keyed by the type name.
66+
"""
5967
annotation_payload = response.get(ANNOTATIONS_KEY, None)
6068
if not annotation_payload:
6169
# An error occurred
@@ -86,43 +94,15 @@ def format_prediction_response(
8694
return annotation_response
8795

8896

89-
def _get_all_field_values(metadata_list: List[dict], key: str):
90-
return {metadata[key] for metadata in metadata_list if key in metadata}
91-
92-
93-
def suggest_metadata_schema(
94-
data: Union[
95-
List[DatasetItem],
96-
List[BoxPrediction],
97-
List[PolygonPrediction],
98-
List[CuboidPrediction],
99-
]
100-
):
101-
metadata_list: List[dict] = [
102-
d.metadata for d in data if d.metadata is not None
103-
]
104-
schema = {}
105-
all_keys = {k for metadata in metadata_list for k in metadata.keys()}
106-
107-
all_key_values: Dict[str, set] = {
108-
k: _get_all_field_values(metadata_list, k) for k in all_keys
109-
}
110-
111-
for key, values in all_key_values.items():
112-
entry: dict = {}
113-
if all(isinstance(x, (float, int)) for x in values):
114-
entry["type"] = "number"
115-
elif len(values) <= 50:
116-
entry["type"] = "category"
117-
entry["choices"] = list(values)
118-
else:
119-
entry["type"] = "text"
120-
schema[key] = entry
121-
return schema
122-
123-
12497
def format_dataset_item_response(response: dict) -> dict:
125-
"""Format the raw client response into api objects."""
98+
"""Format the raw client response into api objects.
99+
100+
Args:
101+
response: JSON dictionary response from REST endpoint
102+
Returns:
103+
item_dict: A dictionary with two entries, one for the dataset item, and annother
104+
for all of the associated annotations.
105+
"""
126106
if ANNOTATIONS_KEY not in response:
127107
raise ValueError(
128108
f"Server response was missing the annotation key: {response}"
@@ -148,6 +128,15 @@ def format_dataset_item_response(response: dict) -> dict:
148128

149129

150130
def convert_export_payload(api_payload):
131+
"""Helper function to convert raw JSON to API objects
132+
133+
Args:
134+
api_payload: JSON dictionary response from REST endpoint
135+
Returns:
136+
return_payload: A list of dictionaries for each dataset item. Each dictionary
137+
is in the same format as format_dataset_item_response: one key for the
138+
dataset item, another for the annotations.
139+
"""
151140
return_payload = []
152141
for row in api_payload:
153142
return_payload_row = {}
@@ -225,6 +214,7 @@ def serialize_and_write_to_presigned_url(
225214
dataset_id: str,
226215
client,
227216
):
217+
"""This helper function can be used to serialize a list of API objects to NDJSON."""
228218
request_id = uuid.uuid4().hex
229219
response = client.make_request(
230220
payload={},

0 commit comments

Comments
 (0)