
    	]j                     
   d Z ddlZddlZddlmZ ddlZddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZmZ ddlmZmZ ddlmZ ddlmZmZm Z m!Z!m"Z"m#Z#  e$ddejJ                  d      Z&ejN                  jQ                  de&ddddiddddigdddife&dddddigdd gd!ddddigdddife&dddddigdd gd!ddddd"igdd gd!gd#d$d%hife&dddddigdd gd!ddddd"igdd gd!gd#ddif e$ddejJ                  d&      dd'dd(idd'dd(igdddifg      ejN                  jR                  d)               Z*ejN                  jQ                  d* e$ddejJ                  d+d,-      dd'dd(idd'dd(igd.d/igd0dd1d.d2igd3dd1gddgddd.d2igf e$ddejJ                  d+d4-      dd'dd(idd'dd(igd.d/igd3dd1d.d2igd0dd1gddgddd.d/igf e$ddejJ                  dd+d45      dd'dd(idd'dd(idd'd6d(igd.d/igd3dd1d.d2igd0dd1d.d7igd8dd1gd9d.d/igiddgddd.d7igf e$ddejJ                  d+d4-      dd'dd(idd'd:d(idd'dd(idd'd6d(igd.d/igd3dd1d.d/igd;dd1d.d2igd0dd1d.d7igd8dd1gd9d.d/igidddgddd.d7igf e$ddejJ                  d+d4-      dd'dd(idd'd:d(idd'd<d(idd'dd(idd'd6d(idd'd=d(igd.d/igd3dd1d.d/igd>dd1d.d?igd;dd1d.d2igd0dd1d.d7igd8dd1d.d7igd8dd1gd9d.d/igidddd9d.d7igid9d.d7igigddd.d?igf e$ddejJ                  d+d,-      dd'dd(idd'dd(idd'd6d(igd.d/igd3dd1d.d2igd0dd1d.d7igd8dd1gd9d.d/igiddgddd.d7igf e$ddejJ                  d+d4-      dd'dd(idd'dd(idd'd6d(igd.d/igd3dd1d.d2igd0dd1d.d7igd8dd1gd9d.d/igiddgddd.d7igf e$ddejJ                  d+d"d4@      dd'dd(idd'dd(idd'd6d(idd'dAd(idd'dBd(igd.d/igdCdd1d.d2igd3dd1d.d7igd8dd1d.dDigdEdd1d.dFigd>dd1gd9d.d/igiddddgd"dd.d/igfgg dGH      ejN                  jR                  dI               Z+ejN                  jR                  dJ        Z,ejN                  j[                   e        dKL      ejN                  jQ                  dMej\                  ejJ                  ej^                  f      ejN                  jR                  dN                      Z0ejN                  j[                   e        dKL      ejN                  jQ                  dMej\                  ejJ                  ej^                  f      ejN                  jR                  dO                      Z1ejN                  jR                  dP        Z2ejN                  jR                  dQ        Z3ejN                  j[                   e        dKL      ejN                  jR                  dR               Z4ejN                  j[                   e        dKL      ejN                  jR                  dS               Z5ejN                  j[                   e        dKL      ejN                  jR                  dT               Z6ejN                  j[                   e        dKL      ejN                  jR                  dU               Z7ejN                  jR                  dV        Z8ejN                  j[                   e        dKL      ejN                  jR                  dW               Z9ejN                  jQ                  dXdY      ejN                  jQ                  dZdY      ejN                  jR                  d[                      Z: G d\ d]e      Z;y)^zThis file and its contents are licensed under the Apache License 2.0. Please see the included NOTICE for copyright information and LICENSE for a copy of the license.
    N)mock)redis_healthcheck)apps)Q)TestCase)_try_breadth_first)Project)ProjectFactory)
Annotation
PredictionTask)AnnotationFactoryTaskFactory)UserFactory   )_client_is_annotatorinvite_client_to_projectmake_annotationmake_annotatormake_project	make_taskTestTa6  
        <View>
          <Text name="meta_info" value="$meta_info"></Text>
          <Text name="text" value=" $text "></Text>
          <Choices name="text_class" choice="single">
            <Choice value="class_A"></Choice>
            <Choice value="class_B"></Choice>
          </Choices>
        </View>titleis_publishedsamplinglabel_configz?project_config, tasks, status_code, expected_response_value_setdatazmeta info Aztext A)	meta_infotextzmeta info Bztext B   iduncompleted_task_idsrF)resultground_truth)r   annotations     detailz
Not found.a8  
                <View>
                  <Text name="location" value="$location"></Text>
                  <Choices name="text_class" choice="single">
                    <Choice value="class_A"></Choice>
                    <Choice value="class_B"></Choice>
                  </Choices>
                </View>London)locationr    c           	      l   t        || j                        }t        |      rt        ||       | j	                  d|j
                   dt        j                  |      d      }|j                  dk(  sJ t        j                  j                         j                  |j                         t               }t        j                  j                         D ]H  }	|	j                   j#                  d      j%                         r.|j'                  |	j
                         J |j)                  d|j
                   d	      }|j                  |k(  sJ t        j*                  |j,                        }
|j                  d
k7  r8|j/                         D ]$  \  }}|dk(  r|}|
|   |v rJ d|
 d| d| d        y y )N/api/projects/z/tasks/bulkapplication/jsonr   content_type   )completed_byF)r&   /nextr)   r#   zFailed on response z: expecting value set "z" for key "")r   userr   r   postr"   jsondumpsstatus_coder   objectsallupdate	annotatorsetr   r'   filterexistsaddgetloadscontentitems)business_client
any_clientproject_configtasksr:   expected_response_value_setprojectr$   r#   trdataresponse_keyexpected_value_sets                K/root/env/lib/python3.12/site-packages/label_studio/tests/test_next_task.pytest_next_taskrR   ,   s   Z >?+?+?@GJ' W5 	
K0tzz%7HWi 	 	A ==C ##1E1E#F 5\\ +}}###7>>@ $$QTT*+ 	

|59:A==K'''JJqyy!E}}0K0Q0Q0S 	r,L,!%;;%9"l#'99r$UG+BCUBVVabnaoopqr9		r     z`project_config, tasks, predictions, annotations, num_annotators, status_code, prelabeling_result12345z
    <View>
      <Text name="location" value="$location"></Text>
      <Choices name="text_class" choice="single">
        <Choice value="class_A"></Choice>
        <Choice value="class_B"></Choice>
      </Choices>
    </View>)r   r   r   model_versionr   somezprediction Ag?)r%   scoreclusterzprediction B      ?z
<View>
  <Text name="location" value="$location"></Text>
  <Choices name="text_class" choice="single">
    <Choice value="class_A"></Choice>
    <Choice value="class_B"></Choice>
  </Choices>
</View>)r   r   r   maximum_annotationsrU   r   ztext Czprediction Cg?r%   ztext A 2皙?ztext A 3ztext C 2皙?zprediction A1)r   r   r   rU   rZ   r   ztext Dztext Eg333333?zprediction D皙?zprediction E)z<no annotations, second task is chosen due to active learningz;no annotations, first task is chosen due to active learningzBfirst task annotation, third task is chosen due to active learningzfirst task annotation, forth task is chosen due to active learning (though task with lowest score exists but in the same cluster)z5lowest prediction is chosen from least solved clusterzBfirst task annotation, labeling is continued with the same clusterz[first task annotation, third task is chosen since cluster is marked as None (no clustering)zwhen some of the tasks are partially labeled, regardless scores sampling operates on depth-first (try to complete all tasks asap))idsc                    t        ||j                  d      }t        |      rt        ||       t        |      rt        ||        G fdd      }| j                  j                  t        d |              t        |||      D ]  \  }}}t        ||      }t        j                  j                  d||j                  |j                  d| |Mdk(  r|j                  n|j                  }t        j                  j                  d|||d	|  |j!                  d
|j"                   d      }|j$                  |	k(  sJ t'        j(                  |j*                        }|j$                  dk(  r|d   d   d   |
k(  sJ y y )NFuse_ml_backendc                       e Zd Z fdZy)?test_next_task_with_active_learning.<locals>.MockAnnotatorCountc                     S N )selfnum_annotatorss    rQ   countzEtest_next_task_with_active_learning.<locals>.MockAnnotatorCount.count  s	    !!rS   N)__name__
__module____qualname__ri   )rh   s   rQ   MockAnnotatorCountrc     s    	"rS   rm   
annotators)return_value)taskrL   rU   r   )rp   r3   rL   r.   r4   r!   predictionsr   r%   rf   )r   r6   r   r   patchobjectr	   zipr   r   r;   createrL   rU   r>   r   rC   r"   r:   r8   rD   rE   )mockerrG   rH   annotator2_clientrI   rJ   rq   r'   rh   r:   prelabeling_resultrL   rm   rp   
prediction
annotationr3   r$   rN   s           `          rQ   #test_next_task_with_active_learningr{      sv   p	 >?+?+?PUVGJ' W5-. !2G<" " LL<N<PQ(+E;(L k$j*w'!!utT\\QXQfQfujtu!3AQ3F://L]LgLgL%%j4lT[j_ijk 	

|59:A==K'''JJqyy!E}}]#A&x04FFFF rS   c                 &    t        ddt        j                  d      }t        | j                  d      ddd	d	d
gidgddiddgdddiddgdddiddgdddiddgdddiddgdg} j                  dj                   dt        j                  |      d      }|j                  dk(  sJ  fd}j                  dk(  sJ  |       dk(  sJ  |       dk(  sJ  |       dk(  sJ  |       dk(  sJ  |       dk(  sJ y ) Nr   Ta*  
            <View>
              <Text name="location" value="$text"></Text>
              <Choices name="text_class" toName="location" choice="single">
                <Choice value="class_A"></Choice>
                <Choice value="class_B"></Choice>
              </Choices>
            </View>r   Fr`   
text_classr,   choicesclass_A	from_nameto_nametypevaluer    zscore = 0.5rY   )r%   rW   )r   rq   zscore = 0.1r[   zscore = 0.3g333333?zscore = 0.2r\   zscore = 0.4r]   r.   /tasks/bulk/r/   r0   r2   c                  :   j                  dj                   d      } | j                  dk(  sJ t        j                  | j
                        }j                  d|d    d|d   t        j                        d      } | j                  d	k(  sJ |d
   d   S )Nr.   r4   r!   /api/tasks/r"   /annotations/rp   r%   r   r2   r   r    )rC   r"   r:   r8   rD   rE   r7   r9   )r$   rp   rG   rL   r%   s     rQ    get_next_task_id_and_complete_itzXtest_active_learning_with_uploaded_predictions.<locals>.get_next_task_id_and_complete_it  s    .E BC}}###zz!))$   $t*]34:Y]YcYcdjYk:l ! 
 }}###F|F##rS    )dictr	   UNCERTAINTYr   r6   r7   r"   r8   r9   r:   rU   )rG   configrJ   r$   r   rL   r%   s   `    @@rQ   .test_active_learning_with_uploaded_predictionsr     s   $$	F 6?#7#7NG(Z^gjsit]uvwF-(VVY:Z9[\-(VVY:Z9[\-(VVY:Z9[\-(VVY:Z9[\-(VVY:Z9[\E 	
L1

58IXj 	 	A ==C
$   B&&& ,->>>+->>>+->>>+->>>+->>>rS   z2Multi user locks only supported with redis enabled)reasonr   c                    t        ddd      }t        ||j                        }||_        |j	                          t        dddii|      j                  }t        dddii|      j                  }t        d	d
i|d      }t        d	di|d      }|j                  d|j                   d      }	|	j                  dk(  sJ t        j                  |	j                        d   }
|j                  t        j                  t        j                  fv r|
||fv sJ t!        ||h|
hz
        d   }n|
|k(  sJ |j                  d|j                   d      }	|	j                  dk(  sJ t        j                  |	j                        d   |k(  sJ y )Ntest_label_racesT  
            <View>
              <Text name="text" value="$text"></Text>
              <Choices name="text_class" choice="single">
                <Choice value="class_A"></Choice>
                <Choice value="class_B"></Choice>
              </Choices>
            </View>r   r   r   r   r    aaabbbemailzann1@testlabelraces.comzann2@testlabelraces.comr.   r4   r!   r"   r   r   r   r6   r   saver   r"   r   rC   r:   r8   rD   rE   r	   UNIFORMr   listconfigured_projectrG   r   r   rL   id1id2ann1ann2r$   selected_ids              rQ   r   r     s     F 6?#7#78GGLLN
Vfe_-w
7
:
:C
Vfe_-w
7
:
:C7$=>ND7$=>ND 	>'**U34A==C**QYY'-KGOOW-@-@AAsCj(((C:-.q1c!!! 	>'**U34A==C::aii &#---rS   c                 p   t        ddd      }t        ||j                        }||_        |j	                          t        dddii|      j                  }t        dddii|      j                  }t        d	d
i|d      }t        d	di|d      }|j                  d|j                   d      }	|	j                  dk(  sJ t        j                  |	j                        d   }
|j                  t        j                  t        j                  fv r|
||fv sJ t!        ||h|
hz
        d   }n|
|k(  sJ |
}|j                  d|j                   d      }	|	j                  dk(  sJ t        j                  |	j                        d   |k(  sJ |j                  d|j                   d      }	|	j                  dk(  sJ t        j                  |	j                        d   |k(  sJ |j                  d|j                   d      }	|	j                  dk(  sJ t        j                  |	j                        d   |k(  sJ y )Nr   Tr   r   r   r    r   r   r   zann1@testlabelracesalltaken.comzann2@testlabelracesalltaken.comr.   r4   r!   r"   r   r   r   s              rQ    test_label_races_after_all_takenr   E  s-     F 6?#7#78GGLLN
Vfe_-w
7
:
:C
Vfe_-w
7
:
:C7$EFQUVD7$EFQUVD 	>'**U34A==C**QYY'-KGOOW-@-@AAsCj(((C:-.q1c!!!
C 	>'**U34A==C::aii &#--- 	>'**U34A==C::aii &#--- 	>'**U34A==C::aii &#---rS   c                    t        dddd      }t        j                  ddddd	gid
g      }t        || j                        }t
        j                  |_        |j                          t        dddii|      j                  }t        dddii|      j                  }t        ddi|d      }t        ddi|d      }|j                  d|j                   d      }|j                  dk(  sJ t        j                  |j                        d   |k(  sJ |j!                  d| d||d      }|j                  dk(  sJ |j                  d|j                   d      }|j                  dk(  sJ t        j                  |j                        d   |k(  sJ |j!                  d| d||d      }|j                  dk(  sJ t"        j$                  j                  |      }	|	j&                  sJ t)        j*                  d      r|	j,                  dk(  sJ |j                  d|j                   d      }|j                  dk(  sJ t        j                  |j                        d   |k(  sJ y )Nr   Tr(   "  
            <View>
              <Text name="text" value="$text"></Text>
              <Choices name="text_class" choice="single" toName="text">
                <Choice value="class_A"></Choice>
                <Choice value="class_B"></Choice>
              </Choices>
            </View>r   r   rZ   r   r}   r    r~   r   r   r   r   r   r   zann1@testbreadthfirst.comzann2@testbreadthfirst.comr.   r4   r!   r"   r   r   r   r   r2   r"   
businesses      ?r   r8   r9   r   r6   r	   SEQUENCEr   r   r   r"   r   rC   r:   rD   rE   r7   r   r;   
is_labeledr   is_installedaccuracy)
rG   r   annotation_resultrL   r   r   r   r   r$   completed_tasks
             rQ   test_breadth_first_simpler   x  sl    	F 

#	U^aj`kTl	mn 6?#7#78G''GLLN
Vfe_-w
7
:
:C
Vfe_-w
7
:
:C7$?@'4PD7$?@'4PD 	>'**U34A==C::aii &#--- 			KuM2#Qb9c	dA==C 	>'**U34A==C::aii &#--- 			KuM2#Qb9c	dA==C\\%%%-N$$$$&&&#--- 	>'**U34A==C::aii &#---rS   c                 t  	
 t        dddd      }t        j                  ddddd	gid
g      	t        || j                        
t
        j                  
_        
j                          	
fd}t        dddii
      j                   t        dddii
      j                   t        dddii
      j                   t        ddi
d      }t        ddi
d      }t        ddi
d      } ||      } ||      }||k(  sJ  ||        ||        ||      }||k(  sJ  ||      } ||      }||k(  sJ y )Nr   T   r   r   r}   r    r~   r   r   c                     | j                  dj                   d      }|j                  dk(  sJ t        j                  |j
                        d   }| j                  d| d|d       |S 	Nr.   r4   r!   r"   r   r   r   r   rC   r"   r:   r8   rD   rE   r7   r>   _rtask_idr   rL   s      rQ   complete_taskz3test_breadth_first_overlap_3.<locals>.complete_task  k    ]]^GJJ<u=>~~$$$**RZZ(.WI];7^oBpqrS   r   r   r   cccr   z!ann1@testbreadthfirstoverlap3.comz!ann2@testbreadthfirstoverlap3.comz!ann3@testbreadthfirstoverlap3.com)r   r8   r9   r   r6   r	   r   r   r   r   r"   r   )rG   r   r   r   r   ann3task_id_ann1task_id_ann2task_id_ann3r   rL   s            @@rQ   test_breadth_first_overlap_3r     sm    	F 

#	U^aj`kTl	mn 6?#7#78GGLLN v'144v'144v'1447$GH'SWXD7$GH'SWXD7$GH'SWXD !&L &L<'''$$ &L<''' &L &L<'''rS   c                    t        dddd      }t        j                  ddddd	gid
g      t        || j                        t
        j                  _        j                          fd}t        dddii       t        dddii       t        ddid      }t        ddid      }t        ddid      } ||        ||        ||       |j                  dj                   d      }|j                  dk(  sJ |j                  dj                   d      }|j                  dk(  sJ |j                  dj                   d      }|j                  dk(  sJ y )N(test_try_take_last_task_at_the_same_timeTr(   r   r   r}   r    r~   r   r   c                     | j                  dj                   d      }|j                  dk(  sJ t        j                  |j
                        d   }| j                  d| d|d       |S r   r   r   s      rQ   r   z?test_try_take_last_task_at_the_same_time.<locals>.complete_task  r   rS   r   r   r   r   zann1@lasttask.comzann2@lasttask.comzann3@lasttask.comr.   r4   r!   r)   )r   r8   r9   r   r6   r	   r   r   r   r   r   rC   r"   r:   )	rG   r   r   r   r   r   r   r   rL   s	          @@rQ   r   r     s~    8	F 

#	U^aj`kTl	mn 6?#7#78G''GLLN v'1v'17$78'4HD7$78'4HD7$78'4HD $$$ 
N7::,e4	5B>>S   	N7::,e4	5B>>S   	N7::,e4	5B>>S   rS   c                 2   t        dddd      }t        j                  ddddd	gid
g      }t        ||j                        }t
        j                  |_        |j                          t        dddii|      j                  }t        dddii|      j                  }t        ddi|d      }t        ddi|d      }|j                  d|j                   d      }	|	j                  dk(  sJ t        j                  |	j                        d   |k(  sJ |j!                  d| d||d      }	|	j                  dk(  sJ |j                  d|j                   d      }	|	j                  dk(  sJ t        j                  |	j                        d   |k(  sJ |j                  d|j                   d      }	|	j                  dk(  sJ t        j                  |	j                        d   |k(  sJ |j!                  d| d||d      }	|	j                  dk(  sJ t"        j$                  j                  |      }
|
j&                  sJ t)        j*                  d      r|
j,                  dk(  sJ |j                  d|j                   d      }	|	j                  dk(  sJ t        j                  |	j                        d   |k(  sJ |j                  d|j                   d      }	|	j                  dk(  sJ t        j                  |	j                        d   |k(  sJ y )Nr   Tr(   r   r   r}   r    r~   r   r   r   r   r   r   zann1@testbreadthlabelraces.comzann2@testbreadthlabelraces.comr.   r4   r!   r"   r   r   r   r   r2   r   r   r   r   )r   rG   r   r   rL   r   r   r   r   r$   r   s              rQ   "test_breadth_first_with_label_racer     s     	F 

#	U^aj`kTl	mn 6?#7#78G''GLLN
Vfe_-w
7
:
:C
Vfe_-w
7
:
:C7$DEwPTUD7$DEwPTUD 	>'**U34A==C::aii &#--- 			KuM2#Qb9c	dA==C 	>'**U34A==C::aii &#--- 	>'**U34A==C::aii &#--- 			KuM2#Qb9c	dA==C\\%%%-N$$$$&&&#--- 	>'**U34A==C::aii &#--- 	>'**U34A==C::aii &#---rS   c           	         t        ddd      }t        j                  dddddgid	g      }t        ||j                        }t
        j                  |_        |j                          t        d
di|d      }t        d
di|d      }g }d}t        |      D ]#  }	|j                  dddt        |	       ii       % |j                  d|j                   dt        j                  |      d      }
|
j                  dk(  sJ |j!                  d|j                   dt        j                  dddd      d      }
|
j                  dk(  sJ t"        j$                  j'                  |j                        j'                  d      }|j)                         dk(  sJ t"        j$                  j'                  |j                        j'                  d      }|j)                         dk(  sJ |j+                         j                  }|j+                         j                  }|j-                  d|j                   d      }
|
j                  dk(  sJ t        j.                  |
j0                        d   |k(  sJ |j-                  d|j                   d      }
|
j                  dk(  sJ t        j.                  |
j0                        d   |k(  sJ t"        j$                  j-                  |      j3                         sJ |j-                  d|j                   d      }
|
j                  dk(  sJ t        j.                  |
j0                        d   |k(  sJ |j                  d| d ||d!"      }
|
j                  dk(  sJ |j-                  d|j                   d      }
|
j                  dk(  sJ t        j.                  |
j0                        d   |k(  sJ |j                  d| d ||d!"      }
|
j                  dk(  sJ |j-                  d|j                   d      }
|
j                  dk(  sJ t        j.                  |
j0                        d   |k(  sJ y#)$a"  
    2 annotators takes and finish annotations one by one
    depending on project settings overlap

    create project
    make annotation result
    make 2 annotators
    bulk create tasks
    change project settings
    check overlap
    next annotate tasks

    check code comments
    r   Tr   r   r}   r    r~   r   r   r   z!ann1@testlabelracewithoverlap.comz!ann2@testlabelracewithoverlap.comr(   r   this is r.   r   r/   r0   r2   /2   rZ   overlap_cohort_percentageshow_overlap_firstr!   rL   overlapr   r4   r"   r   r   r   r   r   N)r   r8   r9   r   r6   r	   r   r   r   r   rangeappendstrr7   r"   r:   rr   r   r;   r@   ri   firstrC   rD   rE   has_lock)r   rG   r   r   rL   r   r   rJ   	num_tasksir$   rM   t1
overlap_idother_ids                  rQ   test_label_race_with_overlapr   W  s    "  F 

#	U^aj`kTl	mn 6?#7#78G''GLLN7$GH'SWXD7$GH'SWXD EI9 >fv#a&':;<=>
L1

58IXj 	 	A ==C 	
A&ZZPRjnop' 	 	A
 ==CGJJ/66q6AA779>>			WZZ		0	7	7	7	BB88:??Jxxz}}H 	>'**U34A==C::aii &*444 	>'**U34A==C::aii &*444<<z*33555 	>'**U34A==C::aii &*444 			K
|=9_p@q	rA==C 	>'**U34A==C::aii &*444 			K
|=9_p@q	rA==C 	>'**U34A==C::aii &(222rS   c           	      	   t        ddd      }t        j                  dddddgid	g      }t        ||j                        }t
        j                  |_        |j                          t        d
di|d      }t        d
di|d      }g }d}t        |      D ]#  }	|j                  dddt        |	       ii       % |j                  d|j                   dt        j                  |      d      }
|
j                  dk(  sJ |j!                  d|j                   dt        j                  dddd      d      }
|
j                  dk(  sJ t"        j$                  j'                  |j                        j'                  d      }|j)                         dk(  sJ t"        j$                  j'                  |j                        j'                  d      }|j)                         dk(  sJ |j+                         j                  }|j+                         j                  }|dt        j                  dddddgid	g      t        j                  g       d}|j-                  d|j                   d      }
|
j                  dk(  sJ t        j.                  |
j0                        d   |k(  sJ |j-                  d|j                   d      }
|
j                  dk(  sJ t        j.                  |
j0                        d   |k(  sJ t"        j$                  j-                  |       j3                         sJ |j-                  d|j                   d      }
|
j                  dk(  sJ t        j.                  |
j0                        d   |k(  sJ |j                  d!| d"|#      }
|
j                  dk(  sJ |j-                  d|j                   d      }
|
j                  dk(  sJ t        j.                  |
j0                        d   |k(  sJ |j                  d!| d"||d$#      }
|
j                  dk(  sJ |j-                  d|j                   d      }
|
j                  dk(  sJ t5        ||       t        j.                  |
j0                        d   |k(  sJ |j-                  d|j                   d      }
|
j                  dk(  sJ y%)&a-  
    2 annotators takes and leaves with draft annotations one by one
    depending on project settings overlap

    create project
    make annotation result
    make 2 annotators
    bulk create tasks
    change project settings
    check overlap
    next annotate tasks

    check code comments
    r   Tr   r   r}   r    r~   r   r   r   zann1@testlabelracewdrafts.comzann2@testlabelracewdrafts.comr(   r   r   r.   r   r/   r0   r2   r   r   r   r!   r   r   r   gZd;@)rp   	lead_timedraftr%   r4   r"   r   r   r   r   r   N)r   r8   r9   r   r6   r	   r   r   r   r   r   r   r   r7   r"   r:   rr   r   r;   r@   ri   r   rC   rD   rE   r   print)r   rG   r   r   rL   r   r   rJ   r   r   r$   rM   r   r   r   annotation_draft_results                   rQ   %test_label_w_drafts_race_with_overlapr     s   "  F 

#	U^aj`kTl	mn 6?#7#78G''GLLN7$CDgtTD7$CDgtTD EI9 >fv#a&':;<=>
L1

58IXj 	 	A ==C 	
A&ZZPRjnop' 	 	A
 ==CGJJ/66q6AA779>>			WZZ		0	7	7	7	BB88:??Jxxz}}H 'FIYbendoXpqr
 **R. 	>'**U34A==C::aii &*444 	>'**U34A==C::aii &*444<<z*33555 	>'**U34A==C::aii &*444 			K
|=9@W	XA==C 	>'**U34A==C::aii &*444 			K
|=9_p@q	rA==C 	>'**U34A==C	*h::aii &(222 	>'**U34A==CrS   c           	      j   t        ddd      }t        j                  dddddgid	g      }t        || j                        }t
        j                  |_        |j                          t        d
di|d      }t        d
di|d      }g }d}t        |      D ]#  }|j                  dddt        |       ii       % | j                  d|j                   dt        j                  |      d      }	|	j                  dk(  sJ | j!                  d|j                   dt        j                  ddi      d      }	|	j                  dk(  sJ t#        d       |j%                  d|j                   d      }	t        j&                  |	j(                        d   }
|j                  d|
 d|
|d       t#        d       |j%                  d|j                   d      }	t        j&                  |	j(                        d   |
k(  sJ t#        d        |j%                  d|j                   d      }	t        j&                  |	j(                        d   }||
k7  sJ t#        d!       t        d"      D ]H  }|j%                  d|j                   d      }	t        j&                  |	j(                        d   |k(  rHJ  y )#Nr   Tr   r   r}   r    r~   r   r   r   zann1@testfetchfinal.comzann2@testfetchfinal.comr(   r   r   r.   r   r/   r0   r2   r   rZ   r!   z#ann1 takes any task and complete itr4   r"   r   r   r   r   zSann2 takes the same task (because of depth-first) but just lock it - don't completezann1 takes another taskz8ann1 should never take task_id since he has completed itr   )r   r8   r9   r   r6   r	   r   r   r   r   r   r   r   r7   r"   r:   rr   r   rC   rD   rE   )rG   r   r   rL   r   r   rJ   r   r   r$   r   another_task_ids               rQ   test_fetch_final_taken_taskr   -  s    F 

#	U^aj`kTl	mn 6?#7#78G''GLLN7$=>ND7$=>ND EI9 >fv#a&':;<=>
L1

58IXj 	 	A ==C 	
A&TZZ9NPQ8R-Sbt 	 	A ==C	
/0>'**U34Ajj#D)GIIG9M2'Uf9gIh	
_`>'**U34A::aii &'111	
#$>'**U34Ajj+D1Og%%%	
DE1X >HH~gjj\78zz!))$T*o===>rS   c                 v   t        ddt        j                  dd      }t        || j                  d      dd	d
dggdgddddd dddd
}ddddd	d ddgddgddgddgddgdd gd!d"gd#d$gd%d&gd'd(gd)d*gd+d,gd-d.gd/d0gd1d2gd3d4gd5d6gd7d8gd9d:ggdgddddd;
d<}g }t        |      D ]$  }|j                  t        d=d>| d?id             & t        j                  d@      5  t        dA      D ]W  }t        dBdCdD| dEii      }t        |      D ]3  }t        |gdAz  gdAz  z   ||   j                  dF|j                         5 Y 	 d d d        t        dBdCdGii      }t        dH       |d   j                  dIj                   dJ      }t!        j"                  |j$                        dK   |j                  k(  sJ dNfdL	}	|j'                         sJ t)        j(                         }
 |	|d          t)        j(                         |
z
  dk  sJ dM       |j'                         sJ y # 1 sw Y   xY w)ONtest_with_failed_matching_scoreTr   aW  
            <View style="display: flex">
              <View style="width: 275px">
                <Header value="Pick tooth label" />
                <PolygonLabels name="tag" toName="img" strokewidth="2" pointstyle="circle" pointsize="small" showInline="true">
                  <Label value="t11" background="#8ffe09"></Label>
                  <Label value="t12" background="#2000b1"></Label>
                </PolygonLabels>
              </View>
              <View>
                <Image name="img" value="$image" showMousePos="true" zoom="true" />
              </View>
            </View>)r   r   r   rZ   r   Fr`   
Yv_lLEp_8IpolygonlabelsgmX/P@glPlGR@t11)pointsr   z$imageimgtagr   i  i  )
r"   r   r   sourcer   r   	parent_idimage_rotationoriginal_widthoriginal_heightz
NsccF-AYMTg޺(FA@gxJ.@g`[=A@g6+@g}	@@gǿzv?@g8j.@gt+>@g.Bz0@gN=@gZD2@giV=@g2Vё4@g+l7<@gE$W,7@g78eg=@g6j9@gĠZSh>@gVL̨9@g6h?@gUv&F9@guU`@@g\X8@gH$A@g4ݵC!8@gVIA@g5=7@gV8B@gj6@g qB@gw sS5@goB@gf'93@gn'QB@g>21@g>~  B@g	3ڼ0@)
r"   r   r   r   r   r   r   r   r   r      r   annz @testwithbadannotationresult.comz>tasks.models.update_project_summary_annotations_and_is_labeled
   r   imagez$https://data.s3.amazonaws.com/image/z.jpg)r%   r3   z3https://data.s3.amazonaws.com/image/uncompleted.jpgz7ann1 takes any task with bad annotation and complete itr.   r4   r"   c                     t        d       | t        ddid      } | j                  d dt        j                  g      d       y )	NzAsync annotation submitr   z'new_ann@testwithbadannotationresult.comTr   r   r   r   )r   r   r7   r8   r9   )new_anngood_resultrL   r   s    rQ   make_async_annotation_submitzEtest_with_bad_annotation_result.<locals>.make_async_annotation_submit  sW    '(?$g/X%Y[bdhiG'-0!TZZ-FG 	 	
rS   zTime of annotation.submit() increases - that might be caused by redundant computations over the rest of the tasks - check that only a single task is affected by /api/tasks/<task_id>/annotationsre   )r   r	   r   r   r6   r   r   r   r   rr   r   r   r>   r"   r   rC   r8   rD   rE   r   time)rG   r   
bad_resultrh   annsr   rp   uncompleted_taskr$   r   rM   r   rL   r   s              @@@rQ   test_with_bad_annotation_resultr   i  s/    /!!F& 6?#7#7NG /1BCDX]W^_J  #$56"$67#%78#%67#%78#%67"$67#%78#%78#%67#%67#%78"$56"$67#%67#%67#%67"$56"$67'* $W-
0 C"KH ND>" iNGs1#5U-V#WY`bfghi
 
T	U r 	Afw2VWXVYY]0^&_`bijD>*  *|b0K=23EEW[\]W^WhWhikokrkr	 !&74i*j!kmtu	
CDQnWZZL67Ajj#D)G&)))))
 $$&&& 			A a) 			a	
 O OO 
 $$&&&K s   #A&H//H8setup_before_upload)FTr   c           	      z   | }t        ddd|dd      }t        || j                        t        j                  ddd	d	d
gidg      d}d}d}|rK|j                  dj                   dt        j                  d|d      d      }|j                  dk(  sJ d}g }	t        |      D ]#  }
|	j                  dddt        |
       ii       % | j                  dj                   dt        j                  |	      d      }|j                  dk(  sJ |rI|j                  dj                   dt        j                  d|d      d      }|j                  dk(  sJ t        |dz  |z        }t        j                  j                  t!        j                        t!        d      z        j#                         |k(  sJ fd}t%        ddid      }t%        ddid      }t        |      D ]  }
 ||       ||      f  t'        d  t        j                  j                  t!        j                        t!        d      z        D              }t'        d! t        j                  j                  t!        j                        t!        d"      z        D              }|r	|sJ |sJ y |rJ |rJ y )#Ntest_overlap_firstTr   zUniform samplingr   )r   r   rZ   r   r   r   r}   r    r~   r   r   i  r.   r   r(   )rZ   r   r/   r0   r!   Fr   r   r   r2   g      Y@)
project_id)overlap__gtc                     | j                  dj                   d      }|j                  dk(  sJ t        j                  |j
                        d   }| j                  d| d|d       y r   r   r   s      rQ   r   z)test_overlap_first.<locals>.complete_task  sf    ]]^GJJ<u=>~~$$$**RZZ(.WI];7^oBpqrS   r   zann1@testoverlapfirst.comzann2@testoverlapfirst.comc              3   4   K   | ]  }|j                     y wre   r   .0rM   s     rQ   	<genexpr>z%test_overlap_first.<locals>.<genexpr>*  s      --s   c              3   6   K   | ]  }|j                      y wre   r  r	  s     rQ   r  z%test_overlap_first.<locals>.<genexpr>-  s      4ALL4s   r   )r   r   r6   r8   r9   rr   r"   r:   r   r   r   r7   intr   r;   r@   r   ri   r   r<   )rG   r  r   cr   r   r   setup_after_uploadr$   rJ   r   expected_tasks_with_overlapr   r   r   "all_tasks_with_overlap_are_labeled)all_tasks_without_overlap_are_not_labeledr   rL   s                    @@rQ   r  r    s    	A"-#F  6?#7#78G

#	U^aj`kTl	mn I ! GGWZZL*ATmno+  

 }}###" E9 >fv#a&':;<=>
L1

58IXj 	 	A ==CGGWZZL*ATmno+  

 }}###"%&?%&G)&S"T<<qGJJ7!:JJKQQSWrrrrr 7$?@'4PD7$?@'4PD./ 1d]4001 *- -"ll11!wzz2JQ[\M]2]^- *& 14 4"&,,"5"5a7::6NQR[\Q]6]"^4 1- 111888555<<<<rS   c                   .    e Zd Zed        Zd Zd Zd Zy)TestTryBreadthFirstc                     t               | _        t               | _        t        dd      | _        t        dd      | _        y )Nr   T)rZ   annotator_evaluation_enabledF)r   r6   
other_userr
   project_with_evalproject_without_eval)clss    rQ   setUpTestDataz!TestTryBreadthFirst.setUpTestData:  s>    =$ !/ !)-!
 $2 !).$
 rS   c                    t        | j                        }t        | j                        }t        | j                        }t        j                  d|d       t        |d       t        |d       t        |d       t        |d       t        j
                  j                  | j                        }t        || j                  | j                        }||k(  sJ y)zh
        Test that _try_breadth_first excludes GT tasks when annotator_evaluation_enabled=True.
        r   r(   Frp   r&   TN)	r   r  r   create_batchr   r;   r@   r   r6   rg   task_1task_2	task_3_gtrJ   r%   s         rQ   8test_excludes_ground_truth_tasks_when_evaluation_enabledzLTestTryBreadthFirst.test_excludes_ground_truth_tasks_when_evaluation_enabledK  s    
 T%;%;<T%;%;<(>(>?	 	&&qvEJ 	vE: 	yt<yu=yu= ##D,B,B#C $E499d6L6LM rS   c                    t        | j                        }t        | j                        }t        | j                        }t        j                  d|d       t        |d       t        |d       t        |d       t        |d       t        j
                  j                  | j                        }t        || j                  | j                        }||k(  sJ y)zi
        Test that _try_breadth_first includes GT tasks when annotator_evaluation_enabled=False.
        r   r(   Fr  TN)	r   r  r   r  r   r;   r@   r   r6   r  s         rQ   9test_includes_ground_truth_tasks_when_evaluation_disabledzMTestTryBreadthFirst.test_includes_ground_truth_tasks_when_evaluation_disabledh  s    
 T%>%>?T%>%>?(A(AB	 	&&qvEJ 	vE: 	yt<yu=yu= ##D,E,E#F $E499d6O6OP """rS   c                     t        | j                        }t        |d       t        j                  j                  | j                        }t        || j                  | j                        }|J y )Nr   Tr  )r   r  r   r   r;   r@   r   r6   )rg   task_gtrJ   r%   s       rQ   Gtest_returns_none_when_no_tasks_with_annotations_and_evaluation_enabledz[TestTryBreadthFirst.test_returns_none_when_no_tasks_with_annotations_and_evaluation_enabled  s_    d&<&<=wT:##D,B,B#C $E499d6L6LM ~~rS   N)rj   rk   rl   classmethodr  r#  r%  r(  rf   rS   rQ   r  r  9  s#    
 
  :#:rS   r  )<__doc__r8   r   unittestr   pytest
core.redisr   django.appsr   django.db.modelsr   django.testr   projects.functions.next_taskr   projects.modelsr	   projects.tests.factoriesr
   tasks.modelsr   r   r   tasks.tests.factoriesr   r   users.tests.factoriesr   utilsr   r   r   r   r   r   r   r   *_project_for_text_choices_onto_A_B_classesmarkparametrize	django_dbrR   r{   r   skipifr   r   r   r   r   r   r   r   r   r   r   r   r  r  rf   rS   rQ   <module>r=     s_       (     ; # 3 5 5 @ -  .2
  	. *  E 7}hGH}hGH )*	
 7 +8J14az5$Q#R }hGH )*	
 7 +8J14az5$Q#R
 +8J14az5$Q#R	 ~&	
" 7 +8J14az4$P#Q
 +8J14az5$Q#R	 )*	
  ! ,,	 8X>?&W_iqJrAst)*!	
kGJV r WJXr@ f ! ,,% 8X>?&W_iqJrAst#^45PQR#^45PQR
  n%&5	
< ! ,,% 8X>?&W_iqJrAst#^45PQR#^45PQR
  n%&5	
< ! ,,$%%  hABhABhAB $^45PQR#^45PQR#^45PQR V^456
 n%&C"	
J ! ,,% hABh
CDhABhAB	 $^45PQR#^45PQR#^45PQR#^45PQR	 V^456	 n%&G$	
N ! ,,% hABh
CDh
CDhABhABh
CD $^45PQR#^45PQR#_56QRS#^45PQR#^45PQR#^45PQR V^456V^456V^456 o&'S*	
Z ! ,,% hABhABhAB $^45PQR#^45PQR#^45PQR V^456
 n%&A!	
H ! ,,% hABhABhAB $^45PTU#^45PTU#^45PTU V^456
 n%&A!	
H ! ,,%$%  hABhABhABhABhAB $^45PTU#^45PTU#^45PTU#^45PTU#^45PTU &.123T4tLn%&C"	
q[x		}  hR	 $G S	hT	$GN 0? 0?f )++4higoow7J7JGL\L\%]^".  _ j".J )++4higoow7J7JGL\L\%]^-.  _ j-.` 3. 3.l .( .(b )++4hi0!  j0!f )++4hi<.  j<.~ )++4hi^3  j^3B )++4hin   jn b 8> 8>v )++4hir'  jr'j .>-}=S=  > ?S=lW( WrS   