U
    PhD                     @  s   d Z ddlmZ ddlmZ ddlmZ ddlZddl	Z	ddl
mZmZ ddlmZ ddlmZ dd	lmZmZ dd
lmZmZ ddlmZmZmZ ddlmZ ddddddgZe ZG dd deZ G dd deZ!e  Z"Z#e! Z$Z%dS )z
A collection of dictionary-based wrappers around the "vanilla" transforms for IO functions
defined in :py:class:`monai.transforms.io.array`.

Class names are ended with 'd' to denote dictionary-based transforms.
    )annotations)Path)CallableN)	DtypeLikeKeysCollection)image_writer)ImageReader)	LoadImage	SaveImage)MapTransform	Transform)GridSamplePadModeensure_tupleensure_tuple_rep)PostFix
LoadImaged
LoadImageDLoadImageDict
SaveImaged
SaveImageDSaveImageDictc                      sz   e Zd ZdZdejdeddddddddfdddd	d
dddddd
dddd fddZddddZdddddZ	  Z
S )r   a/  
    Dictionary-based wrapper of :py:class:`monai.transforms.LoadImage`,
    It can load both image data and metadata. When loading a list of files in one key,
    the arrays will be stacked and a new dimension will be added as the first dimension
    In this case, the metadata of the first image will be used to represent the stacked result.
    The affine transform of all the stacked images should be same.
    The output metadata field will be created as ``meta_keys`` or ``key_{meta_key_postfix}``.

    If reader is not specified, this class automatically chooses readers
    based on the supported suffixes and in the following order:

        - User-specified reader at runtime when calling this loader.
        - User-specified reader in the constructor of `LoadImage`.
        - Readers from the last to the first in the registered list.
        - Current default readers: (nii, nii.gz -> NibabelReader), (png, jpg, bmp -> PILReader),
          (npz, npy -> NumpyReader), (dcm, DICOM series and others -> ITKReader).

    Please note that for png, jpg, bmp, and other 2D formats, readers by default swap axis 0 and 1 after
    loading the array with ``reverse_indexing`` set to ``True`` because the spatial axes definition
    for non-medical specific file formats is different from other common medical packages.

    Note:

        - If `reader` is specified, the loader will attempt to use the specified readers and the default supported
          readers. This might introduce overheads when handling the exceptions of trying the incompatible loaders.
          In this case, it is therefore recommended setting the most appropriate reader as
          the last item of the `reader` parameter.

    See also:

        - tutorial: https://github.com/Project-MONAI/tutorials/blob/master/modules/load_medical_images.ipynb

    NFT.r   ztype[ImageReader] | str | Noner   KeysCollection | Nonestrboolz
str | NoneNone)keysreaderdtype	meta_keysmeta_key_postfixoverwriting
image_onlyensure_channel_firstsimple_keysprune_meta_patternprune_meta_sepallow_missing_keys
expanduserreturnc              	     s   t  || t|||||	|
||f||| _t|tsNtdt|j d|dkrft	dt
| jnt|| _t
| jt
| jkrtdt
| j dt
| j dt	|t
| j| _|| _dS )a  
        Args:
            keys: keys of the corresponding items to be transformed.
                See also: :py:class:`monai.transforms.compose.MapTransform`
            reader: reader to load image file and metadata
                - if `reader` is None, a default set of `SUPPORTED_READERS` will be used.
                - if `reader` is a string, it's treated as a class name or dotted path
                (such as ``"monai.data.ITKReader"``), the supported built-in reader classes are
                ``"ITKReader"``, ``"NibabelReader"``, ``"NumpyReader"``.
                a reader instance will be constructed with the `*args` and `**kwargs` parameters.
                - if `reader` is a reader class/instance, it will be registered to this loader accordingly.
            dtype: if not None, convert the loaded image data to this data type.
            meta_keys: explicitly indicate the key to store the corresponding metadata dictionary.
                the metadata is a dictionary object which contains: filename, original_shape, etc.
                it can be a sequence of string, map to the `keys`.
                if None, will try to construct meta_keys by `key_{meta_key_postfix}`.
            meta_key_postfix: if meta_keys is None, use `key_{postfix}` to store the metadata of the nifti image,
                default is `meta_dict`. The metadata is a dictionary object.
                For example, load nifti file for `image`, store the metadata into `image_meta_dict`.
            overwriting: whether allow overwriting existing metadata of same key.
                default is False, which will raise exception if encountering existing key.
            image_only: if True return dictionary containing just only the image volumes, otherwise return
                dictionary containing image data array and header dict per input key.
            ensure_channel_first: if `True` and loaded both image array and metadata, automatically convert
                the image array shape to `channel first`. default to `False`.
            simple_keys: whether to remove redundant metadata keys, default to False for backward compatibility.
            prune_meta_pattern: combined with `prune_meta_sep`, a regular expression used to match and prune keys
                in the metadata (nested dictionary), default to None, no key deletion.
            prune_meta_sep: combined with `prune_meta_pattern`, used to match and prune keys
                in the metadata (nested dictionary). default is ".", see also :py:class:`monai.transforms.DeleteItemsd`.
                e.g. ``prune_meta_pattern=".*_code$", prune_meta_sep=" "`` removes meta keys that ends with ``"_code"``.
            allow_missing_keys: don't raise exception if key is missing.
            expanduser: if True cast filename to Path and call .expanduser on it, otherwise keep filename as is.
            args: additional parameters for reader if providing a reader name.
            kwargs: additional parameters for reader if providing a reader name.
        z&meta_key_postfix must be a str but is r   Nz3meta_keys should have the same length as keys, got z and )super__init__r	   _loader
isinstancer   	TypeErrortype__name__r   lenr   r   r   
ValueErrorr    r!   )selfr   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   argskwargs	__class__ S/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/transforms/io/dictionary.pyr+   J   s0    6	

"zLoadImaged.__init__r   )r   c                 C  s   | j | d S N)r,   register)r3   r   r8   r8   r9   r;      s    zLoadImaged.registerzImageReader | Nonec                 C  s   t |}| || j| jD ]\}}}| || |}| jjrF|||< qt|ttfsht	dt
| d|d ||< t|d t st	dt
|d  d|p| d| }||kr| jstd| d|d ||< q|S )	zr
        Raises:
            KeyError: When not ``self.overwriting`` and key already exists in ``data``.

        zLloader must return a tuple or list (because image_only=False was used), got r   r      zmetadata must be a dict, got _zMetadata with key z& already exists and overwriting=False.)dictkey_iteratorr   r    r,   r"   r-   tuplelistr2   r/   r!   KeyError)r3   datar   dkeymeta_keyr    r8   r8   r9   __call__   s"    
zLoadImaged.__call__)N)r0   
__module____qualname____doc__npfloat32DEFAULT_POST_FIXr+   r;   rG   __classcell__r8   r8   r6   r9   r   '   s    %.Mc                      s   e Zd ZdZdedddddejdejej	ddd	ddd	ddddfd
ddddddddddddddddddddddd fddZ
dddZdd Z  ZS )r   ar  
    Dictionary-based wrapper of :py:class:`monai.transforms.SaveImage`.

    Note:
        Image should be channel-first shape: [C,H,W,[D]].
        If the data is a patch of an image, the patch index will be appended to the filename.

    Args:
        keys: keys of the corresponding items to be transformed.
            See also: :py:class:`monai.transforms.compose.MapTransform`
        meta_keys: 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 contains values such as ``filename``, ``original_shape``.
            This argument can be a sequence of strings, mapped to the ``keys``.
            If ``None``, will try to construct ``meta_keys`` by ``key_{meta_key_postfix}``.
        meta_key_postfix: if ``meta_keys`` is ``None``, use ``key_{meta_key_postfix}`` to retrieve the metadict.
        output_dir: output image directory.
                    Handled by ``folder_layout`` instead, if ``folder_layout`` is not ``None``.
        output_postfix: a string appended to all output file names, default to ``trans``.
                        Handled by ``folder_layout`` instead, if ``folder_layout`` is not ``None``.
        output_ext: output file extension name, available extensions: ``.nii.gz``, ``.nii``, ``.png``, ``.dcm``.
                    Handled by ``folder_layout`` instead, if ``folder_layout`` not ``None``.
        resample: whether to resample image (if needed) before saving the data array,
            based on the ``spatial_shape`` (and ``original_affine``) from metadata.
        mode: This option is used when ``resample=True``. Defaults to ``"nearest"``.
            Depending on the writers, the possible options are:

            - {``"bilinear"``, ``"nearest"``, ``"bicubic"``}.
              See also: https://pytorch.org/docs/stable/nn.functional.html#grid-sample
            - {``"nearest"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``}.
              See also: https://pytorch.org/docs/stable/nn.functional.html#interpolate

        padding_mode: This option is used when ``resample = True``. Defaults to ``"border"``.
            Possible options are {``"zeros"``, ``"border"``, ``"reflection"``}
            See also: https://pytorch.org/docs/stable/nn.functional.html#grid-sample
        scale: {``255``, ``65535``} postprocess data by clipping to [0, 1] and scaling
            [0, 255] (``uint8``) or [0, 65535] (``uint16``). Default is ``None`` (no scaling).
        dtype: data type during resampling computation. Defaults to ``np.float64`` for best precision.
            if None, use the data type of input data. To set the output data type, use ``output_dtype``.
        output_dtype: data type for saving data. Defaults to ``np.float32``.
        allow_missing_keys: don't raise exception if key is missing.
        squeeze_end_dims: if True, any trailing singleton dimensions will be removed (after the channel
            has been moved to the end). So if input is (C,H,W,D), this will be altered to (H,W,D,C), and
            then if C==1, it will be saved as (H,W,D). If D is also 1, it will be saved as (H,W). If `false`,
            image will always be saved as (H,W,D,C).
        data_root_dir: if not empty, it specifies the beginning parts of the input file's
            absolute path. It's used to compute ``input_file_rel_path``, the relative path to the file from
            ``data_root_dir`` to preserve folder structure when saving in case there are files in different
            folders with the same file names. For example, with the following inputs:

            - input_file_name: ``/foo/bar/test1/image.nii``
            - output_postfix: ``seg``
            - output_ext: ``.nii.gz``
            - output_dir: ``/output``
            - data_root_dir: ``/foo/bar``

            The output will be: ``/output/test1/image/image_seg.nii.gz``

            Handled by ``folder_layout`` instead, if ``folder_layout`` is not ``None``.
        separate_folder: whether to save every file in a separate folder. For example: for the input filename
            ``image.nii``, postfix ``seg`` and folder_path ``output``, if ``separate_folder=True``, it will be saved as:
            ``output/image/image_seg.nii``, if ``False``, saving as ``output/image_seg.nii``. Default to ``True``.
            Handled by ``folder_layout`` instead, if ``folder_layout`` is not ``None``.
        print_log: whether to print logs when saving. Default to ``True``.
        output_format: an optional string to specify the output image writer.
            see also: ``monai.data.image_writer.SUPPORTED_WRITERS``.
        writer: a customised ``monai.data.ImageWriter`` subclass to save data arrays.
            if ``None``, use the default writer from ``monai.data.image_writer`` according to ``output_ext``.
            if it's a string, it's treated as a class name or dotted path;
            the supported built-in writer classes are ``"NibabelWriter"``, ``"ITKWriter"``, ``"PILWriter"``.
        output_name_formatter: a callable function (returning a kwargs dict) to format the output file name.
            see also: :py:func:`monai.data.folder_layout.default_name_formatter`.
            If using a custom ``folder_layout``, consider providing your own formatter.
        folder_layout: A customized ``monai.data.FolderLayoutBase`` subclass to define file naming schemes.
            if ``None``, uses the default ``FolderLayout``.
        savepath_in_metadict: if ``True``, adds a key ``saved_to`` to the metadata, which contains the path
            to where the input image has been saved.
    Nz./transz.nii.gzFnearestT r   r   r   z
Path | strr   z
int | Noner   zDtypeLike | Nonez+type[image_writer.ImageWriter] | str | Nonez(Callable[[dict, Transform], dict] | Nonez"monai.data.FolderLayoutBase | Noner   )r   r   r    
output_diroutput_postfix
output_extresamplemodepadding_modescaler   output_dtyper'   squeeze_end_dimsdata_root_dirseparate_folder	print_logoutput_formatwriteroutput_name_formatterfolder_layoutsavepath_in_metadictr)   c                   sd   t  || t|t| j| _t|t| j| _t||||||	|
|||||||||||d| _d S )N)rR   rS   rT   rU   rV   rW   rX   r   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   )	r*   r+   r   r1   r   r   r    r
   saver)r3   r   r   r    rR   rS   rT   rU   rV   rW   rX   r   rY   r'   rZ   r[   r\   r]   r^   r_   r`   ra   rb   r6   r8   r9   r+     s.    zSaveImaged.__init__c                 C  s   | j |||| | S r:   )rc   set_options)r3   init_kwargsdata_kwargsmeta_kwargswrite_kwargsr8   r8   r9   rd   5  s    zSaveImaged.set_optionsc                 C  sp   t |}| || j| jD ]P\}}}|d krB|d k	rB| d| }|d k	rT||nd }| j|| |d q|S )Nr=   )img	meta_data)r>   r?   r   r    getrc   )r3   rC   rD   rE   rF   r    rj   r8   r8   r9   rG   9  s    zSaveImaged.__call__)NNNN)r0   rH   rI   rJ   rM   r   BORDERrK   float64rL   r+   rd   rG   rN   r8   r8   r6   r9   r      s2   R@1
)&rJ   
__future__r   pathlibr   typingr   numpyrK   monaimonai.configr   r   
monai.datar   monai.data.image_readerr   Zmonai.transforms.io.arrayr	   r
   monai.transforms.transformr   r   monai.utilsr   r   r   monai.utils.enumsr   __all__metarM   r   r   r   r   r   r   r8   r8   r8   r9   <module>   s(     