� ���gLr�� �UdZddlZddlZddlZddlZddlmZmZddlmZ ddlmZ ddl m Z m Z mZmZddlmZmZddlmZddlmZmZmZmZmZmZddlZddlZddl Z!ddl"m#Z$ddl%Z dd l&m'Z(dd l&m)Z*d d l+m,Z,d d l-m.Z.m/Z/d dl0m1Z1d dl2m3Z3m4Z4d dl5m6Z6m7Z7m8Z8ddl9m:Z:ddl;m<Z<m=Z=ddl>m?Z?ddl@mAZAmBZBddlCmDZDe4jEeF��ZGde!jHdeIfd�ZJdeIde!jHfd�ZKdedeLdeLdeMeeLffd�ZNdsdedefd"�ZOe Gd#�d$����ZPGd%�d&��ZQe Gd'�d(eQ����ZRe Gd)�d*eQ����ZSe Gd+�d,eQ����ZTe Gd-�d.eQ����ZUGd/�d0e!jV��ZWGd1�d2eW��ZXGd3�d4eW��ZYGd5�d6eW��ZZGd7�d8eW��Z[e!j\eXd9d:����e!j\eYd;d:����e!j\eZd<d:����e!j\e[d=d:����dtd>e!jHd?eLdeLfd@�Z]GdA�dBe!j'��Z^GdC�dDe*��Z_GdE�dFe(��Z`dG�Zae GdH�dI����Zbe GdJ�dK����Ze GdL�dM����ZceedeeeMePebeAeBeceeReSeTeUe:e<eDe?fZfdudNeefdeLfdO�ZgdNefde!jHfdP�ZhdvdQ�ZidudReedeIeeIeLdfffdS�ZjePjFePebjFebeAjFeAeBjFeBecjFecejFeeRjFeReSjFeSeTjFeTeUjFeUe:jFe:e<jFe<eDjFeDe?jFe?iZkedeIeffeldT<e3dUemdVeIfdW���ZndefdX�Zod>e!jHdeffdY�ZpdudZejqd[e!jHde!jrfd\�Zsd]eeee!jtde!jrfd^�Zudud]eeejqd[e!jHde!jrfd_�Zvd`efda�Zwdud`eejqeefd[e!jHde!jrfdb�Zxd`ed>eWde!jtfdc�Zyddefdeeefgeeffdeffdf�ZzeeeeIedfZ{gfddefdeeefe{geeffdge{deffdh�Z|dtddefdieLdeLfdj�Z}ddefdeLfdk�Z~ddefdeLfdl�Zdm�Z�Gdn�doed��Z�dpeee�deee�fdq�Z�dpeee�fdr�Z�dS)wz[This class handle features definition in datasets and some utilities to display table type.�N)�Iterable�Mapping)�Sequence)�InitVar� dataclass�field�fields)�reduce�wraps)�mul)�Any�Callable�ClassVar�Literal�Optional�Union)�ExtensionArray)�ExtensionDtype�)�config)�camelcase_to_snakecase�snakecase_to_camelcase)� array_cast)� experimental�logging)�asdict�first_non_null_value�zip_dict�)�Audio)�Image�encode_pil_image)�Pdf)� Translation�TranslationVariableLanguages)�Video� arrow_type�returnc�p�tj�|��rdStj�|��rdStj�|��rdStj�|��rdStj�|��rdStj�|��rdStj�|��rdStj� |��rdStj� |��rd Stj� |��rd Stj� |��rd Stj� |��rd Stj�|��rd Stj�|��r*dt!jt%|����j�d�Stj�|��r*dt!jt%|����j�d�Stj�|��r?|j� d|j�d�S|jrd|j�d|j�d�St/d|�d����tj�|��rdStj�|��rdStj�|��r d|j�d�Stj�|��rd|j�d|j�d�Stj�|��rd|j�d|j�d�Stj�|��rdStj� |��rdStj�!|��rdStj�"|��rd Stj�#|��rtI|j%��St/d!|�d"����)#z� _arrow_to_datasets_dtype takes a pyarrow.DataType and converts it to a datasets string dtype. In effect, `dt == string_to_arrow(_arrow_to_datasets_dtype(dt))` �null�bool�int8�int16�int32�int64�uint8�uint16�uint32�uint64�float16�float32�float64ztime32[�]ztime64[Nz timestamp[z, tz=zUnexpected timestamp object �.�date32�date64z duration[z decimal128(�, �)z decimal256(�binary� large_binary�string� large_stringz Arrow type z+ does not have a datasets dtype equivalent.)&�pyarrow�types�is_null� is_boolean�is_int8�is_int16�is_int32�is_int64�is_uint8� is_uint16� is_uint32� is_uint64� is_float16� is_float32� is_float64� is_time32�pa�type_for_alias�str�unit� is_time64� is_timestamp�tz� ValueError� is_date32� is_date64� is_duration� is_decimal128� precision�scale� is_decimal256� is_binary�is_large_binary� is_string�is_large_string� is_dictionary�_arrow_to_datasets_dtype� value_type)r's �j/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/datasets/features/features.pyrere5s�� �}���Z�(�(�:`��v� �� !� !�*� -� -�8`��v� �� � �z� *� *�6`��v� �� � � � +� +�4`��w� �� � � � +� +�2`��w� �� � � � +� +�0`��w� �� � � � +� +�.`��w� �� � �� ,� ,�,`��x� �� � �� ,� ,�*`��x� �� � �� ,� ,�(`��x� �� !� !�*� -� -�&`��y� �� !� !�*� -� -�$`��y� �� !� !�*� -� -�"`��y� �� � �� ,� ,� `�C��*�3�z�?�?�;�;�@�C�C�C�C� �� � �� ,� ,�`�C��*�3�z�?�?�;�;�@�C�C�C�C� �� #� #�J� /� /�`� �=� �2� ��2�2�2� 2� �]� K�F� ��F�F�j�m�F�F�F� F��I�J�I�I�I�J�J� J� �� � �� ,� ,�`��x� �� � �� ,� ,�`��x� �� "� "�:� .� .�`�-�:�?�-�-�-�-� �� $� $�Z� 0� 0�`�H�Z�1�H�H�Z�5E�H�H�H�H� �� $� $�Z� 0� 0� `�H�Z�1�H�H�Z�5E�H�H�H�H� �� � �� ,� ,� `��x� �� &� &�z� 2� 2� `��~� �� � �� ,� ,�`��x� �� &� &�z� 2� 2�`��~� �� $� $�Z� 0� 0�`�'� �(=�>�>�>��^�z�^�^�^�_�_�_��datasets_dtypec ���d1d�}|tjvrtj|��S|dztjvrtj|dz��Stjd|��}|r�|�d��}tjd|��}|dvrtj|��S|r;tj|�d��|�d����St ||d d d gd g� �����tjd|��}|rL|�d��}|dvrtj|��St ||dddgdg� �����tjd|��}|r�|�d��}|dkr?|�d��} | dvrtj| ��St | �d����|dkr?|�d��} | dvrtj | ��St | �d����t ||dddgddg� �����tjd|��} | �r�| �d��} | d kr�tjd!| �d����} | rY| �d��} | �d��}tj t| ��t|����St ||d"d#d$gd%g� �����| d&kr�tjd!| �d����} | rY| �d��} | �d��}tj t| ��t|����St ||d'd(d)gd*g� �����t ||d+d,d-gd%d*g� �����t d.|�d/|dz�d0����)2a string_to_arrow takes a datasets string dtype and converts it to a pyarrow.DataType. In effect, `dt == string_to_arrow(_arrow_to_datasets_dtype(dt))` This is necessary because the datasets.Value() primitive type is constructed using a string dtype Value(dtype=str) But Features.type (via `get_nested_type()` expects to resolve Features into a pyarrow Schema, which means that each Value() must be able to resolve into a corresponding pyarrow.DataType, which is the purpose of this function. Nc�R�|�d|�d�}|rMt|��dkr)d�|dd���dz|dzn|d}|d|�d �z }|rMt|��dkr)d�|dd���d z|dzn|d}|d |�d �z }|S) NzA is not a validly formatted string representation of the pyarrow z type.rr;�����z or rz Valid examples include: r8� and z For more insformation, see: )�len�join)�dtype�pa_dtype�examples�urls�msgs rg�_dtype_error_msgz)string_to_arrow.<locals>._dtype_error_msg�s����i�i�Ya�i�i�i�� � <�KN�x�=�=�[\�K\�K\�t�y�y��#�2�#��/�/�&�8�8�B�<�G�G�bj�kl�bm�H� �;��;�;�;� ;�C� � <�@C�D� � �A� � �4�9�9�T�#�2�#�Y�'�'�'�1�D��H�<�<�SW�XY�SZ�D� �;�D�;�;�;� ;�C�� rh�_z^timestamp\[(.*)\]$rz)^(s|ms|us|ns),\s*tz=([a-zA-Z0-9/_+\-:]*)$)�s�ms�us�nsr� timestampz timestamp[us]z!timestamp[us, tz=America/New_YorkzEhttps://arrow.apache.org/docs/python/generated/pyarrow.timestamp.html)rrrsz^duration\[(.*)\]$�durationz duration[s]z duration[us]zDhttps://arrow.apache.org/docs/python/generated/pyarrow.duration.htmlz^time(.*)\[(.*)\]$�32)rwrxzc is not a valid unit for the pyarrow time32 type. Supported units: s (second) and ms (millisecond).�64)ryrzzh is not a valid unit for the pyarrow time64 type. Supported units: us (microsecond) and ns (nanosecond).�timez time32[s]z time64[us]zBhttps://arrow.apache.org/docs/python/generated/pyarrow.time32.htmlzBhttps://arrow.apache.org/docs/python/generated/pyarrow.time64.htmlz^decimal(.*)\((.*)\)$�128z^(\d+),\s*(-?\d+)$� decimal128zdecimal128(10, 2)zdecimal128(4, -2)zFhttps://arrow.apache.org/docs/python/generated/pyarrow.decimal128.html�256� decimal256zdecimal256(30, 2)zdecimal256(38, -4)zFhttps://arrow.apache.org/docs/python/generated/pyarrow.decimal256.html�decimalzdecimal128(12, 3)zdecimal256(40, 6)zNeither z nor z� seems to be a pyarrow data type. Please make sure to use a correct data type, see: https://arrow.apache.org/docs/python/api/datatypes.html#factory-functions)NN) rQ�__dict__�re�search�groupr{rXr|�time32�time64r��intr�)riru�timestamp_matches�timestamp_internals�internals_matches�duration_matches�duration_internals� time_matches�time_internals_bits�time_internals_unit�decimal_matches�decimal_internals_bits�%decimal_internals_precision_and_scaler]r^s rg�string_to_arrowr�ws���������$�$��{�>�*�,�,�,������,�,��{�>�C�/�0�2�2�2�� �"8�.�I�I����/�5�5�a�8�8���I�&R�Tg�h�h�� �"9� 9� 9��<� 3�4�4� 4� � ��<� 1� 7� 7�� :� :�<M�<S�<S�TU�<V�<V�W�W� W�� � �"��-�/R�S�a�b� ����� ��y�!6��G�G��� �-�3�3�A�6�6�� �!8� 8� 8��;�1�2�2� 2�� � �"��+�^�<�`�a� ����� ��9�2�N�C�C�L���*�0�0��3�3�� �$� &� &�".�"4�"4�Q�"7�"7� �"�k�1�1��y�!4�5�5�5� �*�P�P�P����!�D� (� (�".�"4�"4�Q�"7�"7� �"�l�2�2��y�!4�5�5�5� �*�U�U�U����� � �"��)�<�8�\�\�� ��� � � ��i� 8�.�I�I�O��+�!0�!6�!6�q�!9�!9�� !�U� *� *�46�I�>S�Ud�Uj�Uj�kl�Um�Um�4n�4n� 1�4� �A�G�G��J�J� �=�C�C�A�F�F���}�S��^�^�S��Z�Z�@�@�@� �$�$�&�$�"5�7J�!K�f�g� ������$�u� ,� ,�46�I�>S�Ud�Uj�Uj�kl�Um�Um�4n�4n� 1�4� �A�G�G��J�J� �=�C�C�A�F�F���}�S��^�^�S��Z�Z�@�@�@� �$�$�&�$�"5�7K�!L�f�g� ������� � �"��1�3F�G�`�`�� ��� � � � � U�>� U� U���(<� U� U� U� � �rh�obj�only_1d_for_numpy�optimize_list_castingc�\ ���tjrdtjvrddl}tjrdtjvrddl}tjrdtjvrddlm }tj rdtjvrddl }t|tj��r7|jdkr |ddfS�r |jd kr|d fS��fd �|D��dfStj�rvdtjv�rgt||j���rQ|j|jkrht'|����|j������ ����� ��ddfS|jdkr@|������� ��ddfS�r |jd kr:|������� ��dfS��fd �|������� ��D��dfStjr�dtjvr�t||j��rm|jdkr|� ��ddfS�r |jd kr|� ��dfS��fd�|� ��D��dfStjr�dtjvr�t||j��rm|jdkrtj|��ddfS�r |jd krtj|��dfS��fd�tj|��D��dfStj r9dtjvr+t||jj��rt5|��dfSt|t6j��r,t'|������ ��ddfSt|t6j��r6��fd�|�d��� ��D��dfSt|t6j!��r|�"��dfSt|t6j#��r|�$��dfSt|tJ��rYt|tL�� }i}|� ��D]$\} } t'| ��� ��\} } || z}| || <�%|r|n||fStO|d��rDtj(|��r|d fSt'|�)����� ��ddfSt|tTtVf��r�tY|��dkrq|D]} t[| ��rn�t'| ��� ��\}}|s�s��fd�|D��dfSt|tTtVf��r|d fStU|��dfS|d fS|d fS)a Cast pytorch/tensorflow/pandas objects to python numpy array/lists. It works recursively. If `optimize_list_casting` is True, to avoid iterating over possibly long lists, it first checks (recursively) if the first element that is not None or empty (if it is a sequence) has to be casted. If the first element needs to be casted, then all the elements of the list will be casted, otherwise they'll stay the same. This trick allows to cast objects that contain tokenizers outputs without iterating over every single token for example. Args: obj: the object (nested struct) to cast. only_1d_for_numpy (bool): whether to keep the full multi-dim tensors as multi-dim numpy arrays, or convert them to nested lists of 1-dimensional numpy arrays. This can be useful to keep only 1-d arrays to instantiate Arrow arrays. Indeed Arrow only support converting 1-dimensional array values. optimize_list_casting (bool): whether to optimize list casting by checking the first non-null element to see if it needs to be casted and if it doesn't, not checking the rest of the list elements. Returns: casted_obj: the casted object has_changed (bool): True if the object has been changed, False if it is identical � tensorflowrN�torch�jax�PIL�TrFc�@��g|]}t|�����d��S��r�r�r��_cast_to_python_objects��.0�xr�r�s ��rg� <listcomp>z+_cast_to_python_objects.<locals>.<listcomp>5�K�������,��->�Vk��������rhr�c�@��g|]}t|�����d��Sr�r�r�s ��rgr�z+_cast_to_python_objects.<locals>.<listcomp>Jr�rhc�@��g|]}t|�����d��Sr�r�r�s ��rgr�z+_cast_to_python_objects.<locals>.<listcomp>Yr�rhc�@��g|]}t|�����d��Sr�r�r�s ��rgr�z+_cast_to_python_objects.<locals>.<listcomp>hr�rhc�H��i|]\}}|t|�����d��Sr�r�)r��key�valuer�r�s ��rg� <dictcomp>z+_cast_to_python_objects.<locals>.<dictcomp>{sR��� � � ��C���,��->�Vk������ � � rh�series� __array__c�@��g|]}t|�����d��Sr�r�)r��elmtr�r�s ��rgr�z+_cast_to_python_objects.<locals>.<listcomp>�sK������!�0� �4E�]r��������rh).r� TF_AVAILABLE�sys�modulesr��TORCH_AVAILABLEr�� JAX_AVAILABLE� jax.numpy�numpy� PIL_AVAILABLE� PIL.Image� isinstance�np�ndarray�ndim�Tensorrp�bfloat16r��detach�to�float�cpu�asarrayr!r"�pd�Series�tolist� DataFrame�to_dict�items� Timestamp� to_pydatetime� Timedelta�to_pytimedeltar�dict�hasattr�isscalarr��list�tuplern�#_check_non_null_non_empty_recursive)r�r�r��tfr��jnpr�� has_changed�output�k�v�casted_v� has_changed_v� first_elmt�casted_first_elmt�has_changed_first_elmts `` rgr�r� sP����,�� �|�s�{�:�:����� ���'�S�[�"8�"8�� � � � �� ���� 4� 4������� ������ 4� 4������#�r�z�"�"�G� �8�q�=�=��r�7�D�=� �"� �c�h�!�m�m���:� ������!� ��� �� � � �x�G�s�{�$:�$:�z�#�u�|�?\�?\�$:� �9��� &� &�*�� � � � ���� �,�,�0�0�2�2�8�8�:�:�"3�&;����� �� � � �8�q�=�=��:�:�<�<�#�#�%�%�+�+�-�-�b�1�4�7� 7�"� �c�h�!�m�m��:�:�<�<�#�#�%�%�+�+�-�-�t�3� 3������!�Z�Z�\�\�-�-�/�/�5�5�7�7� ��� �� � � �c����!<�!<��C�QS�QZ�A[�A[�!<� �8�q�=�=��9�9�;�;�r�?�D�(� (�"� �c�h�!�m�m��9�9�;�;��$� $������!�Y�Y�[�[� ��� �� � � �T�%�3�;�"6�"6�:�c�3�;�;W�;W�"6� �8�q�=�=��:�c�?�?�2�&��,� ,�"� �c�h�!�m�m��:�c�?�?�D�(� (������ �Z��_�_� ��� �� � � �E�%�3�;�"6�"6�:�c�3�9�?�;[�;[�"6���$�$�d�*�*� �C��� #� #�C� #�� � � � �0A�Yn� � � �� � �  � � �C��� &� &�<� � � � � �#&�+�+�h�"7�"7�"=�"=�"?�"?�  � � � � � � �C��� &� &�2�� � �"�"�D�(�(� �C��� &� &�0��!�!�#�#�T�)�)� �C�� !� !�.�$�S�$�/�/�/� ����I�I�K�K� !� !�D�A�q�&=��%6�Nc�'�'�'� #�H�m� �=� (�K� �F�1�I�I�$�-�v�v�#�{�:�:� ��k� "� "�$� �;�s� � � ���:� �(��M�M�O�O�7H�`u������� � � �C�$��� '� '�� �s�8�8�a�<�<�!� � � �6�z�B�B���E��8O��.?�Wl�9�9�9� 5� �5�&� +�-B� +������%(� ��� ����c�D�%�=�1�1�+���:�%���9�9�d�?�*���:� ��E�z�rhFTc�2�t|||���dS)a� Cast numpy/pytorch/tensorflow/pandas objects to python lists. It works recursively. If `optimize_list_casting` is True, To avoid iterating over possibly long lists, it first checks (recursively) if the first element that is not None or empty (if it is a sequence) has to be casted. If the first element needs to be casted, then all the elements of the list will be casted, otherwise they'll stay the same. This trick allows to cast objects that contain tokenizers outputs without iterating over every single token for example. Args: obj: the object (nested struct) to cast only_1d_for_numpy (bool, default ``False``): whether to keep the full multi-dim tensors as multi-dim numpy arrays, or convert them to nested lists of 1-dimensional numpy arrays. This can be useful to keep only 1-d arrays to instantiate Arrow arrays. Indeed Arrow only support converting 1-dimensional array values. optimize_list_casting (bool, default ``True``): whether to optimize list casting by checking the first non-null element to see if it needs to be casted and if it doesn't, not checking the rest of the list elements. Returns: casted_obj: the casted object r�rr�)r�r�r�s rg�cast_to_python_objectsr��s,��( #� �0�H]� � � �� � rhc��eZdZUdZeed<dZeeed<dZe e ed<e ddd���Z eed<d �Z d �Zd �ZdS) �Valuea� Scalar feature value of a particular data type. The possible dtypes of `Value` are as follows: - `null` - `bool` - `int8` - `int16` - `int32` - `int64` - `uint8` - `uint16` - `uint32` - `uint64` - `float16` - `float32` (alias float) - `float64` (alias double) - `time32[(s|ms)]` - `time64[(us|ns)]` - `timestamp[(s|ms|us|ns)]` - `timestamp[(s|ms|us|ns), tz=(tzstring)]` - `date32` - `date64` - `duration[(s|ms|us|ns)]` - `decimal128(precision, scale)` - `decimal256(precision, scale)` - `binary` - `large_binary` - `string` - `large_string` Args: dtype (`str`): Name of the data type. Example: ```py >>> from datasets import Features >>> features = Features({'stars': Value(dtype='int32')}) >>> features {'stars': Value(dtype='int32', id=None)} ``` rpN�id�pa_typeF��default�init�repr�_typec��|jdkrd|_|jdkrd|_t|j��|_dS)N�doubler6r�r5)rpr�r���selfs rg� __post_init__zValue.__post_init__s?�� �:�� !� !�"�D�J� �:�� � �"�D�J�&�t�z�2�2�� � � rhc��|jS�N�r�r�s rg�__call__zValue.__call__ � ���|�rhc��tj�|j��rt |��Stj�|j��rt |��Stj�|j��rt|��Stj� |j��rt|��S|Sr�) rQrBrDr�r+� is_integerr�� is_floatingr�rbrS�r�r�s rg�encode_examplezValue.encode_examples��� �8� � �t�|� ,� ,� ���;�;� � �X� � ��� .� .� ��u�:�:� � �X� !� !�$�,� /� /� ���<�<� � �X� � �� � -� -� ��u�:�:� ��Lrh)�__name__� __module__� __qualname__�__doc__rS�__annotations__r�rr�rr rr�r�r�r�r�rhrgr�r��s��������+�+�Z �J�J�J��B��� ����!�G�X�c�]�!�!�!���w�U��?�?�?�E�3�?�?�?�3�3�3���� � � � � rhr�c� �eZdZd�Zd�Zd�ZdS)�_ArrayXDc�8�t|j��|_dSr�)r��shaper�s rgr�z_ArrayXD.__post_init__s���4�:�&�&�� � � rhc�p�t��|jjdz|j|j��}|S)N� ExtensionType)�globals� __class__r�rrp)r�r�s rgr�z_ArrayXD.__call__!s1��F�'�)�)�D�N�3�o�E�F�t�z�SW�S]�^�^���rhc��|Sr�r�r�s rgr�z_ArrayXD.encode_example%s��� rhN)r�rrr�r�r�r�rhrgrrsA������'�'�'��������rhrc�h�eZdZUdZeed<eed<dZeeed<e ddd���Z eed<dS) �Array2Da6Create a two-dimensional array. Args: shape (`tuple`): Size of each dimension. dtype (`str`): Name of the data type. Example: ```py >>> from datasets import Features >>> features = Features({'x': Array2D(shape=(1, 3), dtype='int32')}) ``` rrpNr�Fr�r�� r�rrrr�rrSr�rrr�r�rhrgrr)�e��������� �L�L�L� �J�J�J��B��� ������y�u�5�A�A�A�E�3�A�A�A�A�Arhrc�h�eZdZUdZeed<eed<dZeeed<e ddd���Z eed<dS) �Array3Da;Create a three-dimensional array. Args: shape (`tuple`): Size of each dimension. dtype (`str`): Name of the data type. Example: ```py >>> from datasets import Features >>> features = Features({'x': Array3D(shape=(1, 2, 3), dtype='int32')}) ``` rrpNr�Fr�r�rr�rhrgrrBrrhrc�h�eZdZUdZeed<eed<dZeeed<e ddd���Z eed<dS) �Array4Da=Create a four-dimensional array. Args: shape (`tuple`): Size of each dimension. dtype (`str`): Name of the data type. Example: ```py >>> from datasets import Features >>> features = Features({'x': Array4D(shape=(1, 2, 2, 3), dtype='int32')}) ``` rrpNr�Fr�r�rr�rhrgrr[rrhrc�h�eZdZUdZeed<eed<dZeeed<e ddd���Z eed<dS) �Array5Da@Create a five-dimensional array. Args: shape (`tuple`): Size of each dimension. dtype (`str`): Name of the data type. Example: ```py >>> from datasets import Features >>> features = Features({'x': Array5D(shape=(1, 2, 2, 3, 3), dtype='int32')}) ``` rrpNr�Fr�r�rr�rhrgrrtrrhrc�t�eZdZUdZeeed<dedefd�Z d�Z e d���Z d�Z d �Zd �Zd �Zd �ZdS) �_ArrayXDExtensionTypeN�ndimsrrpc��|j� |jdkrtd���t|��|jkrtd|�d|j�d����td|j��D]}||�td|������t |��|_||_|�|j��|_tj � ||j|j j �d|j j����dS)NrzCYou must instantiate an array type with a value for dim that is > 1zshape=z and ndims=z don't matchz3Support only dynamic size on first dimension. Got: r8)rrXrn�ranger�rrf�_generate_dtype� storage_dtyperQr �__init__r rr�)r�rrp�dims rgrz_ArrayXDExtensionType.__init__�s�� �:� ���q����b�c�c� c� �u�:�:��� #� #��P�e�P�P�� �P�P�P�Q�Q� Q���D�J�'�'� `� `�C��S�z�!� �!^�W\�!^�!^�_�_�_�"��5�\�\�� ����!�1�1�$�/�B�B��� ��!�!�$��(:�t�~�?X�<t�<t�[_�[i�[r�<t�<t�u�u�u�u�urhc�f�tj|j|jf�����Sr�)�json�dumpsrrf�encoder�s rg�__arrow_ext_serialize__z-_ArrayXDExtensionType.__arrow_ext_serialize__�s'���z�4�:�t��7�8�8�?�?�A�A�Arhc�4�tj|��}||�Sr�)r!�loads)�cls� storage_type� serialized�argss rg�__arrow_ext_deserialize__z/_ArrayXDExtensionType.__arrow_ext_deserialize__�s���z�*�%�%���s�D�z�rhc�F�|j|j|���ffSr�)r+r(r$r�s rg� __reduce__z _ArrayXDExtensionType.__reduce__�s$���-��0A�4�C_�C_�Ca�Ca�/b�b�brhc�D�t|j|j|jf��Sr�)�hashr rrfr�s rg�__hash__z_ArrayXDExtensionType.__hash__�s���T�^�T�Z���A�B�B�Brhc��tSr�)�ArrayExtensionArrayr�s rg�__arrow_ext_class__z)_ArrayXDExtensionType.__arrow_ext_class__�s��"�"rhc�z�t|��}t|j��D]}tj|��}�|Sr�)r��reversedrrQ�list_)r�rp�ds rgrz%_ArrayXDExtensionType._generate_dtype�s>����&�&���$�*�%�%� $� $�A��H�U�O�O�E�E�� rhc�*�t|j��Sr�)�PandasArrayExtensionDtyperfr�s rg�to_pandas_dtypez%_ArrayXDExtensionType.to_pandas_dtype�s��(���9�9�9rh)r�rrrrr�rr�rSrr$� classmethodr+r-r0r3rr:r�rhrgrr�s���������E�8�C�=���� v�e� v�C� v� v� v� v�B�B�B�����[�� c�c�c�C�C�C�#�#�#����:�:�:�:�:rhrc��eZdZdZdS)�Array2DExtensionTyperN�r�rrrr�rhrgr=r=�������� �E�E�Erhr=c��eZdZdZdS)�Array3DExtensionType�Nr>r�rhrgrArA�r?rhrAc��eZdZdZdS)�Array4DExtensionType�Nr>r�rhrgrDrD�r?rhrDc��eZdZdZdS)�Array5DExtensionType�Nr>r�rhrgrGrG�r?rhrG)rrr/)rrrB)rrrBrE)rrrBrErHr��unnestc���dtjdtjf�fd� �|r �|��}tj�|��o>tj�|��ptj�|�� S)a When converting a pyarrow array to a numpy array, we must know whether this could be done in zero-copy or not. This function returns the value of the ``zero_copy_only`` parameter to pass to ``.to_numpy()``, given the type of the pyarrow array. # zero copy is available for all primitive types except booleans and temporal types (date, time, timestamp or duration) # primitive types are types for which the physical representation in arrow and in numpy # https://github.com/wesm/arrow/blob/c07b9b48cf3e0bbbab493992a492ae47e5b04cad/python/pyarrow/types.pxi#L821 # see https://arrow.apache.org/docs/python/generated/pyarrow.Array.html#pyarrow.Array.to_numpy # and https://issues.apache.org/jira/browse/ARROW-2871?jql=text%20~%20%22boolean%20to_numpy%22 r�r(c�f��tj�|��r�|j��S|Sr�)rQrB�is_listrf)r��_unnest_pa_types �rgrMz+_is_zero_copy_only.<locals>._unnest_pa_type�s3��� �8� � �G� $� $� 7�"�?�7�#5�6�6� 6��rh)rQ�DataTyperB� is_primitiverD� is_temporal)r�rIrMs @rg�_is_zero_copy_onlyrQ�s���������������� �+�!�/�'�*�*�� �8� � �� )� )� q�2�8�3F�3F�w�3O�3O�3p�SU�S[�Sg�Sg�ho�Sp�Sp�.q�qrhc�(�eZdZd�Zd�Zdd�Zd�ZdS)r2c�d�t|jjd���}|�|���S)NT�rI��zero_copy_only)rQ�storage�type�to_numpy)r�rVs rgr�zArrayExtensionArray.__array__�s-��+�D�L�,=�d�K�K�K���}�}�N�}�;�;�;rhc��|j|Sr��rW)r��is rg� __getitem__zArrayExtensionArray.__getitem__�s���|�A��rhTc ���|j}|����d���}|jjd��0d}t jt|����|t jt j|����z }t|jj ��D]+}||jj|z}|� ��}�,|�|���}|j t|��t|��z g|jj�R�}t|��r?t j |�t j��|t jd���}�nu|jj}|jj } g} t jd�|jD����} t'|��D]�\}} | r | �t j���'|||dz�} | |dz| |z }t| ��D]}| � ��} �| �|���}| �|j |g|dd��R�����tt jt j| ������dkr0t jt| ��t0���}| |dd�<nt j| ��}|S)NFrUrr��axisc�6�g|]}|�����Sr�)�as_py)r��offs rgr�z0ArrayExtensionArray.to_numpy.<locals>.<listcomp>s ��)Q�)Q�)Q�#�#�)�)�+�+�)Q�)Q�)Qrh�rp)rWrCrYrXrr��arangern�sumrr�flatten�reshape�insert�astyper6�nan�array�offsets� enumerate�append�unique�diff�empty�object)r�rVrW� null_mask�size� null_indicesr\� numpy_arrrr�arrays�first_dim_offsetsrC� storage_el� first_dimrvs rgrYzArrayExtensionArray.to_numpy�s��� $� ���O�O�%�%�.�.�e�.�D�D� � �9�?�1� � )��D��9�S��\�\�2�2�9�=�� �"�&�QZ�J[�J[�@\�@\�\�L��4�9�?�+�+� ,� ,���� ���*�*��!�/�/�+�+����(�(��(�G�G�I�)� �)�#�d�)�)�c�,�6G�6G�*G�Z�$�)�/�Z�Z�Z�I��<� � � b��I�i�&6�&6�r�z�&B�&B�L�RT�RX�_`�a�a�a� ���I�O�E��I�O�E��F� "��)Q�)Q���)Q�)Q�)Q� R� R� �'� �2�2� L� L� ��7�� L��M�M�"�&�)�)�)�)�!(��Q��U��!3�J� 1�!�a�%� 8�;L�Q�;O� O�I�"�5�\�\�:�:��%/�%7�%7�%9�%9� � � *� 3� 3�>� 3� R� R�I��M�M�"3�)�"3�I�"J��a�b�b� �"J�"J�"J�K�K�K�K��2�9�R�W�%6�7�7�8�8�9�9�A�=�=��H�S��[�[��?�?�?� �%� �!�!�!� � ��H�V�,�,� ��rhc� �t|jjd���}|�|���}|jjd�.|jt krd�|���D��S|���S)NTrTrUrc�6�g|]}|�����Sr�)r��r��arrs rgr�z1ArrayExtensionArray.to_pylist.<locals>.<listcomp> s ��?�?�?�S�C�J�J�L�L�?�?�?rh)rQrWrXrYrrprsr�)r�rVrws rg� to_pylistzArrayExtensionArray.to_pylistsy��+�D�L�,=�d�K�K�K���M�M��M�@�@� � �9�?�1� � %�)�/�V�*C�*C�?�?�I�,<�,<�,>�,>�?�?�?� ?��#�#�%�%� %rhN)T)r�rrr�r]rYr�r�rhrgr2r2�sV������<�<�<����*�*�*�*�X&�&�&�&�&rhr2c���eZdZdZdedejffd�Zdeej ej ffd�Z e d���Z edefd���Zedefd���Zedefd ���Zedejfd ���Zd S) r9rfc��||_dSr��� _value_type)r�rfs rgrz"PandasArrayExtensionDtype.__init__(s��%����rhrlc�,�t|tj��r;|j�tjd�|jD������}t|jjd���}|� |���}t|��S)Nc��g|] }|j�� Sr�r[)r��chunks rgr�z<PandasArrayExtensionDtype.__from_arrow__.<locals>.<listcomp>-s��;d�;d�;d�e�E�M�;d�;d�;drhTrTrU) r�rQ� ChunkedArrayrX� wrap_array� concat_arrays�chunksrQrWrY�PandasArrayExtensionArray)r�rlrVrws rg�__from_arrow__z(PandasArrayExtensionDtype.__from_arrow__+s��� �e�R�_� -� -� g��J�)�)�"�*:�;d�;d�W\�Wc�;d�;d�;d�*e�*e�f�f�E�+�E�M�,>�t�L�L�L���N�N�.�N�A�A� �(��3�3�3rhc��tSr�)r�)r's rg�construct_array_typez.PandasArrayExtensionDtype.construct_array_type2s��(�(rhr(c��tjSr�)r�r�r�s rgrXzPandasArrayExtensionDtype.type6s ���z�rhc��dS)N�Or�r�s rg�kindzPandasArrayExtensionDtype.kind:s���srhc��d|j�d�S)Nzarray[r7)rfr�s rg�namezPandasArrayExtensionDtype.name>s��*���*�*�*�*rhc��|jSr�r�r�s rgrfz$PandasArrayExtensionDtype.value_typeBs ����rhN)r�rr� _metadatarr�rprrQ�Arrayr�r�r;r��propertyrXrSr�r�rfr�rhrgr9r9%s#�������I�&�5�)D�b�h�)N�#O�&�&�&�&�4�E�"�(�B�O�*C�$D�4�4�4�4��)�)��[�)���d�����X����c�����X���+�c�+�+�+��X�+�� �B�H� � � ��X� � � rhr9c ��eZdZddejdefd�Zdd�Zddeddfd �Ze dd e e deddfd ���Z e d e dddfd ���Zede fd���Zedefd���Zdejfd�Zdeeeejfdeddfd�Zdeeeejfdeejdffd�Z dde edededdfd�Zdefd�Zdejfd�ZdS) r�F�data�copyc�r�|s|ntj|��|_t|j��|_dSr�)r�rl�_datar9rp�_dtype)r�r�r�s rgrz"PandasArrayExtensionArray.__init__Hs/��!%�9�T�T�2�8�D�>�>�� �/�� �;�;�� � � rhNc�H�|tjt��krctjt |j��t���}t t |j����D]}|j|||<�|S|�|jS|j�|��S)a� Convert to NumPy Array. Note that Pandas expects a 1D array when dtype is set to object. But for other dtypes, the returned shape is the same as the one of ``data``. More info about pandas 1D requirement for PandasExtensionArray here: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.api.extensions.ExtensionArray.html#pandas.api.extensions.ExtensionArray rd)r�rprsrrrnr�rrj)r�rp�outr\s rgr�z#PandasArrayExtensionArray.__array__Ls��� �B�H�V�$�$� $� $��(�3�t�z�?�?�&�9�9�9�C��3�t�z�?�?�+�+� '� '����A���A����J� �=��:� ��:�$�$�U�+�+� +rh�deepr(c�.�t|jd���S)NT�r�)r�r�)r�r�s rgr�zPandasArrayExtensionArray.copy`s��(���$�?�?�?�?rhrpc���t���dkr<t�fd��D����r!tj�|�|n|j|���}n/tjt���t ���}�|dd�<|||���S)Nrc3��K�|]J}t|tj��o+|j�djko|j�djkV��KdS�rN)r�r�r�rrp)r�r��scalarss �rg� <genexpr>z;PandasArrayExtensionArray._from_sequence.<locals>.<genexpr>gsi�����$ �$ �jk�J�q�"�*� %� %� e�!�'�W�Q�Z�5E�*E� e�!�'�U\�]^�U_�Ue�Je�$ �$ �$ �$ �$ �$ rh)rpr�rdr�)rn�allr�rlrfrrrs)r'r�rpr�r�s ` rg�_from_sequencez(PandasArrayExtensionArray._from_sequencecs���� �w�<�<�!� � ��$ �$ �$ �$ �ov�$ �$ �$ �! �! � ��8�G�E�M�5�5�u�GW�^b�c�c�c�D�D��8�C��L�L��7�7�7�D��D����G��s�4�d�#�#�#�#rh� to_concatc�*��t���dkr:t�fd��D����rtjd��D����}n9tjt���t ���}d��D��|dd�<||d���S)Nrc3��K�|]D}|jj�djjko|jj�djjkV��EdSr�)r�rrp)r��var�s �rgr�z>PandasArrayExtensionArray._concat_same_type.<locals>.<genexpr>rsc�����& �& �� �H�N�i��l�0�6� 6� e�2�8�>�Y�WX�\�M_�Me�;e�& �& �& �& �& �& rhc��g|] }|j�� Sr��r��r�r�s rgr�z?PandasArrayExtensionArray._concat_same_type.<locals>.<listcomp>vs��;�;�;�2�b�h�;�;�;rhrdc��g|] }|j�� Sr�r�r�s rgr�z?PandasArrayExtensionArray._concat_same_type.<locals>.<listcomp>ys��4�4�4�B�r�x�4�4�4rhFr�)rnr�r��vstackrrrs)r'r�r�s ` rg�_concat_same_typez+PandasArrayExtensionArray._concat_same_typeps���� �y�>�>�A� � �#�& �& �& �& ��& �& �& �# �# � ��9�;�;��;�;�;�<�<�D�D��8�C� �N�N�&�9�9�9�D�4�4�)�4�4�4�D����G��s�4�e�$�$�$�$rhc��|jSr�)r�r�s rgrpzPandasArrayExtensionArray.dtype|s ���{�rhc��|jjSr�)r��nbytesr�s rgr�z PandasArrayExtensionArray.nbytes�s ���z� � rhc�H�tjd�|jD����S)Nc�Z�g|](}tj|�������)Sr�)r��isna�anyr~s rgr�z2PandasArrayExtensionArray.isna.<locals>.<listcomp>�s,��B�B�B�������)�)�+�+�B�B�Brh)r�rlr�r�s rgr�zPandasArrayExtensionArray.isna�s#���x�B�B�t�z�B�B�B�C�C�Crhr�r�c��t���r�)�NotImplementedError)r�r�r�s rg� __setitem__z%PandasArrayExtensionArray.__setitem__�s��!�#�#�#rh�itemc�~�t|t��r |j|St|j|d���S)NFr�)r�r�r�r�)r�r�s rgr]z%PandasArrayExtensionArray.__getitem__�s;�� �d�C� � � $��:�d�#� #�(���D�)9��F�F�F�Frh�indices� allow_fill� fill_valuec��tj|t���}|r�|� |jjntj||jj���}|dk}|dk���rtd���t|��dkrnetj |��std���tj |gt|��z|jj���}t|d���S|j �|d���}|r/|���r|gtj|��z||<t|d���S) NrdrlzFInvalid value in `indices`, must be all >= -1 for `allow_fill` is TruerzAInvalid take for empty PandasArrayExtensionArray, must be all -1.Fr�r_)r�r�r�rp�na_valuerfr�rXrnr�� IndexErrorrlr�r��takerf)r�r�r�r��maskr��tooks rgr�zPandasArrayExtensionArray.take�sE��!�j���<�<�<�� � C�'1�'9�� �#�#�r�z�*�\`�\f�\q�?r�?r�?r� ��b�=�D��"� �!�!�#�#� C� �!i�j�j�j��T���Q�����V�D�\�\� C� �!d�e�e�e��x�� �s�7�|�|� ;�4�:�CX�Y�Y�Y��0��E�B�B�B�B��z���w�Q��/�/�� � 5�$�(�(�*�*� 5�$����t� � �4�D��J�(��E�:�:�:�:rhc�*�t|j��Sr�)rnr�r�s rg�__len__z!PandasArrayExtensionArray.__len__�s���4�:���rhc��t|t��stdt|�������|j|jk���S)NzInvalid type to compare to: )r�r�r�rXr�r�)r��others rg�__eq__z PandasArrayExtensionArray.__eq__�sN���%�!:�;�;� T�%�&R�T�%�[�[�&R�&R�S�S� S�� �e�k�)�.�.�0�0�0rh�Fr�)NF)FN)r�rrr�r�r+rr�r�r;rr9r�� Sequence_r�r�rpr�r�r�r�slicer r�r]r�r�r�r�rhrgr�r�Gsb������<�<�R�Z�<�t�<�<�<�<�,�,�,�,�(@�@��@�*E�@�@�@�@��V[� $� $�%�&?�@� $�OS� $� $� $� $� $��[� $�� %�)�4O�*P� %�Up� %� %� %��[� %���0�����X���!��!�!�!��X�!�D�b�j�D�D�D�D�$�u�S�%���%;�<�$�S�$�T�$�$�$�$�G��c�5�"�*�&<� =�G�%�� �To�Ho�Bp�G�G�G�G� UY�;�;� ��~�;�37�;�MQ�;� $�;�;�;�;�.������1�r�z�1�1�1�1�1�1rhr�c�X�t|t��rt|j��SdSr�)r�rr9rfrds rg�pandas_types_mapperr��s0���%�.�/�/�;�(��)9�:�:�:�;�;rhc�2�eZdZUdZdZeeeed<dZ e e ed<dZ eee ed<dZ ee ed<dZee ed<ej��Zeeed <dZeee efed <dZeeeefed <edd d � ��Ze ed<d�Zd�Zdee efdeeeffd�Zde defd�Zdeeefdee effd�Zd�Z deej!ej"fdej#fd�Z$e%d���Z&dS)� ClassLabela&Feature type for integer class labels. There are 3 ways to define a `ClassLabel`, which correspond to the 3 arguments: * `num_classes`: Create 0 to (num_classes-1) labels. * `names`: List of label strings. * `names_file`: File containing the list of labels. Under the hood the labels are stored as integers. You can use negative integers to represent unknown/missing labels. Args: num_classes (`int`, *optional*): Number of classes. All labels must be < `num_classes`. names (`list` of `str`, *optional*): String names for the integer classes. The order in which the names are provided is kept. names_file (`str`, *optional*): Path to a file with names for the integer classes, one per line. Example: ```py >>> from datasets import Features, ClassLabel >>> features = Features({'label': ClassLabel(num_classes=3, names=['bad', 'ok', 'good'])}) >>> features {'label': ClassLabel(names=['bad', 'ok', 'good'], id=None)} ``` N� num_classes�names� names_filer�r/rpr��_str2int�_int2strFr�r�c�b�||_||_|j�|j�td���|j�a|j� |�|j��|_nx|j�$d�t |j��D��|_nMtd���t |jt��s$tdt|j�������|j�t|j��|_nJ|jt|j��kr-tdt|j���d|j�d����d�|jD��|_ d �t|j ��D��|_ t|j ��t|j ��krtd ���dS) Nz7Please provide either names or names_file but not both.c�,�g|]}t|����Sr��rS)r�r\s rgr�z,ClassLabel.__post_init__.<locals>.<listcomp>�s��F�F�F��c�!�f�f�F�F�Frhz7Please provide either num_classes, names or names_file.z#Please provide names as a list, is zEClassLabel number of names do not match the defined num_classes. Got z names VS z num_classesc�,�g|]}t|����Sr�r��r�r�s rgr�z,ClassLabel.__post_init__.<locals>.<listcomp>�s��:�:�:�t��T���:�:�:rhc��i|]\}}||�� Sr�r�)r�r\r�s rgr�z,ClassLabel.__post_init__.<locals>.<dictcomp>�s��I�I�I�W�Q���q�I�I�IrhzBSome label names are duplicated. Each label name should be unique.)r�r�r�rX�_load_names_from_filerr�� SequenceABC� TypeErrorrXrnr�rnr�)r�r�r�s rgr�zClassLabel.__post_init__�s���&���$��� �?� &�4�:�+A��V�W�W� W� �:� ���*�!�7�7���H�H�� � ��!�-�F�F�e�D�4D�.E�.E�F�F�F�� � � �!Z�[�[�[��D�J� �4�4� V��T�$�t�z�BR�BR�T�T�U�U� U� � � #�"�4�:���D� � � � ��T�Z��� 0� 0��Q��4�:���Q�Q�26�2B�Q�Q�Q��� � ;�:�t�z�:�:�:�� �I�I� �$�-�0H�0H�I�I�I�� � �t�}� � ��T�]�!3�!3� 3� 3��a�b�b� b� 4� 3rhc��|jSr�r�r�s rgr�zClassLabel.__call__�r�rh�valuesr(c����t|t��s(t|t��std|�d����d}t|t��r|g}d}�fd�|D��}|r|n|dS)a$Conversion class name `string` => `integer`. Example: ```py >>> from datasets import load_dataset >>> ds = load_dataset("cornell-movie-review-data/rotten_tomatoes", split="train") >>> ds.features["label"].str2int('neg') 0 ``` �Values zS should be a string or an Iterable (list, numpy array, pytorch, tensorflow tensors)TFc�:��g|]}��|����Sr��� _strval2int)r�r�r�s �rgr�z&ClassLabel.str2int.<locals>.<listcomp>s'���>�>�>�e�$�"�"�5�)�)�>�>�>rhr)r�rSrrX)r�r�� return_listr�s` rg�str2intzClassLabel.str2int�s�����&�#�&�&� �z�&�(�/K�/K� ��u�&�u�u�u��� �� � �f�c� "� "� ��X�F��K�>�>�>�>�v�>�>�>��$�3�v�v�&��)�3rhr�c�P�d}t|��}|j�|��}|�d|j�|�����}|�6 t |��}|dks ||jkrd}n#t $rd}YnwxYw|rt d|�����|S)NFrlTzInvalid string class label )rSr��get�stripr�r�rX)r�r�� failed_parse� int_values rgr�zClassLabel._strval2ints���� ��E� � ���M�%�%�e�,�,� � � �� �)�)�%�+�+�-�-�8�8�I�� �,� #�E� � �I�!�2�~�~��d�6F�)F�)F�'+� ��� "�(�(�(�#'�L�L�L�(���� � D��B�5�B�B�C�C� C��s�B� B�Bc�B��t|t��s(t|t��std|�d����d}t|t��r|g}d}|D])}d|cxkr �jksntd|d������*�fd�|D��}|r|n|dS) a~Conversion `integer` => class name `string`. Regarding unknown/missing labels: passing negative integers raises `ValueError`. Example: ```py >>> from datasets import load_dataset >>> ds = load_dataset("cornell-movie-review-data/rotten_tomatoes", split="train") >>> ds.features["label"].int2str(0) 'neg' ``` r�zU should be an integer or an Iterable (list, numpy array, pytorch, tensorflow tensors)TFrzInvalid integer class label r7c�D��g|]}�jt|����Sr�)r�r�)r�r�r�s �rgr�z&ClassLabel.int2str.<locals>.<listcomp>Gs&���8�8�8�A�$�-��A���'�8�8�8rh)r�r�rrXr�)r�r�r�r�r�s` rg�int2strzClassLabel.int2str,s�����&�#�&�&� �z�&�(�/K�/K� ��w�&�w�w�w��� �� � �f�c� "� "� ��X�F��K�� G� G�A���,�,�,�,�D�,�,�,�,�,� �!E��!E�!E�!E�F�F�F�-�9�8�8�8��8�8�8��$�3�v�v�&��)�3rhc���|j�td���t|t��r|�|��}d|cxkr |jksntd|d�d|j�����|S)NzlTrying to use ClassLabel feature with undefined number of class. Please set ClassLabel.names or num_classes.rl� Class label r7�% greater than configured num_classes )r�rXr�rSr�)r�� example_datas rgr�zClassLabel.encode_exampleJs��� � � #��>��� � �l�C� (� (� 6��<�<� �5�5�L��\�4�4�4�4�D�$4�4�4�4�4��s�L�s�s�s�ae�aq�s�s�t�t� t��rhrWc����t|tj��rst|��dkr`t j|�����}|d�1|d�jkr td|d�d�j�����nLt|tj ��r2tj �fd�|� ��D����}t|�j ��S)a�Cast an Arrow array to the `ClassLabel` arrow storage type. The Arrow types that can be converted to the `ClassLabel` pyarrow storage type are: - `pa.string()` - `pa.int()` Args: storage (`Union[pa.StringArray, pa.IntegerArray]`): PyArrow array to cast. Returns: `pa.Int64Array`: Array in the `ClassLabel` arrow storage type. r�maxNr�r�c�B��g|]}|���|��nd��Sr�r�)r��labelr�s �rgr�z+ClassLabel.cast_storage.<locals>.<listcomp>ps1���i�i�i�E�E�,=��!�!�%�(�(�(�4�i�i�irh)r�rQ� IntegerArrayrn�pc�min_maxrbr�rX� StringArrayrlr�rr�)r�rWrs` rg� cast_storagezClassLabel.cast_storageZs���� �g�r�� /� /� �C��L�L�1�4D�4D��j��)�)�/�/�1�1�G��u�~�)�g�e�n��@P�.P�.P� �j�7�5�>�j�j�X\�Xh�j�j��������� 0� 0� ��h�i�i�i�i�U\�Uf�Uf�Uh�Uh�i�i�i���G��'�4�<�0�0�0rhc��t|d���5}d�|����d��D��cddd��S#1swxYwYdS)Nzutf-8)�encodingc�^�g|]*}|����|�����+Sr�)r�r�s rgr�z4ClassLabel._load_names_from_file.<locals>.<listcomp>ws-��R�R�R�T�T�Z�Z�\�\�R�D�J�J�L�L�R�R�Rrh� )�open�read�split)�names_filepath�fs rgr�z ClassLabel._load_names_from_filets��� �.�7� 3� 3� 3� S�q�R�R�Q�V�V�X�X�^�^�D�-A�-A�R�R�R� S� S� S� S� S� S� S� S� S� S� S� S���� S� S� S� S� S� Ss�1A�A�A)'r�rrrr�rrr�rr�r�rSr�r�rprrQr/r�r r�r�r�rr�r�r�rrr�r�r�r�rr�� Int64Arrayr� staticmethodr�r�rhrgr�r��s���������<+/�K���#��'�.�.�.��E�4��9����)-�J���� �&�-�-�-��B��� ����"�E�8�C�=�"�"�"�%�R�X�Z�Z�G�X�c�]�'�'�'�)-�H�h�t�C��H�~�&�-�-�-�)-�H�h�t�C��H�~�&�-�-�-���|�%�e�D�D�D�E�3�D�D�D�c�c�c�:���4�e�C��M�2�4�u�S�(�]�7K�4�4�4�4�0��������*4�e�C��M�2�4�u�S�(�]�7K�4�4�4�4�<��� 1�E�"�.�"�/�*I�$J�1�r�}�1�1�1�1�4�S�S��\�S�S�Srhr�c��eZdZUdZeed<dZeed<dZe e ed<dZ e e ed<dZ e eed <edd d � ��Ze ed <dS) ra Construct a list of feature from a single type or a dict of types. Mostly here for compatiblity with tfds. Args: feature ([`FeatureType`]): A list of features of a single type or a dictionary of types. length (`int`): Length of the sequence. Example: ```py >>> from datasets import Features, Sequence, Value, ClassLabel >>> features = Features({'post': Sequence(feature={'text': Value(dtype='string'), 'upvotes': Value(dtype='int32'), 'label': ClassLabel(num_classes=2, names=['hot', 'cold'])})}) >>> features {'post': Sequence(feature={'text': Value(dtype='string', id=None), 'upvotes': Value(dtype='int32', id=None), 'label': ClassLabel(names=['hot', 'cold'], id=None)}, length=-1, id=None)} ``` �featurerl�lengthNr�r�rpr�Fr�r�)r�rrrr rrr�r�rrSrprr�rr�r�rhrgrrzs����������&�L�L�L��F�C�����B��� ����!�E�8�C�=�!�!�!�!�G�X�c�]�!�!�!���z��E�B�B�B�E�3�B�B�B�B�Brhrc�x�eZdZUdZeed<dZeeed<dZ e eed<e ddd���Z eed<dS) � LargeLista;Feature type for large list data composed of child feature data type. It is backed by `pyarrow.LargeListType`, which is like `pyarrow.ListType` but with 64-bit rather than 32-bit offsets. Args: feature ([`FeatureType`]): Child feature data type of each item within the large list. rNr�r�Fr�r�) r�rrrr rr�rrSr�rrr�r�rhrgrr�sp����������L�L�L��B��� ����!�G�X�c�]�!�!�!���{��U�C�C�C�E�3�C�C�C�C�Crhr�schemac�N�|�dSt|ttf��r�|�(t|ttttf��rZt |��dkrE|�n,t|ttf��r |d}n|j}t|d|��SdSdS)z� Check if the object is not None. If the object is a list or a tuple, recursively check the first element of the sequence and stop if at any point the first element is not a sequence or is an empty sequence. NFrT)r�r�r�rrrnrr�)r�rs rgr�r��s���  �{��u� �C�$��� '� '� �V�^�z�&�SW�Y^�`i�ks�Rt�?u�?u�^� �s�8�8�a�<�<��~���F�T�5�M�2�2� (���������6�s�1�v�v�F�F� F��5��trhc�4��t�t��r tj�fd��D����St�t��r tj�fd��D����St�t t f��rKt���dkrtd���t�d��}tj |��St�t��r(t�j ��}tj |��St�t��rkt�j ��}t�j t��r!tj�fd�|D����}ntj |�j��}|S���S)a get_nested_type() converts a datasets.FeatureType into a pyarrow.DataType, and acts as the inverse of generate_from_arrow_type(). It performs double-duty as the implementation of Features.type and handles the conversion of datasets.Feature->pa.struct c�<��i|]}|t�|����Sr���get_nested_type�r�r�rs �rgr�z#get_nested_type.<locals>.<dictcomp>��'��� A� A� A�3�S�/�&��+�.�.� A� A� Arhc�<��i|]}|t�|����Sr�rrs �rgr�z#get_nested_type.<locals>.<dictcomp>�rrhrzQWhen defining list feature, you should just provide one example of the inner typerc�Z��i|]'}|jtj|j�j����(Sr�)r�rQr6rXr)r�r rs �rgr�z#get_nested_type.<locals>.<dictcomp>�s-���"_�"_�"_�q�1�6�2�8�A�F�F�M�+J�+J�"_�"_�"_rh)r��FeaturesrQ�structr�r�r�rnrXrr6rr� large_listrr)rrf� data_types` rgrr�s�����&�(�#�#���y� A� A� A� A�&� A� A� A� � � � �F�D� !� !���y� A� A� A� A�&� A� A� A� � � � �F�T�5�M� *� *�� �v�;�;�!� � ��p�q�q� q�$�V�A�Y�/�/� ��x� �#�#�#� �F�I� &� &� �$�V�^�4�4� ��}�Z�(�(�(� �F�H� %� %��$�V�^�4�4� � �f�n�d� +� +� <�� �"_�"_�"_�"_�T^�"_�"_�"_�`�`�I�I����V�]�;�;�I��� �6�8�8�Orhc�������t�t��r+�dkr��td��������fd��D��ndSt�ttf��r��d���dSt�t j��r"t�������St���dkr?�D]}t|���rn�t�|�dz���|kr��fd��D��St���St�t��rl��dSt���dkrF�j ��D]}t|���rn�t�|�dz���|kr��fd��D��St���St�t���rB��dSt�j t��rhi}t�ttf��r�j D]����fd ��D��|�<�|S�j D]!���vr���fd ���D��nd|�<�"|St�t��rtd ��d ����t���dkrr�D]}t|�j ��rn�t|t��st j|��rt�j |�dz���|kr��fd ��D��St���St!�d��r�������ndS�S)a�Encode a nested example. This is used since some features (in particular ClassLabel) have some logic during encoding. To avoid iterating over possibly long lists, it first checks (recursively) if the first element that is not None or empty (if it is a sequence) has to be encoded. If the first element needs to be encoded, then all the elements of the list will be encoded, otherwise they'll stay the same. rNz*Got None but expected a dictionary insteadc �n��i|]1}|t�|��|���dz�����2S�r��level)�encode_nested_exampler�)r�r�r%r�rs ���rgr�z)encode_nested_example.<locals>.<dictcomp>s>��� ^� ^� ^�RS�Q�%�f�Q�i�������5�1�9�M�M�M� ^� ^� ^rhrr$c�:��g|]}t�|�dz�����Sr#�r&�r��or%� sub_schemas ��rgr�z)encode_nested_example.<locals>.<listcomp>�.���_�_�_�VW�1�*�a�u�q�y�Q�Q�Q�_�_�_rhc�:��g|]}t�|�dz�����Sr#r(r)s ��rgr�z)encode_nested_example.<locals>.<listcomp>$r,rhc�v��g|]5}t�j�|�����dz�����6Sr#)r&rr��r�r*r�r%rs ���rgr�z)encode_nested_example.<locals>.<listcomp>0sC���#u�#u�#u�lm�$9�&�.��:K�Q�U�U�ST�X�X�]b�ef�]f�$g�$g�$g�#u�#u�#urhc�P��g|]"}t�j�|�dz�����#Sr#�r&rr/s ���rgr�z)encode_nested_example.<locals>.<listcomp>6s6���f�f�f�Z[�.�v�~�a�/@�!�5�ST�9�U�U�U�f�f�frhz+Got a string but expected a list instead: '�'c�D��g|]}t�j|�dz�����Sr#r1)r�r*r%rs ��rgr�z)encode_nested_example.<locals>.<listcomp>Hs1���c�c�c�Z[�1�&�.�!�5�ST�9�U�U�U�c�c�crhr�)r�r�rXr�r�r�r�r&r�rnr�rrrrSr�r�r�)rr�r%r�� list_dictr�r+s``` @@rgr&r&�s���������&�$���JG� �A�:�:�#�+��I�J�J� J��� _� ^� ^� ^� ^� ^�W]� ^� ^� ^� ^�� � �F�T�5�M� *� *�AG��A�Y� � �;��4� ��R�Z� (� (� �(�������>�>� >��3�x�x�!�|�|�"%���J�:�:�z�R�R�����(��Z�u�q�y�Q�Q�Q�U_�_�_�_�_�_�_�_�[^�_�_�_�_���9�9� � �F�I� &� &�3G� �;��4��3�x�x�!�|�|�#�^� �"%���J�:�:�z�R�R�����(��Z�u�q�y�Q�Q�Q�U_�_�_�_�_�_�_�_�[^�_�_�_�_���9�9� � �F�H� %� %�'G� �;��4� �f�n�d� +� +� !��I��#��e�}�-�-� !���v�v�A�#u�#u�#u�#u�#u�#u�qt�#u�#u�#u�I�a�L�L� � � ����A���8�8�g�f�f�f�f�f�_b�cd�_e�f�f�f�f�!��a�L�L� !� � �c�3� � � ��Q�3�Q�Q�Q�R�R� R��3�x�x�!�|�|�"%���J�:�:�v�~�V�V�����$�J��5�5�d�9;��Z�9P�9P�d�,�V�^�Z�u�WX�y�Y�Y�Y�]g�g�g�c�c�c�c�c�_b�c�c�c�c���9�9� � ��)� *� *�G�-0�_�v�$�$�S�)�)�)�$�F� �Jrh�token_per_repo_idc�����t�t��r��d�t����D��ndSt�ttf��rg�d���dSt ���dkr9�D]}t |���rn�t�|��|kr�fd��D��St���St�t��rf��dS�j �t ���dkr9�D]}t |���rn�t�|��|kr�fd��D��St���St�t��rDt�j t��r��fd��j D��St�j g���St�d��r,t�dd ��r���� �|� ��ndS�S) a�Decode a nested example. This is used since some features (in particular Audio and Image) have some logic during decoding. To avoid iterating over possibly long lists, it first checks (recursively) if the first element that is not None or empty (if it is a sequence) has to be decoded. If the first element needs to be decoded, then all the elements of the list will be decoded, otherwise they'll stay the same. Nc�<�i|]\}\}}|t||����Sr���decode_nested_example)r�r�r+�sub_objs rgr�z)decode_nested_example.<locals>.<dictcomp>\s/�� q� q� q�?W�q�BW�:�w�Q�%�j�'�:�:� q� q� qrhrc�0��g|]}t�|����Sr�r8�r�r*r+s �rgr�z)decode_nested_example.<locals>.<listcomp>j�$���N�N�N�Q�1�*�a�@�@�N�N�Nrhc�0��g|]}t�|����Sr�r8r<s �rgr�z)decode_nested_example.<locals>.<listcomp>vr=rhc�V��i|]%}|t�j|g�|����&Sr�)r9r)r�r�r�rs ��rgr�z)decode_nested_example.<locals>.<dictcomp>{s4���b�b�b�a�A�,�f�n�Q�.?�-@�#�a�&�I�I�b�b�brh�decode_example�decodeT�r5)r�r�rr�r�rnr�r9rrrr��getattrr@)rr�r5r�r+s`` @rgr9r9Rs7������&�$���'l��� r� q�[c�dj�lo�[p�[p� q� q� q� q�� � �F�T�5�M� *� *�!l��A�Y� � �;��4��3�x�x�!�|�|�"%���J�:�:�z�R�R�����(��Z�@�@�J�N�N�N�N�N�N�#�N�N�N�N���9�9� � �F�I� &� &�l� �;��4���J��3�x�x�!�|�|�"%���J�:�:�z�R�R�����(��Z�@�@�J�N�N�N�N�N�N�#�N�N�N�N���9�9� � �F�H� %� %� l� �f�n�d� +� +� @�b�b�b�b�b�SY�Sa�b�b�b� b�(�&�.�)9�3�?�?� ?� ��)� *� *�l�w�v�x��/N�/N�l�RU�Ra�v�$�$�S�<M�$�N�N�N�gk�k� �Jrh�_FEATURE_TYPES� feature_cls� feature_typec ��|tvr9t�d|�dt|j�d|j�d���|t|<dS)zn Register a Feature object using a name and class. This function must be used on a Feature class. zOverwriting feature type 'z' (z -> r<N)rD�logger�warningr�)rErFs rg�register_featurerJ�sa���~�%�%���� |�� |� |�.��:V�:_� |� |�ep�ey� |� |� |� � � �$/�N�<� � � rhc �b��t|t��r d�|D��Sd|vst|dt��rd�|���D��St|��}|�d��}t �|d��p!t���|d��}|�9td|�dtt � ���������|tkr0|�d��}td dt|��i|��S|tkr0|�d��}td dt|��i|��Sd�t|��D���|d i�fd �|���D����S) a�Regenerate the nested feature object from a deserialized dict. We use the '_type' fields to get the dataclass name to load. generate_from_dict is the recursive helper for Features.from_dict, and allows for a convenient constructor syntax to define features from deserialized JSON dictionaries. This function is used in particular when deserializing a :class:`DatasetInfo` that was dumped to a JSON object. This acts as an analogue to :meth:`Features.from_arrow_schema` and handles the recursive field-by-field instantiation, but doesn't require any mapping to/from pyarrow, except for the fact that it takes advantage of the mapping of pyarrow primitive dtypes that :class:`Value` automatically performs. c�,�g|]}t|����Sr���generate_from_dict)r�r�s rgr�z&generate_from_dict.<locals>.<listcomp>�s!��;�;�;�e�"�5�)�)�;�;�;rhr�c�4�i|]\}}|t|����Sr�rM)r�r�r�s rgr�z&generate_from_dict.<locals>.<dictcomp>�s'��M�M�M�:�3���'��.�.�M�M�MrhNzFeature type 'z&' not found. Available feature types: rc��h|] }|j�� Sr��r��r�r s rg� <setcomp>z%generate_from_dict.<locals>.<setcomp>�s��6�6�6�a�1�6�6�6�6rhc�$��i|] \}}|�v� ||�� Sr�r�)r�r�r�� field_namess �rgr�z&generate_from_dict.<locals>.<dictcomp>�s)���J�J�J�$�!�Q��k�9I�9I��A�9I�9I�9Irhr�)r�r�r�r��poprDr�r rX�keysrrNrr )r�r�� class_typerrUs @rgrNrN�s�����#�t���<�;�;�s�;�;�;�;��c���Z��G� �d�;�;��M�M������M�M�M�M� �s�)�)�C� �G�G�G� � �E��#�#�E�4�0�0�N�G�I�I�M�M�%��4N�4N�J����t�%�t�t�W[�\j�\o�\o�\q�\q�Wr�Wr�t�t�u�u�u��Y����'�'�)�$�$���D�D�!3�G�!<�!<�D��D�D�D��X����'�'�)�$�$���C�C� 2�7� ;� ;�C�s�C�C�C�6�6�6�*�#5�#5�6�6�6�K� �:� K� K�J�J�J�J�#�)�)�+�+�J�J�J� K� K�Krhc�N�t|tj��r d�|D��St|tj��r(t t |j��|j���St|tj��rIt |j��}t|tttf��r|gSt |���St|tj ��r$t |j��}t|���St|t��r>ddtt t"t$g|j}||j|j���St|tj��rt-t/|�����St1d|�d����) a� generate_from_arrow_type accepts an arrow DataType and returns a datasets FeatureType to be used as the type for a single field. This is the high-level arrow->datasets type conversion and is inverted by get_nested_type(). This operates at the individual *field* level, whereas Features.from_arrow_schema() operates at the full schema level and holds the methods that represent the bijection from Features<->pyarrow.Schema c�B�i|]}|jt|j����Sr�)r��generate_from_arrow_typerX)r�rs rgr�z,generate_from_arrow_type.<locals>.<dictcomp>�s'��V�V�V�U�� �4�U�Z�@�@�V�V�Vrh)rr�rN)rrprdzCannot convert z to a Feature type.)r�rQ� StructType�FixedSizeListTyperr[rf� list_size�ListTyper�r�r�� LargeListTyperrrrrrrrrNr�rerX)r�r� array_features rgr[r[�s����'�2�=�)�)�I�V�V�g�V�V�V�V� �G�R�1� 2� 2�I�� 8��9K� L� L�U\�Uf�g�g�g�g� �G�R�[� )� )�I�*�7�+=�>�>�� �g��e�T�2� 3� 3� ��9� ���(�(�(�(� �G�R�-� .� .� I�*�7�+=�>�>����)�)�)�)� �G�2� 3� 3�I��t�W�g�w��H���W� ��}�7�=��8J�K�K�K�K� �G�R�[� )� )�I��3�G�<�<�=�=�=�=��G�7�G�G�G�H�H�HrhrrXc���tj|��}tj|���|���}t |jdz ��D]�}t t|jd|j|z dz �d��}|j|j|z dz }tjtj |dz��|ztj �����}tj � ||��}��|S)z=Build a PyArrow ListArray from a multidimensional NumPy array�rXrN) r�rlrQrgrr�r r rrer.� ListArray� from_arrays)rrXr�r\� n_offsets� step_offsetsrms rg�numpy_to_pyarrow_listarrayri�s��� �(�3�-�-�C� �X�c�k�k�m�m�$� /� /� /�F� �3�8�a�<� � �;�;���3�� �*<�C�H�q�L�1�,<�*<� =�q�A�A� ��y���A���!1�2� ��(�2�9�Y��]�3�3�l�B�����T�T�T����)�)�'�6�:�:��� �Mrh�l_arrc�&�tjd�|D����}tjt|����|tjtj|����z }d�|D��}tjdgd�|D��zt ���}tj||d��}tj|tj �����}tj |��}tj � ||��S)Nc��g|]}|du��Sr�r�r~s rgr�z:list_of_pa_arrays_to_pyarrow_listarray.<locals>.<listcomp>�s��7�7�7�#�#��+�7�7�7rhc��g|]}|�|��Sr�r�r~s rgr�z:list_of_pa_arrays_to_pyarrow_listarray.<locals>.<listcomp>�s�� 5� 5� 5�S�S�_�S�_�_�_rhrc�,�g|]}t|����Sr�)rnr~s rgr�z:list_of_pa_arrays_to_pyarrow_listarray.<locals>.<listcomp>�s��)�)�)�C�s�3�x�x�)�)�)rhrdrd) r�rlrernrf�cumsumrsrirQr.r�rerf)rjrtrvrmr�s rg�&list_of_pa_arrays_to_pyarrow_listarrayrp�s�����7�7��7�7�7�8�8�I��9�S��^�^�,�,�Y�7�"�)�B�F�9�DU�DU�:V�:V�V�L� 5� 5�E� 5� 5� 5�E��i� ��)�)�5�)�)�)�)�����G��i���t�4�4�G��h�w�R�X�Z�Z�0�0�0�G� � �e� $� $�F� �<� #� #�G�V� 4� 4�4rhc���t|��dkrt�fd�|D����Stjg����S)zEBuild a PyArrow ListArray from a possibly nested list of NumPy arraysrc�:��g|]}|�t|����nd��S)Nrd)ri)r�rrXs �rgr�z9list_of_np_array_to_pyarrow_listarray.<locals>.<listcomp>s2��� f� f� f�Y\�3�?� '��$� 7� 7� 7� 7�PT� f� f� frhrd)rnrprQrl)rjrXs `rg�%list_of_np_array_to_pyarrow_listarrayrssS��� �5�z�z�A�~�~�5� f� f� f� f�`e� f� f� f� � � ��x���&�&�&�&rhr�c��t|tj��rdSt|t��r"t t |��d��SdS)z�Return `True` if data is a NumPy ndarray or (recursively) if first non-null value in list is a NumPy ndarray. Args: data (Any): Data. Returns: bool TrF)r�r�r�r��contains_any_np_arrayr)r�s rgruru sP���$�� �#�#���t� �D�$� � ��$�%9�$�%?�%?��%B�C�C�C��urhc���t|tj��rt|����St|t��rt �fd�|D����SdS)a0Convert to PyArrow ListArray either a NumPy ndarray or (recursively) a list that may contain any NumPy ndarray. Args: data (Union[np.ndarray, List]): Data. type (pa.DataType): Explicit PyArrow DataType passed to coerce the ListArray data type. Returns: pa.ListArray rdc�2��g|]}t|������S)rd)�!any_np_array_to_pyarrow_listarray)r�r\rXs �rgr�z5any_np_array_to_pyarrow_listarray.<locals>.<listcomp>*s*���6u�6u�6u�kl�7X�YZ�ae�7f�7f�7f�6u�6u�6urhN)r�r�r�rir�rp)r�rXs `rgrxrxsp����$�� �#�#�w�)�$�T�:�:�:�:� �D�$� � �w�5�6u�6u�6u�6u�pt�6u�6u�6u�v�v�v�w�wrhc��t|��rt||j���Stj||j��S)z�Convert to PyArrow ListArray. Args: data (Any): Sequence, iterable, np.ndarray or pd.Series. pa_type (_ArrayXDExtensionType): Any of the ArrayNDExtensionType. Returns: pyarrow.Array rd)rurxrfrQrlr)r�r�s rg�to_pyarrow_listarrayrz-s?���T�"�"�5�0��G�<N�O�O�O�O��x��g�3�4�4�4rhr�funcc����t|t��r8�t�fd�|���D������}�nt|t��r*��fd�|���D����}n�t|tt f��r!�t |d���g��}n�t|t��r,�tt |j�������}nSt|t��r3�tt |j���|j �����}n �|��}|�|n|S)z�Visit a (possibly nested) feature. Args: feature (FeatureType): the feature type to be checked Returns: visited feature (FeatureType) c�8��i|]\}}|t|�����Sr���_visit�r�r�r r{s �rgr�z_visit.<locals>.<dictcomp>Fs'���L�L�L�D�A�q�Q��q�$���L�L�Lrhc�8��i|]\}}|t|�����Sr�r~r�s �rgr�z_visit.<locals>.<dictcomp>Hs'���C�C�C�4�1�a�A�v�a����C�C�Crhr�r) r�rr�r�r�r�rrrrr)rr{r�s ` rgrr=sU����'�8�$�$� ��d�8�L�L�L�L�G�M�M�O�O�L�L�L�M�M�N�N��� �G�T� "� "� ��d�C�C�C�C�7�=�=�?�?�C�C�C�D�D��� �G�d�E�]� +� +���d�F�7�1�:�t�,�,�-�.�.��� �G�Y� '� '���d�9�V�G�O�T�:�:�;�;�<�<��� �G�X� &� &���d�8�F�7�?�D�9�9�'�.�Q�Q�Q�R�R����d�7�m�m���k�7�7�s�*rh� visit_pathc ����t|t��r=t|jt��r#d�|j���D��}t|t ��r:�t ��fd�|���D�������}�n%t|t��r,���fd�|���D�����}n�t|t tf��r'�t|d��dgz��g���}n�t|t��r9�tt|j��dgz��|j ������}nSt|t��r2�tt|j��dgz�������}n �|���}|�|n|S)a0Visit a (possibly nested) feature with its path in the Feature object. A path in a nested feature object is the list of keys that need to be sequentially accessed to get to the sub-feature. For example: - ["foo"] corresponds to the column "foo" - ["foo", 0] corresponds to the sub-feature of the lists in "foo" - ["foo", "bar"] corresponds to the sub-feature of the dicts in "foo" with key "bar" Args: feature (`FeatureType`): the feature type to be checked. Returns: `FeatureType`: the visited feature. c��i|] \}}||g�� Sr�r�)r�r�r s rgr�z$_visit_with_path.<locals>.<dictcomp>ks ��>�>�>�d�a��1�q�c�>�>�>rhc �B��i|]\}}|t|��|gz����Sr���_visit_with_path�r�r�r r{r�s ��rgr�z$_visit_with_path.<locals>.<dictcomp>ns6���h�h�h�PT�PQ�ST�Q� 0��D�*��s�:J� K� K�h�h�hrhc �B��i|]\}}|t|��|gz����Sr�r�r�s ��rgr�z$_visit_with_path.<locals>.<dictcomp>ps3���_�_�_�t�q�!�A�'��4��q�c�1A�B�B�_�_�_rhrr�) r�rrr�r�rr�r�r�rr)rr{r�r�s `` rgr�r�Ws�����&�'�8�$�$�?��G�O�T�)J�)J�?�>�>�g�o�&;�&;�&=�&=�>�>�>���'�8�$�$� (��d�8�h�h�h�h�h�X_�Xe�Xe�Xg�Xg�h�h�h�i�i�ku�v�v��� �G�T� "� "� (��d�_�_�_�_�_�w�}�}���_�_�_�ak�l�l��� �G�d�E�]� +� +� (��d�$�W�Q�Z��z�Q�C�7G�H�H�I�:�V�V��� �G�X� &� &�(��d� �%�g�o�t�Z�1�#�=M�N�N�W^�We� f� f� f�hr� � ��� �G�Y� '� '�(��d�9�-�g�o�t�Z�ST�RU�EU�V�V�W�W�Yc�d�d����d�7�J�'�'���k�7�7�s�*rh�ignore_decode_attributec���t|t��r+td�|���D����St|tt f��rt |d��St|t��rt |j��St|t��rt |j��St|d��o|st|dd��ndS)aRCheck if a (possibly nested) feature requires decoding. Args: feature (FeatureType): the feature type to be checked ignore_decode_attribute (:obj:`bool`, default ``False``): Whether to ignore the current value of the `decode` attribute of the decodable feature types. Returns: :obj:`bool` c3�4K�|]}t|��V��dSr���require_decodingrRs rgr�z#require_decoding.<locals>.<genexpr>�s+����A�A�1�#�A�&�&�A�A�A�A�A�Arhrr@rAT) r�r�r�r�r�r�r�rrrr�rC)rr�s rgr�r�~s����'�4� � �  ��A�A����0@�0@�A�A�A�A�A�A� �G�d�E�]� +� +�  ���� �+�+�+� �G�Y� '� '� ����0�0�0� �G�X� &� &� ����0�0�0��w� 0�1�1� �4K� U�G�G�X�t� ,� ,� ,�QU� rhc��t|t��r+td�|���D����St|tt f��rt |d��St|t��rt |j��St|t��rt |j��St|d��S)z�Check if a (possibly nested) feature requires storage casting. Args: feature (FeatureType): the feature type to be checked Returns: :obj:`bool` c3�4K�|]}t|��V��dSr���require_storage_castrRs rgr�z'require_storage_cast.<locals>.<genexpr>��+����E�E�q�'��*�*�E�E�E�E�E�Erhrr� r�r�r�r�r�r�r�rrrr�r\s rgr�r��s����'�4� � � 0��E�E�G�N�N�4D�4D�E�E�E�E�E�E� �G�d�E�]� +� +�0�#�G�A�J�/�/�/� �G�Y� '� '�0�#�G�O�4�4�4� �G�X� &� &�0�#�G�O�4�4�4��w��/�/�/rhc��t|t��r+td�|���D����St|tt f��rt |d��St|t��rt |j��St|t��rt |j��St|d��S)z�Check if a (possibly nested) feature requires embedding data into storage. Args: feature (FeatureType): the feature type to be checked Returns: :obj:`bool` c3�4K�|]}t|��V��dSr�r�rRs rgr�z(require_storage_embed.<locals>.<genexpr>�r�rhr� embed_storager�r\s rg�require_storage_embedr��s����'�4� � � 1��E�E�G�N�N�4D�4D�E�E�E�E�E�E� �G�d�E�]� +� +�1�#�G�A�J�/�/�/� �G�Y� '� '�1�#�G�O�4�4�4� �G�X� &� &�1�#�G�O�4�4�4��w��0�0�0rhc�J��t����fd���}d|_|S)z� Wrapper to keep the secondary dictionary, which tracks whether keys are decodable, of the :class:`datasets.Features` object in sync with the main dictionary. c����|r|d}|dd�}n|�d��}�|g|�Ri|��}t|d��sJ�d�|���D��|_|S)Nrrr��_column_requires_decodingc�4�i|]\}}|t|����Sr�r��r��colrs rgr�z?keep_features_dicts_synced.<locals>.wrapper.<locals>.<dictcomp>�s(��)j�)j�)j�\�S�RY�#�/?��/H�/H�)j�)j�)jrh)rVr�r�r�)r*�kwargsr�r�r{s �rg�wrapperz+keep_features_dicts_synced.<locals>.wrapper�s���� � 2�#�A�w�D�����8�D�D�%�z�z�&�1�1�D��d�4�)�$�)�)�)�&�)�)���t�8�9�9�9�9�9�)j�)j�]a�]g�]g�]i�]i�)j�)j�)j��&�� rh�_keep_dicts_synced)r �_decorator_name_)r{r�s` rg�keep_features_dicts_syncedr��s=���  �4�[�[� � � � ��[� � 4�G�� �Nrhc �j��eZdZdZ�fd�Zeej��Zeej��Zeej ��Z eej ��Z eej ��Z eej ��Z eej ��Z d�Zed���Zed���Zedejddfd���Zedd ���Zd �Zdefd �Zed eddfd ���Zd�Zdefd�Zd�Zd dedeeee ee!dfffd�Z"dedefd�Z#d dedeeee ee!dfffd�Z$dd�Z%d!d�Z&d"dd�Z'�xZ(S)#ra�A special dictionary that defines the internal structure of a dataset. Instantiated with a dictionary of type `dict[str, FieldType]`, where keys are the desired column names, and values are the type of that column. `FieldType` can be one of the following: - [`Value`] feature specifies a single data type value, e.g. `int64` or `string`. - [`ClassLabel`] feature specifies a predefined set of classes which can have labels associated to them and will be stored as integers in the dataset. - Python `dict` specifies a composite feature containing a mapping of sub-fields to sub-features. It's possible to have nested fields of nested fields in an arbitrary manner. - Python `list`, [`LargeList`] or [`Sequence`] specifies a composite feature containing a sequence of sub-features, all of the same feature type. <Tip> A [`Sequence`] with an internal dictionary feature will be automatically converted into a dictionary of lists. This behavior is implemented to have a compatibility layer with the TensorFlow Datasets library but may be un-wanted in some cases. If you don't want this behavior, you can use a Python `list` or a [`LargeList`] instead of the [`Sequence`]. </Tip> - [`Array2D`], [`Array3D`], [`Array4D`] or [`Array5D`] feature for multidimensional arrays. - [`Audio`] feature to store the absolute path to an audio file or a dictionary with the relative path to an audio file ("path" key) and its bytes content ("bytes" key). This feature extracts the audio data. - [`Image`] feature to store the absolute path to an image file, an `np.ndarray` object, a `PIL.Image.Image` object or a dictionary with the relative path to an image file ("path" key) and its bytes content ("bytes" key). This feature extracts the image data. - [`Translation`] or [`TranslationVariableLanguages`] feature specific to Machine Translation. c���|std���|^}}tt|��j|i|��d�|���D��|_dS)Nz<descriptor '__init__' of 'Features' object needs an argumentc�4�i|]\}}|t|����Sr�r�r�s rgr�z%Features.__init__.<locals>.<dictcomp>�s4��; �; �; �/;�s�G�C�!�'�*�*�; �; �; rh)r��superrrr�r�)r*r�r�r s �rgrzFeatures.__init__�su���� \��Z�[�[� [�� ��t�&��h����&��7��7�7�7�; �; �?C�z�z�|�|�; �; �; ��&�&�&rhc�0�tt|��ffSr�)rr�r�s rgr-zFeatures.__reduce__s���$�t�*�*��&�&rhc� �t|��S)z] Features field types. Returns: :obj:`pyarrow.DataType` rr�s rgrXz Features.type s���t�$�$�$rhc��dd|���ii}tj|j���dt j|��i��S)zV Features schema. Returns: :obj:`pyarrow.Schema` �info�features� huggingface)r�rQrrX� with_metadatar!r")r�� hf_metadatas rg� arrow_schemazFeatures.arrow_schemasJ��� �D�L�L�N�N�;�<� ��y���#�#�1�1�=�$�*�[�BY�BY�2Z�[�[�[rh� pa_schemar(c�d���t���|j�|d|jvrstj|jd�����}d|vr>d|dvr4|dd�&t�|dd����j���fd�|D��}|di|��S)a� Construct [`Features`] from Arrow Schema. It also checks the schema metadata for Hugging Face Datasets features. Non-nullable fields are not supported and set to nullable. Also, pa.dictionary is not supported and it uses its underlying type instead. Therefore datasets convert DictionaryArray objects to their actual values. Args: pa_schema (`pyarrow.Schema`): Arrow Schema. Returns: [`Features`] Ns huggingfacer�r�c���i|]P}|j|j�vr+��|j��|kr �|jnt|j����QSr�)r�rr[rX)r�r�metadata_features�metadata_features_schemas ��rgr�z.Features.from_arrow_schema.<locals>.<dictcomp>7su��� � � � � �J��:�!2�2�2�7O�7U�7U�V[�V`�7a�7a�ej�7j�7j�"�%�*�-�-�-�e�j�9�9�  � � rhr�)r�metadatar!r&rA� from_dictr�)r'r�r�r�r�r�s @@rg�from_arrow_schemazFeatures.from_arrow_schemas�����$%�J�J�� � � )�n� �@R�.R�.R��z�)�"4�^�"D�"K�"K�"M�"M�N�N�H���!�!�j�H�V�4D�&D�&D��RX�IY�Zd�Ie�Iq�$,�$6�$6�x��7G� �7S�$T�$T�!�#4�#A� � � � � � � #�  � � ���s�z�z�S�z�z�rhc�0�t|��}|di|��S)a Construct [`Features`] from dict. Regenerate the nested feature object from a deserialized dict. We use the `_type` key to infer the dataclass name of the feature `FieldType`. It allows for a convenient constructor syntax to define features from deserialized JSON dictionaries. This function is used in particular when deserializing a [`DatasetInfo`] that was dumped to a JSON object. This acts as an analogue to [`Features.from_arrow_schema`] and handles the recursive field-by-field instantiation, but doesn't require any mapping to/from pyarrow, except for the fact that it takes advantage of the mapping of pyarrow primitive dtypes that [`Value`] automatically performs. Args: dic (`dict[str, Any]`): Python dictionary. Returns: `Features` Example:: >>> Features.from_dict({'_type': {'dtype': 'string', 'id': None, '_type': 'Value'}}) {'_type': Value(dtype='string', id=None)} r�rM)r'�dicr�s rgr�zFeatures.from_dictAs#��4!��%�%���s�z�z�S�z�z�rhc� �t|��Sr�)rr�s rgr�zFeatures.to_dict^s���d�|�|�rhc������|���}dtdtfd��dtttfdtf��fd� �dtdtf�fd� ���|��d��S)Nrr(c���t|t��s"tdt|���d|�����dD]�}t|�|��t��r+t ||��dgkr||d||<t|�|��t��r+t ||��dgkr||d||<��t|�d��t��r\t|d�d��t ��r.d�t |dd��D��|dd<|S) N�Expected a dict but got a �: �rr��sequencerpr� class_labelr�c�4�i|]\}}t|��|��Sr�r�)r��label_id� label_names rgr�z<Features._to_yaml_list.<locals>.simplify.<locals>.<dictcomp>�s1��3�3�3�2F�(�J�C��M�M�:�3�3�3rh)r�r�r�rXr�r�rn)r� list_types rg�simplifyz(Features._to_yaml_list.<locals>.simplifyesw���g�t�,�,� Y�� W�T�'�]�]� W� W�g� W� W�X�X�X�?� F� F� � �g�k�k�)�4�4�d�;�;�E��W�Y�EW�@X�@X�]d�\e�@e�@e�)0��);�G�)D�G�I�&��g�k�k�)�4�4�d�;�;�F��W�Y�EW�@X�@X�]e�\f�@f�@f�)0��);�H�)E�G�I�&���'�+�+�m�4�4�d�;�;� � �7�S`�Ka�Ke�Ke�fm�Kn�Kn�pt�@u�@u� �3�3�JS�T[�\i�Tj�kr�Ts�Jt�Jt�3�3�3�� �&�w�/��Nrhr�c ����t|t��r�|�dd��}|dkr-|�d��}�d�|��i|���S|dkr-|�d��}�d�|��i|���S|dkr|S|r|sdt|��iS|rd�t|��|i��iSd �fd �|���D��iSt|t ��r%�d ��|d ����i��St|t ��r�t |����Std t|���d|�����)Nr�rrrrr�r�rprc�4��g|]\}}d|i�|�����SrQr�)r�r��_feature� to_yaml_inners �rgr�zAFeatures._to_yaml_list.<locals>.to_yaml_inner.<locals>.<listcomp>�s4���&s�&s�&s�Uc�UY�[c���'P� � �h�8O�8O�'P�&s�&s�&srhr�r�"Expected a dict or a list but got r�) r�r�rVrr�r�r�r�rX)r�r�r�r�r�s ��rgr�z-Features._to_yaml_list.<locals>.to_yaml_inner�s�����#�t�$�$� Y������.�.���K�'�'�"�w�w�y�1�1�H�#�8�\�=�=��3J�3J�$R�c�$R�S�S�S��j�(�(�"�w�w�y�1�1�H�#�8�Z���x�1H�1H�$P�C�$P�Q�Q�Q��g�%�%��J��u�3�u�#�%;�E�%B�%B�C�C��u�#�X�X�/E�e�/L�/L�c�.R�%S�%S�T�T�$�&s�&s�&s�&s�gj�gp�gp�gr�gr�&s�&s�&s�t�t��C��&�&� Y��x����-�-��A��2G�2G�)H�)H� I�J�J�J��C��'�'� Y�$�}�T�#�Y�Y�/�/�/�� W�T�#�Y�Y� W� W�RU� W� W�X�X�Xrhc���t|t��r �fd�|���D��St|t��r�fd�|D��St|t��r�t|����S|S)Nc�.��i|]\}}|�|����Sr�r�)r�r�r�� to_yaml_typess �rgr�zAFeatures._to_yaml_list.<locals>.to_yaml_types.<locals>.<dictcomp>�s)���D�D�D���1��=�=��+�+�D�D�Drhc�&��g|] }�|����Sr�r�)r�r�r�s �rgr�zAFeatures._to_yaml_list.<locals>.to_yaml_types.<locals>.<listcomp>�s#���6�6�6�Q� � �a�(�(�6�6�6rh)r�r�r�r�r�)r�r�s �rgr�z-Features._to_yaml_list.<locals>.to_yaml_types�s�����#�t�$�$� �D�D�D�D�� � � � �D�D�D�D��C��&�&� �6�6�6�6�#�6�6�6�6��C��'�'� �$�}�T�#�Y�Y�/�/�/�� rhr)r�r�rr�)r�� yaml_datar�r�r�s @@@rg� _to_yaml_listzFeatures._to_yaml_listas�������L�L�N�N� � �d� �t� � � � �D Y�u�T�4�Z�0� Y�T� Y� Y� Y� Y� Y� Y� Y�0 �t� �� � � � � � ��}�]�]�9�5�5�h�?�@�@�@rhr�c����tj|��}dtdtfd��dtttfdtttff��fd� �|��|����S)Nrr(c ����t�t��s"tdt����d������dD]7}t��|��t ��r d�|i�|<�8t��d��t��r�t�d�d��t��r�t �ddt���}|rid�|D��ttt|d ��d z����kr)td t|d ��d z�d �����fd �|D���dd<�S)Nr�r�r�rpr�r�)r�c�,�g|]}t|����Sr�)r�)r�r�s rgr�z@Features._from_yaml_list.<locals>.unsimplify.<locals>.<listcomp>�s��!J�!J�!J�H�#�h�-�-�!J�!J�!Jrhrlrz1ClassLabel expected a value for all label ids [0:z] but some ids are missing.c�8��g|]}�dd|��S)r�r�r�)r�r�rs �rgr�z@Features._from_yaml_list.<locals>.unsimplify.<locals>.<listcomp>�s*���2w�2w�2w�ai�7�=�3I�'�3R�S[�3\�2w�2w�2wrh) r�r�r�rXr�rS�sortedr�r�rrX)rr�� label_idss` rg� unsimplifyz,Features._from_yaml_list.<locals>.unsimplify�s�����g�t�,�,� Y�� W�T�'�]�]� W� W�g� W� W�X�X�X�?� G� G� � �g�k�k�)�4�4�c�:�:�G�*1�7�9�3E�)F�G�I�&���'�+�+�m�4�4�d�;�;� x� �7�S`�Ka�Ke�Ke�fm�Kn�Kn�pt�@u�@u� x�"�7�=�#9�'�#B��L�L�L� ���!J�!J� �!J�!J�!J�d�SX�Y\�]f�gi�]j�Yk�Yk�no�Yo�Sp�Sp�Nq�Nq�!q�!q�$��C�PY�Z\�P]�L^�L^�ab�Lb�������3x�2w�2w�2w�mv�2w�2w�2w�� �&�w�/��Nrhr�c���t|t���r_|siStt|����}|dkr1�|���|��}d�|��i|�ddi�S|dkr1�|���|��}d�|��i|�ddi�S|dkr��|��|��gS|dkr�|d��S|d krqt|d t ��rE t |d ��i|�dd i�S#t$rdt|d ��icYSwxYw�|d ��Sdt|��i�|��|�St|t��r(d �|D��}�fd �t||��D��Std t|���d|�����)Nrrr�rr�rr�rrpr�c�8�g|]}|�d����SrQ)rV)r�r�s rgr�zEFeatures._from_yaml_list.<locals>.from_yaml_inner.<locals>.<listcomp>�s$��B�B�B�(����f�-�-�B�B�Brhc�.��i|]\}}|�|����Sr�r�)r�r�r��from_yaml_inners �rgr�zEFeatures._from_yaml_list.<locals>.from_yaml_inner.<locals>.<dictcomp>�s)���^�^�^�N�D�(��o�o�h�7�7�^�^�^rhr�r�) r�r��next�iterrVrSr�rXrr��zipr�rX)r�r�r�r�r�r�s ��rgr�z1Features._from_yaml_list.<locals>.from_yaml_inner�sK����#�t�$�$� Y����I��T�#�Y�Y�����L�(�(�)�z�#���2�2�5�9�9�H�%���x�'@�'@�^�C�^��R]�^�^�^��J�&�&�)�z�#���2�2�5�9�9�H�%���x�'@�'@�]�C�]��R\�]�]�]��F�?�?�+�O�J�J�s�O�O�E�,B�C�C�D�D��H�$�$�*�?�3�x�=�9�9�9��g�%�%�!�#�g�,��4�4� =�S�!�#�g�,�/�/�/�#<�c�#<�7�G�#<�#<�<��)�S�S�S�$+�-C�C��L�-Q�-Q�#R�R�R�R�S���� /��s�7�|�<�<�<�#�%;�E�%B�%B�]�j�j�QT�o�o�V[�F\�]�]��C��&�&� Y�B�B�c�B�B�B��^�^�^�^�c�RW�Y\�o�o�^�^�^�^�� W�T�#�Y�Y� W� W�RU� W� W�X�X�Xs�D�!E�E)r��deepcopyr�rr�r�)r'r�r�r�s @@rg�_from_yaml_listzFeatures._from_yaml_list�s������M�)�,�,� � �� �� � � � �6 Y��t�T�z�!2� Y�u�T�4�Z�7H� Y� Y� Y� Y� Y� Y� Y�D�}�}�_�_�Y�7�7�8�8�8rhc�@�t|��}t||��S)z� Encode example into a format for Arrow. Args: example (`dict[str, Any]`): Data in a Dataset row. Returns: `dict[str, Any]` )r�r&)r��examples rgr�zFeatures.encode_example�s!��)��1�1��$�T�7�3�3�3rh� column_namec�B���t|��}��fd�|D��S)a Encode column into a format for Arrow. Args: column (`list[Any]`): Data in a Dataset column. column_name (`str`): Dataset column name. Returns: `list[Any]` c�@��g|]}t��|d�����Sr#r()r�r�r�r�s ��rgr�z*Features.encode_column.<locals>.<listcomp> s.���Y�Y�Y�3�%�d�;�&7��A�F�F�F�Y�Y�Yrh)r��r��columnr�s` `rg� encode_columnzFeatures.encode_column�s1����(��/�/��Y�Y�Y�Y�Y�RX�Y�Y�Y�Yrhc�"���i}t|��t���kr/tdt|���dt��������|���D]&\�}t|��}��fd�|D��|�<�'|S)z� Encode batch into a format for Arrow. Args: batch (`dict[str, list[Any]]`): Data in a Dataset batch. Returns: `dict[str, list[Any]]` zColumn mismatch between batch z and features c�@��g|]}t��|d�����Sr#r()r�r�r�r�s ��rgr�z)Features.encode_batch.<locals>.<listcomp>s.���!c�!c�!c�UX�"7��S� �3�a�"P�"P�"P�!c�!c�!crh)�setrXr�r�)r��batch� encoded_batchr�r�s` @rg� encode_batchzFeatures.encode_batchs������ � �u�:�:��T��� "� "��c�c�%�j�j�c�c�X[�\`�Xa�Xa�c�c�d�d� d� �;�;�=�=� d� d�K�C��+�F�3�3�F�!c�!c�!c�!c�!c�\b�!c�!c�!c�M�#� � ��rhNr�r5c�~������fd�t�fd�����D�����D��S)a�Decode example with custom feature decoding. Args: example (`dict[str, Any]`): Dataset row data. token_per_repo_id (`dict`, *optional*): To access and decode audio or image files from private repositories on the Hub, you can pass a dictionary `repo_id (str) -> token (bool or str)`. Returns: `dict[str, Any]` c�`��i|]*\}\}}|�j|rt||����n|��+S)rB)r�r9)r�r�rr�r�r5s ��rgr�z+Features.decode_example.<locals>.<dictcomp>/s]��� � � �.� �-�g�u� ��-�k�:��.�w��Qb�c�c�c�c�� � � rhc�$��i|] \}}|�v� ||�� Sr�r�)r�r�r�r�s �rgr�z+Features.decode_example.<locals>.<dictcomp>4s$���M�M�M� ��U�c�W�n�n��e�n�n�nrh)rr�)r�r�r5s```rgr@zFeatures.decode_example!sb����� � � � � �2:�M�M�M�M�d�j�j�l�l�M�M�M�w�2�2�  � � � rhr�c�B����j�r��fd�|D��n|S)z�Decode column with custom feature decoding. Args: column (`list[Any]`): Dataset column data. column_name (`str`): Dataset column name. Returns: `list[Any]` c�D��g|]}|�t��|��nd��Sr�r8)r�r�r�r�s ��rgr�z*Features.decode_column.<locals>.<listcomp>Es5��� p� p� p�`e��@Q� "�4� �#4�e� <� <� <�W[� p� p� prh)r�r�s` `rg� decode_columnzFeatures.decode_column8s<�����-�k�:� � p� p� p� p� p�io� p� p� p� p�� rhr�c�����i}|���D]'\�}�j�r���fd�|D��n||�<�(|S)a�Decode batch with custom feature decoding. Args: batch (`dict[str, list[Any]]`): Dataset batch data. token_per_repo_id (`dict`, *optional*): To access and decode audio or image files from private repositories on the Hub, you can pass a dictionary repo_id (str) -> token (bool or str) Returns: `dict[str, list[Any]]` c�H��g|]}|�t��|����nd��S)NrBr8)r�r�r�r�r5s ���rgr�z)Features.decode_batch.<locals>.<listcomp>ZsN��������(�*�$�{�*;�U�Vg�h�h�h�h����rh)r�r�)r�r�r5� decoded_batchr�r�s` ` @rg� decode_batchzFeatures.decode_batchJs������� �#(�;�;�=�=� � � �K���1�+�>� �������"(� ����� �+� &� &��rhc�*�tj|��S)a� Make a deep copy of [`Features`]. Returns: [`Features`] Example: ```py >>> from datasets import load_dataset >>> ds = load_dataset("cornell-movie-review-data/rotten_tomatoes", split="train") >>> copy_of_features = ds.features.copy() >>> copy_of_features {'label': ClassLabel(names=['neg', 'pos'], id=None), 'text': Value(dtype='string', id=None)} ``` )r�r�r�s rgr�z Features.copyes��$�}�T�"�"�"rhr�c�B��d�fd� �t�||����S)a� Reorder Features fields to match the field order of other [`Features`]. The order of the fields is important since it matters for the underlying arrow data. Re-ordering the fields allows to make the underlying arrow data type match. Args: other ([`Features`]): The other [`Features`] to align with. Returns: [`Features`] Example:: >>> from datasets import Features, Sequence, Value >>> # let's say we have two features with a different order of nested fields (for a and b for example) >>> f1 = Features({"root": Sequence({"a": Value("string"), "b": Value("string")})}) >>> f2 = Features({"root": {"b": Sequence(Value("string")), "a": Sequence(Value("string"))}}) >>> assert f1.type != f2.type >>> # re-ordering keeps the base structure (here Sequence is defined at the root level), but makes the fields order match >>> f1.reorder_fields_as(f2) {'root': Sequence(feature={'b': Value(dtype='string', id=None), 'a': Value(dtype='string', id=None)}, length=-1, id=None)} >>> assert f1.reorder_fields_as(f2).type == f2.type �c �^������r d�dd�znd}t�t��r>�j�t�t��rd�����D���n�g�t�t��r�t ������}|�d���t�t��rTd�����D���������}td�|���D��fi|��S�g�������}t|dfi|��St�t��r�t�t��std ��d ���|z���t���t���krod ��d ��d �� ���� ��z �d�� ���� ��z �d� |z}t|�������fd��D��St�t��r�t�t��std ��d ���|z���t���t���krtd��d ���|z�������fd�tt�����D��St�t��rQt�t��std ��d ���|z���t��j�j�����S�S)Nz at rr�c��i|] \}}||g�� Sr�r��r�r�r�s rgr�zIFeatures.reorder_fields_as.<locals>.recursive_reorder.<locals>.<dictcomp>�� ��@�@�@���A�a�!��@�@�@rhrc��i|] \}}||g�� Sr�r�rs rgr�zIFeatures.reorder_fields_as.<locals>.recursive_reorder.<locals>.<dictcomp>�rrhc�&�i|]\}}||d��S�rr�rs rgr�zIFeatures.reorder_fields_as.<locals>.recursive_reorder.<locals>.<dictcomp>�s"��$K�$K�$K���A�Q��!��$K�$K�$KrhrzType mismatch: between rmzKeys mismatch: between z (source) and z (target). z are missing from target and z are missing from sourcec �P��i|]"}|��|�|�d|��z����#S�r8r�)r�r��recursive_reorder�source�stack�targets ����rgr�zIFeatures.reorder_fields_as.<locals>.recursive_reorder.<locals>.<dictcomp>�sC���n�n�n�`c��.�.�v�c�{�F�3�K��QZ�UX�QZ�QZ�IZ�[�[�n�n�nrhzLength mismatch: between c�H��g|]}��|�|�dz����S)z.<list>r�)r�r\rr r r s ����rgr�zIFeatures.reorder_fields_as.<locals>.recursive_reorder.<locals>.<listcomp>�s7���o�o�o�WX�)�)�&��)�V�A�Y�� �@Q�R�R�o�o�orh)r�rrr�r��varsr�rVrXr�rWr�rnrr)r r r �stack_position�sequence_kwargs� reordered�messagers``` �rgrz5Features.reorder_fields_as.<locals>.recursive_reorder�s�������38�@�V�e�A�B�B�i�/�/�b�N��&�(�+�+� &�����f�d�+�+�&�@�@������@�@�@�F�F�$�X�F��&�(�+�+�! �"&�v�,�,�"3�"3�"5�"5��(�,�,�Y�7�7���f�d�+�+�E�@�@������@�@�@�F� 1� 1�&�&�%� H� H�I�#�$K�$K����9J�9J�$K�$K�$K�_�_��_�_�_�$�X�F� 1� 1�&�&�%� H� H�I�#�I�a�L�D�D�O�D�D�D��F�D�)�)� �!�&�$�/�/�g�$�%T�v�%T�%T�F�%T�%T�We�%e�f�f�f��&�>�>�V�F�^�^�3�3�W�&�W�W��W�W�!�;�;�=�=�6�;�;�=�=�8�W�W�%�{�{�}�}�v�{�{�}�}�<�W�W�W�Yg�h�� %�W�-�-�-�n�n�n�n�n�n�n�gm�n�n�n�n��F�D�)�)� �!�&�$�/�/�g�$�%T�v�%T�%T�F�%T�%T�We�%e�f�f�f��v�;�;�#�f�+�+�-�-�$�%V��%V�%V�f�%V�%V�Yg�%g�h�h�h�o�o�o�o�o�o�o�\a�be�fl�bm�bm�\n�\n�o�o�o�o��F�I�.�.� �!�&�)�4�4�g�$�%T�v�%T�%T�F�%T�%T�We�%e�f�f�f� �!2�!2�6�>�6�>�SX�!Y�!Y�Z�Z�Z�� rh)r�)r)r�r�rs @rg�reorder_fields_aszFeatures.reorder_fields_asys@���6) �) �) �) �) �) �V�)�)�$��6�6�7�7�7rh�c����td|��D�]h}d}|���}|���D�]3\�}t|t��r9d}|��fd�|���D����|�=�Tt|t ��rXt|jt��r>d}|��fd�|j���D����|�=��t|d��rb|� ��|krJd}|��fd�|� �����D����|�=��5|}|rn��j|S)abFlatten the features. Every dictionary column is removed and is replaced by all the subfields it contains. The new fields are named by concatenating the name of the original column and the subfield name like this: `<original>.<subfield>`. If a column contains nested dictionaries, then all the lower-level subfields names are also concatenated to form new columns: `<original>.<subfield>.<subsubfield>`, etc. Returns: [`Features`]: The flattened features. Example: ```py >>> from datasets import load_dataset >>> ds = load_dataset("rajpurkar/squad", split="train") >>> ds.features.flatten() {'answers.answer_start': Sequence(feature=Value(dtype='int32', id=None), length=-1, id=None), 'answers.text': Sequence(feature=Value(dtype='string', id=None), length=-1, id=None), 'context': Value(dtype='string', id=None), 'id': Value(dtype='string', id=None), 'question': Value(dtype='string', id=None), 'title': Value(dtype='string', id=None)} ``` rTFc�&��i|] \}}��d|��|��Srr��r�r�r�r�s �rgr�z$Features.flatten.<locals>.<dictcomp>�s-���%]�%]�%]�$�!�Q��&:�&:�q�&:�&:�A�%]�%]�%]rhc�p��i|]2\}}��d|��t|t��st|��n|g��3Sr)r�r�rrs �rgr�z$Features.flatten.<locals>.<dictcomp>�s\������ $��1� +�0�0�Q�0�0�Z�PQ�SW�EX�EX�2a�(�1�+�+�+�_`�^a���rhrgc�&��i|] \}}��d|��|��Srr�rs �rgr�z$Features.flatten.<locals>.<dictcomp>�s-���%g�%g�%g�$�!�Q��&:�&:�q�&:�&:�A�%g�%g�%grh) rr�r�r�r��updaterrr�rg)r�� max_depth�depth� no_change� flattened� subfeaturer�s @rgrgzFeatures.flatten�s����4�1�i�(�(� � �E��I�� � � � �I�+/�:�:�<�<� /� /�'� �Z��j�$�/�/�/� %�I��$�$�%]�%]�%]�%]�*�JZ�JZ�J\�J\�%]�%]�%]�^�^�^�!�+�.�.�� �H�5�5� /�*�Z�EW�Y]�:^�:^� /� %�I��$�$�����(2�(:�(@�(@�(B�(B������� "�+�.�.��Z��3�3�/� �8J�8J�8L�8L�PZ�8Z�8Z� %�I��$�$�%g�%g�%g�%g�*�J\�J\�J^�J^�Jd�Jd�Jf�Jf�%g�%g�%g�h�h�h�!�+�.���D�� ��� �� rh)r(rr�)r�rr(r)r))r�rrrrr�r�r�� __delitem__r� setdefaultrV�popitem�clearr-r�rXr�r;rQ�Schemar�r�r�r�r�r�r�rSr�r�rrr+r@r�r�r�rrg� __classcell__)r s@rgrr�s����������@ � � � � �-�,�T�-=�>�>�K�,�,�T�-=�>�>�K� '� '�� � 4� 4�F�+�+�D�O�<�<�J� $� $�T�X� .� .�C�(�(���6�6�G� &� &�t�z� 2� 2�E�'�'�'��%�%��X�%��\�\��X�\���"�)�� �����[��B�����[��8���HA�t�HA�HA�HA�HA�T�B9��B9��B9�B9�B9��[�B9�H 4� 4� 4�Z��Z�Z�Z�Z� ���& � �d� �x��S�RW�X[�]a�cg�Xg�Rh�Mh�Hi�?j� � � � �. �D� �s� � � � �$��$��8�D��e�TW�Y]�_c�Tc�Nd�Id�De�;f�����6#�#�#�#�(F8�F8�F8�F8�P2�2�2�2�2�2�2�2�2rhr� features_listc�@��i�|D]�}|���D]t\}}|�vr6t|t��r!t�||g��d�|<�?|�vs,t�|t��r�|jdkr|�|<�u���fd�|D��S)zoAlign dictionaries of features so that the keys that are found in multiple dictionaries share the same feature.rr*c�j��g|]/}t�fd�|���D������0S)c�"��i|] }|�|�� Sr�r�)r�r�� name2features �rgr�z._align_features.<locals>.<listcomp>.<dictcomp> s���B�B�B�Q�a��a��B�B�Brh)rrW)r�r�r)s �rgr�z#_align_features.<locals>.<listcomp> s>��� b� b� b��H�B�B�B�B�(�-�-�/�/�B�B�B� C� C� b� b� brh)r�r�r��_align_featuresr�rp)r%r�r�r�r)s @rgr*r*�s�����L�!�$�$���N�N�$�$� $� $�D�A�q��L� � �Z��4�%8�%8� �"1�<��?�A�2F�"G�"G��"J� �Q����,�&�&�:�l�1�o�u�+M�+M�&�R^�_`�Ra�Rg�kq�Rq�Rq�"#� �Q���  $� c� b� b� b�Ta� b� b� b�brhc ��i}|D]Q}|���D]:\}}||vs,t||t��r||jdkr|||<�;�R|D]�}|���D]�\}}t|t��r3t||t��rt |||g���Mt|t��r |jdks.|||kr"t d|�d|�d|�d||�d� �������dS)z�Check if the dictionaries of features can be aligned. Two dictonaries of features can be aligned if the keys they share have the same type or some of them is of type `Value("null")`. r*z.The features can't be aligned because the key z of features z has unexpected type - z (expected either z or Value("null").N)r�r�r�rpr��!_check_if_features_can_be_alignedrX)r%r)r�r�r�s rgr,r, s��� �L�!�$�$���N�N�$�$� $� $�D�A�q�� �$�$��L��O�U�)K�)K�$�P\�]^�P_�Pe�io�Po�Po�"#� �Q��� $�"�����N�N�$�$� � �D�A�q��!�T�"�"� �z�,�q�/�4�'H�'H� �1�<��?�A�2F�G�G�G�G� ��E�*�*� �q�w�&�/@�/@�l�ST�o�YZ�FZ�FZ� �p�a�p�p�V^�p�p�wx�p�p�MY�Z[�M\�p�p�p�����  ��rh)FTr�r�r)�rr�r!r�r��collections.abcrrrr�r�� dataclassesrrrr � functoolsr r �operatorr �typingr rrrrrr�r��pandasr�rArQ�pyarrow.compute�computer�� pyarrow.types�pandas.api.extensionsr�PandasExtensionArrayr�PandasExtensionDtyper�r�namingrr�tabler�utilsrr�utils.py_utilsrrr�audior �imager!r"�pdfr#� translationr$r%�videor&� get_loggerr�rHrNrSrer�r+r�r�r�r�rrrrrr rr=rArDrG�register_extension_typerQr2r9r�r�r�rr�r�� FeatureTyper�rr&r9rDrrXrJrNr[r�rerir�rprsrurxrzr� _VisitPathr�r�r�r�r�rr*r,r�rhrg�<module>rFs3 �� b�a�a� � � � � � � � � � � � � � � � �-�-�-�-�-�-�-�-�3�3�3�3�3�3�1�1�1�1�1�1�9�9�9�9�9�9�9�9�9�9�9�9�#�#�#�#�#�#�#�#�������D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�����������������������H�H�H�H�H�H�H�H�H�H�H�H�������C�C�C�C�C�C�C�C�������)�)�)�)�)�)�)�)�C�C�C�C�C�C�C�C�C�C�������*�*�*�*�*�*�*�*�������B�B�B�B�B�B�B�B������� �� �H� %� %��?`���?`��?`�?`�?`�?`�DR�C�R�B�K�R�R�R�R�ji��i��i�VZ�i�_d�eh�jn�en�_o�i�i�i�i�X � �� �]`� � � � �2 �H�H�H�H�H�H�H� ��H�V � � � � � � � � �B�B�B�B�B�h�B�B� ��B�0 �B�B�B�B�B�h�B�B� ��B�0 �B�B�B�B�B�h�B�B� ��B�0 �B�B�B�B�B�h�B�B� ��B�0+:�+:�+:�+:�+:�B�,�+:�+:�+:�\�����0���������0���������0���������0���� ���/�/���@�@�A�A�A����/�/� �7�C�C�D�D�D����/�/� �g�F�F�G�G�G����/�/���I�I�J�J�J�r�r�� �r�T�r�d�r�r�r�r�,:&�:&�:&�:&�:&�"�+�:&�:&�:&�z � � � � � 4� � � �De1�e1�e1�e1�e1� 4�e1�e1�e1�P;�;�;�  �BS�BS�BS�BS�BS�BS�BS� ��BS�J �C�C�C�C�C�C�C� ��C�: �D�D�D�D�D�D�D� ��D�"��� � ��� � � � � � � � � � ��! �� �*��X�k�5J��VZ�����,#�K�#�B�K�#�#�#�#�LT�T�T�T�n0�0�(�4��U�SV�X\�^b�Sb�Mc�Hc�Cd�:e�0�0�0�0�h �N�E������+� �)�+G� �� � ��x� ��g� ��g� ��g� ��g� �N�E� �N�E� �N�E��L�#�*���S�+�%�&����$� /�� /�� /� /� /��� /� L�C� L� L� L� L�FI�b�k�I�k�I�I�I�I�> � �B�J� �b�k� �R�\� � � � � 5�$�x���7I�2J� 5�r�|� 5� 5� 5� 5�'�'��b�j�1A�'���'�`b�`l�'�'�'�'�������" w� w�E�"�*�d�2B�,C� w�2�;� w�bd�bn� w� w� w� w� 5�s� 5�-B� 5�r�x� 5� 5� 5� 5� +�K�+�x�� �x� �?T�0T�'U�+�Ze�+�+�+�+�.�%��W�Q�Z��(� )� �vx�$+�$+� �$+� (�+�z�)B�H�[�DY�)Y� Z�$+�hr�$+��$+�$+�$+�$+�N � �k� �D� �UY� � � � �00�+�0�$�0�0�0�0�(1�;�1�4�1�1�1�1�(���,_�_�_�_�_�t�_�_�_�D c�4��>� c�d�8�n� c� c� c� c��T�(�^������rh
Memory