o  i=@sddlmZddlmZmZmZddlZddlmZddl m Z ddl m Z m Z ddlmZmZddlmZdd lmZdd lmZdd lmZdd lmZmZdd lmZddlmZGdddeZ GdddeeZ!Gddde!Z"GdddeeZ#GdddeZ$dS)) annotations)HashableMappingSequenceN)ndarray)Tensor)EquispacedKspaceMaskRandomKspaceMask) DtypeLikeKeysCollection)NdarrayOrTensor)InvertibleTransform) SpatialCrop)NormalizeIntensity) MapTransformRandomizableTransform) FastMRIKeys)convert_to_tensorc@&eZdZdZddd d ZdddZdS)ExtractDataKeyFromMetaKeyday Moves keys from meta to data. It is useful when a dataset of paired samples is loaded and certain keys should be moved from meta to data. Args: keys: keys to be transferred from meta to data meta_key: the meta key where all the meta-data is stored allow_missing_keys: don't raise exception if key is missing Example: When the fastMRI dataset is loaded, "kspace" is stored in the data dictionary, but the ground-truth image with the key "reconstruction_rss" is stored in the meta data. In this case, ExtractDataKeyFromMetaKeyd moves "reconstruction_rss" to data. Fkeysr meta_keystrallow_missing_keysboolreturnNonecCt|||||_dSN)r__init__r)selfrrrr!q/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/apps/reconstruction/transforms/dictionary.pyr/ z#ExtractDataKeyFromMetaKeyd.__init__data"Mapping[Hashable, NdarrayOrTensor]dict[Hashable, Tensor]cCsZt|}|jD]#}|||jvr||j|||<q|js*td|d|jjdq|S) Args: data: is a dictionary containing (key,value) pairs from the loaded dataset Returns: the new data dictionary zKey `z` of transform `z=` was missing in the meta data and allow_missing_keys==False.)dictrrrKeyError __class____name__r r$dkeyr!r!r"__call__3s z#ExtractDataKeyFromMetaKeyd.__call__NF)rr rrrrrrr$r%rr&r+ __module__ __qualname____doc__rr/r!r!r!r"rs rcsHeZdZdZejZ   dd ddZ d!d"fdd Zd#ddZZ S)$RandomKspaceMaskda} Dictionary-based wrapper of :py:class:`monai.apps.reconstruction.transforms.array.RandomKspacemask`. Other mask transforms can inherit from this class, for example: :py:class:`monai.apps.reconstruction.transforms.dictionary.EquispacedKspaceMaskd`. Args: keys: keys of the corresponding items to be transformed. See also: monai.transforms.MapTransform center_fractions: Fraction of low-frequency columns to be retained. If multiple values are provided, then one of these numbers is chosen uniformly each time. accelerations: Amount of under-sampling. This should have the same length as center_fractions. If multiple values are provided, then one of these is chosen uniformly each time. spatial_dims: Number of spatial dims (e.g., it's 2 for a 2D data; it's also 2 for pseudo-3D datasets like the fastMRI dataset). The last spatial dim is selected for sampling. For the fastMRI dataset, k-space has the form (...,num_slices,num_coils,H,W) and sampling is done along W. For a general 3D data with the shape (...,num_coils,H,W,D), sampling is done along D. is_complex: if True, then the last dimension will be reserved for real/imaginary parts. allow_missing_keys: don't raise exception if key is missing. TFrr center_fractionsSequence[float] accelerations spatial_dimsint is_complexrrrrcC$t|||t||||d|_dSN)r8r:r;r=)rrr maskerr rr8r:r;r=rr!r!r"rd  zRandomKspaceMaskd.__init__Nseed int | Nonestatenp.random.RandomState | Nonec t|||j|||Srsuperset_random_stater@r rCrEr*r!r"rJuz"RandomKspaceMaskd.set_random_stater$r%r&cCsLt|}||D]}|||\||d<||d<|jj|tj<q |S)r'_maskedZ _masked_ifft)r( key_iteratorr@maskrMASKr,r!r!r"r/|s  "zRandomKspaceMaskd.__call__r7TFrr r8r9r:r9r;r<r=rrrrrNN)rCrDrErFrr6r1) r+r3r4r5r backendrrJr/ __classcell__r!r!rLr"r6Hs r6cs>eZdZdZejZ   ddddZ ddfdd ZZS)EquispacedKspaceMaskda Dictionary-based wrapper of :py:class:`monai.apps.reconstruction.transforms.array.EquispacedKspaceMask`. Args: keys: keys of the corresponding items to be transformed. See also: monai.transforms.MapTransform center_fractions: Fraction of low-frequency columns to be retained. If multiple values are provided, then one of these numbers is chosen uniformly each time. accelerations: Amount of under-sampling. This should have the same length as center_fractions. If multiple values are provided, then one of these is chosen uniformly each time. spatial_dims: Number of spatial dims (e.g., it's 2 for a 2D data; it's also 2 for pseudo-3D datasets like the fastMRI dataset). The last spatial dim is selected for sampling. For the fastMRI dataset, k-space has the form (...,num_slices,num_coils,H,W) and sampling is done along W. For a general 3D data with the shape (...,num_coils,H,W,D), sampling is done along D. is_complex: if True, then the last dimension will be reserved for real/imaginary parts. allow_missing_keys: don't raise exception if key is missing. r7TFrr r8r9r:r;r<r=rrrrcCr>r?)rrrr@rAr!r!r"rrBzEquispacedKspaceMaskd.__init__NrCrDrErFcrGrrHrKrLr!r"rJrMz&EquispacedKspaceMaskd.set_random_staterRrSrT)rCrDrErFrrW) r+r3r4r5rrUrrJrVr!r!rLr"rWs rWc@r)ReferenceBasedSpatialCropda  Dictionary-based wrapper of :py:class:`monai.transforms.SpatialCrop`. This is similar to :py:class:`monai.transforms.SpatialCropd` which is a general purpose cropper to produce sub-volume region of interest (ROI). Their difference is that this transform does cropping according to a reference image. If a dimension of the expected ROI size is larger than the input image size, will not crop that dimension. Args: keys: keys of the corresponding items to be transformed. See also: :py:class:`monai.transforms.compose.MapTransform` ref_key: key of the item to be used to crop items of "keys" allow_missing_keys: don't raise exception if key is missing. Example: In an image reconstruction task, let keys=["image"] and ref_key=["target"]. Also, let data be the data dictionary. Then, ReferenceBasedSpatialCropd center-crops data["image"] based on the spatial size of data["target"] by calling :py:class:`monai.transforms.SpatialCrop`. Frr ref_keyrrrrrcCrr)rrrY)r rrYrr!r!r"rr#z#ReferenceBasedSpatialCropd.__init__r$Mapping[Hashable, Tensor]r&cCstt|}||jjdd}||D]$}||}tdd|jddD}t||d}t|||||<q|S)a This transform can support to crop ND spatial (channel-first) data. It also supports pseudo ND spatial data (e.g., (C,H,W) is a pseudo-3D data point where C is the number of slices) Args: data: is a dictionary containing (key,value) pairs from the loaded dataset Returns: the new data dictionary Ncss|]}|dVqdS)r7Nr!).0ir!r!r" sz6ReferenceBasedSpatialCropd.__call__..) roi_centerroi_size)r(rYshaperOtuplerr)r r$r-r`r.imager_cropperr!r!r"r/s  z#ReferenceBasedSpatialCropd.__call__Nr0)rr rYrrrrr)r$rZrr&r2r!r!r!r"rXs rXcsBeZdZdZejZddddejdfdfdd ZdddZ Z S)!ReferenceBasedNormalizeIntensitydao Dictionary-based wrapper of :py:class:`monai.transforms.NormalizeIntensity`. This is similar to :py:class:`monai.transforms.NormalizeIntensityd` and can normalize non-zero values or the entire image. The difference is that this transform does normalization according to a reference image. Args: keys: keys of the corresponding items to be transformed. See also: monai.transforms.MapTransform ref_key: key of the item to be used to normalize items of "keys" subtrahend: the amount to subtract by (usually the mean) divisor: the amount to divide by (usually the standard deviation) nonzero: whether only normalize non-zero values. channel_wise: if True, calculate on each channel separately, otherwise, calculate on the entire image directly. default to False. dtype: output data type, if None, same as input image. defaults to float32. allow_missing_keys: don't raise exception if key is missing. Example: In an image reconstruction task, let keys=["image", "target"] and ref_key=["image"]. Also, let data be the data dictionary. Then, ReferenceBasedNormalizeIntensityd normalizes data["target"] and data["image"] based on the mean-std of data["image"] by calling :py:class:`monai.transforms.NormalizeIntensity`. NFrr rYr subtrahendNdarrayOrTensor | Nonedivisornonzeror channel_wisedtyper rrrc s*t||t||||||_||_dSr)rIrrdefault_normalizerrY) r rrYrfrhrirjrkrrLr!r"rs  z*ReferenceBasedNormalizeIntensityd.__init__r$r%dict[Hashable, NdarrayOrTensor]cCs^t|}|jjr9|jjdurtdd||jD}n|jj}|jjdur4tdd||jD}nS|jj}nN|jjdur[t||jt rO||j }n||j }n|jj}|jjdurt||jt ru||j }n||j j dd }n|jj}t|||jj|jj|jj}||d<||d<||D] }|||||<q|S) a This transform can support to normalize ND spatial (channel-first) data. It also supports pseudo ND spatial data (e.g., (C,H,W) is a pseudo-3D data point where C is the number of slices) Args: data: is a dictionary containing (key,value) pairs from the loaded dataset Returns: the new data dictionary NcSs.g|]}t|tr |n|qSr!) isinstancermeanfloatitemr\valr!r!r" :s.z>ReferenceBasedNormalizeIntensityd.__call__..cSs2g|]}t|tr |n |jddqS)Funbiased)rnrstdrprqrrr!r!r"rtDs$Frurorw)r(rlrjrfnparrayrYrhrnrrorprqrwrrirkrO)r r$r-rfrh normalizerr.r!r!r"r/$sH       z*ReferenceBasedNormalizeIntensityd.__call__)rr rYrrfrgrhrgrirrjrrkr rrrr)r$r%rrm) r+r3r4r5rrUrxfloat32rr/rVr!r!rLr"resre)% __future__rcollections.abcrrrnumpyrxrtorchrZ*monai.apps.reconstruction.transforms.arrayrr monai.configr r monai.config.type_definitionsr monai.transformsr monai.transforms.croppad.arrayr monai.transforms.intensity.arrayrmonai.transforms.transformrr monai.utilsrmonai.utils.type_conversionrrr6rWrXrer!r!r!r"s&         )E45