def get_files(contenders, check=True, pattern=None, force=False): '''get_dcm_files will take a list of single dicom files or directories, and return a generator that yields complete paths to all files :param pattern: A pattern to use with fnmatch. If None, * is used :param force: force reading of the files, if some headers invalid. Not recommended, as many non-dicom will come through ''' if not isinstance(contenders, list): contenders = [contenders] for contender in contenders: if os.path.isdir(contender): dicom_files = recursive_find(contender, pattern=pattern) else: dicom_files = [contender] for dicom_file in dicom_files: if dicom_file is not None: if check: validated_files = validate_dicoms(dicom_file, force=force) else: validated_files = [dicom_file] for validated_file in validated_files: bot.debug("Found contender file %s" % (validated_file)) yield validated_file
def test_recursive_find(self): '''test_recursive_find should detect 7 dicoms ''' print("Testing recursive find.") from deid.utils import recursive_find files = recursive_find(self.pwd, pattern='*.dcm') print("Found %s files" % (len(files))) self.assertTrue(len(files) == 7)
def test_recursive_find_as_list(self): '''test_recursive_find_as_list should detect 7 dicoms ''' print("Testing recursive find as lit.") from deid.utils import recursive_find expected = 7 files = list(recursive_find(self.pwd, pattern='*.dcm')) found = len(files) print("Found %s files" % (len(files))) self.assertTrue(found == expected)
def test_recursive_find(self): '''test_recursive_find should detect 7 dicoms ''' print("Testing recursive find.") from deid.utils import recursive_find found = 0 expected = 7 for file in recursive_find(self.pwd, pattern='*.dcm'): found += 1 print("Found %s files" % (found)) self.assertTrue(found == expected)
def get_files(contenders, check=True, pattern=None, force=False, tempdir=None): """get_files will take a list of single dicom files or directories, and return a generator that yields complete paths to all files Parameters ========== contenders: a list of files or directories (contenders!) check: boolean to indicate if we should validate dicoms (default True) pattern: A pattern to use with fnmatch. If None, * is used force: force reading of the files, if some headers invalid. Not recommended, as many non-dicom will come through """ if not isinstance(contenders, list): contenders = [contenders] for contender in contenders: if os.path.isdir(contender): dicom_files = recursive_find(contender, pattern=pattern) else: dicom_files = [contender] for dicom_file in dicom_files: dfile, dextension = os.path.splitext(dicom_file) # The code currently only assumes a single-file per zip. This could be # expanded to allow for multiple test files within an archive. if dextension == ".zip": with zipfile.ZipFile(dicom_file, "r") as compressedFile: compressedFile.extractall(tempdir) dicom_file = next( os.path.join(tempdir, f) for f in os.listdir(tempdir) if os.path.isfile(os.path.join(tempdir, f))) if dicom_file is not None: if check: validated_files = validate_dicoms(dicom_file, force=force) else: validated_files = [dicom_file] for validated_file in validated_files: bot.debug("Found contender file %s" % (validated_file)) yield validated_file
def get_files(contenders, check=True, pattern=None, force=False): '''get_dcm_files will take a list of single dicom files or directories, and return a single list of complete paths to all files :param pattern: A pattern to use with fnmatch. If None, * is used :param force: force reading of the files, if some headers invalid. Not recommended, as many non-dicom will come through ''' if not isinstance(contenders, list): contenders = [contenders] dcm_files = [] for contender in contenders: if os.path.isdir(contender): dicom_dir = recursive_find(contender, pattern=pattern) bot.debug("Found %s contender files in %s" % (len(dicom_dir), os.path.basename(contender))) dcm_files.extend(dicom_dir) else: bot.debug("Adding single contender file %s" % (contender)) dcm_files.append(contender) if check: dcm_files = validate_dicoms(dcm_files, force=force) return dcm_files