Skip to content

Instantly share code, notes, and snippets.

@hiropppe
Created January 16, 2017 18:15
Show Gist options
  • Save hiropppe/4254d92374b72a2383901745410ea107 to your computer and use it in GitHub Desktop.
Save hiropppe/4254d92374b72a2383901745410ea107 to your computer and use it in GitHub Desktop.
import tables as tb
from scipy.sparse import lil_matrix
h5 = tb.open_file('sparse.h5', 'a')
data_group = h5.create_group(h5.root, 'data_group')
indices_group = h5.create_group(h5.root, 'indices_group')
indptr_group = h5.create_group(h5.root, 'indptr_group')
for i in xrange(10000):
name = 'm{:d}'.format(i)
m = lil_matrix((361, 200000))
for j in xrange(10):
m[i % 361, j] = 1
m = m.tocsr()
data = m.data
data_atom = tb.Atom.from_dtype(data.dtype)
data_store = h5.create_carray(data_group, name, data_atom, data.shape)
data_store[:] = data
indices = m.indices
indices_atom = tb.Atom.from_dtype(indices.dtype)
indices_store = h5.create_carray(indices_group, name, indices_atom, indices.shape)
indices_store[:] = indices
indptr = m.indptr
indptr_atom = tb.Atom.from_dtype(indptr.dtype)
indptr_store = h5.create_carray(indptr_group, name, indptr_atom, indptr.shape)
indptr_store[:] = indptr
h5.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment