-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersistence.py
67 lines (54 loc) · 1.48 KB
/
persistence.py
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
# coding=utf-8
"""
Persistence utilities.
"""
import h5py
from scipy import sparse
def save_csr_to_hdf(
csr_matrix: sparse.csr_matrix,
h5f: h5py.File,
key: str,
**kwargs
) -> None:
"""
Save a CSR matrix to already opened HDF5 file.
:param csr_matrix: sparse.csr_matrix
The matrix.
:param h5f: h5py.File
The opened HDF5 file.
:param key: str
A key.
:param kwargs: Dict[str, Any]
Additional settings passed to h5py.create_dataset
:return: None.
"""
# Read out components of CSR matrix
data, indices, indptr = csr_matrix.data, csr_matrix.indices, csr_matrix.indptr
# Create group
h5g = h5f.create_group(name=key)
# Create dataset per component
h5g.create_dataset(name='data', data=data, **kwargs)
h5g.create_dataset(name='indices', data=indices, **kwargs)
h5g.create_dataset(name='indptr', data=indptr, **kwargs)
def load_csr_from_hdf(
h5f: h5py.File,
key: str
) -> sparse.csr_matrix:
"""
Load a CSR file from already opened HDF5 file.
:param h5f: h5py.File
The already opened file.
:param key: str
The key.
:return: sparse.csr_matrix
The recovered CSR matrix.
"""
# Find group
group = h5f[key]
# Read out components
data = group['data']
indices = group['indices']
indptr = group['indptr']
# Compose matrix
matrix = sparse.csr_matrix((data, indices, indptr))
return matrix