22import json
33import logging
44import time
5+ import warnings
56from collections import namedtuple
67from datetime import datetime , timezone
78from pathlib import Path
3738logger = logging .getLogger (__name__ )
3839
3940
40- class QueueMode (enum .Enum ):
41- Batch = "Batch"
42- Dataset = "Dataset"
43-
44-
4541class Project (DbObject , Updateable , Deletable ):
4642 """ A Project is a container that includes a labeling frontend, an ontology,
4743 datasets and labels.
@@ -89,9 +85,12 @@ class Project(DbObject, Updateable, Deletable):
8985 benchmarks = Relationship .ToMany ("Benchmark" , False )
9086 ontology = Relationship .ToOne ("Ontology" , True )
9187
92- def update (self , ** kwargs ):
88+ class QueueMode (enum .Enum ):
89+ Batch = "Batch"
90+ Dataset = "Dataset"
9391
94- mode : Optional [QueueMode ] = kwargs .pop ("queue_mode" , None )
92+ def update (self , ** kwargs ):
93+ mode : Optional [Project .QueueMode ] = kwargs .pop ("queue_mode" , None )
9594 if mode :
9695 self ._update_queue_mode (mode )
9796
@@ -569,14 +568,69 @@ def setup(self, labeling_frontend, labeling_frontend_options) -> None:
569568 timestamp = datetime .now (timezone .utc ).strftime ("%Y-%m-%dT%H:%M:%SZ" )
570569 self .update (setup_complete = timestamp )
571570
572- def _update_queue_mode (self , mode : QueueMode ) -> QueueMode :
571+ def create_batch (self , name : str , data_rows : List [str ], priority : int = 5 ):
572+ """Create a new batch for a project. Batches is in Beta and subject to change
573+
574+ Args:
575+ name: a name for the batch, must be unique within a project
576+ data_rows: Either a list of `DataRows` or Data Row ids
577+ priority: An optional priority for the Data Rows in the Batch. 1 highest -> 5 lowest
578+
579+ """
580+
581+ # @TODO: make this automatic?
582+ if self .queue_mode () != Project .QueueMode .Batch :
583+ raise ValueError ("Project must be in batch mode" )
584+
585+ dr_ids = []
586+ for dr in data_rows :
587+ if isinstance (dr , Entity .DataRow ):
588+ dr_ids .append (dr .uid )
589+ elif isinstance (dr , str ):
590+ dr_ids .append (dr )
591+ else :
592+ raise ValueError ("You can DataRow ids or DataRow objects" )
593+
594+ if len (dr_ids ) > 25_000 :
595+ raise ValueError (
596+ f"Batch exceeds max size, break into smaller batches" )
597+ if not len (dr_ids ):
598+ raise ValueError ("You need at least one data row in a batch" )
599+
600+ method = 'createBatch'
601+ query_str = """mutation %sPyApi($projectId: ID!, $batchInput: CreateBatchInput!) {
602+ project(where: {id: $projectId}) {
603+ %s(input: $batchInput) {
604+ %s
605+ }
606+ }
607+ }
608+ """ % (method , method , query .results_query_part (Entity .Batch ))
609+
610+ params = {
611+ "projectId" : self .uid ,
612+ "batchInput" : {
613+ "name" : name ,
614+ "dataRowIds" : dr_ids ,
615+ "priority" : priority
616+ }
617+ }
618+
619+ res = self .client .execute (query_str , params ,
620+ experimental = True )["project" ][method ]
621+
622+ res ['size' ] = len (dr_ids )
623+ return Entity .Batch (self .client , res )
624+
625+ def _update_queue_mode (self ,
626+ mode : "Project.QueueMode" ) -> "Project.QueueMode" :
573627
574628 if self .queue_mode () == mode :
575629 return mode
576630
577- if mode == QueueMode .Batch :
631+ if mode == Project . QueueMode .Batch :
578632 status = "ENABLED"
579- elif mode == QueueMode .Dataset :
633+ elif mode == Project . QueueMode .Dataset :
580634 status = "DISABLED"
581635 else :
582636 raise ValueError (
@@ -598,7 +652,7 @@ def _update_queue_mode(self, mode: QueueMode) -> QueueMode:
598652
599653 return mode
600654
601- def queue_mode (self ) -> QueueMode :
655+ def queue_mode (self ) -> "Project. QueueMode" :
602656 """Provides the status of if queue mode is enabled in the project."""
603657
604658 query_str = """query %s($projectId: ID!) {
@@ -612,9 +666,9 @@ def queue_mode(self) -> QueueMode:
612666 query_str , {'projectId' : self .uid })["project" ]["tagSetStatus" ]
613667
614668 if status == "ENABLED" :
615- return QueueMode .Batch
669+ return Project . QueueMode .Batch
616670 elif status == "DISABLED" :
617- return QueueMode .Dataset
671+ return Project . QueueMode .Dataset
618672 else :
619673 raise ValueError ("Status not known" )
620674
0 commit comments