Skip to content

Commit c0733d2

Browse files
committedDec 4, 2024
ENH+CMP: Speed up selective extraction, move to pydicom.dcmread
1 parent aa0b062 commit c0733d2

File tree

5 files changed

+21
-23
lines changed

5 files changed

+21
-23
lines changed
 

‎src/dcmstack/dcmstack.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111
from ordereddict import OrderedDict
1212

1313
import numpy as np
14-
try:
15-
import pydicom
16-
except ImportError:
17-
import dicom as pydicom
14+
import pydicom
15+
from pydicom.datadict import tag_for_keyword
1816
import nibabel as nb
1917
from nibabel.nifti1 import Nifti1Extensions
2018
from nibabel.spatialimages import HeaderDataError
@@ -353,12 +351,13 @@ def get_ordinate(self, nw):
353351
'''
354352

355353

356-
_pix_attrs = ('PixelData', 'FloatPixelData', 'DoubleFloatPixelData')
354+
_pix_tags = tuple(tag_for_keyword(x) for x in ('PixelData', 'FloatPixelData', 'DoubleFloatPixelData'))
357355

358356

359357
def is_image(dcm):
360358
'''Test if the data set is an image'''
361-
if any(hasattr(dcm, attr) for attr in _pix_attrs):
359+
tags = set(dcm.keys())
360+
if any(t in tags for t in _pix_tags):
362361
return True
363362
return False
364363

@@ -1056,7 +1055,7 @@ def parse_and_group(src_paths, group_by=DEFAULT_GROUP_KEYS, extractor=None,
10561055
for dcm_path in src_paths:
10571056
#Read the DICOM file
10581057
try:
1059-
dcm = pydicom.read_file(dcm_path, force=force)
1058+
dcm = pydicom.dcmread(dcm_path, force=force, defer_size=32)
10601059
except Exception as e:
10611060
if warn_on_except:
10621061
warnings.warn('Error reading file %s: %s' % (dcm_path, str(e)))

‎src/dcmstack/extract.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import pydicom
1212
from pydicom.tag import BaseTag
1313
from pydicom.dataset import PrivateBlock
14-
from pydicom.datadict import keyword_for_tag, private_dictionaries
14+
from pydicom.datadict import keyword_for_tag, tag_for_keyword, private_dictionaries
1515
from pydicom.charset import decode_element
1616
from nibabel.nicom import csareader
1717

@@ -76,10 +76,9 @@ def ignore_unknown_private(tag, name, ds):
7676

7777
def make_ignore_except_rule(include):
7878
"""Make rule that ignores everying not in `include`"""
79+
incl_tags = set([tag_for_keyword(x) for x in include if isinstance(x, str)])
7980
def ignore_except(tag, name, ds):
80-
if tag in include:
81-
return False
82-
return name not in include
81+
return tag not in incl_tags
8382
return ignore_except
8483

8584

‎test/test_dcmmeta.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ def test_from_dicom():
10391039
'dcmstack',
10401040
'2D_16Echo_qT2')
10411041
src_fn = path.join(data_dir, 'TE_40_SlcPos_-33.707626341697.dcm')
1042-
src_dcm = pydicom.read_file(src_fn)
1042+
src_dcm = pydicom.dcmread(src_fn)
10431043
src_dw = nb.nicom.dicomwrappers.wrapper_from_data(src_dcm)
10441044
meta = {'EchoTime': 40}
10451045
nw = dcmmeta.NiftiWrapper.from_dicom(src_dcm, meta)

‎test/test_dcmstack.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def test_image_collision():
205205
'dcmstack',
206206
'2D_16Echo_qT2',
207207
'TE_20_SlcPos_-33.707626341697.dcm')
208-
dcm = pydicom.read_file(dcm_path)
208+
dcm = pydicom.dcmread(dcm_path)
209209
stack = dcmstack.DicomStack('EchoTime')
210210
stack.add_dcm(dcm)
211211
with pytest.raises(dcmstack.ImageCollisionError):
@@ -259,7 +259,7 @@ def setup_method(self, method):
259259
'data',
260260
'dcmstack',
261261
'2D_16Echo_qT2')
262-
self.inputs = [pydicom.read_file(path.join(data_dir, fn))
262+
self.inputs = [pydicom.dcmread(path.join(data_dir, fn))
263263
for fn in ('TE_20_SlcPos_-33.707626341697.dcm',
264264
'TE_20_SlcPos_-23.207628249046.dcm',
265265
'TE_40_SlcPos_-33.707626341697.dcm',
@@ -310,7 +310,7 @@ def setup_method(self, method):
310310
'data',
311311
'dcmstack',
312312
'2D_16Echo_qT2')
313-
self.inputs = [pydicom.read_file(path.join(data_dir, fn))
313+
self.inputs = [pydicom.dcmread(path.join(data_dir, fn))
314314
for fn in ('TE_40_SlcPos_-33.707626341697.dcm',
315315
'TE_40_SlcPos_-23.207628249046.dcm',
316316
'TE_60_SlcPos_-33.707626341697.dcm',
@@ -355,7 +355,7 @@ def setup_method(self, method):
355355
'data',
356356
'dcmstack',
357357
'2D_16Echo_qT2')
358-
self.inputs = [pydicom.read_file(path.join(data_dir, fn))
358+
self.inputs = [pydicom.dcmread(path.join(data_dir, fn))
359359
for fn in ('TE_40_SlcPos_-33.707626341697.dcm',
360360
'TE_40_SlcPos_-23.207628249046.dcm',
361361
'TE_60_SlcPos_-33.707626341697.dcm',
@@ -408,7 +408,7 @@ def setup_method(self, method):
408408
'data',
409409
'dcmstack',
410410
'2D_16Echo_qT2')
411-
self.inputs = [pydicom.read_file(path.join(data_dir, fn))
411+
self.inputs = [pydicom.dcmread(path.join(data_dir, fn))
412412
for fn in ('TE_40_SlcPos_-33.707626341697.dcm',
413413
'TE_40_SlcPos_-23.207628249046.dcm',
414414
'TE_60_SlcPos_-33.707626341697.dcm',
@@ -477,7 +477,7 @@ def setup_method(self, method):
477477
'data',
478478
'dcmstack',
479479
'2D_16Echo_qT2')
480-
self.inputs = [pydicom.read_file(path.join(self.data_dir, fn))
480+
self.inputs = [pydicom.dcmread(path.join(self.data_dir, fn))
481481
for fn in ('TE_20_SlcPos_-33.707626341697.dcm',
482482
'TE_20_SlcPos_-23.207628249046.dcm'
483483
)
@@ -544,7 +544,7 @@ def setup_method(self, method):
544544
'data',
545545
'dcmstack',
546546
'2D_16Echo_qT2')
547-
self.inputs = [pydicom.read_file(path.join(self.data_dir, fn))
547+
self.inputs = [pydicom.dcmread(path.join(self.data_dir, fn))
548548
for fn in ('TE_20_SlcPos_-33.707626341697.dcm',
549549
'TE_20_SlcPos_-23.207628249046.dcm',
550550
'TE_40_SlcPos_-33.707626341697.dcm',
@@ -646,7 +646,7 @@ def setup_method(self, method):
646646
def test_default(self):
647647
res = dcmstack.parse_and_group(self.in_paths)
648648
assert len(res) == 1
649-
ds = pydicom.read_file(self.in_paths[0])
649+
ds = pydicom.dcmread(self.in_paths[0])
650650
group_key = list(res.keys())[0]
651651
for attr_idx, attr in enumerate(dcmstack.DEFAULT_GROUP_KEYS):
652652
if attr in dcmstack.DEFAULT_CLOSE_KEYS:
@@ -672,7 +672,7 @@ def setup_method(self, method):
672672
def test_default(self):
673673
res = dcmstack.parse_and_stack(self.in_paths)
674674
assert len(res) == 1
675-
ds = pydicom.read_file(self.in_paths[0])
675+
ds = pydicom.dcmread(self.in_paths[0])
676676
group_key = list(res.keys())[0]
677677
for attr_idx, attr in enumerate(dcmstack.DEFAULT_GROUP_KEYS):
678678
if attr in dcmstack.DEFAULT_CLOSE_KEYS:

‎test/test_extract.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
class TestCsa(object):
2121
def setup_method(self, method):
2222
data_fn = path.join(test_dir, 'data', 'extract', 'csa_test.dcm')
23-
self.data = pydicom.read_file(data_fn)
23+
self.data = pydicom.dcmread(data_fn)
2424

2525
def teardown_method(self, method):
2626
del self.data
@@ -70,7 +70,7 @@ def test_csa_series_trans(self):
7070
class TestMetaExtractor(object):
7171
def setup_method(self, method):
7272
data_fn = path.join(test_dir, 'data', 'extract', 'csa_test.dcm')
73-
self.data = pydicom.read_file(data_fn)
73+
self.data = pydicom.dcmread(data_fn)
7474

7575
def teardown_method(self,method):
7676
del self.data

0 commit comments

Comments
 (0)
Please sign in to comment.