def write_sparse_matrix_hdf5(filename, mtx, name='a sparse matrix'):
    """Assume CSR/CSC."""
    fd = pt.openFile(filename, mode='w', title=name)
    try:
        info = fd.createGroup('/', 'info')
        fd.createArray(info, 'dtype', mtx.dtype.str)
        fd.createArray(info, 'shape', mtx.shape)
        fd.createArray(info, 'format', mtx.format)

        data = fd.createGroup('/', 'data')
        fd.createArray(data, 'data', mtx.data)
        fd.createArray(data, 'indptr', mtx.indptr)
        fd.createArray(data, 'indices', mtx.indices)

    except:
        print 'matrix must be in SciPy sparse CSR/CSC format!'
        print mtx.__repr__()
        raise

    fd.close()