-
Notifications
You must be signed in to change notification settings - Fork 68
[AL-2075] Batch list and export #551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
eb7cff0
[AL-2075] Batch list and export
farkob 7dee8ef
format
farkob 658c618
remove archived_at
farkob a61e64d
add archive, batch.project, changelog
farkob 83bfc53
types
farkob 2eca4cb
types
farkob 398d16e
add project type
farkob 2f62897
fix order
farkob 2ca4d86
ignore type
farkob 3416746
add project.batches() test
farkob 29f7ca0
update test order
farkob fcd6af3
add changes
farkob 386e163
updated
farkob File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name = "labelbox" | ||
__version__ = "3.19.1" | ||
__version__ = "3.20.0" | ||
|
||
import sys | ||
import warnings | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,18 @@ | ||
from labelbox.orm.db_object import DbObject | ||
from labelbox.orm.model import Field, Relationship | ||
from typing import Generator, TYPE_CHECKING | ||
from labelbox.orm.db_object import DbObject, experimental | ||
from labelbox.orm import query | ||
from labelbox.orm.model import Entity, Field, Relationship | ||
from labelbox.exceptions import LabelboxError, ResourceNotFoundError | ||
from io import StringIO | ||
import ndjson | ||
import requests | ||
import logging | ||
import time | ||
|
||
if TYPE_CHECKING: | ||
from labelbox import Project | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Batch(DbObject): | ||
|
@@ -21,5 +34,87 @@ class Batch(DbObject): | |
size = Field.Int("size") | ||
|
||
# Relationships | ||
project = Relationship.ToOne("Project") | ||
created_by = Relationship.ToOne("User") | ||
|
||
def __init__(self, client, project_id, *args, **kwargs): | ||
super().__init__(client, *args, **kwargs) | ||
self.project_id = project_id | ||
|
||
def project(self) -> 'Project': # type: ignore | ||
""" Returns Project which this Batch belongs to | ||
|
||
Raises: | ||
LabelboxError: if the project is not found | ||
""" | ||
query_str = """query getProjectPyApi($projectId: ID!) { | ||
project( | ||
where: {id: $projectId}){ | ||
%s | ||
}}""" % query.results_query_part(Entity.Project) | ||
params = {"projectId": self.project_id} | ||
response = self.client.execute(query_str, params) | ||
|
||
if response is None: | ||
raise ResourceNotFoundError(Entity.Project, params) | ||
|
||
return Entity.Project(self.client, response["project"]) | ||
|
||
def remove_queued_data_rows(self) -> None: | ||
""" Removes remaining queued data rows from the batch and labeling queue. | ||
|
||
Args: | ||
batch (Batch): Batch to remove queued data rows from | ||
""" | ||
|
||
project_id_param = "projectId" | ||
batch_id_param = "batchId" | ||
self.client.execute("""mutation ArchiveBatchPyApi($%s: ID!, $%s: ID!) { | ||
project(where: {id: $%s}) { archiveBatch(batchId: $%s) { id archivedAt } } | ||
}""" % (project_id_param, batch_id_param, project_id_param, | ||
batch_id_param), { | ||
project_id_param: self.project_id, | ||
batch_id_param: self.uid | ||
}, | ||
experimental=True) | ||
|
||
def export_data_rows(self, timeout_seconds=120) -> Generator: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this the same output structure as dataset? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes |
||
""" Returns a generator that produces all data rows that are currently | ||
in this batch. | ||
|
||
Note: For efficiency, the data are cached for 30 minutes. Newly created data rows will not appear | ||
until the end of the cache period. | ||
|
||
Args: | ||
timeout_seconds (float): Max waiting time, in seconds. | ||
Returns: | ||
Generator that yields DataRow objects belonging to this batch. | ||
Raises: | ||
LabelboxError: if the export fails or is unable to download within the specified time. | ||
""" | ||
id_param = "batchId" | ||
query_str = """mutation GetBatchDataRowsExportUrlPyApi($%s: ID!) | ||
{exportBatchDataRows(data:{batchId: $%s }) {downloadUrl createdAt status}} | ||
""" % (id_param, id_param) | ||
sleep_time = 2 | ||
while True: | ||
res = self.client.execute(query_str, {id_param: self.uid}) | ||
res = res["exportBatchDataRows"] | ||
if res["status"] == "COMPLETE": | ||
download_url = res["downloadUrl"] | ||
response = requests.get(download_url) | ||
response.raise_for_status() | ||
reader = ndjson.reader(StringIO(response.text)) | ||
return ( | ||
Entity.DataRow(self.client, result) for result in reader) | ||
elif res["status"] == "FAILED": | ||
raise LabelboxError("Data row export failed.") | ||
|
||
timeout_seconds -= sleep_time | ||
if timeout_seconds <= 0: | ||
raise LabelboxError( | ||
f"Unable to export data rows within {timeout_seconds} seconds." | ||
) | ||
|
||
logger.debug("Batch '%s' data row export, waiting for server...", | ||
self.uid) | ||
time.sleep(sleep_time) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is going to be a new version of the SDK, can we also include information on this merged PR?
#540
Updated
NDJsonConverter
now supports Video bounding box annotations.cc @msokoloff1 if this wording looks okay
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup looks good. Thanks