
    h                       S SK Jr  S SKJr  S SKJr  S SKJr  S SKJ	r	  S SK
Jr  \(       a/  S SKrS SKJr  S S	KJrJr  \R$                  S
:  a  S SKJr  OS SKJr   S SKrS SKJr  S/r " S S\5      rg! \ a    Sr\" \5      Sef = f)    )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                      \ rS rSr% SrS\S'   S\S'   S\S'   S	S	S
.       SS jjrSS jrSS jrSS\	R                  S.       SS jjr\SS j5       rSrg	)r   #   a)	  
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   c                  [        S5        [        U[        [        45      (       a  U/nS nU(       dI  U(       a  UR	                  U5      OUnUR                  5       U l        U R                  4U l        S U l        OUR                  " U6 n[        U5      S:  a  UOUR                  5       R                  5       U l        UR	                  [        U[        5      (       d  U(       a  UO[        UR                  5      5      nUR                  5       U l        U R                  U R                  4U l        Uc  SOUR                  U l        UR                  U l        g )Nz'`PolarsDataset` is considered unstable.   r   )r   
isinstancestrr   selectto_torchr   r   r   len	to_seriesr   columnswidth	_n_labels_n_features)selfframer   r   label_framefeature_frames         I/home/james-whalen/.local/lib/python3.13/site-packages/polars/ml/torch.py__init__PolarsDataset.__init__w   s    	HIec4[))GE(,6>ELL2EM)224DM MM+DLDK,,.K"5zA~;3H3H3Jhj K "LLx..( [001M
 *224DM MM4;;7DL*29J9J(..    c           	         [        SS/05      nU R                  U5      nS H  n[        X#[        X5      5        M     U$ )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__PolarsDataset.__copy__   sE    1#/~~k2
D L(;<
 r*   c                    S[        U 5      R                   S[        U 5       SU R                   SU R                   S[        U 5      S S3$ )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__PolarsDataset.__repr__   s]     T
##$ %I; ))* +~~&bhq\	$	
r*   T)r   r   r   c               r   U R                  5       nU(       a-  U R                  R                  [        R                  US9Ul        U R
                  bS  U(       a-  U R
                  R                  [        R                  US9Ul        UR                  UR
                  4Ul        U$ UR                  4Ul        U$ )a  
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   )r3   r   totorchfloat16r   r   )r#   r   r   r   dss        r'   halfPolarsDataset.half   s    & ]]_--**5==*VBK;;" KKNN5==NV	++ryy1BJ 	 ++BJ	r*   c                ~    U R                   R                  U R                  b  U R                  R                  S.$ SS.$ )z"Return the features/labels schema.N)r   r   )r   dtyper   r<   s    r'   schemaPolarsDataset.schema   s>     +++/;;+Bdkk''
 	
HL
 	
r*   )r"   r!   r   r   r   )r$   r   r   (str | Expr | Sequence[str | Expr] | Noner   rJ   returnNone)rK   r   )rK   r   )r   boolr   rM   r   r   rK   r   )rK   zdict[str, torch.dtype | None])r:   
__module____qualname____firstlineno____doc____annotations__r(   r3   r=   rA   preserve_formatrD   propertyrH   __static_attributes__ r*   r'   r   r   #   s    M^   ;?=A / / 8	 /
 ; / 
 /D
 ','<'<  	
 % 
< 
 
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   rV   r*   r'   <module>re      s    "   9 &  $(+
7"*%. 
r
M r
  %	C  c
$%s   
A5 5B