
    q-Ph                       d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ er*d dlZd dlmZ d d	lmZmZ ej        d
k    rd dlmZ nd dlmZ 	 d dlZd dlmZ n# e$ r dZ ee          dw xY wdgZ G d de          ZdS )    )annotations)TYPE_CHECKING)issue_unstable_warning)	DataFrame)Expr)excludeN)Sequence)Tensormemory_format)      )Self)TensorDatasetz`Required package 'torch' not installed.
Please install it using the command `pip install torch`.PolarsDatasetc                      e Zd ZU dZded<   ded<   ded<   ddd	ddZddZddZddej	        dddZ
ed d            ZdS )!r   a5
  
    TensorDataset class specialized for use with Polars DataFrames.

    .. warning::
        This functionality is considered **unstable**. It may be changed
        at any point without it being considered a breaking change.

    Parameters
    ----------
    frame
        Polars DataFrame containing the data that will be retrieved as Tensors.
    label
        One or more column names or expressions that label the feature data; results
        in `(features,label)` tuples, where all non-label columns are considered
        to be features. If no label is designated then each returned item is a
        simple `(features,)` tuple containing all row elements.
    features
        One or more column names or expressions that represent the feature data.
        If not provided, all columns not designated as labels are considered to be
        features.

    Notes
    -----
    * Integer, slice, range, integer list/Tensor Dataset indexing is all supported.
    * Designating multi-element labels is also supported.

    Examples
    --------
    >>> from torch.utils.data import DataLoader
    >>> df = pl.DataFrame(
    ...     data=[
    ...         (0, 1, 1.5),
    ...         (1, 0, -0.5),
    ...         (2, 0, 0.0),
    ...         (3, 1, -2.25),
    ...     ],
    ...     schema=["lbl", "feat1", "feat2"],
    ...     orient="row",
    ... )

    Create a Dataset from a Polars DataFrame, standardising the dtype and
    separating the label/feature columns.

    >>> ds = df.to_torch("dataset", label="lbl", dtype=pl.Float32)
    >>> ds  # doctest: +IGNORE_RESULT
    <PolarsDataset [len:4, features:2, labels:1] at 0x156B033B0>
    >>> ds.features
    tensor([[ 1.0000,  1.5000],
            [ 0.0000, -0.5000],
            [ 0.0000,  0.0000],
            [ 1.0000, -2.2500]])
    >>> ds[0]
    (tensor([1.0000, 1.5000]), tensor(0.))

    The Dataset can be used standalone, or in conjunction with a DataLoader.

    >>> dl = DataLoader(ds, batch_size=2)
    >>> list(dl)
    [[tensor([[ 1.0000,  1.5000],
              [ 0.0000, -0.5000]]),
      tensor([0., 1.])],
     [tensor([[ 0.0000,  0.0000],
              [ 1.0000, -2.2500]]),
      tensor([2., 3.])]]

    Note that the label can be given as an expression as well as a column name,
    allowing for independent transform and dtype adjustment from the feature
    columns.

    >>> ds = df.to_torch(
    ...     "dataset",
    ...     dtype=pl.Float32,
    ...     label=(pl.col("lbl") * 8).cast(pl.Int16),
    ... )
    >>> ds[:2]
    (tensor([[ 1.0000,  1.5000],
    [ 0.0000, -0.5000]]), tensor([0, 8], dtype=torch.int16))
    ztuple[Tensor, ...]tensorszTensor | Nonelabelsr
   featuresN)labelr   framer   r   (str | Expr | Sequence[str | Expr] | NonereturnNonec                  t          d           t          |t          t          f          r|g}d }|sG|r|                    |          n|}|                                | _        | j        f| _        d | _        n |j        | }t          |          dk    r|n|
                                                                | _        |                    t          |t                    s|r|nt          |j                            }|                                | _        | j        | j        f| _        |dn|j        | _        |j        | _        d S )Nz'`PolarsDataset` is considered unstable.   r   )r   
isinstancestrr   selectto_torchr   r   r   len	to_seriesr   columnswidth	_n_labels_n_features)selfr   r   r   label_framefeature_frames         O/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/polars/ml/torch.py__init__zPolarsDataset.__init__w   sI    	HIIIec4[)) 	GE(, 	86>IELL222EM)2244DM M+DLDKK&%,.K"5zzA~~;3H3H3J3Jhjj K "LLx..22:2[011 M
 *2244DM M4;7DL*29J(.    r   c           	         t          ddgi          }|                     |          }dD ]!}t          ||t          | |                     "|S )z,Return a shallow copy of this PolarsDataset.blankr   )r   r   r   r$   r%   )r   	__class__setattrgetattr)r&   dummy_framedataset_copyattrs       r)   __copy__zPolarsDataset.__copy__   s^    1#//~~k22
 	= 	=D L$d(;(;<<<<r+   r   c                    dt          |           j         dt          |            d| j         d| j         dt          |           ddS )z4Return a string representation of the PolarsDataset.<z [len:z, features:z	, labels:z] at 0xX>)type__name__r    r%   r$   idr&   s    r)   __repr__zPolarsDataset.__repr__   sr    $T

# $ $II$ $)$ $ ~$ $ hh#	$ $ $	
r+   T)r   r   r   boolr   c               2   |                                  }|r+| j                            t          j        |          |_        | j        A|r+| j                            t          j        |          |_        |j        |j        f|_        n|j        f|_        |S )ah  
        Return a copy of this PolarsDataset with the numeric data converted to f16.

        Parameters
        ----------
        features
            Convert feature data to half precision (f16).
        labels
            Convert label data to half precision (f16).
        memory_format
            Desired memory format for the modified tensors.
        )r   )r4   r   totorchfloat16r   r   )r&   r   r   r   dss        r)   halfzPolarsDataset.half   s    & ]]__ 	W-**5=*VVBK;" W KNN5=NVV	+ry1BJJ+BJ	r+   dict[str, torch.dtype | None]c                F    | j         j        | j        | j        j        nddS )z"Return the features/labels schema.N)r   r   )r   dtyper   r<   s    r)   schemazPolarsDataset.schema   s/     ++/;+Bdk''
 
 	
r+   )r   r   r   r   r   r   r   r   )r   r   )r   r   )r   r>   r   r>   r   r   r   r   )r   rE   )r:   
__module____qualname____doc____annotations__r*   r4   r=   rA   preserve_formatrD   propertyrH    r+   r)   r   r   #   s         M M^   ;?=A /  /  /  /  /  /D   
 
 
 
 ','<     < 
 
 
 X
 
 
r+   )
__future__r   typingr   polars._utils.unstabler   polars.dataframer   polars.exprr   polars.selectorsr   syscollections.abcr	   rA   r
   r   version_infor   typing_extensionstorch.utils.datar   ImportErrormsg__all__r   rO   r+   r)   <module>r^      s   " " " " " "             9 9 9 9 9 9 & & & & & &       $ $ $ $ $ $ 	+JJJ((((((++++++++
7""******%LLL....... % % %	C  +c

$% 
r
 r
 r
 r
 r
M r
 r
 r
 r
 r
s   
A A1