Skip to content

Commit

Permalink
fix: make neuroimaging compatible with pydicom 3.x, and drop support …
Browse files Browse the repository at this point in the history
…of pydicom < 1.0
  • Loading branch information
yarikoptic committed Nov 3, 2024
1 parent 8bec011 commit e41109b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
43 changes: 23 additions & 20 deletions datalad_neuroimaging/extractors/dicom.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,29 @@
import logging
lgr = logging.getLogger('datalad.metadata.extractors.dicom')
from datalad.log import log_progress
from datalad.support.exceptions import CapturedException
from datalad.support.external_versions import external_versions

try:
# renamed for 1.0 release
import pydicom as dcm
from pydicom.errors import InvalidDicomError
import pydicom as dcm
from pydicom.errors import InvalidDicomError

NOT_IMPLEMENTED_TYPES = tuple() # (FileDataset,)
if external_versions["pydicom"] >= "3":
# everything is a FileDataset now, so we will decide based on have a UID
pass
else:
from pydicom.dicomdir import DicomDir
except ImportError: # pragma: no cover
import dicom as dcm
from dicom.errors import InvalidDicomError
from dicom.dicomdir import DicomDir
NOT_IMPLEMENTED_TYPES = (DicomDir,)

try:
from collections.abc import MutableSequence
except ImportError:
from collections import MutableSequence

from distutils.version import LooseVersion
from datalad_deprecated.metadata.definitions import vocabulary_id
from datalad_deprecated.metadata.extractors.base import BaseMetadataExtractor


# pydicom 2.0.0 renamed PersonName3 to PersonName:
PersonName = dcm.valuerep.PersonName3 \
if LooseVersion(dcm.__version__) < "2.0.0" else dcm.valuerep.PersonName
PersonName = dcm.valuerep.PersonName
# Data types we care to extract/handle
_SCALAR_TYPES = (
int, float, string_types, dcm.valuerep.DSfloat, dcm.valuerep.IS,
Expand Down Expand Up @@ -156,17 +155,21 @@ def get_metadata(self, dataset, content):
continue

try:
d = dcm.read_file(absfp, defer_size=1000, stop_before_pixels=True)
except InvalidDicomError:
d = dcm.dcmread(absfp, defer_size=1000, stop_before_pixels=True)
except InvalidDicomError as exc:
# we can only ignore
lgr.debug('"%s" does not look like a DICOM file, skipped', f)
lgr.debug('"%s" does not look like a DICOM file, skipped: %s',
absfp,
CapturedException(exc))
continue

if isinstance(d, DicomDir):
lgr.debug("%s appears to be a DICOMDIR file. Extraction not yet"
" implemented, skipped", f)
if NOT_IMPLEMENTED_TYPES and isinstance(d, NOT_IMPLEMENTED_TYPES):
lgr.debug("%s appears to be a DICOMDIR or alike: got %s. Extraction not yet"
" implemented, skipped", f, d)
continue
elif not hasattr(d, 'SeriesInstanceUID'):
lgr.debug("%s does not have SeriesInstanceUID, skipped", f)
continue

ddict = None
if content:
ddict = _struct2dict(d)
Expand Down
4 changes: 3 additions & 1 deletion datalad_neuroimaging/tests/test_dicomconv.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ def test_dicom_metadata_aggregation(path=None):
def test_validate_bids_fixture():
bids_ds = get_bids_dataset()
# dicom source dataset is absent
eq_(len(bids_ds.subdatasets(fulfilled=True, return_type='list')), 0)
# yoh: disabled since makes little sense (now?) since dataset is subdataset
# as of 978772e9468a5ae30de309cc6ac4370795de75cc etc in 2018
# eq_(len(bids_ds.subdatasets(fulfilled=True, return_type='list')), 0)
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ python_requires = >= 3.9
install_requires =
datalad >= 0.16.7
datalad-deprecated >= 0.2.7
pydicom # DICOM metadata
pydicom >= 2.0.0 # DICOM metadata
pybids >= 0.15.1 # BIDS metadata
nibabel # NIfTI metadata
pandas # bids2scidata export
Expand Down

0 comments on commit e41109b

Please sign in to comment.