Skip to content

Commit

Permalink
added dicomSort functionality to downloadSeries
Browse files Browse the repository at this point in the history
  • Loading branch information
Jermiah committed Nov 25, 2023
1 parent 7e51e20 commit a229025
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 39 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,6 @@ old_src
resources/*
dicomsort.py
NBIA-toolkit.code-workspace
docs/data/*
docs/data/*
driver.py
data/*
66 changes: 38 additions & 28 deletions driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,53 @@
password=PASSWORD,
log_level="DEBUG")

series = client.getSeries(Collection="RADCURE")
# series = client.getSeries(Collection="RADCURE")

seriesList = [_["SeriesInstanceUID"] for _ in series]
seriesList = seriesList[0:5]
# seriesList = [_["SeriesInstanceUID"] for _ in series]

series_one = "1.3.6.1.4.1.14519.5.2.1.245877810326980920641905130135164701440"

client.downloadSeries(
SeriesInstanceUID=series_one,
downloadDir="./data/rawdata",
filePattern = '%PatientName/%StudyDescription-%StudyDate/%SeriesNumber-%SeriesDescription-%SeriesInstanceUID/%InstanceNumber.dcm',
overwrite = True
)


def download(series) -> bool:
response = client.downloadSeries(
SeriesInstanceUID = series,
downloadDir = "/home/bioinf/bhklab/jermiah/projects/rawdata")
return response

if (True):
# iterate through each series and download
# print out the progressbar
with mp.Pool(processes=30) as pool:
for _ in tqdm(pool.imap_unordered(download, seriesList), total=len(seriesList)):
pass
else:
_log = logging.getLogger(__name__)
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)
# def download(series) -> bool:
# response = client.downloadSeries(
# SeriesInstanceUID = series,
# downloadDir = "./rawdata2")
# return response

USERNAME = "sejinkim"
PASSWORD = "q6mJyLD8cPeGTwg!"
nbia.getToken(user=USERNAME, pw=PASSWORD)
# download(seriesList[0])

collection_name = "RADCURE"
metadata = nbia.getSeries(collection = collection_name, format = "df", api_url = "restricted")

# # if (True):
# # # iterate through each series and download
# # # print out the progressbar
# # with mp.Pool(processes=30) as pool:
# # for _ in tqdm(pool.imap_unordered(download, seriesList), total=len(seriesList)):
# # pass
# # else:
# _log = logging.getLogger(__name__)
# for handler in logging.root.handlers[:]:
# logging.root.removeHandler(handler)

# USERNAME = "sejinkim"
# PASSWORD = "q6mJyLD8cPeGTwg!"
# nbia.getToken(user=USERNAME, pw=PASSWORD)

# collection_name = "RADCURE"
# metadata = nbia.getSeries(collection = collection_name, format = "df", api_url = "restricted")

nbia.downloadSeries(series_data = seriesList, path = "/home/bioinf/bhklab/jermiah/projects/rawdata", input_type = "list", api_url = "restricted")
# with mp.Pool(processes=30) as pool:
# for _ in tqdm(pool.imap_unordered(download, seriesList), total=len(seriesList)):
# pass
# python dicomsort.py -u /home/bioinf/bhklab/jermiah/projects/rawdata /home/bioinf/bhklab/jermiah/projects/NBIA-toolkit/resources/rawdata/%PatientID/%StudyDate-%StudyID-%StudyDescription-%StudyInstanceUID/%SeriesNumber-%SeriesDescription-%SeriesIntanceUID/%InstanceNumber-%SOPInstanceUID.dcm
# nbia.downloadSeries(series_data = seriesList, path = "/home/bioinf/bhklab/jermiah/projects/rawdata", input_type = "list", api_url = "restricted")
# # with mp.Pool(processes=30) as pool:
# # for _ in tqdm(pool.imap_unordered(download, seriesList), total=len(seriesList)):
# # pass
# # python dicomsort.py -u /home/bioinf/bhklab/jermiah/projects/rawdata /home/bioinf/bhklab/jermiah/projects/NBIA-toolkit/resources/rawdata/%PatientID/%StudyDate-%StudyID-%StudyDescription-%StudyInstanceUID/%SeriesNumber-%SeriesDescription-%SeriesIntanceUID/%InstanceNumber-%SOPInstanceUID.dcm



15 changes: 15 additions & 0 deletions src/nbiatoolkit/dicomsort/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# from .logger import setup_logger
# from .nbia_endpoints import NBIA_ENDPOINTS
# from .md5 import validateMD5
# __all__ = [
# "setup_logger",
# "NBIA_ENDPOINTS",
# "validateMD5"
# ]

from .dicomsort import DICOMSorter
from .helper_functions import parseDICOMKeysFromFormat, sanitizeFileName, truncateUID

__all__ = [
"DICOMSorter"
]
Empty file.
52 changes: 42 additions & 10 deletions src/nbiatoolkit/nbia.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from nbiatoolkit.utils.nbia_endpoints import NBIA_ENDPOINTS
from nbiatoolkit.utils.logger import setup_logger
from nbiatoolkit.utils.md5 import validateMD5
from nbiatoolkit.dicomsort import DICOMSorter

import requests
from requests.exceptions import JSONDecodeError as JSONDecodeError
import io, zipfile, os
Expand Down Expand Up @@ -141,27 +143,57 @@ def getSeries(self,

return response


def downloadSeries(
self, SeriesInstanceUID: str, downloadDir: str,
self, SeriesInstanceUID: str, downloadDir: str,
filePattern: str = '%PatientName/%StudyDescription-%StudyDate/%SeriesNumber-%SeriesDescription-%SeriesInstanceUID/%InstanceNumber.dcm',
overwrite: bool = False
) -> bool:

# create temporary directory
from tempfile import TemporaryDirectory

params = dict()
params["SeriesInstanceUID"] = SeriesInstanceUID

response = self.query_api(
endpoint = NBIA_ENDPOINTS.DOWNLOAD_SERIES,
params = params)

if isinstance(response, bytes):
file = zipfile.ZipFile(io.BytesIO(response))
seriesDir = os.path.join(downloadDir, SeriesInstanceUID)
file.extractall(path=seriesDir)
if not isinstance(response, bytes):
# Handle the case where the expected binary data is not received
# Log error or raise an exception
return False

file = zipfile.ZipFile(io.BytesIO(response))

with TemporaryDirectory() as tempDir:
file.extractall(path=tempDir)
assert validateMD5(seriesDir=tempDir) == True

# Create an instance of DICOMSorter with the desired target pattern
sorter = DICOMSorter(
sourceDir = tempDir,
destinationDir=downloadDir,
targetPattern=filePattern,
truncateUID=True,
sanitizeFilename=True
)

sorter.sortDICOMFiles(option="move", overwrite=overwrite)



# if isinstance(response, bytes):
# file = zipfile.ZipFile(io.BytesIO(response))
# seriesDir = os.path.join(downloadDir, SeriesInstanceUID)
# file.extractall(path=seriesDir)

validateMD5(seriesDir=seriesDir)
else:
# Handle the case where the expected binary data is not received
# Log error or raise an exception
pass
# validateMD5(seriesDir=seriesDir)
# else:
# # Handle the case where the expected binary data is not received
# # Log error or raise an exception
# pass

return True

Expand Down

0 comments on commit a229025

Please sign in to comment.