mirror of
https://github.com/slsdetectorgroup/aare.git
synced 2025-06-06 21:00:41 +02:00
add contextManager to hanldle files
This commit is contained in:
parent
e7bf408a21
commit
d0151153fb
4
.vscode/launch.json
vendored
4
.vscode/launch.json
vendored
@ -4,11 +4,11 @@
|
|||||||
"name": "(gdb) Launch",
|
"name": "(gdb) Launch",
|
||||||
"type": "cppdbg",
|
"type": "cppdbg",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "${workspaceFolder}/build/aare",
|
"program": "${workspaceFolder}/build/examples/json_example",
|
||||||
"args": [],
|
"args": [],
|
||||||
"stopAtEntry": true,
|
"stopAtEntry": true,
|
||||||
"cwd": "${fileDirname}",
|
"cwd": "${fileDirname}",
|
||||||
"environment": [],
|
"environment": [{"name": "PROJECT_ROOT_DIR", "value": "/home/l_bechir/github/aare"}],
|
||||||
"externalConsole": false,
|
"externalConsole": false,
|
||||||
"MIMode": "gdb",
|
"MIMode": "gdb",
|
||||||
"setupCommands": [
|
"setupCommands": [
|
||||||
|
@ -38,6 +38,15 @@ class Frame {
|
|||||||
std::memcpy(m_data, other.m_data, m_rows * m_cols * m_bitdepth / 8);
|
std::memcpy(m_data, other.m_data, m_rows * m_cols * m_bitdepth / 8);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
// add move constructor
|
||||||
|
Frame(Frame &&other) {
|
||||||
|
m_rows = other.rows();
|
||||||
|
m_cols = other.cols();
|
||||||
|
m_bitdepth = other.bitdepth();
|
||||||
|
m_data = other.m_data;
|
||||||
|
other.m_data = nullptr;
|
||||||
|
other.m_rows = other.m_cols = other.m_bitdepth = 0;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
NDView<T> view() {
|
NDView<T> view() {
|
||||||
|
@ -10,4 +10,3 @@ target_link_libraries(numpy_example PUBLIC aare)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,30 +1,29 @@
|
|||||||
// Your First C++ Program
|
// Your First C++ Program
|
||||||
#include "aare/FileHandler.hpp"
|
#include "aare/ContextManager.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#define AARE_ROOT_DIR_VAR "PROJECT_ROOT_DIR"
|
#define AARE_ROOT_DIR_VAR "PROJECT_ROOT_DIR"
|
||||||
|
|
||||||
void test(FileHandler *f, int frame_number) {
|
void test(File *f, int frame_number) {
|
||||||
std::cout << "frame number: " << frame_number << std::endl;
|
std::cout << "frame number: " << frame_number << std::endl;
|
||||||
Frame *frame = f->get_frame(frame_number);
|
Frame frame = f->get_frame(frame_number);
|
||||||
std::cout << *((uint16_t *)frame->get(0, 0)) << std::endl;
|
std::cout << *((uint16_t *)frame.get(0, 0)) << std::endl;
|
||||||
std::cout << *((uint16_t *)frame->get(0, 1)) << std::endl;
|
std::cout << *((uint16_t *)frame.get(0, 1)) << std::endl;
|
||||||
std::cout << *((uint16_t *)frame->get(1, 0)) << std::endl;
|
std::cout << *((uint16_t *)frame.get(1, 0)) << std::endl;
|
||||||
std::cout << *((uint16_t *)frame->get(511, 1023)) << std::endl;
|
std::cout << *((uint16_t *)frame.get(511, 1023)) << std::endl;
|
||||||
|
|
||||||
delete frame;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
ContextManager ctx_manager;
|
||||||
auto PROJECT_ROOT_DIR = std::filesystem::path(getenv(AARE_ROOT_DIR_VAR));
|
auto PROJECT_ROOT_DIR = std::filesystem::path(getenv(AARE_ROOT_DIR_VAR));
|
||||||
std::filesystem::path fpath(PROJECT_ROOT_DIR / "data" / "jungfrau_single_master_0.json");
|
std::filesystem::path fpath(PROJECT_ROOT_DIR / "data" / "jungfrau_single_master_0.json");
|
||||||
// std::filesystem::path fpath(PROJECT_ROOT_DIR / "data" / "test_numpy_file.npy");
|
// std::filesystem::path fpath(PROJECT_ROOT_DIR / "data" / "test_numpy_file.npy");
|
||||||
std::cout << fpath << std::endl;
|
std::cout << fpath << std::endl;
|
||||||
|
|
||||||
auto fileHandler = new FileHandler(fpath);
|
|
||||||
test(fileHandler, 0);
|
|
||||||
test(fileHandler, 2);
|
|
||||||
test(fileHandler, 9);
|
|
||||||
|
|
||||||
delete fileHandler;
|
File *file = ctx_manager.get_file(fpath);
|
||||||
}
|
test(file, 0);
|
||||||
|
test(file, 2);
|
||||||
|
test(file, 9);
|
||||||
|
|
||||||
|
}
|
@ -1,30 +1,32 @@
|
|||||||
// Your First C++ Program
|
// Your First C++ Program
|
||||||
#include "aare/FileHandler.hpp"
|
#include "aare/ContextManager.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#define AARE_ROOT_DIR_VAR "PROJECT_ROOT_DIR"
|
#define AARE_ROOT_DIR_VAR "PROJECT_ROOT_DIR"
|
||||||
|
|
||||||
void test(FileHandler *f, int frame_number) {
|
void test(File* f, int frame_number) {
|
||||||
std::cout << "frame number: " << frame_number << std::endl;
|
std::cout << "frame number: " << frame_number << std::endl;
|
||||||
Frame *frame = f->get_frame(frame_number);
|
Frame frame = f->get_frame(frame_number);
|
||||||
std::cout << *((uint16_t *)frame->get(0, 0)) << std::endl;
|
std::cout << *((uint16_t *)frame.get(0, 0)) << std::endl;
|
||||||
std::cout << *((uint16_t *)frame->get(0, 1)) << std::endl;
|
std::cout << *((uint16_t *)frame.get(0, 1)) << std::endl;
|
||||||
std::cout << *((uint16_t *)frame->get(1, 0)) << std::endl;
|
std::cout << *((uint16_t *)frame.get(1, 0)) << std::endl;
|
||||||
std::cout << *((uint16_t *)frame->get(49, 49)) << std::endl;
|
std::cout << *((uint16_t *)frame.get(49, 49)) << std::endl;
|
||||||
|
|
||||||
delete frame;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
ContextManager ctx_manager;
|
||||||
|
|
||||||
auto PROJECT_ROOT_DIR = std::filesystem::path(getenv(AARE_ROOT_DIR_VAR));
|
auto PROJECT_ROOT_DIR = std::filesystem::path(getenv(AARE_ROOT_DIR_VAR));
|
||||||
// std::filesystem::path fpath(PROJECT_ROOT_DIR / "data" / "jungfrau_single_master_0.json");
|
// std::filesystem::path fpath(PROJECT_ROOT_DIR / "data" / "jungfrau_single_master_0.json");
|
||||||
std::filesystem::path fpath(PROJECT_ROOT_DIR / "data" / "test_numpy_file.npy");
|
std::filesystem::path fpath(PROJECT_ROOT_DIR / "data" / "test_numpy_file.npy");
|
||||||
std::cout << fpath << std::endl;
|
std::cout << fpath << std::endl;
|
||||||
|
|
||||||
auto fileHandler = new FileHandler(fpath);
|
File* file = ctx_manager.get_file(fpath);
|
||||||
test(fileHandler, 0);
|
test(file, 0);
|
||||||
test(fileHandler, 2);
|
test(file, 2);
|
||||||
test(fileHandler, 24);
|
test(file, 24);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
delete fileHandler;
|
|
||||||
}
|
}
|
||||||
|
34
file_io/include/aare/ContextManager.hpp
Normal file
34
file_io/include/aare/ContextManager.hpp
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#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,24 +1,22 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "aare/defs.hpp"
|
|
||||||
#include "aare/Frame.hpp"
|
|
||||||
#include "SubFile.hpp"
|
#include "SubFile.hpp"
|
||||||
|
#include "aare/Frame.hpp"
|
||||||
|
#include "aare/defs.hpp"
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <fmt/core.h>
|
#include <fmt/core.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
class File {
|
class File {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual Frame* get_frame(size_t frame_number) = 0;
|
virtual Frame get_frame(size_t frame_number) =0;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//comment
|
// comment
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual ~File() = default;
|
virtual ~File() = default;
|
||||||
std::filesystem::path fname;
|
std::filesystem::path fname;
|
||||||
std::filesystem::path base_path;
|
std::filesystem::path base_path;
|
||||||
std::string base_name, ext;
|
std::string base_name, ext;
|
||||||
@ -35,11 +33,9 @@ class File {
|
|||||||
ssize_t cols{};
|
ssize_t cols{};
|
||||||
ssize_t bitdepth{};
|
ssize_t bitdepth{};
|
||||||
// File();
|
// File();
|
||||||
|
|
||||||
|
|
||||||
inline size_t bytes_per_frame() const { return rows * cols * bitdepth / 8; }
|
inline size_t bytes_per_frame() const { return rows * cols * bitdepth / 8; }
|
||||||
inline size_t pixels() const { return rows * cols; }
|
inline size_t pixels() const { return rows * cols; }
|
||||||
|
|
||||||
|
|
||||||
// size_t total_frames();
|
// size_t total_frames();
|
||||||
};
|
};
|
@ -9,6 +9,12 @@ class FileFactory{
|
|||||||
public:
|
public:
|
||||||
static FileFactory* get_factory(std::filesystem::path);
|
static FileFactory* get_factory(std::filesystem::path);
|
||||||
// virtual int deleteFile() = 0;
|
// virtual int deleteFile() = 0;
|
||||||
|
static File* load_file(std::filesystem::path p){
|
||||||
|
auto factory = get_factory(p);
|
||||||
|
File* 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 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_metadata(File*)=0;
|
||||||
virtual void parse_fname(File*)=0;
|
virtual void parse_fname(File*)=0;
|
||||||
|
@ -1,28 +0,0 @@
|
|||||||
#include <filesystem>
|
|
||||||
#include "aare/FileFactory.hpp"
|
|
||||||
#include "aare/File.hpp"
|
|
||||||
|
|
||||||
class FileHandler{
|
|
||||||
private:
|
|
||||||
std::filesystem::path fpath;
|
|
||||||
FileFactory* fileFactory;
|
|
||||||
File* f;
|
|
||||||
|
|
||||||
public:
|
|
||||||
FileHandler(std::filesystem::path fname){
|
|
||||||
this->fpath = fname;
|
|
||||||
this->fileFactory= FileFactory::get_factory(fname);
|
|
||||||
this->f= fileFactory->load_file();
|
|
||||||
delete fileFactory;
|
|
||||||
}
|
|
||||||
|
|
||||||
Frame* get_frame(int index){
|
|
||||||
return f->get_frame(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
~FileHandler(){
|
|
||||||
delete f;
|
|
||||||
}
|
|
||||||
};
|
|
@ -9,7 +9,7 @@ class JsonFile : public File {
|
|||||||
using config = RawFileConfig;
|
using config = RawFileConfig;
|
||||||
public:
|
public:
|
||||||
|
|
||||||
Frame *get_frame(size_t frame_number);
|
Frame get_frame(size_t frame_number);
|
||||||
int n_subfiles;
|
int n_subfiles;
|
||||||
std::vector<SubFile *> subfiles;
|
std::vector<SubFile *> subfiles;
|
||||||
int subfile_rows, subfile_cols;
|
int subfile_rows, subfile_cols;
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
#include "aare/FileFactory.hpp"
|
#include "aare/FileFactory.hpp"
|
||||||
|
#include "aare/JsonFile.hpp"
|
||||||
class JsonFileFactory: public FileFactory
|
class JsonFileFactory: public FileFactory
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
/* data */
|
/* data */
|
||||||
public:
|
public:
|
||||||
JsonFileFactory(std::filesystem::path fpath);
|
JsonFileFactory(std::filesystem::path fpath);
|
||||||
File* load_file() override;
|
virtual JsonFile* load_file() override;
|
||||||
void parse_metadata(File*) override;
|
void parse_metadata(File*) override;
|
||||||
void parse_fname(File*) override;
|
void parse_fname(File*) override;
|
||||||
void open_subfiles(File*);
|
void open_subfiles(File*);
|
||||||
|
@ -11,7 +11,7 @@ class NumpyFile : public File {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
NumpyFile(std::filesystem::path fname);
|
NumpyFile(std::filesystem::path fname);
|
||||||
Frame *get_frame(size_t frame_number) override;
|
Frame get_frame(size_t frame_number) override;
|
||||||
header_t header{};
|
header_t header{};
|
||||||
static constexpr std::array<char, 6> magic_str{'\x93', 'N', 'U', 'M', 'P', 'Y'};
|
static constexpr std::array<char, 6> magic_str{'\x93', 'N', 'U', 'M', 'P', 'Y'};
|
||||||
uint8_t major_ver_{};
|
uint8_t major_ver_{};
|
||||||
@ -24,4 +24,10 @@ class NumpyFile : public File {
|
|||||||
return std::accumulate(header.shape.begin() + 1, header.shape.end(), 1, std::multiplies<uint64_t>());
|
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(); };
|
inline ssize_t bytes_per_frame() { return header.dtype.itemsize * pixels_per_frame(); };
|
||||||
|
~NumpyFile(){
|
||||||
|
if (fp != nullptr) {
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
};
|
};
|
@ -14,7 +14,7 @@ class NumpyFileFactory : public FileFactory {
|
|||||||
public:
|
public:
|
||||||
NumpyFileFactory(std::filesystem::path fpath);
|
NumpyFileFactory(std::filesystem::path fpath);
|
||||||
void parse_metadata(File *_file) override;
|
void parse_metadata(File *_file) override;
|
||||||
File *load_file() override;
|
NumpyFile* load_file() override;
|
||||||
void parse_fname(File*){};
|
void parse_fname(File*){};
|
||||||
|
|
||||||
};
|
};
|
@ -1,6 +1,6 @@
|
|||||||
#include "aare/JsonFile.hpp"
|
#include "aare/JsonFile.hpp"
|
||||||
|
|
||||||
Frame *JsonFile::get_frame(size_t frame_number) {
|
Frame JsonFile::get_frame(size_t frame_number) {
|
||||||
if (frame_number > this->total_frames) {
|
if (frame_number > this->total_frames) {
|
||||||
throw std::runtime_error("Frame number out of range");
|
throw std::runtime_error("Frame number out of range");
|
||||||
}
|
}
|
||||||
@ -9,7 +9,7 @@ Frame *JsonFile::get_frame(size_t frame_number) {
|
|||||||
size_t frame_size = this->subfiles[subfile_id]->bytes_per_frame();
|
size_t frame_size = this->subfiles[subfile_id]->bytes_per_frame();
|
||||||
buffer = new std::byte[frame_size];
|
buffer = new std::byte[frame_size];
|
||||||
this->subfiles[subfile_id]->get_frame(buffer, frame_number % this->max_frames_per_file);
|
this->subfiles[subfile_id]->get_frame(buffer, frame_number % this->max_frames_per_file);
|
||||||
auto f = new Frame(buffer, this->rows, this->cols, this->bitdepth );
|
auto f = Frame(buffer, this->rows, this->cols, this->bitdepth );
|
||||||
|
|
||||||
|
|
||||||
delete[] buffer;
|
delete[] buffer;
|
||||||
|
@ -49,8 +49,8 @@ void JsonFileFactory::open_subfiles(File*_file) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
File* JsonFileFactory::load_file() {
|
JsonFile* JsonFileFactory::load_file() {
|
||||||
JsonFile *file = new JsonFile();
|
JsonFile* file = new JsonFile();
|
||||||
file->fname = this->m_fpath;
|
file->fname = this->m_fpath;
|
||||||
this->parse_fname(file);
|
this->parse_fname(file);
|
||||||
this->parse_metadata(file);
|
this->parse_metadata(file);
|
||||||
|
@ -6,16 +6,16 @@ NumpyFile::NumpyFile(std::filesystem::path fname_){
|
|||||||
fp = fopen(this->fname.c_str(), "rb");
|
fp = fopen(this->fname.c_str(), "rb");
|
||||||
}
|
}
|
||||||
|
|
||||||
Frame* NumpyFile::get_frame(size_t frame_number) {
|
Frame NumpyFile::get_frame(size_t frame_number) {
|
||||||
if (fp == nullptr) {
|
if (fp == nullptr) {
|
||||||
throw std::runtime_error("File not open");
|
throw std::runtime_error("File not open");
|
||||||
}
|
}
|
||||||
if (frame_number > header.shape[0]) {
|
if (frame_number > header.shape[0]) {
|
||||||
throw std::runtime_error("Frame number out of range");
|
throw std::runtime_error("Frame number out of range");
|
||||||
}
|
}
|
||||||
Frame *frame = new Frame(header.shape[1], header.shape[2], header.dtype.itemsize*8);
|
Frame frame = Frame(header.shape[1], header.shape[2], header.dtype.itemsize*8);
|
||||||
fseek(fp, header_size + frame_number * bytes_per_frame(), SEEK_SET);
|
fseek(fp, header_size + frame_number * bytes_per_frame(), SEEK_SET);
|
||||||
fread(frame->_get_data(), bytes_per_frame(), 1, fp);
|
fread(frame._get_data(), bytes_per_frame(), 1, fp);
|
||||||
return frame;
|
return frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,8 +72,8 @@ void NumpyFileFactory::parse_metadata(File *_file) {
|
|||||||
file->header = {dtype, fortran_order, shape};
|
file->header = {dtype, fortran_order, shape};
|
||||||
}
|
}
|
||||||
|
|
||||||
File* NumpyFileFactory::load_file() {
|
NumpyFile* NumpyFileFactory::load_file() {
|
||||||
NumpyFile *file = new NumpyFile(this->m_fpath);
|
NumpyFile* file = new NumpyFile(this->m_fpath);
|
||||||
parse_metadata(file);
|
parse_metadata(file);
|
||||||
std::cout << "parsed header: " << file->header.to_string() << std::endl;
|
std::cout << "parsed header: " << file->header.to_string() << std::endl;
|
||||||
return file;
|
return file;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user