Skip to content

Commit f45c932

Browse files
committed
Update to include comment when convert MM -> Binsparse
1 parent d7784a7 commit f45c932

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

examples/convert_binsparse.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55

66
template <typename T, typename I>
77
void convert_to_binsparse(std::string input_file, std::string output_file, std::string format, std::string comment) {
8+
nlohmann::json user_keys;
9+
user_keys["comment"] = comment;
810
if (format == "CSR") {
911
std::cout << "Reading in " << input_file << "...\n";
1012
auto x = binsparse::__detail::mmread<T, I, binsparse::__detail::csr_matrix_owning<T, I>>(input_file);
1113
binsparse::csr_matrix<T, I> matrix{x.values().data(), x.colind().data(), x.rowptr().data(), std::get<0>(x.shape()), std::get<1>(x.shape()), I(x.size())};
12-
binsparse::write_csr_matrix(output_file, matrix);
14+
binsparse::write_csr_matrix(output_file, matrix, user_keys);
1315
std::cout << "Writing to binsparse file " << output_file << " using " << format << " format...\n";
1416
} else {
1517
auto x = binsparse::__detail::mmread<T, I, binsparse::__detail::coo_matrix_owning<T, I>>(input_file);
1618
binsparse::coo_matrix<T, I> matrix{x.values().data(), x.rowind().data(), x.colind().data(), std::get<0>(x.shape()), std::get<1>(x.shape()), I(x.size())};
17-
binsparse::write_coo_matrix(output_file, matrix);
19+
binsparse::write_coo_matrix(output_file, matrix, user_keys);
1820
std::cout << "Writing to binsparse file " << output_file << " using " << format << " format...\n";
1921
}
2022
}

examples/inspect_binsparse.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#include <iostream>
33
#include <concepts>
44
#include <complex>
5+
#include <ranges>
6+
7+
class Foo {};
58

69
int main(int argc, char** argv) {
710

@@ -13,6 +16,7 @@ int main(int argc, char** argv) {
1316
std::string input_file(argv[1]);
1417

1518
auto j = binsparse::inspect(input_file);
19+
1620
auto metadata = j["binsparse"];
1721

1822
std::cout << "Inspecting Binsparse v" << metadata["version"] << " file...\n";
@@ -30,14 +34,26 @@ int main(int argc, char** argv) {
3034
auto i0 = metadata["data_types"]["pointers_to_1"];
3135
auto i1 = metadata["data_types"]["indices_1"];
3236
auto t = metadata["data_types"]["values"];
33-
std::cout << "Stored using index types: " << i0 << " " << i1 << std::endl;
34-
std::cout << "Value type: " << t << std::endl;
37+
std::cout << "Stored using index types: " << i0 << " " << i1 << " and "
38+
<< "value type: " << t << std::endl;
3539
} else {
3640
assert(false);
3741
}
3842

39-
std::cout << "Raw JSON:\n";
40-
std::cout << j.dump(2) << std::endl;
43+
44+
nlohmann::json user;
45+
std::cout << "User-provided keys:\n";
46+
std::size_t n_keys = 0;
47+
for (auto&& v : j.items()) {
48+
if (v.key() != "binsparse") {
49+
user[v.key()] = v.value();
50+
n_keys++;
51+
}
52+
}
53+
54+
if (n_keys > 0) {
55+
std::cout << user.dump(2) << std::endl;
56+
}
4157

4258
return 0;
4359
}

include/binsparse/binsparse.hpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ namespace binsparse {
1919

2020
template <typename T, typename I>
2121
void write_csr_matrix(std::string fname,
22-
csr_matrix<T, I> m) {
22+
csr_matrix<T, I> m,
23+
nlohmann::json user_keys = {}) {
2324

2425
H5::H5File f(fname.c_str(), H5F_ACC_TRUNC);
2526

@@ -41,6 +42,10 @@ void write_csr_matrix(std::string fname,
4142
j["binsparse"]["data_types"]["indices_1"] = type_info<I>::label();
4243
j["binsparse"]["data_types"]["values"] = type_info<T>::label();
4344

45+
for (auto&& v : user_keys.items()) {
46+
j[v.key()] = v.value();
47+
}
48+
4449
hdf5_tools::write_dataset(f, "metadata", j.dump(2));
4550

4651
f.close();
@@ -82,7 +87,8 @@ csr_matrix<T, I> read_csr_matrix(std::string fname) {
8287

8388
template <typename T, typename I>
8489
void write_coo_matrix(std::string fname,
85-
coo_matrix<T, I> m) {
90+
coo_matrix<T, I> m,
91+
nlohmann::json user_keys = {}) {
8692

8793
H5::H5File f(fname.c_str(), H5F_ACC_TRUNC);
8894

@@ -103,6 +109,10 @@ void write_coo_matrix(std::string fname,
103109
j["binsparse"]["data_types"]["indices_0"] = type_info<I>::label();
104110
j["binsparse"]["data_types"]["indices_1"] = type_info<I>::label();
105111
j["binsparse"]["data_types"]["values"] = type_info<T>::label();
112+
113+
for (auto&& v : user_keys.items()) {
114+
j[v.key()] = v.value();
115+
}
106116

107117
hdf5_tools::write_dataset(f, "metadata", j.dump(2));
108118

0 commit comments

Comments
 (0)