§
    ­¸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n# e$ r dZdZdZ	dZd	ZY nw xY wdd„Zd d„Zd!d„Zd"d„Zd#d„ZdS )$z4Rich console helper for the training callback stack.é    )Úannotations)ÚAny)ÚConsoleÚGroup)ÚLive)ÚTableTNFÚtrainerr   Úreturnc                óú   — t           €t          d¦  «        ‚t          | dg ¦  «        D ]D}t          d„ t	          |¦  «        j        D ¦   «         ¦  «        rt          |dd¦  «        }|�|c S ŒEt          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    V — ŒdS )ÚRichProgressBarN)Ú__name__)Ú.0Úclss     úh/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/rfdetr/utilities/console.pyú	<genexpr>z$_get_rich_console.<locals>.<genexpr>7   s*   è è € ÐMÐM°SˆsŒ|Ð0Ò0ÐMÐMÐMÐMÐMÐMó    Ú_consoleT)Úforce_terminal)r   ÚRuntimeErrorÚgetattrÚanyÚtypeÚ__mro__)r	   ÚcbÚ
cb_consoles      r   Ú_get_rich_consoler      s˜   € õ. €ÝØbñ
ô 
ð 	
õ �g˜{¨BÑ/Ô/ð "ð "ˆÝÐMÐM½DÀ¹H¼HÔ<LÐMÑMÔMÑMÔMð 	"Ý   Z°Ñ6Ô6ˆJØÐ%Ø!Ð!Ð!Ð!øÝ $Ð'Ñ'Ô'Ð'r   Úboolc                óV   — 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  óT   K  — | ]#}|j         j                             d ¦  «        V — Œ$dS )ÚProgressBarN)Ú	__class__r   Úendswith)r   Úcallbacks     r   r   z$_has_progress_bar.<locals>.<genexpr>H   s6   è è € Ð]Ð]ÀxˆxÔ!Ô*×3Ò3°MÑBÔBÐ]Ð]Ð]Ð]Ð]Ð]r   )r   r   )r	   r   s     r   Ú_has_progress_barr&   >   s3   € õ ˜ ¨bÑ1Ô1€IÝÐ]Ð]ÐS\Ð]Ñ]Ô]Ñ]Ô]Ð]r   Ú	title_pfxÚstrÚoverallúdict[str, float]Úmax_detsÚintc           
     ó¢  ‡5— d1d„}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;|                      dd	 ||d         ¦  «        fd ||d         ¦  «        fgf¦  «         d„ |D ¦   «         }d„ |D ¦   «         Š5d}|D ]|\  }	}
t          |
¦  «        }t          ‰5|||z   …         ¦  «        |dz
  z   }t          |	¦  «        dz   }||k    r+t          ||z
  ¦  «        D ]}‰5|||z  z   xx         dz  cc<   Œ||z  }Œ}g }d}|D ]7\  }	}
t          |
¦  «        }|                      |||z   dz
  |	f¦  «         ||z  }Œ8d„ |dd…         D ¦   «         }t          |¦  «        }d2ˆ5fd#„}d$}d%}d&}d'}d(\  }}d)}d*\  }}d+}d,}d-\  }}} d.\  }!}"}#t          ‰5¦  «        |z   dz
  }$| › d/�}%|%                     |$dz   ¦  «        }&|}'t          |¦  «        D ]8\  }(\  })}*}+|'| ||)|*¦  «        z  z  }'|'|(t          |¦  «        dz
  k     r|n|z  }'Œ9|},|D ]+\  })}*}	|,|	                      ||)|*¦  «        ¦  «        |z   z  },Œ,|}-t          ‰5¦  «        D ]!\  }(}.|-||.z  z  }-|(|dz
  k     r|-|(|v r|n|z  }-Œ"|-|z  }-|}/t          |¦  «        D ])\  }(\  }0}+|/|0                     ‰5|(         ¦  «        |z   z  }/Œ*|}1t          ‰5¦  «        D ]\  }(}.|1||.z  z  }1|1|(|dz
  k     r| n|z  }1Œ|}2t          |¦  «        D ])\  }(\  }+}3|2|3                     ‰5|(         ¦  «        |z   z  }2Œ*|!}4t          ‰5¦  «        D ]\  }(}.|4||.z  z  }4|4|(|dz
  k     r|#n|"z  }4Œd0                     |&|'|,|-|/|1|2|4g¦  «        S )3u(  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()``.
    ÚvÚfloatr
   r(   c                ó&   — | | k    s| dk     rdS | d›S ©Nr   u   â€”z.4f© ©r.   s    r   Ú_fmtz$_render_overall_merged.<locals>._fmtZ   ó"   € Ø�Š6ˆ6�Q˜’U�UØ�5ØˆzˆzÐ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 50c                ó,   — g | ]\  }}|D ]	\  }}||f‘Œ
ŒS r2   r2   )r   Ú_ÚcolsÚsr.   s        r   ú
<listcomp>z*_render_overall_merged.<locals>.<listcomp>€   s1   € Ð"QÐ"QÐ"Q©g¨a°ÈDÐ"QÐ"QÁDÀAÀq A q 6Ð"QÐ"QÐ"QÐ"Qr   c                ón   — g | ]2\  }}t          t          |¦  «        t          |¦  «        ¦  «        d z   ‘Œ3S )é   )ÚmaxÚlen)r   rB   r.   s      r   rC   z*_render_overall_merged.<locals>.<listcomp>�   s7   € ÐBÐBÐB±T°Q¸��S ™VœV¥S¨¡V¤VÑ,Ô,¨qÑ0ÐBÐBÐBr   r   é   rE   c                ó   — h | ]\  }}}|’Œ	S r2   r2   )r   ÚstartÚendr@   s       r   ú	<setcomp>z)_render_overall_merged.<locals>.<setcomp>”   s   € Ð4Ð4Ð4™˜˜s A�Ð4Ð4Ð4r   NéÿÿÿÿrJ   r,   rK   c                óD   •— t          ‰| |dz   …         ¦  «        || z
  z   S )z:Return merged cell width for columns start..end inclusive.rH   )Úsum)rJ   rK   Ú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.   r/   r
   r(   )rJ   r,   rK   r,   r
   r,   )ÚappendrG   rO   ÚrangeÚcenterÚ	enumerateÚjoin)6r'   r)   r+   r4   Úmar_lblÚmar_keyÚgroupsÚflatÚcolÚgrprA   ÚncÚcell_wÚneededÚkÚspansÚgrp_endsÚnrQ   Ú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ÚirB   Úer@   Úr2Úr3ÚwÚr4ÚsubÚr5Úr6ÚvalÚr7rP   s6                                                        @r   Ú_render_overall_mergedr†   K   s¤  ø€ ðð ð ð ð
 �(ˆnˆn€GØ �hÐ Ð €Gð à˜$˜$˜w {Ô3Ñ4Ô4Ð5Ø�t�t˜G HÔ-Ñ.Ô.Ð/Ø�t�t˜G HÔ-Ñ.Ô.Ð/ðð	
ð 
�'˜4˜4 ¨Ô 0Ñ1Ô1Ð2Ð3Ð4àà�t�t˜G DœMÑ*Ô*Ð+Ø˜˜˜g kÔ2Ñ3Ô3Ð4Ø˜4˜4 ¨Ô 1Ñ2Ô2Ð3ðð	
ð7€Fð& ˜7Ð"Ð"Ø�Šàà˜d˜d 7Ð+;Ô#<Ñ=Ô=Ð>Ø˜4˜4 ¨Ô 6Ñ7Ô7Ð8ððñ	
ô 	
ð 	
ð #RÐ"Q¸Ð"QÑ"QÔ"Q€DØBÐB¸TÐBÑBÔB€Fà
€CØð ð ‰	ˆˆTÝ�‰YŒYˆÝ�V˜C #¨¡(˜NÔ+Ñ,Ô,°°Q±Ñ7ˆÝ�S‘”˜A‘ˆØ�FŠ?ˆ?Ý˜6 F™?Ñ+Ô+ð *ð *�Ø�s˜Q ™V‘|Ð$Ð$Ô$¨Ñ)Ð$Ð$Ñ$Ð$Øˆr‰	ˆˆà(*€EØ
€CØð ð ‰	ˆˆTÝ�‰YŒYˆØ�Š�c˜3 ™8 a™<¨Ð-Ñ.Ô.Ð.Øˆr‰	ˆˆà4Ð4¨¨s°¨s¬Ð4Ñ4Ô4€HÝˆD‰	Œ	€Að<ð <ð <ð <ð <ð <ð ÐØÐØ€NØ€NØ(4Ñ%€OÐ%Ø€JØ(4Ñ%€OÐ%Ø€JØ€MØ%8Ñ"€Hˆi˜Ø;NÑ8ÐÐ+¨[å�&‰kŒk˜A‰o Ñ!€GØÐ.Ð.Ð.€EØ—’˜g¨™kÑ*Ô*€Jà	€BÝ! %Ñ(Ô(ð Eð E‰ˆ‰9ˆAˆq�!Ø
Ð   q¨!¡¤Ñ,Ñ,ˆØ
˜A¥ E¡
¤
¨Q¡Ò.Ð.ˆjˆjÐ4DÑDˆˆà	€BØð 7ð 7‰	ˆˆ1ˆcØ
ˆc�jŠj˜˜˜q !™œÑ%Ô%¨Ñ6Ñ6ˆˆà	€BÝ˜&Ñ!Ô!ð Að A‰ˆˆ1Ø
Ð Ñ"Ñ"ˆØˆq�1‰uŠ9ˆ9Ø  X  �*�*°=Ñ@ˆBøØÐ
Ñ€Bà	€BÝ  ‘”ð 5ð 5‰ˆ‰8ˆC�Ø
ˆc�jŠj˜ œÑ#Ô# nÑ4Ñ4ˆˆà	€BÝ˜&Ñ!Ô!ð 4ð 4‰ˆˆ1Ø
Ð Ñ"Ñ"ˆØ
˜1˜q 1™uš9˜9ˆiˆi¨)Ñ3ˆˆà	€BÝ  ‘”ð 5ð 5‰ˆ‰8ˆAˆsØ
ˆc�jŠj˜ œÑ#Ô# nÑ4Ñ4ˆˆà	€BÝ˜&Ñ!Ô!ð @ð @‰ˆˆ1Ø
Ð Ñ"Ñ"ˆØ
˜Q  Q¡šY˜YˆkˆkÐ,?Ñ?ˆˆà�9Š9�j " b¨"¨b°"°b¸"Ð=Ñ>Ô>Ð>r   Úoverall_renderedÚ	per_classúlist[dict[str, Any]]r   c                ó˜  — t           €J ‚dd„}|g}|�r/t          €J ‚t          | › d�dd	d¬
¦  «        }|                     ddd	¬¦  «         |                     dd¬¦  «         |                     dd¬¦  «         |                     dd¬¦  «         |                     dd¬¦  «         |                     dd¬¦  «         |D ]m}|                     |d          ||d         ¦  «         ||d         ¦  «         ||d         ¦  «         ||d         ¦  «         ||d         ¦  «        ¦  «         Œn|                     |¦  «         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.
    Nr.   r/   r
   r(   c                ó&   — | | k    s| dk     rdS | d›S r1   r2   r3   s    r   r4   z'_build_summary_renderable.<locals>._fmtã   r5   r   u    â€” Per-class Metricsz	bold cyanT)rx   Útitle_styleÚshow_headerÚheader_styleÚClassÚdim)ÚstyleÚno_wrapzAP 50:95Úright)ÚjustifyÚARr;   r=   r>   ÚnameÚapÚarÚf1Ú	precisionÚrecallrS   )r   r   Ú
add_columnÚadd_rowrT   )r'   r‡   rˆ   r4   ÚrenderablesÚtableÚrows          r   Ú_build_summary_renderabler¡   Ð   s§  € õ" ÐÐÐðð ð ð ð
 /Ð/€KØñ "ÝÐ Ð Ð åØÐ6Ð6Ð6Ø#ØØ$ð	
ñ 
ô 
ˆð 	×Ò˜¨°tÐÑ<Ô<Ð<Ø×Ò˜¨WÐÑ5Ô5Ð5Ø×Ò˜ wÐÑ/Ô/Ð/Ø×Ò˜ wÐÑ/Ô/Ð/Ø×Ò˜¨gÐÑ6Ô6Ð6Ø×Ò˜¨7ÐÑ3Ô3Ð3Øð 	ð 	ˆCØ�MŠMØ�F”Ø��S˜”Y‘”Ø��S˜”Y‘”Ø��S˜”Y‘”Ø��S˜Ô%Ñ&Ô&Ø��S˜”]Ñ#Ô#ñô ð ð ð 	×Ò˜5Ñ!Ô!Ð!Ý�+ÐÐr   ÚconsoleÚNonec                ó`   — t           sdS |                      t          |||¦  «        ¦  «         dS )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¡   )r¢   r'   r‡   rˆ   s       r   Ú_render_summary_tablesr§     s7   € õ ð ØˆØ‡M‚MÕ+¨IÐ7GÈÑSÔSÑTÔTÐTÐTÐTr   )r	   r   r
   r   )r	   r   r
   r   )r'   r(   r)   r*   r+   r,   r
   r(   )r'   r(   r‡   r(   rˆ   r‰   r
   r   )
r¢   r   r'   r(   r‡   r(   rˆ   r‰   r
   r£   )Ú__doc__Ú
__future__r   Útypingr   Úrich.consoler   r   Ú	rich.liver   Ú
rich.tabler   r¥   ÚImportErrorr   r&   r†   r¡   r§   r2   r   r   ú<module>r¯      s9  ðð ;Ð :à "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ð Ð ðØ+Ð+Ð+Ð+Ð+Ð+Ð+Ð+ØÐÐÐÐÐØ Ð Ð Ð Ð Ð ð ÐÐøð ð ð ð Ø€GØ€EØ€DØ€EØÐÐÐðøøøð!(ð !(ð !(ð !(ðH
^ð 
^ð 
^ð 
^ðB?ð B?ð B?ð B?ðJ2ð 2ð 2ð 2ðjUð Uð Uð Uð Uð Us   �' §9¸9