From b1b020ad60ee41ade1efbb82a39ad67afae25319 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Fr=C3=B6jdh?= Date: Fri, 25 Oct 2024 10:23:34 +0200 Subject: [PATCH] WIP --- CMakeLists.txt | 255 ++++++++++++++++++++++++++ README.md | 13 ++ cmake/helpers.cmake | 6 + include/aare/Dtype.hpp | 83 +++++++++ include/aare/Frame.hpp | 76 ++++++++ include/aare/NDArray.hpp | 380 +++++++++++++++++++++++++++++++++++++++ include/aare/NDView.hpp | 159 ++++++++++++++++ include/aare/defs.hpp | 156 ++++++++++++++++ src/CMakeLists.txt | 33 ++++ src/Dtype.cpp | 191 ++++++++++++++++++++ src/Dtype.test.cpp | 54 ++++++ src/Frame.cpp | 110 ++++++++++++ src/Frame.test.cpp | 152 ++++++++++++++++ src/NDArray.test.cpp | 377 ++++++++++++++++++++++++++++++++++++++ src/NDView.test.cpp | 193 ++++++++++++++++++++ src/defs.cpp | 65 +++++++ src/defs.test.cpp | 42 +++++ tests/CMakeLists.txt | 44 +++++ tests/test.cpp | 21 +++ tests/test_config.hpp.in | 7 + 20 files changed, 2417 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 cmake/helpers.cmake create mode 100644 include/aare/Dtype.hpp create mode 100644 include/aare/Frame.hpp create mode 100644 include/aare/NDArray.hpp create mode 100644 include/aare/NDView.hpp create mode 100644 include/aare/defs.hpp create mode 100644 src/CMakeLists.txt create mode 100644 src/Dtype.cpp create mode 100644 src/Dtype.test.cpp create mode 100644 src/Frame.cpp create mode 100644 src/Frame.test.cpp create mode 100644 src/NDArray.test.cpp create mode 100644 src/NDView.test.cpp create mode 100644 src/defs.cpp create mode 100644 src/defs.test.cpp create mode 100644 tests/CMakeLists.txt create mode 100644 tests/test.cpp create mode 100644 tests/test_config.hpp.in diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..f169113 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,255 @@ +cmake_minimum_required(VERSION 3.12) + +set(CMAKE_CXX_STANDARD 17) #TODO! Global or per target? +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +project(aare + VERSION 0.1 + DESCRIPTION "Data processing library for PSI detectors" + HOMEPAGE_URL "https://github.com/slsdetectorgroup/aare" + LANGUAGES C CXX +) + +cmake_policy(SET CMP0135 NEW) +cmake_policy(SET CMP0079 NEW) + +include(GNUInstallDirs) +include(FetchContent) + +#Set default build type if none was specified +include(cmake/helpers.cmake) +default_build_type("Release") + +option(AARE_USE_WARNINGS "Enable warnings" ON) +option(AARE_PYTHON_BINDINGS "Build python bindings" ON) +option(AARE_TESTS "Build tests" ON) +option(AARE_EXAMPLES "Build examples" ON) +option(AARE_IN_GITHUB_ACTIONS "Running in Github Actions" OFF) + +option(AARE_FETCH_FMT "Use FetchContent to download fmt" ON) +option(AARE_FETCH_PYBIND11 "Use FetchContent to download pybind11" ON) +option(AARE_FETCH_CATCH "Use FetchContent to download catch2" ON) +option(AARE_FETCH_JSON "Use FetchContent to download nlohmann::json" ON) +option(AARE_FETCH_ZMQ "Use FetchContent to download libzmq" ON) +option(ENABLE_DRAFTS "Enable zmq drafts (depends on gnutls or nss)" OFF) + + +#Convenience option to use system libraries +option(AARE_SYSTEM_LIBRARIES "Use system libraries" OFF) +if(AARE_SYSTEM_LIBRARIES) + message(STATUS "Build using system libraries") + set(AARE_FETCH_FMT OFF CACHE BOOL "Disabled FetchContent for FMT" FORCE) + set(AARE_FETCH_PYBIND11 OFF CACHE BOOL "Disabled FetchContent for pybind11" FORCE) + set(AARE_FETCH_CATCH OFF CACHE BOOL "Disabled FetchContent for catch2" FORCE) + set(AARE_FETCH_JSON OFF CACHE BOOL "Disabled FetchContent for nlohmann::json" FORCE) + set(AARE_FETCH_ZMQ OFF CACHE BOOL "Disabled FetchContent for libzmq" FORCE) + +endif() + + +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +if(AARE_FETCH_ZMQ) + + FetchContent_Declare( + libzmq + GIT_REPOSITORY https://github.com/zeromq/libzmq.git + GIT_TAG v4.3.4 + ) + # TODO! Verify that this is what we want to do in aare + # Using GetProperties and Populate to be able to exclude zmq + # from install (not possible with FetchContent_MakeAvailable(libzmq)) + FetchContent_GetProperties(libzmq) + if(NOT libzmq_POPULATED) + FetchContent_Populate(libzmq) + add_subdirectory(${libzmq_SOURCE_DIR} ${libzmq_BINARY_DIR} EXCLUDE_FROM_ALL) + endif() + +else() + find_package(ZeroMQ 4 REQUIRED) +endif() + + +if (AARE_FETCH_FMT) + set(FMT_TEST OFF CACHE INTERNAL "disabling fmt tests") + FetchContent_Declare( + fmt + GIT_REPOSITORY https://github.com/fmtlib/fmt.git + GIT_TAG 10.2.1 + GIT_PROGRESS TRUE + USES_TERMINAL_DOWNLOAD TRUE + ) + FetchContent_MakeAvailable(fmt) + set_property(TARGET fmt PROPERTY POSITION_INDEPENDENT_CODE ON) +else() + find_package(fmt 6 REQUIRED) +endif() + + +add_library(aare_compiler_flags INTERFACE) +target_compile_features(aare_compiler_flags INTERFACE cxx_std_17) + +################# +# MSVC specific # +################# +if(MSVC) +add_compile_definitions(AARE_MSVC) +if(CMAKE_BUILD_TYPE STREQUAL "Release") + message(STATUS "Release build") + target_compile_options(aare_compiler_flags INTERFACE /O2) +else() + message(STATUS "Debug build") + target_compile_options( + aare_compiler_flags + INTERFACE + /Od + /Zi + /MDd + /D_ITERATOR_DEBUG_LEVEL=2 + ) + target_link_options( + aare_compiler_flags + INTERFACE + /DEBUG:FULL + ) +endif() +target_compile_options( + aare_compiler_flags + INTERFACE + /w # disable warnings +) + + +else() +###################### +# GCC/Clang specific # +###################### + +if(CMAKE_BUILD_TYPE STREQUAL "Release") + message(STATUS "Release build") + target_compile_options(aare_compiler_flags INTERFACE -O3) +else() + message(STATUS "Debug build") + target_compile_options( + aare_compiler_flags + INTERFACE + -Og + -ggdb3 + # -D_GLIBCXX_DEBUG # causes errors with boost + -D_GLIBCXX_DEBUG_PEDANTIC + ) + + if (NOT AARE_PYTHON_BINDINGS) + target_compile_options( + aare_compiler_flags + INTERFACE + -fdiagnostics-parseable-fixits + # -fdiagnostics-generate-patch + -fdiagnostics-show-template-tree + -fsanitize=address,undefined,pointer-compare + -fno-sanitize-recover + # -D_FORTIFY_SOURCE=2 # not needed for debug builds + # -fstack-protector # cause errors wih folly? (ProducerConsumerQueue.hpp) + -fno-omit-frame-pointer + ) + + target_link_libraries( + aare_compiler_flags + INTERFACE + -fdiagnostics-parseable-fixits + # -fdiagnostics-generate-patch + -fdiagnostics-show-template-tree + -fsanitize=address,undefined,pointer-compare + -fno-sanitize-recover + # -D_FORTIFY_SOURCE=2 + -fno-omit-frame-pointer + ) + endif() + +endif() + +if(AARE_USE_WARNINGS) + target_compile_options( + aare_compiler_flags + INTERFACE + -Wall + -Wextra + -pedantic + -Wshadow + -Wnon-virtual-dtor + -Woverloaded-virtual + -Wdouble-promotion + -Wformat=2 + -Wredundant-decls + -Wvla + -Wdouble-promotion + -Werror=return-type #important can cause segfault in optimzed builds + ) +endif() + + + +endif() #GCC/Clang specific + + + + + + + + +if(AARE_TESTS) + enable_testing() + add_subdirectory(tests) +endif() + +add_subdirectory(src) + + +#Overall target to link to when using the library +add_library(aare INTERFACE) +target_link_libraries(aare INTERFACE aare_core aare_compiler_flags) +target_include_directories(aare INTERFACE + $ + $ +) + +# add_subdirectory(examples) + + + + +# custom target to run check formatting with clang-format +add_custom_target( + check-format + COMMAND find \( -name "*.cpp" -o -name "*.hpp" \) -not -path "./build/*" | xargs -I {} -n 1 -P 10 bash -c "clang-format -Werror -style=\"file:.clang-format\" {} | diff {} -" + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + COMMENT "Checking code formatting with clang-format" + VERBATIM + +) + +add_custom_target( + format-files + COMMAND find \( -name "*.cpp" -o -name "*.hpp" \) -not -path "./build/*" | xargs -I {} -n 1 -P 10 bash -c "clang-format -i -style=\"file:.clang-format\" {}" + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + COMMENT "Formatting with clang-format" + VERBATIM +) + +if (AARE_IN_GITHUB_ACTIONS) + message(STATUS "Running in Github Actions") + set(CLANG_TIDY_COMMAND "clang-tidy-17") +else() + set(CLANG_TIDY_COMMAND "clang-tidy") +endif() + +add_custom_target( + clang-tidy + COMMAND find \( -path "./src/*" -a -not -path "./src/python/*" -a \( -name "*.cpp" -not -name "*.test.cpp"\) \) -not -name "CircularFifo.hpp" -not -name "ProducerConsumerQueue.hpp" -not -name "VariableSizeClusterFinder.hpp" | xargs -I {} -n 1 -P 10 bash -c "${CLANG_TIDY_COMMAND} --config-file=.clang-tidy -p build {}" + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + COMMENT "linting with clang-tidy" + VERBATIM +) + diff --git a/README.md b/README.md index b7ee44c..765e4c5 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,15 @@ # aare Data analysis library for PSI hybrid detectors + + + +## Project structure + +include/aare - public headers + + +## Open questions + +- How many sub libraries? +- Where to place test data? This data is also needed for github actions... +- What to return to numpy? Our NDArray or a numpy ndarray? Lifetime? \ No newline at end of file diff --git a/cmake/helpers.cmake b/cmake/helpers.cmake new file mode 100644 index 0000000..2a6dfb7 --- /dev/null +++ b/cmake/helpers.cmake @@ -0,0 +1,6 @@ +function(default_build_type val) +if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + message(STATUS "No build type selected, default to Release") + set(CMAKE_BUILD_TYPE ${val} CACHE STRING "Build type (default ${val})" FORCE) +endif() +endfunction() diff --git a/include/aare/Dtype.hpp b/include/aare/Dtype.hpp new file mode 100644 index 0000000..7e1e62a --- /dev/null +++ b/include/aare/Dtype.hpp @@ -0,0 +1,83 @@ +#pragma once +#include +#include +#include +#include + +namespace aare { + +// The format descriptor is a single character that specifies the type of the data +// - python documentation: https://docs.python.org/3/c-api/arg.html#numbers +// - py::format_descriptor::format() (in pybind11) does not return the same format as +// written in python.org documentation. +// - numpy also doesn't use the same format. and also numpy associates the format +// with variable bitdepth types. (e.g. long is int64 on linux64 and int32 on win64) +// https://numpy.org/doc/stable/reference/arrays.scalars.html +// +// github issue discussing this: +// https://github.com/pybind/pybind11/issues/1908#issuecomment-658358767 +// +// [IN LINUX] the difference is for int64 (long) and uint64 (unsigned long). The format +// descriptor is 'q' and 'Q' respectively and in the documentation it is 'l' and 'k'. + +// in practice numpy doesn't seem to care when reading buffer info: the library +// interprets 'q' or 'l' as int64 and 'Q' or 'L' as uint64. +// for this reason we decided to use the same format descriptor as pybind to avoid +// any further discrepancies. + +// in the following order: +// int8, uint8, int16, uint16, int32, uint32, int64, uint64, float, double +const char DTYPE_FORMAT_DSC[] = {'b', 'B', 'h', 'H', 'i', 'I', 'q', 'Q', 'f', 'd'}; + +// on linux64 & apple +const char NUMPY_FORMAT_DSC[] = {'b', 'B', 'h', 'H', 'i', 'I', 'l', 'L', 'f', 'd'}; +/** + * @brief enum class to define the endianess of the system + */ +enum class endian { +#ifdef _WIN32 + little = 0, + big = 1, + native = little +#else + little = __ORDER_LITTLE_ENDIAN__, + big = __ORDER_BIG_ENDIAN__, + native = __BYTE_ORDER__ +#endif +}; + +/** + * @brief class to define the data type of the pixels + * @note only native endianess is supported + */ +class Dtype { + public: + enum TypeIndex { INT8, UINT8, INT16, UINT16, INT32, UINT32, INT64, UINT64, FLOAT, DOUBLE, ERROR, NONE }; + + uint8_t bitdepth() const; + size_t bytes() const; + std::string format_descr() const { return std::string(1, DTYPE_FORMAT_DSC[static_cast(m_type)]); } + std::string numpy_descr() const { return std::string(1, NUMPY_FORMAT_DSC[static_cast(m_type)]); } + + explicit Dtype(const std::type_info &t); + explicit Dtype(std::string_view sv); + static Dtype from_bitdepth(uint8_t bitdepth); + + // not explicit to allow conversions form enum to DType + Dtype(Dtype::TypeIndex ti); // NOLINT + + bool operator==(const Dtype &other) const noexcept; + bool operator!=(const Dtype &other) const noexcept; + bool operator==(const std::type_info &t) const; + bool operator!=(const std::type_info &t) const; + + // bool operator==(DType::TypeIndex ti) const; + // bool operator!=(DType::TypeIndex ti) const; + std::string to_string() const; + void set_type(Dtype::TypeIndex ti) { m_type = ti; } + + private: + TypeIndex m_type{TypeIndex::ERROR}; +}; + +} // namespace aare \ No newline at end of file diff --git a/include/aare/Frame.hpp b/include/aare/Frame.hpp new file mode 100644 index 0000000..88e4ab2 --- /dev/null +++ b/include/aare/Frame.hpp @@ -0,0 +1,76 @@ +#pragma once +#include "aare/Dtype.hpp" +#include "aare/NDArray.hpp" +#include "aare/defs.hpp" + +#include +#include +#include +#include + +namespace aare { + +/** + * @brief Frame class to represent a single frame of data + * model class + * should be able to work with streams coming from files or network + */ +class Frame { + uint32_t m_rows; + uint32_t m_cols; + Dtype m_dtype; + std::byte *m_data; + + public: + Frame(uint32_t rows, uint32_t cols, Dtype dtype); + Frame(const std::byte *bytes, uint32_t rows, uint32_t cols, Dtype dtype); + ~Frame() noexcept; + + // disable copy and assignment + Frame &operator=(const Frame &other)=delete; + Frame(const Frame &other)=delete; + + // enable move + Frame &operator=(Frame &&other) noexcept; + Frame(Frame &&other) noexcept; + + // explicit copy + Frame copy() const; + + uint32_t rows() const; + uint32_t cols() const; + size_t bitdepth() const; + Dtype dtype() const; + uint64_t size() const; + size_t bytes() const; + std::byte *data() const; + + std::byte *get(uint32_t row, uint32_t col); + + // TODO! can we, or even want to remove the template? + template void set(uint32_t row, uint32_t col, T data) { + assert(sizeof(T) == m_dtype.bytes()); + if (row >= m_rows || col >= m_cols) { + throw std::out_of_range("Invalid row or column index"); + } + std::memcpy(m_data + (row * m_cols + col) * m_dtype.bytes(), &data, m_dtype.bytes()); + } + template T get_t(uint32_t row, uint32_t col) { + assert(sizeof(T) == m_dtype.bytes()); + if (row >= m_rows || col >= m_cols) { + throw std::out_of_range("Invalid row or column index"); + } + T data; + std::memcpy(&data, m_data + (row * m_cols + col) * m_dtype.bytes(), m_dtype.bytes()); + return data; + } + template NDView view() { + std::array shape = {static_cast(m_rows), static_cast(m_cols)}; + T *data = reinterpret_cast(m_data); + return NDView(data, shape); + } + + template NDArray image() { return NDArray(this->view()); } +}; + +} // namespace aare \ No newline at end of file diff --git a/include/aare/NDArray.hpp b/include/aare/NDArray.hpp new file mode 100644 index 0000000..0a54091 --- /dev/null +++ b/include/aare/NDArray.hpp @@ -0,0 +1,380 @@ +#pragma once +/* +Container holding image data, or a time series of image data in contigious +memory. + + +TODO! Add expression templates for operators + +*/ +#include "aare/NDView.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace aare { + +template class NDArray { + public: + NDArray() : shape_(), strides_(c_strides(shape_)), data_(nullptr){}; + + explicit NDArray(std::array shape) + : shape_(shape), strides_(c_strides(shape_)), + size_(std::accumulate(shape_.begin(), shape_.end(), 1, std::multiplies<>())), data_(new T[size_]){}; + + NDArray(std::array shape, T value) : NDArray(shape) { this->operator=(value); } + + /* When constructing from a NDView we need to copy the data since + NDArray expect to own its data, and span is just a view*/ + explicit NDArray(NDView span) : NDArray(span.shape()) { + std::copy(span.begin(), span.end(), begin()); + // fmt::print("NDArray(NDView span)\n"); + } + + // Move constructor + NDArray(NDArray &&other) noexcept + : shape_(other.shape_), strides_(c_strides(shape_)), size_(other.size_), data_(other.data_) { + + other.reset(); + // fmt::print("NDArray(NDArray &&other)\n"); + } + + // Copy constructor + NDArray(const NDArray &other) + : shape_(other.shape_), strides_(c_strides(shape_)), size_(other.size_), data_(new T[size_]) { + std::copy(other.data_, other.data_ + size_, data_); + // fmt::print("NDArray(const NDArray &other)\n"); + } + + ~NDArray() { delete[] data_; } + + auto begin() { return data_; } + auto end() { return data_ + size_; } + + using value_type = T; + + NDArray &operator=(NDArray &&other) noexcept; // Move assign + NDArray &operator=(const NDArray &other); // Copy assign + + NDArray operator+(const NDArray &other); + NDArray &operator+=(const NDArray &other); + NDArray operator-(const NDArray &other); + NDArray &operator-=(const NDArray &other); + NDArray operator*(const NDArray &other); + NDArray &operator*=(const NDArray &other); + NDArray operator/(const NDArray &other); + // NDArray& operator/=(const NDArray& other); + template NDArray &operator/=(const NDArray &other) { + // check shape + if (shape_ == other.shape()) { + for (uint32_t i = 0; i < size_; ++i) { + data_[i] /= other(i); + } + return *this; + } + throw(std::runtime_error("Shape of NDArray must match")); + } + + NDArray operator>(const NDArray &other); + + bool operator==(const NDArray &other) const; + bool operator!=(const NDArray &other) const; + + NDArray &operator=(const T & /*value*/); + NDArray &operator+=(const T & /*value*/); + NDArray operator+(const T & /*value*/); + NDArray &operator-=(const T & /*value*/); + NDArray operator-(const T & /*value*/); + NDArray &operator*=(const T & /*value*/); + NDArray operator*(const T & /*value*/); + NDArray &operator/=(const T & /*value*/); + NDArray operator/(const T & /*value*/); + + NDArray &operator&=(const T & /*mask*/); + + void sqrt() { + for (int i = 0; i < size_; ++i) { + data_[i] = std::sqrt(data_[i]); + } + } + + NDArray &operator++(); // pre inc + + template std::enable_if_t operator()(Ix... index) { + return data_[element_offset(strides_, index...)]; + } + + template std::enable_if_t operator()(Ix... index) const { + return data_[element_offset(strides_, index...)]; + } + + template std::enable_if_t value(Ix... index) { + return data_[element_offset(strides_, index...)]; + } + + T &operator()(int i) { return data_[i]; } + const T &operator()(int i) const { return data_[i]; } + + T *data() { return data_; } + std::byte *buffer() { return reinterpret_cast(data_); } + uint64_t size() const { return size_; } + size_t total_bytes() const { return size_ * sizeof(T); } + std::array shape() const noexcept { return shape_; } + int64_t shape(int64_t i) const noexcept { return shape_[i]; } + std::array strides() const noexcept { return strides_; } + size_t bitdepth() const noexcept { return sizeof(T) * 8; } + std::array byte_strides() const noexcept { + auto byte_strides = strides_; + for (auto &val : byte_strides) + val *= sizeof(T); + return byte_strides; + // return strides_; + } + + NDView span() const { return NDView{data_, shape_}; } + + void Print(); + void Print_all(); + void Print_some(); + + void reset() { + data_ = nullptr; + size_ = 0; + std::fill(shape_.begin(), shape_.end(), 0); + std::fill(strides_.begin(), strides_.end(), 0); + } + + private: + std::array shape_; + std::array strides_; + uint64_t size_{}; + T *data_; +}; + +// Move assign +template NDArray &NDArray::operator=(NDArray &&other) noexcept { + if (this != &other) { + delete[] data_; + data_ = other.data_; + shape_ = other.shape_; + size_ = other.size_; + strides_ = other.strides_; + other.reset(); + } + return *this; +} + +template NDArray NDArray::operator+(const NDArray &other) { + NDArray result(*this); + result += other; + return result; +} +template NDArray &NDArray::operator+=(const NDArray &other) { + // check shape + if (shape_ == other.shape_) { + for (uint32_t i = 0; i < size_; ++i) { + data_[i] += other.data_[i]; + } + return *this; + } + throw(std::runtime_error("Shape of ImageDatas must match")); +} + +template NDArray NDArray::operator-(const NDArray &other) { + NDArray result{*this}; + result -= other; + return result; +} + +template NDArray &NDArray::operator-=(const NDArray &other) { + // check shape + if (shape_ == other.shape_) { + for (uint32_t i = 0; i < size_; ++i) { + data_[i] -= other.data_[i]; + } + return *this; + } + throw(std::runtime_error("Shape of ImageDatas must match")); +} +template NDArray NDArray::operator*(const NDArray &other) { + NDArray result = *this; + result *= other; + return result; +} + +template NDArray &NDArray::operator*=(const NDArray &other) { + // check shape + if (shape_ == other.shape_) { + for (uint32_t i = 0; i < size_; ++i) { + data_[i] *= other.data_[i]; + } + return *this; + } + throw(std::runtime_error("Shape of ImageDatas must match")); +} + +template NDArray NDArray::operator/(const NDArray &other) { + NDArray result = *this; + result /= other; + return result; +} + +template NDArray &NDArray::operator&=(const T &mask) { + for (auto it = begin(); it != end(); ++it) + *it &= mask; + return *this; +} + +// template +// NDArray& NDArray::operator/=(const NDArray& +// other) +// { +// //check shape +// if (shape_ == other.shape_) { +// for (int i = 0; i < size_; ++i) { +// data_[i] /= other.data_[i]; +// } +// return *this; +// } else { +// throw(std::runtime_error("Shape of ImageDatas must match")); +// } +// } + +template NDArray NDArray::operator>(const NDArray &other) { + if (shape_ == other.shape_) { + NDArray result{shape_}; + for (int i = 0; i < size_; ++i) { + result(i) = (data_[i] > other.data_[i]); + } + return result; + } + throw(std::runtime_error("Shape of ImageDatas must match")); +} + +template NDArray &NDArray::operator=(const NDArray &other) { + if (this != &other) { + delete[] data_; + shape_ = other.shape_; + strides_ = other.strides_; + size_ = other.size_; + data_ = new T[size_]; + std::copy(other.data_, other.data_ + size_, data_); + } + return *this; +} + +template bool NDArray::operator==(const NDArray &other) const { + if (shape_ != other.shape_) + return false; + + for (uint32_t i = 0; i != size_; ++i) + if (data_[i] != other.data_[i]) + return false; + + return true; +} + +template bool NDArray::operator!=(const NDArray &other) const { + return !((*this) == other); +} +template NDArray &NDArray::operator++() { + for (uint32_t i = 0; i < size_; ++i) + data_[i] += 1; + return *this; +} +template NDArray &NDArray::operator=(const T &value) { + std::fill_n(data_, size_, value); + return *this; +} + +template NDArray &NDArray::operator+=(const T &value) { + for (uint32_t i = 0; i < size_; ++i) + data_[i] += value; + return *this; +} + +template NDArray NDArray::operator+(const T &value) { + NDArray result = *this; + result += value; + return result; +} +template NDArray &NDArray::operator-=(const T &value) { + for (uint32_t i = 0; i < size_; ++i) + data_[i] -= value; + return *this; +} +template NDArray NDArray::operator-(const T &value) { + NDArray result = *this; + result -= value; + return result; +} + +template NDArray &NDArray::operator/=(const T &value) { + for (uint32_t i = 0; i < size_; ++i) + data_[i] /= value; + return *this; +} +template NDArray NDArray::operator/(const T &value) { + NDArray result = *this; + result /= value; + return result; +} +template NDArray &NDArray::operator*=(const T &value) { + for (uint32_t i = 0; i < size_; ++i) + data_[i] *= value; + return *this; +} +template NDArray NDArray::operator*(const T &value) { + NDArray result = *this; + result *= value; + return result; +} +template void NDArray::Print() { + if (shape_[0] < 20 && shape_[1] < 20) + Print_all(); + else + Print_some(); +} +template void NDArray::Print_all() { + for (auto row = 0; row < shape_[0]; ++row) { + for (auto col = 0; col < shape_[1]; ++col) { + std::cout << std::setw(3); + std::cout << (*this)(row, col) << " "; + } + std::cout << "\n"; + } +} +template void NDArray::Print_some() { + for (auto row = 0; row < 5; ++row) { + for (auto col = 0; col < 5; ++col) { + std::cout << std::setw(7); + std::cout << (*this)(row, col) << " "; + } + std::cout << "\n"; + } +} + +template void save(NDArray &img, std::string &pathname) { + std::ofstream f; + f.open(pathname, std::ios::binary); + f.write(img.buffer(), img.size() * sizeof(T)); + f.close(); +} + +template +NDArray load(const std::string &pathname, std::array shape) { + NDArray img{shape}; + std::ifstream f; + f.open(pathname, std::ios::binary); + f.read(img.buffer(), img.size() * sizeof(T)); + f.close(); + return img; +} + +} // namespace aare \ No newline at end of file diff --git a/include/aare/NDView.hpp b/include/aare/NDView.hpp new file mode 100644 index 0000000..8a73038 --- /dev/null +++ b/include/aare/NDView.hpp @@ -0,0 +1,159 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +namespace aare { + +template using Shape = std::array; + +// TODO! fix mismatch between signed and unsigned +template Shape make_shape(const std::vector &shape) { + if (shape.size() != Ndim) + throw std::runtime_error("Shape size mismatch"); + Shape arr; + std::copy_n(shape.begin(), Ndim, arr.begin()); + return arr; +} + +template int64_t element_offset(const Strides & /*unused*/) { return 0; } + +template +int64_t element_offset(const Strides &strides, int64_t i, Ix... index) { + return i * strides[Dim] + element_offset(strides, index...); +} + +template std::array c_strides(const std::array &shape) { + std::array strides{}; + std::fill(strides.begin(), strides.end(), 1); + for (int64_t i = Ndim - 1; i > 0; --i) { + strides[i - 1] = strides[i] * shape[i]; + } + return strides; +} + +template std::array make_array(const std::vector &vec) { + assert(vec.size() == Ndim); + std::array arr{}; + std::copy_n(vec.begin(), Ndim, arr.begin()); + return arr; +} + +template class NDView { + public: + NDView() = default; + ~NDView() = default; + NDView(const NDView &) = default; + NDView(NDView &&) = default; + + NDView(T *buffer, std::array shape) + : buffer_(buffer), strides_(c_strides(shape)), shape_(shape), + size_(std::accumulate(std::begin(shape), std::end(shape), 1, std::multiplies<>())) {} + + // NDView(T *buffer, const std::vector &shape) + // : buffer_(buffer), strides_(c_strides(make_array(shape))), shape_(make_array(shape)), + // size_(std::accumulate(std::begin(shape), std::end(shape), 1, std::multiplies<>())) {} + + template std::enable_if_t operator()(Ix... index) { + return buffer_[element_offset(strides_, index...)]; + } + + template std::enable_if_t operator()(Ix... index) const { + return buffer_[element_offset(strides_, index...)]; + } + + uint64_t size() const { return size_; } + size_t total_bytes() const { return size_ * sizeof(T); } + std::array strides() const noexcept { return strides_; } + + T *begin() { return buffer_; } + T *end() { return buffer_ + size_; } + T &operator()(int64_t i) const { return buffer_[i]; } + T &operator[](int64_t i) const { return buffer_[i]; } + + bool operator==(const NDView &other) const { + if (size_ != other.size_) + return false; + for (uint64_t i = 0; i != size_; ++i) { + if (buffer_[i] != other.buffer_[i]) + return false; + } + return true; + } + + NDView &operator+=(const T val) { return elemenwise(val, std::plus()); } + NDView &operator-=(const T val) { return elemenwise(val, std::minus()); } + NDView &operator*=(const T val) { return elemenwise(val, std::multiplies()); } + NDView &operator/=(const T val) { return elemenwise(val, std::divides()); } + + NDView &operator/=(const NDView &other) { return elemenwise(other, std::divides()); } + + NDView &operator=(const T val) { + for (auto it = begin(); it != end(); ++it) + *it = val; + return *this; + } + + NDView &operator=(const NDView &other) { + if (this == &other) + return *this; + shape_ = other.shape_; + strides_ = other.strides_; + size_ = other.size_; + buffer_ = other.buffer_; + return *this; + } + + NDView &operator=(NDView &&other) noexcept { + if (this == &other) + return *this; + shape_ = std::move(other.shape_); + strides_ = std::move(other.strides_); + size_ = other.size_; + buffer_ = other.buffer_; + other.buffer_ = nullptr; + return *this; + } + + auto &shape() { return shape_; } + auto shape(int64_t i) const { return shape_[i]; } + + T *data() { return buffer_; } + void print_all() const; + + private: + T *buffer_{nullptr}; + std::array strides_{}; + std::array shape_{}; + uint64_t size_{}; + + template NDView &elemenwise(T val, BinaryOperation op) { + for (uint64_t i = 0; i != size_; ++i) { + buffer_[i] = op(buffer_[i], val); + } + return *this; + } + template NDView &elemenwise(const NDView &other, BinaryOperation op) { + for (uint64_t i = 0; i != size_; ++i) { + buffer_[i] = op(buffer_[i], other.buffer_[i]); + } + return *this; + } +}; +template void NDView::print_all() const { + for (auto row = 0; row < shape_[0]; ++row) { + for (auto col = 0; col < shape_[1]; ++col) { + std::cout << std::setw(3); + std::cout << (*this)(row, col) << " "; + } + std::cout << "\n"; + } +} + +} // namespace aare \ No newline at end of file diff --git a/include/aare/defs.hpp b/include/aare/defs.hpp new file mode 100644 index 0000000..1db17f2 --- /dev/null +++ b/include/aare/defs.hpp @@ -0,0 +1,156 @@ +#pragma once + +#include "aare/Dtype.hpp" +// #include "aare/utils/logger.hpp" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +/** + * @brief LOCATION macro to get the current location in the code + */ +#define LOCATION std::string(__FILE__) + std::string(":") + std::to_string(__LINE__) + ":" + std::string(__func__) + ":" + + +namespace aare { + +class Cluster { + public: + int cluster_sizeX; + int cluster_sizeY; + int16_t x; + int16_t y; + Dtype dt; + + private: + std::byte *m_data; + + public: + Cluster(int cluster_sizeX_, int cluster_sizeY_, Dtype dt_ = Dtype(typeid(int32_t))) + : cluster_sizeX(cluster_sizeX_), cluster_sizeY(cluster_sizeY_), dt(dt_) { + m_data = new std::byte[cluster_sizeX * cluster_sizeY * dt.bytes()]{}; + } + Cluster() : Cluster(3, 3) {} + Cluster(const Cluster &other) : Cluster(other.cluster_sizeX, other.cluster_sizeY, other.dt) { + if (this == &other) + return; + x = other.x; + y = other.y; + memcpy(m_data, other.m_data, other.bytes()); + } + Cluster &operator=(const Cluster &other) { + if (this == &other) + return *this; + this->~Cluster(); + new (this) Cluster(other); + return *this; + } + Cluster(Cluster &&other) noexcept + : cluster_sizeX(other.cluster_sizeX), cluster_sizeY(other.cluster_sizeY), x(other.x), y(other.y), dt(other.dt), + m_data(other.m_data) { + other.m_data = nullptr; + other.dt = Dtype(Dtype::TypeIndex::ERROR); + } + ~Cluster() { delete[] m_data; } + template T get(int idx) { + (sizeof(T) == dt.bytes()) ? 0 : throw std::invalid_argument("[ERROR] Type size mismatch"); + return *reinterpret_cast(m_data + idx * dt.bytes()); + } + template auto set(int idx, T val) { + (sizeof(T) == dt.bytes()) ? 0 : throw std::invalid_argument("[ERROR] Type size mismatch"); + return memcpy(m_data + idx * dt.bytes(), &val, (size_t)dt.bytes()); + } + // auto x() const { return x; } + // auto y() const { return y; } + // auto x(int16_t x_) { return x = x_; } + // auto y(int16_t y_) { return y = y_; } + + template std::string to_string() const { + (sizeof(T) == dt.bytes()) ? 0 : throw std::invalid_argument("[ERROR] Type size mismatch"); + std::string s = "x: " + std::to_string(x) + " y: " + std::to_string(y) + "\nm_data: ["; + for (int i = 0; i < cluster_sizeX * cluster_sizeY; i++) { + s += std::to_string(*reinterpret_cast(m_data + i * dt.bytes())) + " "; + } + s += "]"; + return s; + } + /** + * @brief size of the cluster in bytes when saved to a file + */ + size_t size() const { return cluster_sizeX * cluster_sizeY ; } + size_t bytes() const { return cluster_sizeX * cluster_sizeY * dt.bytes(); } + auto begin() const { return m_data; } + auto end() const { return m_data + cluster_sizeX * cluster_sizeY * dt.bytes(); } + std::byte *data() { return m_data; } +}; + +/** + * @brief header contained in parts of frames + */ +struct sls_detector_header { + uint64_t frameNumber; + uint32_t expLength; + uint32_t packetNumber; + uint64_t bunchId; + uint64_t timestamp; + uint16_t modId; + uint16_t row; + uint16_t column; + uint16_t reserved; + uint32_t debug; + uint16_t roundRNumber; + uint8_t detType; + uint8_t version; + std::array packetMask; + std::string to_string() { + std::string packetMaskStr = "["; + for (auto &i : packetMask) { + packetMaskStr += std::to_string(i) + ", "; + } + packetMaskStr += "]"; + + return "frameNumber: " + std::to_string(frameNumber) + "\n" + "expLength: " + std::to_string(expLength) + "\n" + + "packetNumber: " + std::to_string(packetNumber) + "\n" + "bunchId: " + std::to_string(bunchId) + "\n" + + "timestamp: " + std::to_string(timestamp) + "\n" + "modId: " + std::to_string(modId) + "\n" + + "row: " + std::to_string(row) + "\n" + "column: " + std::to_string(column) + "\n" + + "reserved: " + std::to_string(reserved) + "\n" + "debug: " + std::to_string(debug) + "\n" + + "roundRNumber: " + std::to_string(roundRNumber) + "\n" + "detType: " + std::to_string(detType) + "\n" + + "version: " + std::to_string(version) + "\n" + "packetMask: " + packetMaskStr + "\n"; + } +}; + +template struct t_xy { + T row; + T col; + bool operator==(const t_xy &other) const { return row == other.row && col == other.col; } + bool operator!=(const t_xy &other) const { return !(*this == other); } + std::string to_string() const { return "{ x: " + std::to_string(row) + " y: " + std::to_string(col) + " }"; } +}; +using xy = t_xy; + +using dynamic_shape = std::vector; + +enum class DetectorType { Jungfrau, Eiger, Mythen3, Moench, ChipTestBoard, Unknown }; + +enum class TimingMode { Auto, Trigger }; + +template T StringTo(const std::string &arg) { return T(arg); } + +template std::string toString(T arg) { return T(arg); } + +template <> DetectorType StringTo(const std::string & /*name*/); +template <> std::string toString(DetectorType arg); + +template <> TimingMode StringTo(const std::string & /*mode*/); + +using DataTypeVariants = std::variant; + +} // namespace aare \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..e14b87e --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,33 @@ + + +set(SourceFiles + ${CMAKE_CURRENT_SOURCE_DIR}/defs.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/Dtype.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/Frame.cpp +) + + +add_library(aare_core STATIC ${SourceFiles}) +target_include_directories(aare_core PUBLIC ${CMAKE_SOURCE_DIR}/include) +target_link_libraries(aare_core PUBLIC fmt::fmt PRIVATE aare_compiler_flags ) + +if (AARE_PYTHON_BINDINGS) +set_property(TARGET aare_core PROPERTY POSITION_INDEPENDENT_CODE ON) +endif() + +if(AARE_TESTS) + set(TestSources + ${CMAKE_CURRENT_SOURCE_DIR}/defs.test.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/Dtype.test.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/Frame.test.cpp + # ${CMAKE_CURRENT_SOURCE_DIR}/test/ProducerConsumerQueue.test.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/NDArray.test.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/NDView.test.cpp + # ${CMAKE_CURRENT_SOURCE_DIR}/test/CircularFifo.test.cpp + # ${CMAKE_CURRENT_SOURCE_DIR}/test/wrappers.test.cpp + # ${CMAKE_CURRENT_SOURCE_DIR}/test/Transforms.test.cpp + + ) + target_sources(tests PRIVATE ${TestSources} ) + target_link_libraries(tests PRIVATE aare_core) +endif() \ No newline at end of file diff --git a/src/Dtype.cpp b/src/Dtype.cpp new file mode 100644 index 0000000..565d509 --- /dev/null +++ b/src/Dtype.cpp @@ -0,0 +1,191 @@ + +#include "aare/Dtype.hpp" +#include "aare/defs.hpp" + +#include + +namespace aare { + +/** + * @brief Construct a DType object from a type_info object + * @param t type_info object + * @throw runtime_error if the type is not supported + * @note supported types are: int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t, float, double + * @note the type_info object is obtained using typeid (e.g. typeid(int)) + */ +Dtype::Dtype(const std::type_info &t) { + if (t == typeid(int8_t)) + m_type = TypeIndex::INT8; + else if (t == typeid(uint8_t)) + m_type = TypeIndex::UINT8; + else if (t == typeid(int16_t)) + m_type = TypeIndex::INT16; + else if (t == typeid(uint16_t)) + m_type = TypeIndex::UINT16; + else if (t == typeid(int32_t)) + m_type = TypeIndex::INT32; + else if (t == typeid(uint32_t)) + m_type = TypeIndex::UINT32; + else if (t == typeid(int64_t)) // NOLINT + m_type = TypeIndex::INT64; + else if (t == typeid(uint64_t)) + m_type = TypeIndex::UINT64; + else if (t == typeid(float)) + m_type = TypeIndex::FLOAT; + else if (t == typeid(double)) + m_type = TypeIndex::DOUBLE; + else + throw std::runtime_error("Could not construct data type. Type not supported."); +} + +/** + * @brief Get the bitdepth of the data type + * @return bitdepth + */ +uint8_t Dtype::bitdepth() const { + switch (m_type) { + case TypeIndex::INT8: + case TypeIndex::UINT8: + return 8; + case TypeIndex::INT16: + case TypeIndex::UINT16: + return 16; + case TypeIndex::INT32: + case TypeIndex::UINT32: + return 32; + case TypeIndex::INT64: + case TypeIndex::UINT64: + return 64; + case TypeIndex::FLOAT: + return 32; + case TypeIndex::DOUBLE: + return 64; + case TypeIndex::NONE: + return 0; + default: + throw std::runtime_error(LOCATION + "Could not get bitdepth. Type not supported."); + } +} + +/** + * @brief Get the number of bytes of the data type + */ +size_t Dtype::bytes() const { return bitdepth() / 8; } + +/** + * @brief Construct a DType object from a TypeIndex + * @param ti TypeIndex + * + */ +Dtype::Dtype(Dtype::TypeIndex ti) : m_type(ti) {} + +/** + * @brief Construct a DType object from a string + * @param sv string_view + * @throw runtime_error if the type is not supported + * @note example strings: ""); pos != std::string_view::npos) { + const auto endianess = [](const char c) { + if (c == '<') + return endian::little; + return endian::big; + }(sv[pos]); + if (endianess != endian::native) { + throw std::runtime_error("Non native endianess not supported"); + } + } + + // we are done with the endianess so we can remove the prefix + sv.remove_prefix(std::min(sv.find_first_not_of("<>"), sv.size())); + + if (sv == "i1") + m_type = TypeIndex::INT8; + else if (sv == "u1") + m_type = TypeIndex::UINT8; + else if (sv == "i2") + m_type = TypeIndex::INT16; + else if (sv == "u2") + m_type = TypeIndex::UINT16; + else if (sv == "i4") + m_type = TypeIndex::INT32; + else if (sv == "u4") + m_type = TypeIndex::UINT32; + else if (sv == "i8") + m_type = TypeIndex::INT64; + else if (sv == "u8") + m_type = TypeIndex::UINT64; + else if (sv == "f4") + m_type = TypeIndex::FLOAT; + else if (sv == "f8") + m_type = TypeIndex::DOUBLE; + else + throw std::runtime_error("Cannot construct data type from string."); +} + +Dtype Dtype::from_bitdepth(uint8_t bitdepth) { + switch (bitdepth) { + case 8: + return Dtype(TypeIndex::UINT8); + case 16: + return Dtype(TypeIndex::UINT16); + case 32: + return Dtype(TypeIndex::UINT32); + case 64: + return Dtype(TypeIndex::UINT64); + default: + throw std::runtime_error("Could not construct data type from bitdepth."); + } +} +/** + * @brief Get the string representation of the data type + * @return string representation + */ +std::string Dtype::to_string() const { + + char ec{}; + if (endian::native == endian::little) + ec = '<'; + else + ec = '>'; + + switch (m_type) { + case TypeIndex::INT8: + return fmt::format("{}i1", ec); + case TypeIndex::UINT8: + return fmt::format("{}u1", ec); + case TypeIndex::INT16: + return fmt::format("{}i2", ec); + case TypeIndex::UINT16: + return fmt::format("{}u2", ec); + case TypeIndex::INT32: + return fmt::format("{}i4", ec); + case TypeIndex::UINT32: + return fmt::format("{}u4", ec); + case TypeIndex::INT64: + return fmt::format("{}i8", ec); + case TypeIndex::UINT64: + return fmt::format("{}u8", ec); + case TypeIndex::FLOAT: + return "f4"; + case TypeIndex::DOUBLE: + return "f8"; + case TypeIndex::ERROR: + throw std::runtime_error("Could not get string representation. Type not supported."); + case TypeIndex::NONE: + throw std::runtime_error("Could not get string representation. Type not supported."); + } + return {}; +} + +bool Dtype::operator==(const Dtype &other) const noexcept { return m_type == other.m_type; } +bool Dtype::operator!=(const Dtype &other) const noexcept { return !(*this == other); } + +bool Dtype::operator==(const std::type_info &t) const { return Dtype(t) == *this; } +bool Dtype::operator!=(const std::type_info &t) const { return Dtype(t) != *this; } + +} // namespace aare diff --git a/src/Dtype.test.cpp b/src/Dtype.test.cpp new file mode 100644 index 0000000..b252267 --- /dev/null +++ b/src/Dtype.test.cpp @@ -0,0 +1,54 @@ + + +#include "aare/Dtype.hpp" +#include + +using aare::Dtype; +using aare::endian; + +TEST_CASE("Construct from typeid") { + REQUIRE(Dtype(typeid(int)) == typeid(int)); + REQUIRE(Dtype(typeid(int)) != typeid(double)); +} + +TEST_CASE("Construct from string") { + if (endian::native == endian::little) { + REQUIRE(Dtype("i1") == typeid(int8_t)); + REQUIRE(Dtype(">u1") == typeid(uint8_t)); + REQUIRE(Dtype(">i2") == typeid(int16_t)); + REQUIRE(Dtype(">u2") == typeid(uint16_t)); + REQUIRE(Dtype(">i4") == typeid(int)); + REQUIRE(Dtype(">u4") == typeid(unsigned)); + REQUIRE(Dtype(">i4") == typeid(int32_t)); + // REQUIRE(Dtype(">i8") == typeid(long)); + REQUIRE(Dtype(">i8") == typeid(int64_t)); + REQUIRE(Dtype(">u4") == typeid(uint32_t)); + REQUIRE(Dtype(">u8") == typeid(uint64_t)); + REQUIRE(Dtype("f4") == typeid(float)); + REQUIRE(Dtype("f8") == typeid(double)); + } +} + +TEST_CASE("Construct from string with endianess") { + // TODO! handle big endian system in test! + REQUIRE(Dtype("i4") == typeid(int32_t)); +} + +TEST_CASE("Convert to string") { REQUIRE(Dtype(typeid(int)).to_string() == " +#include +#include +#include + +namespace aare { + +/** + * @brief Construct a new Frame + * @param bytes pointer to the data to be copied into the frame + * @param rows number of rows + * @param cols number of columns + * @param bitdepth bitdepth of the pixels + */ +Frame::Frame(const std::byte *bytes, uint32_t rows, uint32_t cols, Dtype dtype) + : m_rows(rows), m_cols(cols), m_dtype(dtype), m_data(new std::byte[rows * cols * m_dtype.bytes()]) { + + std::memcpy(m_data, bytes, rows * cols * m_dtype.bytes()); +} + +/** + * @brief Construct a new Frame + * @param rows number of rows + * @param cols number of columns + * @param bitdepth bitdepth of the pixels + * @note the data is initialized to zero + */ +Frame::Frame(uint32_t rows, uint32_t cols, Dtype dtype) + : m_rows(rows), m_cols(cols), m_dtype(dtype), m_data(new std::byte[rows * cols * dtype.bytes()]) { + + std::memset(m_data, 0, rows * cols * dtype.bytes()); +} + +uint32_t Frame::rows() const { return m_rows; } +uint32_t Frame::cols() const { return m_cols; } +size_t Frame::bitdepth() const { return m_dtype.bitdepth(); } +Dtype Frame::dtype() const { return m_dtype; } +uint64_t Frame::size() const { return m_rows * m_cols; } +size_t Frame::bytes() const { return m_rows * m_cols * m_dtype.bytes(); } +std::byte *Frame::data() const { return m_data; } + +/** + * @brief Get the pointer to the pixel at the given row and column + * @param row row index + * @param col column index + * @return pointer to the pixel + * @note the user should cast the pointer to the appropriate type + */ +std::byte *Frame::get(uint32_t row, uint32_t col) { + if ((row >= m_rows) || (col >= m_cols)) { + std::cerr << "Invalid row or column index" << '\n'; + return nullptr; + } + return m_data + (row * m_cols + col) * (m_dtype.bytes()); +} + +// Frame &Frame::operator=(const Frame &other) { +// if (this == &other) { +// return *this; +// } +// m_rows = other.rows(); +// m_cols = other.cols(); +// m_dtype = other.dtype(); +// m_data = new std::byte[m_rows * m_cols * m_dtype.bytes()]; +// if (m_data == nullptr) { +// throw std::bad_alloc(); +// } +// std::memcpy(m_data, other.m_data, m_rows * m_cols * m_dtype.bytes()); +// return *this; +// } +Frame &Frame::operator=(Frame &&other) noexcept { + if (this == &other) { + return *this; + } + m_rows = other.rows(); + m_cols = other.cols(); + m_dtype = other.dtype(); + if (m_data != nullptr) { + delete[] m_data; + } + m_data = other.m_data; + other.m_data = nullptr; + other.m_rows = other.m_cols = 0; + other.m_dtype = Dtype(Dtype::TypeIndex::ERROR); + return *this; +} +Frame::Frame(Frame &&other) noexcept + : m_rows(other.rows()), m_cols(other.cols()), m_dtype(other.dtype()), m_data(other.m_data) { + + other.m_data = nullptr; + other.m_rows = other.m_cols = 0; + other.m_dtype = Dtype(Dtype::TypeIndex::ERROR); +} +// Frame::Frame(const Frame &other) +// : m_rows(other.rows()), m_cols(other.cols()), m_dtype(other.dtype()), +// m_data(new std::byte[m_rows * m_cols * m_dtype.bytes()]) { + +// std::memcpy(m_data, other.m_data, m_rows * m_cols * m_dtype.bytes()); +// } + +Frame Frame::copy() const { + Frame frame(m_rows, m_cols, m_dtype); + std::memcpy(frame.m_data, m_data, m_rows * m_cols * m_dtype.bytes()); + return frame; +} + +Frame::~Frame() noexcept { delete[] m_data; } +} // namespace aare diff --git a/src/Frame.test.cpp b/src/Frame.test.cpp new file mode 100644 index 0000000..e8ce5d2 --- /dev/null +++ b/src/Frame.test.cpp @@ -0,0 +1,152 @@ +#include "aare/Frame.hpp" +#include "aare/Dtype.hpp" +#include + +using namespace aare; + +TEST_CASE("Construct a frame") { + size_t rows = 10; + size_t cols = 10; + size_t bitdepth = 8; + + Frame frame(rows, cols, Dtype::from_bitdepth(bitdepth)); + + REQUIRE(frame.rows() == rows); + REQUIRE(frame.cols() == cols); + REQUIRE(frame.bitdepth() == bitdepth); + REQUIRE(frame.bytes() == rows * cols * bitdepth / 8); + + // data should be initialized to 0 + for (size_t i = 0; i < rows; i++) { + for (size_t j = 0; j < cols; j++) { + uint8_t *data = (uint8_t *)frame.get(i, j); + REQUIRE(data != nullptr); + REQUIRE(*data == 0); + } + } +} + +TEST_CASE("Set a value in a 8 bit frame") { + size_t rows = 10; + size_t cols = 10; + size_t bitdepth = 8; + + Frame frame(rows, cols, Dtype::from_bitdepth(bitdepth)); + + // set a value + uint8_t value = 255; + frame.set(5, 7, value); + + // only the value we did set should be non-zero + for (size_t i = 0; i < rows; i++) { + for (size_t j = 0; j < cols; j++) { + uint8_t *data = (uint8_t *)frame.get(i, j); + REQUIRE(data != nullptr); + if (i == 5 && j == 7) { + REQUIRE(*data == value); + } else { + REQUIRE(*data == 0); + } + } + } +} + +TEST_CASE("Set a value in a 64 bit frame") { + size_t rows = 10; + size_t cols = 10; + size_t bitdepth = 64; + + Frame frame(rows, cols, Dtype::from_bitdepth(bitdepth)); + + // set a value + uint64_t value = 255; + frame.set(5, 7, value); + + // only the value we did set should be non-zero + for (size_t i = 0; i < rows; i++) { + for (size_t j = 0; j < cols; j++) { + uint64_t *data = (uint64_t *)frame.get(i, j); + REQUIRE(data != nullptr); + if (i == 5 && j == 7) { + REQUIRE(*data == value); + } else { + REQUIRE(*data == 0); + } + } + } +} + +TEST_CASE("Move construct a frame") { + size_t rows = 10; + size_t cols = 10; + size_t bitdepth = 8; + + Frame frame(rows, cols, Dtype::from_bitdepth(bitdepth)); + std::byte *data = frame.data(); + + Frame frame2(std::move(frame)); + + // state of the moved from object + REQUIRE(frame.rows() == 0); + REQUIRE(frame.cols() == 0); + REQUIRE(frame.dtype() == Dtype(Dtype::TypeIndex::ERROR)); + REQUIRE(frame.data() == nullptr); + + // state of the moved to object + REQUIRE(frame2.rows() == rows); + REQUIRE(frame2.cols() == cols); + REQUIRE(frame2.bitdepth() == bitdepth); + REQUIRE(frame2.bytes() == rows * cols * bitdepth / 8); + REQUIRE(frame2.data() == data); +} + +TEST_CASE("Move assign a frame") { + size_t rows = 10; + size_t cols = 10; + size_t bitdepth = 8; + + Frame frame(rows, cols, Dtype::from_bitdepth(bitdepth)); + std::byte *data = frame.data(); + + Frame frame2(5, 5, Dtype::from_bitdepth(16)); + + frame2 = std::move(frame); + + // state of the moved from object + REQUIRE(frame.rows() == 0); + REQUIRE(frame.cols() == 0); + REQUIRE(frame.dtype() == Dtype(Dtype::TypeIndex::ERROR)); + REQUIRE(frame.data() == nullptr); + + // state of the moved to object + REQUIRE(frame2.rows() == rows); + REQUIRE(frame2.cols() == cols); + REQUIRE(frame2.bitdepth() == bitdepth); + REQUIRE(frame2.bytes() == rows * cols * bitdepth / 8); + REQUIRE(frame2.data() == data); +} + +TEST_CASE("test explicit copy constructor") { + size_t rows = 10; + size_t cols = 10; + size_t bitdepth = 8; + + Frame frame(rows, cols, Dtype::from_bitdepth(bitdepth)); + std::byte *data = frame.data(); + + Frame frame2 = frame.copy(); + + // state of the original object + REQUIRE(frame.rows() == rows); + REQUIRE(frame.cols() == cols); + REQUIRE(frame.bitdepth() == bitdepth); + REQUIRE(frame.bytes() == rows * cols * bitdepth / 8); + REQUIRE(frame.data() == data); + + // state of the copied object + REQUIRE(frame2.rows() == rows); + REQUIRE(frame2.cols() == cols); + REQUIRE(frame2.bitdepth() == bitdepth); + REQUIRE(frame2.bytes() == rows * cols * bitdepth / 8); + REQUIRE(frame2.data() != data); +} \ No newline at end of file diff --git a/src/NDArray.test.cpp b/src/NDArray.test.cpp new file mode 100644 index 0000000..97aafa0 --- /dev/null +++ b/src/NDArray.test.cpp @@ -0,0 +1,377 @@ +#include "aare/NDArray.hpp" +#include +#include + +using aare::NDArray; +using aare::NDView; +using aare::Shape; + +TEST_CASE("Initial size is zero if no size is specified") { + NDArray a; + REQUIRE(a.size() == 0); + REQUIRE(a.shape() == Shape<2>{0, 0}); +} + +TEST_CASE("Construct from a DataSpan") { + std::vector some_data(9, 42); + NDView view(some_data.data(), Shape<2>{3, 3}); + + NDArray image(view); + + REQUIRE(image.shape() == view.shape()); + REQUIRE(image.size() == view.size()); + REQUIRE(image.data() != view.data()); + + for (uint32_t i = 0; i < image.size(); ++i) { + REQUIRE(image(i) == view(i)); + } + + // Changing the image doesn't change the view + image = 43; + for (uint32_t i = 0; i < image.size(); ++i) { + REQUIRE(image(i) != view(i)); + } +} + +TEST_CASE("1D image") { + std::array shape{{20}}; + NDArray img(shape, 3); + REQUIRE(img.size() == 20); + REQUIRE(img(5) == 3); +} + +TEST_CASE("Accessing a const object") { + const NDArray img({3, 4, 5}, 0); + REQUIRE(img(1, 1, 1) == 0); + REQUIRE(img.size() == 3 * 4 * 5); + REQUIRE(img.shape() == Shape<3>{3, 4, 5}); + REQUIRE(img.shape(0) == 3); + REQUIRE(img.shape(1) == 4); + REQUIRE(img.shape(2) == 5); +} + +TEST_CASE("Indexing of a 2D image") { + std::array shape{{3, 7}}; + NDArray img(shape, 5); + for (uint32_t i = 0; i != img.size(); ++i) { + REQUIRE(img(i) == 5); + } + + for (uint32_t i = 0; i != img.size(); ++i) { + img(i) = i; + } + REQUIRE(img(0, 0) == 0); + REQUIRE(img(0, 1) == 1); + REQUIRE(img(1, 0) == 7); +} + +TEST_CASE("Indexing of a 3D image") { + NDArray img{{{3, 4, 2}}, 5.0f}; + for (uint32_t i = 0; i != img.size(); ++i) { + REQUIRE(img(i) == 5.0f); + } + + // Double check general properties + REQUIRE(img.size() == 3 * 4 * 2); + + for (uint32_t i = 0; i != img.size(); ++i) { + img(i) = float(i); + } + REQUIRE(img(0, 0, 0) == 0); + REQUIRE(img(0, 0, 1) == 1); + REQUIRE(img(0, 1, 1) == 3); + REQUIRE(img(1, 2, 0) == 12); + REQUIRE(img(2, 3, 1) == 23); +} + +TEST_CASE("Divide double by int") { + NDArray a{{5}, 5}; + NDArray b{{5}, 5}; + a /= b; + for (auto it : a) { + REQUIRE(it == 1.0); + } +} + +TEST_CASE("Elementwise multiplication of 3D image") { + std::array shape{3, 4, 2}; + NDArray a{shape}; + NDArray b{shape}; + for (uint32_t i = 0; i != a.size(); ++i) { + a(i) = i; + b(i) = i; + } + auto c = a * b; + REQUIRE(c(0, 0, 0) == 0 * 0); + REQUIRE(c(0, 0, 1) == 1 * 1); + REQUIRE(c(0, 1, 1) == 3 * 3); + REQUIRE(c(1, 2, 0) == 12 * 12); + REQUIRE(c(2, 3, 1) == 23 * 23); +} + +TEST_CASE("Compare two images") { + NDArray a; + NDArray b; + CHECK((a == b)); + + a = NDArray{{5, 10}, 0}; + CHECK((a != b)); + + b = NDArray{{5, 10}, 0}; + CHECK((a == b)); + + b(3, 3) = 7; + CHECK((a != b)); +} + +TEST_CASE("Size and shape matches") { + int64_t w = 15; + int64_t h = 75; + std::array shape{w, h}; + NDArray a{shape}; + REQUIRE(a.size() == static_cast(w * h)); + REQUIRE(a.shape() == shape); +} + +TEST_CASE("Initial value matches for all elements") { + double v = 4.35; + NDArray a{{5, 5}, v}; + for (uint32_t i = 0; i < a.size(); ++i) { + REQUIRE(a(i) == v); + } +} + +TEST_CASE("Data layout of 3D image, fast index last") { + NDArray a{{3, 3, 3}, 0}; + REQUIRE(a.size() == 27); + int *ptr = a.data(); + + for (int i = 0; i < 9; ++i) { + *ptr++ = 10 + i; + REQUIRE(a(0, 0, i) == 10 + i); + REQUIRE(a(i) == 10 + i); + } +} + +TEST_CASE("Bitwise and on data") { + + NDArray a({3}, 0); + uint16_t mask = 0x3FF; + a(0) = 16684; + a(1) = 33068; + a(2) = 52608; + + a &= mask; + + REQUIRE(a(0) == 300); + REQUIRE(a(1) == 300); + REQUIRE(a(2) == 384); +} + +// TEST_CASE("Benchmarks") +// { +// NDArray img; +// std::array shape{ 512, 1024 }; +// BENCHMARK("Allocate 500k double image") +// { +// NDArrayim{ shape }; +// } +// BENCHMARK("Allocate 500k double image with initial value") +// { +// NDArrayim{ shape, 3.14 }; +// } + +// NDArray a{ shape, 1.2 }; +// NDArray b{ shape, 53. }; +// auto c = a + b; +// c = a * b; +// BENCHMARK("Multiply two images") +// { +// c = a * b; +// } +// BENCHMARK("Divide two images") +// { +// c = a / b; +// } +// BENCHMARK("Add two images") +// { +// c = a + b; +// } +// BENCHMARK("Subtract two images") +// { +// c = a - b; +// } +// } + +TEST_CASE("Elementwise operatios on images") { + std::array shape{5, 5}; + double a_val = 3.0; + double b_val = 8.0; + + SECTION("Add two images") { + NDArray A(shape, a_val); + NDArray B(shape, b_val); + + auto C = A + B; + + // Value of C matches + for (uint32_t i = 0; i < C.size(); ++i) { + REQUIRE(C(i) == a_val + b_val); + } + + // Value of A is not changed + for (uint32_t i = 0; i < A.size(); ++i) { + REQUIRE(A(i) == a_val); + } + + // Value of B is not changed + for (uint32_t i = 0; i < B.size(); ++i) { + REQUIRE(B(i) == b_val); + } + + // A, B and C referes to different data + REQUIRE(A.data() != B.data()); + REQUIRE(B.data() != C.data()); + } + SECTION("Subtract two images") { + NDArray A(shape, a_val); + NDArray B(shape, b_val); + auto C = A - B; + + // Value of C matches + for (uint32_t i = 0; i < C.size(); ++i) { + REQUIRE(C(i) == a_val - b_val); + } + + // Value of A is not changed + for (uint32_t i = 0; i < A.size(); ++i) { + REQUIRE(A(i) == a_val); + } + + // Value of B is not changed + for (uint32_t i = 0; i < B.size(); ++i) { + REQUIRE(B(i) == b_val); + } + + // A, B and C referes to different data + REQUIRE(A.data() != B.data()); + REQUIRE(B.data() != C.data()); + } + SECTION("Multiply two images") { + NDArray A(shape, a_val); + NDArray B(shape, b_val); + auto C = A * B; + + // Value of C matches + for (uint32_t i = 0; i < C.size(); ++i) { + REQUIRE(C(i) == a_val * b_val); + } + + // Value of A is not changed + for (uint32_t i = 0; i < A.size(); ++i) { + REQUIRE(A(i) == a_val); + } + + // Value of B is not changed + for (uint32_t i = 0; i < B.size(); ++i) { + REQUIRE(B(i) == b_val); + } + + // A, B and C referes to different data + REQUIRE(A.data() != B.data()); + REQUIRE(B.data() != C.data()); + } + SECTION("Divide two images") { + NDArray A(shape, a_val); + NDArray B(shape, b_val); + auto C = A / B; + + // Value of C matches + for (uint32_t i = 0; i < C.size(); ++i) { + REQUIRE(C(i) == a_val / b_val); + } + + // Value of A is not changed + for (uint32_t i = 0; i < A.size(); ++i) { + REQUIRE(A(i) == a_val); + } + + // Value of B is not changed + for (uint32_t i = 0; i < B.size(); ++i) { + REQUIRE(B(i) == b_val); + } + + // A, B and C referes to different data + REQUIRE(A.data() != B.data()); + REQUIRE(B.data() != C.data()); + } + + SECTION("subtract scalar") { + NDArray A(shape, a_val); + NDArray B(shape, b_val); + double v = 1.0; + auto C = A - v; + REQUIRE(C.data() != A.data()); + + // Value of C matches + for (uint32_t i = 0; i < C.size(); ++i) { + REQUIRE(C(i) == a_val - v); + } + + // Value of A is not changed + for (uint32_t i = 0; i < A.size(); ++i) { + REQUIRE(A(i) == a_val); + } + } + SECTION("add scalar") { + NDArray A(shape, a_val); + NDArray B(shape, b_val); + double v = 1.0; + auto C = A + v; + REQUIRE(C.data() != A.data()); + + // Value of C matches + for (uint32_t i = 0; i < C.size(); ++i) { + REQUIRE(C(i) == a_val + v); + } + + // Value of A is not changed + for (uint32_t i = 0; i < A.size(); ++i) { + REQUIRE(A(i) == a_val); + } + } + SECTION("divide with scalar") { + NDArray A(shape, a_val); + NDArray B(shape, b_val); + double v = 3.7; + auto C = A / v; + REQUIRE(C.data() != A.data()); + + // Value of C matches + for (uint32_t i = 0; i < C.size(); ++i) { + REQUIRE(C(i) == a_val / v); + } + + // Value of A is not changed + for (uint32_t i = 0; i < A.size(); ++i) { + REQUIRE(A(i) == a_val); + } + } + SECTION("multiply with scalar") { + NDArray A(shape, a_val); + NDArray B(shape, b_val); + double v = 3.7; + auto C = A / v; + REQUIRE(C.data() != A.data()); + + // Value of C matches + for (uint32_t i = 0; i < C.size(); ++i) { + REQUIRE(C(i) == a_val / v); + } + + // Value of A is not changed + for (uint32_t i = 0; i < A.size(); ++i) { + REQUIRE(A(i) == a_val); + } + } +} \ No newline at end of file diff --git a/src/NDView.test.cpp b/src/NDView.test.cpp new file mode 100644 index 0000000..3070de6 --- /dev/null +++ b/src/NDView.test.cpp @@ -0,0 +1,193 @@ +#include "aare/NDView.hpp" +#include + +#include +#include + +using aare::NDView; +using aare::Shape; + +TEST_CASE("Element reference 1D") { + std::vector vec; + for (int i = 0; i != 10; ++i) { + vec.push_back(i); + } + NDView data(vec.data(), Shape<1>{10}); + REQUIRE(vec.size() == static_cast(data.size())); + for (int i = 0; i != 10; ++i) { + REQUIRE(data(i) == vec[i]); + REQUIRE(data[i] == vec[i]); + } +} + +TEST_CASE("Element reference 2D") { + std::vector vec; + for (int i = 0; i != 12; ++i) { + vec.push_back(i); + } + + NDView data(vec.data(), Shape<2>{3, 4}); + REQUIRE(vec.size() == static_cast(data.size())); + int i = 0; + for (int row = 0; row != 3; ++row) { + for (int col = 0; col != 4; ++col) { + REQUIRE(data(row, col) == i); + REQUIRE(data[i] == vec[i]); + ++i; + } + } +} + +TEST_CASE("Element reference 3D") { + std::vector vec; + for (int i = 0; i != 24; ++i) { + vec.push_back(i); + } + NDView data(vec.data(), Shape<3>{2, 3, 4}); + REQUIRE(vec.size() == static_cast(data.size())); + int i = 0; + for (int frame = 0; frame != 2; ++frame) { + for (int row = 0; row != 3; ++row) { + for (int col = 0; col != 4; ++col) { + REQUIRE(data(frame, row, col) == i); + REQUIRE(data[i] == vec[i]); + ++i; + } + } + } +} + +TEST_CASE("Plus and miuns with single value") { + std::vector vec; + for (int i = 0; i != 12; ++i) { + vec.push_back(i); + } + NDView data(vec.data(), Shape<2>{3, 4}); + data += 5; + int i = 0; + for (int row = 0; row != 3; ++row) { + for (int col = 0; col != 4; ++col) { + REQUIRE(data(row, col) == i + 5); + ++i; + } + } + data -= 3; + i = 0; + for (int row = 0; row != 3; ++row) { + for (int col = 0; col != 4; ++col) { + REQUIRE(data(row, col) == i + 2); + ++i; + } + } +} + +TEST_CASE("Multiply and divide with single value") { + std::vector vec; + for (int i = 0; i != 12; ++i) { + vec.push_back(i); + } + NDView data(vec.data(), Shape<2>{3, 4}); + data *= 5; + int i = 0; + for (int row = 0; row != 3; ++row) { + for (int col = 0; col != 4; ++col) { + REQUIRE(data(row, col) == i * 5); + ++i; + } + } + data /= 3; + i = 0; + for (int row = 0; row != 3; ++row) { + for (int col = 0; col != 4; ++col) { + REQUIRE(data(row, col) == (i * 5) / 3); + ++i; + } + } +} + +TEST_CASE("elementwise assign") { + std::vector vec(25); + NDView data(vec.data(), Shape<2>{5, 5}); + + data = 3; + for (auto it : data) { + REQUIRE(it == 3); + } +} + +TEST_CASE("iterators") { + std::vector vec; + for (int i = 0; i != 12; ++i) { + vec.push_back(i); + } + NDView data(vec.data(), Shape<1>{12}); + int i = 0; + for (const auto item : data) { + REQUIRE(item == vec[i]); + ++i; + } + REQUIRE(i == 12); + + for (auto ptr = data.begin(); ptr != data.end(); ++ptr) { + *ptr += 1; + } + for (auto &item : data) { + ++item; + } + + i = 0; + for (const auto item : data) { + REQUIRE(item == i + 2); + ++i; + } +} + +// TEST_CASE("shape from vector") { +// std::vector vec; +// for (int i = 0; i != 12; ++i) { +// vec.push_back(i); +// } +// std::vector shape{3, 4}; +// NDView data(vec.data(), shape); +// } + +TEST_CASE("divide with another span") { + std::vector vec0{9, 12, 3}; + std::vector vec1{3, 2, 1}; + std::vector result{3, 6, 3}; + + NDView data0(vec0.data(), Shape<1>{static_cast(vec0.size())}); + NDView data1(vec1.data(), Shape<1>{static_cast(vec1.size())}); + + data0 /= data1; + + for (size_t i = 0; i != vec0.size(); ++i) { + REQUIRE(data0[i] == result[i]); + } +} + +TEST_CASE("Retrieve shape") { + std::vector vec; + for (int i = 0; i != 12; ++i) { + vec.push_back(i); + } + NDView data(vec.data(), Shape<2>{3, 4}); + REQUIRE(data.shape()[0] == 3); + REQUIRE(data.shape()[1] == 4); +} + +TEST_CASE("compare two views") { + std::vector vec1; + for (int i = 0; i != 12; ++i) { + vec1.push_back(i); + } + NDView view1(vec1.data(), Shape<2>{3, 4}); + + std::vector vec2; + for (int i = 0; i != 12; ++i) { + vec2.push_back(i); + } + NDView view2(vec2.data(), Shape<2>{3, 4}); + + REQUIRE((view1 == view2)); +} \ No newline at end of file diff --git a/src/defs.cpp b/src/defs.cpp new file mode 100644 index 0000000..f3cf9d9 --- /dev/null +++ b/src/defs.cpp @@ -0,0 +1,65 @@ +#include "aare/defs.hpp" +#include +#include + +namespace aare { + +/** + * @brief Convert a DetectorType to a string + * @param type DetectorType + * @return string representation of the DetectorType + */ +template <> std::string toString(DetectorType arg) { + switch (arg) { + case DetectorType::Jungfrau: + return "Jungfrau"; + case DetectorType::Eiger: + return "Eiger"; + case DetectorType::Mythen3: + return "Mythen3"; + case DetectorType::Moench: + return "Moench"; + case DetectorType::ChipTestBoard: + return "ChipTestBoard"; + default: + return "Unknown"; + } +} + +/** + * @brief Convert a string to a DetectorType + * @param name string representation of the DetectorType + * @return DetectorType + * @throw runtime_error if the string does not match any DetectorType + */ +template <> DetectorType StringTo(const std::string &arg) { + if (arg == "Jungfrau") + return DetectorType::Jungfrau; + if (arg == "Eiger") + return DetectorType::Eiger; + if (arg == "Mythen3") + return DetectorType::Mythen3; + if (arg == "Moench") + return DetectorType::Moench; + if (arg == "ChipTestBoard") + return DetectorType::ChipTestBoard; + throw std::runtime_error("Could not decode dector from: \"" + arg + "\""); +} + +/** + * @brief Convert a string to a TimingMode + * @param mode string representation of the TimingMode + * @return TimingMode + * @throw runtime_error if the string does not match any TimingMode + */ +template <> TimingMode StringTo(const std::string &arg) { + if (arg == "auto") + return TimingMode::Auto; + if (arg == "trigger") + return TimingMode::Trigger; + throw std::runtime_error("Could not decode timing mode from: \"" + arg + "\""); +} + +// template <> TimingMode StringTo(std::string mode); + +} // namespace aare \ No newline at end of file diff --git a/src/defs.test.cpp b/src/defs.test.cpp new file mode 100644 index 0000000..7475fe6 --- /dev/null +++ b/src/defs.test.cpp @@ -0,0 +1,42 @@ +#include "aare/defs.hpp" +// #include "aare/utils/floats.hpp" + +#include +#include +TEST_CASE("Enum to string conversion") { + // By the way I don't think the enum string conversions should be in the defs.hpp file + // but let's use this to show a test + REQUIRE(toString(aare::DetectorType::Jungfrau) == "Jungfrau"); +} + +TEST_CASE("Cluster creation") { + aare::Cluster c(13, 15); + REQUIRE(c.cluster_sizeX == 13); + REQUIRE(c.cluster_sizeY == 15); + REQUIRE(c.dt == aare::Dtype(typeid(int32_t))); + REQUIRE(c.data() != nullptr); + + aare::Cluster c2(c); + REQUIRE(c2.cluster_sizeX == 13); + REQUIRE(c2.cluster_sizeY == 15); + REQUIRE(c2.dt == aare::Dtype(typeid(int32_t))); + REQUIRE(c2.data() != nullptr); +} + +// TEST_CASE("cluster set and get data") { + +// aare::Cluster c2(33, 44, aare::Dtype(typeid(double))); +// REQUIRE(c2.cluster_sizeX == 33); +// REQUIRE(c2.cluster_sizeY == 44); +// REQUIRE(c2.dt == aare::Dtype::DOUBLE); +// double v = 3.14; +// c2.set(0, v); +// double v2 = c2.get(0); +// REQUIRE(aare::compare_floats(v, v2)); + +// c2.set(33 * 44 - 1, 123.11); +// double v3 = c2.get(33 * 44 - 1); +// REQUIRE(aare::compare_floats(123.11, v3)); + +// REQUIRE_THROWS_AS(c2.set(0, 1), std::invalid_argument); // set int to double +// } \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..bea9e32 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,44 @@ + +if (AARE_FETCH_CATCH) + FetchContent_Declare( + Catch2 + GIT_SHALLOW TRUE + GIT_REPOSITORY https://github.com/catchorg/Catch2.git + GIT_TAG v3.5.3 + ) + FetchContent_MakeAvailable(Catch2) +else() + find_package(Catch2 3 REQUIRED) +endif() + + +list(APPEND CMAKE_MODULE_PATH ${Catch2_SOURCE_DIR}/extras) + +add_executable(tests test.cpp) +target_link_libraries(tests PRIVATE Catch2::Catch2WithMain) + +set_target_properties(tests PROPERTIES + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR} + OUTPUT_NAME run_tests +) + +include(CTest) +include(Catch) +catch_discover_tests(tests) + +set(TestSources + ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp + ) +target_sources(tests PRIVATE ${TestSources} ) + +#Work around to remove, this is not the way to do it =) +# target_include_directories(tests PRIVATE ${CMAKE_SOURCE_DIR}/include/common) +target_link_libraries(tests PRIVATE aare_core aare_compiler_flags) + + + +#configure a header to pass test file paths +get_filename_component(TEST_FILE_PATH ${PROJECT_SOURCE_DIR}/data ABSOLUTE) +configure_file(test_config.hpp.in test_config.hpp) +target_include_directories(tests PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) + diff --git a/tests/test.cpp b/tests/test.cpp new file mode 100644 index 0000000..1a76f0a --- /dev/null +++ b/tests/test.cpp @@ -0,0 +1,21 @@ +#include "test_config.hpp" +#include +#include +#include +#include + +TEST_CASE("Test suite can find data assets") { + auto fpath = test_data_path() / "numpy" / "test_numpy_file.npy"; + REQUIRE(std::filesystem::exists(fpath)); +} + +TEST_CASE("Test suite can open data assets") { + auto fpath = test_data_path() / "numpy" / "test_numpy_file.npy"; + auto f = std::ifstream(fpath, std::ios::binary); + REQUIRE(f.is_open()); +} + +TEST_CASE("Test float32 and char8") { + REQUIRE(sizeof(float) == 4); + REQUIRE(CHAR_BIT == 8); +} \ No newline at end of file diff --git a/tests/test_config.hpp.in b/tests/test_config.hpp.in new file mode 100644 index 0000000..7669d49 --- /dev/null +++ b/tests/test_config.hpp.in @@ -0,0 +1,7 @@ +#pragma once +#include + +static constexpr auto test_data_path_str = "@TEST_FILE_PATH@"; +inline auto test_data_path() { + return std::filesystem::path(test_data_path_str); +} \ No newline at end of file