Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DICOM extractor rejects pydicom types #11

Merged
merged 1 commit into from
Apr 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions datalad_neuroimaging/extractors/dicom.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,39 @@


def _is_good_type(v):
if isinstance(v, (int, float, string_types)):
if isinstance(
v,
(int, float, string_types, dcm.valuerep.DSfloat, dcm.valuerep.IS,
dcm.valuerep.PersonName3)):
return True
elif isinstance(v, (list, tuple)):
return all(map(_is_good_type, v))


def _sanitize_unicode(s):
return s.replace(u"\u0000", "").strip()


def _convert_value(v):
t = type(v)
if t in (int, float):
cv = v
elif t == str:
cv = _sanitize_unicode(v)
elif t == bytes:
s = v.decode('ascii', 'replace')
cv = _sanitize_unicode(s)
elif t == dcm.valuerep.DSfloat:
cv = float(v)
elif t == dcm.valuerep.IS:
cv = int(v)
elif t == dcm.valuerep.PersonName3:
cv = str(v)
elif isinstance(v, (list, tuple)):
cv = list(map(_convert_value, v))
else:
return False
cv = v
return cv


context = {
Expand All @@ -48,7 +75,7 @@ def _is_good_type(v):


def _struct2dict(struct):
return {k: getattr(struct, k)
return {k: _convert_value(getattr(struct, k))
for k in struct.dir()
if hasattr(struct, k) and
_is_good_type(getattr(struct, k))}
Expand Down
7 changes: 6 additions & 1 deletion datalad_neuroimaging/extractors/tests/test_dicom.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# copyright and license terms.
#
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
"""Test audio extractor"""
"""Test DICOM extractor"""

from datalad.tests.utils import SkipTest
try:
Expand Down Expand Up @@ -48,6 +48,11 @@ def test_dicom(path):
# no point in testing ALL keys, but we got plenty
assert(len(meta.keys()) > 70)
eq_(meta['SeriesDate'], '20070205')
# make sure we have PatientName -- this is not using a basic data type, but
# dicom.valuerep.PersonName3 -- conversion should have handled that
# we can only test if the key is there, the source dicom has an empty
# string as value
eq_(meta['PatientName'], '')

# now ask for the dataset metadata, which should have both the unique props
# and a list of imageseries (one in this case, but a list)
Expand Down