U Ph'@sddlmZddlZddlmZddlZddlZddlm Z ddl m Z ddl m Z ddlmZddlmZdd lmZdd lmZmZmZeZGd d d ZdS) ) annotationsN)chain)KeysCollection) DataLoader)Dataset) MetaTensor)affine_to_spacing) concatenate)PostFixconvert_data_typeconvert_to_tensorc@sxeZdZdZdddedfddddd d d d d ZddZd"d d ddddZd#d dddZd$d dd dddd d!Z dS)%DatasetSummarya This class provides a way to calculate a reasonable output voxel spacing according to the input dataset. The achieved values can used to resample the input in 3d segmentation tasks (like using as the `pixdim` parameter in `monai.transforms.Spacingd`). In addition, it also supports to compute the mean, std, min and max intensities of the input, and these statistics are helpful for image normalization (as parameters of `monai.transforms.ScaleIntensityRanged` and `monai.transforms.NormalizeIntensityd`). The algorithm for calculation refers to: `Automated Design of Deep Learning Methods for Biomedical Image Segmentation `_. imagelabelNrrz str | NonezKeysCollection | Nonestrint)dataset image_key label_keymeta_keymeta_key_postfix num_workerscKsBtf|d|d||_||_||_|p4|d||_g|_dS)a Args: dataset: dataset from which to load the data. image_key: key name of images (default: ``image``). label_key: key name of labels (default: ``label``). meta_key: explicitly indicate the key of the corresponding metadata dictionary. for example, for data with key `image`, the metadata by default is in `image_meta_dict`. the metadata is a dictionary object which contains: filename, affine, original_shape, etc. if None, will try to construct meta_keys by `{image_key}_{meta_key_postfix}`. This is not required if `data[image_key]` is a MetaTensor. meta_key_postfix: use `{image_key}_{meta_key_postfix}` to fetch the metadata from dict, the metadata is a dictionary object (default: ``meta_dict``). num_workers: how many subprocesses to use for data loading. ``0`` means that the data will be loaded in the main process (default: ``0``). kwargs: other parameters (except `batch_size` and `num_workers`) for DataLoader, this class forces to use ``batch_size=1``. )r batch_sizer_N)r data_loaderrrr all_meta_data)selfrrrrrrkwargsrO/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/data/dataset_summary.py__init__-s zDatasetSummary.__init__cCsd|jD]X}t||jtr(||jj}n*|j|kr>||j}ntd|jd|j |qdS)z^ This function is used to collect the metadata for all images of the dataset. z&To collect metadata for the dataset, `z` or `data.meta` must exist.N) r isinstancerrmetarwarningswarnrappend)rdata meta_dictrrr collect_meta_dataQs   z DatasetSummary.collect_meta_dataaffine$@float) spacing_keyanisotropic_threshold percentilec Cst|jdkr|||jdkr,tdg}|jD]d}t||dddd}|jdkrr||dddq6|jd kr|t|ddq6td q6t|dd }t |t j dd ^}}t j |dd } t | t| |krt | } t |dd| f|| | <t| } t| S)a  Calculate the target spacing according to all spacings. If the target spacing is very anisotropic, decrease the spacing value of the maximum axis according to percentile. The spacing is computed from `affine_to_spacing(data[spacing_key][0], 3)` if `data[spacing_key]` is a matrix, otherwise, the `data[spacing_key]` must be a vector of pixdim values. Args: spacing_key: key of the affine used to compute spacing in metadata (default: ``affine``). anisotropic_threshold: threshold to decide if the target spacing is anisotropic (default: ``3``). percentile: for anisotropic target spacing, use the percentile of all spacings of the anisotropic axis to replace that axis. rz6The provided spacing_key is not in self.all_meta_data.FT) track_meta wrap_sequencerNr+z/data[spacing_key] must be a vector or a matrix.)to_cataxis)r' output_typer2)r5)lenrr) ValueErrorr ndimr&rr r npndarraymedianmaxminargmaxr0listtuple) rr.r/r0Zspacingsr'Z spacing_valsZ all_spacingsrZtarget_spacingZ largest_axisoutputrrr get_target_spacing_s(      z!DatasetSummary.get_target_spacing)foreground_thresholdc Cs$td}td}gg}}d}|jD]}|jrP|jrP||j||j}} n|\}} t|tjd^}} t| tjd^} } |t| |k} || || |t | 7}|| 7}|t|  7}q(t |t ||_|_|| |_t|||jd |_dS)a? This function is used to calculate the maximum, minimum, mean and standard deviation of intensities of the input dataset. Args: foreground_threshold: the threshold to distinguish if a voxel belongs to foreground, this parameter is used to select the foreground of images for calculation. Normally, `label > 0` means the corresponding voxel belongs to foreground, thus if you need to calculate the statistics for whole images, you can set the threshold to ``-1`` (default: ``0``). grr'r6r3N)torch as_tensorrrrr Tensorwherer&r=itemr>r7sumsquaredata_maxdata_minZ data_meansqrtZdata_std) rrDZ voxel_sumZvoxel_square_sumZ voxel_maxZ voxel_minZvoxel_ctr'rrrZimage_foregroundrrr calculate_statisticss&       z#DatasetSummary.calculate_statisticsT ?X@bool)rD sampling_flagintervalmin_percentilemax_percentilec Csg}|jD]}|jr2|jr2||j||j}} n|\}} t|tjd^}} t| tjd^} } |t| |k} |r| dd|} || q t t |}t |||g\|_ |_t ||_dS)a This function is used to calculate the percentiles of intensities (and median) of the input dataset. To get the required values, all voxels need to be accumulated. To reduce the memory used, this function can be set to accumulate only a part of the voxels. Args: foreground_threshold: the threshold to distinguish if a voxel belongs to foreground, this parameter is used to select the foreground of images for calculation. Normally, `label > 0` means the corresponding voxel belongs to foreground, thus if you need to calculate the statistics for whole images, you can set the threshold to ``-1`` (default: ``0``). sampling_flag: whether to sample only a part of the voxels (default: ``True``). interval: the sampling interval for accumulating voxels (default: ``10``). min_percentile: minimal percentile (default: ``0.5``). max_percentile: maximal percentile (default: ``99.5``). rEN)rrrr rFrHrItolistr&r@rr:r0Zdata_min_percentileZdata_max_percentiler<Z data_median) rrDrUrVrWrXZall_intensitiesr'rrr intensitiesrrr calculate_percentiless"     z$DatasetSummary.calculate_percentiles)r*r+r,)r)rTrQrRrS) __name__ __module__ __qualname____doc__DEFAULT_POST_FIXr!r)rCrPr[rrrr r s $('r ) __future__rr$ itertoolsrnumpyr:rF monai.configrZmonai.data.dataloaderrZmonai.data.datasetrmonai.data.meta_tensorrmonai.data.utilsrmonai.transformsr monai.utilsr r r r#r`r rrrr  s