U
    PhF(                     @  s4  d dl mZ d dlZd dl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 d dlmZmZ d dlmZ e
d0d
d
dddddZe
d1d
ddddddZd2ddZd
ddddddZd3d
dddddddZd
ddd d!d"Zd4dd#ddd$d%d&Zd5dd(d)d)ddd*dd(dd+d,ddd-d.d/ZdS )6    )annotationsN)Sequence)Path)overload)KeysCollectionPathLike)partition_datasetselect_cross_validation_folds)ensure_tupleFr   boolstr)base_direlement
check_pathreturnc                 C  s   d S N r   r   r   r   r   R/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/data/decathlon_datalist.py_compute_path   s    r   zlist[PathLike]z	list[str]c                 C  s   d S r   r   r   r   r   r   r      s    c                   sr   dddfdd t |ttjfr. |S t |trn|D ]}t |ttjfs<|  S q< fdd|D S |S )a^  
    Args:
        base_dir: the base directory of the dataset.
        element: file path(s) to append to directory.
        check_path: if `True`, only compute when the result is an existing path.

    Raises:
        TypeError: When ``element`` contains a non ``str``.
        TypeError: When ``element`` type is not in ``Union[list, str]``.

    r   )r   itemc                   s2   t jt j| |} r,t j|s,| S | S r   )ospathnormpathjoinexists)r   r   resultr   r   r   
_join_path/   s    z!_compute_path.<locals>._join_pathc                   s   g | ]} |qS r   r   ).0e)r   r   r   r   
<listcomp><   s     z!_compute_path.<locals>.<listcomp>)
isinstancer   r   r   list)r   r   r   r    r   )r   r   r   r   r   "   s    


z
list[dict])r   is_segmentationitemsr   c                 C  sz   |D ]p}t |ts(tdt|j d| D ]B\}}|dksL|r`|dkr`t| |dd||< q0t| |dd||< q0q|S )a(  
    Args:
        base_dir: the base directory of the dataset.
        is_segmentation: whether the datalist is for segmentation task.
        items: list of data items, each of which is a dict keyed by element names.

    Raises:
        TypeError: When ``items`` contains a non ``dict``.

    z+Every item in items must be a dict but got .imagelabelFr   T)r"   dict	TypeErrortype__name__r%   r   )r   r$   r%   r   kvr   r   r   _append_paths@   s    
r/   TtrainingzPathLike | None)data_list_file_pathr$   data_list_keyr   r   c              	   C  s   t | } |  s td|  dt| }t|}W 5 Q R X ||kr\td| d|  d|| }|dkrt|d tsdd	 |D }|d
kr| j}t	|||S )a  Load image/label paths of decathlon challenge from JSON file

    Json file is similar to what you get from http://medicaldecathlon.com/
    Those dataset.json files

    Args:
        data_list_file_path: the path to the json file of datalist.
        is_segmentation: whether the datalist is for segmentation task, default is True.
        data_list_key: the key to get a list of dictionary to be used, default is "training".
        base_dir: the base directory of the dataset, if None, use the datalist directory.

    Raises:
        ValueError: When ``data_list_file_path`` does not point to a file.
        ValueError: When ``data_list_key`` is not specified in the data list file.

    Returns a list of data items, each of which is a dict keyed by element names, for example:

    .. code-block::

        [
            {'image': '/workspace/data/chest_19.nii.gz',  'label': 0},
            {'image': '/workspace/data/chest_31.nii.gz',  'label': 1}
        ]

    zData list file  does not exist.z
Data list z not specified in "z".testr   c                 S  s   g | ]}d |iqS )r'   r   )r   ir   r   r   r!      s     z+load_decathlon_datalist.<locals>.<listcomp>N)
r   is_file
ValueErroropenjsonloadr"   r)   parentr/   )r1   r$   r2   r   	json_file	json_dataZexpected_datar   r   r   load_decathlon_datalistW   s    
r>   zSequence[str] | strr)   )data_property_file_pathproperty_keysr   c              	   C  sx   t | } |  s td|  dt| }t|}W 5 Q R X i }t|D ](}||krftd| d|| ||< qJ|S )a  Load the properties from the JSON file contains data property with specified `property_keys`.

    Args:
        data_property_file_path: the path to the JSON file of data properties.
        property_keys: expected keys to load from the JSON file, for example, we have these keys
            in the decathlon challenge:
            `name`, `description`, `reference`, `licence`, `tensorImageSize`,
            `modality`, `labels`, `numTraining`, `numTest`, etc.

    zData property file r3   zkey z" is not in the data property file.)r   r6   r7   r8   r9   r:   r
   KeyError)r?   r@   r<   r=   
propertieskeyr   r   r   load_decathlon_properties   s    
rD   r   )datalistkeysroot_dirallow_missing_keysc              	   C  s   g }| D ]}t |D ]}||kr:|std| d| qt || D ]b}t|ttjfsptd| d| dt|}t|ttjfrt||}| sF|	| qFqq|S )a|  Checks whether some files in the Decathlon datalist are missing.
    It would be helpful to check missing files before a heavy training run.

    Args:
        datalist: a list of data items, every item is a dictionary.
            usually generated by `load_decathlon_datalist` API.
        keys: expected keys to check in the datalist.
        root_dir: if not None, provides the root dir for the relative file paths in `datalist`.
        allow_missing_keys: whether allow missing keys in the datalist items.
            if False, raise exception if missing. default to False.

    Returns:
        A list of missing filenames.

    zkey `z#` is missing in the datalist item: zfilepath of key `z2` must be a string or a list of strings, but got: r&   )
r
   r7   r"   r   r   r   r   joinpathr   append)rE   rF   rG   rH   Zmissing_filesr   r-   fr   r   r   check_missing_files   s     rL   
validationintzSequence[int] | intzPath | str | NonezKeysCollection | Nonez
str | None)rE   nfoldstrain_folds	val_folds	train_keyval_keyfilenameshuffleseedcheck_missingrF   rG   rH   raise_errorc              	   C  s   |	r>|
dk	r>t | |
||}|r>d| }|r4t|t| t| |||d}t||d}t||d}||||i}t|ttfrt	|d}t
j||dd W 5 Q R X |S )aC  
    Utility to create new Decathlon style datalist based on cross validation partition.

    Args:
        datalist: loaded list of dictionaries for all the items to partition.
        nfolds: number of the kfold split.
        train_folds: indices of folds for training part.
        val_folds: indices of folds for validation part.
        train_key: the key of train part in the new datalist, defaults to "training".
        val_key: the key of validation part in the new datalist, defaults to "validation".
        filename: if not None and ends with ".json", save the new datalist into JSON file.
        shuffle: whether to shuffle the datalist before partition, defaults to `True`.
        seed: if `shuffle` is True, set the random seed, defaults to `0`.
        check_missing: whether to check all the files specified by `keys` are existing.
        keys: if not None and check_missing_files is True, the expected keys to check in the datalist.
        root_dir: if not None, provides the root dir for the relative file paths in `datalist`.
        allow_missing_keys: if check_missing_files is `True`, whether allow missing keys in the datalist items.
            if False, raise exception if missing. default to False.
        raise_error: when found missing files, if `True`, raise exception and stop, if `False`, print warning.

    Nz(some files of the datalist are missing: )datanum_partitionsrU   rV   )
partitionsfoldsw   )indent)rL   r7   warningswarnr   r	   r"   r   r   r8   r9   dump)rE   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rF   rG   rH   rX   filesmsgrY   Z
train_listval_listretrK   r   r   r    create_cross_validation_datalist   s    %

rg   )F)F)F)Tr0   N)NF)
r0   rM   NTr   FNNFT)
__future__r   r9   r   r`   collections.abcr   pathlibr   typingr   monai.configr   r   monai.data.utilsr   r	   monai.utilsr
   r   r/   r>   rD   rL   rg   r   r   r   r   <module>   sD   
   1   +          