+
    QV-jé7  ã                   óV  € ^ RI t ^ RIHtHt ^ RIHt ^ RIHtHtH	t	H
t
HtHt ^ RIt^RIHt ^RIHt ^RIHt ^RIHtHtHt ^R	IHtHt ]'       d   ^ RIt^R
IHt ]P@                  '       d$   ^ RIt ! R R]PB                  PD                  4      t#] ! R R4      4       t$RR R llt%R# )é    N)Ú	dataclassÚfield)ÚPath)ÚTYPE_CHECKINGÚAnyÚClassVarÚDictÚOptionalÚUnion)Úconfig)ÚDownloadConfig)Ú
array_cast)Úis_local_pathÚis_remote_urlÚxopen)Úno_op_if_value_is_nullÚstring_to_dict)ÚFeatureTypec                   óH   a a€ ] tR t^t oRtV3R lV 3R lltR tRtVtV ;t	# )ÚNifti1ImageWrapperzI
A wrapper around nibabel's Nifti1Image to customize its representation.
c                óH   <€ V ^8„  d   QhRS[ P                  P                  /# )é   Únifti_image)ÚnibÚnifti1ÚNifti1Image)ÚformatÚ__classdict__s   "€Úh/Volumes/fast/ai/experiments/ui-tars-smoke/.venv/lib/python3.14/site-packages/datasets/features/nifti.pyÚ__annotate__ÚNifti1ImageWrapper.__annotate__   s   ø€ ÷ 		+ñ 		+©¯
©
×(>Ñ(>ñ 		+ó    c           	     ó¾   <€ \         SV `  VP                  VP                  VP                  VP
                  VP                  VP                  4       R 7       Wn        R# ))ÚdataobjÚaffineÚheaderÚextraÚfile_mapÚdtypeN)	ÚsuperÚ__init__r$   r%   r&   r'   r(   Úget_data_dtyper   )Úselfr   Ú	__class__s   &&€r   r+   ÚNifti1ImageWrapper.__init__   sV   ø€ Ü‰GÑØ#×+Ñ+Ø"×)Ñ)Ø"×)Ñ)Ø!×'Ñ'Ø$×-Ñ-Ø!×0Ñ0Ó2ð ô ð  +Ör"   c                ó‚  € ^ RI HpHpHpHp ^ RIHp V P                  P                  4       pV! 4       pVP                  VP                  4       VP                  VP                  n        RVP                  n        RVP                  n        Rp\!        V P                  R4      '       dm   RV P                  P"                  9   dR   \%        V P                  P"                  R,          RR4      e(   V P                  P"                  R,          P&                  pVf   RpV! W†R	7      p	VP)                  V	.4       V! V4       R# )
r   )ÚNiiVueÚ
ShowRenderÚ	SliceTypeÚVolume)ÚdisplayTNr(   ÚimageÚfilenamezvolume.nii.gz)ÚnameÚdata)Ú	ipyniivuer1   r2   r3   r4   ÚIPython.displayr5   r   Úto_bytesÚset_slice_typeÚMULTIPLANARÚALWAYSÚoptsÚmultiplanar_show_renderÚshow_3d_crosshairÚmultiplanar_force_renderÚhasattrr(   Úgetattrr7   Úload_volumes)
r-   r1   r2   r3   r4   r5   Úbytes_Únvr8   Úvolumes
   &         r   Ú_repr_html_ÚNifti1ImageWrapper._repr_html_'   sö   € ßGÓGÝ/à×%Ñ%×.Ñ.Ó0ˆFÙ“ˆBØ×Ñ˜i×3Ñ3Ô4Ø.8×.?Ñ.?ˆB�G‰GÔ+Ø(,ˆB�G‰GÔ%Ø/3ˆB�G‰GÔ,ØˆDÜ�t×'Ñ'¨×4Ò4à˜t×/Ñ/×8Ñ8Ô8Ü × 0Ñ 0× 9Ñ 9¸'Õ BÀJÐPTÓUÒaà×+Ñ+×4Ñ4°WÕ=×FÑF�DØŠ|Ø&�Ù Ô3ˆFØ�O‰O˜V˜HÔ%Ù�BŽKr"   )r   )
Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r+   rJ   Ú__static_attributes__Ú__classdictcell__Ú__classcell__)r.   r   s   @@r   r   r      s   ù‡ € ñ	÷		+ó 		+÷	ò 	r"   r   c                   ó2  a € ] tR t^?t o RtRt]! RRR7      tRt]	P                  ! R]	P                  ! 4       R]	P                  ! 4       /4      t]! R RRR	7      tR
 tV 3R lR ltRV 3R lR lltRV 3R lR lltV 3R lR ltV 3R lR ltV 3R ltRtV tR# )ÚNiftiaq  
**Experimental.**
Nifti [`Feature`] to read NIfTI neuroimaging files.

Input: The Nifti feature accepts as input:
- A `str`: Absolute path to the NIfTI file (i.e. random access is allowed).
- A `pathlib.Path`: path to the NIfTI file (i.e. random access is allowed).
- A `dict` with the keys:
    - `path`: String with relative path of the NIfTI file in a dataset repository.
    - `bytes`: Bytes of the NIfTI file.
  This is useful for archived files with sequential access.

- A `nibabel` image object (e.g., `nibabel.nifti1.Nifti1Image`).

Args:
    decode (`bool`, defaults to `True`):
        Whether to decode the NIfTI data. If `False` a string with the bytes is returned. `decode=False` is not supported when decoding examples.

Examples:

```py
>>> from datasets import Dataset, Nifti
>>> ds = Dataset.from_dict({"nifti": ["path/to/file.nii.gz"]}).cast_column("nifti", Nifti())
>>> ds.features["nifti"]
Nifti(decode=True, id=None)
>>> ds[0]["nifti"]
<nibabel.nifti1.Nifti1Image object at 0x7f8a1c2d8f40>
>>> ds = ds.cast_column("nifti", Nifti(decode=False))
>>> ds[0]["nifti"]
{'bytes': None,
'path': 'path/to/file.nii.gz'}
```
TNF)ÚdefaultÚreprznibabel.nifti1.Nifti1ImageÚbytesÚpath)rV   ÚinitrW   c                ó   € V P                   # ©N)Úpa_type)r-   s   &r   Ú__call__ÚNifti.__call__k   s   € Ø�|‰|Ðr"   c                óF   <€ V ^8„  d   QhRS[ S[S[S[S[R3,          RS[/# )r   Úvalueúnib.Nifti1ImageÚreturn)r   ÚstrrX   Ú	bytearrayÚdict)r   r   s   "€r   r    ÚNifti.__annotate__n   s-   ø€ ÷ &ñ &¡E©#©u±iÁÐGXÐ*XÕ$Yð &Ñ^bñ &r"   c                ó<  € \         P                  '       d   ^ RIpMRp\        V\        4      '       d   RVRR/# \        V\
        4      '       d   R\	        VP                  4       4      RR/# \        V\        \        34      '       d   RRRV/# Ve1   \        WP                  P                  4      '       d   \        V4      # \        V\        4      '       d®   VP                  R4      eB   \        P                  P!                  VR,          4      '       d   RRRVP                  R4      /# VP                  R4      f   VP                  R4      e%   RVP                  R4      RVP                  R4      /# \#        RV R24      h\#        R\%        V4       R24      h)zÏEncode example into a format for Arrow.

Args:
    value (`str`, `bytes`, `nibabel.Nifti1Image` or `dict`):
        Data passed as input to Nifti feature.

Returns:
    `dict` with "path" and "bytes" fields
NrY   rX   zTA nifti sample should have one of 'path' or 'bytes' but they are missing or None in Ú.zPA nifti sample should be a string, bytes, Path, nibabel image, or dict, but got )r   ÚNIBABEL_AVAILABLEÚnibabelÚ
isinstancerd   r   ÚabsoluterX   re   ÚspatialimagesÚSpatialImageÚencode_nibabel_imagerf   ÚgetÚosrY   ÚisfileÚ
ValueErrorÚtype)r-   ra   r   s   && r   Úencode_exampleÚNifti.encode_examplen   sk  € ô ×#×#Ð#Ü!àˆCä�eœS×!Ò!Ø˜E 7¨DÐ1Ð1Ü˜œt×$Ò$ØœC §¡Ó 0Ó1°7¸DÐAÐAÜ˜¤¤yÐ1×2Ò2Ø˜D '¨5Ð1Ð1ØŠ_¤¨E×3DÑ3D×3QÑ3Q×!RÒ!Rä'¨Ó.Ð.Ü˜œt×$Ò$Ø�y‰y˜Ó Ò,´·±·±ÀÀfÅ×1NÒ1Nà  v¨u¯y©y¸Ó/@ÐAÐAØ—‘˜7Ó#Ò/°5·9±9¸VÓ3DÒ3Pà §¡¨7Ó!3°V¸U¿Y¹YÀvÓ=NÐOÐOä ØjÐkpÐjqÐqrÐsóð ô ØbÔcgÐhmÓcnÐboÐopÐqóð r"   c                ó$   <€ V ^8„  d   QhRS[ RR/# )r   ra   rc   r   )rf   )r   r   s   "€r   r    rg   –   s   ø€ ÷ =)ñ =)¡Dð =)ÐEYñ =)r"   c                óâ  € \         P                  '       d   ^ RIpM\        R4      hVf   / pVR,          VR,          rTVEfS   Vf   \	        RV R24      hVP                  R4      '       d@   \        VP                  R4      R,          4      '       d   VP                  R4      R,          p\        V4      '       d   VP                  ! V4      pEMVP                  R4      R,          pVP                  \         P                  4      '       d   \         P                  M\         P                  p\        Wx4      p	V	e   VP                  V	R	,          4      MRp
\        V
R
7      p\        VRVR7      ;_uu_ 4       pVP                  ! V4      pRRR4       M>^ RIpVR,          R8X  d   VP#                  V4      pVP$                  P'                  V4      p\)        X4      #   + '       g   i     L; i)a   Decode example NIfTI file into nibabel image object.

Args:
    value (`str` or `dict`):
        A string with the absolute NIfTI file path, a dictionary with
        keys:

        - `path`: String with absolute or relative NIfTI file path.
        - `bytes`: The bytes of the NIfTI file.

    token_per_repo_id (`dict`, *optional*):
        To access and decode NIfTI files from private repositories on
        the Hub, you can pass a dictionary
        repo_id (`str`) -> token (`bool` or `str`).

Returns:
    `nibabel.Nifti1Image` objects
Nz:To support decoding NIfTI files, please install 'nibabel'.rY   rX   zBA nifti should have one of 'path' or 'bytes' but both are None in ri   zgzip://ú::Úrepo_id©ÚtokenÚrb©Údownload_config:Nr   Ns   ‹éÿÿÿÿ)r   rj   rk   ÚImportErrorrt   Ú
startswithr   ÚsplitÚloadÚHF_ENDPOINTÚHUB_DATASETS_URLÚHUB_DATASETS_HFFS_URLr   rq   r   r   ÚgzipÚ
decompressr   Ú
from_bytesr   )r-   ra   Útoken_per_repo_idr   rY   rG   ÚniftiÚ
source_urlÚpatternÚsource_url_fieldsr}   r€   Úfr‰   s   &&&           r   Údecode_exampleÚNifti.decode_example–   s¢  € ô& ×#×#Ð#Ü!äÐZÓ[Ð[àÒ$Ø "Ðà˜V•} e¨G¥nˆfØ‹>ØŠ|Ü Ð#eÐfkÐelÐlmÐ!nÓoÐoð —?‘? 9×-Ò-´-ÀÇ
Á
È4Ó@PÐQSÕ@T×2UÒ2UØŸ:™: dÓ+¨BÕ/�DÜ  ×&Ò&ØŸHšH T›N’Eà!%§¡¨DÓ!1°"Õ!5�Jð &×0Ñ0´×1CÑ1C×DÒDô ×/Ò/ä#×9Ñ9ð ô
 )7°zÓ(KÐ%àO`ÒOlÐ)×-Ñ-Ð.?À	Õ.JÔKÐrvð ô '5¸5Ô&A�OÜ˜t T¸?×KÕKÈqØ #§¢¨£˜÷ LÐKó ð �r•
˜kÔ)àŸ™¨Ó0�à—O‘O×.Ñ.¨vÓ6ˆEä! %Ó(Ð(÷ L×Kús   Å9GÇG.	c                óZ   <€ V ^8„  d   QhRS[ P                  RS[RS[RS[ P                  /# )r   ÚstorageÚlocal_filesÚremote_filesrc   )ÚpaÚStructArrayÚbool)r   r   s   "€r   r    rg   Õ   s;   ø€ ÷ C1ñ C1Ù—~‘~ðC1ÙLPðC1ÙgkðC1á	�‰ñC1r"   c           	     ó�  a€ Sf   / o\         V3R l4       p\        P                  ! VP                  4        Uu. uFl  pVed   VR,          fP   V'       d   \	        VR,          4      '       g!   V'       d(   \        VR,          4      '       d   V! VR,          4      M
VR,          MRNKn  	  up\        P                  ! 4       R7      p\        P                  ! VP                  R4      P                  4        Uu. uF\  pVeT   V'       d   \	        V4      '       g   V'       d1   \        V4      '       d    \        P                  P                  V4      MTMRNK^  	  up\        P                  ! 4       R7      p	\        P                  P                  Wy.RR.VP                  4       R7      p\        WP                   4      # u upi u upi )a·  Embed NifTI files into the Arrow array.

Args:
    storage (`pa.StructArray`):
        PyArrow array to embed.
    token_per_repo_id (`dict`, optional):
        Dictionary repo_id -> token to fetch the files bytes.
    local_files (`bool`, defaults to `True`)
        Whether to embed local files data in the array

        <Added version="4.8.5"/>
    remote_files (`bool`, defaults to `True`)
        Whether to embed remote files data in the array.
        E.g. files with paths that start with hf:// or https://

        <Added version="4.8.5"/>

Returns:
    `pa.StructArray`: Array in the NifTI arrow storage type, that is
        `pa.struct({"bytes": pa.binary(), "path": pa.string()})`.
Nc                 ó²  <€ V P                  R 4      R,          pVP                  \        P                  4      '       d   \        P                  M\        P
                  p\        W4      pVe   SP                  VR,          4      MRp\        VR7      p\        V RVR7      ;_uu_ 4       pVP                  4       uuRRR4       #   + '       g   i     R# ; i)rz   Nr{   r|   r~   r   r�   )r„   rƒ   r   r†   r‡   rˆ   r   rq   r   r   Úread)rY   rŽ   r�   r�   r}   r€   r‘   rŒ   s   &      €r   Úpath_to_bytesÚ*Nifti.embed_storage.<locals>.path_to_bytesð   s£   ø€ àŸ™ DÓ)¨"Õ-ˆJà+5×+@Ñ+@Ä×ASÑAS×+TÒ+T”×'Ò'ÔZ`×ZvÑZvð ô !/¨zÓ CÐØK\ÒKhÐ%×)Ñ)Ð*;¸IÕ*FÔGÐnrˆEÜ,°5Ô9ˆOÜ�t˜T°?×CÕCÀqØ—v‘v“x÷ D×C×CÓCús   Â*CÃC	rX   rY   ©ru   ©Úmask)r   r˜   ÚarrayÚ	to_pylistr   r   Úbinaryr   rr   rY   ÚbasenameÚstringr™   Úfrom_arraysÚis_nullr   r]   )
r-   r•   rŒ   r–   r—   rž   ÚxÚbytes_arrayrY   Ú
path_arrays
   &&f&&     r   Úembed_storageÚNifti.embed_storageÕ   s”  ø€ ð0 Ò$Ø "Ðä	ô		 ó 
 ð		 ô —h’hð !×*Ñ*Ô,ó
ñ -�Að ’=ð	 ˜•zÒ)ß%¬-¸¸&½	×*BÒ*BÏÔYfÐghÐioÕgp×YqÒYqñ " ! F¥)Ô,ð ˜7žð òñ -ñ
ô —’“ô
ˆô —X’Xð $ŸM™M¨&Ó1×;Ñ;Ô=ó	ñ >�Dð Ò#÷ $¬°d×(;Ò(;ÇÔR_Ð`d×ReÒReô —G‘G×$Ñ$ TÔ*áð òñ >ñ	ô —’“ô
ˆ
ô —.‘.×,Ñ,¨kÐ-FÈÐRXÐHYÐ`g×`oÑ`oÓ`qÐ,ÓrˆÜ˜'§<¡<Ó0Ð0ùò7
ùò	s#   ¹F>ÁF>Á56F>Ã8GÄ	GÄ"8Gc                óH   <€ V ^8„  d   QhRS[ RS[S[R3,          3,          /# )r   rc   r   )r   r	   rd   )r   r   s   "€r   r    rg     s(   ø€ ÷ 
ñ 
™˜}©d±3¸Ð3EÕ.FÐFÕGñ 
r"   c                óZ   € ^RI Hp V P                  '       d   V # RV! R4      RV! R4      /# )zfIf in the decodable state, return the feature itself, otherwise flatten the feature into a dictionary.)ÚValuerX   r¥   rY   r§   )Úfeaturesr±   Údecode)r-   r±   s   & r   ÚflattenÚNifti.flatten  s8   € å#ð �{�{ˆ{ð ð	
ð ™˜x›Ø™˜h›ðð	
r"   c                ó�   <€ V ^8„  d   QhRS[ S[P                  S[P                  S[P                  3,          RS[P                  /# )r   r•   rc   )r   r˜   ÚStringArrayr™   ÚBinaryArray)r   r   s   "€r   r    rg   '  s:   ø€ ÷ "1ñ "1¡E©"¯.©.¹"¿.¹.É"Ï.É.Ð*XÕ$Yð "1Ñ^`×^lÑ^lñ "1r"   c                ó*  € \         P                  P                  VP                  4      '       dr   \         P                  ! R.\        V4      ,          \         P                  ! 4       R7      p\         P                  P                  W!.RR.VP                  4       R7      pEMß\         P                  P                  VP                  4      '       dr   \         P                  ! R.\        V4      ,          \         P                  ! 4       R7      p\         P                  P                  W.RR.VP                  4       R7      pEM?\         P                  P                  VP                  4      '       Ed   VP                  P                  R4      ^ 8¼  d   VP                  R4      pM<\         P                  ! R.\        V4      ,          \         P                  ! 4       R7      pVP                  P                  R4      ^ 8¼  d   VP                  R4      pM<\         P                  ! R.\        V4      ,          \         P                  ! 4       R7      p\         P                  P                  W#.RR.VP                  4       R7      p\        WP                   4      # )aƒ  Cast an Arrow array to the Nifti arrow storage type.
The Arrow types that can be converted to the Nifti pyarrow storage type are:

- `pa.string()` - it must contain the "path" data
- `pa.binary()` - it must contain the NIfTI bytes
- `pa.struct({"bytes": pa.binary()})`
- `pa.struct({"path": pa.string()})`
- `pa.struct({"bytes": pa.binary(), "path": pa.string()})`  - order doesn't matter

Args:
    storage (`Union[pa.StringArray, pa.StructArray, pa.BinaryArray]`):
        PyArrow array to cast.

Returns:
    `pa.StructArray`: Array in the Nifti arrow storage type, that is
        `pa.struct({"bytes": pa.binary(), "path": pa.string()})`.
Nr    rX   rY   r¡   )r˜   ÚtypesÚ	is_stringru   r£   Úlenr¥   r™   r¨   r©   Ú	is_binaryr§   Ú	is_structÚget_field_indexr   r   r]   )r-   r•   r«   r¬   s   &&  r   Úcast_storageÚNifti.cast_storage'  s¶  € ô$ �8‰8×Ñ˜gŸl™l×+Ò+ÜŸ(š( D 6¬C°«LÕ#8¼r¿yºy»{ÔKˆKÜ—n‘n×0Ñ0°+Ð1GÈ'ÐSYÐIZÐah×apÑapÓarÐ0ÓsŠGÜ�X‰X×Ñ §¡×-Ò-ÜŸš 4 &¬3¨w«<Õ"7¼b¿iºi»kÔJˆJÜ—n‘n×0Ñ0°'Ð1FÈÐRXÐHYÐ`g×`oÑ`oÓ`qÐ0ÓrŠGÜ�X‰X×Ñ §¡×-Ó-Ø�|‰|×+Ñ+¨GÓ4¸Ô9Ø%Ÿm™m¨GÓ4‘ä Ÿhšh¨ v´°G³Õ'<Ä2Ç9Â9Ã;ÔO�Ø�|‰|×+Ñ+¨FÓ3°qÔ8Ø$Ÿ]™]¨6Ó2‘
äŸXšX t f¬s°7«|Õ&;Ä"Ç)Â)Ã+ÔN�
Ü—n‘n×0Ñ0°+Ð1JÈWÐV\ÐL]Ðdk×dsÑdsÓduÐ0ÓvˆGÜ˜'§<¡<Ó0Ð0r"   c                ó†   <€ V ^8„  d   Qh/ S[ ;R&   S[S[,          ;R&   S[S[,          ;R&   S[S[,          ;R&   S[;R&   # )r   r³   Úidr)   r]   Ú_type)rš   r
   rd   r   r   )r   r   s   "€r   r    rg   ?   s\   ø‡ ‚ ñH ÑñI ñJ 	‘�Ñ7ñK ñP ‘C�=Ñ7ñQ ñR ‘c�]ÑSñS ñT Ñ?òU r"   © r\   )NTT)rL   rM   rN   rO   rP   r³   r   rÃ   r)   r˜   Ústructr¥   r§   r]   rÄ   r^   rv   r’   r­   r´   rÀ   Ú__annotate_func__rQ   rR   )r   s   @r   rU   rU   ?   sš   ø‡ € ñ ðD €FÙ d°Ô7€Bð 8€EØŸYšY¨°·²³¸fÀbÇiÂiÃkÐ'RÓS€GÙ˜w¨U¸Ô?€Eò÷&ð &÷P=)ò =)÷~C1ò C1÷J
ð 
÷"1ð "1÷Q ƒ r"   rU   c                óŽ   € V ^8„  d   QhRRR\         R\        \        \        \        \        \
        3,          ,          3,          /# )r   Úimgrb   Úforce_bytesrc   )rš   rf   rd   r
   r   rX   )r   s   "r   r    r    L  s@   € ÷ /ñ /Ð/ð /¼dð /ÌtÔTWÔYaÔbgÔhkÔmrÐhrÕbsÕYtÐTtÕOuñ /r"   c                óÄ   € \        V R4      '       d:   V P                  e,   V'       g$   V P                  R,          P                  pRVRR/# V P                  4       pRRRV/# )a‘  
Encode a nibabel image object into a dictionary.

If the image has an associated file path, returns the path. Otherwise, serializes
the image content into bytes.

Args:
    img: A nibabel image object (e.g., Nifti1Image).
    force_bytes: If `True`, always serialize to bytes even if a file path exists. Needed to upload bytes properly.

Returns:
    dict: A dictionary with "path" or "bytes" field.
r(   Nr6   rY   rX   )rD   r(   r7   r<   )rÉ   rÊ   r7   Ú
bytes_datas   &&  r   rp   rp   L  sY   € ô ˆs�J×Ò C§L¡LÒ$<Ç[Ø—<‘< Õ(×1Ñ1ˆØ˜ '¨4Ð0Ð0à—‘“€JØ�D˜' :Ð.Ð.r"   )F)&rr   Údataclassesr   r   Úpathlibr   Útypingr   r   r   r	   r
   r   Úpyarrowr˜   Ú r   Údownload.download_configr   Útabler   Úutils.file_utilsr   r   r   Úutils.py_utilsr   r   rk   r   r²   r   rj   r   r   r   rU   rp   rÅ   r"   r   Ú<module>rÖ      s�   ðÛ 	ß (Ý ß F× Fã å Ý 5Ý ß BÑ Bß C÷ Ûå%à	××ÐÛô%˜SŸZ™Z×3Ñ3ô %ðP ÷I1ð I1ó ðI1÷X/ñ /r"   