§
    ß©ºjÕ0  ã                  óB  — U d Z ddlmZ ddlZddlZddlmZmZmZ ddl	m
Z
 ddlmZmZ ddlmZ dZd	ed
<   dZd	ed<   dZded<   dZd	ed<    e
dd¬¦  «         G d„ d¦  «        ¦   «         Z e
dd¬¦  «         G d„ d¦  «        ¦   «         Zd.d„Zd/d$„Zd0d)„Zdd*œd1d-„ZdS )2uî  Python-side partition spec builder (ADR-0046 Â§"Panoptic / semantic
partitioning").

The instance paradigm reaches partitioned eval through the locked
spine: matching runs once, accumulate+summarize fans out over image
subsets (the C3 axiom). The panoptic and semantic paradigms compute
their summaries from per-image accumulations rather than from an
AP-shaped accumulator tensor; cleanly subsetting that pipeline would
require refactoring both summarize stages. As a pragmatic
phase-1 fallback (per ADR-0046 Â§"Performance"), the panoptic /
semantic Python wrappers loop the unchanged single-eval over each
slice's image subset and feed the resulting metrics back through the
shared :func:`slices_batch_panoptic` / :func:`slices_batch_semantic`
Arrow builders. This is closer to the C1 path than C3; LVIS-scale
panoptic / semantic users who need C3 performance for partitioned
eval should file an issue.

This module owns the manifest â†’ slice list resolution shared by both
paradigm wrappers. Mirrors ``vernier_core::partition::PartitionSpec``
verbatim:

- canonical slice order is ``(axis ascending, value ascending,
  __unassigned__ last)``;
- every axis gets an ``__unassigned__`` slice for dataset images not
  covered by any of that axis's manifest values;
- ``cross_axes`` opts in joint cells via the ``::`` cross-separator
  (per ADR-0046 Â§E2);
- a hard cap of 256 total slices matches ``SLICES_CAP`` in the Rust
  spec builder.

The bit-identical-overall contract (ADR-0046's load-bearing parity
claim, since pycocotools has no slicing notion) is preserved by
construction: the ``overall`` summary is computed by running the
unchanged un-partitioned ``evaluate(...)`` over the full GT/DT pair,
i.e. the same code path the user would call without ``manifest=``.
é    )ÚannotationsN)ÚIterableÚMappingÚSequence)Ú	dataclass)ÚAnyÚFinal)Úmanifest_to_json_bytesÚ__unassigned__z
Final[str]Ú
UNASSIGNEDz::ÚCROSS_SEPARATORé   z
Final[int]Ú
SLICES_CAPÚ1ÚMANIFEST_VERSIONT)ÚfrozenÚslotsc                  ó2   — e Zd ZU dZded<   ded<   ded<   dS )ÚPartitionSliceuþ   One slice in a Python-side panoptic / semantic partition spec.

    Mirrors ``vernier_core::partition::Slice`` minus the
    ``image_indices`` field (the Python loop operates on image ids
    directly â€” there is no flat I-axis to resolve against).
    ÚstrÚaxisÚvalueúfrozenset[int]Ú	image_idsN©Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__© ó    úg/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/vernier/_partition_spec.pyr   r   A   s<   € € € € € € ðð ð €I€I�IØ€J€J�JØÐÐÑÐÐr"   r   c                  ó   — e Zd ZU dZded<   dS )ÚPartitionSpecz}Resolved partition spec consumed by the panoptic / semantic
    per-slice loop. Slices are pre-sorted in the canonical order.ztuple[PartitionSlice, ...]ÚslicesNr   r!   r"   r#   r%   r%   O   s,   € € € € € € ðEð Eð 'Ð&Ð&Ñ&Ð&Ð&r"   r%   ÚmanifestÚobjectÚknown_image_idsr   Úreturnú6tuple[dict[str, dict[str, frozenset[int]]], list[str]]c               óÈ  — t          | ¦  «        }t          j        |¦  «        }t          |t          ¦  «        st          d¦  «        ‚|}|                     d¦  «        }|t          k    rt          d|›dt          ›�¦  «        ‚|                     d¦  «        }|dk    rt          d|›d�¦  «        ‚|                     d	g ¦  «        }t          |t          ¦  «        st          d
¦  «        ‚|}d}	i }
g }t          |¦  «        D �]½\  }}t          |t          ¦  «        st          d|› d|›�¦  «        ‚|}d|vrt          d|› d�¦  «        ‚	 t          |d         ¦  «        }n6# t          t
          f$ r"}t          d|› d|d         ›d�¦  «        |‚d}~ww xY wt          d„ |D ¦   «         ¦  «        }|st          d|› d�¦  «        ‚|D ]&}t          |v rt          d|›dt          ›d�¦  «        ‚Œ'|	€|}	n|	|k    rt          d|› d|›d|	›d�¦  «        ‚||vr$|                     t!          |¦  «        ¦  «         �Œ9|D ]�}||         }t          |t           ¦  «        st          d|› d|›d|›d�¦  «        ‚|
                     |i ¦  «                             |t%          ¦   «         ¦  «                             |¦  «         Œ‚�Œ¿d„ |
                     ¦   «         D ¦   «         }||fS )a  Resolve any of the four ``manifest=`` shapes (dict, JSON path,
    Arrow PyCapsule, canonical JSON dict) into a
    ``per_axis[axis][value] -> {image_ids}`` mapping plus a list of
    unknown-key warnings.

    Defers shape coercion to the FFI's ``manifest_to_json_bytes``
    helper so all four shapes land on the same canonical JSON wire
    form. Manifest rows whose ``key`` is absent from
    ``known_image_ids`` are skipped with a warning string (the caller
    surfaces them via :func:`warnings.warn`).
    zmanifest must be a JSON objectÚmanifest_versionzunsupported manifest_version z; expected Úkey_kindÚimage_idz=partitioned eval consumes key_kind="image_id" manifests; got zH. A key_kind="result" manifest must be routed through vernier.aggregate.Úrowszmanifest 'rows' must be a listNzrow z is not a JSON object: Úkeyz is missing the 'key' columnz: key z is not an integer image idc              3  ó@   K  — | ]}|d k    ¯t          |¦  «        V — ŒdS )r1   N)r   )Ú.0Úks     r#   ú	<genexpr>z,_parse_canonical_manifest.<locals>.<genexpr>‘   s,   è è € Ð$MÐ$M°À!ÀuÂ*À*¥S¨¡V¤VÀ*À*À*À*Ð$MÐ$Mr"   zA has no axis columns; the manifest must declare at least one axiszmanifest axis z! contains the reserved separator z; rename the columnz axes z differ from first row z"; vernier rejects ragged manifestsz axis z: value z is not a stringc                óR   — i | ]$\  }}|d „ |                      ¦   «         D ¦   «         “Œ%S )c                ó4   — i | ]\  }}|t          |¦  «        “ŒS r!   )Ú	frozenset)r3   ÚvÚidss      r#   ú
<dictcomp>z8_parse_canonical_manifest.<locals>.<dictcomp>.<dictcomp>®   s$   € Ð<Ð<Ð<¡6 1 cˆQ•	˜#‘”Ð<Ð<Ð<r"   )Úitems)r3   ÚaxÚvaluess      r#   r;   z-_parse_canonical_manifest.<locals>.<dictcomp>­   sB   € ð 4ð 4ð 4ÙAKÀÀVˆÐ<Ð<¨V¯\ª\©^¬^Ð<Ñ<Ô<ð4ð 4ð 4r"   )r
   ÚjsonÚloadsÚ
isinstanceÚdictÚ
ValueErrorÚgetr   ÚlistÚ	enumerater   ÚintÚ	TypeErrorÚsortedr   Úappendr   Ú
setdefaultÚsetÚaddr<   )r'   r)   Ú
bytes_blobÚdoc_anyÚdocÚversionr.   Úrows_objr0   Ú
axis_namesÚper_axisÚwarnings_seenÚrow_idxÚrowÚ	row_typedr/   ÚeÚrow_axesr=   r   r   s                        r#   Ú_parse_canonical_manifestr[   W   sñ  € õ  (¨Ñ1Ô1€Jõ ”:˜jÑ)Ô)€GÝ�g�tÑ$Ô$ð ;ÝÐ9Ñ:Ô:Ð:ð "€Cà�gŠgÐ(Ñ)Ô)€GØÕ"Ò"Ð"ÝÐc¸ÐcÐcÕO_ÐcÐcÑdÔdÐdØ�wŠw�zÑ"Ô"€HØ�:ÒÐÝð*Øð*ð *ð *ñ
ô 
ð 	
ð
 —G’G˜F BÑ'Ô'€HÝ�h¥Ñ%Ô%ð ;ÝÐ9Ñ:Ô:Ð:Ø€Dà#'€JØ/1€HØ!€Må! $™œð &Oñ &O‰ˆ�Ý˜#�wÑ'Ô'ð 	MÝÐK GÐKÐKÀCÐKÐKÑLÔLÐLØ'*ˆ	Ø˜	Ð!Ð!ÝÐI GÐIÐIÐIÑJÔJÐJð	Ý˜9 UÔ+Ñ,Ô,ˆHˆHøÝ�:Ð&ð 	ð 	ð 	ÝØU�wÐUÐU i°Ô&6ÐUÐUÐUñô àðøøøøð	øøøõ %Ð$MÐ$M°YÐ$MÑ$MÔ$MÑMÔMˆØð 	ÝØa�wÐaÐaÐañô ð ð ð 	ð 	ˆBÝ "Ð$Ð$Ý ð> Rð >ð >Ý&ð>ð >ð >ñô ð ð %ð
 ÐØ!ˆJˆJØ˜8Ò#Ð#ÝðD�wð Dð D hð Dð DØðDð Dð Dñô ð ð ˜?Ð*Ð*Ø× Ò ¥ X¡¤Ñ/Ô/Ð/ÙØð 	Oð 	OˆBØ" 2œˆEÝ˜e¥SÑ)Ô)ð `Ý Ð!^¨Ð!^Ð!^°rÐ!^Ð!^ÀUÐ!^Ð!^Ð!^Ñ_Ô_Ð_Ø×Ò  BÑ'Ô'×2Ò2°5½#¹%¼%Ñ@Ô@×DÒDÀXÑNÔNÐNÐNñ		Oð4ð 4ØOWÏ~Ê~ÑO_ÔO_ð4ñ 4ô 4€Fð �=Ð Ð s   ÅE#Å#FÅ4FÆFrT   ú*Mapping[str, Mapping[str, frozenset[int]]]Ú
cross_axesúSequence[Sequence[str]]ÚNonec                ó¢  — |D ]Ë}t          |¦  «        dk     r t          dt          |¦  «        ›d�¦  «        ‚t          ¦   «         }|D ]…}|| vrDt          dt          |¦  «        ›d|›dt	          |                      ¦   «         ¦  «        ›�¦  «        ‚||v r"t          dt          |¦  «        ›d|›�¦  «        ‚|                     |¦  «         Œ†ŒÌdS )z¡Mirror ``vernier_core::partition::validate_cross_axes``: each
    tuple must have >=2 axes, every name must be a known axis, no
    repeated axes within a tuple.é   z--cross tuple zO must name at least two axes; a single-axis cross is a marginal and is rejectedz names unknown axis z; known axes: z repeats axis N)ÚlenrC   rE   rL   rI   ÚkeysrM   )rT   r]   ÚtupÚseenr=   s        r#   Ú_validate_cross_axesrf   ³   s  € ð ð ð ˆÝˆs‰8Œ8�aŠ<ˆ<ÝðE¥ c¡¤ð Eð Eð Eñô ð õ ™œˆØð 	ð 	ˆBØ˜Ð!Ð!Ý ð?¥T¨#¡Y¤Yð ?ð ?Àbð ?ð ?Ý#)¨(¯-ª-©/¬/Ñ#:Ô#:ð?ð ?ñô ð ð �TˆzˆzÝ Ð!Sµ$°s±)´)Ð!SÐ!SÈRÐ!SÐ!SÑTÔTÐTØ�HŠH�R‰LŒLˆLˆLð	ðð r"   ÚaxesúSequence[str]Úall_image_idsúlist[PartitionSlice]c                óŽ  ‡— t                                | ¦  «        }ˆfd„| D ¦   «         }g g}|D ])}g }|D ] }|D ]}	|                     g |¢|	‘¦  «         ŒŒ!|}Œ*g }
t          ¦   «         }|D ]†}|sŒ|d         d         }|dd…         D ]
\  }}||z  }Œ|                     |¦  «         t                                d„ |D ¦   «         ¦  «        }|
                     t          |||¬¦  «        ¦  «         Œ‡|t          |¦  «        z
  }|
                     t          |t          |¬¦  «        ¦  «         |
                     d„ ¬¦  «         |
S )	zèCartesian-product the per-axis value sets and intersect the
    image-id sets to produce one joint cell per combination. Adds a
    final ``__unassigned__`` joint bucket for dataset images covered by
    no joint cell of this tuple.c                ód   •— g | ],}t          ‰|                              ¦   «         d „ ¬¦  «        ‘Œ-S )c                ó   — | d         S )Nr   r!   )Úkvs    r#   ú<lambda>z1_expand_cross_slices.<locals>.<listcomp>.<lambda>Ù   s
   € °B°q´E€ r"   ©r1   )rI   r<   )r3   r=   rT   s     €r#   ú
<listcomp>z(_expand_cross_slices.<locals>.<listcomp>Ø   sG   ø€ ð ?ð ?ð ?Ø?A�ˆx˜Œ|×!Ò!Ñ#Ô#Ð)9Ð)9Ð:Ñ:Ô:ð?ð ?ð ?r"   r   é   Nc              3  ó    K  — | ]	\  }}|V — Œ
d S ©Nr!   )r3   r9   Ú_s      r#   r5   z'_expand_cross_slices.<locals>.<genexpr>ï   s&   è è € Ð+@Ð+@±$°!°Q¨AÐ+@Ð+@Ð+@Ð+@Ð+@Ð+@r"   ©r   r   r   c                ó   — | j         | j        fS rt   )r   r   )Úss    r#   ro   z&_expand_cross_slices.<locals>.<lambda>÷   s   € ˜AœF A¤GÐ,€ r"   rp   )	r   ÚjoinrJ   rL   Úupdater   r8   r   Úsort)rg   rT   ri   Újoined_axisÚper_axis_sortedÚcombosÚaxis_valuesÚnext_combosÚcomboÚentryÚoutÚcoveredÚ	joint_idsru   r:   Újoined_valueÚmissings    `               r#   Ú_expand_cross_slicesrˆ   Ì   s®  ø€ õ "×&Ò& tÑ,Ô,€Kð?ð ?ð ?ð ?ØEIð?ñ ?ô ?€Oð
 79°T€FØ&ð ð ˆØ>@ˆØð 	4ð 	4ˆEØ$ð 4ð 4�Ø×"Ò" ? U ?¨E ?Ñ3Ô3Ð3Ð3ð4àˆˆà "€CÝ™œ€GØð 	^ð 	^ˆØð 	Øà$)¨!¤H¨Q¤Kˆ	Ø˜A˜B˜B”ið 	(ð 	(‰FˆAˆsØ! C™ˆIˆIØ�Š�yÑ!Ô!Ð!Ý&×+Ò+Ð+@Ð+@¸%Ð+@Ñ+@Ô+@Ñ@Ô@ˆØ�
Š
•> {¸,ÐR[Ð\Ñ\Ô\Ñ]Ô]Ð]Ð]Ø�i¨Ñ0Ô0Ñ0€GØ‡J‚J�~ ;µjÈGÐTÑTÔTÑUÔUÐUð
 ‡H‚HÐ,Ð,€HÑ-Ô-Ð-Ø€Jr"   )r]   úIterable[int]úSequence[Sequence[str]] | Nonec          	     óž  — t          |¦  «        }t          | |¬¦  «        \  }}|D ]"}t          j        d|›d�t          d¬¦  «         Œ#|€g }nd„ |D ¦   «         }t          ||¦  «         g }t          |                     ¦   «         ¦  «        D ]º}	||	         }
t          ¦   «         }t          |
                     ¦   «         ¦  «        D ]D}|
|         }| 	                    |¦  «         | 
                    t          |	||¬¦  «        ¦  «         ŒE|t          |¦  «        z
  }| 
                    t          |	t          |¬¦  «        ¦  «         Œ»g }|D ]&}|                     t          |||¦  «        ¦  «         Œ't          |¦  «        t          |¦  «        z   }|t           k    rt#          d	|› d
t           › d�¦  «        ‚g |¢|¢R }t%          |¬¦  «        S )a…  Resolve a manifest input (any of the four shapes) plus the live
    dataset image-id set into a :class:`PartitionSpec`.

    Emits :class:`UserWarning` for manifest rows whose key is absent
    from ``all_image_ids`` (the "no silent data loss" discipline from
    ADR-0046). The warning text names the offending key so the user
    can locate the stale row in their manifest workbook.
    )r)   zmanifest row key=z( is absent from the dataset; row skippedé   )Ú
stacklevelNc                ó,   — g | ]}t          |¦  «        ‘ŒS r!   )rE   )r3   Úts     r#   rq   zbuild_spec.<locals>.<listcomp>  s   € Ð7Ð7Ð7 q�4 ™7œ7Ð7Ð7Ð7r"   rv   zpartition would produce z slices but the cap is z*; reduce cross_axes or narrow the manifest)r&   )r8   r[   ÚwarningsÚwarnÚUserWarningrf   rI   rc   rL   rz   rJ   r   r   Úextendrˆ   rb   r   rC   r%   )r'   ri   r]   ÚknownrT   Úunknown_keysr4   Úcross_axes_normÚmarginalr   r>   r„   r   r:   r‡   Újointrd   Útotalr&   s                      r#   Ú
build_specrš   û   s  € õ �mÑ$Ô$€EÝ6°xÐQVÐWÑWÔWÑ€HˆlØð 
ð 
ˆÝŒØM ÐMÐMÐMÝØð	
ñ 	
ô 	
ð 	
ð 	
ð ÐØ+-ˆˆà7Ð7¨JÐ7Ñ7Ô7ˆÝ˜ ?Ñ3Ô3Ð3à%'€HÝ�x—}’}‘”Ñ'Ô'ð Xð XˆØ˜$”ˆÝ™EœEˆÝ˜FŸKšK™MœMÑ*Ô*ð 	Sð 	SˆEØ˜”-ˆCØ�NŠN˜3ÑÔÐØ�OŠO�N°¸EÈSÐQÑQÔQÑRÔRÐRÐRØ�) GÑ,Ô,Ñ,ˆØ�Š�¨D½
ÈgÐVÑVÔVÑWÔWÐWÐWà"$€EØð Að AˆØ�ŠÕ)¨#¨x¸Ñ?Ô?Ñ@Ô@Ð@Ð@å�‰MŒM�C ™JœJÑ&€EØ�zÒÐÝð8 uð 8ð 8ÅZð 8ð 8ð 8ñ
ô 
ð 	
ð
 !ˆxÐ ˜%Ð Ð €FÝ Ð'Ñ'Ô'Ð'r"   )r'   r(   r)   r   r*   r+   )rT   r\   r]   r^   r*   r_   )rg   rh   rT   r\   ri   r   r*   rj   )r'   r(   ri   r‰   r]   rŠ   r*   r%   )r   Ú
__future__r   r?   r�   Úcollections.abcr   r   r   Údataclassesr   Útypingr   r	   Úvernier._corer
   r   r    r   r   r   r   r%   r[   rf   rˆ   rš   r!   r"   r#   ú<module>r       sÙ  ðð#ð #ð #ðJ #Ð "Ð "Ð "Ð "Ð "à €€€Ø €€€Ø 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ø !Ð !Ð !Ð !Ð !Ð !Ø Ð Ð Ð Ð Ð Ð Ð à 0Ð 0Ð 0Ð 0Ð 0Ð 0ð *€
Ð )Ð )Ð )Ñ )ð #€Ð "Ð "Ð "Ñ "ð €
Ð Ð Ð Ñ ð  #Ð Ð "Ð "Ð "Ñ "ð €�$˜dÐ#Ñ#Ô#ð
ð 
ð 
ð 
ð 
ñ 
ô 
ñ $Ô#ð
ð €�$˜dÐ#Ñ#Ô#ð'ð 'ð 'ð 'ð 'ñ 'ô 'ñ $Ô#ð'ðY!ð Y!ð Y!ð Y!ðxð ð ð ð2,ð ,ð ,ð ,ðf 26ð	4(ð 4(ð 4(ð 4(ð 4(ð 4(ð 4(ð 4(r"   