pyemma.msm.io.write_matrix

pyemma.msm.io.write_matrix(filename, A, mode='default', fmt='%.18e', header='', comments='#')

Write matrix to ascii file.

Parameters:
  • filename (str) – Relative or absolute pathname of the output file.
  • A ((M, N) ndarray or sparse matrix) –
  • mode ({'default', 'dense', 'sparse'}) –

    How to determine the storage format

    mode  
    ‘default’ Use the type of A to determine the format
    ‘dense’ Enforce conversion to a dense representationand store the corresponding ndarray
    ‘sparse’ Convert to sparse matrix in COO-format andand store the coordinate list as ndarray
  • fmt (str or sequence of strs, optional) – A single format (%10.5f), a sequence of formats, or a multi-format string, e.g. ‘Iteration %d - %10.5f’, in which case delimiter is ignored.
  • header (str, optional) – String that will be written at the beginning of the file.
  • comments (str, optional) – String that will be prepended to the header strings, to mark them as comments. Default: ‘# ‘.

See also

read_matrix()

Notes

(M, N) dense matrices are stored as ascii file with M rows and N columns. Sparse matrices are converted to coordinate list (COO) format. The coordinate list [...,(row, col, value),...] is then stored as a dense (K, 3) ndarray. K is the number of nonzero entries in the sparse matrix.

Using the naming scheme name.xxx for dense matrices and name.coo.xxx for sparse matrices will allow read_matrix to automatically infer the appropriate matrix type from the given filename.

Examples

>>> from tempfile import NamedTemporaryFile
>>> from pyemma.msm.io import read_matrix, write_matrix

dense

Use temporary file with ending ‘.dat’

>>> tmpfile = NamedTemporaryFile(suffix='.dat')

Dense (3, 2) matrix

>>> A = np.array([[3, 1], [2, 1], [1, 1]])
>>> write_matrix(tmpfile.name, A)

Load from disk

>>> X = load_matrix(tmpfile.name)
>>> X
array([[ 3.,  1.],
       [ 2.,  1.],
       [ 1.,  1.]])

sparse

>>> from scipy.sparse import csr_matrix

Use temporary file with ending ‘.coo.dat’

>>> tmpfile = NamedTemporaryFile(suffix='.coo.dat')

Sparse (3, 3) matrix

>>> A = csr_matrix(np.eye(3))
>>> write_matrix(tmpfile.name, A)

Load from disk

>>> X = load_matrix(tmpfile.name)
>>> X
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])