14
14
from __future__ import division
15
15
16
16
import operator
17
+ import warnings
17
18
18
19
import numpy as np
19
20
20
21
from . import csareader as csar
21
22
from .dwiparams import B2q , nearest_pos_semi_def , q2bg
22
23
from ..openers import ImageOpener
23
24
from ..onetime import setattr_on_read as one_time
24
- from ..pydicom_compat import tag_for_keyword
25
+ from ..pydicom_compat import tag_for_keyword , Sequence
25
26
26
27
27
28
class WrapperError (Exception ):
@@ -502,8 +503,32 @@ def image_shape(self):
502
503
rows , cols = self .get ('Rows' ), self .get ('Columns' )
503
504
if None in (rows , cols ):
504
505
raise WrapperError ("Rows and/or Columns are empty." )
506
+
505
507
# Check number of frames
508
+ first_frame = self .frames [0 ]
506
509
n_frames = self .get ('NumberOfFrames' )
510
+ # some Philips may have derived images appended
511
+ has_derived = False
512
+ if hasattr (first_frame , 'get' ) and first_frame .get ([0x18 , 0x9117 ]):
513
+ # DWI image may include derived isotropic, ADC or trace volume
514
+ try :
515
+ self .frames = Sequence (
516
+ frame for frame in self .frames if
517
+ frame .MRDiffusionSequence [0 ].DiffusionDirectionality
518
+ != 'ISOTROPIC'
519
+ )
520
+ except IndexError :
521
+ # Sequence tag is found but missing items!
522
+ raise WrapperError ("Diffusion file missing information" )
523
+ except AttributeError :
524
+ # DiffusionDirectionality tag is not required
525
+ pass
526
+ else :
527
+ if n_frames != len (self .frames ):
528
+ warnings .warn ("Derived images found and removed" )
529
+ n_frames = len (self .frames )
530
+ has_derived = True
531
+
507
532
assert len (self .frames ) == n_frames
508
533
frame_indices = np .array (
509
534
[frame .FrameContentSequence [0 ].DimensionIndexValues
@@ -522,6 +547,15 @@ def image_shape(self):
522
547
if stackid_tag in dim_seq :
523
548
stackid_dim_idx = dim_seq .index (stackid_tag )
524
549
frame_indices = np .delete (frame_indices , stackid_dim_idx , axis = 1 )
550
+ dim_seq .pop (stackid_dim_idx )
551
+ if has_derived :
552
+ # derived volume is included
553
+ derived_tag = tag_for_keyword ("DiffusionBValue" )
554
+ if derived_tag not in dim_seq :
555
+ raise WrapperError ("Missing information, cannot remove indices "
556
+ "with confidence." )
557
+ derived_dim_idx = dim_seq .index (derived_tag )
558
+ frame_indices = np .delete (frame_indices , derived_dim_idx , axis = 1 )
525
559
# account for the 2 additional dimensions (row and column) not included
526
560
# in the indices
527
561
n_dim = frame_indices .shape [1 ] + 2
0 commit comments