diff --git a/datalad_neuroimaging/extractors/dicom.py b/datalad_neuroimaging/extractors/dicom.py index 81e970f..59dfe19 100644 --- a/datalad_neuroimaging/extractors/dicom.py +++ b/datalad_neuroimaging/extractors/dicom.py @@ -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, @@ -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) diff --git a/datalad_neuroimaging/tests/test_dicomconv.py b/datalad_neuroimaging/tests/test_dicomconv.py index 227162a..9d2ee98 100644 --- a/datalad_neuroimaging/tests/test_dicomconv.py +++ b/datalad_neuroimaging/tests/test_dicomconv.py @@ -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) diff --git a/setup.cfg b/setup.cfg index c2f8ef7..3d26336 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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