Skip to content

TEST: Convert nibabel.nicom to unittest/pytest #884

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

Merged
merged 1 commit into from
Feb 8, 2020
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
5 changes: 4 additions & 1 deletion nibabel/nicom/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# init to allow relative imports in tests
from ...pydicom_compat import have_dicom
import unittest

dicom_test = unittest.skipUnless(have_dicom, "Could not import dicom or pydicom")
79 changes: 38 additions & 41 deletions nibabel/nicom/tests/test_csareader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@

import numpy as np

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

from nose.tools import (assert_true, assert_false, assert_equal, assert_raises)

from ...testing import skipif

from nibabel.pydicom_compat import dicom_test, pydicom
from .test_dicomwrappers import (IO_DATA_PATH, DATA)
import pytest
from . import dicom_test
from .test_dicomwrappers import IO_DATA_PATH, DATA

CSA2_B0 = open(pjoin(IO_DATA_PATH, 'csa2_b0.bin'), 'rb').read()
CSA2_B1000 = open(pjoin(IO_DATA_PATH, 'csa2_b1000.bin'), 'rb').read()
Expand All @@ -27,59 +25,61 @@
@dicom_test
def test_csa_header_read():
hdr = csa.get_csa_header(DATA, 'image')
assert_equal(hdr['n_tags'], 83)
assert_equal(csa.get_csa_header(DATA, 'series')['n_tags'], 65)
assert_raises(ValueError, csa.get_csa_header, DATA, 'xxxx')
assert_true(csa.is_mosaic(hdr))
assert hdr['n_tags'] == 83
assert csa.get_csa_header(DATA, 'series')['n_tags'] == 65
with pytest.raises(ValueError):
csa.get_csa_header(DATA, 'xxxx')
assert csa.is_mosaic(hdr)
# Get a shallow copy of the data, lacking the CSA marker
# Need to do it this way because del appears broken in pydicom 0.9.7
data2 = pydicom.dataset.Dataset()
for element in DATA:
if (element.tag.group, element.tag.elem) != (0x29, 0x10):
data2.add(element)
assert_equal(csa.get_csa_header(data2, 'image'), None)
assert csa.get_csa_header(data2, 'image') is None
# Add back the marker - CSA works again
data2[(0x29, 0x10)] = DATA[(0x29, 0x10)]
assert_true(csa.is_mosaic(csa.get_csa_header(data2, 'image')))
assert csa.is_mosaic(csa.get_csa_header(data2, 'image'))


def test_csas0():
for csa_str in (CSA2_B0, CSA2_B1000):
csa_info = csa.read(csa_str)
assert_equal(csa_info['type'], 2)
assert_equal(csa_info['n_tags'], 83)
assert csa_info['type'] == 2
assert csa_info['n_tags'] == 83
tags = csa_info['tags']
assert_equal(len(tags), 83)
assert len(tags) == 83
n_o_m = tags['NumberOfImagesInMosaic']
assert_equal(n_o_m['items'], [48])
assert n_o_m['items'] == [48]
csa_info = csa.read(CSA2_B1000)
b_matrix = csa_info['tags']['B_matrix']
assert_equal(len(b_matrix['items']), 6)
assert len(b_matrix['items']) == 6
b_value = csa_info['tags']['B_value']
assert_equal(b_value['items'], [1000])
assert b_value['items'] == [1000]


def test_csa_len0():
# We did get a failure for item with item_len of 0 - gh issue #92
csa_info = csa.read(CSA2_0len)
assert_equal(csa_info['type'], 2)
assert_equal(csa_info['n_tags'], 44)
assert csa_info['type'] == 2
assert csa_info['n_tags'] == 44
tags = csa_info['tags']
assert_equal(len(tags), 44)
assert len(tags) == 44


def test_csa_nitem():
# testing csa.read's ability to raise an error when n_items >= 200
assert_raises(csa.CSAReadError, csa.read, CSA_STR_1001n_items)
with pytest.raises(csa.CSAReadError):
csa.read(CSA_STR_1001n_items)
# OK when < 1000
csa_info = csa.read(CSA_STR_valid)
assert_equal(len(csa_info['tags']), 1)
assert len(csa_info['tags']) == 1
# OK after changing module global
n_items_thresh = csa.MAX_CSA_ITEMS
try:
csa.MAX_CSA_ITEMS = 2000
csa_info = csa.read(CSA_STR_1001n_items)
assert_equal(len(csa_info['tags']), 1)
assert len(csa_info['tags']) == 1
finally:
csa.MAX_CSA_ITEMS = n_items_thresh

Expand All @@ -88,32 +88,30 @@ def test_csa_params():
for csa_str in (CSA2_B0, CSA2_B1000):
csa_info = csa.read(csa_str)
n_o_m = csa.get_n_mosaic(csa_info)
assert_equal(n_o_m, 48)
assert n_o_m == 48
snv = csa.get_slice_normal(csa_info)
assert_equal(snv.shape, (3,))
assert_true(np.allclose(1,
np.sqrt((snv * snv).sum())))
assert snv.shape == (3,)
assert np.allclose(1, np.sqrt((snv * snv).sum()))
amt = csa.get_acq_mat_txt(csa_info)
assert_equal(amt, '128p*128')
assert amt == '128p*128'
csa_info = csa.read(CSA2_B0)
b_matrix = csa.get_b_matrix(csa_info)
assert_equal(b_matrix, None)
assert b_matrix is None
b_value = csa.get_b_value(csa_info)
assert_equal(b_value, 0)
assert b_value == 0
g_vector = csa.get_g_vector(csa_info)
assert_equal(g_vector, None)
assert g_vector is None
csa_info = csa.read(CSA2_B1000)
b_matrix = csa.get_b_matrix(csa_info)
assert_equal(b_matrix.shape, (3, 3))
assert b_matrix.shape == (3, 3)
# check (by absence of error) that the B matrix is positive
# semi-definite.
dwp.B2q(b_matrix) # no error
b_value = csa.get_b_value(csa_info)
assert_equal(b_value, 1000)
assert b_value == 1000
g_vector = csa.get_g_vector(csa_info)
assert_equal(g_vector.shape, (3,))
assert_true(
np.allclose(1, np.sqrt((g_vector * g_vector).sum())))
assert g_vector.shape == (3,)
assert np.allclose(1, np.sqrt((g_vector * g_vector).sum()))


def test_ice_dims():
Expand All @@ -124,9 +122,8 @@ def test_ice_dims():
for csa_str, ex_dims in ((CSA2_B0, ex_dims0),
(CSA2_B1000, ex_dims1)):
csa_info = csa.read(csa_str)
assert_equal(csa.get_ice_dims(csa_info),
ex_dims)
assert_equal(csa.get_ice_dims({}), None)
assert csa.get_ice_dims(csa_info) == ex_dims
assert csa.get_ice_dims({}) is None


@dicom_test
Expand All @@ -138,4 +135,4 @@ def test_missing_csa_elem():
csa_tag = pydicom.dataset.Tag(0x29, 0x1010)
del dcm[csa_tag]
hdr = csa.get_csa_header(dcm, 'image')
assert_equal(hdr, None)
assert hdr is None
44 changes: 17 additions & 27 deletions nibabel/nicom/tests/test_dicomreaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@

"""

from os.path import join as pjoin, abspath
from os.path import join as pjoin

import numpy as np

from .. import dicomreaders as didr
from ...pydicom_compat import pydicom

from nibabel.pydicom_compat import dicom_test, pydicom
import pytest
from . import dicom_test

from .test_dicomwrappers import (EXPECTED_AFFINE,
EXPECTED_PARAMS,
IO_DATA_PATH,
DATA)

from nose.tools import (assert_true, assert_false, assert_equal, assert_raises)
from .test_dicomwrappers import EXPECTED_AFFINE, EXPECTED_PARAMS, IO_DATA_PATH, DATA

from numpy.testing import assert_array_equal, assert_array_almost_equal

Expand All @@ -24,19 +21,20 @@
def test_read_dwi():
img = didr.mosaic_to_nii(DATA)
arr = img.get_data()
assert_equal(arr.shape, (128, 128, 48))
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')
assert_equal(data.ndim, 4)
assert data.ndim == 4
assert_array_almost_equal(aff, EXPECTED_AFFINE)
assert_array_almost_equal(bs, (0, EXPECTED_PARAMS[0]))
assert_array_almost_equal(gs, (np.zeros((3,)), EXPECTED_PARAMS[1]))
assert_raises(IOError, didr.read_mosaic_dwi_dir, 'improbable')
with pytest.raises(IOError):
didr.read_mosaic_dwi_dir('improbable')


@dicom_test
Expand All @@ -53,29 +51,21 @@ def test_passing_kwds():
dicom_kwargs=dict(force=True))
assert_array_equal(data, data2)
# This should raise an error in pydicom.dicomio.read_file
assert_raises(TypeError,
func,
IO_DATA_PATH,
dwi_glob,
dicom_kwargs=dict(not_a_parameter=True))
with pytest.raises(TypeError):
func(IO_DATA_PATH, dwi_glob, dicom_kwargs=dict(not_a_parameter=True))
# These are invalid dicoms, so will raise an error unless force=True
assert_raises(pydicom.filereader.InvalidDicomError,
func,
IO_DATA_PATH,
csa_glob)
with pytest.raises(pydicom.filereader.InvalidDicomError):
func(IO_DATA_PATH, csa_glob)
# But here, we catch the error because the dicoms are in the wrong
# format
assert_raises(didr.DicomReadError,
func,
IO_DATA_PATH,
csa_glob,
dicom_kwargs=dict(force=True))
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]
series = didr.slices_to_series(wrappers)
assert_equal(len(series), 1)
assert_equal(len(series[0]), 2)
assert len(series) == 1
assert len(series[0]) == 2

Loading