Skip to content

Commit 0da29a1

Browse files
committed
TST: Update test for MultiframeWrapper.image_shape
1 parent 62ed070 commit 0da29a1

File tree

1 file changed

+87
-16
lines changed

1 file changed

+87
-16
lines changed

nibabel/nicom/tests/test_dicomwrappers.py

100644100755
Lines changed: 87 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -417,44 +417,115 @@ def test_shape(self):
417417
# PerFrameFunctionalGroupsSequence does not match NumberOfFrames
418418
assert_raises(AssertionError, getattr, dw, 'image_shape')
419419
# Make some fake frame data for 3D
420-
def my_fake_frames(div_seq):
421-
return fake_frames('FrameContentSequence',
422-
'DimensionIndexValues',
423-
div_seq)
420+
def my_fake_frames(div_seq, sid_seq=None, sid_dim=None):
421+
frames = fake_frames('FrameContentSequence',
422+
'DimensionIndexValues',
423+
div_seq)
424+
if sid_seq is None:
425+
sid_seq = [div_elem[sid_dim] for div_elem in div_seq]
426+
for i, sid in enumerate(sid_seq):
427+
setattr(frames[i].FrameContentSequence[0], 'StackID', sid)
428+
# make fake DimensionIndexSequence
429+
def my_fake_dim_idx_seq(size, idx=None):
430+
class DimensionIndex(object):
431+
def __init__(self, dim_idx_ptr, func_grp_ptr):
432+
self.DimensionIndexPointer = dim_idx_ptr
433+
self.FunctionalGroupPointer = func_grp_ptr
434+
dim_idx_seq = [DimensionIndex((0x0, 0x0), (0x0, 0x0))] * size
435+
if idx is not None:
436+
dim_idx_seq[idx] = DimensionIndex((0x20, 0x9056),(0x20, 0x9111))
437+
return dim_idx_seq
438+
# check 3D shape when StackID index is 0
439+
sid_dim = 0
440+
fake_mf['DimensionIndexSequence'] = my_fake_dim_idx_seq(2, sid_dim)
424441
div_seq = ((1, 1), (1, 2), (1, 3), (1, 4))
425-
frames = my_fake_frames(div_seq)
442+
frames = my_fake_frames(div_seq, sid_dim=sid_dim)
443+
fake_mf['NumberOfFrames'] = 4
426444
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
427445
assert_equal(MFW(fake_mf).image_shape, (32, 64, 4))
428-
# Check stack number matching
446+
# Check stack number matching when StackID index is 0
429447
div_seq = ((1, 1), (1, 2), (1, 3), (2, 4))
430-
frames = my_fake_frames(div_seq)
448+
frames = my_fake_frames(div_seq, sid_dim=sid_dim)
449+
fake_mf['NumberOfFrames'] = 4
431450
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
432451
assert_raises(didw.WrapperError, getattr, MFW(fake_mf), 'image_shape')
433-
# Make some fake frame data for 4D
434-
fake_mf['NumberOfFrames'] = 6
452+
# Make some fake frame data for 4D when StackID index is 0
453+
fake_mf['DimensionIndexSequence'] = my_fake_dim_idx_seq(3, sid_dim)
435454
div_seq = ((1, 1, 1), (1, 2, 1), (1, 1, 2), (1, 2, 2),
436455
(1, 1, 3), (1, 2, 3))
437-
frames = my_fake_frames(div_seq)
456+
frames = my_fake_frames(div_seq, sid_dim=sid_dim)
457+
fake_mf['NumberOfFrames'] = 6
438458
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
439459
assert_equal(MFW(fake_mf).image_shape, (32, 64, 2, 3))
440-
# Check stack number matching for 4D
460+
# Check stack number matching for 4D when StackID index is 0
441461
div_seq = ((1, 1, 1), (1, 2, 1), (1, 1, 2), (1, 2, 2),
442462
(1, 1, 3), (2, 2, 3))
443-
frames = my_fake_frames(div_seq)
463+
frames = my_fake_frames(div_seq, sid_dim=sid_dim)
464+
fake_mf['NumberOfFrames'] = 6
444465
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
445466
assert_raises(didw.WrapperError, getattr, MFW(fake_mf), 'image_shape')
446-
# Check indices can be non-contiguous
467+
# Check indices can be non-contiguous when StackID index is 0
447468
div_seq = ((1, 1, 1), (1, 2, 1), (1, 1, 3), (1, 2, 3))
448-
frames = my_fake_frames(div_seq)
469+
frames = my_fake_frames(div_seq, sid_dim=sid_dim)
449470
fake_mf['NumberOfFrames'] = 4
450471
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
451472
assert_equal(MFW(fake_mf).image_shape, (32, 64, 2, 2))
452-
# Check indices can include zero
473+
# Check indices can include zero when StackID index is 0
453474
div_seq = ((1, 1, 0), (1, 2, 0), (1, 1, 3), (1, 2, 3))
454-
frames = my_fake_frames(div_seq)
475+
frames = my_fake_frames(div_seq, sid_dim=sid_dim)
455476
fake_mf['NumberOfFrames'] = 4
456477
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
457478
assert_equal(MFW(fake_mf).image_shape, (32, 64, 2, 2))
479+
# check 3D shape when there is no StackID index
480+
fake_mf['DimensionIndexSequence'] = my_fake_dim_idx_seq(1, None)
481+
div_seq = ((1,), (2,), (3,), (4,))
482+
sid_seq = (1, 1, 1, 1)
483+
frames = my_fake_frames(div_seq, sid_seq=sid_seq)
484+
fake_mf['NumberOfFrames'] = 4
485+
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
486+
assert_equal(MFW(fake_mf).image_shape, (32, 64, 4))
487+
# check 3D stack number matching when there is no StackID index
488+
div_seq = ((1,), (2,), (3,), (4,))
489+
sid_seq = (1, 1, 1, 2)
490+
frames = my_fake_frames(div_seq, sid_seq=sid_seq)
491+
fake_mf['NumberOfFrames'] = 4
492+
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
493+
assert_raises(didw.WrapperError, getattr, MFW(fake_mf), 'image_shape')
494+
# check 4D shape when there is no StackID index
495+
fake_mf['DimensionIndexSequence'] = my_fake_dim_idx_seq(2, None)
496+
div_seq = ((1, 1), (2, 1), (1, 2), (2, 2), (1, 3), (2, 3))
497+
sid_seq = (1, 1, 1, 1, 1, 1)
498+
frames = my_fake_frames(div_seq, sid_seq=sid_seq)
499+
fake_mf['NumberOfFrames'] = 6
500+
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
501+
assert_equal(MFW(fake_mf).image_shape, (32, 64, 2, 3))
502+
# check 4D stack number matching when there is no StackID index
503+
div_seq = ((1, 1), (2, 1), (1, 2), (2, 2), (1, 3), (2, 3))
504+
sid_seq = (1, 1, 1, 1, 1, 2)
505+
frames = my_fake_frames(div_seq, sid_seq=sid_seq)
506+
fake_mf['NumberOfFrames'] = 6
507+
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
508+
assert_raises(didw.WrapperError, getattr, MFW(fake_mf), 'image_shape')
509+
# check 3D shape when StackID index is 1
510+
sid_dim = 1
511+
fake_mf['DimensionIndexSequence'] = my_fake_dim_idx_seq(2, sid_dim)
512+
div_seq = ((1, 1), (2, 1), (3, 1), (4, 1))
513+
frames = my_fake_frames(div_seq, sid_dim=sid_dim)
514+
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
515+
assert_equal(MFW(fake_mf).image_shape, (32, 64, 4))
516+
# Check stack number matching when StackID index is 1
517+
div_seq = ((1, 1), (2, 1), (3, 1), (4, 1))
518+
frames = my_fake_frames(div_seq, sid_dim=sid_dim)
519+
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
520+
assert_raises(didw.WrapperError, getattr, MFW(fake_mf), 'image_shape')
521+
# Make some fake frame data for 4D when StackID index is 1
522+
fake_mf['DimensionIndexSequence'] = my_fake_dim_idx_seq(3, sid_dim)
523+
div_seq = ((1, 1, 1), (2, 1, 1), (1, 1, 2), (2, 1, 2),
524+
(1, 1, 3), (2, 1, 3))
525+
frames = my_fake_frames(div_seq, sid_dim=sid_dim)
526+
fake_mf['NumberOfFrames'] = 6
527+
fake_mf['PerFrameFunctionalGroupsSequence'] = frames
528+
assert_equal(MFW(fake_mf).image_shape, (32, 64, 2, 3))
458529

459530
def test_iop(self):
460531
# Test Image orient patient for multiframe

0 commit comments

Comments
 (0)