1414from contextlib import contextmanager
1515from functools import partial
1616from types import TracebackType
17- from typing import Dict , Generator , List , Optional , TextIO , Tuple , Type , Union
17+ from typing import (
18+ ClassVar ,
19+ Dict ,
20+ FrozenSet ,
21+ Generator ,
22+ List ,
23+ Optional ,
24+ TextIO ,
25+ Tuple ,
26+ Type ,
27+ Union ,
28+ )
1829
1930import pandas as pd
2031import pydicom
@@ -67,7 +78,7 @@ def fileno(file_or_fd: TextIO) -> Optional[int]:
6778@contextmanager
6879def stdout_redirected (
6980 to : Union [str , TextIO ] = os .devnull , stdout : Optional [TextIO ] = None
70- ) -> Generator :
81+ ) -> Generator [ Optional [ TextIO ], None , None ] :
7182 if platform .system () == "Windows" :
7283 yield None
7384 return
@@ -136,23 +147,25 @@ class DicomDownloader:
136147 :param num_processes: The number of processes to run for downloading
137148 """
138149
139- ACCEPTED_FORMATS = {
140- ".dcm" ,
141- ".nia" ,
142- ".nii" ,
143- ".nii.gz" ,
144- ".hdr" ,
145- ".img" ,
146- ".img.gz" ,
147- ".tif" ,
148- ".TIF" ,
149- ".tiff" ,
150- ".TIFF" ,
151- ".mha" ,
152- ".mhd" ,
153- ".nrrd" ,
154- ".nhdr" ,
155- }
150+ ACCEPTED_FORMATS : ClassVar [FrozenSet [str ]] = frozenset (
151+ {
152+ ".dcm" ,
153+ ".nia" ,
154+ ".nii" ,
155+ ".nii.gz" ,
156+ ".hdr" ,
157+ ".img" ,
158+ ".img.gz" ,
159+ ".tif" ,
160+ ".TIF" ,
161+ ".tiff" ,
162+ ".TIFF" ,
163+ ".mha" ,
164+ ".mhd" ,
165+ ".nrrd" ,
166+ ".nhdr" ,
167+ }
168+ )
156169
157170 def __init__ (
158171 self ,
@@ -239,7 +252,7 @@ def __exit__(
239252 @staticmethod
240253 def get_download_id (
241254 study_uid : str ,
242- series_uid : str = None ,
255+ series_uid : Optional [ str ] = None ,
243256 always_download_in_study_folder : bool = False ,
244257 ) -> str :
245258 """
@@ -259,7 +272,7 @@ def get_download_id(
259272
260273 def get_download_path (self , download_id : str ) -> pathlib .Path :
261274 """
262- Builds the folder hierarchy where the data will be stored. The hierarchy depends on the
275+ Build the folder hierarchy where the data will be stored. The hierarchy depends on the
263276 `hierarchical_storage` parameter. Given a download ID
264277 263a1dad02916f5eca3c4eec51dc9d281735b47b8eb8bc2343c56e6ccd and `hierarchical_storage` = 2,
265278 the data will be stored in 26/3a/1dad02916f5eca3c4eec51dc9d281735b47b8eb8bc2343c56e6ccd.
@@ -277,13 +290,13 @@ def get_download_path(self, download_id: str) -> pathlib.Path:
277290 def download_data (
278291 self ,
279292 study_uid : str ,
280- series_uid : str = None ,
293+ series_uid : Optional [ str ] = None ,
281294 output_dir : Union [str , pathlib .Path ] = "out" ,
282295 save_metadata : bool = True ,
283296 existing_ids : Optional [List [str ]] = None ,
284297 ) -> Tuple [List [Dict [str , str ]], List [Dict [str , str ]]]:
285298 """
286- Downloads the data related to the StudyInstanceUID and SeriesInstanceUID (if given,
299+ Download the data related to the StudyInstanceUID and SeriesInstanceUID (if given,
287300 otherwise the entire study will be downloaded).
288301
289302 :param study_uid: The StudyInstanceUID
@@ -333,7 +346,7 @@ def download_data(
333346 base_dict [self .series_instance_uid_field ] = series_uid
334347
335348 # Init the readers/writers
336- series_reader = sitk .ImageSeriesReader () # type: ignore
349+ series_reader = sitk .ImageSeriesReader ()
337350 with tempfile .TemporaryDirectory () as tmp_dir :
338351 # Create the download dir
339352 current_tmp_dir = pathlib .Path (tmp_dir )
@@ -361,11 +374,11 @@ def download_data(
361374 progress_bar .close ()
362375
363376 # Get Series ID names from folder
364- series_uids = sitk .ImageSeriesReader .GetGDCMSeriesIDs (str (current_tmp_dir )) # type: ignore
377+ series_uids = sitk .ImageSeriesReader .GetGDCMSeriesIDs (str (current_tmp_dir ))
365378 logger .info (f"Study ID has { len (series_uids )} series." )
366379 for series in series_uids :
367380 # Get the DICOMs corresponding to the series
368- files = series_reader .GetGDCMSeriesFileNames ( # type: ignore
381+ files = series_reader .GetGDCMSeriesFileNames (
369382 str (current_tmp_dir ), series
370383 )
371384 current_dict = base_dict .copy ()
@@ -374,11 +387,12 @@ def download_data(
374387 )
375388 try :
376389 # Read the series
377- with simpleitk_warning_file .open ("w" ) as f , stdout_redirected (
378- f , stdout = sys .stderr
390+ with (
391+ simpleitk_warning_file .open ("w" ) as f ,
392+ stdout_redirected (f , stdout = sys .stderr ),
379393 ):
380- series_reader .SetFileNames (files ) # type: ignore
381- image = series_reader .Execute () # type: ignore
394+ series_reader .SetFileNames (files )
395+ image = series_reader .Execute ()
382396 with simpleitk_warning_file .open ("r" ) as f :
383397 content = f .read ()
384398 if "warning" in content .lower ():
@@ -431,9 +445,9 @@ def download_data(
431445 series_download_dir / f"{ series } _meta.dcm" ,
432446 )
433447 dcm_info = pydicom .dcmread (str (files [0 ]), stop_before_pixels = True )
434- current_dict [
435- self . deid_study_instance_uid_field
436- ] = dcm_info . StudyInstanceUID
448+ current_dict [self . deid_study_instance_uid_field ] = (
449+ dcm_info . StudyInstanceUID
450+ )
437451 current_dict [self .deid_series_instance_uid_field ] = series
438452 downloaded_series_info .append (current_dict )
439453
@@ -442,7 +456,7 @@ def download_data(
442456 def fix_mapping_dataframe (
443457 self ,
444458 df : pd .DataFrame ,
445- mapping_df : pd .DataFrame = None ,
459+ mapping_df : Optional [ pd .DataFrame ] = None ,
446460 output_dir : Union [str , pathlib .Path ] = "out" ,
447461 study_uid_col : str = "study_instance_uid" ,
448462 series_uid_col : str = "series_instance_uid" ,
@@ -464,7 +478,8 @@ def fix_mapping_dataframe(
464478 output_dir = pathlib .Path (output_dir )
465479 if not output_dir .exists () or not len (list (output_dir .glob ("*" ))):
466480 warnings .warn (
467- "Cannot fix the mapping file if the output directory does not exist."
481+ "Cannot fix the mapping file if the output directory does not exist." ,
482+ stacklevel = 2 ,
468483 )
469484 return None
470485 if mapping_df is None :
@@ -547,7 +562,7 @@ def download_data_from_dataframe(
547562 output_dir : Union [str , pathlib .Path ] = "out" ,
548563 study_uid_col : str = "study_instance_uid" ,
549564 series_uid_col : Optional [str ] = "series_instance_uid" ,
550- mapping_df : pd .DataFrame = None ,
565+ mapping_df : Optional [ pd .DataFrame ] = None ,
551566 download_full_study : bool = False ,
552567 save_metadata : bool = True ,
553568 ) -> Tuple [pd .DataFrame , pd .DataFrame ]:
@@ -593,7 +608,8 @@ def download_data_from_dataframe(
593608 warnings .warn (
594609 "download_full_study = False will only download a specified series but "
595610 "have not provided a valid Series UID column of the DataFrame, "
596- "as a result the full study will be downloaded."
611+ "as a result the full study will be downloaded." ,
612+ stacklevel = 2 ,
597613 )
598614
599615 # Create list of rows
0 commit comments