2
2
import json
3
3
import logging
4
4
import time
5
+ import warnings
5
6
from collections import namedtuple
6
7
from datetime import datetime , timezone
7
8
from pathlib import Path
37
38
logger = logging .getLogger (__name__ )
38
39
39
40
40
- class QueueMode (enum .Enum ):
41
- Batch = "Batch"
42
- Dataset = "Dataset"
43
-
44
-
45
41
class Project (DbObject , Updateable , Deletable ):
46
42
""" A Project is a container that includes a labeling frontend, an ontology,
47
43
datasets and labels.
@@ -89,9 +85,12 @@ class Project(DbObject, Updateable, Deletable):
89
85
benchmarks = Relationship .ToMany ("Benchmark" , False )
90
86
ontology = Relationship .ToOne ("Ontology" , True )
91
87
92
- def update (self , ** kwargs ):
88
+ class QueueMode (enum .Enum ):
89
+ Batch = "Batch"
90
+ Dataset = "Dataset"
93
91
94
- mode : Optional [QueueMode ] = kwargs .pop ("queue_mode" , None )
92
+ def update (self , ** kwargs ):
93
+ mode : Optional [Project .QueueMode ] = kwargs .pop ("queue_mode" , None )
95
94
if mode :
96
95
self ._update_queue_mode (mode )
97
96
@@ -569,14 +568,69 @@ def setup(self, labeling_frontend, labeling_frontend_options) -> None:
569
568
timestamp = datetime .now (timezone .utc ).strftime ("%Y-%m-%dT%H:%M:%SZ" )
570
569
self .update (setup_complete = timestamp )
571
570
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" :
573
627
574
628
if self .queue_mode () == mode :
575
629
return mode
576
630
577
- if mode == QueueMode .Batch :
631
+ if mode == Project . QueueMode .Batch :
578
632
status = "ENABLED"
579
- elif mode == QueueMode .Dataset :
633
+ elif mode == Project . QueueMode .Dataset :
580
634
status = "DISABLED"
581
635
else :
582
636
raise ValueError (
@@ -598,7 +652,7 @@ def _update_queue_mode(self, mode: QueueMode) -> QueueMode:
598
652
599
653
return mode
600
654
601
- def queue_mode (self ) -> QueueMode :
655
+ def queue_mode (self ) -> "Project. QueueMode" :
602
656
"""Provides the status of if queue mode is enabled in the project."""
603
657
604
658
query_str = """query %s($projectId: ID!) {
@@ -612,9 +666,9 @@ def queue_mode(self) -> QueueMode:
612
666
query_str , {'projectId' : self .uid })["project" ]["tagSetStatus" ]
613
667
614
668
if status == "ENABLED" :
615
- return QueueMode .Batch
669
+ return Project . QueueMode .Batch
616
670
elif status == "DISABLED" :
617
- return QueueMode .Dataset
671
+ return Project . QueueMode .Dataset
618
672
else :
619
673
raise ValueError ("Status not known" )
620
674
0 commit comments