3
3
#include < binsparse/containers/matrices.hpp>
4
4
#include < nlohmann/json.hpp>
5
5
#include < binsparse/containers/matrices.hpp>
6
+ #include < binsparse/detail.hpp>
6
7
#include " hdf5_tools.hpp"
7
8
#include " type_info.hpp"
8
9
#include < memory>
13
14
14
15
namespace binsparse {
15
16
17
+ inline constexpr double version = 0.1 ;
18
+
16
19
// CSR Format
17
20
21
+ template <typename T, typename I, typename Order>
22
+ void write_dense_matrix (std::string fname,
23
+ dense_matrix<T, I, Order> m,
24
+ nlohmann::json user_keys = {}) {
25
+ H5::H5File f (fname.c_str (), H5F_ACC_TRUNC);
26
+
27
+ std::span<T> values (m.values , m.m *m.n );
28
+
29
+ hdf5_tools::write_dataset (f, " values" , values);
30
+
31
+ using json = nlohmann::json;
32
+ json j;
33
+ j[" binsparse" ][" version" ] = version;
34
+ j[" binsparse" ][" format" ] = __detail::get_matrix_string (m);
35
+ j[" binsparse" ][" shape" ] = {m.m , m.n };
36
+ j[" binsparse" ][" nnz" ] = m.m * m.n ;
37
+ j[" binsparse" ][" data_types" ][" values" ] = type_info<T>::label ();
38
+
39
+ for (auto && v : user_keys.items ()) {
40
+ j[v.key ()] = v.value ();
41
+ }
42
+
43
+ hdf5_tools::set_attribute (f, " binsparse" , j.dump (2 ));
44
+
45
+ f.close ();
46
+ }
47
+
48
+ template <typename T, typename I, typename Order, typename Allocator = std::allocator<T>>
49
+ auto read_dense_matrix (std::string fname, Allocator&& alloc = Allocator{}) {
50
+ H5::H5File f (fname.c_str (), H5F_ACC_RDWR);
51
+
52
+ auto metadata = hdf5_tools::get_attribute (f, " binsparse" );
53
+
54
+ using json = nlohmann::json;
55
+ auto data = json::parse (metadata);
56
+
57
+ std::cout << " Reading values...\n " ;
58
+ auto binsparse_metadata = data[" binsparse" ];
59
+
60
+ assert (binsparse_metadata[" format" ] == __detail::get_matrix_string (dense_matrix<T, I, Order>{}));
61
+
62
+ auto nrows = binsparse_metadata[" shape" ][0 ];
63
+ auto ncols = binsparse_metadata[" shape" ][1 ];
64
+ auto nnz = binsparse_metadata[" nnz" ];
65
+
66
+ auto values = hdf5_tools::read_dataset<T>(f, " values" , alloc);
67
+
68
+ return dense_matrix<T, I, Order>{values.data (), nrows, ncols};
69
+ }
70
+
18
71
template <typename T, typename I>
19
72
void write_csr_matrix (std::string fname,
20
73
csr_matrix<T, I> m,
@@ -32,7 +85,7 @@ void write_csr_matrix(std::string fname,
32
85
33
86
using json = nlohmann::json;
34
87
json j;
35
- j[" binsparse" ][" version" ] = 0.5 ;
88
+ j[" binsparse" ][" version" ] = version ;
36
89
j[" binsparse" ][" format" ] = " CSR" ;
37
90
j[" binsparse" ][" shape" ] = {m.m , m.n };
38
91
j[" binsparse" ][" nnz" ] = m.nnz ;
@@ -53,7 +106,7 @@ template <typename T, typename I, typename Allocator>
53
106
csr_matrix<T, I> read_csr_matrix (std::string fname, Allocator&& alloc) {
54
107
H5::H5File f (fname.c_str (), H5F_ACC_RDWR);
55
108
56
- auto metadata = hdf5_tools::read_dataset< char > (f, " metadata " );
109
+ auto metadata = hdf5_tools::get_attribute (f, " binsparse " );
57
110
58
111
using json = nlohmann::json;
59
112
auto data = json::parse (metadata);
@@ -100,7 +153,7 @@ void write_coo_matrix(std::string fname,
100
153
101
154
using json = nlohmann::json;
102
155
json j;
103
- j[" binsparse" ][" version" ] = 0.5 ;
156
+ j[" binsparse" ][" version" ] = version ;
104
157
j[" binsparse" ][" format" ] = " COO" ;
105
158
j[" binsparse" ][" shape" ] = {m.m , m.n };
106
159
j[" binsparse" ][" nnz" ] = m.nnz ;
@@ -121,14 +174,14 @@ template <typename T, typename I, typename Allocator>
121
174
coo_matrix<T, I> read_coo_matrix (std::string fname, Allocator&& alloc) {
122
175
H5::H5File f (fname.c_str (), H5F_ACC_RDWR);
123
176
124
- auto metadata = hdf5_tools::read_dataset< char > (f, " metadata " );
177
+ auto metadata = hdf5_tools::get_attribute (f, " binsparse " );
125
178
126
179
using json = nlohmann::json;
127
180
auto data = json::parse (metadata);
128
181
129
182
auto binsparse_metadata = data[" binsparse" ];
130
183
131
- assert (binsparse_metadata[" format" ] == " COO" );
184
+ assert (binsparse_metadata[" format" ] == " COO" || binsparse_metadata[ " format " ] == " COOR " );
132
185
133
186
auto nrows = binsparse_metadata[" shape" ][0 ];
134
187
auto ncols = binsparse_metadata[" shape" ][1 ];
0 commit comments