Skip to content

Commit

Permalink
Remove common prefixes from filenames (#45)
Browse files Browse the repository at this point in the history
I removed common prefixes from filenames. This allows to verify
filenames like "1.2.86.1.dcm", ..., "1.2.86.12.dcm".

I also removed `vdcms = [d.rsplit('.', 1)[0] for d in filenames]` since
this is redundant in combination with the line after it: `vdcms =
[int(''.join(c for c in str(fn) if c.isdigit())) for fn in filenames]`

Finally, `True` is returned if filenames could not be verified for a
particular series: only one slice or no number in the filename. This
makes the default of checking for numbers more user friendly.
  • Loading branch information
joeranbosma authored Mar 28, 2023
1 parent 5f67119 commit 4108621
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
long_description = fh.read()

setuptools.setup(
version='2.1.4',
version='2.1.5',
author_email='[email protected]',
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
28 changes: 23 additions & 5 deletions src/picai_prep/dcm2mha.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,15 +759,33 @@ def get_orientation_tuple_sitk(self, ds: pydicom.FileDataset) -> Tuple:
return tuple(self.get_orientation_matrix(ds).transpose().flatten())

def _verify_dicom_filenames(self, filenames: Optional[List[PathLike]] = None) -> bool:
"""Verify DICOM filenames have increasing numbers, with no gaps"""
"""
Verify DICOM filenames have increasing numbers, with no gaps
Common prefixes are removed from the filenames before checking the numbers,
this allows to verify filenames like "1.2.86.1.dcm", ..., "1.2.86.12.dcm".
"""
if filenames is None:
filenames = [os.path.basename(dcm) for dcm in self.dicom_slice_paths]

vdcms = [d.rsplit('.', 1)[0] for d in filenames]
vdcms = [int(''.join(c for c in d if c.isdigit())) for d in vdcms]
# remove common prefixes
common_prefix = os.path.commonprefix(filenames)
if common_prefix:
filenames = [fn.replace(common_prefix, "") for fn in filenames]
common_postfix = os.path.commonprefix([fn[::-1] for fn in filenames])[::-1]
if common_postfix:
filenames = [fn.replace(common_postfix, "") for fn in filenames]

# extract numbers from filenames
filename_digits = [(''.join(c for c in str(fn) if c.isdigit())) for fn in filenames]
filename_digits = [int(d) for d in filename_digits if d]
if len(filename_digits) < 2:
# either no numbers in the filenames, or only one file
return True

missing_slices = False
for num in range(min(vdcms), max(vdcms) + 1):
if num not in vdcms:
for num in range(min(filename_digits), max(filename_digits) + 1):
if num not in filename_digits:
missing_slices = True
break
if missing_slices:
Expand Down

0 comments on commit 4108621

Please sign in to comment.