@@ -470,10 +470,10 @@ def __init__(self, dcm_data):
470
470
# Try to determine slice order and minimal image position patient
471
471
self ._frame_slc_ord = self ._ipp = None
472
472
try :
473
- frame_ipps = [self . shared . PlanePositionSequence [0 ].ImagePositionPatient ]
473
+ frame_ipps = [f . PlanePositionSequence [0 ].ImagePositionPatient for f in self . frames ]
474
474
except AttributeError :
475
475
try :
476
- frame_ipps = [f . PlanePositionSequence [0 ].ImagePositionPatient for f in self . frames ]
476
+ frame_ipps = [self . shared . PlanePositionSequence [0 ].ImagePositionPatient ]
477
477
except AttributeError :
478
478
frame_ipps = None
479
479
if frame_ipps is not None and all (ipp is not None for ipp in frame_ipps ):
@@ -575,33 +575,38 @@ def image_shape(self):
575
575
raise WrapperError ('Missing information, cannot remove indices with confidence.' )
576
576
derived_dim_idx = dim_seq .index (derived_tag )
577
577
frame_indices = np .delete (frame_indices , derived_dim_idx , axis = 1 )
578
+ dim_seq .pop (derived_dim_idx )
578
579
# Determine the shape and which indices to use
579
580
shape = [rows , cols ]
580
581
curr_parts = n_frames
581
582
frames_per_part = 1
582
583
del_indices = {}
584
+ stackpos_tag = pydicom .datadict .tag_for_keyword ('InStackPositionNumber' )
585
+ slice_dim_idx = dim_seq .index (stackpos_tag )
583
586
for row_idx , row in enumerate (frame_indices .T ):
584
587
unique = np .unique (row )
585
588
count = len (unique )
586
- if count == 1 or curr_parts == 1 :
589
+ if curr_parts == 1 or ( count == 1 and row_idx != slice_dim_idx ) :
587
590
del_indices [row_idx ] = count
588
591
continue
589
592
# Replace slice indices with order determined from slice positions along normal
590
- if len (shape ) == 2 :
593
+ if row_idx == slice_dim_idx :
594
+ if len (shape ) > 2 :
595
+ raise WrapperError ('Non-singular index precedes the slice index' )
591
596
row = self ._frame_slc_ord
592
597
frame_indices .T [row_idx , :] = row
593
598
unique = np .unique (row )
594
599
if len (unique ) != count :
595
600
raise WrapperError ("Number of slice indices and positions don't match" )
596
601
new_parts , leftover = divmod (curr_parts , count )
597
602
allowed_val_counts = [new_parts * frames_per_part ]
598
- if len ( shape ) > 2 :
603
+ if row_idx != slice_dim_idx :
599
604
# Except for the slice dim, having a unique value for each frame is valid
600
605
allowed_val_counts .append (n_frames )
601
606
if leftover != 0 or any (
602
607
np .count_nonzero (row == val ) not in allowed_val_counts for val in unique
603
608
):
604
- if len ( shape ) == 2 :
609
+ if row_idx == slice_dim_idx :
605
610
raise WrapperError ('Missing slices from multiframe' )
606
611
del_indices [row_idx ] = count
607
612
continue
0 commit comments