U PhF(@s4ddlmZddlZddlZddlZddlmZddlmZddl m Z ddl m Z m Z ddlmZmZddlmZe d0d d d d d ddZe d1d dd dd ddZd2ddZd d dddddZd3d d d dddddZd ddd d!d"Zd4dd#dd d$d%d&Zd5dd(d)d)d d d*d d(d d+d,d d d-d.d/ZdS)6) annotationsN)Sequence)Path)overload)KeysCollectionPathLike)partition_datasetselect_cross_validation_folds) ensure_tupleFrboolstr)base_direlement check_pathreturncCsdSNr rrrrR/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/data/decathlon_datalist.py _compute_pathsrzlist[PathLike]z list[str]cCsdSrrrrrrrscsrdddfdd t|ttjfr.|St|trn|D]}t|ttjfs<|Sq<fdd|DS|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 itemcs2tjtj||}r,tj|s,|S|Sr)ospathnormpathjoinexists)r rresultrrr _join_path/sz!_compute_path.._join_pathcsg|]}|qSrr).0e)rr rr <sz!_compute_path..) isinstancer rrlist)r rrr r)rr rrr"s    z list[dict])r is_segmentationitemsrcCsz|D]p}t|ts(tdt|jd|D]B\}}|dksL|r`|dkr`t||dd||<q0t||dd||<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 .imagelabelFrT)r"dict TypeErrortype__name__r%r)r r$r%rkvrrr _append_paths@s  r/TtrainingzPathLike | None)data_list_file_pathr$ data_list_keyr rc Cst|}|s td|dt|}t|}W5QRX||kr\td|d|d||}|dkrt|dtsdd |D}|d kr|j}t |||S) aLoad 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} ] zData list file  does not exist.z Data list z not specified in "z".testrcSsg|] }d|iqS)r'r)rirrrr!sz+load_decathlon_datalist..N) ris_file ValueErroropenjsonloadr"r)parentr/)r1r$r2r json_file json_dataZ expected_datarrrload_decathlon_datalistWs r>zSequence[str] | strr))data_property_file_path property_keysrc Csxt|}|s td|dt|}t|}W5QRXi}t|D](}||krftd|d||||<qJ|S)aLoad 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. zData property file r3zkey z" is not in the data property file.)rr6r7r8r9r:r KeyError)r?r@r<r= propertieskeyrrrload_decathlon_propertiess   rDr)datalistkeysroot_dirallow_missing_keysc Csg}|D]}t|D]}||kr:|std|d|qt||D]b}t|ttjfsptd|d|dt|}t|ttjfrt||}|sF| |qFqq|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. zkey `z#` is missing in the datalist item: zfilepath of key `z2` must be a string or a list of strings, but got: r&) r r7r"r rrrjoinpathrappend)rErFrGrHZ missing_filesrr-frrrcheck_missing_filess  rL validationintzSequence[int] | intzPath | str | NonezKeysCollection | Nonez str | None)rEnfolds train_folds val_folds train_keyval_keyfilenameshuffleseed check_missingrFrGrH raise_errorc Cs| r>| dk r>t|| | | }|r>d|}| r4t|t|t||||d}t||d}t||d}||||i}t|ttfrt |d}t j ||ddW5QRX|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: )datanum_partitionsrUrV) partitionsfoldsw)indent) rLr7warningswarnrr r"r rr8r9dump)rErOrPrQrRrSrTrUrVrWrFrGrHrXfilesmsgrYZ train_listval_listretrKrrr create_cross_validation_datalists%       rg)F)F)F)Tr0N)NF) r0rMNTrFNNFT) __future__rr9rr`collections.abcrpathlibrtypingr monai.configrrmonai.data.utilsrr monai.utilsr rr/r>rDrLrgrrrr sD      1+