Skip to content

Commit 804340e

Browse files
* refactor: further cleanup
1 parent e74b5d8 commit 804340e

File tree

12 files changed

+0
-1238
lines changed

12 files changed

+0
-1238
lines changed

libs/labelbox/src/labelbox/schema/batch.py

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -87,59 +87,6 @@ def remove_queued_data_rows(self) -> None:
8787
},
8888
experimental=True)
8989

90-
def export_data_rows(self,
91-
timeout_seconds=120,
92-
include_metadata: bool = False) -> Generator:
93-
""" Returns a generator that produces all data rows that are currently
94-
in this batch.
95-
96-
Note: For efficiency, the data are cached for 30 minutes. Newly created data rows will not appear
97-
until the end of the cache period.
98-
99-
Args:
100-
timeout_seconds (float): Max waiting time, in seconds.
101-
include_metadata (bool): True to return related DataRow metadata
102-
Returns:
103-
Generator that yields DataRow objects belonging to this batch.
104-
Raises:
105-
LabelboxError: if the export fails or is unable to download within the specified time.
106-
"""
107-
warnings.warn(
108-
"You are currently utilizing exports v1 for this action, which will be deprecated after April 30th, 2024. We recommend transitioning to exports v2. To view export v2 details, visit our docs: https://docs.labelbox.com/reference/label-export",
109-
DeprecationWarning)
110-
111-
id_param = "batchId"
112-
metadata_param = "includeMetadataInput"
113-
query_str = """mutation GetBatchDataRowsExportUrlPyApi($%s: ID!, $%s: Boolean!)
114-
{exportBatchDataRows(data:{batchId: $%s , includeMetadataInput: $%s}) {downloadUrl createdAt status}}
115-
""" % (id_param, metadata_param, id_param, metadata_param)
116-
sleep_time = 2
117-
while True:
118-
res = self.client.execute(query_str, {
119-
id_param: self.uid,
120-
metadata_param: include_metadata
121-
})
122-
res = res["exportBatchDataRows"]
123-
if res["status"] == "COMPLETE":
124-
download_url = res["downloadUrl"]
125-
response = requests.get(download_url)
126-
response.raise_for_status()
127-
reader = parser.reader(StringIO(response.text))
128-
return (
129-
Entity.DataRow(self.client, result) for result in reader)
130-
elif res["status"] == "FAILED":
131-
raise LabelboxError("Data row export failed.")
132-
133-
timeout_seconds -= sleep_time
134-
if timeout_seconds <= 0:
135-
raise LabelboxError(
136-
f"Unable to export data rows within {timeout_seconds} seconds."
137-
)
138-
139-
logger.debug("Batch '%s' data row export, waiting for server...",
140-
self.uid)
141-
time.sleep(sleep_time)
142-
14390
def delete(self) -> None:
14491
""" Deletes the given batch.
14592

libs/labelbox/src/labelbox/schema/dataset.py

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -337,58 +337,6 @@ def data_row_for_external_id(self, external_id) -> "DataRow":
337337
external_id)
338338
return data_rows[0]
339339

340-
def export_data_rows(self,
341-
timeout_seconds=120,
342-
include_metadata: bool = False) -> Generator:
343-
""" Returns a generator that produces all data rows that are currently
344-
attached to this dataset.
345-
346-
Note: For efficiency, the data are cached for 30 minutes. Newly created data rows will not appear
347-
until the end of the cache period.
348-
349-
Args:
350-
timeout_seconds (float): Max waiting time, in seconds.
351-
include_metadata (bool): True to return related DataRow metadata
352-
Returns:
353-
Generator that yields DataRow objects belonging to this dataset.
354-
Raises:
355-
LabelboxError: if the export fails or is unable to download within the specified time.
356-
"""
357-
warnings.warn(
358-
"You are currently utilizing exports v1 for this action, which will be deprecated after April 30th, 2024. We recommend transitioning to exports v2. To view export v2 details, visit our docs: https://docs.labelbox.com/reference/label-export",
359-
DeprecationWarning)
360-
id_param = "datasetId"
361-
metadata_param = "includeMetadataInput"
362-
query_str = """mutation GetDatasetDataRowsExportUrlPyApi($%s: ID!, $%s: Boolean!)
363-
{exportDatasetDataRows(data:{datasetId: $%s , includeMetadataInput: $%s}) {downloadUrl createdAt status}}
364-
""" % (id_param, metadata_param, id_param, metadata_param)
365-
sleep_time = 2
366-
while True:
367-
res = self.client.execute(query_str, {
368-
id_param: self.uid,
369-
metadata_param: include_metadata
370-
})
371-
res = res["exportDatasetDataRows"]
372-
if res["status"] == "COMPLETE":
373-
download_url = res["downloadUrl"]
374-
response = requests.get(download_url)
375-
response.raise_for_status()
376-
reader = parser.reader(StringIO(response.text))
377-
return (
378-
Entity.DataRow(self.client, result) for result in reader)
379-
elif res["status"] == "FAILED":
380-
raise LabelboxError("Data row export failed.")
381-
382-
timeout_seconds -= sleep_time
383-
if timeout_seconds <= 0:
384-
raise LabelboxError(
385-
f"Unable to export data rows within {timeout_seconds} seconds."
386-
)
387-
388-
logger.debug("Dataset '%s' data row export, waiting for server...",
389-
self.uid)
390-
time.sleep(sleep_time)
391-
392340
def export(
393341
self,
394342
task_name: Optional[str] = None,

libs/labelbox/src/labelbox/schema/model_run.py

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -461,57 +461,6 @@ def get_config(self) -> Dict[str, Any]:
461461
experimental=True)
462462
return res["modelRun"]["trainingMetadata"]
463463

464-
@experimental
465-
def export_labels(
466-
self,
467-
download: bool = False,
468-
timeout_seconds: int = 600
469-
) -> Optional[Union[str, List[Dict[Any, Any]]]]:
470-
"""
471-
Experimental. To use, make sure client has enable_experimental=True.
472-
473-
Fetches Labels from the ModelRun
474-
475-
Args:
476-
download (bool): Returns the url if False
477-
Returns:
478-
URL of the data file with this ModelRun's labels.
479-
If download=True, this instead returns the contents as NDJSON format.
480-
If the server didn't generate during the `timeout_seconds` period,
481-
None is returned.
482-
"""
483-
warnings.warn(
484-
"You are currently utilizing exports v1 for this action, which will be deprecated after April 30th, 2024. We recommend transitioning to exports v2. To view export v2 details, visit our docs: https://docs.labelbox.com/reference/label-export",
485-
DeprecationWarning)
486-
sleep_time = 2
487-
query_str = """mutation exportModelRunAnnotationsPyApi($modelRunId: ID!) {
488-
exportModelRunAnnotations(data: {modelRunId: $modelRunId}) {
489-
downloadUrl createdAt status
490-
}
491-
}
492-
"""
493-
494-
while True:
495-
url = self.client.execute(
496-
query_str, {'modelRunId': self.uid},
497-
experimental=True)['exportModelRunAnnotations']['downloadUrl']
498-
499-
if url:
500-
if not download:
501-
return url
502-
else:
503-
response = requests.get(url)
504-
response.raise_for_status()
505-
return parser.loads(response.content)
506-
507-
timeout_seconds -= sleep_time
508-
if timeout_seconds <= 0:
509-
return None
510-
511-
logger.debug("ModelRun '%s' label export, waiting for server...",
512-
self.uid)
513-
time.sleep(sleep_time)
514-
515464
def export(self,
516465
task_name: Optional[str] = None,
517466
params: Optional[ModelRunExportParams] = None) -> ExportTask:

libs/labelbox/src/labelbox/schema/project.py

Lines changed: 0 additions & 205 deletions
Original file line numberDiff line numberDiff line change
@@ -317,203 +317,6 @@ def labels(self, datasets=None, order_by=None) -> PaginatedCollection:
317317
return PaginatedCollection(self.client, query_str, {id_param: self.uid},
318318
["project", "labels"], Label)
319319

320-
def export_queued_data_rows(
321-
self,
322-
timeout_seconds=120,
323-
include_metadata: bool = False) -> List[Dict[str, str]]:
324-
""" Returns all data rows that are currently enqueued for this project.
325-
326-
Args:
327-
timeout_seconds (float): Max waiting time, in seconds.
328-
include_metadata (bool): True to return related DataRow metadata
329-
Returns:
330-
Data row fields for all data rows in the queue as json
331-
Raises:
332-
LabelboxError: if the export fails or is unable to download within the specified time.
333-
"""
334-
warnings.warn(
335-
"You are currently utilizing exports v1 for this action, which will be deprecated after April 30th, 2024. We recommend transitioning to exports v2. To view export v2 details, visit our docs: https://docs.labelbox.com/reference/label-export",
336-
DeprecationWarning)
337-
id_param = "projectId"
338-
metadata_param = "includeMetadataInput"
339-
query_str = """mutation GetQueuedDataRowsExportUrlPyApi($%s: ID!, $%s: Boolean!)
340-
{exportQueuedDataRows(data:{projectId: $%s , includeMetadataInput: $%s}) {downloadUrl createdAt status} }
341-
""" % (id_param, metadata_param, id_param, metadata_param)
342-
sleep_time = 2
343-
start_time = time.time()
344-
while True:
345-
res = self.client.execute(query_str, {
346-
id_param: self.uid,
347-
metadata_param: include_metadata
348-
})
349-
res = res["exportQueuedDataRows"]
350-
if res["status"] == "COMPLETE":
351-
download_url = res["downloadUrl"]
352-
response = requests.get(download_url)
353-
response.raise_for_status()
354-
return parser.loads(response.text)
355-
elif res["status"] == "FAILED":
356-
raise LabelboxError("Data row export failed.")
357-
358-
current_time = time.time()
359-
if current_time - start_time > timeout_seconds:
360-
raise LabelboxError(
361-
f"Unable to export data rows within {timeout_seconds} seconds."
362-
)
363-
364-
logger.debug(
365-
"Project '%s' queued data row export, waiting for server...",
366-
self.uid)
367-
time.sleep(sleep_time)
368-
369-
def label_generator(self, timeout_seconds=600, **kwargs):
370-
"""
371-
Download text and image annotations, or video annotations.
372-
373-
For a mixture of text/image and video, use project.export_labels()
374-
375-
Returns:
376-
LabelGenerator for accessing labels
377-
"""
378-
_check_converter_import()
379-
json_data = self.export_labels(download=True,
380-
timeout_seconds=timeout_seconds,
381-
**kwargs)
382-
383-
# assert that the instance this would fail is only if timeout runs out
384-
assert isinstance(
385-
json_data,
386-
List), "Unable to successfully get labels. Please try again"
387-
388-
if json_data is None:
389-
raise TimeoutError(
390-
f"Unable to download labels in {timeout_seconds} seconds."
391-
"Please try again or contact support if the issue persists.")
392-
393-
is_video = [
394-
"frames" in row["Label"]
395-
for row in json_data
396-
if row["Label"] and not row["Skipped"]
397-
]
398-
399-
if len(is_video) and not all(is_video) and any(is_video):
400-
raise ValueError(
401-
"Found mixed data types of video and text/image. "
402-
"Use project.export_labels() to export projects with mixed data types. "
403-
)
404-
if len(is_video) and all(is_video):
405-
# Filter skipped labels to avoid inference errors
406-
json_data = [
407-
label for label in self.export_labels(download=True)
408-
if not label["Skipped"]
409-
]
410-
411-
return LBV1Converter.deserialize_video(json_data, self.client)
412-
413-
return LBV1Converter.deserialize(json_data)
414-
415-
def export_labels(self,
416-
download=False,
417-
timeout_seconds=1800,
418-
**kwargs) -> Optional[Union[str, List[Dict[Any, Any]]]]:
419-
""" Calls the server-side Label exporting that generates a JSON
420-
payload, and returns the URL to that payload.
421-
422-
Will only generate a new URL at a max frequency of 30 min.
423-
424-
Args:
425-
download (bool): Returns the url if False
426-
timeout_seconds (float): Max waiting time, in seconds.
427-
start (str): Earliest date for labels, formatted "YYYY-MM-DD" or "YYYY-MM-DD hh:mm:ss"
428-
end (str): Latest date for labels, formatted "YYYY-MM-DD" or "YYYY-MM-DD hh:mm:ss"
429-
last_activity_start (str): Will include all labels that have had any updates to
430-
data rows, issues, comments, metadata, or reviews since this timestamp.
431-
formatted "YYYY-MM-DD" or "YYYY-MM-DD hh:mm:ss"
432-
last_activity_end (str): Will include all labels that do not have any updates to
433-
data rows, issues, comments, metadata, or reviews after this timestamp.
434-
formatted "YYYY-MM-DD" or "YYYY-MM-DD hh:mm:ss"
435-
436-
Returns:
437-
URL of the data file with this Project's labels. If the server didn't
438-
generate during the `timeout_seconds` period, None is returned.
439-
"""
440-
warnings.warn(
441-
"You are currently utilizing exports v1 for this action, which will be deprecated after April 30th, 2024. We recommend transitioning to exports v2. To view export v2 details, visit our docs: https://docs.labelbox.com/reference/label-export",
442-
DeprecationWarning)
443-
444-
def _string_from_dict(dictionary: dict, value_with_quotes=False) -> str:
445-
"""Returns a concatenated string of the dictionary's keys and values
446-
447-
The string will be formatted as {key}: 'value' for each key. Value will be inclusive of
448-
quotations while key will not. This can be toggled with `value_with_quotes`"""
449-
450-
quote = "\"" if value_with_quotes else ""
451-
return ",".join([
452-
f"""{c}: {quote}{dictionary.get(c)}{quote}"""
453-
for c in dictionary
454-
if dictionary.get(c)
455-
])
456-
457-
sleep_time = 2
458-
id_param = "projectId"
459-
filter_param = ""
460-
filter_param_dict = {}
461-
462-
if "start" in kwargs or "end" in kwargs:
463-
created_at_dict = {
464-
"start": kwargs.get("start", ""),
465-
"end": kwargs.get("end", "")
466-
}
467-
[validate_datetime(date) for date in created_at_dict.values()]
468-
filter_param_dict["labelCreatedAt"] = "{%s}" % _string_from_dict(
469-
created_at_dict, value_with_quotes=True)
470-
471-
if "last_activity_start" in kwargs or "last_activity_end" in kwargs:
472-
last_activity_start = kwargs.get('last_activity_start')
473-
last_activity_end = kwargs.get('last_activity_end')
474-
475-
if last_activity_start:
476-
validate_datetime(str(last_activity_start))
477-
if last_activity_end:
478-
validate_datetime(str(last_activity_end))
479-
480-
filter_param_dict["lastActivityAt"] = "{%s}" % _string_from_dict(
481-
{
482-
"start": last_activity_start,
483-
"end": last_activity_end
484-
},
485-
value_with_quotes=True)
486-
487-
if filter_param_dict:
488-
filter_param = """, filters: {%s }""" % (_string_from_dict(
489-
filter_param_dict, value_with_quotes=False))
490-
491-
query_str = """mutation GetLabelExportUrlPyApi($%s: ID!)
492-
{exportLabels(data:{projectId: $%s%s}) {downloadUrl createdAt shouldPoll} }
493-
""" % (id_param, id_param, filter_param)
494-
495-
start_time = time.time()
496-
497-
while True:
498-
res = self.client.execute(query_str, {id_param: self.uid})
499-
res = res["exportLabels"]
500-
if not res["shouldPoll"] and res["downloadUrl"] is not None:
501-
url = res['downloadUrl']
502-
if not download:
503-
return url
504-
else:
505-
response = requests.get(url)
506-
response.raise_for_status()
507-
return response.json()
508-
509-
current_time = time.time()
510-
if current_time - start_time > timeout_seconds:
511-
return None
512-
513-
logger.debug("Project '%s' label export, waiting for server...",
514-
self.uid)
515-
time.sleep(sleep_time)
516-
517320
def export(
518321
self,
519322
task_name: Optional[str] = None,
@@ -1994,11 +1797,3 @@ class LabelingParameterOverride(DbObject):
19941797
"consensus average_benchmark_agreement last_activity_time")
19951798
LabelerPerformance.__doc__ = (
19961799
"Named tuple containing info about a labeler's performance.")
1997-
1998-
1999-
def _check_converter_import():
2000-
if 'LBV1Converter' not in globals():
2001-
raise ImportError(
2002-
"Missing dependencies to import converter. "
2003-
"Use `pip install labelbox[data] --upgrade` to add missing dependencies. "
2004-
"or download raw json with project.export_labels()")

0 commit comments

Comments
 (0)