U
    Ph`                     @  s   d dl mZ d dlmZmZ d dlmZ d dlZd dl	m
Z
 d dlmZ d dlmZ d dlmZmZmZ d d	lmZmZ G d
d de
eZdS )    )annotations)CallableSequence)AnyN)Dataset)	DtypeLike)ImageReader)	LoadImageRandomizableapply_transform)MAX_SEEDget_seedc                   @  sv   e Zd ZdZdddddddejdf	ddddddd	d	d
dddddZddddZddddddZddddZ	dS )ImageDataseta<  
    Loads image/segmentation pairs of files from the given filename lists. Transformations can be specified
    for the image and segmentation arrays separately.
    The difference between this dataset and `ArrayDataset` is that this dataset can apply transform chain to images
    and segs and return both the images and metadata, and no need to specify transform to load images from files.
    For more information, please see the image_dataset demo in the MONAI tutorial repo,
    https://github.com/Project-MONAI/tutorials/blob/master/modules/image_dataset.ipynb
    NTFzSequence[str]zSequence[str] | NonezSequence[float] | NonezCallable | Noneboolr   zImageReader | str | NoneNone)image_files	seg_fileslabels	transformseg_transformlabel_transform
image_onlytransform_with_metadatadtypereaderreturnc                 O  s   |dk	r6t |t |kr6tdt | dt | d|| _|| _|| _|| _|| _|| _|rj|rjtd|| _|| _	t
|
||	f||| _| jt d d| _dS )a  
        Initializes the dataset with the image and segmentation filename lists. The transform `transform` is applied
        to the images and `seg_transform` to the segmentations.

        Args:
            image_files: list of image filenames.
            seg_files: if in segmentation task, list of segmentation filenames.
            labels: if in classification task, list of classification labels.
            transform: transform to apply to image arrays.
            seg_transform: transform to apply to segmentation arrays.
            label_transform: transform to apply to the label data.
            image_only: if True return only the image volume, otherwise, return image volume and the metadata.
            transform_with_metadata: if True, the metadata will be passed to the transforms whenever possible.
            dtype: if not None convert the loaded image to this data type.
            reader: register reader to load image file and metadata, if None, will use the default readers.
                If a string of reader name provided, will construct a reader object with the `*args` and `**kwargs`
                parameters, supported reader name: "NibabelReader", "PILReader", "ITKReader", "NumpyReader"
            args: additional parameters for reader if providing a reader name.
            kwargs: additional parameters for reader if providing a reader name.

        Raises:
            ValueError: When ``seg_files`` length differs from ``image_files``

        NzAMust have same the number of segmentation as image files: images=z, segmentations=.z7transform_with_metadata=True requires image_only=False.seedr   )len
ValueErrorr   r   r   r   r   r   r   r   r	   loaderset_random_stater   _seed)selfr   r   r   r   r   r   r   r   r   r   argskwargs r'   M/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/data/image_dataset.py__init__$   s"    (zImageDataset.__init__int)r   c                 C  s
   t | jS )N)r   r   )r$   r'   r'   r(   __len__`   s    zImageDataset.__len__z
Any | None)datar   c                 C  s   | j jtdd| _d S )Nuint32)r   )Rrandintr   r#   )r$   r,   r'   r'   r(   	randomizec   s    zImageDataset.randomize)indexc                 C  s  |    d\}}}}| jrF| | j| }| jd k	rx| | j| }n2| | j| \}}| jd k	rx| | j| \}}| jd k	rt| jtr| jj| j	d | j
rt| j||fddd\}}nt| j|dd}| jd k	r:| jd k	r:t| jtr| jj| j	d | j
r*t| j||fddd\}}nt| j|dd}| jd k	rl| j| }| jd k	rlt| j|dd}|g}|d k	r|| |d k	r|| | js|d k	r|| | js|d k	r|| t|dkr|d S t|S )	N)NNNNr   FT)	map_itemsunpack_items)r2      r   )r0   r   r!   r   r   r   
isinstancer
   r"   r#   r   r   r   r   r   appendr   tuple)r$   r1   	meta_dataZseg_meta_dataseglabelimgr,   r'   r'   r(   __getitem__f   sT    


   






zImageDataset.__getitem__)N)
__name__
__module____qualname____doc__npfloat32r)   r+   r0   r<   r'   r'   r'   r(   r      s   $<r   )
__future__r   collections.abcr   r   typingr   numpyrA   torch.utils.datar   monai.configr   monai.data.image_readerr   monai.transformsr	   r
   r   monai.utilsr   r   r   r'   r'   r'   r(   <module>   s   