Skip to content

Commit

Permalink
conform to file interface (PR#4) (#33)
Browse files Browse the repository at this point in the history

* use FileInterface with numpy

---------

Co-authored-by: Bechir <[email protected]>
  • Loading branch information
Bechir-Brahem and Bechir authored Mar 27, 2024
1 parent dc9fb51 commit d07f867
Show file tree
Hide file tree
Showing 22 changed files with 387 additions and 205 deletions.
14 changes: 6 additions & 8 deletions examples/json_example.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
// Your First C++ Program
#include "aare/ContextManager.hpp"
#include <iostream>
#include "aare/File.hpp"
#include "aare/utils/logger.hpp"
#include <iostream>

#define AARE_ROOT_DIR_VAR "PROJECT_ROOT_DIR"

void test(File *f, int frame_number) {
void test(File& f, int frame_number) {
std::cout << "frame number: " << frame_number << std::endl;
Frame frame = f->get_frame(frame_number);
Frame frame = f.iread(frame_number);
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(1, 0)) << std::endl;
std::cout << *((uint16_t *)frame.get(511, 1023)) << std::endl;
}

int main() {
ContextManager ctx_manager;
auto PROJECT_ROOT_DIR = std::filesystem::path(getenv(AARE_ROOT_DIR_VAR));
std::filesystem::path fpath(PROJECT_ROOT_DIR / "data" /"jungfrau"/ "jungfrau_single_master_0.json");
std::filesystem::path fpath(PROJECT_ROOT_DIR / "data" / "jungfrau" / "jungfrau_single_master_0.json");
std::cout << fpath << std::endl;


File *file = ctx_manager.get_file(fpath);
File file(fpath, "r");
test(file, 0);
test(file, 2);
test(file, 9);
Expand Down
11 changes: 6 additions & 5 deletions examples/multiport_example.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
// Your First C++ Program
#include "aare/ContextManager.hpp"
#include "aare/File.hpp"
#include "aare/utils/logger.hpp"
#include <iostream>
#include <iostream>

#define AARE_ROOT_DIR_VAR "PROJECT_ROOT_DIR"

void test(File *f, int frame_number) {
void test(File &f, int frame_number) {
std::cout << "frame number: " << frame_number << std::endl;
Frame frame = f->get_frame(frame_number);
Frame frame = f.iread(frame_number);
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(255, 1023)) << std::endl;
std::cout << *((uint16_t *)frame.get(511, 1023)) << std::endl;
}

int main() {
ContextManager ctx_manager;
auto PROJECT_ROOT_DIR = std::filesystem::path(getenv(AARE_ROOT_DIR_VAR));
std::filesystem::path fpath(PROJECT_ROOT_DIR / "data" / "jungfrau" / "jungfrau_double_master_0.json");
std::cout << fpath << std::endl;

File *file = ctx_manager.get_file(fpath);
File file(fpath, "r");
test(file, 0);
test(file, 9);
}
23 changes: 9 additions & 14 deletions examples/mythen_example.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Your First C++ Program
#include "aare/ContextManager.hpp"
#include "aare/File.hpp"
#include "aare/utils/logger.hpp"
#include <iostream>

#define AARE_ROOT_DIR_VAR "PROJECT_ROOT_DIR"

void test1(File *f, int frame_number) {
void test1(File &f, int frame_number) {
std::cout << "frame number: " << frame_number << std::endl;
Frame frame = f->get_frame(frame_number);
Frame frame = f.iread(frame_number);
std::cout << *((uint32_t *)frame.get(0, 0)) << std::endl;
std::cout << *((uint32_t *)frame.get(0, 1)) << std::endl;
std::cout << *((uint32_t *)frame.get(0, 3839)) << std::endl;
Expand All @@ -20,29 +20,24 @@ void test1(File *f, int frame_number) {
}
}

void test2(File *f, int frame_number) {
void test2(File &f, int frame_number) {
std::cout << "frame number: " << frame_number << std::endl;
Frame frame = f->get_frame(frame_number);
Frame frame = f.iread(frame_number);
std::cout << *((uint32_t *)frame.get(0, 0)) << std::endl;
std::cout << *((uint32_t *)frame.get(0, 1)) << std::endl;
std::cout << *((uint32_t *)frame.get(0, 1280*4 -1)) << std::endl;
std::cout << *((uint32_t *)frame.get(0, 1280 * 4 - 1)) << std::endl;
}

int main() {
ContextManager ctx_manager;
auto PROJECT_ROOT_DIR = std::filesystem::path(getenv(AARE_ROOT_DIR_VAR));
if (PROJECT_ROOT_DIR.empty()) {
throw std::runtime_error("environment variable PROJECT_ROOT_DIR is not set");
}
std::filesystem::path fpath(PROJECT_ROOT_DIR / "data" / "mythen" / "m3_master_0.json");
File *file = ctx_manager.get_file(fpath);
File file(fpath, "r");
test1(file, 0);

fpath = (PROJECT_ROOT_DIR / "data" / "mythen" / "scan242_master_3.raw");
file = ctx_manager.get_file(fpath);
test2(file, 0);




File file2(fpath, "r");
test2(file2, 0);
}
9 changes: 4 additions & 5 deletions examples/numpy_example.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
// Your First C++ Program
#include "aare/ContextManager.hpp"
#include "aare/File.hpp"
#include <iostream>

#define AARE_ROOT_DIR_VAR "PROJECT_ROOT_DIR"

void test(File *f, int frame_number) {
void test(File& f, int frame_number) {
std::cout << "frame number: " << frame_number << std::endl;
Frame frame = f->get_frame(frame_number);
Frame frame = f.iread(frame_number);
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(1, 0)) << std::endl;
std::cout << *((uint16_t *)frame.get(49, 49)) << std::endl;
}

int main() {
ContextManager ctx_manager;

auto PROJECT_ROOT_DIR = std::filesystem::path(getenv(AARE_ROOT_DIR_VAR));
std::filesystem::path fpath(PROJECT_ROOT_DIR / "data" / "numpy" / "test_numpy_file.npy");
std::cout << fpath << std::endl;

File *file = ctx_manager.get_file(fpath);
File file(fpath,"r");
test(file, 0);
test(file, 2);
test(file, 24);
Expand Down
9 changes: 4 additions & 5 deletions examples/raw_example.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
// Your First C++ Program
#include "aare/ContextManager.hpp"
#include "aare/File.hpp"
#include <iostream>
#include "aare/utils/logger.hpp"

#define AARE_ROOT_DIR_VAR "PROJECT_ROOT_DIR"

void test(File *f, int frame_number) {
void test(File& f, int frame_number) {
std::cout << "frame number: " << frame_number << std::endl;
Frame frame = f->get_frame(frame_number);
Frame frame = f.iread(frame_number);
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, 95)) << std::endl;
}

int main() {
ContextManager ctx_manager;
auto PROJECT_ROOT_DIR = std::filesystem::path(getenv(AARE_ROOT_DIR_VAR));
if (PROJECT_ROOT_DIR.empty()) {
throw std::runtime_error("environment variable PROJECT_ROOT_DIR is not set");
}
std::filesystem::path fpath(PROJECT_ROOT_DIR / "data" /"moench"/ "moench04_noise_200V_sto_both_100us_no_light_thresh_900_master_0.raw");
File *file = ctx_manager.get_file(fpath);
File file(fpath, "r");
test(file, 0);
test(file, 2);
test(file, 99);
Expand Down
34 changes: 0 additions & 34 deletions file_io/include/aare/ContextManager.hpp

This file was deleted.

65 changes: 27 additions & 38 deletions file_io/include/aare/File.hpp
Original file line number Diff line number Diff line change
@@ -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{};

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();
};
// 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);


~File();
};
14 changes: 7 additions & 7 deletions file_io/include/aare/FileFactory.hpp
Original file line number Diff line number Diff line change
@@ -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;


Expand Down
Loading

0 comments on commit d07f867

Please sign in to comment.