
    ]ic9                        % S r SSKJr  SSKJrJr  SSKrSSKJr  SSK	r	SSK
Jr  SSKrSSKrSSKrSSKrSSKJrJrJr  SS	KJrJrJr  SS
KJr  SSKJrJrJr  SSKJ r J!r!  SSK"J#r#J$r$J%r%  SSK&r'\" \S5      r\RP                  \)/S4   \*S'   S r+\ " S S5      5       r,S r-S\R\                  S\4S jr/\" 5       S\," 5       4S\S\S\)S\S\R`                  \   S\,S\Rb                  \    4S jjr2S r3S\Rb                  \)   S\S\Rb                  \    S\Rb                  \    4S  jr4S\R\                  S\S\S!\Rb                  \    S"\RP                  S\54S# jr6g)$z
This defines the `dora grid` action, and provides a `run_grid` API
that can be used from a notebook or any other script.

When using the API, you can provide the equivalent of the command line flags
with the `RunGridArgs` dataclass.
    )ProcessPoolExecutor)	dataclassfieldN)partial)Path   )SlurmConfigSubmitRulesupdate_from_args)ExplorerLauncherHerd)DecoratedMain)colorize
simple_logfatal)SheepShepherd)import_or_fatalreliable_rmtreetry_loadzGrid:logc                      g N )argskwargss     F/mnt/rpi/tmp/demucs-venv-sys/lib/python3.13/site-packages/dora/grid.pyno_printr   %   s        c                      \ rS rSr% Sr\" \S9r\R                  \
   \S'   Sr\\S'   Sr\\S'   S	r\R"                  \   \S
'   Sr\\S'   Sr\\S'   Sr\\S'   Sr\\S'   Sr\\S'   Sr\R"                  \   \S'   Sr\\S'   S	r\R"                  \   \S'   S	r\R"                  \   \S'   S	r\R"                  \   \S'   Sr\\S'   Srg	)RunGridArgs)   a)  
Arguments to tune the behavior of the `run_grid` function.

Args:
    patterns (list[str]): List of patterns used to filter by name
        the XPs.
    monitor (bool): if True, will monitor the advances of the XPs
        every `interval` minutes, stopping only when all runs completed or
        failed.
    interval (float): interval in minutes to wait between updates.
    trim (int or None): if provided, will trim all XP logs to the epoch of
        the XP with the provided index. Useful to compare XP started at different
        times.
    trim_last (bool): if True, will trim all XP to the least advanced XP.
    silent (bool): if True, do not print anything (e.g. API usage).
    dry_run (bool): if True, Dora will simulate the run of the grid, without scheduling
        or canceling any XP.
    cancel (bool): if True, will cancel all XPs in the grid. If `patterns` is provided,
        only XP matching the patterns will be canceled.
    clear (bool): This will cancel any previous job, clear the XP folder,
        and reschedule a new experiment.

)default_factorypatternsTmonitor   intervalNtrimF	trim_lastsilentdry_runcancelclearinitjupyterfolderr   tail_from_commandliner   )__name__
__module____qualname____firstlineno____doc__r   listr%   tpListstr__annotations__r&   boolr(   floatr)   Optionalintr*   r+   r,   r-   r.   r/   r0   r1   r   r2   r3   __static_attributes__r   r    r   r"   r"   )   s    . #48Hbggcl8 GTHe!D"++c
!ItFD GTFDE4#D"++d
#GT  $FBKK# CS	 !D"++c
!#t#r    r"   c                    UR                   R                  nUc  UR                  S-   n[        U5      nU R                  bA  U R                  R                  SS5      S-   n[        UR                  5      R                  U-  nU R                  b  WR                  5       (       GdU  / n[        UR                  5      R                  n[        R                  " U5       H  u  pn
U
 H  n[        U5      U-  R                  U5      nUR                  R                  S5      (       d  MB  UR                  R                  S5      (       a  Md  UR                  UR                   -  nUR#                  [%        U5      R                  SS5      5        M     M     U R                  b'  WR                  5       (       d  ['        SW SU S35        ['        S	S
R)                  U5       35        [*        R,                  " S5        US-   U R                  -   n[        U5      n UR.                  n[5        W[6        5      (       d  [3        U S35        U$ ! [0         a    [3        U S35         N?f = f)Nz.grids./z.py_zNo grid file z in package z. Maybe you made a typo?zPotential grids are: z, r   z( has no exploration function `explorer`.z' must be an instance of `dora.Explorer`)doragrid_packagepackager   gridreplacer   __file__parentexistsoswalkrelative_tonameendswith
startswithstemappendr<   r   joinsysexitexplorerAttributeErrorr   
isinstancer   )r   mainrH   gridsgrid_filename	grid_file
candidatespkg_rootrootfoldersfilesfilefullpath	grid_namerJ   rZ   s                   r   _get_exploreri   [   s   99))L||h.L)Eyy		))#s3e;(//-?	yy	 0 0 2 2
'..$&GGH$5 D5 J-::8D==))%009Q9QRU9V9V'>H%%c(m&;&;C&EF	  %6 99 )9)9););-l<. I) ) *#DIIj$9#:;<s"TYY.I9%DF== h))
ABCO	  FCDEFs   I	 	I$#I$r   r]   c                     [        X5      nUR                  5       n[        X05        [        5       n[        X@5        [	        5       nSUl        [        XP5        [        XU R                  XCU5        g )NT)ri   get_slurm_configr   r
   r"   r3   run_gridrJ   )r   r]   rZ   slurmrules	grid_argss         r   grid_actionrp      sZ    D'H!!#EU!MEU!I"&IY%TTYYi@r    rZ   rh   rn   rm   returnc           	      z   [        U[        5      (       d   eUc  U R                  5       nU R                  R                  U R                  R
                  -  U-  nUR                  SSS9  [        5       n[        U [        S9nU R                  (       a7  [        S5       n	[        XXyS9n
U" U
5        UR                  5         SSS5        O[        XU5      n
U" U
5        UR                  5         [        UR                   R#                  5       5      n[%        UR&                  X5      nUR(                  (       Ga#  UR*                  (       a  [-        S5        [        S[/        U5       S	U S
35        UR0                  (       a*  [3        S5      nUR5                  5       S:w  a  [-        S5        [        S5        U H-  nUR6                  c  M  UR9                  UR6                  5        M/     UR;                  5         [        S5        U HR  nUR<                  R>                  RA                  5       (       a  [C        UR<                  R>                  5        SUl        MT     / n/ nURE                  5        Hb  nURF                  UR                   ;  d  M  URI                  U5         URK                  URF                  5      nUc   eURI                  U5        Md     UR                  5         URV                  (       d  U Vs0 s H  oR<                  RX                  U_M     nnURZ                   H  nU Vs/ s H  nUU;   d  M  UU   PM     nnU(       d  M)  US   nUR\                  UR<                  RX                     n[/        U5      S:X  a  UR_                  UXC5        Mt  URa                  U5         U H  nUR_                  XU5        M     SSS5        M     U H  nURc                  5       (       a  M  UR6                  c   eUR9                  UR6                  5        U Re                  UR<                  5      n[        SUR6                  RT                   SUR<                  RX                   SU 35        M     URV                  (       a  U H  nURc                  5       (       a  M  UR6                  c   eU Re                  UR<                  5      n[        SUR6                  RT                   SUR<                  RX                   SU 35        UR9                  UR6                  5        M     UR*                  (       d  U H  nXmR<                  RX                  -  nURA                  5       (       d  URg                  5       (       aO  URg                  5       (       a6  URi                  5       UR<                  R>                  Ri                  5       :X  d   eM  URk                  UR<                  R>                  5        M     UR;                  5         U H  nURm                  5         M     URn                  (       a$  U H  nU Rq                  UR<                  5        M      URV                  (       a  U$ U(       d  [        S5        U$ UR>                  UR                  URr                  4 Vs/ s H
  nUc  M  UPM     nnU(       GaW  UR0                  (       d  [u        S5      e[/        U5      S:X  d   eUS   n UU   nU Re                  WR<                  5      nUR>                  b!  [y        UR<                  R>                  5        U$ URr                  b`  UR                  RA                  5       (       d  [-        SUR                   S35        [z        R|                  " SSS S!S"UR                  /5        U$ UR                  RA                  5       (       d  [-        S#U S35         [~        R                  " [        UR                  5      [        R                  5        U$ UR                  (       a  [        n O[x        n U " S$U 35         UR                  (       a&  UR                  (       d  SS%KHJIn!  U!R                  SS&9  UR                  5         [        XPXU 5      (       a   U$ UR                  (       d   U$ [        UR                  S'-  5      n"U " 5         [        U"5       H?  n#S(U"U#-
  S) S*3n$U"U#-
  S+:  a  [        U$S,5      n$U " U$S-S.9  [        R                  " S5        MA     U " S/5        M  ! , (       d  f       GNW= f! [L         a  n[        SURF                   SU 35        UU R                  RN                  RP                  -  n[S        U5      nUbC  US   n[        SURT                   SURF                   S35        UR9                  U5         SnAGMa   SnAGMh  SnAff = fs  snf s  snf ! , (       d  f       GM  = fs  snf ! [v         a    [-        SUR>                   35         GN1f = f! [         a     U$ f = f)0a  
Run a grid search, this is the API underlying the `dora grid` command,
so that it can be used from a notebook.
You can also provide patterns to filter out XPs to be displayed.

Args:
    main (DecoratedMain): main training function, decorated with Dora.
    explorer (Explorer): explorer instance that will define the XPs to launch.
    grid_name (str): this must be a unique name for the grid.
    rules (SubmitRules): see `dora.conf.SubmitRules`, those defines the
        rules for rescheduling failed XP etc.
    slurm (SlurmConfig or None): if provided, this will override
        the default Slurm config defined my the `main` argument.

Returns:
    A list of `dora.shep.Sheep`.

NT)exist_okparents)r      )poolz'--dry_run is incompatible with --clear.zYou are about to restart z experiments from the grid z' from scratch. This cannot be reverted.zConfirm [yN]: yzAbort...zCanceling all current jobs...zDeleting XP folders...z$Error when trying to load old sheep z: r   zCanceling job z from unloadable sheep rD   r   z for no longer required sheep rE   z for sheep zNo sheep to handle.zIThe folder, log, and tail flags are only supported from the command line.zInvalid index zLog z does not existr2   z-n200z-fz"Log file does not exist for sheep zMonitoring Grid )display)wait<   zNext update in z.0fz seconds       
   31)endz<                                                            )Rr\   r   rk   rG   dir_gridsmkdirr   r   r   _slowr   r   completeupdater9   sheepsvalues_filter_grid_sheepsr%   r.   r,   r   lenr3   inputlowerjobcancel_lazycommitxpr1   rN   r   iterdirrR   rV   get_sheep_from_sig	Exceptionshepjob_filer   job_idr-   sig
job_arraysslurm_configsmaybe_submit_lazy	job_arrayis_doneget_name
is_symlinkresolve
symlink_tounlinkr/   init_xpr2   RuntimeError
IndexErrorprintrO   execvpshutilcopyfileobjopenrX   stdoutBrokenPipeErrorr+   r   r0   IPythonry   clear_outputr&   rA   r(   ranger   timesleep)%r]   rZ   rh   rn   rm   r   grid_folderherdshepherdrv   launcherr   replsheep	to_unlink
old_sheepschild	old_sheeperrorr   jobsr   	sheep_mapr   r   array_sheepsfirstrR   linkactionactionsindexmaybe_printry   r   elaouts%                                        r   rl   rl      s<   * h))))}%%'))--$))"2"22Y>KtT26D#&Hzz #tAHXMMO $#
 HT2OO$++$$&'F =Fzzz<<;<'F}4OPY{ [5 5 	6!!)*Dzz|s"j!+,Eyy$$$UYY/  	$%Exx%%''0EI 
 IJ$$&::T[[(U#-$77

C	 !,,,!!),! '$ OO;;6<=fUXX\\5(f	=I6?Tis3)CSNIcNiLT OE&&uxx||4E< A%**5%?''.!- 225G ". /. )  	  ""==,,,  /==.D.!5!5 6 7"))*!D63 4   {{E==??yy,,,}}UXX.nUYY%5%5$6k%((,,qQUPVWX$$UYY/  <<E((,,.D{{}} 1 1((T\\^uxx?V?V?X-XXX-X0  	ELLN yyELL"  {{!"%)[[$((DII$F]$F&&v$FG]%%  Q R R7|q   
	25ME }}UXX&;;"%((//"  YY"99##%%UYYK78IIfvtUD%))DE  99##%%:4&BC""4		?CJJ?  {{"9+./
<<'  d +4x== M || M DMMB&'<C#ECK#4ODCs{RsD)&JJqM   	H% ] $#N  .:5::,bPQ 499>>#:#::)#q'C.4KEJJ<WXYZ((-- $.  >T /.V ^  	2N4;;-01	2 # ss   #d'"d9< g!1
g&?	g&/g+$g>/g>2h 8h, '
d69
gBgg+
g;	"h)(h),
h:9h:c                     U(       d  gU HM  nSnUS S S:X  a  USS  nSn[         R                   " U SU-   S-   5      nU(       a  U(       a    gMD  U(       a  MM    g   g)NTFr   !*)fnmatch)rR   r%   patternnegresults        r   _match_namer   P  si    2A;#abkGCsW}s':;   r    r%   r   c                 z   / n[        U 5       H/  n UR                  [        U5      5        U R                  U5        M1     / nU HA  nUR                  UR                  5      n[        Xp5      (       d  M0  UR                  U5        MC     U(       a  U Vs/ s H  oU   PM	     nnU$ ! [         a     M  f = fs  snf r   )r9   rV   rA   remove
ValueErrorr   r   r   )	r%   r]   r   indexespr   r   rR   idxs	            r   r   r   a  s    G(^	NN3q6" OOA  C}}UXX&t&&JJu  #*+7C3x7+J  		 ,s   B'B8'
B54B5r   r   c                    UR                  U Vs/ s H  oUR                  PM     sn5      u  pgU Vs/ s H  oQR                  UR                  5      PM     nnSn	U R                  b  [	        XR                     5      n	O#U R
                  (       a  [        S U 5       5      n	U	b  U V
s/ s H  oSU	 PM	     nn
/ nSn[        [        X8U5      5       H  u  nu  p^nUR                  5       nUR                  5       (       d  SnUc  SnOUSS nUUUR                  (       a  UR                  R                  OSUR                  R                  US.n0 nUUS	'    UR                  X^5      nUR#                  U5        UR%                  U5        M     U(       a	  U" S
U5        [&        R(                  " S[&        R*                  " S	UR-                  5       5      /UR/                  5       -   S9nU" [&        R0                  " UUUR3                  5       S95        U$ s  snf s  snf s  sn
f ! [         a    UR!                  U5      n Nf = f)z|Single iteration of monitoring of the jobs in a Grid.
Returns `True` if all jobs are done or failed, and `False` otherwise.
Nc              3   8   #    U  H  n[        U5      v   M     g 7fr   )r   ).0metricss     r   	<genexpr>monitor.<locals>.<genexpr>  s     9yG3w<<ys   TFzN/A    )rR   r   sidr   stateMetazBase name: )shortengroups)colors)	get_namesr   get_xp_historyr)   r   r*   min	enumeratezipr   r   r   r   r   process_sheepNotImplementedErrorprocess_historyr   rV   tttablegroupget_grid_metaget_grid_metrics	treetable
get_colors)r   r]   rZ   r   r   r   names	base_name	historiesr)   r   linesfinishedr   historyrR   r   metalineotherr   s                        r   r&   r&   u  s   
 ~~T&BTExxT&BCE<@AD5$$UXX.DIADyy9YY'(	9y993<=9Ud^9	=EH)23t3N)O%%}}H=E"1IE',yy599##b88<<
 V	6**5:E 	ET- *P0 M9-HHHHVX3356
%%'(E UE(2E2E2GHIO_ 'CA >. # 	6,,W5E	6s"   H$H)HH$$II)7r8   concurrent.futuresr   dataclassesr   r   r   	functoolsr   rO   pathlibr   typingr:   r   rX   r   confr	   r
   r   explorer   r   r   r]   r   r   r   r   r   r   r   r   utilsr   r   r   r   r   Callabler<   r=   r   r"   ri   Anyrp   r@   r;   rl   r   r   r>   r&   r   r    r   <module>r     s   3 (   	    
  < < - -  , , ! = =  '
G <R[[# <	 .$ .$ .$b#L	Abff 	AM 	A #.-SW!,@= @H @ @@8:K8P@@35775>@F""''#, m  "35775>(4"&& 4 4 4QV 44)-4r    