o
    #i.                     @  s   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
dBdCddZe
dBdDddZdBddZdEddZ			dFdGd!d"ZdHd'd(Z		dIdJd.d/Z		0			 							dKdLd@dAZdS )M    )annotationsN)Sequence)Path)overload)KeysCollectionPathLike)partition_datasetselect_cross_validation_folds)ensure_tupleFbase_dirr   element
check_pathboolreturnstrc                 C     d S N r   r   r   r   r   _/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/data/decathlon_datalist.py_compute_path      r   list[PathLike]	list[str]c                 C  r   r   r   r   r   r   r   r      r   c                   sl   d	fdd t |ttjfr |S t |tr4|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>N)r   r   r   r   )
isinstancer   r   r   list)r   r   r   r$   r   )r"   r   r   r   r   "   s   

is_segmentationitems
list[dict]c                 C  sz   |D ]8}t |tstdt|j d| D ]!\}}|dks&|r0|dkr0t| |dd||< qt| |dd||< qq|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   
r4   Ttrainingdata_list_file_pathdata_list_keyPathLike | Nonec                 C  s   t | } |  std|  dt| }t|}W d   n1 s$w   Y  ||vr8td| d|  d|| }|dkrNt|d tsNd	d
 |D }|du rU| j}t	|||S )a5
  Load image/label paths of decathlon challenge from JSON file

    JSON file should follow the format of the Medical Segmentation Decathlon
    datalist.json files, see http://medicaldecathlon.com.
    The files are structured as follows:

    .. code-block:: python

        {
            "metadata_key_0": "metadata_value_0",
            "metadata_key_1": "metadata_value_1",
            ...,
            "training": [
                {"image": "path/to/image_1.nii.gz", "label": "path/to/label_1.nii.gz"},
                {"image": "path/to/image_2.nii.gz", "label": "path/to/label_2.nii.gz"},
                ...
            ],
            "test": [
                "path/to/image_3.nii.gz",
                "path/to/image_4.nii.gz",
                ...
            ]
        }


    The metadata keys are optional for loading the datalist, but include:
        - some string items: ``name``, ``description``, ``reference``, ``licence``, ``release``, ``tensorImageSize``
        - two dict items: ``modality`` (keyed by channel index), and ``labels`` (keyed by label index)
        - and two integer items: ``numTraining`` and ``numTest``, with the number of items.

    The ``training`` key contains a list of dictionaries, each of which has at least
    the ``image`` and ``label`` keys.
    The image and label are loaded by :py:func:`monai.transforms.LoadImaged`, so both can be either
    a single file path or a list of file paths, in which case they are loaded as multi-channel images.
    Each item can also include a ``fold`` key for cross-validation purposes.
    The "test" key contains a list of image paths, without labels, MONAI also supports a "validation" list
    with the same format as the "training" list.


    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:: python

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

    zData list file  does not exist.Nz
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>)
r   is_file
ValueErroropenjsonloadr&   r.   parentr4   )r6   r(   r7   r   	json_file	json_dataZexpected_datar   r   r   load_decathlon_datalistW   s   A
rD   data_property_file_pathproperty_keysSequence[str] | strr.   c                 C  s   t | } |  std|  dt| }t|}W d   n1 s$w   Y  i }t|D ]}||vr=td| d|| ||< q/|S )a  Extract the properties with the specified keys from the Decathlon JSON file.
    See under `load_decathlon_datalist` for the expected keys in the Decathlon challenge.

    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 r9   Nzkey z" is not in the data property file.)r   r<   r=   r>   r?   r@   r
   KeyError)rE   rF   rB   rC   
propertieskeyr   r   r   load_decathlon_properties   s   
rK   datalistkeysr   root_dirallow_missing_keysc              	   C  s   g }| D ]R}t |D ]K}||vr|std| d| q
t || D ]1}t|ttjfs8td| d| dt|}t|ttjfrKt||}| sT|	| q#q
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
   r=   r&   r   r   r   r   joinpathr   append)rL   rM   rN   rO   Zmissing_filesr   r2   fr   r   r   check_missing_files   s&   
rS   
validationnfoldsinttrain_foldsSequence[int] | int	val_folds	train_keyval_keyfilenamePath | str | Noneshuffleseedcheck_missingKeysCollection | None
str | Noneraise_errorc                 C  s   |	r|
durt | |
||}|rd| }|rt|t| t| |||d}t||d}t||d}||||i}t|ttfr^t	|d}t
j||dd W d   |S 1 sYw   Y  |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_partitionsr^   r_   )
partitionsfoldsw   )indent)rS   r=   warningswarnr   r	   r&   r   r   r>   r?   dump)rL   rU   rW   rY   rZ   r[   r\   r^   r_   r`   rM   rN   rO   rc   filesmsgrd   Z
train_listZval_listretrR   r   r   r    create_cross_validation_datalist   s$   %


rq   )F)r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   )r   r   r(   r   r)   r*   r   r*   )Tr5   N)
r6   r   r(   r   r7   r   r   r8   r   r*   )rE   r   rF   rG   r   r.   )NF)rL   r*   rM   r   rN   r8   rO   r   )
r5   rT   NTr   FNNFT)rL   r*   rU   rV   rW   rX   rY   rX   rZ   r   r[   r   r\   r]   r^   r   r_   rV   r`   r   rM   ra   rN   rb   rO   r   rc   r   )
__future__r   r?   r   rk   collections.abcr   pathlibr   typingr   monai.configr   r   monai.data.utilsr   r	   monai.utilsr
   r   r4   rD   rK   rS   rq   r   r   r   r   <module>   sD   


S+