Skip to content

Commit

Permalink
Merge pull request #1050 from effigies/mnt/update_minimum_pydicom
Browse files Browse the repository at this point in the history
MNT: Set minimum pydicom to 1.0.0
  • Loading branch information
effigies authored Sep 10, 2021
2 parents 322fa38 + f1983ba commit 3c2961b
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 61 deletions.
7 changes: 4 additions & 3 deletions nibabel/dft.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
from io import BytesIO

from .nifti1 import Nifti1Header
from nibabel.optpkg import optional_package

from .pydicom_compat import pydicom, read_file
pydicom = optional_package("pydicom")[0]

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

Expand Down Expand Up @@ -241,7 +242,7 @@ def __getattribute__(self, name):
return val

def dicom(self):
return read_file(self.files[0])
return pydicom.read_file(self.files[0])


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

def _update_file(c, path, fname, studies, series, storage_instances):
try:
do = read_file(f'{path}/{fname}')
do = pydicom.read_file(f'{path}/{fname}')
except pydicom.filereader.InvalidDicomError:
logger.debug(' not a DICOM file')
return None
Expand Down
14 changes: 7 additions & 7 deletions nibabel/nicom/dicomwrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

import numpy as np

from nibabel.optpkg import optional_package
from . import csareader as csar
from .dwiparams import B2q, nearest_pos_semi_def, q2bg
from ..openers import ImageOpener
from ..onetime import auto_attr as one_time
from ..pydicom_compat import tag_for_keyword, Sequence
from ..deprecated import deprecate_with_version

pydicom = optional_package("pydicom")[0]


class WrapperError(Exception):
pass
Expand Down Expand Up @@ -52,10 +54,8 @@ def wrapper_from_file(file_like, *args, **kwargs):
dcm_w : ``dicomwrappers.Wrapper`` or subclass
DICOM wrapper corresponding to DICOM data type
"""
from ..pydicom_compat import read_file

with ImageOpener(file_like) as fobj:
dcm_data = read_file(fobj, *args, **kwargs)
dcm_data = pydicom.read_file(fobj, *args, **kwargs)
return wrapper_from_data(dcm_data)


Expand Down Expand Up @@ -520,7 +520,7 @@ def image_shape(self):
if hasattr(first_frame, 'get') and first_frame.get([0x18, 0x9117]):
# DWI image may include derived isotropic, ADC or trace volume
try:
self.frames = Sequence(
self.frames = pydicom.Sequence(
frame for frame in self.frames if
frame.MRDiffusionSequence[0].DiffusionDirectionality
!= 'ISOTROPIC'
Expand Down Expand Up @@ -550,15 +550,15 @@ def image_shape(self):
# Determine if one of the dimension indices refers to the stack id
dim_seq = [dim.DimensionIndexPointer
for dim in self.get('DimensionIndexSequence')]
stackid_tag = tag_for_keyword('StackID')
stackid_tag = pydicom.datadict.tag_for_keyword('StackID')
# remove the stack id axis if present
if stackid_tag in dim_seq:
stackid_dim_idx = dim_seq.index(stackid_tag)
frame_indices = np.delete(frame_indices, stackid_dim_idx, axis=1)
dim_seq.pop(stackid_dim_idx)
if has_derived:
# derived volume is included
derived_tag = tag_for_keyword("DiffusionBValue")
derived_tag = pydicom.datadict.tag_for_keyword("DiffusionBValue")
if derived_tag not in dim_seq:
raise WrapperError("Missing information, cannot remove indices "
"with confidence.")
Expand Down
6 changes: 4 additions & 2 deletions nibabel/nicom/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from ...pydicom_compat import have_dicom
import unittest
from nibabel.optpkg import optional_package

dicom_test = unittest.skipUnless(have_dicom, "Could not import dicom or pydicom")
pydicom, have_dicom, _ = optional_package("pydicom")

dicom_test = unittest.skipUnless(have_dicom, "Could not import pydicom")
3 changes: 1 addition & 2 deletions nibabel/nicom/tests/test_csareader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@

import numpy as np

from ...pydicom_compat import pydicom
from .. import csareader as csa
from .. import dwiparams as dwp

import pytest
from . import dicom_test
from . import pydicom, dicom_test
from .test_dicomwrappers import IO_DATA_PATH, DATA

CSA2_B0 = open(pjoin(IO_DATA_PATH, 'csa2_b0.bin'), 'rb').read()
Expand Down
14 changes: 5 additions & 9 deletions nibabel/nicom/tests/test_dicomreaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,23 @@

import numpy as np

from nibabel.optpkg import optional_package
from .. import dicomreaders as didr
from ...pydicom_compat import pydicom

import pytest
from . import dicom_test

from .test_dicomwrappers import EXPECTED_AFFINE, EXPECTED_PARAMS, IO_DATA_PATH, DATA

import pytest
from numpy.testing import assert_array_equal, assert_array_almost_equal

pydicom, _, setup_module = optional_package("pydicom")


@dicom_test
def test_read_dwi():
img = didr.mosaic_to_nii(DATA)
arr = img.get_fdata()
assert arr.shape == (128, 128, 48)
assert_array_almost_equal(img.affine, EXPECTED_AFFINE)


@dicom_test
def test_read_dwis():
data, aff, bs, gs = didr.read_mosaic_dwi_dir(IO_DATA_PATH,
'siemens_dwi_*.dcm.gz')
Expand All @@ -37,7 +34,6 @@ def test_read_dwis():
didr.read_mosaic_dwi_dir('improbable')


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

@dicom_test

def test_slices_to_series():
dicom_files = (pjoin(IO_DATA_PATH, "%d.dcm" % i) for i in range(2))
wrappers = [didr.wrapper_from_file(f) for f in dicom_files]
Expand Down
12 changes: 5 additions & 7 deletions nibabel/nicom/tests/test_dicomwrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@

import numpy as np

from nibabel.pydicom_compat import have_dicom, pydicom, read_file, tag_for_keyword

from . import pydicom, have_dicom, dicom_test
from .. import dicomwrappers as didw
from .. import dicomreaders as didr
from ...volumeutils import endian_codes

import pytest
from unittest import TestCase
from . import dicom_test

from numpy.testing import assert_array_equal, assert_array_almost_equal
from ...tests.nibabel_data import get_nibabel_data, needs_nibabel_data
Expand All @@ -26,8 +24,8 @@
DATA_FILE = pjoin(IO_DATA_PATH, 'siemens_dwi_1000.dcm.gz')
DATA_FILE_PHILIPS = pjoin(IO_DATA_PATH, 'philips_mprage.dcm.gz')
if have_dicom:
DATA = read_file(gzip.open(DATA_FILE))
DATA_PHILIPS = read_file(gzip.open(DATA_FILE_PHILIPS))
DATA = pydicom.read_file(gzip.open(DATA_FILE))
DATA_PHILIPS = pydicom.read_file(gzip.open(DATA_FILE_PHILIPS))
else:
DATA = None
DATA_PHILIPS = None
Expand Down Expand Up @@ -434,8 +432,8 @@ def __init__(self, div, sid):
dim_idx_seq = [DimIdxSeqElem()] * num_of_frames
# add an entry for StackID into the DimensionIndexSequence
if sid_dim is not None:
sid_tag = tag_for_keyword('StackID')
fcs_tag = tag_for_keyword('FrameContentSequence')
sid_tag = pydicom.datadict.tag_for_keyword('StackID')
fcs_tag = pydicom.datadict.tag_for_keyword('FrameContentSequence')
dim_idx_seq[sid_dim] = DimIdxSeqElem(sid_tag, fcs_tag)
# create the PerFrameFunctionalGroupsSequence
frames = [PerFrmFuncGrpSeqElem(div, sid)
Expand Down
7 changes: 3 additions & 4 deletions nibabel/nicom/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
"""
import re

from nibabel.optpkg import optional_package
from .test_dicomwrappers import DATA, DATA_PHILIPS
from ..utils import find_private_section

from . import dicom_test
from ...pydicom_compat import pydicom
from .test_dicomwrappers import DATA, DATA_PHILIPS
pydicom, _, setup_module = optional_package("pydicom")


@dicom_test
def test_find_private_section_real():
# Find section containing named private creator information
# On real data first
Expand Down
4 changes: 3 additions & 1 deletion nibabel/nifti1.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import numpy.linalg as npl
from numpy.compat.py3k import asstr

from .optpkg import optional_package
from .filebasedimages import SerializableImage
from .volumeutils import Recoder, make_dt_codes, endian_codes
from .spatialimages import HeaderDataError, ImageFileError
Expand All @@ -25,7 +26,8 @@
from . import analyze # module import
from .spm99analyze import SpmAnalyzeHeader
from .casting import have_binary128
from .pydicom_compat import have_dicom, pydicom as pdcm

pdcm, have_dicom, _ = optional_package("pydicom")

# nifti1 flat header definition for Analyze-like first 348 bytes
# first number in comments indicates offset in file header in bytes
Expand Down
27 changes: 7 additions & 20 deletions nibabel/pydicom_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,17 @@
pydicom = read_file = tag_for_keyword = Sequence = None

try:
import dicom as pydicom
import pydicom
except ImportError:
try:
import pydicom
except ImportError:
have_dicom = False
else: # pydicom module available
from pydicom.dicomio import read_file
from pydicom.sequence import Sequence
# Values not imported by default
import pydicom.values
else: # dicom module available
have_dicom = False
else: # pydicom module available
from pydicom.dicomio import read_file
from pydicom.sequence import Sequence
# Values not imported by default
import dicom.values
from dicom.sequence import Sequence
read_file = pydicom.read_file
import pydicom.values

if have_dicom:
try:
# Versions >= 1.0
tag_for_keyword = pydicom.datadict.tag_for_keyword
except AttributeError:
# Versions < 1.0 - also has more search options.
tag_for_keyword = pydicom.datadict.tag_for_name
tag_for_keyword = pydicom.datadict.tag_for_keyword


@deprecate_with_version("dicom_test has been moved to nibabel.nicom.tests",
Expand Down
3 changes: 1 addition & 2 deletions nibabel/tests/test_dft.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
# Shield optional package imports
from ..optpkg import optional_package

from nibabel.pydicom_compat import have_dicom

have_dicom = optional_package('pydicom')[1]
PImage, have_pil, _ = optional_package('PIL.Image')

data_dir = pjoin(dirname(__file__), 'data')
Expand Down
5 changes: 3 additions & 2 deletions nibabel/tests/test_nifti1.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
slice_order_codes)
from nibabel.spatialimages import HeaderDataError
from nibabel.tmpdirs import InTemporaryDirectory
from nibabel.optpkg import optional_package
from ..freesurfer import load as mghload
from ..orientations import aff2axcodes

Expand Down Expand Up @@ -52,8 +53,8 @@
header_file = os.path.join(data_path, 'nifti1.hdr')
image_file = os.path.join(data_path, 'example4d.nii.gz')

from ..pydicom_compat import pydicom, have_dicom
dicom_test = unittest.skipUnless(have_dicom, "Could not import dicom or pydicom")
pydicom, have_dicom, _ = optional_package("pydicom")
dicom_test = unittest.skipUnless(have_dicom, "Could not import pydicom")


# Example transformation matrix
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ packages = find:

[options.extras_require]
dicom =
pydicom >=0.9.9
pydicom >=1.0.0
dicomfs =
%(dicom)s
pillow
Expand Down
2 changes: 1 addition & 1 deletion tools/ci/env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ DEFAULT_OPT_DEPENDS="scipy matplotlib pillow pydicom h5py indexed_gzip"
# pydicom has skipped some important pre-releases, so enable a check against master
PYDICOM_MASTER="git+https://github.com/pydicom/pydicom.git@master"
# Minimum versions of optional requirements
MIN_OPT_DEPENDS="matplotlib==1.5.3 pydicom==0.9.9 pillow==2.6"
MIN_OPT_DEPENDS="matplotlib==1.5.3 pydicom==1.0.1 pillow==2.6"

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

0 comments on commit 3c2961b

Please sign in to comment.