1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
## Automatically adapted for scipy Oct 19, 2005 by convertcode.py
"""
Matrix Market I/O in Python.
"""
#
# Author: Pearu Peterson <pearu@cens.ioc.ee>
# Created: October, 2004
#
# References:
# http://math.nist.gov/MatrixMarket/
#
# TODO: support for sparse matrices, need spmatrix.tocoo().
import os
from numpy import asarray, real, imag, conj, zeros, ndarray
__all__ = ['mminfo','mmread','mmwrite']
def mminfo(source):
""" Queries the contents of the Matrix Market file 'filename' to
extract size and storage information.
Inputs:
source - Matrix Market filename (extension .mtx) or open file object
Outputs:
rows,cols - number of matrix rows and columns
entries - number of non-zero entries of a sparse matrix
or rows*cols for a dense matrix
rep - 'coordinate' | 'array'
field - 'real' | 'complex' | 'pattern' | 'integer'
symm - 'general' | 'symmetric' | 'skew-symmetric' | 'hermitian'
"""
close_it = 0
if type(source) is type(''):
if not os.path.isfile(source):
if source[-4:] != '.mtx':
source = source + '.mtx'
source = open(source,'r')
close_it = 1
line = source.readline().split()
if not line[0].startswith('%%MatrixMarket'):
raise ValueError,'source is not in Matrix Market format'
assert len(line)==5,`line`
assert line[1].strip().lower()=='matrix',`line`
rep = line[2].strip().lower()
if rep=='dense': rep='array'
elif rep=='sparse': rep='coordinate'
field = line[3].strip().lower()
symm = line[4].strip().lower()
while line:
line = source.readline()
if line.startswith('%'):
continue
line = line.split()
if rep=='array':
assert len(line)==2,`line`
rows,cols = map(eval,line)
entries = rows*cols
else:
assert len(line)==3,`line`
rows,cols,entries = map(eval,line)
break
if close_it:
source.close()
return (rows,cols,entries,rep,field,symm)
def mmread(source):
""" Reads the contents of a Matrix Market file 'filename' into a matrix.
Inputs:
source - Matrix Market filename (extensions .mtx, .mtz.gz)
or open file object.
Outputs:
a - sparse or full matrix
"""
close_it = 0
if type(source) is type(''):
if not os.path.isfile(source):
if os.path.isfile(source+'.mtx'):
source = source + '.mtx'
elif os.path.isfile(source+'.mtx.gz'):
source = source + '.mtx.gz'
if source[-3:] == '.gz':
import gzip
source = gzip.open(source)
else:
source = open(source,'r')
close_it = 1
rows,cols,entries,rep,field,symm = mminfo(source)
try:
from scipy.sparse import coo_matrix
except ImportError:
coo_matrix = None
if field=='integer':
dtype='i'
elif field=='real':
dtype='d'
elif field=='complex':
dtype='D'
elif field=='pattern':
dtype='d'
else:
raise ValueError,`field`
has_symmetry = symm in ['symmetric','skew-symmetric','hermitian']
is_complex = field=='complex'
is_skew = symm=='skew-symmetric'
is_herm = symm=='hermitian'
is_pattern = field=='pattern'
if rep == 'array':
a = zeros((rows,cols),dtype=dtype)
line = 1
i,j = 0,0
while line:
line = source.readline()
if not line or line.startswith('%'):
continue
if is_complex:
aij = complex(*map(float,line.split()))
else:
aij = float(line)
a[i,j] = aij
if has_symmetry and i!=j:
if is_skew:
a[j,i] = -aij
elif is_herm:
a[j,i] = conj(aij)
else:
a[j,i] = aij
if i<rows-1:
i = i + 1
else:
j = j + 1
if not has_symmetry:
i = 0
else:
i = j
assert i in [0,j] and j==cols,`i,j,rows,cols`
elif rep=='coordinate' and coo_matrix is None:
# Read sparse matrix to dense when coo_matrix is not available.
a = zeros((rows,cols), dtype=dtype)
line = 1
k = 0
while line:
line = source.readline()
if not line or line.startswith('%'):
continue
l = line.split()
i,j = map(int,l[:2])
i,j = i-1,j-1
if is_complex:
aij = complex(*map(float,l[2:]))
else:
aij = float(l[2])
a[i,j] = aij
if has_symmetry and i!=j:
if is_skew:
a[j,i] = -aij
elif is_herm:
a[j,i] = conj(aij)
else:
a[j,i] = aij
k = k + 1
assert k==entries,`k,entries`
elif rep=='coordinate':
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`
if close_it:
source.close()
return a
def mmwrite(target,a,comment='',field=None,precision=None):
""" Writes the sparse or dense matrix A to a Matrix Market formatted file.
Inputs:
target - Matrix Market filename (extension .mtx) or open file object
a - sparse or full matrix
comment - comments to be prepended to the Matrix Market file
field - 'real' | 'complex' | 'pattern' | 'integer'
precision - Number of digits to display for real or complex values.
"""
close_it = 0
if type(target) is type(''):
if target[-4:] != '.mtx':
target = target + '.mtx'
target = open(target,'w')
close_it = 1
if isinstance(a, list) or isinstance(a, ndarray) or isinstance(a, tuple) or hasattr(a,'__array__'):
rep = 'array'
a = asarray(a)
if len(a.shape) != 2:
raise ValueError, 'expected matrix'
rows,cols = a.shape
entries = rows*cols
typecode = a.dtype.char
if field is not None:
if field=='integer':
a = a.astype('i')
elif field=='real':
if typecode not in 'fd':
a = a.astype('d')
elif field=='complex':
if typecode not in 'FD':
a = a.astype('D')
elif field=='pattern':
pass
else:
raise ValueError,'unknown field '+field
typecode = a.dtype.char
else:
rep = 'coordinate'
from scipy.sparse import spmatrix
if not isinstance(a,spmatrix):
raise ValueError,'unknown matrix type ' + `type(a)`
rows,cols = a.shape
entries = a.getnnz()
typecode = a.dtype.char
if precision is None:
if typecode in 'fF':
precision = 8
else:
precision = 16
if field is None:
if typecode in 'li':
field = 'integer'
elif typecode in 'df':
field = 'real'
elif typecode in 'DF':
field = 'complex'
else:
raise TypeError,'unexpected typecode '+typecode
if rep == 'array':
symm = _get_symmetry(a)
else:
symm = 'general'
target.write('%%%%MatrixMarket matrix %s %s %s\n' % (rep,field,symm))
for line in comment.split('\n'):
target.write('%%%s\n' % (line))
if field in ['real','integer']:
if field=='real':
format = '%%.%ie\n' % precision
else:
format = '%i\n'
elif field=='complex':
format = '%%.%ie %%.%ie\n' % (precision,precision)
if rep == 'array':
target.write('%i %i\n' % (rows,cols))
if field in ['real','integer']:
if symm=='general':
for j in range(cols):
for i in range(rows):
target.write(format % a[i,j])
else:
for j in range(cols):
for i in range(j,rows):
target.write(format % a[i,j])
elif field=='complex':
if symm=='general':
for j in range(cols):
for i in range(rows):
aij = a[i,j]
target.write(format % (real(aij),imag(aij)))
else:
for j in range(cols):
for i in range(j,rows):
aij = a[i,j]
target.write(format % (real(aij),imag(aij)))
elif field=='pattern':
raise ValueError,'Pattern type inconsisted with dense matrix'
else:
raise TypeError,'Unknown matrix type '+`field`
else:
format = '%i %i ' + format
target.write('%i %i %i\n' % (rows,cols,entries))
assert symm=='general',`symm`
if field in ['real','integer']:
for i in range(entries):
target.write(format % (a.rowcol(i)[0] + 1,a.rowcol(i)[1] + 1,a.getdata(i))) #convert base 0 to base 1
elif field=='complex':
for i in range(entries):
target.write(format % (a.rowcol(i)[0] + 1,a.rowcol(i)[1] + 1,real(a.getdata(i)),imag(a.getdata(i))))
elif field=='pattern':
raise NotImplementedError,`field`
else:
raise TypeError,'Unknown matrix type '+`field`
if close_it:
target.close()
else:
target.flush()
return
def _get_symmetry(a):
m,n = a.shape
if m!=n:
return 'general'
issymm = 1
isskew = 1
isherm = a.dtype.char in 'FD'
for j in range(n):
for i in range(j+1,n):
aij,aji = a[i][j],a[j][i]
if issymm and aij != aji:
issymm = 0
if isskew and aij != -aji:
isskew = 0
if isherm and aij != conj(aji):
isherm = 0
if not (issymm or isskew or isherm):
break
if issymm: return 'symmetric'
if isskew: return 'skew-symmetric'
if isherm: return 'hermitian'
return 'general'
if __name__ == '__main__':
import sys
import time
for filename in sys.argv[1:]:
print 'Reading',filename,'...',
sys.stdout.flush()
t = time.time()
mmread(filename)
print 'took %s seconds' % (time.time() - t)
|