U PhH@sNddlmZddlZddlmZddlmZmZddlZ ddl Z ddl m Z ddl mZddlmZddlmZdd lmZdd lmZdd lmZdd lmZmZmZdd lmZddlm Z m!Z!m"Z"m#Z#ddl$m%Z%m&Z&m'Z'm(Z(ddl)m*Z*m+Z+ddZ,e(ddrej-j./e&e,e(dde'd\Z0Z1ee2dZ3dgZ4GdddZ5dS)) annotationsN)path)Anycast) get_context)EnsureSameShaped) get_logger) SegSummarizer) datafold_read) config_parser) ConfigParser) DataLoaderDatasetpartition_dataset) no_collation)Compose EnsureTyped LoadImaged Orientationd) ImageMetaKeyStrEnum min_versionoptional_import) DataStatsKeysImageStatsKeyscCs|d|jS)Nztag:yaml.org,2002:str)represent_scalarvalue)dumperdatarW/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/apps/auto3dseg/data_analyzer.pystrenum_representer#sr!yamltqdmz4.47.0) module_name DataAnalyzerc@steZdZdZd%d ddddddddddddddddZeddddddZd&dd Zd'ddddddd"d#d$Zd S)(r&a The DataAnalyzer automatically analyzes given medical image dataset and reports the statistics. The module expects file paths to the image data and utilizes the LoadImaged transform to read the files, which supports nii, nii.gz, png, jpg, bmp, npz, npy, and dcm formats. Currently, only segmentation task is supported, so the user needs to provide paths to the image and label files (if have). Also, label data format is preferred to be (1,H,W,D), with the label index in the first dimension. If it is in onehot format, it will be converted to the preferred format. Args: datalist: a Python dictionary storing group, fold, and other information of the medical image dataset, or a string to the JSON file storing the dictionary. dataroot: user's local directory containing the datasets. output_path: path to save the analysis result. average: whether to average the statistical value across different image modalities. do_ccp: apply the connected component algorithm to process the labels/images device: a string specifying hardware (CUDA/CPU) utilized for the operations. worker: number of workers to use for loading datasets in each GPU/CPU sub-process. image_key: a string that user specify for the image. The DataAnalyzer will look it up in the datalist to locate the image files of the dataset. label_key: a string that user specify for the label. The DataAnalyzer will look it up in the datalist to locate the label files of the dataset. If label_key is NoneType or "None", the DataAnalyzer will skip looking for labels and all label-related operations. hist_bins: bins to compute histogram for each image channel. hist_range: ranges to compute histogram for each image channel. fmt: format used to save the analysis results. Currently support ``"json"`` and ``"yaml"``, defaults to "yaml". histogram_only: whether to only compute histograms. Defaults to False. extra_params: other optional arguments. Currently supported arguments are : 'allowed_shape_difference' (default 5) can be used to change the default tolerance of the allowed shape differences between the image and label items. In case of shape mismatch below the tolerance, the label image will be resized to match the image using nearest interpolation. Examples: .. code-block:: python from monai.apps.auto3dseg.data_analyzer import DataAnalyzer datalist = { "testing": [{"image": "image_003.nii.gz"}], "training": [ {"fold": 0, "image": "image_001.nii.gz", "label": "label_001.nii.gz"}, {"fold": 0, "image": "image_002.nii.gz", "label": "label_002.nii.gz"}, {"fold": 1, "image": "image_001.nii.gz", "label": "label_001.nii.gz"}, {"fold": 1, "image": "image_004.nii.gz", "label": "label_004.nii.gz"}, ], } dataroot = '/datasets' # the directory where you have the image files (nii.gz) DataAnalyzer(datalist, dataroot) Notes: The module can also be called from the command line interface (CLI). For example: .. code-block:: bash python -m monai.apps.auto3dseg \ DataAnalyzer \ get_all_case_stats \ --datalist="my_datalist.json" \ --dataroot="my_dataroot_dir" ./datastats.yamlTFcudaimagelabelrNr"z str | dictstrboolzstr | torch.deviceintz str | Nonezlist | int | Nonez list | Noner)datalistdataroot output_pathaveragedo_ccpdeviceworker image_key label_key hist_bins hist_rangefmthistogram_only extra_paramscKst|r,td|dt|d||_||_||_||_ ||_ t ||_ ||_ ||_| dkrndn| |_| |_| dkrddgn| |_| |_| |_||_dS)NzFile z( already exists and will be overwritten.z' will be overwritten by a new datastat.Nonei i)risfilewarningswarnloggerdebugr0r1r2r3r4torchr5r6r7r8r9r:r;r<r=)selfr0r1r2r3r4r5r6r7r8r9r:r;r<r=rrr __init__rs"  zDataAnalyzer.__init__z list[str]dict)keysresultreturncsvtjkstjtjkrdSfdd|D}|D]<}d|kr4t|dr4td|d|dddSq4dS) ay Check data uniformity since DataAnalyzer provides no support to multi-modal images with different affine matrices/spacings due to monai transforms. Args: keys: a list of string-type keys under image_stats dictionary. Returns: False if one of the selected key values is not constant across the dataset images. Tcs g|]}tjtj|qSr)rSUMMARY IMAGE_STATS).0keyrIrr sz7DataAnalyzer._check_data_uniformity..stdevzsummary image_stats z has non-zero stdev .F)rrKrLnpanyrBrC)rHrIZconstant_propsproprrOr _check_data_uniformitysz#DataAnalyzer._check_data_uniformitytrainingc Cs^tjitjgi}tjitjgi}|jjdkr s0