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