added tests and features to load full file

This commit is contained in:
Erik Frojdh
2024-04-02 17:19:10 +02:00
parent d196eb5a2e
commit 670d9415e6
9 changed files with 78 additions and 22 deletions

View File

@@ -1,6 +1,7 @@
#pragma once
#include "aare/FileInterface.hpp"
#include "aare/NumpyHelpers.hpp"
#include "aare/DType.hpp"
#include "aare/defs.hpp"
#include <iostream>
#include <numeric>
@@ -45,6 +46,18 @@ class NumpyFile : public FileInterface {
ssize_t cols() const override { return m_header.shape[2]; }
ssize_t bitdepth() const override { return m_header.dtype.bitdepth(); }
DType dtype() const { return m_header.dtype; }
std::vector<size_t> shape() const { return m_header.shape; }
//load the full numpy file into a NDArray
template<typename T, size_t NDim>
NDArray<T,NDim> load(){
NDArray<T,NDim> arr(make_shape<NDim>(m_header.shape));
fseek(fp, header_size, SEEK_SET);
fread(arr.data(), sizeof(T), arr.size(), fp);
return arr;
}
~NumpyFile();
};

View File

@@ -14,7 +14,7 @@
#include "aare/DType.hpp"
#include "aare/defs.hpp"
using shape_t = std::vector<uint64_t>;
using shape_t = std::vector<size_t>;
struct header_t {
header_t() : dtype(aare::DType(aare::DType::ERROR)), fortran_order(false), shape(shape_t()){};
@@ -53,7 +53,7 @@ std::string get_value_from_map(const std::string &mapstr);
std::unordered_map<std::string, std::string> parse_dict(std::string in, const std::vector<std::string> &keys);
template <typename T, size_t N> inline bool in_array(T val, const std::array<T, N> &arr) {
template <typename T, size_t N> bool in_array(T val, const std::array<T, N> &arr) {
return std::find(std::begin(arr), std::end(arr), val) != std::end(arr);
}
bool is_digits(const std::string &str);