U
    Ph                     @  s|   d dl mZ d dlmZmZ d dlZd dlmZ d dlm	Z	 dddgZ
d	d
d	dddZG dd deZG dd deZdS )    )annotations)ABCabstractmethodN)PathLike)create_file_basenameFolderLayoutBaseFolderLayoutdefault_name_formatterdictzmonai.transforms.Transform)metadictsaverreturnc                 C  sP   | r|  tjjjt|ddn
t|dd}| r@|  tjjjdnd}| |dS )zxReturns a kwargs dict for :py:meth:`FolderLayout.filename`,
    according to the input metadata and SaveImage transform._data_indexr   N)subjectidx)getmonaiutilsImageMetaKeyFILENAME_OR_OBJgetattrPATCH_INDEX)r   r   r   patch_index r   M/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/data/folder_layout.pyr	      s    
c                   @  s"   e Zd ZdZeddddZdS )r   a  
    Abstract base class to define a common interface for FolderLayout and derived classes
    Mainly, defines the ``filename(**kwargs) -> PathLike`` function, which must be defined
    by the deriving class.

    Example:

    .. code-block:: python

        from monai.data import FolderLayoutBase

        class MyFolderLayout(FolderLayoutBase):
            def __init__(
                self,
                basepath: Path,
                extension: str = "",
                makedirs: bool = False
            ):
                self.basepath = basepath
                if not extension:
                    self.extension = ""
                elif extension.startswith("."):
                    self.extension = extension:
                else:
                    self.extension = f".{extension}"
                self.makedirs = makedirs

            def filename(self, patient_no: int, image_name: str, **kwargs) -> Path:
                sub_path = self.basepath / patient_no
                if not sub_path.exists():
                    sub_path.mkdir(parents=True)

                file = image_name
                for k, v in kwargs.items():
                    file += f"_{k}-{v}"

                file +=  self.extension
                return sub_path / file

    r   )r   c                 K  s   t dS )zu
        Create a filename with path based on the input kwargs.
        Abstract method, implement your own.
        N)NotImplementedError)selfkwargsr   r   r   filenameM   s    zFolderLayoutBase.filenameN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   #   s   )c                   @  s<   e Zd ZdZdddddddddd	ZddddddZdS )r   a6  
    A utility class to create organized filenames within ``output_dir``. The
    ``filename`` method could be used to create a filename following the folder structure.

    Example:

    .. code-block:: python

        from monai.data import FolderLayout

        layout = FolderLayout(
            output_dir="/test_run_1/",
            postfix="seg",
            extension="nii",
            makedirs=False)
        layout.filename(subject="Sub-A", idx="00", modality="T1")
        # return value: "/test_run_1/Sub-A_seg_00_modality-T1.nii"

    The output filename is a string starting with a ``subject`` ID, and
    includes additional information about a customized index and image
    modality.  This utility class doesn't alter the underlying image data, but
    provides a convenient way to create filenames.
     Fr   strbool)
output_dirpostfix	extensionparentmakedirsdata_root_dirc                 C  s(   || _ || _|| _|| _|| _|| _dS )a  
        Args:
            output_dir: output directory.
            postfix: a postfix string for output file name appended to ``subject``.
            extension: output file extension to be appended to the end of an output filename.
            parent: whether to add a level of parent folder to contain each image to the output filename.
            makedirs: whether to create the output parent directories if they do not exist.
            data_root_dir: an optional `PathLike` object to preserve the folder structure of the input `subject`.
                Please see :py:func:`monai.data.utils.create_file_basename` for more details.
        N)r&   r'   extr)   r*   r+   )r   r&   r'   r(   r)   r*   r+   r   r   r   __init__o   s    zFolderLayout.__init__r   N)r   r   c              	   K  s~   t | j|| j| j| j|| jd}| D ]\}}|d| d| 7 }q(| jdk	rz| j }||rr|dsrd| n| 7 }|S )a  
        Create a filename based on the input ``subject`` and ``idx``.

        The output filename is formed as:

            ``output_dir/[subject/]subject[_postfix][_idx][_key-value][ext]``

        Args:
            subject: subject name, used as the primary id of the output filename.
                When a `PathLike` object is provided, the base filename will be used as the subject name,
                the extension name of `subject` will be ignored, in favor of ``extension``
                from this class's constructor.
            idx: additional index name of the image.
            kwargs: additional keyword arguments to be used to form the output filename.
                The key-value pairs will be appended to the output filename as ``f"_{k}-{v}"``.
        )r'   input_file_namefolder_pathr+   separate_folderr   r*   _-N.)	r   r'   r&   r+   r)   r*   itemsr,   
startswith)r   r   r   r   	full_namekvr,   r   r   r   r      s    	
"zFolderLayout.filename)r#   r#   FFr#   )r   N)r   r    r!   r"   r-   r   r   r   r   r   r   V   s        )
__future__r   abcr   r   r   monai.configr   monai.data.utilsr   __all__r	   r   r   r   r   r   r   <module>   s   
3