U Ph @sddlmZddlZddlmZddlZddlmZddlm Z e ddd\Z Z e d \Z Z d Zd Zd d ddddgZd&dddddd Zd'dddddddddd Zd(d!dd"d"dd#d$dZd%dZdS))) 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_URLstrz str | Nonelist)query attributereturncCszts tdt|}t|}|g}t|jdkr>|S|D].}|dk rj||krj|||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: ?&. 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_listdrJ/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/apps/tcia/utils.pyr %s   F md5hashes.csvTrboolNone) series_uid download_dir output_dir check_md5hashes_filenameprogressrc Cs|rdnd}t|d|}tjjj|tj||d||d|rtsTt dt tj||}|j D]&\} } tjjj tj|| | ddqnd 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_dfZdcmZmd5hashrrrr Ls  iUrtuple)dsfind_sopref_series_uid_tagref_sop_uid_tagrcCsP|r|n|}d}|D]6}|jdkr6|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. SQ)ZVRr tagvalue)rArBrCrDZ ref_uid_tagoutputelemitemrrrr us     cCsBtd|dd}|D]&}td|dd}||kr|SqdS)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=ZSOPInstanceUIDrE)r )Z study_uid ref_sop_uid series_listZ series_idZ sop_id_listrrrr s  )N)Fr T)Fr;r>) __future__rr2typingrr.monai.config.type_definitionsr monai.utilsrrrr6r5r r__all__r r r r rrrr s4      ++