AARE
Data analysis library for PSI hybrid detectors
Loading...
Searching...
No Matches
NumpyFile.hpp
Go to the documentation of this file.
1#pragma once
2#include "aare/core/DType.hpp"
3#include "aare/core/defs.hpp"
6#include <filesystem>
7#include <iostream>
8#include <numeric>
9
10namespace aare {
11
18class NumpyFile : public FileInterface {
19
20 public:
27 NumpyFile(const std::filesystem::path &fname, const std::string &mode = "r", FileConfig cfg = {});
28
29 void write(Frame &frame) override;
30 Frame read() override { return get_frame(this->current_frame++); }
31
32 std::vector<Frame> read(size_t n_frames) override;
33 void read_into(std::byte *image_buf) override { return get_frame_into(this->current_frame++, image_buf); }
34 void read_into(std::byte *image_buf, size_t n_frames) override;
35 size_t frame_number(size_t frame_index) override { return frame_index; };
36 size_t bytes_per_frame() override;
37 size_t pixels() override;
38 void seek(size_t frame_number) override { this->current_frame = frame_number; }
39 size_t tell() override { return this->current_frame; }
40 size_t total_frames() const override { return m_header.shape[0]; }
41 ssize_t rows() const override { return m_header.shape[1]; }
42 ssize_t cols() const override { return m_header.shape[2]; }
43 ssize_t bitdepth() const override { return m_header.dtype.bitdepth(); }
44
49 DType dtype() const { return m_header.dtype; }
50
55 std::vector<size_t> shape() const { return m_header.shape; }
56
63 template <typename T, size_t NDim> NDArray<T, NDim> load() {
64 NDArray<T, NDim> arr(make_shape<NDim>(m_header.shape));
65 fseek(fp, header_size, SEEK_SET);
66 fread(arr.data(), sizeof(T), arr.size(), fp);
67 return arr;
68 }
69
70 ~NumpyFile();
71
72 private:
73 FILE *fp = nullptr;
75 size_t current_frame{};
76 uint32_t header_len{};
77 uint8_t header_len_size{};
78 size_t header_size{};
80 uint8_t major_ver_{};
81 uint8_t minor_ver_{};
82
83 void load_metadata();
84 void get_frame_into(size_t, std::byte *);
86};
87
88} // namespace aare
Definition DType.hpp:20
uint8_t bitdepth() const
Definition DType.cpp:36
FileInterface class to define the interface for file operations.
Definition FileInterface.hpp:33
Definition Frame.hpp:18
Definition NDArray.hpp:23
NumpyFile class to read and write numpy files.
Definition NumpyFile.hpp:18
size_t tell() override
get the current position of the file pointer
Definition NumpyFile.hpp:39
std::vector< size_t > shape() const
get the shape of the numpy file
Definition NumpyFile.hpp:55
size_t current_frame
Definition NumpyFile.hpp:75
void write(Frame &frame) override
write a frame to the file
Definition NumpyFile.cpp:31
FILE * fp
Definition NumpyFile.hpp:73
void load_metadata()
Definition NumpyFile.cpp:103
ssize_t rows() const override
get the number of rows in the file
Definition NumpyFile.hpp:41
size_t pixels() override
get the number of pixels in one frame
Definition NumpyFile.cpp:58
uint8_t minor_ver_
Definition NumpyFile.hpp:81
size_t bytes_per_frame() override
get the size of one frame in bytes
Definition NumpyFile.cpp:61
uint8_t header_len_size
Definition NumpyFile.hpp:77
uint8_t major_ver_
Definition NumpyFile.hpp:80
size_t header_size
Definition NumpyFile.hpp:78
Frame read() override
write a vector of frames to the file
Definition NumpyFile.hpp:30
size_t initial_header_len
Definition NumpyFile.hpp:74
void read_into(std::byte *image_buf) override
read one frame from the file at the current position and store it in the provided buffer
Definition NumpyFile.hpp:33
NumpyHeader m_header
Definition NumpyFile.hpp:79
NDArray< T, NDim > load()
load the numpy file into an NDArray
Definition NumpyFile.hpp:63
size_t frame_number(size_t frame_index) override
get the frame number at the given frame index
Definition NumpyFile.hpp:35
Frame get_frame(size_t frame_number)
Definition NumpyFile.cpp:42
void get_frame_into(size_t, std::byte *)
Definition NumpyFile.cpp:47
DType dtype() const
get the data type of the numpy file
Definition NumpyFile.hpp:49
ssize_t cols() const override
get the number of columns in the file
Definition NumpyFile.hpp:42
void seek(size_t frame_number) override
seek to the given frame number
Definition NumpyFile.hpp:38
~NumpyFile()
Definition NumpyFile.cpp:80
ssize_t bitdepth() const override
get the bitdepth of the file
Definition NumpyFile.hpp:43
size_t total_frames() const override
get the total number of frames in the file
Definition NumpyFile.hpp:40
uint32_t header_len
Definition NumpyFile.hpp:76
Frame class to represent a single frame of data model class should be able to work with streams comin...
Definition CircularFifo.hpp:11
FileConfig structure to store the configuration of a file dtype: data type of the file rows: number o...
Definition FileInterface.hpp:17
Definition NumpyHelpers.hpp:21
DType dtype
Definition NumpyHelpers.hpp:22
shape_t shape
Definition NumpyHelpers.hpp:24