U Ph=@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@s6eZdZdZdddddddd Zd d d d dZdS)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. Fr strboolNone)keysmeta_keyallow_missing_keysreturncCst|||||_dSN)r__init__r)selfrrrrd/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/apps/reconstruction/transforms/dictionary.pyr/sz#ExtractDataKeyFromMetaKeyd.__init__"Mapping[Hashable, NdarrayOrTensor]dict[Hashable, Tensor]datarcCsZt|}|jD]F}|||jkr4||j|||<q|jstd|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__rr$dkeyrrr __call__3s  z#ExtractDataKeyFromMetaKeyd.__call__N)Fr) __module__ __qualname____doc__rr-rrrr rsrc s^eZdZdZejZdddddddd d d d Zdddddfdd ZdddddZZ 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. TFr Sequence[float]intrrrcenter_fractions accelerations spatial_dims is_complexrrcCs$t|||t||||d|_dSN)r7r8r9r:)rrr maskerrrr7r8r9r:rrrr rds zRandomKspaceMaskd.__init__N int | Nonenp.random.RandomState | Noneseedstatercs t|||j|||Srsuperset_random_stater<rrArBr(rr rEusz"RandomKspaceMaskd.set_random_stater!r"r#cCsLt|}||D]4}|||\||d<||d<|jj|tj<q|S)r%_maskedZ _masked_ifft)r& key_iteratorr<maskrMASKr*rrr r-|s "zRandomKspaceMaskd.__call__)r3TF)NN) r)r/r0r1r backendrrEr- __classcell__rrrGr r2Hsr2c sNeZdZdZejZdddddddd d d d Zdddddfdd 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. r3TFr r4r5rrr6cCs$t|||t||||d|_dSr;)rrrr<r=rrr rs zEquispacedKspaceMaskd.__init__Nr>r?r@cs t|||j|||SrrCrFrGrr rEsz&EquispacedKspaceMaskd.set_random_state)r3TF)NN) r)r/r0r1rrLrrErMrrrGr rNsrNc@s6eZdZdZdddddddd Zd d d d dZdS)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`. Fr rrr)rref_keyrrcCst|||||_dSr)rrrP)rrrPrrrr rsz#ReferenceBasedSpatialCropd.__init__zMapping[Hashable, Tensor]r"r#cCstt|}||jjdd}||D]H}||}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)r3Nr).0irrr sz6ReferenceBasedSpatialCropd.__call__..) roi_centerroi_size)r&rPshaperItuplerr)rr$r+rVr,imagerUcropperrrr r-s  z#ReferenceBasedSpatialCropd.__call__N)Fr.rrrr rOsrOc s\eZdZdZejZddddejdfddddddddd d fd d Zd ddddZ 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`. NFr rzNdarrayOrTensor | Nonerr r) rrP subtrahenddivisornonzero channel_wisedtyperrc s*t||t||||||_||_dSr)rDrrdefault_normalizerrP) rrrPr\r]r^r_r`rrGrr rs z*ReferenceBasedNormalizeIntensityd.__init__r!zdict[Hashable, NdarrayOrTensor]r#cCsbt|}|jjrr|jjdkr8tdd||jD}n|jj}|jjdkrhtdd||jD}n|jj}n|jjdkrt||jt r||j }q||j }n|jj}|jjdkrt||jt r||j }n||j j dd }n|jj}t|||jj|jj|jj}||d<||d<||D]}|||||<qF|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) isinstancermeanfloatitemrRvalrrr :sz>ReferenceBasedNormalizeIntensityd.__call__..cSs2g|]*}t|tr|n|jddqS)Funbiased)rbrstdrdrerfrrr rhDsFrircrk)r&rar_r\nparrayrPr]rbrrcrdrerkrr^r`rI)rr$r+r\r] normalizerr,rrr r-$sH      z*ReferenceBasedNormalizeIntensityd.__call__) r)r/r0r1rrLrlfloat32rr-rMrrrGr r[s$r[)% __future__rcollections.abcrrrnumpyrlrtorchrZ*monai.apps.reconstruction.transforms.arrayrr monai.configr r monai.config.type_definitionsr monai.transformsr Zmonai.transforms.croppad.arrayrZ monai.transforms.intensity.arrayrmonai.transforms.transformrr monai.utilsrmonai.utils.type_conversionrrr2rNrOr[rrrr  s$         )E45