Skip to content

Commit 3c2961b

Browse files
authored
Merge pull request #1050 from effigies/mnt/update_minimum_pydicom
MNT: Set minimum pydicom to 1.0.0
2 parents 322fa38 + f1983ba commit 3c2961b

13 files changed

+45
-61
lines changed

nibabel/dft.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
from io import BytesIO
2525

2626
from .nifti1 import Nifti1Header
27+
from nibabel.optpkg import optional_package
2728

28-
from .pydicom_compat import pydicom, read_file
29+
pydicom = optional_package("pydicom")[0]
2930

3031
logger = logging.getLogger('nibabel.dft')
3132

@@ -241,7 +242,7 @@ def __getattribute__(self, name):
241242
return val
242243

243244
def dicom(self):
244-
return read_file(self.files[0])
245+
return pydicom.read_file(self.files[0])
245246

246247

247248
class _db_nochange:
@@ -386,7 +387,7 @@ def _update_dir(c, dir, files, studies, series, storage_instances):
386387

387388
def _update_file(c, path, fname, studies, series, storage_instances):
388389
try:
389-
do = read_file(f'{path}/{fname}')
390+
do = pydicom.read_file(f'{path}/{fname}')
390391
except pydicom.filereader.InvalidDicomError:
391392
logger.debug(' not a DICOM file')
392393
return None

nibabel/nicom/dicomwrappers.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717

1818
import numpy as np
1919

20+
from nibabel.optpkg import optional_package
2021
from . import csareader as csar
2122
from .dwiparams import B2q, nearest_pos_semi_def, q2bg
2223
from ..openers import ImageOpener
2324
from ..onetime import auto_attr as one_time
24-
from ..pydicom_compat import tag_for_keyword, Sequence
2525
from ..deprecated import deprecate_with_version
2626

27+
pydicom = optional_package("pydicom")[0]
28+
2729

2830
class WrapperError(Exception):
2931
pass
@@ -52,10 +54,8 @@ def wrapper_from_file(file_like, *args, **kwargs):
5254
dcm_w : ``dicomwrappers.Wrapper`` or subclass
5355
DICOM wrapper corresponding to DICOM data type
5456
"""
55-
from ..pydicom_compat import read_file
56-
5757
with ImageOpener(file_like) as fobj:
58-
dcm_data = read_file(fobj, *args, **kwargs)
58+
dcm_data = pydicom.read_file(fobj, *args, **kwargs)
5959
return wrapper_from_data(dcm_data)
6060

6161

@@ -520,7 +520,7 @@ def image_shape(self):
520520
if hasattr(first_frame, 'get') and first_frame.get([0x18, 0x9117]):
521521
# DWI image may include derived isotropic, ADC or trace volume
522522
try:
523-
self.frames = Sequence(
523+
self.frames = pydicom.Sequence(
524524
frame for frame in self.frames if
525525
frame.MRDiffusionSequence[0].DiffusionDirectionality
526526
!= 'ISOTROPIC'
@@ -550,15 +550,15 @@ def image_shape(self):
550550
# Determine if one of the dimension indices refers to the stack id
551551
dim_seq = [dim.DimensionIndexPointer
552552
for dim in self.get('DimensionIndexSequence')]
553-
stackid_tag = tag_for_keyword('StackID')
553+
stackid_tag = pydicom.datadict.tag_for_keyword('StackID')
554554
# remove the stack id axis if present
555555
if stackid_tag in dim_seq:
556556
stackid_dim_idx = dim_seq.index(stackid_tag)
557557
frame_indices = np.delete(frame_indices, stackid_dim_idx, axis=1)
558558
dim_seq.pop(stackid_dim_idx)
559559
if has_derived:
560560
# derived volume is included
561-
derived_tag = tag_for_keyword("DiffusionBValue")
561+
derived_tag = pydicom.datadict.tag_for_keyword("DiffusionBValue")
562562
if derived_tag not in dim_seq:
563563
raise WrapperError("Missing information, cannot remove indices "
564564
"with confidence.")

nibabel/nicom/tests/__init__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from ...pydicom_compat import have_dicom
21
import unittest
2+
from nibabel.optpkg import optional_package
33

4-
dicom_test = unittest.skipUnless(have_dicom, "Could not import dicom or pydicom")
4+
pydicom, have_dicom, _ = optional_package("pydicom")
5+
6+
dicom_test = unittest.skipUnless(have_dicom, "Could not import pydicom")

nibabel/nicom/tests/test_csareader.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77

88
import numpy as np
99

10-
from ...pydicom_compat import pydicom
1110
from .. import csareader as csa
1211
from .. import dwiparams as dwp
1312

1413
import pytest
15-
from . import dicom_test
14+
from . import pydicom, dicom_test
1615
from .test_dicomwrappers import IO_DATA_PATH, DATA
1716

1817
CSA2_B0 = open(pjoin(IO_DATA_PATH, 'csa2_b0.bin'), 'rb').read()

nibabel/nicom/tests/test_dicomreaders.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,23 @@
66

77
import numpy as np
88

9+
from nibabel.optpkg import optional_package
910
from .. import dicomreaders as didr
10-
from ...pydicom_compat import pydicom
11-
12-
import pytest
13-
from . import dicom_test
14-
1511
from .test_dicomwrappers import EXPECTED_AFFINE, EXPECTED_PARAMS, IO_DATA_PATH, DATA
1612

13+
import pytest
1714
from numpy.testing import assert_array_equal, assert_array_almost_equal
1815

16+
pydicom, _, setup_module = optional_package("pydicom")
17+
1918

20-
@dicom_test
2119
def test_read_dwi():
2220
img = didr.mosaic_to_nii(DATA)
2321
arr = img.get_fdata()
2422
assert arr.shape == (128, 128, 48)
2523
assert_array_almost_equal(img.affine, EXPECTED_AFFINE)
2624

2725

28-
@dicom_test
2926
def test_read_dwis():
3027
data, aff, bs, gs = didr.read_mosaic_dwi_dir(IO_DATA_PATH,
3128
'siemens_dwi_*.dcm.gz')
@@ -37,7 +34,6 @@ def test_read_dwis():
3734
didr.read_mosaic_dwi_dir('improbable')
3835

3936

40-
@dicom_test
4137
def test_passing_kwds():
4238
# Check that we correctly pass keywords to dicom
4339
dwi_glob = 'siemens_dwi_*.dcm.gz'
@@ -61,7 +57,7 @@ def test_passing_kwds():
6157
with pytest.raises(didr.DicomReadError):
6258
func(IO_DATA_PATH, csa_glob, dicom_kwargs=dict(force=True))
6359

64-
@dicom_test
60+
6561
def test_slices_to_series():
6662
dicom_files = (pjoin(IO_DATA_PATH, "%d.dcm" % i) for i in range(2))
6763
wrappers = [didr.wrapper_from_file(f) for f in dicom_files]

nibabel/nicom/tests/test_dicomwrappers.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@
99

1010
import numpy as np
1111

12-
from nibabel.pydicom_compat import have_dicom, pydicom, read_file, tag_for_keyword
13-
12+
from . import pydicom, have_dicom, dicom_test
1413
from .. import dicomwrappers as didw
1514
from .. import dicomreaders as didr
1615
from ...volumeutils import endian_codes
1716

1817
import pytest
1918
from unittest import TestCase
20-
from . import dicom_test
2119

2220
from numpy.testing import assert_array_equal, assert_array_almost_equal
2321
from ...tests.nibabel_data import get_nibabel_data, needs_nibabel_data
@@ -26,8 +24,8 @@
2624
DATA_FILE = pjoin(IO_DATA_PATH, 'siemens_dwi_1000.dcm.gz')
2725
DATA_FILE_PHILIPS = pjoin(IO_DATA_PATH, 'philips_mprage.dcm.gz')
2826
if have_dicom:
29-
DATA = read_file(gzip.open(DATA_FILE))
30-
DATA_PHILIPS = read_file(gzip.open(DATA_FILE_PHILIPS))
27+
DATA = pydicom.read_file(gzip.open(DATA_FILE))
28+
DATA_PHILIPS = pydicom.read_file(gzip.open(DATA_FILE_PHILIPS))
3129
else:
3230
DATA = None
3331
DATA_PHILIPS = None
@@ -434,8 +432,8 @@ def __init__(self, div, sid):
434432
dim_idx_seq = [DimIdxSeqElem()] * num_of_frames
435433
# add an entry for StackID into the DimensionIndexSequence
436434
if sid_dim is not None:
437-
sid_tag = tag_for_keyword('StackID')
438-
fcs_tag = tag_for_keyword('FrameContentSequence')
435+
sid_tag = pydicom.datadict.tag_for_keyword('StackID')
436+
fcs_tag = pydicom.datadict.tag_for_keyword('FrameContentSequence')
439437
dim_idx_seq[sid_dim] = DimIdxSeqElem(sid_tag, fcs_tag)
440438
# create the PerFrameFunctionalGroupsSequence
441439
frames = [PerFrmFuncGrpSeqElem(div, sid)

nibabel/nicom/tests/test_utils.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
"""
33
import re
44

5+
from nibabel.optpkg import optional_package
6+
from .test_dicomwrappers import DATA, DATA_PHILIPS
57
from ..utils import find_private_section
68

7-
from . import dicom_test
8-
from ...pydicom_compat import pydicom
9-
from .test_dicomwrappers import DATA, DATA_PHILIPS
9+
pydicom, _, setup_module = optional_package("pydicom")
1010

1111

12-
@dicom_test
1312
def test_find_private_section_real():
1413
# Find section containing named private creator information
1514
# On real data first

nibabel/nifti1.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import numpy.linalg as npl
1818
from numpy.compat.py3k import asstr
1919

20+
from .optpkg import optional_package
2021
from .filebasedimages import SerializableImage
2122
from .volumeutils import Recoder, make_dt_codes, endian_codes
2223
from .spatialimages import HeaderDataError, ImageFileError
@@ -25,7 +26,8 @@
2526
from . import analyze # module import
2627
from .spm99analyze import SpmAnalyzeHeader
2728
from .casting import have_binary128
28-
from .pydicom_compat import have_dicom, pydicom as pdcm
29+
30+
pdcm, have_dicom, _ = optional_package("pydicom")
2931

3032
# nifti1 flat header definition for Analyze-like first 348 bytes
3133
# first number in comments indicates offset in file header in bytes

nibabel/pydicom_compat.py

+7-20
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,17 @@
3030
pydicom = read_file = tag_for_keyword = Sequence = None
3131

3232
try:
33-
import dicom as pydicom
33+
import pydicom
3434
except ImportError:
35-
try:
36-
import pydicom
37-
except ImportError:
38-
have_dicom = False
39-
else: # pydicom module available
40-
from pydicom.dicomio import read_file
41-
from pydicom.sequence import Sequence
42-
# Values not imported by default
43-
import pydicom.values
44-
else: # dicom module available
35+
have_dicom = False
36+
else: # pydicom module available
37+
from pydicom.dicomio import read_file
38+
from pydicom.sequence import Sequence
4539
# Values not imported by default
46-
import dicom.values
47-
from dicom.sequence import Sequence
48-
read_file = pydicom.read_file
40+
import pydicom.values
4941

5042
if have_dicom:
51-
try:
52-
# Versions >= 1.0
53-
tag_for_keyword = pydicom.datadict.tag_for_keyword
54-
except AttributeError:
55-
# Versions < 1.0 - also has more search options.
56-
tag_for_keyword = pydicom.datadict.tag_for_name
43+
tag_for_keyword = pydicom.datadict.tag_for_keyword
5744

5845

5946
@deprecate_with_version("dicom_test has been moved to nibabel.nicom.tests",

nibabel/tests/test_dft.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
# Shield optional package imports
1717
from ..optpkg import optional_package
1818

19-
from nibabel.pydicom_compat import have_dicom
20-
19+
have_dicom = optional_package('pydicom')[1]
2120
PImage, have_pil, _ = optional_package('PIL.Image')
2221

2322
data_dir = pjoin(dirname(__file__), 'data')

nibabel/tests/test_nifti1.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
slice_order_codes)
2525
from nibabel.spatialimages import HeaderDataError
2626
from nibabel.tmpdirs import InTemporaryDirectory
27+
from nibabel.optpkg import optional_package
2728
from ..freesurfer import load as mghload
2829
from ..orientations import aff2axcodes
2930

@@ -52,8 +53,8 @@
5253
header_file = os.path.join(data_path, 'nifti1.hdr')
5354
image_file = os.path.join(data_path, 'example4d.nii.gz')
5455

55-
from ..pydicom_compat import pydicom, have_dicom
56-
dicom_test = unittest.skipUnless(have_dicom, "Could not import dicom or pydicom")
56+
pydicom, have_dicom, _ = optional_package("pydicom")
57+
dicom_test = unittest.skipUnless(have_dicom, "Could not import pydicom")
5758

5859

5960
# Example transformation matrix

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ packages = find:
3737

3838
[options.extras_require]
3939
dicom =
40-
pydicom >=0.9.9
40+
pydicom >=1.0.0
4141
dicomfs =
4242
%(dicom)s
4343
pillow

tools/ci/env.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ DEFAULT_OPT_DEPENDS="scipy matplotlib pillow pydicom h5py indexed_gzip"
99
# pydicom has skipped some important pre-releases, so enable a check against master
1010
PYDICOM_MASTER="git+https://github.com/pydicom/pydicom.git@master"
1111
# Minimum versions of optional requirements
12-
MIN_OPT_DEPENDS="matplotlib==1.5.3 pydicom==0.9.9 pillow==2.6"
12+
MIN_OPT_DEPENDS="matplotlib==1.5.3 pydicom==1.0.1 pillow==2.6"
1313

1414
# Numpy and scipy upload nightly/weekly/intermittent wheels
1515
NIGHTLY_WHEELS="https://pypi.anaconda.org/scipy-wheels-nightly/simple"

0 commit comments

Comments
 (0)