<s> """<STR_LIT>""" <EOL> import warnings <EOL> from numbers import Integral <EOL> import numpy as np <EOL> from . volumeutils import ( native_code , swapped_code , make_dt_codes , <EOL> array_from_file ) <EOL> from . spatialimages import SpatialImage <EOL> from . arraywriters import make_array_writer <EOL> from . wrapstruct import WrapStruct <EOL> from . fileslice import canonical_slicers , predict_shape , slice2outax <EOL> BLOCK_SIZE = <NUM_LIT> <EOL> main_header_dtd = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , np . uint32 ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . uint32 ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . uint32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ] <EOL> hdr_dtype = np . dtype ( main_header_dtd ) <EOL> subheader_dtd = [ <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . int16 ) , <EOL> ( '<STR_LIT>' , np . int16 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . uint32 ) , <EOL> ( '<STR_LIT>' , np . uint32 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . uint32 ) , <EOL> ( '<STR_LIT>' , np . uint32 ) , <EOL> ( '<STR_LIT>' , np . uint32 ) , <EOL> ( '<STR_LIT>' , np . uint32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , np . uint16 ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> subhdr_dtype = np . dtype ( subheader_dtd ) <EOL> _dtdefs = ( <EOL> ( <NUM_LIT:1> , '<STR_LIT>' , np . uint8 ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' , np . int16 ) , <EOL> ( <NUM_LIT:3> , '<STR_LIT>' , np . int32 ) , <EOL> ( <NUM_LIT:4> , '<STR_LIT>' , np . float32 ) , <EOL> ( <NUM_LIT:5> , '<STR_LIT>' , np . float32 ) , <EOL> ( <NUM_LIT:6> , '<STR_LIT>' , np . int16 ) , <EOL> ( <NUM_LIT:7> , '<STR_LIT>' , np . int32 ) ) <EOL> data_type_codes = make_dt_codes ( _dtdefs ) <EOL> ft_defs = ( <EOL> ( <NUM_LIT:0> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:1> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:3> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:4> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:5> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:6> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:7> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:8> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:9> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:10> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:11> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:12> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT> , '<STR_LIT>' ) ) <EOL> file_type_codes = dict ( ft_defs ) <EOL> patient_orient_defs = ( <EOL> ( <NUM_LIT:0> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:1> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:3> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:4> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:5> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:6> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:7> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:8> , '<STR_LIT>' ) ) <EOL> patient_orient_codes = dict ( patient_orient_defs ) <EOL> patient_orient_radiological = [ <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:6> ] <EOL> patient_orient_neurological = [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:7> ] <EOL> class EcatHeader ( WrapStruct ) : <EOL> """<STR_LIT>""" <EOL> template_dtype = hdr_dtype <EOL> _ft_codes = file_type_codes <EOL> _patient_orient_codes = patient_orient_codes <EOL> def __init__ ( self , <EOL> binaryblock = None , <EOL> endianness = None , <EOL> check = True ) : <EOL> """<STR_LIT>""" <EOL> super ( EcatHeader , self ) . __init__ ( binaryblock , endianness , check ) <EOL> @ classmethod <EOL> def guessed_endian ( klass , hdr ) : <EOL> """<STR_LIT>""" <EOL> if not hdr [ '<STR_LIT>' ] == <NUM_LIT> : <EOL> return swapped_code <EOL> else : <EOL> return native_code <EOL> @ classmethod <EOL> def default_structarr ( klass , endianness = None ) : <EOL> '''<STR_LIT>''' <EOL> hdr_data = super ( EcatHeader , klass ) . default_structarr ( endianness ) <EOL> hdr_data [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> hdr_data [ '<STR_LIT>' ] = <NUM_LIT> <EOL> hdr_data [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> hdr_data [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> hdr_data [ '<STR_LIT>' ] = <NUM_LIT:1.0> <EOL> return hdr_data <EOL> def get_data_dtype ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def get_patient_orient ( self ) : <EOL> """<STR_LIT>""" <EOL> code = self . _structarr [ '<STR_LIT>' ] . item ( ) <EOL> if code not in self . _patient_orient_codes : <EOL> raise KeyError ( '<STR_LIT>' % code ) <EOL> return self . _patient_orient_codes [ code ] <EOL> def get_filetype ( self ) : <EOL> """<STR_LIT>""" <EOL> code = self . _structarr [ '<STR_LIT>' ] . item ( ) <EOL> if code not in self . _ft_codes : <EOL> raise KeyError ( '<STR_LIT>' % code ) <EOL> return self . _ft_codes [ code ] <EOL> @ classmethod <EOL> def _get_checks ( klass ) : <EOL> '''<STR_LIT>''' <EOL> return ( ) <EOL> def read_mlist ( fileobj , endianness ) : <EOL> """<STR_LIT>""" <EOL> dt = np . dtype ( np . int32 ) <EOL> if endianness is not native_code : <EOL> dt = dt . newbyteorder ( endianness ) <EOL> mlists = [ ] <EOL> mlist_index = <NUM_LIT:0> <EOL> mlist_block_no = <NUM_LIT:2> <EOL> while True : <EOL> fileobj . seek ( ( mlist_block_no - <NUM_LIT:1> ) * BLOCK_SIZE ) <EOL> dat = fileobj . read ( BLOCK_SIZE ) <EOL> rows = np . ndarray ( shape = ( <NUM_LIT:32> , <NUM_LIT:4> ) , dtype = dt , buffer = dat ) <EOL> n_unused , mlist_block_no , _ , n_rows = rows [ <NUM_LIT:0> ] <EOL> if not ( n_unused + n_rows ) == <NUM_LIT> : <EOL> mlist = [ ] <EOL> return mlist <EOL> mlists . append ( rows [ <NUM_LIT:1> : n_rows + <NUM_LIT:1> ] ) <EOL> mlist_index += n_rows <EOL> if mlist_block_no <= <NUM_LIT:2> : <EOL> break <EOL> return np . row_stack ( mlists ) <EOL> def get_frame_order ( mlist ) : <EOL> """<STR_LIT>""" <EOL> ids = mlist [ : , <NUM_LIT:0> ] . copy ( ) <EOL> n_valid = np . sum ( ids > <NUM_LIT:0> ) <EOL> ids [ ids <= <NUM_LIT:0> ] = ids . max ( ) + <NUM_LIT:1> <EOL> valid_order = np . argsort ( ids ) <EOL> if not all ( valid_order == sorted ( valid_order ) ) : <EOL> warnings . warn_explicit ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % valid_order , <EOL> UserWarning , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> id_dict = { } <EOL> for i in range ( n_valid ) : <EOL> id_dict [ i ] = [ valid_order [ i ] , ids [ valid_order [ i ] ] ] <EOL> return id_dict <EOL> def get_series_framenumbers ( mlist ) : <EOL> """<STR_LIT>""" <EOL> nframes = len ( mlist ) <EOL> frames_order = get_frame_order ( mlist ) <EOL> mlist_nframes = len ( frames_order ) <EOL> trueframenumbers = np . arange ( nframes - mlist_nframes , nframes ) <EOL> frame_dict = { } <EOL> try : <EOL> for frame_stored , ( true_order , _ ) in frames_order . items ( ) : <EOL> frame_dict [ frame_stored ] = trueframenumbers [ true_order ] + <NUM_LIT:1> <EOL> return frame_dict <EOL> except : <EOL> raise IOError ( '<STR_LIT>' ) <EOL> def read_subheaders ( fileobj , mlist , endianness ) : <EOL> """<STR_LIT>""" <EOL> subheaders = [ ] <EOL> dt = subhdr_dtype <EOL> if endianness is not native_code : <EOL> dt = dt . newbyteorder ( endianness ) <EOL> for mat_id , sh_blkno , sh_last_blkno , mat_stat in mlist : <EOL> if sh_blkno == <NUM_LIT:0> : <EOL> break <EOL> offset = ( sh_blkno - <NUM_LIT:1> ) * BLOCK_SIZE <EOL> fileobj . seek ( offset ) <EOL> tmpdat = fileobj . read ( BLOCK_SIZE ) <EOL> sh = np . ndarray ( shape = ( ) , dtype = dt , buffer = tmpdat ) <EOL> subheaders . append ( sh ) <EOL> return subheaders <EOL> class EcatSubHeader ( object ) : <EOL> _subhdrdtype = subhdr_dtype <EOL> _data_type_codes = data_type_codes <EOL> def __init__ ( self , hdr , mlist , fileobj ) : <EOL> """<STR_LIT>""" <EOL> self . _header = hdr <EOL> self . endianness = hdr . endianness <EOL> self . _mlist = mlist <EOL> self . fileobj = fileobj <EOL> self . subheaders = read_subheaders ( fileobj , mlist , hdr . endianness ) <EOL> def get_shape ( self , frame = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> subhdr = self . subheaders [ frame ] <EOL> x = subhdr [ '<STR_LIT>' ] . item ( ) <EOL> y = subhdr [ '<STR_LIT>' ] . item ( ) <EOL> z = subhdr [ '<STR_LIT>' ] . item ( ) <EOL> return ( x , y , z ) <EOL> def get_nframes ( self ) : <EOL> """<STR_LIT>""" <EOL> framed = get_frame_order ( self . _mlist ) <EOL> return len ( framed ) <EOL> def _check_affines ( self ) : <EOL> """<STR_LIT>""" <EOL> nframes = self . get_nframes ( ) <EOL> if nframes == <NUM_LIT:1> : <EOL> return True <EOL> affs = [ self . get_frame_affine ( i ) for i in range ( nframes ) ] <EOL> if affs : <EOL> i = iter ( affs ) <EOL> first = i . next ( ) <EOL> for item in i : <EOL> if not np . all ( first == item ) : <EOL> return False <EOL> return True <EOL> def get_frame_affine ( self , frame = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> subhdr = self . subheaders [ frame ] <EOL> x_off = subhdr [ '<STR_LIT>' ] <EOL> y_off = subhdr [ '<STR_LIT>' ] <EOL> z_off = subhdr [ '<STR_LIT>' ] <EOL> zooms = self . get_zooms ( frame = frame ) <EOL> dims = self . get_shape ( frame ) <EOL> origin_offset = ( np . array ( dims ) - <NUM_LIT:1> ) / <NUM_LIT> <EOL> aff = np . diag ( zooms ) <EOL> aff [ : <NUM_LIT:3> , - <NUM_LIT:1> ] = - origin_offset * zooms [ : - <NUM_LIT:1> ] + np . array ( [ x_off , y_off , <EOL> z_off ] ) <EOL> return aff <EOL> def get_zooms ( self , frame = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> subhdr = self . subheaders [ frame ] <EOL> x_zoom = subhdr [ '<STR_LIT>' ] * <NUM_LIT:10> <EOL> y_zoom = subhdr [ '<STR_LIT>' ] * <NUM_LIT:10> <EOL> z_zoom = subhdr [ '<STR_LIT>' ] * <NUM_LIT:10> <EOL> return ( x_zoom , y_zoom , z_zoom , <NUM_LIT:1> ) <EOL> def _get_data_dtype ( self , frame ) : <EOL> dtcode = self . subheaders [ frame ] [ '<STR_LIT>' ] . item ( ) <EOL> return self . _data_type_codes . dtype [ dtcode ] <EOL> def _get_frame_offset ( self , frame = <NUM_LIT:0> ) : <EOL> return int ( self . _mlist [ frame ] [ <NUM_LIT:1> ] * BLOCK_SIZE ) <EOL> def _get_oriented_data ( self , raw_data , orientation = None ) : <EOL> '''<STR_LIT>''' <EOL> if orientation is None : <EOL> orientation = self . _header [ '<STR_LIT>' ] <EOL> elif orientation == '<STR_LIT>' : <EOL> orientation = patient_orient_neurological [ <NUM_LIT:0> ] <EOL> elif orientation == '<STR_LIT>' : <EOL> orientation = patient_orient_radiological [ <NUM_LIT:0> ] <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if orientation in patient_orient_neurological : <EOL> raw_data = raw_data [ : : - <NUM_LIT:1> , : : - <NUM_LIT:1> , : : - <NUM_LIT:1> ] <EOL> elif orientation in patient_orient_radiological : <EOL> raw_data = raw_data [ : : , : : - <NUM_LIT:1> , : : - <NUM_LIT:1> ] <EOL> return raw_data <EOL> def raw_data_from_fileobj ( self , frame = <NUM_LIT:0> , orientation = None ) : <EOL> '''<STR_LIT>''' <EOL> dtype = self . _get_data_dtype ( frame ) <EOL> if self . _header . endianness is not native_code : <EOL> dtype = dtype . newbyteorder ( self . _header . endianness ) <EOL> shape = self . get_shape ( frame ) <EOL> offset = self . _get_frame_offset ( frame ) <EOL> fid_obj = self . fileobj <EOL> raw_data = array_from_file ( shape , dtype , fid_obj , offset = offset ) <EOL> raw_data = self . _get_oriented_data ( raw_data , orientation ) <EOL> return raw_data <EOL> def data_from_fileobj ( self , frame = <NUM_LIT:0> , orientation = None ) : <EOL> '''<STR_LIT>''' <EOL> header = self . _header <EOL> subhdr = self . subheaders [ frame ] <EOL> raw_data = self . raw_data_from_fileobj ( frame , orientation ) <EOL> data = raw_data * np . asscalar ( header [ '<STR_LIT>' ] ) <EOL> data = data * np . asscalar ( subhdr [ '<STR_LIT>' ] ) <EOL> return data <EOL> class EcatImageArrayProxy ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , subheader ) : <EOL> self . _subheader = subheader <EOL> self . _data = None <EOL> x , y , z = subheader . get_shape ( ) <EOL> nframes = subheader . get_nframes ( ) <EOL> self . _shape = ( x , y , z , nframes ) <EOL> @ property <EOL> def shape ( self ) : <EOL> return self . _shape <EOL> @ property <EOL> def is_proxy ( self ) : <EOL> return True <EOL> def __array__ ( self ) : <EOL> '''<STR_LIT>''' <EOL> data = np . empty ( self . shape ) <EOL> frame_mapping = get_frame_order ( self . _subheader . _mlist ) <EOL> for i in sorted ( frame_mapping ) : <EOL> data [ : , : , : , i ] = self . _subheader . data_from_fileobj ( <EOL> frame_mapping [ i ] [ <NUM_LIT:0> ] ) <EOL> return data <EOL> def __getitem__ ( self , sliceobj ) : <EOL> """<STR_LIT>""" <EOL> sliceobj = canonical_slicers ( sliceobj , self . shape ) <EOL> ax_inds = [ i for i , obj in enumerate ( sliceobj ) if obj is not None ] <EOL> assert len ( ax_inds ) == len ( self . shape ) <EOL> frame_mapping = get_frame_order ( self . _subheader . _mlist ) <EOL> slice3 = sliceobj [ ax_inds [ <NUM_LIT:3> ] ] <EOL> in_slicer = sliceobj [ : ax_inds [ <NUM_LIT:3> ] ] + sliceobj [ ax_inds [ <NUM_LIT:3> ] + <NUM_LIT:1> : ] <EOL> if isinstance ( slice3 , Integral ) : <EOL> data = self . _subheader . data_from_fileobj ( frame_mapping [ slice3 ] [ <NUM_LIT:0> ] ) <EOL> return data [ in_slicer ] <EOL> out_shape = predict_shape ( sliceobj , self . shape ) <EOL> out_data = np . empty ( out_shape ) <EOL> out_slicer = [ slice ( None ) ] * len ( out_shape ) <EOL> in2out_ind = slice2outax ( len ( self . shape ) , sliceobj ) [ <NUM_LIT:3> ] <EOL> for i in list ( range ( self . shape [ <NUM_LIT:3> ] ) ) [ slice3 ] : <EOL> data = self . _subheader . data_from_fileobj ( <EOL> frame_mapping [ i ] [ <NUM_LIT:0> ] ) <EOL> out_slicer [ in2out_ind ] = i <EOL> out_data [ tuple ( out_slicer ) ] = data [ in_slicer ] <EOL> return out_data <EOL> class EcatImage ( SpatialImage ) : <EOL> """<STR_LIT>""" <EOL> _header = EcatHeader <EOL> header_class = _header <EOL> valid_exts = ( '<STR_LIT>' , ) <EOL> _subheader = EcatSubHeader <EOL> files_types = ( ( '<STR_LIT:image>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> ImageArrayProxy = EcatImageArrayProxy <EOL> def __init__ ( self , dataobj , affine , header , <EOL> subheader , mlist , <EOL> extra = None , file_map = None ) : <EOL> """<STR_LIT>""" <EOL> self . _subheader = subheader <EOL> self . _mlist = mlist <EOL> self . _dataobj = dataobj <EOL> if affine is not None : <EOL> affine = np . array ( affine , dtype = np . float64 , copy = True ) <EOL> if not affine . shape == ( <NUM_LIT:4> , <NUM_LIT:4> ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . _affine = affine <EOL> if extra is None : <EOL> extra = { } <EOL> self . extra = extra <EOL> self . _header = header <EOL> if file_map is None : <EOL> file_map = self . __class__ . make_file_map ( ) <EOL> self . file_map = file_map <EOL> self . _data_cache = None <EOL> @ property <EOL> def affine ( self ) : <EOL> if not self . _subheader . _check_affines ( ) : <EOL> warnings . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' , UserWarning ) <EOL> return self . _affine <EOL> def get_frame_affine ( self , frame ) : <EOL> """<STR_LIT>""" <EOL> return self . _subheader . get_frame_affine ( frame = frame ) <EOL> def get_frame ( self , frame , orientation = None ) : <EOL> '''<STR_LIT>''' <EOL> return self . _subheader . data_from_fileobj ( frame , orientation ) <EOL> def get_data_dtype ( self , frame ) : <EOL> subhdr = self . _subheader <EOL> dt = subhdr . _get_data_dtype ( frame ) <EOL> return dt <EOL> @ property <EOL> def shape ( self ) : <EOL> x , y , z = self . _subheader . get_shape ( ) <EOL> nframes = self . _subheader . get_nframes ( ) <EOL> return ( x , y , z , nframes ) <EOL> def get_mlist ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _mlist <EOL> def get_subheaders ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _subheader <EOL> @ classmethod <EOL> def from_filespec ( klass , filespec ) : <EOL> return klass . from_filename ( filespec ) <EOL> @ staticmethod <EOL> def _get_fileholders ( file_map ) : <EOL> """<STR_LIT>""" <EOL> return file_map [ '<STR_LIT>' ] , file_map [ '<STR_LIT:image>' ] <EOL> @ classmethod <EOL> def from_file_map ( klass , file_map ) : <EOL> """<STR_LIT>""" <EOL> hdr_file , img_file = klass . _get_fileholders ( file_map ) <EOL> hdr_fid = hdr_file . get_prepare_fileobj ( mode = '<STR_LIT:rb>' ) <EOL> header = klass . _header . from_fileobj ( hdr_fid ) <EOL> hdr_copy = header . copy ( ) <EOL> mlist = np . zeros ( ( header [ '<STR_LIT>' ] , <NUM_LIT:4> ) , dtype = np . int32 ) <EOL> mlist_data = read_mlist ( hdr_fid , hdr_copy . endianness ) <EOL> mlist [ : len ( mlist_data ) ] = mlist_data <EOL> subheaders = klass . _subheader ( hdr_copy , mlist , hdr_fid ) <EOL> data = klass . ImageArrayProxy ( subheaders ) <EOL> if not subheaders . _check_affines ( ) : <EOL> warnings . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' , UserWarning ) <EOL> aff = subheaders . get_frame_affine ( ) <EOL> img = klass ( data , aff , header , subheaders , mlist , <EOL> extra = None , file_map = file_map ) <EOL> return img <EOL> def _get_empty_dir ( self ) : <EOL> '''<STR_LIT>''' <EOL> return np . array ( [ <NUM_LIT> , <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:0> ] , dtype = np . int32 ) <EOL> def _write_data ( self , data , stream , pos , dtype = None , endianness = None ) : <EOL> '''<STR_LIT>''' <EOL> if dtype is None : <EOL> dtype = data . dtype <EOL> if endianness is None : <EOL> endianness = native_code <EOL> stream . seek ( pos ) <EOL> make_array_writer ( data . newbyteorder ( endianness ) , <EOL> dtype ) . to_fileobj ( stream ) <EOL> def to_file_map ( self , file_map = None ) : <EOL> '''<STR_LIT>''' <EOL> if file_map is None : <EOL> file_map = self . file_map <EOL> self . get_data ( ) <EOL> hdr = self . header <EOL> mlist = self . _mlist <EOL> subheaders = self . get_subheaders ( ) <EOL> dir_pos = <NUM_LIT> <EOL> entry_pos = dir_pos + <NUM_LIT:16> <EOL> current_dir = self . _get_empty_dir ( ) <EOL> hdr_fh , img_fh = self . _get_fileholders ( file_map ) <EOL> hdrf = hdr_fh . get_prepare_fileobj ( mode = '<STR_LIT:wb>' ) <EOL> imgf = hdrf <EOL> hdr . write_to ( hdrf ) <EOL> for index in range ( <NUM_LIT:0> , self . header [ '<STR_LIT>' ] ) : <EOL> frame_offset = subheaders . _get_frame_offset ( index ) - <NUM_LIT> <EOL> imgf . seek ( frame_offset ) <EOL> subhdr = subheaders . subheaders [ index ] <EOL> imgf . write ( subhdr . tostring ( ) ) <EOL> pos = imgf . tell ( ) <EOL> imgf . seek ( pos + <NUM_LIT:2> ) <EOL> image = self . _subheader . raw_data_from_fileobj ( index ) <EOL> self . _write_data ( image , imgf , pos + <NUM_LIT:2> , endianness = '<STR_LIT:>>' ) <EOL> self . _write_data ( mlist [ index ] , imgf , entry_pos , endianness = '<STR_LIT:>>' ) <EOL> entry_pos = entry_pos + <NUM_LIT:16> <EOL> current_dir [ <NUM_LIT:0> ] = current_dir [ <NUM_LIT:0> ] - <NUM_LIT:1> <EOL> current_dir [ <NUM_LIT:3> ] = current_dir [ <NUM_LIT:3> ] + <NUM_LIT:1> <EOL> if current_dir [ <NUM_LIT:0> ] == <NUM_LIT:0> : <EOL> self . _write_data ( current_dir , imgf , dir_pos ) <EOL> current_dir = self . _get_empty_dir ( ) <EOL> current_dir [ <NUM_LIT:3> ] = dir_pos / <NUM_LIT> <EOL> dir_pos = mlist [ index ] [ <NUM_LIT:2> ] + <NUM_LIT:1> <EOL> entry_pos = dir_pos + <NUM_LIT:16> <EOL> tmp_avail = current_dir [ <NUM_LIT:0> ] <EOL> tmp_used = current_dir [ <NUM_LIT:3> ] <EOL> while current_dir [ <NUM_LIT:0> ] > <NUM_LIT:0> : <EOL> entry_pos = dir_pos + <NUM_LIT:16> + ( <NUM_LIT:16> * current_dir [ <NUM_LIT:3> ] ) <EOL> self . _write_data ( np . zeros ( <NUM_LIT:4> , dtype = np . int32 ) , imgf , entry_pos ) <EOL> current_dir [ <NUM_LIT:0> ] = current_dir [ <NUM_LIT:0> ] - <NUM_LIT:1> <EOL> current_dir [ <NUM_LIT:3> ] = current_dir [ <NUM_LIT:3> ] + <NUM_LIT:1> <EOL> current_dir [ <NUM_LIT:0> ] = tmp_avail <EOL> current_dir [ <NUM_LIT:3> ] = tmp_used <EOL> self . _write_data ( current_dir , imgf , dir_pos , endianness = '<STR_LIT:>>' ) <EOL> @ classmethod <EOL> def from_image ( klass , img ) : <EOL> raise NotImplementedError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> @ classmethod <EOL> def load ( klass , filespec ) : <EOL> return klass . from_filename ( filespec ) <EOL> load = EcatImage . load </s>
