Ë
    ëÓ^jŸ%  ã                  óÄ   — d Z ddlmZ ddlmZ 	 ddlmZmZ ddlm	Z	 ddl
mZ dZdd
„Zdd„Zdd„Z	 	 	 	 	 	 	 	 dd„Z	 	 	 	 	 	 	 	 	 	 dd„Zy# e$ r dZdZdZ	dZd	ZY Œ8w xY w)z4Rich console helper for the training callback stack.é    )Úannotations)ÚAny)ÚConsoleÚGroup)ÚLive)ÚTableTNFc                óÒ   — t         €t        d«      ‚t        | dg «      D ];  }t        d„ t	        |«      j
                  D «       «      sŒ)t        |dd«      }|€Œ9|c S  t        d¬«      S )u‰  Return a Rich Console appropriate for the current training context.

    When a ``RichProgressBar`` callback is active, its ``_console`` is the Rich global
    singleton that owns the Live progress display.  Printing through that same console
    instance ensures output appears correctly above the progress bars rather than
    conflicting with the Live display's cursor positioning (particularly on Windows).

    Falls back to ``Console(force_terminal=True)`` for plain-terminal and TQDM contexts.

    Args:
        trainer: The PTL Trainer (or any object with a ``callbacks`` attribute).

    Returns:
        A Rich ``Console`` instance suitable for printing metric tables.

    Note:
        Reads ``RichProgressBar._console``, a private PTL attribute. When ``_console`` is
        ``None`` (outside an active fit/validate/test stage) or the attribute is renamed in a
        future PTL release, the function silently falls back to ``Console(force_terminal=True)``
        â€” a fresh console with an empty live stack that may reintroduce cursor conflicts on
        Windows. Subclasses and theme wrappers of ``RichProgressBar`` are matched via MRO.
    NzT_get_rich_console called when Rich is not installed; check _IS_RICH_AVAILABLE first.Ú	callbacksc              3  ó:   K  — | ]  }|j                   d k(  –— Œ y­w)ÚRichProgressBarN)Ú__name__)Ú.0Úclss     úc/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/rfdetr/utilities/console.pyú	<genexpr>z$_get_rich_console.<locals>.<genexpr>7   s   è ø€ ÒM°Sˆs�|‰|Ð0Õ0ÑMùs   ‚Ú_consoleT)Úforce_terminal)r   ÚRuntimeErrorÚgetattrÚanyÚtypeÚ__mro__)ÚtrainerÚcbÚ
cb_consoles      r   Ú_get_rich_consoler      sr   € ô. €ÜØbó
ð 	
ô �g˜{¨BÓ/ò "ˆÜÑM¼DÀ»H×<LÑ<LÔMÕMÜ   Z°Ó6ˆJØÑ%Ø!Ò!ð	"ô
  $Ô'Ð'ó    c                ó@   — t        | dg «      }t        d„ |D «       «      S )zûReturn whether the trainer has a Lightning progress bar callback.

    Args:
        trainer: The PTL Trainer (or any object with a ``callbacks`` attribute).

    Returns:
        ``True`` when any callback class name ends with ``"ProgressBar"``.
    r
   c              3  óf   K  — | ])  }|j                   j                  j                  d «      –— Œ+ y­w)ÚProgressBarN)Ú	__class__r   Úendswith)r   Úcallbacks     r   r   z$_has_progress_bar.<locals>.<genexpr>H   s'   è ø€ Ò]Àxˆx×!Ñ!×*Ñ*×3Ñ3°M×BÑ]ùs   ‚/1)r   r   )r   r
   s     r   Ú_has_progress_barr$   >   s$   € ô ˜ ¨bÓ1€IÜÑ]ÐS\Ô]Ó]Ð]r   c           
     ó<  ‡8— d'd„}d|› �}d|› �}dd ||d   «      fd ||d   «      fd	 ||d
   «      fgfd| |||   «      fgfdd ||d   «      fd ||d   «      fd ||d   «      fgfg}d|v r+|j                  dd ||d   «      fd ||d   «      fgf«       |D ���	�
cg c]  \  }}|D ]	  \  }	}
|	|
f‘Œ Œ }}	}}}
|D �	�
cg c]&  \  }	}
t        t        |	«      t        |
«      «      dz   ‘Œ( c}
}	Š8d}|D ]e  \  }}t        |«      }t        ‰8|||z    «      |dz
  z   }t        |«      dz   }||kD  r&t	        ||z
  «      D ]  }‰8|||z  z   xx   dz  cc<   Œ ||z  }Œg g }d}|D ]/  \  }}t        |«      }|j                  |||z   dz
  |f«       ||z  }Œ1 |dd D ���ch c]  \  }}}|’Œ
 }}}}t        |«      }d(ˆ8fd„}d}d}d}d}d\  }}d}d \  }} d!}!d"}"d#\  }#}$}%d$\  }&}'}(t        ‰8«      |z   dz
  })| › d%�}*|*j                  |)dz   «      }+|},t        |«      D ]0  \  }-\  }	}.}|,| ||	|.«      z  z  },|,|-t        |«      dz
  k  r|n|z  },Œ2 |}/|D ]$  \  }	}.}|/|j                   ||	|.«      «      |z   z  }/Œ& |}0t        ‰8«      D ]!  \  }-}1|0||1z  z  }0|-|dz
  k  sŒ|0|-|v r|!n|"z  }0Œ# |0| z  }0|}2t        |«      D ]"  \  }-\  }3}|2|3j                  ‰8|-   «      |z   z  }2Œ$ |#}4t        ‰8«      D ]  \  }-}1|4||1z  z  }4|4|-|dz
  k  r|%n|$z  }4Œ |}5t        |«      D ]"  \  }-\  }}6|5|6j                  ‰8|-   «      |z   z  }5Œ$ |&}7t        ‰8«      D ]  \  }-}1|7||1z  z  }7|7|-|dz
  k  r|(n|'z  }7Œ d&j                  |+|,|/|0|2|4|5|7g«      S c c}
}	}}w c c}
}	w c c}}}w ))u(  Render the overall metrics table with merged group-header cells.

    Uses only plain Unicode box-drawing characters (no ANSI colour codes) so the output renders correctly in both
    terminals and Jupyter/Colab notebook widgets.

    Args:
        title_pfx: Capitalised split name used in the title (e.g. ``"Val"``).
        overall: Ordered mapping of metric label â†’ scalar value.
        max_dets: Maximum detection count used for the mAR column label.

    Returns:
        Multi-line plain-text string ready to pass to ``console.print()``.
    c                ó    — | | k7  s| dk  ry| d›S ©Nr   u   â€”z.4f© ©Úvs    r   Ú_fmtz$_render_overall_merged.<locals>._fmtZ   ó   € Ø�Š6�Q˜’UØØ�C�Ðr   ú@zmAR @ÚmAPz50:95z	mAP 50:95Ú50zmAP 50Ú75zmAP 75ÚmARzF1 sweepÚF1ÚPrecÚ	PrecisionÚRecallzsegm mAP 50:95zsegm mAPzsegm mAP 50é   r   é   Néÿÿÿÿc                ó2   •— t        ‰| |dz    «      || z
  z   S )z:Return merged cell width for columns start..end inclusive.r7   )Úsum)ÚstartÚendÚwidthss     €r   Úgrp_wz%_render_overall_merged.<locals>.grp_w—   s"   ø€ ä�6˜% #¨¡'Ð*Ó+¨s°U©{Ñ;Ð;r   u   â”�u   â”€u   â”ƒu   â”‚)u   â”�u   â”“u   â”³)u   â”¡u   â”©u   â•‡u   â”¯)u   â”œu   â”¤u   â”¼)u   â””u   â”˜u   â”´u    â€” Overall Metricsú
©r*   ÚfloatÚreturnÚstr)r;   Úintr<   rD   rB   rD   )ÚappendÚmaxÚlenr:   ÚrangeÚcenterÚ	enumerateÚjoin)9Ú	title_pfxÚoverallÚmax_detsr+   Úmar_lblÚmar_keyÚgroupsÚ_ÚcolsÚsr*   ÚflatÚcolÚgrpÚncÚcell_wÚneededÚkÚspansr;   r<   Úgrp_endsÚnr>   Úheavy_horizontalÚlight_horizontalÚheavy_verticalÚlight_verticalÚtop_left_cornerÚtop_right_cornerÚ
top_t_downÚtransition_leftÚtransition_rightÚ
group_joinÚsubgroup_joinÚmid_leftÚ	mid_rightÚ	mid_crossÚbottom_left_cornerÚbottom_right_cornerÚbottom_t_upÚinner_wÚtitleÚ
title_lineÚr1ÚiÚeÚr2Úr3ÚwÚr4ÚsubÚr5Úr6ÚvalÚr7r=   s9                                                           @r   Ú_render_overall_mergedr   K   s"  ø€ óð
 �(�ˆn€GØ�h�ZÐ €Gð à™$˜w {Ñ3Ó4Ð5Ø‘t˜G HÑ-Ó.Ð/Ø‘t˜G HÑ-Ó.Ð/ðð	
ð 
�'™4 ¨Ñ 0Ó1Ð2Ð3Ð4àà‘t˜G D™MÓ*Ð+Ø™˜g kÑ2Ó3Ð4Ø™4 ¨Ñ 1Ó2Ð3ðð	
ð7€Fð& ˜7Ñ"Ø�‰àà™d 7Ð+;Ñ#<Ó=Ð>Ø™4 ¨Ñ 6Ó7Ð8ððô	
ð :@×"QÑ"Q©g¨a°ÈDÒ"QÁDÀAÀq A q¢6Ð"Q 6Ð"Q€DÓ"QØ=A×B±T°Q¸œœS ›V¤S¨£VÓ,¨qÓ0ÓB€Fà
€CØò ‰	ˆˆTÜ�‹YˆÜ�V˜C #¨¡(Ð+Ó,°°Q±Ñ7ˆÜ�S“˜A‘ˆØ�FŠ?Ü˜6 F™?Ó+ò *�Ø�s˜Q ™V‘|Ó$¨Ñ)Ô$ð*àˆr‰	‰ðð )+€EØ
€CØò ‰	ˆˆTÜ�‹YˆØ�‰�c˜3 ™8 a™<¨Ð-Ô.Øˆr‰	‰ðð
 */¨s°¨×4Ð4™˜˜s A’Ð4€HÒ4ÜˆD‹	€Aõ<ð ÐØÐØ€NØ€NØ(4Ñ%€OÐ%Ø€JØ(4Ñ%€OÐ%Ø€JØ€MØ%8Ñ"€Hˆi˜Ø;NÑ8ÐÐ+¨[ä�&‹k˜A‰o Ñ!€GØˆkÐ-Ð.€EØ—‘˜g¨™kÓ*€Jà	€BÜ! %Ó(ò E‰ˆ‰9ˆAˆq�!Ø
Ð¡ q¨!£Ñ,Ñ,ˆØ
˜A¤ E£
¨Q¡Ò.‰jÐ4DÑD‰ðEð 
€BØò 7‰	ˆˆ1ˆcØ
ˆc�j‰j™˜q !›Ó%¨Ñ6Ñ6‰ð7ð 
€BÜ˜&Ó!ò A‰ˆˆ1Ø
Ð Ñ"Ñ"ˆØˆq�1‰u‹9Ø  X¡‘*°=Ñ@‰BðAð Ð
Ñ€Bà	€BÜ  “ò 5‰ˆ‰8ˆC�Ø
ˆc�j‰j˜ ™Ó# nÑ4Ñ4‰ð5ð 
€BÜ˜&Ó!ò 4‰ˆˆ1Ø
Ð Ñ"Ñ"ˆØ
˜1˜q 1™uš9‰i¨)Ñ3‰ð4ð 
€BÜ  “ò 5‰ˆ‰8ˆAˆsØ
ˆc�j‰j˜ ™Ó# nÑ4Ñ4‰ð5ð 
€BÜ˜&Ó!ò @‰ˆˆ1Ø
Ð Ñ"Ñ"ˆØ
˜Q  Q¡šY‰kÐ,?Ñ?‰ð@ð �9‰9�j " b¨"¨b°"°b¸"Ð=Ó>Ð>ùõ[ #RùÛBùô& 5s   Â%N	
Ã+NÆ#Nc                ó
  — t         €J ‚dd„}|g}|rët        €J ‚t        | › d�ddd¬«      }|j                  ddd¬«       |j                  d	d
¬«       |j                  dd
¬«       |j                  dd
¬«       |j                  dd
¬«       |j                  dd
¬«       |D ]H  }|j                  |d    ||d   «       ||d   «       ||d   «       ||d   «       ||d   «      «       ŒJ |j	                  |«       t        |Ž S )aÚ  Build a single Rich renderable for overall and per-class metric tables.

    Args:
        title_pfx: Split label (e.g. ``"Val"`` or ``"Test"``).
        overall_rendered: Pre-rendered overall table string.
        per_class: Per-class dicts with keys ``name``, ``ap``, ``ar``,
            ``f1``, ``precision``, ``recall``; skipped when empty.

    Returns:
        Rich ``Group`` renderable containing the overall table and, when present,
        the per-class table.
    c                ó    — | | k7  s| dk  ry| d›S r'   r(   r)   s    r   r+   z'_build_summary_renderable.<locals>._fmtã   r,   r   u    â€” Per-class Metricsz	bold cyanT)rq   Útitle_styleÚshow_headerÚheader_styleÚClassÚdim)ÚstyleÚno_wrapzAP 50:95Úright)ÚjustifyÚARr2   r4   r5   ÚnameÚapÚarÚf1Ú	precisionÚrecallr@   )r   r   Ú
add_columnÚadd_rowrE   )rL   Úoverall_renderedÚ	per_classr+   ÚrenderablesÚtableÚrows          r   Ú_build_summary_renderabler™   Ð   s9  € ô" ÐÐÐóð
 /Ð/€KÙÜÐ Ð Ð äØ�KÐ5Ð6Ø#ØØ$ô	
ˆð 	×Ñ˜¨°tÐÔ<Ø×Ñ˜¨WÐÔ5Ø×Ñ˜ wÐÔ/Ø×Ñ˜ wÐÔ/Ø×Ñ˜¨gÐÔ6Ø×Ñ˜¨7ÐÔ3Øò 	ˆCØ�M‰MØ�F‘Ù�S˜‘Y“Ù�S˜‘Y“Ù�S˜‘Y“Ù�S˜Ñ%Ó&Ù�S˜‘]Ó#õð	ð 	×Ñ˜5Ô!Ü�+ÐÐr   c                óJ   — t         sy| j                  t        |||«      «       y)a‡  Print overall and per-class metric tables to ``console``.

    Args:
        console: Rich ``Console`` instance to print to.
        title_pfx: Split label (e.g. ``"Val"`` or ``"Test"``).
        overall_rendered: Pre-rendered overall table string.
        per_class: Per-class dicts with keys ``name``, ``ap``, ``ar``,
            ``f1``, ``precision``, ``recall``; skipped when empty.
    N)Ú_IS_RICH_AVAILABLEÚprintr™   )ÚconsolerL   r”   r•   s       r   Ú_render_summary_tablesrž     s!   € õ ØØ‡M�MÔ+¨IÐ7GÈÓSÕTr   )r   r   rB   r   )r   r   rB   Úbool)rL   rC   rM   zdict[str, float]rN   rD   rB   rC   )rL   rC   r”   rC   r•   úlist[dict[str, Any]]rB   r   )
r�   r   rL   rC   r”   rC   r•   r    rB   ÚNone)Ú__doc__Ú
__future__r   Útypingr   Úrich.consoler   r   Ú	rich.liver   Ú
rich.tabler   r›   ÚImportErrorr   r$   r   r™   rž   r(   r   r   ú<module>r©      sÈ   ðñ ;å "å ðß+ÝÝ ð Ðó!(óH
^óB?ðJ2Øð2àð2ð $ð2ð ó	2ðjUØðUàðUð ðUð $ð	Uð
 
ôUøðk ò Ø€GØ€EØ€DØ€EØÒðús   �A ÁAÁA