@@ -405,7 +405,7 @@ def get_frame_order(mlist):
405
405
>>> ecat_file = os.path.join(nibabel_dir,'tests','data','tinypet.v')
406
406
>>> img = ecat.load(ecat_file)
407
407
>>> mlist = img.get_mlist()
408
- >>> mlist. get_frame_order()
408
+ >>> get_frame_order(mlist )
409
409
{0: [0, 16842758]}
410
410
"""
411
411
ids = mlist [:, 0 ].copy ()
@@ -449,7 +449,7 @@ def get_series_framenumbers(mlist):
449
449
>>> ecat_file = os.path.join(nibabel_dir,'tests','data','tinypet.v')
450
450
>>> img = ecat.load(ecat_file)
451
451
>>> mlist = img.get_mlist()
452
- >>> mlist. get_series_framenumbers()
452
+ >>> get_series_framenumbers(mlist )
453
453
{0: 1}
454
454
"""
455
455
nframes = len (mlist )
@@ -502,46 +502,6 @@ def read_subheaders(fileobj, mlist, endianness):
502
502
return subheaders
503
503
504
504
505
- class EcatMlist (object ):
506
- # Can we do without this object, just using the functions?
507
-
508
- def __init__ (self ,fileobj , hdr ):
509
- """ gets list of frames and subheaders in pet file
510
-
511
- Container for Ecat mlist
512
-
513
- Data for mlist is numpy array shape (frames, 4)
514
-
515
- Columns are:
516
-
517
- * 0 - Matrix identifier.
518
- * 1 - subheader block number
519
- * 2 - Last block number of matrix data block.
520
- * 3 - Matrix status:
521
- * 1 - exists - rw
522
- * 2 - exists - ro
523
- * 3 - matrix deleted
524
-
525
- A block above is 512 bytes in the image data file
526
-
527
- Parameters
528
- -----------
529
- fileobj : file-like
530
- ECAT file <filename>.v fileholder or file object with read, seek
531
- methods
532
- """
533
- self .hdr = hdr
534
- self ._mlist = np .zeros ((hdr ['num_frames' ], 4 ), dtype = 'uint32' )
535
- mlist_data = read_mlist (fileobj , hdr .endianness )
536
- self ._mlist [:len (mlist_data )] = mlist_data
537
-
538
- def get_frame_order (self ):
539
- return get_frame_order (self ._mlist )
540
-
541
- def get_series_framenumbers (self ):
542
- return get_series_framenumbers (self ._mlist )
543
-
544
-
545
505
class EcatSubHeader (object ):
546
506
547
507
_subhdrdtype = subhdr_dtype
@@ -554,17 +514,17 @@ def __init__(self, hdr, mlist, fileobj):
554
514
Parameters
555
515
-----------
556
516
hdr : EcatHeader
557
-
558
- mlist : EcatMlist
559
-
517
+ ECAT main header
518
+ mlist : array shape (N, 4)
519
+ Matrix list
560
520
fileobj : ECAT file <filename>.v fileholder or file object
561
521
with read, seek methods
562
522
"""
563
523
self ._header = hdr
564
524
self .endianness = hdr .endianness
565
525
self ._mlist = mlist
566
526
self .fileobj = fileobj
567
- self .subheaders = read_subheaders (fileobj , mlist . _mlist , hdr .endianness )
527
+ self .subheaders = read_subheaders (fileobj , mlist , hdr .endianness )
568
528
569
529
def get_shape (self , frame = 0 ):
570
530
""" returns shape of given frame"""
@@ -576,11 +536,9 @@ def get_shape(self, frame=0):
576
536
577
537
def get_nframes (self ):
578
538
"""returns number of frames"""
579
- mlist = self ._mlist
580
- framed = mlist .get_frame_order ()
539
+ framed = get_frame_order (self ._mlist )
581
540
return len (framed )
582
541
583
-
584
542
def _check_affines (self ):
585
543
"""checks if all affines are equal across frames"""
586
544
nframes = self .get_nframes ()
@@ -625,9 +583,7 @@ def _get_data_dtype(self, frame):
625
583
return self ._data_type_codes .dtype [dtcode ]
626
584
627
585
def _get_frame_offset (self , frame = 0 ):
628
- mlist = self ._mlist ._mlist
629
- offset = (mlist [frame ][1 ]) * BLOCK_SIZE
630
- return int (offset )
586
+ return int (self ._mlist [frame ][1 ] * BLOCK_SIZE )
631
587
632
588
def _get_oriented_data (self , raw_data , orientation = None ):
633
589
'''
@@ -725,7 +681,7 @@ def __array__(self):
725
681
(``__getitem__``) below, or ``subheader.data_from_fileobj(frame)``
726
682
'''
727
683
data = np .empty (self .shape )
728
- frame_mapping = self ._subheader ._mlist . get_frame_order ( )
684
+ frame_mapping = get_frame_order ( self ._subheader ._mlist )
729
685
for i in sorted (frame_mapping ):
730
686
data [:,:,:,i ] = self ._subheader .data_from_fileobj (frame_mapping [i ][0 ])
731
687
return data
@@ -737,7 +693,7 @@ def __getitem__(self, sliceobj):
737
693
# Indices into sliceobj referring to image axes
738
694
ax_inds = [i for i , obj in enumerate (sliceobj ) if not obj is None ]
739
695
assert len (ax_inds ) == len (self .shape )
740
- frame_mapping = self ._subheader ._mlist . get_frame_order ( )
696
+ frame_mapping = get_frame_order ( self ._subheader ._mlist )
741
697
# Analyze index for 4th axis
742
698
slice3 = sliceobj [ax_inds [3 ]]
743
699
# We will load volume by volume. Make slicer into volume by dropping
@@ -769,7 +725,6 @@ class EcatImage(SpatialImage):
769
725
_header = EcatHeader
770
726
header_class = _header
771
727
_subheader = EcatSubHeader
772
- _mlist = EcatMlist
773
728
files_types = (('image' , '.v' ), ('header' , '.v' ))
774
729
775
730
ImageArrayProxy = EcatImageArrayProxy
@@ -795,8 +750,8 @@ def __init__(self, dataobj, affine, header,
795
750
meta data for this image format
796
751
subheader : None or subheader instance
797
752
meta data for each sub-image for frame in the image
798
- mlist : None or mlist instance
799
- meta data with array giving offset and order of data in file
753
+ mlist : None or array
754
+ Matrix list array giving offset and order of data in file
800
755
extra : None or mapping, optional
801
756
metadata associated with this image that cannot be
802
757
stored in header or subheader
@@ -871,7 +826,8 @@ def shape(self):
871
826
return (x , y , z , nframes )
872
827
873
828
def get_mlist (self ):
874
- """ get access to the mlist """
829
+ """ get access to the mlist
830
+ """
875
831
return self ._mlist
876
832
877
833
def get_subheaders (self ):
@@ -904,29 +860,29 @@ def from_file_map(klass, file_map):
904
860
header = klass ._header .from_fileobj (hdr_fid )
905
861
hdr_copy = header .copy ()
906
862
### LOAD MLIST
907
- mlist = klass ._mlist (hdr_fid , hdr_copy )
863
+ mlist = np .zeros ((header ['num_frames' ], 4 ), dtype = np .int32 )
864
+ mlist_data = read_mlist (hdr_fid , hdr_copy .endianness )
865
+ mlist [:len (mlist_data )] = mlist_data
908
866
### LOAD SUBHEADERS
909
- subheaders = klass ._subheader (hdr_copy ,
910
- mlist ,
911
- hdr_fid )
867
+ subheaders = klass ._subheader (hdr_copy , mlist , hdr_fid )
912
868
### LOAD DATA
913
869
## Class level ImageArrayProxy
914
870
data = klass .ImageArrayProxy (subheaders )
915
-
916
871
## Get affine
917
872
if not subheaders ._check_affines ():
918
- warnings .warn ('Affines different across frames, loading affine from FIRST frame' ,
919
- UserWarning )
873
+ warnings .warn ('Affines different across frames, loading affine '
874
+ 'from FIRST frame' , UserWarning )
920
875
aff = subheaders .get_frame_affine ()
921
- img = klass (data , aff , header , subheaders , mlist , extra = None , file_map = file_map )
876
+ img = klass (data , aff , header , subheaders , mlist ,
877
+ extra = None , file_map = file_map )
922
878
return img
923
879
924
880
def _get_empty_dir (self ):
925
881
'''
926
882
Get empty directory entry of the form
927
883
[numAvail, nextDir, previousDir, numUsed]
928
884
'''
929
- return np .array ([31 , 2 , 0 , 0 ], dtype = np .uint32 )
885
+ return np .array ([31 , 2 , 0 , 0 ], dtype = np .int32 )
930
886
931
887
def _write_data (self , data , stream , pos , dtype = None , endianness = None ):
932
888
'''
@@ -962,9 +918,11 @@ def to_file_map(self, file_map=None):
962
918
if file_map is None :
963
919
file_map = self .file_map
964
920
921
+ # It appears to be necessary to load the data before saving even if the
922
+ # data itself is not used.
965
923
data = self .get_data ()
966
924
hdr = self .get_header ()
967
- mlist = self .get_mlist (). _mlist
925
+ mlist = self ._mlist
968
926
subheaders = self .get_subheaders ()
969
927
dir_pos = 512
970
928
entry_pos = dir_pos + 16 #528
@@ -999,8 +957,7 @@ def to_file_map(self, file_map=None):
999
957
self ._write_data (image , imgf , pos + 2 , endianness = '>' )
1000
958
1001
959
#Move to dictionnary offset and write dictionnary entry
1002
- self ._write_data (mlist [index ], imgf , entry_pos ,
1003
- np .uint32 , endianness = '>' )
960
+ self ._write_data (mlist [index ], imgf , entry_pos , endianness = '>' )
1004
961
1005
962
entry_pos = entry_pos + 16
1006
963
@@ -1022,7 +979,7 @@ def to_file_map(self, file_map=None):
1022
979
#Fill directory with empty data until directory is full
1023
980
while current_dir [0 ] > 0 :
1024
981
entry_pos = dir_pos + 16 + (16 * current_dir [3 ])
1025
- self ._write_data (np .array ([ 0 , 0 , 0 , 0 ] ), imgf , entry_pos , np . uint32 )
982
+ self ._write_data (np .zeros ( 4 , dtype = np . int32 ), imgf , entry_pos )
1026
983
current_dir [0 ] = current_dir [0 ] - 1
1027
984
current_dir [3 ] = current_dir [3 ] + 1
1028
985
0 commit comments