U
    Ph                  	   @  s   d dl mZ d dlZd dlmZ d dlZd dlmZ d dlm	Z	 e	ddd\Z
Ze	d	\ZZd
ZdZddddddgZd&ddddddZd'ddddddddddZd(d!dd"d"dd#d$dZd%d ZdS ))    )annotationsN)Iterable)PathLike)optional_importZrequestsget)namepandasz^(?!.*LICENSE).*z?https://services.cancerimagingarchive.net/nbia-api/services/v1/get_tcia_metadatadownload_tcia_series_instanceget_tcia_ref_uidmatch_tcia_ref_uid_in_studyDCM_FILENAME_REGEXBASE_URLstrz
str | Nonelist)query	attributereturnc                 C  sz   t stdt |  }t|}|  g }t|jdkr>|S | D ].}|dk	rj||krj|||  qF|| qF|S )a  
    Achieve metadata of a public The Cancer Imaging Archive (TCIA) dataset.

    This function makes use of The National Biomedical Imaging Archive (NBIA) REST APIs to access the metadata
    of objects in the TCIA database.
    Please refer to the following link for more details:
    https://wiki.cancerimagingarchive.net/display/Public/NBIA+Search+REST+API+Guide

    This function relies on `requests` package.

    Args:
        query: queries used to achieve the corresponding metadata. A query is consisted with query name and
            query parameters. The format is like: <query name>?<parameter 1>&<parameter 2>.
            For example: "getSeries?Collection=C4KC-KiTS&Modality=SEG"
            Please refer to the section of Image Metadata APIs in the link mentioned
            above for more details.
        attribute: Achieved metadata may contain multiple attributes, if specifying an attribute name, other attributes
            will be ignored.

    z1requests package is necessary, please install it.r   N)	has_requests
ValueErrorr   requests_getZraise_for_statuslentextjsonappend)r   r   full_urlrespZmetadata_listd r   J/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/apps/tcia/utils.pyr	   %   s    Fmd5hashes.csvTr   boolNone)
series_uiddownload_dir
output_dir	check_md5hashes_filenameprogressr   c                 C  s   |rdnd}t  | d|  }tjjj|tj||  d||d |rtsTt	dt
tj||}|jD ]&\}	}
tjjjtj||	|
dd qnd	S )
a  
    Download a dicom series from a public The Cancer Imaging Archive (TCIA) dataset.
    The downloaded compressed file will be stored in `download_dir`, and the uncompressed folder will be saved
    in `output_dir`.

    Args:
        series_uid: SeriesInstanceUID of a dicom series.
        download_dir: the path to store the downloaded compressed file. The full path of the file is:
            `os.path.join(download_dir, f"{series_uid}.zip")`.
        output_dir: target directory to save extracted dicom series.
        check_md5: whether to download the MD5 hash values as well. If True, will check hash values for all images in
            the downloaded dicom series.
        hashes_filename: file that contains hashes.
        progress: whether to display progress bar.

    ZgetImageWithMD5HashZgetImagez?SeriesInstanceUID=z.zip)urlfilepathr%   r(   z/pandas package is necessary, please install it.md5)r*   val	hash_typeN)r   monaiappsutilsdownload_and_extractospathjoin
has_pandasr   pdread_csvvalues
check_hash)r#   r$   r%   r&   r'   r(   Z
query_namedownload_urlZ	hashes_dfZdcmZmd5hashr   r   r   r
   L   s              iU  r   tuple)dsfind_sopref_series_uid_tagref_sop_uid_tagr   c                 C  sP   |r|n|}d}| D ]6}|j dkr6|D ]}t||}q&|j|kr|j  S q|S )a  
    Achieve the referenced UID from the referenced Series Sequence for the input pydicom dataset object.
    The referenced UID could be Series Instance UID or SOP Instance UID. The UID will be detected from
    the data element of the input object. If the data element is a sequence, each dataset within the sequence
    will be detected iteratively. The first detected UID will be returned.

    Args:
        ds: a pydicom dataset object.
        find_sop: whether to achieve the referenced SOP Instance UID.
        ref_series_uid_tag: tag of the referenced Series Instance UID.
        ref_sop_uid_tag: tag of the referenced SOP Instance UID.

     SQ)ZVRr   tagvalue)rA   rB   rC   rD   Zref_uid_tagoutputelemitemr   r   r   r   u   s    

c                 C  sB   t d|  dd}|D ]&}t d| dd}||kr|  S qdS )z
    Match the SeriesInstanceUID from all series in a study according to the input SOPInstanceUID.

    Args:
        study_uid: StudyInstanceUID.
        ref_sop_uid: SOPInstanceUID.

    zgetSeries?StudyInstanceUID=SeriesInstanceUID)r   r   z%getSOPInstanceUIDs?SeriesInstanceUID=ZSOPInstanceUIDrE   )r	   )Z	study_uidref_sop_uidseries_listZ	series_idZsop_id_listr   r   r   r      s    	 
)N)Fr    T)Fr;   r>   )
__future__r   r2   typingr   r.   monai.config.type_definitionsr   monai.utilsr   r   r   r6   r5   r   r   __all__r	   r
   r   r   r   r   r   r   <module>   s4   
+   +    