Skip to content

Commit 7f3b4bb

Browse files
committed
TST: Update dependent tests that were missed previously
1 parent f48f3c5 commit 7f3b4bb

File tree

1 file changed

+65
-93
lines changed

1 file changed

+65
-93
lines changed

nibabel/nicom/tests/test_dicomwrappers.py

Lines changed: 65 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import gzip
66
from hashlib import sha1
77
from decimal import Decimal
8-
from copy import copy
8+
from copy import copy
9+
from collections import namedtuple
910

1011
import numpy as np
1112

@@ -379,8 +380,40 @@ class Fake(object):
379380
setattr(fake_frame, seq_name, [fake_element])
380381
frames.append(fake_frame)
381382
return frames
382-
383-
383+
384+
385+
def fake_shape_dependents(div_seq, sid_seq=None, sid_dim=None):
386+
class DimIdxSeqElem(object):
387+
def __init__(self, dip=(0, 0), fgp=(0, 0)):
388+
self.DimensionIndexPointer = dip
389+
self.FunctionalGroupPointer = fgp
390+
class FrmContSeqElem(object):
391+
def __init__(self, div, sid):
392+
self.DimensionIndexValues = div
393+
self.StackID = sid
394+
class PerFrmFuncGrpSeqElem(object):
395+
def __init__(self, div, sid):
396+
self.FrameContentSequence = [FrmContSeqElem(div, sid)]
397+
# if no StackID values passed in then use the values at index 'sid_dim' in
398+
# the value for DimensionIndexValues for it
399+
if sid_seq is None:
400+
if sid_dim is None:
401+
sid_dim = 0
402+
sid_seq = [div[sid_dim] for div in div_seq]
403+
# create the DimensionIndexSequence
404+
num_of_frames = len(div_seq)
405+
dim_idx_seq = [DimIdxSeqElem()] * num_of_frames
406+
# add an entry for StackID into the DimensionIndexSequence
407+
if sid_dim is not None:
408+
dim_idx_seq[sid_dim] = DimIdxSeqElem((0x20, 0x9056), (0x20, 0x9111))
409+
# create the PerFrameFunctionalGroupsSequence
410+
frames = [PerFrmFuncGrpSeqElem(div, sid)
411+
for div, sid in zip(div_seq, sid_seq)]
412+
return {'NumberOfFrames' : num_of_frames,
413+
'DimensionIndexSequence' : dim_idx_seq,
414+
'PerFrameFunctionalGroupsSequence' : frames}
415+
416+
384417
class TestMultiFrameWrapper(TestCase):
385418
# Test MultiframeWrapper
386419
MINIMAL_MF = {
@@ -406,117 +439,65 @@ def test_shape(self):
406439
assert_raises(AssertionError, getattr, dw, 'image_shape')
407440
fake_mf['NumberOfFrames'] = 4
408441
# PerFrameFunctionalGroupsSequence does not match NumberOfFrames
409-
assert_raises(AssertionError, getattr, dw, 'image_shape')
410-
# Make some fake frame data for 3D
411-
def my_fake_frames(div_seq, sid_seq=None, sid_dim=None):
412-
frames = fake_frames('FrameContentSequence',
413-
'DimensionIndexValues',
414-
div_seq)
415-
if sid_seq is None:
416-
sid_seq = [div_elem[sid_dim] for div_elem in div_seq]
417-
for i, sid in enumerate(sid_seq):
418-
setattr(frames[i].FrameContentSequence[0], 'StackID', sid)
419-
# make fake DimensionIndexSequence
420-
def my_fake_dim_idx_seq(size, idx=None):
421-
class DimensionIndex(object):
422-
def __init__(self, dim_idx_ptr, func_grp_ptr):
423-
self.DimensionIndexPointer = dim_idx_ptr
424-
self.FunctionalGroupPointer = func_grp_ptr
425-
dim_idx_seq = [DimensionIndex((0x0, 0x0), (0x0, 0x0))] * size
426-
if idx is not None:
427-
dim_idx_seq[idx] = DimensionIndex((0x20, 0x9056),(0x20, 0x9111))
428-
return dim_idx_seq
429-
430-
# check 3D shape when StackID index is 0
431-
sid_dim = 0
432-
fake_mf['DimensionIndexSequence'] = my_fake_dim_idx_seq(2, sid_dim)
433-
div_seq = ((1, 1), (1, 2), (1, 3), (1, 4))
434-
frames = my_fake_frames(div_seq, sid_dim=sid_dim)
435-
fake_mf['NumberOfFrames'] = 4
436-
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
442+
assert_raises(AssertionError, getattr, dw, 'image_shape')
443+
# check 3D shape when StackID index is 0
444+
div_seq = ((1, 1), (1, 2), (1, 3), (1, 4))
445+
fake_mf.update(fake_shape_dependents(div_seq, sid_dim=0))
437446
assert_equal(MFW(fake_mf).image_shape, (32, 64, 4))
438447
# Check stack number matching when StackID index is 0
439448
div_seq = ((1, 1), (1, 2), (1, 3), (2, 4))
440-
frames = my_fake_frames(div_seq, sid_dim=sid_dim)
441-
fake_mf['NumberOfFrames'] = 4
442-
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
449+
fake_mf.update(fake_shape_dependents(div_seq, sid_dim=0))
443450
assert_raises(didw.WrapperError, getattr, MFW(fake_mf), 'image_shape')
444-
# Make some fake frame data for 4D when StackID index is 0
445-
fake_mf['DimensionIndexSequence'] = my_fake_dim_idx_seq(3, sid_dim)
451+
# Make some fake frame data for 4D when StackID index is 0
446452
div_seq = ((1, 1, 1), (1, 2, 1), (1, 1, 2), (1, 2, 2),
447453
(1, 1, 3), (1, 2, 3))
448-
frames = my_fake_frames(div_seq, sid_dim=sid_dim)
449-
fake_mf['NumberOfFrames'] = 6
450-
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
454+
fake_mf.update(fake_shape_dependents(div_seq, sid_dim=0))
451455
assert_equal(MFW(fake_mf).image_shape, (32, 64, 2, 3))
452456
# Check stack number matching for 4D when StackID index is 0
453457
div_seq = ((1, 1, 1), (1, 2, 1), (1, 1, 2), (1, 2, 2),
454458
(1, 1, 3), (2, 2, 3))
455-
frames = my_fake_frames(div_seq, sid_dim=sid_dim)
456-
fake_mf['NumberOfFrames'] = 6
457-
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
459+
fake_mf.update(fake_shape_dependents(div_seq, sid_dim=0))
458460
assert_raises(didw.WrapperError, getattr, MFW(fake_mf), 'image_shape')
459461
# Check indices can be non-contiguous when StackID index is 0
460462
div_seq = ((1, 1, 1), (1, 2, 1), (1, 1, 3), (1, 2, 3))
461-
frames = my_fake_frames(div_seq, sid_dim=sid_dim)
462-
fake_mf['NumberOfFrames'] = 4
463-
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
463+
fake_mf.update(fake_shape_dependents(div_seq, sid_dim=0))
464464
assert_equal(MFW(fake_mf).image_shape, (32, 64, 2, 2))
465465
# Check indices can include zero when StackID index is 0
466466
div_seq = ((1, 1, 0), (1, 2, 0), (1, 1, 3), (1, 2, 3))
467-
frames = my_fake_frames(div_seq, sid_dim=sid_dim)
468-
fake_mf['NumberOfFrames'] = 4
469-
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
467+
fake_mf.update(fake_shape_dependents(div_seq, sid_dim=0))
470468
assert_equal(MFW(fake_mf).image_shape, (32, 64, 2, 2))
471-
# check 3D shape when there is no StackID index
472-
fake_mf['DimensionIndexSequence'] = my_fake_dim_idx_seq(1, None)
469+
# check 3D shape when there is no StackID index
473470
div_seq = ((1,), (2,), (3,), (4,))
474471
sid_seq = (1, 1, 1, 1)
475-
frames = my_fake_frames(div_seq, sid_seq=sid_seq)
476-
fake_mf['NumberOfFrames'] = 4
477-
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
472+
fake_mf.update(fake_shape_dependents(div_seq, sid_seq=sid_seq))
478473
assert_equal(MFW(fake_mf).image_shape, (32, 64, 4))
479474
# check 3D stack number matching when there is no StackID index
480475
div_seq = ((1,), (2,), (3,), (4,))
481476
sid_seq = (1, 1, 1, 2)
482-
frames = my_fake_frames(div_seq, sid_seq=sid_seq)
483-
fake_mf['NumberOfFrames'] = 4
484-
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
477+
fake_mf.update(fake_shape_dependents(div_seq, sid_seq=sid_seq))
485478
assert_raises(didw.WrapperError, getattr, MFW(fake_mf), 'image_shape')
486-
# check 4D shape when there is no StackID index
487-
fake_mf['DimensionIndexSequence'] = my_fake_dim_idx_seq(2, None)
479+
# check 4D shape when there is no StackID index
488480
div_seq = ((1, 1), (2, 1), (1, 2), (2, 2), (1, 3), (2, 3))
489481
sid_seq = (1, 1, 1, 1, 1, 1)
490-
frames = my_fake_frames(div_seq, sid_seq=sid_seq)
491-
fake_mf['NumberOfFrames'] = 6
492-
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
482+
fake_mf.update(fake_shape_dependents(div_seq, sid_seq=sid_seq))
493483
assert_equal(MFW(fake_mf).image_shape, (32, 64, 2, 3))
494484
# check 4D stack number matching when there is no StackID index
495485
div_seq = ((1, 1), (2, 1), (1, 2), (2, 2), (1, 3), (2, 3))
496486
sid_seq = (1, 1, 1, 1, 1, 2)
497-
frames = my_fake_frames(div_seq, sid_seq=sid_seq)
498-
fake_mf['NumberOfFrames'] = 6
499-
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
487+
fake_mf.update(fake_shape_dependents(div_seq, sid_seq=sid_seq))
500488
assert_raises(didw.WrapperError, getattr, MFW(fake_mf), 'image_shape')
501-
# check 3D shape when StackID index is 1
502-
sid_dim = 1
503-
fake_mf['DimensionIndexSequence'] = my_fake_dim_idx_seq(2, sid_dim)
489+
# check 3D shape when StackID index is 1
504490
div_seq = ((1, 1), (2, 1), (3, 1), (4, 1))
505-
frames = my_fake_frames(div_seq, sid_dim=sid_dim)
506-
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
491+
fake_mf.update(fake_shape_dependents(div_seq, sid_dim=1))
507492
assert_equal(MFW(fake_mf).image_shape, (32, 64, 4))
508493
# Check stack number matching when StackID index is 1
509-
div_seq = ((1, 1), (2, 1), (3, 1), (4, 1))
510-
frames = my_fake_frames(div_seq, sid_dim=sid_dim)
511-
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
494+
div_seq = ((1, 1), (2, 1), (3, 2), (4, 1))
495+
fake_mf.update(fake_shape_dependents(div_seq, sid_dim=1))
512496
assert_raises(didw.WrapperError, getattr, MFW(fake_mf), 'image_shape')
513-
# Make some fake frame data for 4D when StackID index is 1
514-
fake_mf['DimensionIndexSequence'] = my_fake_dim_idx_seq(3, sid_dim)
497+
# Make some fake frame data for 4D when StackID index is 1
515498
div_seq = ((1, 1, 1), (2, 1, 1), (1, 1, 2), (2, 1, 2),
516499
(1, 1, 3), (2, 1, 3))
517-
frames = my_fake_frames(div_seq, sid_dim=sid_dim)
518-
fake_mf['NumberOfFrames'] = 6
519-
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
500+
fake_mf.update(fake_shape_dependents(div_seq, sid_dim=1))
520501
assert_equal(MFW(fake_mf).image_shape, (32, 64, 2, 3))
521502

522503
def test_iop(self):
@@ -633,12 +614,9 @@ def test_data_fake(self):
633614
assert_raises(didw.WrapperError, dw.get_data)
634615
# Make shape and indices
635616
fake_mf['Rows'] = 2
636-
fake_mf['Columns'] = 3
637-
fake_mf['NumberOfFrames'] = 4
638-
frames = fake_frames('FrameContentSequence',
639-
'DimensionIndexValues',
640-
((1, 1), (1, 2), (1, 3), (1, 4)))
641-
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
617+
fake_mf['Columns'] = 3
618+
dim_idxs = ((1, 1), (1, 2), (1, 3), (1, 4))
619+
fake_mf.update(fake_shape_dependents(dim_idxs, sid_dim=0))
642620
assert_equal(MFW(fake_mf).image_shape, (2, 3, 4))
643621
# Still fails - no data
644622
assert_raises(didw.WrapperError, dw.get_data)
@@ -653,11 +631,9 @@ def test_data_fake(self):
653631
fake_mf['RescaleSlope'] = 2.0
654632
fake_mf['RescaleIntercept'] = -1
655633
assert_array_equal(MFW(fake_mf).get_data(), data * 2.0 - 1)
656-
# Check slice sorting
657-
frames = fake_frames('FrameContentSequence',
658-
'DimensionIndexValues',
659-
((1, 4), (1, 2), (1, 3), (1, 1)))
660-
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
634+
# Check slice sorting
635+
dim_idxs = ((1, 4), (1, 2), (1, 3), (1, 1))
636+
fake_mf.update(fake_shape_dependents(dim_idxs, sid_dim=0))
661637
sorted_data = data[..., [3, 1, 2, 0]]
662638
fake_mf['pixel_array'] = np.rollaxis(sorted_data, 2)
663639
assert_array_equal(MFW(fake_mf).get_data(), data * 2.0 - 1)
@@ -679,11 +655,7 @@ def test_data_fake(self):
679655
[1, 2, 1, 2],
680656
[1, 3, 1, 2],
681657
[1, 1, 1, 2]]
682-
frames = fake_frames('FrameContentSequence',
683-
'DimensionIndexValues',
684-
dim_idxs)
685-
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
686-
fake_mf['NumberOfFrames'] = len(frames)
658+
fake_mf.update(fake_shape_dependents(dim_idxs, sid_dim=0))
687659
shape = (2, 3, 4, 2, 2)
688660
data = np.arange(np.prod(shape)).reshape(shape)
689661
sorted_data = data.reshape(shape[:2] + (-1,), order='F')

0 commit comments

Comments
 (0)