o  i@sddlmZddlZddlmZddlZddlmZddlm Z e ddd\Z Z e d \Z Z d Zd Zgd Zd/d0ddZ   d1d2d!d"Z  # $d3d4d+d,Zd-d.ZdS)5) annotationsN)Iterable)PathLike)optional_importZrequestsget)namepandasz^(?!.*LICENSE).*z?https://services.cancerimagingarchive.net/nbia-api/services/v1/)get_tcia_metadatadownload_tcia_series_instanceget_tcia_ref_uidmatch_tcia_ref_uid_in_studyDCM_FILENAME_REGEXBASE_URLquerystr attribute str | NonereturnlistcCsztstdt|}t|}|g}t|jdkr|S|D]}|dur5||vr5|||q#||q#|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: ?&. 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.rN) has_requests ValueErrorr requests_getZraise_for_statuslentextjsonappend)rrfull_urlrespZ metadata_listdrW/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/apps/tcia/utils.pyr %s   r F md5hashes.csvT series_uid download_dirr output_dir check_md5boolhashes_filenameprogressNonec Cs|rdnd}t|d|}tjjj|tj||d||d|rKts*t dt tj||}|j D]\} } tjjj tj|| | ddq7d Sd 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. ZgetImageWithMD5HashZgetImagez?SeriesInstanceUID=z.zip)urlfilepathr$r(z/pandas package is necessary, please install it.md5)r+val hash_typeN)rmonaiappsutilsdownload_and_extractospathjoin has_pandasrpdread_csvvalues check_hash) r"r#r$r%r'r(Z query_name download_urlZ hashes_dfZdcmZmd5hashrrr r Ls  r  iUdsrfind_sopref_series_uid_tagtupleref_sop_uid_tagcCsP|r|n|}d}|D]}|jdkr|D]}t||}q|j|kr%|jSq |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. ZSQ)ZVRr tagvalue)rArBrCrEZ ref_uid_tagoutputelemitemrrr r us     r cCsBtd|dd}|D]}td|dd}||vr|Sq dS)z Match the SeriesInstanceUID from all series in a study according to the input SOPInstanceUID. Args: study_uid: StudyInstanceUID. ref_sop_uid: SOPInstanceUID. zgetSeries?StudyInstanceUID=SeriesInstanceUID)rrz%getSOPInstanceUIDs?SeriesInstanceUID=ZSOPInstanceUIDrF)r )Z study_uid ref_sop_uid series_listZ series_idZ sop_id_listrrr r s  r )N)rrrrrr)Fr!T)r"rr#rr$rr%r&r'rr(r&rr))Fr<r?) rArrBr&rCrDrErDrr) __future__rr3collections.abcrr/monai.config.type_definitionsr monai.utilsrrrr7r6r r__all__r r r r rrrr s*      + +