
    x-j6                    d   d dl mZ d dlmZmZmZmZ d dlZddl	m
Z
 ddlmZ erd dlmZmZmZmZ d dlmZ d dlmZ  ed	          Z G d
 dee                   Z G d dee                   Z G d dee                   ZddZ G d dee                   Z G d dee                   ZdS )    )annotations)TYPE_CHECKINGAnyGenericTypeVarN   )core)randperm)	GeneratorIteratorSequenceSized)Tensor_Tc                  @    e Zd ZU dZded<   dddZdd	ZerddZdS dS )Samplera  
    An abstract class to encapsulate methods and behaviors of samplers.

    All sampler used by :code:`paddle.io.BatchSampler` should be a subclass
    of :code:`paddle.io.Sampler`, BatchSampler subclasses should
    implement following methods:

    :code:`__iter__`: return sample index iterably, which iterate over indices
    of dataset elements

    :code:`__len__`: the number of sample in :attr:`data_source`


    Args:
        data_source(Dataset, optional): this could be an instance of
                :code:`paddle.io.Dataset` other Python object which
                implemented :code:`__len__` for Sampler to get indices
                as the range of :attr:`dataset` length. Default None.

    Returns:
        Sampler: an iterable object for sample indices iterating

    Examples:

        .. code-block:: python

            >>> import numpy as np
            >>> from paddle.io import Dataset, Sampler

            >>> class RandomDataset(Dataset):  # type: ignore[type-arg]
            ...     def __init__(self, num_samples):
            ...         self.num_samples = num_samples
            ...
            ...     def __getitem__(self, idx):
            ...         image = np.random.random([784]).astype('float32')
            ...         label = np.random.randint(0, 9, (1, )).astype('int64')
            ...         return image, label
            ...
            ...     def __len__(self):
            ...         return self.num_samples
            ...
            >>> class MySampler(Sampler):  # type: ignore[type-arg]
            ...     def __init__(self, data_source):
            ...         self.data_source = data_source
            ...
            ...     def __iter__(self):
            ...         return iter(range(len(self.data_source)))  # type: ignore[arg-type]
            ...
            ...     def __len__(self):
            ...         return len(self.data_source)  # type: ignore[arg-type]
            ...
            >>> sampler = MySampler(data_source=RandomDataset(100))

            >>> for index in sampler:
            ...     print(index)
            0
            1
            2
            ...
            99

    see `paddle.io.BatchSampler`
    see `paddle.io.DataLoader`

    Sized | Nonedata_sourceNreturnNonec                    || _         d S Nr   selfr   s     \/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/io/dataloader/sampler.py__init__zSampler.__init__m       &    Iterator[_T]c                    t           r   )NotImplementedErrorr   s    r   __iter__zSampler.__iter__p   s    !!r   intc                    d S r    r#   s    r   __len__zSampler.__len__w   s      r   r   )r   r   r   r   )r   r    r   r%   )	__name__
__module____qualname____doc____annotations__r   r$   r   r(   r'   r   r   r   r   (   sz         @ @D ' ' ' ' '" " " "
  &%%%%%%& &r   r   c                  6    e Zd ZU dZded<   ddZddZdd
ZdS )SequenceSamplera4  
    Iterate samples sequentially, yield :code:`0, 1, 2, ..., len(data_source) -1`
    generally,

    Args:
        data_source(Dataset): dataset to sample, this could be an
                instance of :code:`paddle.io.Dataset` other Python
                object which implemented :code:`__len__`.

    Returns:
        Sampler: a Sampler yield sample index sequentially

    Examples:

        .. code-block:: python

            >>> import numpy as np
            >>> from paddle.io import Dataset, SequenceSampler

            >>> class RandomDataset(Dataset):  # type: ignore[type-arg]
            ...     def __init__(self, num_samples):
            ...         self.num_samples = num_samples
            ...
            ...     def __getitem__(self, idx):
            ...         image = np.random.random([784]).astype('float32')
            ...         label = np.random.randint(0, 9, (1, )).astype('int64')
            ...         return image, label
            ...
            ...     def __len__(self):
            ...         return self.num_samples
            ...
            >>> sampler = SequenceSampler(data_source=RandomDataset(100))

            >>> for index in sampler:
            ...     print(index)
            0
            1
            2
            ...
            99

    see `paddle.io.Sampler`
    r   r   r   r   c                    || _         d S r   r   r   s     r   r   zSequenceSampler.__init__   r   r   Iterator[int]c                ^    t          t          t          | j                                      S r   )iterrangelenr   r#   s    r   r$   zSequenceSampler.__iter__   s#    E#d.//00111r   r%   c                *    t          | j                  S r   )r6   r   r#   s    r   r(   zSequenceSampler.__len__   s    4#$$$r   N)r   r   r   r   r   r2   r)   r*   r+   r,   r-   r.   r   r$   r(   r'   r   r   r0   r0   z   sk         * *X ' ' ' '2 2 2 2% % % % % %r   r0   c                  j    e Zd ZU dZded<   ded<   ded<   	 	 	 dddZedd            ZddZddZ	d	S )RandomSamplera  
    Iterate samples randomly, yield shuffled indices, if :attr:`replacement=False`,
    yield shuffled indices of the whole data source, if :attr:`replacement=True`,
    :attr:`num_samples` can set to specify the sample number to draw.

    Args:
        data_source(Dataset): dataset to sample, this could be an
                instance of :ref:`api_paddle_io_Dataset` or :ref:`api_paddle_io_IterableDataset` or other Python
                object which implemented :code:`__len__` to get indices as the range of :code:`dataset` length. Default None.
        replacement(bool, optional): If False, sample the whole dataset, If True,
                set :attr:`num_samples` for how many samples to draw. Default False.
        num_samples(int, optional): set sample number to draw. Default None, which is set to the length of `data_source`.
        generator(Generator, optional): specify a generator to sample the :code:`data_source`. Default None, disabled.

    Returns:
        RandomSampler: a Sampler yield sample index randomly.

    Examples:

        .. code-block:: python

            >>> import numpy as np
            >>> from paddle.io import Dataset, RandomSampler

            >>> np.random.seed(2023)
            >>> class RandomDataset(Dataset):  # type: ignore[type-arg]
            ...     def __init__(self, num_samples):
            ...         self.num_samples = num_samples
            ...
            ...     def __getitem__(self, idx):
            ...         image = np.random.random([784]).astype('float32')
            ...         label = np.random.randint(0, 9, (1, )).astype('int64')
            ...         return image, label
            ...
            ...     def __len__(self):
            ...         return self.num_samples
            ...
            >>> sampler = RandomSampler(data_source=RandomDataset(100))

            >>> for index in sampler:
            ...     print(index)
            56
            12
            68
            ...
            87
    r   r   boolreplacement!Generator[int, None, None] | None	generatorFNnum_samples
int | Noner   r   c                   || _         || _        || _        || _        t	          | j        t
                    st          d| j                   | j        sI| j        t          | j                   k    r,t          d| j         dt          | j                              t	          | j        t                    r| j        dk    rt          d| j                   d S )Nz:expect boolean value for replacement, but got replacement=zunum_samples should be smaller than or equal to length of data_source when replacement is False, but got num_samples: z > data_source: r   z>num_samples should be a positive integer, but got num_samples=)r   r=   _num_samplesr?   
isinstancer<   	TypeErrorr@   r6   
ValueErrorr%   )r   r   r=   r@   r?   s        r   r   zRandomSampler.__init__   s    '&'"$*D11 	2#/2 2  
  	D$4s4;K7L7L$L$Lb(,(8b bJMdN^J_J_b b  
 $*C00 	D4D4I4I:'+'7: :   5J4Ir   r%   c                F    | j         t          | j                  S | j         S r   )rC   r6   r   r#   s    r   r@   zRandomSampler.num_samples  s%    $t'(((  r   r2   c              #  $  K   t          | j                  }| j        rEt          | j                  D ].}	 t          | j                  }n# t          $ r Y  d S w xY w|V  /d S | j        rTt          j	        
                    t          j        |          | j        d                                          D ]}|V  d S t          j	        
                    t          j        |          | j        d                                          D ]}|V  d S )NT)replaceF)r6   r   r?   r5   r@   nextStopIterationr=   nprandomchoicearangetolist)r   niindexs       r   r$   zRandomSampler.__iter__  sH      !!> 	 4+,,   00EE$   FFF   	 Y--IaLL$"2D .  &((   E  KKKK   
  Y--IaLL$"2E .  &((   E  KKKK   s   A


AAc                    | j         S r   )r@   r#   s    r   r(   zRandomSampler.__len__!  s    r   )FNN)
r   r   r=   r<   r@   rA   r?   r>   r   r   r)   r8   )
r*   r+   r,   r-   r.   r   propertyr@   r$   r(   r'   r   r   r;   r;      s         . .` 0000
 ""&7;    < ! ! ! X!
       *           r   r;   Tc                j   t          | t          j                  r|                                 } t          | t          t
          f          rt          j        |           } t          | t          j                  s
J d            t          | j
                  dk    s
J d            |                     d| j
        d         f          } t          j        | dk              s
J d            t          j        | t          j        k              r
J d            t          j        | t          j        k              r
J d            t          j        | dk    d	
          }t          j        |dk              s
J d            |s"t          j        ||k              s
J d            | |                     d	
          z  } g }t#          | j
        d                   D ]J}t          j                            | j
        d	         ||| |                   }|                    |           Kt          j        |          S )Nz=weights should be paddle.Tensor, numpy.ndarray, list or tuple   z$weights should be a 1-D or 2-D arrayg        z weights should be positive valuezweights should not be INFzweights should not be NaN   )axisr   z#weights should have positive valueszUweights positive value number should not less than num_samples when replacement=False)rD   r	   DenseTensornumpylisttuplerL   arrayndarrayr6   shapereshapeallanyinfnansumr5   rM   rN   append)weightsr@   r=   	non_zerosretsrR   rets          r   _weighted_samplerm   %  s"   '4+,, "--//'D%=)) $(7##grz**  G * w}"""$J"""oor7=#4566G6'S.!!EE#EEE!vg'((EE*EEE(vg'((EE*EEE(w}1---I6)a-  GG"GGG  
vi;.// 	
 	
;	
 	
/
 +++GD7=#$$  iM!k;

 
 	C8D>>r   c                  N    e Zd ZU dZded<   ded<   ded<   	 dddZddZddZdS )WeightedRandomSampleraH  
    Random sample with given weights (probabilities), sample index will be in range
    [0, len(weights) - 1], if :attr:`replacement` is True, index can be sampled
    multiple times.

    Args:
        weights(numpy.ndarray|paddle.Tensor|list|tuple): sequence of weights,
                should be numpy array, paddle.Tensor, list or tuple
        num_samples(int): set sample number to draw from sampler.
        replacement(bool): Whether to draw sample with replacements, default True

    Returns:
        Sampler: a Sampler yield sample index randomly by given weights

    Examples:

        .. code-block:: python

            >>> import numpy as np
            >>> from paddle.io import WeightedRandomSampler

            >>> np.random.seed(2023)
            >>> sampler = WeightedRandomSampler(
            ...     weights=[0.1, 0.3, 0.5, 0.7, 0.2],
            ...     num_samples=5,
            ...     replacement=True
            ... )
            >>> for index in sampler:
            ...     print(index)
            2
            4
            3
            1
            1
    +npt.NDArray[Any] | Tensor | Sequence[float]ri   r%   r@   r<   r=   Tr   r   c                    t          |t                    r|dk    rt          d          t          |t                    st          d          || _        || _        || _        d S )Nr   z(num_samples should be a positive integerz%replacement should be a boolean value)rD   r%   rF   r<   ri   r@   r=   )r   ri   r@   r=   s       r   r   zWeightedRandomSampler.__init__n  so     +s++ 	I{a/?/?GHHH+t,, 	FDEEE&&r   r2   c                    t          | j        | j        | j                  }t	          |                    d                                                    S NrX   )rm   ri   r@   r=   r4   rb   rP   )r   idxss     r   r$   zWeightedRandomSampler.__iter__|  sH    L$*D,<
 
 DLL$$++--...r   c                x    t          j        | j        j                  | j        j        d         z  }| j        |z  S rs   )rL   prodri   ra   r@   )r   muls     r   r(   zWeightedRandomSampler.__len__  s3    gdl())T\-?-CC#%%r   NT)ri   rp   r@   r%   r=   r<   r   r   r8   r)   r9   r'   r   r   ro   ro   E  s         " "H 9888 !	' ' ' ' '/ / / /& & & & & &r   ro   c                  6    e Zd ZU dZded<   ddZddZdd
ZdS )SubsetRandomSamplera  
    Randomly sample elements from a given list of indices, without replacement.

    Args:
        indices (sequence): a sequence of indices

    Examples:

        .. code-block:: python

            >>> import paddle
            >>> from paddle.io import SubsetRandomSampler

            >>> paddle.seed(2023)
            >>> sampler = SubsetRandomSampler(indices=[1, 3, 5, 7, 9])

            >>> for index in sampler:
            ...     print(index)
            9
            3
            7
            5
            1

    Sequence[int]indicesr   r   c                X    t          |          dk    rt          d          || _        d S )Nr   zHThe length of `indices` in SubsetRandomSampler should be greater than 0.)r6   rF   r|   )r   r|   s     r   r   zSubsetRandomSampler.__init__  s5    w<<1Z   r   r2   c              #  p   K   t          t          | j                            D ]}| j        |         V  d S r   )r
   r6   r|   )r   rR   s     r   r$   zSubsetRandomSampler.__iter__  sF      #dl++,, 	" 	"A,q/!!!!	" 	"r   r%   c                *    t          | j                  S r   )r6   r|   r#   s    r   r(   zSubsetRandomSampler.__len__  s    4<   r   N)r|   r{   r   r   r8   r)   r9   r'   r   r   rz   rz     sj          4    " " " "! ! ! ! ! !r   rz   rx   )
__future__r   typingr   r   r   r   r\   rL   	frameworkr	   tensorr
   collections.abcr   r   r   r   numpy.typingnptpaddler   r   r   r%   r0   r;   rm   ro   rz   r'   r   r   <module>r      s   # " " " " "                            DDDDDDDDDDDD WT]]O& O& O& O& O&gbk O& O& O&d6% 6% 6% 6% 6%gcl 6% 6% 6%ro  o  o  o  o GCL o  o  o d   @?& ?& ?& ?& ?&GCL ?& ?& ?&D)! )! )! )! )!'#, )! )! )! )! )!r   