mirror of
https://github.com/slsdetectorgroup/aare.git
synced 2025-06-18 18:27:13 +02:00
conform to file interface (PR#4) (#33)
* use FileInterface with numpy --------- Co-authored-by: Bechir <bechir.brahem420@gmail.com>
This commit is contained in:
@ -1,34 +0,0 @@
|
||||
#include "aare/File.hpp"
|
||||
#include "aare/FileFactory.hpp"
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
|
||||
|
||||
/**
|
||||
* @brief A class to manage the context of the files and network connections
|
||||
* closes the files and network connections when the context is destroyed
|
||||
*/
|
||||
class ContextManager {
|
||||
public:
|
||||
ContextManager() = default;
|
||||
|
||||
File *get_file(std::filesystem::path fname) {
|
||||
auto tmp_file = FileFactory::load_file(fname);
|
||||
this->files.push_back(tmp_file);
|
||||
return tmp_file;
|
||||
}
|
||||
|
||||
// prevent default copying, it can delete the file twice
|
||||
ContextManager(const ContextManager &) = delete;
|
||||
ContextManager &operator=(const ContextManager &) = delete;
|
||||
|
||||
~ContextManager() {
|
||||
for (auto f : files) {
|
||||
delete f;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<File *> files;
|
||||
// std::vector<NetworkConnection*> connections;
|
||||
};
|
@ -1,41 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include "SubFile.hpp"
|
||||
#include "aare/Frame.hpp"
|
||||
#include "aare/defs.hpp"
|
||||
#include <filesystem>
|
||||
#include <fmt/core.h>
|
||||
#include <iostream>
|
||||
|
||||
class File {
|
||||
|
||||
public:
|
||||
virtual Frame get_frame(size_t frame_number) =0;
|
||||
|
||||
#include "aare/FileInterface.hpp"
|
||||
class File : public FileInterface {
|
||||
private:
|
||||
// comment
|
||||
FileInterface *file_impl;
|
||||
|
||||
public:
|
||||
virtual ~File() = default;
|
||||
std::filesystem::path fname;
|
||||
std::filesystem::path base_path;
|
||||
std::string base_name, ext;
|
||||
int findex;
|
||||
size_t total_frames{};
|
||||
size_t max_frames_per_file{};
|
||||
// options:
|
||||
// - r reading
|
||||
// - w writing (overwrites existing file)
|
||||
// - a appending (appends to existing file)
|
||||
// TODO! do we need to support w+, r+ and a+?
|
||||
File(std::filesystem::path fname, std::string mode);
|
||||
Frame read() override;
|
||||
std::vector<Frame> read(size_t n_frames) override;
|
||||
void read_into(std::byte *image_buf) override;
|
||||
void read_into(std::byte *image_buf, size_t n_frames) override;
|
||||
size_t frame_number(size_t frame_index) override;
|
||||
size_t bytes_per_frame() override;
|
||||
size_t pixels() override;
|
||||
void seek(size_t frame_number) override;
|
||||
size_t tell() override;
|
||||
size_t total_frames() const override ;
|
||||
ssize_t rows() const override ;
|
||||
ssize_t cols() const override ;
|
||||
ssize_t bitdepth() const override ;
|
||||
File(File &&other);
|
||||
|
||||
std::string version;
|
||||
DetectorType type;
|
||||
TimingMode timing_mode;
|
||||
bool quad{false};
|
||||
|
||||
ssize_t rows{};
|
||||
ssize_t cols{};
|
||||
ssize_t bitdepth{};
|
||||
// File();
|
||||
|
||||
inline size_t bytes_per_frame() const { return rows * cols * bitdepth / 8; }
|
||||
inline size_t pixels() const { return rows * cols; }
|
||||
|
||||
// size_t total_frames();
|
||||
};
|
||||
~File();
|
||||
};
|
||||
|
@ -1,23 +1,23 @@
|
||||
#pragma once
|
||||
#include <filesystem>
|
||||
#include "aare/File.hpp"
|
||||
#include "aare/FileInterface.hpp"
|
||||
class FileFactory{
|
||||
// Class that will be used to create File objects
|
||||
// Class that will be used to create FileInterface objects
|
||||
// follows the factory pattern
|
||||
protected:
|
||||
std::filesystem::path m_fpath;
|
||||
public:
|
||||
static FileFactory* get_factory(std::filesystem::path);
|
||||
// virtual int deleteFile() = 0;
|
||||
static File* load_file(std::filesystem::path p){
|
||||
static FileInterface* load_file(std::filesystem::path p){
|
||||
auto factory = get_factory(p);
|
||||
File* tmp= factory->load_file();
|
||||
FileInterface* tmp= factory->load_file();
|
||||
delete factory;
|
||||
return tmp;
|
||||
};
|
||||
virtual File* load_file()=0;//TODO: add option to load all file to memory or keep it on disk
|
||||
virtual void parse_metadata(File*)=0;
|
||||
virtual void parse_fname(File*)=0;
|
||||
virtual FileInterface* load_file()=0;//TODO: add option to load all file to memory or keep it on disk
|
||||
virtual void parse_metadata(FileInterface*)=0;
|
||||
virtual void parse_fname(FileInterface*)=0;
|
||||
virtual ~FileFactory() = default;
|
||||
|
||||
|
||||
|
85
file_io/include/aare/FileInterface.hpp
Normal file
85
file_io/include/aare/FileInterface.hpp
Normal file
@ -0,0 +1,85 @@
|
||||
#pragma once
|
||||
#include "aare/Frame.hpp"
|
||||
#include "aare/defs.hpp"
|
||||
#include "aare/utils/logger.hpp"
|
||||
#include <filesystem>
|
||||
#include <vector>
|
||||
class FileInterface {
|
||||
public:
|
||||
friend class FileFactory;
|
||||
// write one frame
|
||||
// virtual void write(Frame &frame) = 0;
|
||||
|
||||
// write n_frames frames
|
||||
// virtual void write(std::vector<Frame> &frames) = 0;
|
||||
|
||||
// read one frame
|
||||
virtual Frame read() = 0;
|
||||
|
||||
// read n_frames frames
|
||||
virtual std::vector<Frame> read(size_t n_frames) = 0; // Is this the right interface?
|
||||
|
||||
// read one frame into the provided buffer
|
||||
virtual void read_into(std::byte *image_buf) = 0;
|
||||
|
||||
// read n_frames frame into the provided buffer
|
||||
virtual void read_into(std::byte *image_buf, size_t n_frames) = 0;
|
||||
|
||||
// read the frame number on position frame_index
|
||||
virtual size_t frame_number(size_t frame_index) = 0;
|
||||
|
||||
// size of one frame, important fro teh read_into function
|
||||
virtual size_t bytes_per_frame() = 0;
|
||||
|
||||
// number of pixels in one frame
|
||||
virtual size_t pixels() = 0;
|
||||
// goto frame number
|
||||
virtual void seek(size_t frame_number) = 0;
|
||||
|
||||
// return the position of the file pointer (in number of frames)
|
||||
virtual size_t tell() = 0;
|
||||
|
||||
// Getter functions
|
||||
virtual size_t total_frames() const = 0;
|
||||
virtual ssize_t rows() const = 0;
|
||||
virtual ssize_t cols() const = 0;
|
||||
virtual ssize_t bitdepth() const = 0;
|
||||
|
||||
// read one frame at position frame_number
|
||||
Frame iread(size_t frame_number) {
|
||||
auto old_pos = tell();
|
||||
seek(frame_number);
|
||||
Frame tmp = read();
|
||||
seek(old_pos);
|
||||
return tmp;
|
||||
};
|
||||
|
||||
// read n_frames frames starting at frame_number
|
||||
std::vector<Frame> iread(size_t frame_number, size_t n_frames) {
|
||||
auto old_pos = tell();
|
||||
seek(frame_number);
|
||||
std::vector<Frame> tmp = read(n_frames);
|
||||
seek(old_pos);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
// function to query the data type of the file
|
||||
/*virtual DataType dtype = 0; */
|
||||
|
||||
virtual ~FileInterface(){
|
||||
|
||||
};
|
||||
|
||||
public:
|
||||
std::filesystem::path m_fname;
|
||||
std::filesystem::path m_base_path;
|
||||
std::string m_base_name, m_ext;
|
||||
int m_findex;
|
||||
size_t m_total_frames{};
|
||||
size_t max_frames_per_file{};
|
||||
std::string version;
|
||||
DetectorType m_type;
|
||||
ssize_t m_rows{};
|
||||
ssize_t m_cols{};
|
||||
ssize_t m_bitdepth{};
|
||||
};
|
@ -1,17 +1,30 @@
|
||||
#pragma once
|
||||
#include "aare/File.hpp"
|
||||
#include "aare/defs.hpp"
|
||||
#include "aare/FileInterface.hpp"
|
||||
#include "aare/NumpyHelpers.hpp"
|
||||
#include "aare/defs.hpp"
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
|
||||
|
||||
class NumpyFile : public File {
|
||||
class NumpyFile : public FileInterface {
|
||||
FILE *fp = nullptr;
|
||||
|
||||
|
||||
public:
|
||||
Frame read() override { return get_frame(this->current_frame++); }
|
||||
|
||||
std::vector<Frame> read(size_t n_frames) override;
|
||||
void read_into(std::byte *image_buf) override { return get_frame_into(this->current_frame++, image_buf); }
|
||||
void read_into(std::byte *image_buf, size_t n_frames) override;
|
||||
size_t frame_number(size_t frame_index) override { return frame_index; };
|
||||
size_t bytes_per_frame() override;
|
||||
size_t pixels() override;
|
||||
void seek(size_t frame_number) override { this->current_frame = frame_number; }
|
||||
size_t tell() override { return this->current_frame; }
|
||||
size_t total_frames() const override { return header.shape[0]; }
|
||||
ssize_t rows()const override { return header.shape[1]; }
|
||||
ssize_t cols()const override { return header.shape[2]; }
|
||||
ssize_t bitdepth()const override { return header.dtype.itemsize; }
|
||||
|
||||
NumpyFile(std::filesystem::path fname);
|
||||
Frame get_frame(size_t frame_number) override;
|
||||
header_t header{};
|
||||
static constexpr std::array<char, 6> magic_str{'\x93', 'N', 'U', 'M', 'P', 'Y'};
|
||||
uint8_t major_ver_{};
|
||||
@ -20,14 +33,15 @@ class NumpyFile : public File {
|
||||
uint8_t header_len_size{};
|
||||
const uint8_t magic_string_length{6};
|
||||
ssize_t header_size{};
|
||||
inline ssize_t pixels_per_frame() {
|
||||
return std::accumulate(header.shape.begin() + 1, header.shape.end(), 1, std::multiplies<uint64_t>());
|
||||
};
|
||||
inline ssize_t bytes_per_frame() { return header.dtype.itemsize * pixels_per_frame(); };
|
||||
~NumpyFile(){
|
||||
|
||||
~NumpyFile() {
|
||||
if (fp != nullptr) {
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private:
|
||||
size_t current_frame{};
|
||||
void get_frame_into(size_t, std::byte *);
|
||||
Frame get_frame(size_t frame_number);
|
||||
};
|
@ -9,12 +9,12 @@
|
||||
class NumpyFileFactory : public FileFactory {
|
||||
private:
|
||||
std::ifstream f;
|
||||
void read_data(File *_file);
|
||||
void read_data(FileInterface *_file);
|
||||
|
||||
public:
|
||||
NumpyFileFactory(std::filesystem::path fpath);
|
||||
void parse_metadata(File *_file) override;
|
||||
void parse_metadata(FileInterface *_file) override;
|
||||
NumpyFile* load_file() override;
|
||||
void parse_fname(File*){};
|
||||
void parse_fname(FileInterface*){};
|
||||
|
||||
};
|
@ -1,15 +1,32 @@
|
||||
#pragma once
|
||||
#include "aare/defs.hpp"
|
||||
#include "aare/FileInterface.hpp"
|
||||
#include "aare/Frame.hpp"
|
||||
#include "aare/File.hpp"
|
||||
#include "aare/SubFile.hpp"
|
||||
#include "aare/defs.hpp"
|
||||
|
||||
class RawFile : public FileInterface {
|
||||
|
||||
class RawFile : public File {
|
||||
using config = RawFileConfig;
|
||||
|
||||
using config = RawFileConfig;
|
||||
public:
|
||||
public:
|
||||
Frame read() override { return get_frame(this->current_frame++); };
|
||||
std::vector<Frame> read(size_t n_frames) override;
|
||||
void read_into(std::byte *image_buf) override { return get_frame_into(this->current_frame++, image_buf); };
|
||||
void read_into(std::byte *image_buf, size_t n_frames) override;
|
||||
size_t frame_number(size_t frame_index) override;
|
||||
|
||||
// size of one frame, important fro teh read_into function
|
||||
size_t bytes_per_frame() override { return m_rows * m_cols * m_bitdepth / 8; }
|
||||
|
||||
// number of pixels in one frame
|
||||
size_t pixels() override { return m_rows * m_cols; }
|
||||
|
||||
// goto frame number
|
||||
void seek(size_t frame_number) { this->current_frame = frame_number; };
|
||||
|
||||
// return the position of the file pointer (in number of frames)
|
||||
size_t tell() { return this->current_frame; };
|
||||
|
||||
Frame get_frame(size_t frame_number);
|
||||
size_t n_subfiles;
|
||||
size_t n_subfile_parts;
|
||||
std::vector<std::vector<SubFile *>> subfiles;
|
||||
@ -17,6 +34,8 @@ class RawFile : public File {
|
||||
xy geometry;
|
||||
std::vector<xy> positions;
|
||||
config cfg{0, 0};
|
||||
TimingMode timing_mode;
|
||||
bool quad{false};
|
||||
|
||||
inline void set_config(int row, int col) {
|
||||
cfg.module_gap_row = row;
|
||||
@ -32,11 +51,21 @@ class RawFile : public File {
|
||||
}
|
||||
|
||||
inline std::filesystem::path master_fname() {
|
||||
return this->base_path / fmt::format("{}_master_{}{}", this->base_name, this->findex, this->ext);
|
||||
return this->m_base_path / fmt::format("{}_master_{}{}", this->m_base_name, this->m_findex, this->m_ext);
|
||||
}
|
||||
inline std::filesystem::path data_fname(int mod_id, int file_id) {
|
||||
return this->base_path / fmt::format("{}_d{}_f{}_{}.raw", this->base_name, file_id, mod_id, this->findex);
|
||||
return this->m_base_path / fmt::format("{}_d{}_f{}_{}.raw", this->m_base_name, file_id, mod_id, this->m_findex);
|
||||
}
|
||||
|
||||
~RawFile();
|
||||
|
||||
size_t total_frames() const { return m_total_frames; }
|
||||
ssize_t rows() const { return m_rows; }
|
||||
ssize_t cols() const { return m_cols; }
|
||||
ssize_t bitdepth() const { return m_bitdepth; }
|
||||
|
||||
private:
|
||||
size_t current_frame{};
|
||||
void get_frame_into(size_t frame_number, std::byte *image_buf);
|
||||
Frame get_frame(size_t frame_number);
|
||||
};
|
@ -8,9 +8,9 @@ class RawFileFactory : public FileFactory {
|
||||
public:
|
||||
RawFileFactory(std::filesystem::path fpath);
|
||||
virtual RawFile *load_file() override;
|
||||
void parse_metadata(File *) override;
|
||||
void parse_fname(File *) override;
|
||||
void open_subfiles(File *);
|
||||
void parse_metadata(FileInterface *) override;
|
||||
void parse_fname(FileInterface *) override;
|
||||
void open_subfiles(FileInterface *);
|
||||
sls_detector_header read_header(const std::filesystem::path &fname);
|
||||
void find_geometry(File *);
|
||||
void find_geometry(FileInterface *);
|
||||
};
|
||||
|
@ -35,6 +35,7 @@ class SubFile {
|
||||
SubFile(std::filesystem::path fname,DetectorType detector, ssize_t rows, ssize_t cols, uint16_t bitdepth);
|
||||
|
||||
size_t get_part(std::byte *buffer, int frame_number);
|
||||
size_t frame_number(int frame_index);
|
||||
|
||||
// TODO: define the inlines as variables and assign them in constructor
|
||||
inline size_t bytes_per_part() { return (m_bitdepth / 8) * m_rows * m_cols; }
|
||||
|
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "aare/File.hpp"
|
||||
#include "aare/FileInterface.hpp"
|
||||
#include <filesystem>
|
||||
#include <fmt/core.h>
|
||||
|
||||
|
Reference in New Issue
Block a user