tatami_hdf5
tatami bindings for HDF5-backed matrices
Loading...
Searching...
No Matches
load_compressed_sparse_matrix.hpp
Go to the documentation of this file.
1#ifndef TATAMI_HDF5_LOAD_SPARSE_MATRIX_HPP
2#define TATAMI_HDF5_LOAD_SPARSE_MATRIX_HPP
3
4#include "H5Cpp.h"
5
6#include <string>
7
8#include "tatami/tatami.hpp"
9#include "utils.hpp"
10
17namespace tatami_hdf5 {
18
44template<typename Value_, typename Index_, class ValueStorage_ = std::vector<Value_>, class IndexStorage_ = std::vector<Index_>, class PointerStorage_ = std::vector<size_t> >
45std::shared_ptr<tatami::Matrix<Value_, Index_> > load_compressed_sparse_matrix(
46 size_t nr,
47 size_t nc,
48 const std::string& file,
49 const std::string& vals,
50 const std::string& idx,
51 const std::string& ptr,
52 bool row)
53{
54 H5::H5File file_handle(file, H5F_ACC_RDONLY);
55
56 auto dhandle = open_and_check_dataset<false>(file_handle, vals);
57 const size_t nonzeros = get_array_dimensions<1>(dhandle, "vals")[0];
58
59 ValueStorage_ x(nonzeros);
60 dhandle.read(x.data(), define_mem_type<tatami::ElementType<ValueStorage_> >());
61
62 auto ihandle = open_and_check_dataset<true>(file_handle, idx);
63 if (get_array_dimensions<1>(ihandle, "idx")[0] != nonzeros) {
64 throw std::runtime_error("number of non-zero elements is not consistent between 'data' and 'idx'");
65 }
66 IndexStorage_ i(nonzeros);
67 ihandle.read(i.data(), define_mem_type<tatami::ElementType<IndexStorage_> >());
68
69 auto phandle = open_and_check_dataset<true>(file_handle, ptr);
70 const size_t ptr_size = get_array_dimensions<1>(phandle, "ptr")[0];
71 if (ptr_size != (row ? nr : nc) + 1) {
72 throw std::runtime_error("'ptr' dataset should have length equal to the number of " + (row ? std::string("rows") : std::string("columns")) + " plus 1");
73 }
74
75 // Because HDF5 doesn't have a native type for size_t.
76 PointerStorage_ p(ptr_size);
77 if constexpr(std::is_same<size_t, tatami::ElementType<PointerStorage_> >::value) {
78 if constexpr(std::is_same<size_t, hsize_t>::value) {
79 phandle.read(p.data(), H5::PredType::NATIVE_HSIZE);
80 } else {
81 std::vector<hsize_t> p0(ptr_size);
82 phandle.read(p0.data(), H5::PredType::NATIVE_HSIZE);
83 std::copy(p0.begin(), p0.end(), p.begin());
84 }
85 } else {
86 phandle.read(p.data(), define_mem_type<tatami::ElementType<PointerStorage_> >());
87 }
88
89 return std::make_shared<tatami::CompressedSparseMatrix<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(nr, nc, std::move(x), std::move(i), std::move(p), row);
90}
91
92}
93
94#endif
Representations for matrix data in HDF5 files.
Definition CompressedSparseMatrix.hpp:23
std::shared_ptr< tatami::Matrix< Value_, Index_ > > load_compressed_sparse_matrix(size_t nr, size_t nc, const std::string &file, const std::string &vals, const std::string &idx, const std::string &ptr, bool row)
Definition load_compressed_sparse_matrix.hpp:45
typename std::remove_cv< typename std::remove_reference< decltype(std::declval< Array_ >()[0])>::type >::type ElementType
Utilities for HDF5 extraction.