aiidateam / aiida-core

The official repository for the AiiDA code
https://aiida-core.readthedocs.io
Other
415 stars 187 forks source link

Sparse arrays data class. #875

Open vdikan opened 6 years ago

vdikan commented 6 years ago

For some problems (e.g. PDOS calculation) I would like to have/implement something like SparseData, that is like ArrayData but stores on disk a sparse-matrix representation of numpy array.

Small snippet of how it can be done with existing scipy.sparse library:

   #+begin_src python :results output

     import numpy as np
     from scipy.sparse import csr_matrix

     bands = [[0, 0,   0,     58.2, 93.6,  0],
              [0, 0,   113.7, 33.5,    0,  0],
              [0, 205, 0,     506,     0,  0]]

     # indptr = [0, 2, 4, 6]
     # indices = [3, 4, 2, 3, 1, 3]
     # data = [58, 93, 113, 33, 205, 506]

     indptr = [0]
     indices = []
     data = []

     for b in bands:
         for pos, num in enumerate(b):
             if (num != 0.):
                 indices.append(pos)
                 data.append(num)
         indptr.append(len(data))

     print csr_matrix((data, indices, indptr), dtype=float).toarray()
   #+end_src

   #+RESULTS:
   : [[   0.     0.     0.    58.2   93.6]
   :  [   0.     0.   113.7   33.5    0. ]
   :  [   0.   205.     0.   506.     0. ]]
espenfl commented 3 years ago

I support adding something like this as this is fairly general. Also needed when storing properties for each ionic/sc step.