Skip to content

Commit

Permalink
refactor: Refactor NBIAClient's _downloadSingleSeries method
Browse files Browse the repository at this point in the history
This commit refactors the _downloadSingleSeries method in the NBIAClient class. The changes include:
- Reformatting the method signature for better readability
- Adding exception handling for MD5 hash validation
- Commenting out a line of code that is not currently being used
- Adding error handling and logging for sorting DICOM files
  • Loading branch information
jjjermiah committed Jan 21, 2024
1 parent c854242 commit ff11711
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/nbiatoolkit/nbia.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ def downloadSeries(
# downloads in the future
def _downloadSingleSeries(
self, SeriesInstanceUID: str, downloadDir: str,
filePattern: str, overwrite: bool) -> bool:
filePattern: str, overwrite: bool
) -> bool:

# create temporary directory
from tempfile import TemporaryDirectory
Expand All @@ -223,8 +224,11 @@ def _downloadSingleSeries(

with TemporaryDirectory() as tempDir:
file.extractall(path=tempDir)
if not validateMD5(seriesDir=tempDir) and not overwrite:
self.log.error("MD5 validation failed. Exiting...")

try:
validateMD5(seriesDir=tempDir)
except Exception as e:
self.log.error("Error validating MD5 hash: %s", e)
return False

# Create an instance of DICOMSorter with the desired target pattern
Expand All @@ -235,8 +239,12 @@ def _downloadSingleSeries(
truncateUID=True,
sanitizeFilename=True
)

sorter.sortDICOMFiles(option="move", overwrite=overwrite)
# sorter.sortDICOMFiles(option="move", overwrite=overwrite)
if not sorter.sortDICOMFiles(option="move", overwrite=overwrite):
self.log.error(
"Error sorting DICOM files for series %s\n \
failed files located at %s", SeriesInstanceUID, tempDir)
return False

return True

Expand Down

0 comments on commit ff11711

Please sign in to comment.