5
5
import gzip
6
6
from hashlib import sha1
7
7
from decimal import Decimal
8
- from copy import copy
8
+ from copy import copy
9
+ from collections import namedtuple
9
10
10
11
import numpy as np
11
12
@@ -379,8 +380,40 @@ class Fake(object):
379
380
setattr (fake_frame , seq_name , [fake_element ])
380
381
frames .append (fake_frame )
381
382
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
+
384
417
class TestMultiFrameWrapper (TestCase ):
385
418
# Test MultiframeWrapper
386
419
MINIMAL_MF = {
@@ -406,117 +439,65 @@ def test_shape(self):
406
439
assert_raises (AssertionError , getattr , dw , 'image_shape' )
407
440
fake_mf ['NumberOfFrames' ] = 4
408
441
# 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 ))
437
446
assert_equal (MFW (fake_mf ).image_shape , (32 , 64 , 4 ))
438
447
# Check stack number matching when StackID index is 0
439
448
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 ))
443
450
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
446
452
div_seq = ((1 , 1 , 1 ), (1 , 2 , 1 ), (1 , 1 , 2 ), (1 , 2 , 2 ),
447
453
(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 ))
451
455
assert_equal (MFW (fake_mf ).image_shape , (32 , 64 , 2 , 3 ))
452
456
# Check stack number matching for 4D when StackID index is 0
453
457
div_seq = ((1 , 1 , 1 ), (1 , 2 , 1 ), (1 , 1 , 2 ), (1 , 2 , 2 ),
454
458
(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 ))
458
460
assert_raises (didw .WrapperError , getattr , MFW (fake_mf ), 'image_shape' )
459
461
# Check indices can be non-contiguous when StackID index is 0
460
462
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 ))
464
464
assert_equal (MFW (fake_mf ).image_shape , (32 , 64 , 2 , 2 ))
465
465
# Check indices can include zero when StackID index is 0
466
466
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 ))
470
468
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
473
470
div_seq = ((1 ,), (2 ,), (3 ,), (4 ,))
474
471
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 ))
478
473
assert_equal (MFW (fake_mf ).image_shape , (32 , 64 , 4 ))
479
474
# check 3D stack number matching when there is no StackID index
480
475
div_seq = ((1 ,), (2 ,), (3 ,), (4 ,))
481
476
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 ))
485
478
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
488
480
div_seq = ((1 , 1 ), (2 , 1 ), (1 , 2 ), (2 , 2 ), (1 , 3 ), (2 , 3 ))
489
481
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 ))
493
483
assert_equal (MFW (fake_mf ).image_shape , (32 , 64 , 2 , 3 ))
494
484
# check 4D stack number matching when there is no StackID index
495
485
div_seq = ((1 , 1 ), (2 , 1 ), (1 , 2 ), (2 , 2 ), (1 , 3 ), (2 , 3 ))
496
486
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 ))
500
488
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
504
490
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 ))
507
492
assert_equal (MFW (fake_mf ).image_shape , (32 , 64 , 4 ))
508
493
# 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 ))
512
496
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
515
498
div_seq = ((1 , 1 , 1 ), (2 , 1 , 1 ), (1 , 1 , 2 ), (2 , 1 , 2 ),
516
499
(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 ))
520
501
assert_equal (MFW (fake_mf ).image_shape , (32 , 64 , 2 , 3 ))
521
502
522
503
def test_iop (self ):
@@ -633,12 +614,9 @@ def test_data_fake(self):
633
614
assert_raises (didw .WrapperError , dw .get_data )
634
615
# Make shape and indices
635
616
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 ))
642
620
assert_equal (MFW (fake_mf ).image_shape , (2 , 3 , 4 ))
643
621
# Still fails - no data
644
622
assert_raises (didw .WrapperError , dw .get_data )
@@ -653,11 +631,9 @@ def test_data_fake(self):
653
631
fake_mf ['RescaleSlope' ] = 2.0
654
632
fake_mf ['RescaleIntercept' ] = - 1
655
633
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 ))
661
637
sorted_data = data [..., [3 , 1 , 2 , 0 ]]
662
638
fake_mf ['pixel_array' ] = np .rollaxis (sorted_data , 2 )
663
639
assert_array_equal (MFW (fake_mf ).get_data (), data * 2.0 - 1 )
@@ -679,11 +655,7 @@ def test_data_fake(self):
679
655
[1 , 2 , 1 , 2 ],
680
656
[1 , 3 , 1 , 2 ],
681
657
[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 ))
687
659
shape = (2 , 3 , 4 , 2 , 2 )
688
660
data = np .arange (np .prod (shape )).reshape (shape )
689
661
sorted_data = data .reshape (shape [:2 ] + (- 1 ,), order = 'F' )
0 commit comments