a
    ßñhŠ'  ã                   @   sÐ   d dl Z d dlmZ d dl mZ d dlmZmZmZm	Z	m
Z
mZmZ e
ddd�ZG dd	„ d	ee ƒZG d
d„ dee ƒZG dd„ dee ƒZG dd„ dee ƒZG dd„ dee ƒZG dd„ dee	e  ƒZdS )é    N)Úint_classes)ÚTensor)ÚIteratorÚOptionalÚSequenceÚListÚTypeVarÚGenericÚSizedÚT_coT)Ú	covariantc                   @   s6   e Zd ZdZee ddœdd„Zee dœdd„Z	dS )	ÚSampleraé  Base class for all Samplers.

    Every Sampler subclass has to provide an :meth:`__iter__` method, providing a
    way to iterate over indices of dataset elements, and a :meth:`__len__` method
    that returns the length of the returned iterators.

    .. note:: The :meth:`__len__` method isn't strictly required by
              :class:`~torch.utils.data.DataLoader`, but is expected in any
              calculation involving the length of a :class:`~torch.utils.data.DataLoader`.
    N)Údata_sourceÚreturnc                 C   s   d S ©N© ©Úselfr   r   r   úX/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torch/utils/data/sampler.pyÚ__init__   s    zSampler.__init__©r   c                 C   s   t ‚d S r   )ÚNotImplementedError©r   r   r   r   Ú__iter__   s    zSampler.__iter__)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r
   r   r   r   r   r   r   r   r   r   	   s   r   c                   @   s8   e Zd ZU dZeed< dd„ Zdd„ Zedœdd	„Z	d
S )ÚSequentialSamplerzƒSamples elements sequentially, always in the same order.

    Arguments:
        data_source (Dataset): dataset to sample from
    r   c                 C   s
   || _ d S r   )r   r   r   r   r   r   ?   s    zSequentialSampler.__init__c                 C   s   t tt| jƒƒƒS r   )ÚiterÚrangeÚlenr   r   r   r   r   r   B   s    zSequentialSampler.__iter__r   c                 C   s
   t | jƒS r   )r!   r   r   r   r   r   Ú__len__E   s    zSequentialSampler.__len__N)
r   r   r   r   r
   Ú__annotations__r   r   Úintr"   r   r   r   r   r   7   s
   
r   c                   @   s^   e Zd ZU dZeed< eed< deeee ddœdd„Z	e
ed	œd
d„ƒZdd„ Zdd„ ZdS )ÚRandomSamplera:  Samples elements randomly. If without replacement, then sample from a shuffled dataset.
    If with replacement, then user can specify :attr:`num_samples` to draw.

    Arguments:
        data_source (Dataset): dataset to sample from
        replacement (bool): samples are drawn on-demand with replacement if ``True``, default=``False``
        num_samples (int): number of samples to draw, default=`len(dataset)`. This argument
            is supposed to be specified only when `replacement` is ``True``.
        generator (Generator): Generator used in sampling.
    r   ÚreplacementFN)r   r&   Únum_samplesr   c                 C   st   || _ || _|| _|| _t| jtƒs4td | j¡ƒ‚| jd urJ|sJtdƒ‚t| j	t
ƒr`| j	dkrptd | j	¡ƒ‚d S )Nú=replacement should be a boolean value, but got replacement={}zfWith replacement=False, num_samples should not be specified, since a random permute will be performed.r   úFnum_samples should be a positive integer value, but got num_samples={})r   r&   Ú_num_samplesÚ	generatorÚ
isinstanceÚboolÚ	TypeErrorÚformatÚ
ValueErrorr'   r$   )r   r   r&   r'   r+   r   r   r   r   W   s    ÿÿzRandomSampler.__init__r   c                 C   s   | j d u rt| jƒS | j S r   )r*   r!   r   r   r   r   r   r'   j   s    

zRandomSampler.num_samplesc                 c   sÄ   t | jƒ}| jd u r@t ¡ }| ttjdtjd� 	¡  
¡ ƒ¡ n| j}| jr¦t| jd ƒD ]"}tj|dtj|d� ¡ E d H  qZtj|| jd ftj|d� ¡ E d H  ntj|| jd� ¡ E d H  d S )Nr   ©Údtypeé    )r3   )ÚhighÚsizer2   r+   ©r+   )r!   r   r+   ÚtorchÚ	GeneratorZmanual_seedr$   ÚemptyÚint64Zrandom_Úitemr&   r    r'   ÚrandintÚtolistÚrandperm)r   Únr+   Ú_r   r   r   r   q   s    

$ (zRandomSampler.__iter__c                 C   s   | j S r   ©r'   r   r   r   r   r"      s    zRandomSampler.__len__)FNN)r   r   r   r   r
   r#   r-   r   r$   r   Úpropertyr'   r   r"   r   r   r   r   r%   I   s   

  ÿÿr%   c                   @   sD   e Zd ZU dZee ed< dee ddœdd„Zdd„ Zd	d
„ Z	dS )ÚSubsetRandomSamplerzÎSamples elements randomly from a given list of indices, without replacement.

    Arguments:
        indices (sequence): a sequence of indices
        generator (Generator): Generator used in sampling.
    ÚindicesN)rD   r   c                 C   s   || _ || _d S r   )rD   r+   )r   rD   r+   r   r   r   r   Œ   s    zSubsetRandomSampler.__init__c                    s$   ‡ fdd„t jtˆ jƒˆ jd�D ƒS )Nc                 3   s   | ]}ˆ j | V  qd S r   )rD   )Ú.0Úir   r   r   Ú	<genexpr>‘   ó    z/SubsetRandomSampler.__iter__.<locals>.<genexpr>r6   )r7   r>   r!   rD   r+   r   r   r   r   r   �   s    zSubsetRandomSampler.__iter__c                 C   s
   t | jƒS r   )r!   rD   r   r   r   r   r"   “   s    zSubsetRandomSampler.__len__)N)
r   r   r   r   r   r$   r#   r   r   r"   r   r   r   r   rC   ƒ   s
   
rC   c                   @   sT   e Zd ZU dZeed< eed< eed< dee	 eeddœdd	„Z
d
d„ Zdd„ ZdS )ÚWeightedRandomSamplera  Samples elements from ``[0,..,len(weights)-1]`` with given probabilities (weights).

    Args:
        weights (sequence)   : a sequence of weights, not necessary summing up to one
        num_samples (int): number of samples to draw
        replacement (bool): if ``True``, samples are drawn with replacement.
            If not, they are drawn without replacement, which means that when a
            sample index is drawn for a row, it cannot be drawn again for that row.
        generator (Generator): Generator used in sampling.

    Example:
        >>> list(WeightedRandomSampler([0.1, 0.9, 0.4, 0.7, 3.0, 0.6], 5, replacement=True))
        [4, 4, 1, 4, 5]
        >>> list(WeightedRandomSampler([0.9, 0.4, 0.05, 0.2, 0.3, 0.1], 5, replacement=False))
        [0, 1, 4, 3, 2]
    Úweightsr'   r&   TN)rJ   r'   r&   r   c                 C   sj   t |tƒrt |tƒs|dkr*td |¡ƒ‚t |tƒsBtd |¡ƒ‚tj|tjd�| _|| _	|| _
|| _d S )Nr   r)   r(   r1   )r,   Ú_int_classesr-   r0   r/   r7   Z	as_tensorÚdoublerJ   r'   r&   r+   )r   rJ   r'   r&   r+   r   r   r   r   ¬   s    ÿÿ
ÿzWeightedRandomSampler.__init__c                 C   s&   t j| j| j| j| jd�}t| ¡ ƒS )Nr6   )r7   ZmultinomialrJ   r'   r&   r+   r   r=   )r   Zrand_tensorr   r   r   r   º   s    zWeightedRandomSampler.__iter__c                 C   s   | j S r   rA   r   r   r   r   r"   ¾   s    zWeightedRandomSampler.__len__)TN)r   r   r   r   r   r#   r$   r-   r   Úfloatr   r   r"   r   r   r   r   rI   —   s   
 ÿ
ÿrI   c                   @   s8   e Zd ZdZee eeddœdd„Zdd„ Zdd	„ Z	dS )
ÚBatchSamplerai  Wraps another sampler to yield a mini-batch of indices.

    Args:
        sampler (Sampler or Iterable): Base sampler. Can be any iterable object
        batch_size (int): Size of mini-batch.
        drop_last (bool): If ``True``, the sampler will drop the last batch if
            its size would be less than ``batch_size``

    Example:
        >>> list(BatchSampler(SequentialSampler(range(10)), batch_size=3, drop_last=False))
        [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
        >>> list(BatchSampler(SequentialSampler(range(10)), batch_size=3, drop_last=True))
        [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
    N)ÚsamplerÚ
batch_sizeÚ	drop_lastr   c                 C   sX   t |tƒrt |tƒs|dkr*td |¡ƒ‚t |tƒsBtd |¡ƒ‚|| _|| _|| _d S )Nr   zDbatch_size should be a positive integer value, but got batch_size={}z9drop_last should be a boolean value, but got drop_last={})r,   rK   r-   r0   r/   rO   rP   rQ   )r   rO   rP   rQ   r   r   r   r   Ò   s    ÿÿ
ÿzBatchSampler.__init__c                 c   sN   g }| j D ]&}| |¡ t|ƒ| jkr
|V  g }q
t|ƒdkrJ| jsJ|V  d S )Nr   )rO   Úappendr!   rP   rQ   )r   ÚbatchÚidxr   r   r   r   á   s    

zBatchSampler.__iter__c                 C   s4   | j rt| jƒ| j S t| jƒ| j d | j S d S )Né   )rQ   r!   rO   rP   r   r   r   r   r"   ë   s    zBatchSampler.__len__)
r   r   r   r   r   r$   r-   r   r   r"   r   r   r   r   rN   Â   s   
rN   )r7   Z
torch._sixr   rK   r   Útypingr   r   r   r   r   r	   r
   r   r   r$   r   r%   rC   rI   rN   r   r   r   r   Ú<module>   s   $.:+