[Scipy-svn] r3629 - trunk/scipy/io
scipy-svn at scipy.org
scipy-svn at scipy.org
Mon Dec 10 17:36:50 EST 2007
Author: wnbell
Date: 2007-12-10 16:36:31 -0600 (Mon, 10 Dec 2007)
New Revision: 3629
Modified:
trunk/scipy/io/mio4.py
trunk/scipy/io/mio5.py
trunk/scipy/io/mmio.py
Log:
made matlab return appropriate sparse format
Modified: trunk/scipy/io/mio4.py
===================================================================
--- trunk/scipy/io/mio4.py 2007-12-10 21:34:27 UTC (rev 3628)
+++ trunk/scipy/io/mio4.py 2007-12-10 22:36:31 UTC (rev 3629)
@@ -166,13 +166,18 @@
res = self.read_array()
tmp = res[:-1,:]
dims = res[-1,0:2]
- ij = N.transpose(tmp[:,0:2]).astype('i') - 1 # for 1-based indexing
- vals = tmp[:,2]
- if res.shape[1] == 4:
- vals = vals + res[:-1,3] * 1j
+ I = N.ascontiguousarray(tmp[:,0],dtype='intc') #fixes byte order also
+ J = N.ascontiguousarray(tmp[:,1],dtype='intc')
+ I -= 1 # for 1-based indexing
+ J -= 1
+ if res.shape[1] == 3:
+ V = N.ascontiguousarray(tmp[:,2],dtype='float')
+ else:
+ V = N.ascontiguousarray(tmp[:,2],dtype='complex')
+ V.imag = tmp[:,3]
if have_sparse:
- return scipy.sparse.csc_matrix((vals,ij), dims)
- return (dims, ij, vals)
+ return scipy.sparse.coo_matrix((V,(I,J)), dims)
+ return (dims, I, J, V)
class MatFile4Reader(MatFileReader):
Modified: trunk/scipy/io/mio5.py
===================================================================
--- trunk/scipy/io/mio5.py 2007-12-10 21:34:27 UTC (rev 3628)
+++ trunk/scipy/io/mio5.py 2007-12-10 22:36:31 UTC (rev 3629)
@@ -348,41 +348,32 @@
class Mat5SparseMatrixGetter(Mat5MatrixGetter):
def get_raw_array(self):
- rowind = self.read_element()
- colind = self.read_element()
+ rowind = self.read_element()
+ indptr = self.read_element()
if self.header['is_complex']:
# avoid array copy to save memory
- res = self.read_element(copy=False)
- res_j = self.read_element(copy=False)
- res = res + (res_j * 1j)
+ data = self.read_element(copy=False)
+ data_j = self.read_element(copy=False)
+ data = data + (data_j * 1j)
else:
- res = self.read_element()
+ data = self.read_element()
''' From the matlab (TM) API documentation, last found here:
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/
rowind are simply the row indices for all the (res) non-zero
entries in the sparse array. rowind has nzmax entries, so
may well have more entries than len(res), the actual number
of non-zero entries, but rowind[len(res):] can be discarded
- and should be 0. colind has length (number of columns + 1),
+ and should be 0. indptr has length (number of columns + 1),
and is such that, if D = diff(colind), D[j] gives the number
of non-zero entries in column j. Because rowind values are
stored in column order, this gives the column corresponding to
each rowind
'''
- cols = N.empty((len(res)), dtype=rowind.dtype)
- col_counts = N.diff(colind)
- start_row = 0
- for i in N.where(col_counts)[0]:
- end_row = start_row + col_counts[i]
- cols[start_row:end_row] = i
- start_row = end_row
- ij = N.vstack((rowind[:len(res)], cols))
if have_sparse:
- result = scipy.sparse.csc_matrix((res,ij),
- self.header['dims'])
+ dims = self.header['dims']
+ return scipy.sparse.csc_matrix((data,rowind,indptr), dims)
else:
- result = (dims, ij, res)
- return result
+ return (dims, data, rowind, indptr)
class Mat5CharMatrixGetter(Mat5MatrixGetter):
Modified: trunk/scipy/io/mmio.py
===================================================================
--- trunk/scipy/io/mmio.py 2007-12-10 21:34:27 UTC (rev 3628)
+++ trunk/scipy/io/mmio.py 2007-12-10 22:36:31 UTC (rev 3629)
@@ -11,7 +11,7 @@
import os
from numpy import asarray, real, imag, conj, zeros, ndarray, \
- empty, concatenate, ones
+ empty, concatenate, ones, ascontiguousarray
from itertools import izip
__all__ = ['mminfo','mmread','mmwrite']
@@ -195,21 +195,20 @@
if is_pattern:
flat_data = flat_data.reshape(-1,2)
- I = flat_data[:,0].astype('i')
- J = flat_data[:,1].astype('i')
+ I = ascontiguousarray(flat_data[:,0], dtype='intc')
+ J = ascontiguousarray(flat_data[:,1], dtype='intc')
V = ones(len(I))
elif is_complex:
flat_data = flat_data.reshape(-1,4)
- I = flat_data[:,0].astype('i')
- J = flat_data[:,1].astype('i')
- V = empty(len(I),dtype='complex')
- V.real = flat_data[:,2]
+ I = ascontiguousarray(flat_data[:,0], dtype='intc')
+ J = ascontiguousarray(flat_data[:,1], dtype='intc')
+ V = ascontiguousarray(flat_data[:,2], dtype='complex')
V.imag = flat_data[:,3]
else:
flat_data = flat_data.reshape(-1,3)
- I = flat_data[:,0].astype('i')
- J = flat_data[:,1].astype('i')
- V = flat_data[:,2].copy()
+ I = ascontiguousarray(flat_data[:,0], dtype='intc')
+ J = ascontiguousarray(flat_data[:,1], dtype='intc')
+ V = ascontiguousarray(flat_data[:,2], dtype='float')
I -= 1 #adjust indices (base 1 -> base 0)
J -= 1
@@ -231,39 +230,6 @@
V = concatenate((V,od_V))
a = coo_matrix((V, (I, J)), dims=(rows, cols), dtype=dtype)
-
-# k = 0
-# data,row,col = [],[],[]
-# row_append = row.append
-# col_append = col.append
-# data_append = data.append
-# line = '%'
-# while line:
-# if not line.startswith('%'):
-# l = line.split()
-# i = int(l[0])-1
-# j = int(l[1])-1
-# if is_pattern:
-# aij = 1.0 #use 1.0 for pattern matrices
-# elif is_complex:
-# aij = complex(*map(float,l[2:]))
-# else:
-# aij = float(l[2])
-# row_append(i)
-# col_append(j)
-# data_append(aij)
-# if has_symmetry and i!=j:
-# if is_skew:
-# aij = -aij
-# elif is_herm:
-# aij = conj(aij)
-# row_append(j)
-# col_append(i)
-# data_append(aij)
-# k += 1
-# line = source.readline()
-# assert k==entries,`k,entries`
-# a = coo_matrix((data, (row, col)), dims=(rows, cols), dtype=dtype)
else:
raise NotImplementedError,`rep`
More information about the Scipy-svn
mailing list