fix ci and add formatting (#48)

* add dependency

* dont run blocking zmq example and add formatting

* format files
This commit is contained in:
Bechir Braham
2024-04-03 13:47:52 +02:00
committed by GitHub
parent 23c855acbc
commit 31a20d4f6c
34 changed files with 438 additions and 522 deletions

View File

@ -8,7 +8,7 @@ File::File(std::filesystem::path fname, std::string mode, FileConfig cfg) {
file_impl = FileFactory::load_file(fname, mode, cfg);
}
void File::write(Frame& frame) { file_impl->write(frame); }
void File::write(Frame &frame) { file_impl->write(frame); }
Frame File::read() { return file_impl->read(); }
size_t File::total_frames() const { return file_impl->total_frames(); }
std::vector<Frame> File::read(size_t n_frames) { return file_impl->read(n_frames); }
@ -33,4 +33,4 @@ File::File(File &&other) {
// write move assignment operator
}
} // namespace aare

View File

@ -1,22 +1,21 @@
#include "aare/FileFactory.hpp"
#include "aare/FileInterface.hpp"
#include "aare/RawFileFactory.hpp"
#include "aare/NumpyFileFactory.hpp"
#include "aare/utils/logger.hpp"
#include "aare/RawFileFactory.hpp"
#include "aare/utils/logger.hpp"
#include <iostream>
namespace aare {
FileFactory *FileFactory::get_factory(std::filesystem::path fpath) {
if (fpath.extension() == ".raw" || fpath.extension() == ".json"){
aare::logger::debug("Loading",fpath.extension(),"file");
if (fpath.extension() == ".raw" || fpath.extension() == ".json") {
aare::logger::debug("Loading", fpath.extension(), "file");
return new RawFileFactory(fpath);
}
if (fpath.extension() == ".raw" || fpath.extension() == ".json"){
aare::logger::debug("Loading",fpath.extension(),"file");
}
if (fpath.extension() == ".raw" || fpath.extension() == ".json") {
aare::logger::debug("Loading", fpath.extension(), "file");
return new RawFileFactory(fpath);
}
}
// check if extension is numpy
else if (fpath.extension() == ".npy") {
aare::logger::debug("Loading numpy file");
@ -27,5 +26,3 @@ FileFactory *FileFactory::get_factory(std::filesystem::path fpath) {
}
} // namespace aare

View File

@ -1,10 +1,10 @@
#include "aare/NumpyFile.hpp"
namespace aare{
namespace aare {
NumpyFile::NumpyFile(const std::filesystem::path& fname) {
//TODO! add opts to constructor
NumpyFile::NumpyFile(const std::filesystem::path &fname) {
// TODO! add opts to constructor
m_fname = fname;
fp = fopen(m_fname.c_str(), "rb");
if (!fp) {
@ -26,8 +26,7 @@ NumpyFile::NumpyFile(FileConfig config, header_t header) {
throw std::runtime_error(fmt::format("Could not open: {} for reading", m_fname.c_str()));
}
initial_header_len =
aare::NumpyHelpers::write_header(std::filesystem::path(m_fname.c_str()), header);
initial_header_len = aare::NumpyHelpers::write_header(std::filesystem::path(m_fname.c_str()), header);
}
void NumpyFile::write(Frame &frame) {
@ -41,8 +40,6 @@ void NumpyFile::write(Frame &frame) {
fwrite(frame.data(), frame.size(), 1, fp);
}
Frame NumpyFile::get_frame(size_t frame_number) {
Frame frame(m_header.shape[1], m_header.shape[2], m_header.dtype.bitdepth());
get_frame_into(frame_number, frame.data());
@ -97,17 +94,14 @@ NumpyFile::~NumpyFile() {
std::string header_str = ss.str();
// write header
fwrite(header_str.c_str(), header_str.size(), 1, fp);
}
if (fp != nullptr) {
fclose(fp);
}
}
void NumpyFile::load_metadata(){
void NumpyFile::load_metadata() {
// read magic number
std::array<char, 6> tmp{};
@ -120,8 +114,8 @@ void NumpyFile::load_metadata(){
}
// read version
fread(reinterpret_cast<char *>(&major_ver_), sizeof(major_ver_), 1,fp);
fread(reinterpret_cast<char *>(&minor_ver_), sizeof(minor_ver_), 1,fp);
fread(reinterpret_cast<char *>(&major_ver_), sizeof(major_ver_), 1, fp);
fread(reinterpret_cast<char *>(&minor_ver_), sizeof(minor_ver_), 1, fp);
if (major_ver_ == 1) {
header_len_size = 2;
@ -132,7 +126,7 @@ void NumpyFile::load_metadata(){
}
// read header length
fread(reinterpret_cast<char *>(&header_len), header_len_size,1, fp);
fread(reinterpret_cast<char *>(&header_len), header_len_size, 1, fp);
header_size = aare::NumpyHelpers::magic_string_length + 2 + header_len_size + header_len;
if (header_size % 16 != 0) {
fmt::print("Warning: header length is not a multiple of 16\n");
@ -140,8 +134,7 @@ void NumpyFile::load_metadata(){
// read header
std::string header(header_len, '\0');
fread(header.data(), header_len,1,fp);
fread(header.data(), header_len, 1, fp);
// parse header
std::vector<std::string> keys{"descr", "fortran_order", "shape"};
@ -171,5 +164,4 @@ void NumpyFile::load_metadata(){
m_header = {dtype, fortran_order, shape};
}
} // namespace aare

View File

@ -5,7 +5,6 @@ namespace aare {
NumpyFileFactory::NumpyFileFactory(std::filesystem::path fpath) { this->m_fpath = fpath; }
NumpyFile *NumpyFileFactory::load_file_read() {
NumpyFile *file = new NumpyFile(this->m_fpath);
return file;
@ -13,8 +12,6 @@ NumpyFile *NumpyFileFactory::load_file_read() {
NumpyFile *NumpyFileFactory::load_file_write(FileConfig config) {
NumpyFile *file = new NumpyFile(config, {config.dtype, false, {config.rows, config.cols}});
return file;
};

View File

@ -78,7 +78,7 @@ std::unordered_map<std::string, std::string> parse_dict(std::string in, const st
}
aare::DType parse_descr(std::string typestring) {
if (typestring.length() < 3) {
throw std::runtime_error("invalid typestring (length)");
}
@ -187,7 +187,7 @@ template <typename T> inline std::string write_tuple(const std::vector<T> &v) {
// ss << v[i] << delimiter;
// }
// ss << v.back();
std::copy(v.begin(), v.end()-1, std::ostream_iterator<T>(ss, ", "));
std::copy(v.begin(), v.end() - 1, std::ostream_iterator<T>(ss, ", "));
ss << v.back();
ss << ")";
}
@ -214,7 +214,6 @@ size_t write_header(std::filesystem::path fname, const header_t &header) {
return write_header(out, header);
}
size_t write_header(std::ostream &out, const header_t &header) {
std::string header_dict = write_header_dict(header.dtype.str(), header.fortran_order, header.shape);

View File

@ -1,7 +1,7 @@
#include "aare/RawFile.hpp"
#include "aare/utils/logger.hpp"
namespace aare{
namespace aare {
Frame RawFile::get_frame(size_t frame_number) {
auto f = Frame(this->m_rows, this->m_cols, this->m_bitdepth);
@ -62,8 +62,6 @@ void RawFile::read_into(std::byte *image_buf, size_t n_frames) {
}
}
size_t RawFile::frame_number(size_t frame_index) {
if (frame_index > this->m_total_frames) {
throw std::runtime_error(LOCATION + "Frame number out of range");

View File

@ -65,7 +65,7 @@ void RawFileFactory::parse_raw_metadata(RawFile *file) {
} else if (key == "Pixels") {
// Total number of pixels cannot be found yet looking at
// submodule
pos = value.find(',');
pos = value.find(',');
file->subfile_cols = std::stoi(value.substr(1, pos));
file->subfile_rows = std::stoi(value.substr(pos + 1));
} else if (key == "Total Frames") {
@ -77,7 +77,7 @@ void RawFileFactory::parse_raw_metadata(RawFile *file) {
} else if (key == "Max Frames Per File") {
file->max_frames_per_file = std::stoi(value);
} else if (key == "Geometry") {
pos = value.find(',');
pos = value.find(',');
file->geometry = {std::stoi(value.substr(1, pos)), std::stoi(value.substr(pos + 1))};
}
}
@ -114,8 +114,8 @@ void RawFileFactory::open_subfiles(FileInterface *_file) {
for (size_t i = 0; i != file->n_subfiles; ++i) {
auto v = std::vector<SubFile *>(file->n_subfile_parts);
for (size_t j = 0; j != file->n_subfile_parts; ++j) {
v[j] =
new SubFile(file->data_fname(i, j), file->m_type, file->subfile_rows, file->subfile_cols, file->bitdepth());
v[j] = new SubFile(file->data_fname(i, j), file->m_type, file->subfile_rows, file->subfile_cols,
file->bitdepth());
}
file->subfiles.push_back(v);
}
@ -147,7 +147,6 @@ sls_detector_header RawFileFactory::read_header(const std::filesystem::path &fna
return h;
}
void RawFileFactory::find_geometry(FileInterface *_file) {
auto file = dynamic_cast<RawFile *>(_file);
uint16_t r{};

View File

@ -12,7 +12,8 @@ SubFile::SubFile(std::filesystem::path fname, DetectorType detector, ssize_t row
this->m_bitdepth = bitdepth;
this->n_frames = std::filesystem::file_size(fname) / (sizeof(sls_detector_header) + rows * cols * bitdepth / 8);
if (read_impl_map.find({detector, bitdepth}) == read_impl_map.end()) {
auto error_msg = LOCATION + "No read_impl function found for detector: " + toString(detector) + " and bitdepth: " + std::to_string(bitdepth);
auto error_msg = LOCATION + "No read_impl function found for detector: " + toString(detector) +
" and bitdepth: " + std::to_string(bitdepth);
throw std::runtime_error(error_msg);
}
this->read_impl = read_impl_map.at({detector, bitdepth});

View File

@ -10,4 +10,4 @@ bool is_master_file(std::filesystem::path fpath) {
return false;
}
}// namespace aare
} // namespace aare

View File

@ -1,53 +1,50 @@
#include <catch2/catch_test_macros.hpp>
#include "aare/NumpyFile.hpp"
#include "aare/NDArray.hpp"
#include <catch2/catch_test_macros.hpp>
#include "test_config.hpp"
using aare::NumpyFile;
using aare::DType;
TEST_CASE("Read a 1D numpy file with int32 data type"){
using aare::NumpyFile;
TEST_CASE("Read a 1D numpy file with int32 data type") {
auto fpath = test_data_path() / "numpy" / "test_1d_int32.npy";
REQUIRE(std::filesystem::exists(fpath));
NumpyFile f(fpath);
//we know the file contains 10 elements of np.int32 containing values 0-9
// we know the file contains 10 elements of np.int32 containing values 0-9
REQUIRE(f.dtype() == DType::INT32);
REQUIRE(f.shape() == std::vector<size_t>{10});
//use the load function to read the full file into a NDArray
auto data = f.load<int32_t,1>();
for(size_t i = 0; i < 10; i++){
// use the load function to read the full file into a NDArray
auto data = f.load<int32_t, 1>();
for (size_t i = 0; i < 10; i++) {
REQUIRE(data(i) == i);
}
}
TEST_CASE("Read a 3D numpy file with np.double data type"){
TEST_CASE("Read a 3D numpy file with np.double data type") {
auto fpath = test_data_path() / "numpy" / "test_3d_double.npy";
REQUIRE(std::filesystem::exists(fpath));
NumpyFile f(fpath);
//we know the file contains 10 elements of np.int32 containing values 0-9
// we know the file contains 10 elements of np.int32 containing values 0-9
REQUIRE(f.dtype() == DType::DOUBLE);
REQUIRE(f.shape() == std::vector<size_t>{3,2,5});
//use the load function to read the full file into a NDArray
//numpy code to generate the array
// arr2[0,0,0] = 1.0
// arr2[0,0,1] = 2.0
// arr2[0,1,0] = 72.0
// arr2[2,0,4] = 63.0
auto data = f.load<double,3>();
REQUIRE(data(0,0,0) == 1.0);
REQUIRE(data(0,0,1) == 2.0);
REQUIRE(data(0,1,0) == 72.0);
REQUIRE(data(2,0,4) == 63.0);
REQUIRE(f.shape() == std::vector<size_t>{3, 2, 5});
// use the load function to read the full file into a NDArray
// numpy code to generate the array
// arr2[0,0,0] = 1.0
// arr2[0,0,1] = 2.0
// arr2[0,1,0] = 72.0
// arr2[2,0,4] = 63.0
auto data = f.load<double, 3>();
REQUIRE(data(0, 0, 0) == 1.0);
REQUIRE(data(0, 0, 1) == 2.0);
REQUIRE(data(0, 1, 0) == 72.0);
REQUIRE(data(2, 0, 4) == 63.0);
}

View File

@ -1,9 +1,9 @@
#include <catch2/catch_test_macros.hpp>
#include "aare/NumpyHelpers.hpp" //Is this really a public header?
#include <catch2/catch_test_macros.hpp>
using namespace aare::NumpyHelpers;
using namespace aare::NumpyHelpers;
TEST_CASE("is_digits with a few standard cases"){
TEST_CASE("is_digits with a few standard cases") {
REQUIRE(is_digits(""));
REQUIRE(is_digits("123"));
REQUIRE(is_digits("0"));
@ -13,17 +13,15 @@ TEST_CASE("is_digits with a few standard cases"){
REQUIRE_FALSE(is_digits("abcdef"));
}
TEST_CASE("Check for quotes and return stripped string"){
REQUIRE(parse_str("'hej'") == "hej");
REQUIRE(parse_str("'hej hej'") == "hej hej");
REQUIRE(parse_str("''") == "");
TEST_CASE("Check for quotes and return stripped string") {
REQUIRE(parse_str("'hej'") == "hej");
REQUIRE(parse_str("'hej hej'") == "hej hej");
REQUIRE(parse_str("''") == "");
}
TEST_CASE("parsing a string without quotes throws"){
REQUIRE_THROWS(parse_str("hej"));
}
TEST_CASE("parsing a string without quotes throws") { REQUIRE_THROWS(parse_str("hej")); }
TEST_CASE("trim whitespace"){
TEST_CASE("trim whitespace") {
REQUIRE(trim(" hej ") == "hej");
REQUIRE(trim("hej") == "hej");
REQUIRE(trim(" hej") == "hej");
@ -32,7 +30,7 @@ TEST_CASE("trim whitespace"){
REQUIRE(trim(" \thej hej ") == "hej hej");
}
TEST_CASE("parse data type descriptions"){
TEST_CASE("parse data type descriptions") {
REQUIRE(parse_descr("<i1") == aare::DType::INT8);
REQUIRE(parse_descr("<i2") == aare::DType::INT16);
REQUIRE(parse_descr("<i4") == aare::DType::INT32);
@ -47,14 +45,14 @@ TEST_CASE("parse data type descriptions"){
REQUIRE(parse_descr("<f8") == aare::DType::DOUBLE);
}
TEST_CASE("is element in array"){
REQUIRE(in_array(1, std::array<int, 3>{1,2,3}));
REQUIRE_FALSE(in_array(4, std::array<int, 3>{1,2,3}));
TEST_CASE("is element in array") {
REQUIRE(in_array(1, std::array<int, 3>{1, 2, 3}));
REQUIRE_FALSE(in_array(4, std::array<int, 3>{1, 2, 3}));
REQUIRE(in_array(1, std::array<int, 1>{1}));
REQUIRE_FALSE(in_array(1, std::array<int, 0>{}));
}
TEST_CASE("Parse numpy dict"){
TEST_CASE("Parse numpy dict") {
std::string in = "{'descr': '<f4', 'fortran_order': False, 'shape': (3, 4)}";
std::vector<std::string> keys{"descr", "fortran_order", "shape"};
auto map = parse_dict(in, keys);