#NEWLINE# def _read_matrix( fid , tag , shape , rlims , matrix_coding ) : #NEWLINE# #INDENT# 'Read a matrix (dense or sparse) tag' #NEWLINE# matrix_coding = ( matrix_coding >> 16 ) #NEWLINE# if ( shape is not None ) : #NEWLINE# #INDENT# raise ValueError ( 'Row reading not implemented for matrices yet' ) #NEWLINE# #UNINDENT# if ( matrix_coding == _matrix_coding_dense ) : #NEWLINE# #INDENT# pos = fid . tell ( ) #NEWLINE# fid . seek ( ( tag . size - 4 ) , 1 ) #NEWLINE# ndim = int ( np . fromstring ( fid . read ( 4 ) , dtype = '>i4' ) ) #NEWLINE# fid . seek ( ( ( - ( ndim + 1 ) ) * 4 ) , 1 ) #NEWLINE# dims = np . fromstring ( fid . read ( ( 4 * ndim ) ) , dtype = '>i4' ) [ : : ( - 1 ) ] #NEWLINE# fid . seek ( pos , 0 ) #NEWLINE# if ( ndim > 3 ) : #NEWLINE# #INDENT# raise Exception ( 'Only 2 or 3-dimensional matrices are supported at this time' ) #NEWLINE# #UNINDENT# matrix_type = ( _data_type & tag . type ) #NEWLINE# if ( matrix_type == FIFF . FIFFT_INT ) : #NEWLINE# #INDENT# data = np . fromstring ( read_big ( fid , ( 4 * dims . prod ( ) ) ) , dtype = '>i4' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( matrix_type == FIFF . FIFFT_JULIAN ) : #NEWLINE# #INDENT# data = np . fromstring ( read_big ( fid , ( 4 * dims . prod ( ) ) ) , dtype = '>i4' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( matrix_type == FIFF . FIFFT_FLOAT ) : #NEWLINE# #INDENT# data = np . fromstring ( read_big ( fid , ( 4 * dims . prod ( ) ) ) , dtype = '>f4' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( matrix_type == FIFF . FIFFT_DOUBLE ) : #NEWLINE# #INDENT# data = np . fromstring ( read_big ( fid , ( 8 * dims . prod ( ) ) ) , dtype = '>f8' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( matrix_type == FIFF . FIFFT_COMPLEX_FLOAT ) : #NEWLINE# #INDENT# data = np . fromstring ( read_big ( fid , ( ( 4 * 2 ) * dims . prod ( ) ) ) , dtype = '>f4' ) #NEWLINE# data = ( data [ : : 2 ] + ( 1j * data [ 1 : : 2 ] ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( matrix_type == FIFF . FIFFT_COMPLEX_DOUBLE ) : #NEWLINE# #INDENT# data = np . fromstring ( read_big ( fid , ( ( 8 * 2 ) * dims . prod ( ) ) ) , dtype = '>f8' ) #NEWLINE# data = ( data [ : : 2 ] + ( 1j * data [ 1 : : 2 ] ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise Exception ( ( 'Cannot handle matrix of type %d yet' % matrix_type ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# data . shape = dims #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( matrix_coding in ( _matrix_coding_CCS , _matrix_coding_RCS ) ) : #NEWLINE# #INDENT# from scipy import sparse #NEWLINE# pos = fid . tell ( ) #NEWLINE# fid . seek ( ( tag . size - 4 ) , 1 ) #NEWLINE# ndim = int ( np . fromstring ( fid . read ( 4 ) , dtype = '>i4' ) ) #NEWLINE# fid . seek ( ( ( - ( ndim + 2 ) ) * 4 ) , 1 ) #NEWLINE# dims = np . fromstring ( fid . read ( ( 4 * ( ndim + 1 ) ) ) , dtype = '>i4' ) #NEWLINE# if ( ndim != 2 ) : #NEWLINE# #INDENT# raise Exception ( 'Only two-dimensional matrices are supported at this time' ) #NEWLINE# #UNINDENT# fid . seek ( pos , 0 ) #NEWLINE# nnz = int ( dims [ 0 ] ) #NEWLINE# nrow = int ( dims [ 1 ] ) #NEWLINE# ncol = int ( dims [ 2 ] ) #NEWLINE# sparse_data = np . fromstring ( fid . read ( ( 4 * nnz ) ) , dtype = '>f4' ) #NEWLINE# shape = ( dims [ 1 ] , dims [ 2 ] ) #NEWLINE# if ( matrix_coding == _matrix_coding_CCS ) : #NEWLINE# #INDENT# tmp_indices = fid . read ( ( 4 * nnz ) ) #NEWLINE# sparse_indices = np . fromstring ( tmp_indices , dtype = '>i4' ) #NEWLINE# tmp_ptrs = fid . read ( ( 4 * ( ncol + 1 ) ) ) #NEWLINE# sparse_ptrs = np . fromstring ( tmp_ptrs , dtype = '>i4' ) #NEWLINE# if ( ( sparse_ptrs [ ( - 1 ) ] > len ( sparse_indices ) ) or np . any ( ( sparse_ptrs < 0 ) ) ) : #NEWLINE# #INDENT# sparse_indices = np . concatenate ( ( np . fromstring ( tmp_indices [ : ( 4 * ( nrow + 1 ) ) ] , dtype = '>i4' ) , np . fromstring ( tmp_indices [ ( 4 * ( nrow + 1 ) ) : ] , dtype = '<i4' ) ) ) #NEWLINE# sparse_ptrs = np . fromstring ( tmp_ptrs , dtype = '<i4' ) #NEWLINE# #UNINDENT# data = sparse . csc_matrix ( ( sparse_data , sparse_indices , shape ) , shape = shape ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# sparse_indices = np . fromstring ( fid . read ( ( 4 * nnz ) ) , dtype = '>i4' ) #NEWLINE# sparse_ptrs = np . fromstring ( fid . read ( ( 4 * ( nrow + 1 ) ) ) , dtype = '>i4' ) #NEWLINE# data = sparse . csr_matrix ( ( sparse_data , sparse_indices , sparse_ptrs ) , shape = shape ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise Exception ( 'Cannot handle other than dense or sparse matrices yet' ) #NEWLINE# #UNINDENT# #UNINDENT# return data