From 4ff2e18c6a8d8ba687cd31c84fa5f0bc916f21af Mon Sep 17 00:00:00 2001 From: Bechir Date: Thu, 21 Mar 2024 16:17:00 +0100 Subject: [PATCH] change image to NDArray --- core/include/aare/Frame.hpp | 6 +- core/include/aare/{Image.hpp => NDArray.hpp} | 168 +++++++++--------- .../aare/VariableSizeClusterFinder.hpp | 10 +- core/test/wrappers.test.cpp | 28 +-- data/jungfrau_double_master_0.json | 2 +- data/jungfrau_single_master_0.json | 2 +- data/m3_master_0.json | 2 +- 7 files changed, 109 insertions(+), 109 deletions(-) rename core/include/aare/{Image.hpp => NDArray.hpp} (65%) diff --git a/core/include/aare/Frame.hpp b/core/include/aare/Frame.hpp index 5e08b22..b6e8446 100644 --- a/core/include/aare/Frame.hpp +++ b/core/include/aare/Frame.hpp @@ -1,5 +1,5 @@ #pragma once -#include "aare/Image.hpp" +#include "aare/NDArray.hpp" #include "aare/defs.hpp" #include #include @@ -47,8 +47,8 @@ class Frame { } template - Image image() { - return Image(this->view()); + NDArray image() { + return NDArray(this->view()); } ~Frame() { delete[] m_data; } diff --git a/core/include/aare/Image.hpp b/core/include/aare/NDArray.hpp similarity index 65% rename from core/include/aare/Image.hpp rename to core/include/aare/NDArray.hpp index 54deddf..fb3378a 100644 --- a/core/include/aare/Image.hpp +++ b/core/include/aare/NDArray.hpp @@ -7,7 +7,7 @@ memory. TODO! Add expression templates for operators */ -#include "aare/View.hpp" +#include "aare/NDView.hpp" #include #include @@ -19,47 +19,47 @@ TODO! Add expression templates for operators #include -template class Image { +template class NDArray { public: - Image() + NDArray() : shape_(), strides_(c_strides(shape_)), size_(0), data_(nullptr){}; - explicit Image(std::array shape) + 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_]){}; - Image(std::array shape, T value) : Image(shape) { + NDArray(std::array shape, T value) : NDArray(shape) { this->operator=(value); } - /* When constructing from a View we need to copy the data since - Image expect to own its data, and span is just a view*/ - Image(View span):Image(span.shape()){ + /* When constructing from a NDView we need to copy the data since + NDArray expect to own its data, and span is just a view*/ + NDArray(NDView span):NDArray(span.shape()){ std::copy(span.begin(), span.end(), begin()); - // fmt::print("Image(View span)\n"); + // fmt::print("NDArray(NDView span)\n"); } // Move constructor - Image(Image &&other) + NDArray(NDArray &&other) : shape_(other.shape_), strides_(c_strides(shape_)), size_(other.size_), data_(nullptr) { data_ = other.data_; other.reset(); - // fmt::print("Image(Image &&other)\n"); + // fmt::print("NDArray(NDArray &&other)\n"); } // Copy constructor - Image(const Image &other) + 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("Image(const Image &other)\n"); + // fmt::print("NDArray(const NDArray &other)\n"); } - ~Image() { + ~NDArray() { delete[] data_; } @@ -68,19 +68,19 @@ template class Image { using value_type = T; - Image &operator=(Image &&other); // Move assign - Image &operator=(const Image &other); // Copy assign + NDArray &operator=(NDArray &&other); // Move assign + NDArray &operator=(const NDArray &other); // Copy assign - Image operator+(const Image &other); - Image &operator+=(const Image &other); - Image operator-(const Image &other); - Image &operator-=(const Image &other); - Image operator*(const Image &other); - Image &operator*=(const Image &other); - Image operator/(const Image &other); - // Image& operator/=(const Image& 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); + // NDArray& operator/=(const NDArray& other); template - Image &operator/=(const Image &other) { + NDArray &operator/=(const NDArray &other) { // check shape if (shape_ == other.shape()) { for (int i = 0; i < size_; ++i) { @@ -88,26 +88,26 @@ template class Image { } return *this; } else { - throw(std::runtime_error("Shape of Image must match")); + throw(std::runtime_error("Shape of NDArray must match")); } } - Image operator>(const Image &other); + NDArray operator>(const NDArray &other); - bool operator==(const Image &other) const; - bool operator!=(const Image &other) const; + bool operator==(const NDArray &other) const; + bool operator!=(const NDArray &other) const; - Image &operator=(const T &); - Image &operator+=(const T &); - Image operator+(const T &); - Image &operator-=(const T &); - Image operator-(const T &); - Image &operator*=(const T &); - Image operator*(const T &); - Image &operator/=(const T &); - Image operator/(const T &); + NDArray &operator=(const T &); + NDArray &operator+=(const T &); + NDArray operator+(const T &); + NDArray &operator-=(const T &); + NDArray operator-(const T &); + NDArray &operator*=(const T &); + NDArray operator*(const T &); + NDArray &operator/=(const T &); + NDArray operator/(const T &); - Image &operator&=(const T &); + NDArray &operator&=(const T &); void sqrt() { for (int i = 0; i < size_; ++i) { @@ -115,7 +115,7 @@ template class Image { } } - Image &operator++(); // pre inc + NDArray &operator++(); // pre inc template typename std::enable_if::type @@ -153,7 +153,7 @@ template class Image { // return strides_; } - View span() const { return View{data_, shape_}; } + NDView span() const { return NDView{data_, shape_}; } void Print(); void Print_all(); @@ -175,7 +175,7 @@ template class Image { // Move assign template -Image &Image::operator=(Image &&other) { +NDArray &NDArray::operator=(NDArray &&other) { if (this != &other) { delete[] data_; data_ = other.data_; @@ -188,14 +188,14 @@ Image &Image::operator=(Image &&other) { } template -Image Image::operator+(const Image &other) { - Image result(*this); +NDArray NDArray::operator+(const NDArray &other) { + NDArray result(*this); result += other; return result; } template -Image & -Image::operator+=(const Image &other) { +NDArray & +NDArray::operator+=(const NDArray &other) { // check shape if (shape_ == other.shape_) { for (int i = 0; i < size_; ++i) { @@ -208,15 +208,15 @@ Image::operator+=(const Image &other) { } template -Image Image::operator-(const Image &other) { - Image result{*this}; +NDArray NDArray::operator-(const NDArray &other) { + NDArray result{*this}; result -= other; return result; } template -Image & -Image::operator-=(const Image &other) { +NDArray & +NDArray::operator-=(const NDArray &other) { // check shape if (shape_ == other.shape_) { for (int i = 0; i < size_; ++i) { @@ -228,15 +228,15 @@ Image::operator-=(const Image &other) { } } template -Image Image::operator*(const Image &other) { - Image result = *this; +NDArray NDArray::operator*(const NDArray &other) { + NDArray result = *this; result *= other; return result; } template -Image & -Image::operator*=(const Image &other) { +NDArray & +NDArray::operator*=(const NDArray &other) { // check shape if (shape_ == other.shape_) { for (int i = 0; i < size_; ++i) { @@ -249,21 +249,21 @@ Image::operator*=(const Image &other) { } template -Image Image::operator/(const Image &other) { - Image result = *this; +NDArray NDArray::operator/(const NDArray &other) { + NDArray result = *this; result /= other; return result; } template -Image &Image::operator&=(const T &mask) { +NDArray &NDArray::operator&=(const T &mask) { for (auto it = begin(); it != end(); ++it) *it &= mask; return *this; } // template -// Image& Image::operator/=(const Image& +// NDArray& NDArray::operator/=(const NDArray& // other) // { // //check shape @@ -278,9 +278,9 @@ Image &Image::operator&=(const T &mask) { // } template -Image Image::operator>(const Image &other) { +NDArray NDArray::operator>(const NDArray &other) { if (shape_ == other.shape_) { - Image result{shape_}; + NDArray result{shape_}; for (int i = 0; i < size_; ++i) { result(i) = (data_[i] > other.data_[i]); } @@ -291,8 +291,8 @@ Image Image::operator>(const Image &other) { } template -Image & -Image::operator=(const Image &other) { +NDArray & +NDArray::operator=(const NDArray &other) { if (this != &other) { delete[] data_; shape_ = other.shape_; @@ -305,7 +305,7 @@ Image::operator=(const Image &other) { } template -bool Image::operator==(const Image &other) const { +bool NDArray::operator==(const NDArray &other) const { if (shape_ != other.shape_) return false; @@ -317,78 +317,78 @@ bool Image::operator==(const Image &other) const { } template -bool Image::operator!=(const Image &other) const { +bool NDArray::operator!=(const NDArray &other) const { return !((*this) == other); } template -Image &Image::operator++() { +NDArray &NDArray::operator++() { for (int i = 0; i < size_; ++i) data_[i] += 1; return *this; } template -Image &Image::operator=(const T &value) { +NDArray &NDArray::operator=(const T &value) { std::fill_n(data_, size_, value); return *this; } template -Image &Image::operator+=(const T &value) { +NDArray &NDArray::operator+=(const T &value) { for (int i = 0; i < size_; ++i) data_[i] += value; return *this; } template -Image Image::operator+(const T &value) { - Image result = *this; +NDArray NDArray::operator+(const T &value) { + NDArray result = *this; result += value; return result; } template -Image &Image::operator-=(const T &value) { +NDArray &NDArray::operator-=(const T &value) { for (int i = 0; i < size_; ++i) data_[i] -= value; return *this; } template -Image Image::operator-(const T &value) { - Image result = *this; +NDArray NDArray::operator-(const T &value) { + NDArray result = *this; result -= value; return result; } template -Image &Image::operator/=(const T &value) { +NDArray &NDArray::operator/=(const T &value) { for (int i = 0; i < size_; ++i) data_[i] /= value; return *this; } template -Image Image::operator/(const T &value) { - Image result = *this; +NDArray NDArray::operator/(const T &value) { + NDArray result = *this; result /= value; return result; } template -Image &Image::operator*=(const T &value) { +NDArray &NDArray::operator*=(const T &value) { for (int i = 0; i < size_; ++i) data_[i] *= value; return *this; } template -Image Image::operator*(const T &value) { - Image result = *this; +NDArray NDArray::operator*(const T &value) { + NDArray result = *this; result *= value; return result; } -template void Image::Print() { +template void NDArray::Print() { if (shape_[0] < 20 && shape_[1] < 20) Print_all(); else Print_some(); } -template void Image::Print_all() { +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); @@ -397,7 +397,7 @@ template void Image::Print_all() { std::cout << "\n"; } } -template void Image::Print_some() { +template void NDArray::Print_some() { for (auto row = 0; row < 5; ++row) { for (auto col = 0; col < 5; ++col) { std::cout << std::setw(7); @@ -408,7 +408,7 @@ template void Image::Print_some() { } template -void save(Image &img, std::string pathname) { +void save(NDArray &img, std::string pathname) { std::ofstream f; f.open(pathname, std::ios::binary); f.write(img.buffer(), img.size() * sizeof(T)); @@ -416,9 +416,9 @@ void save(Image &img, std::string pathname) { } template -Image load(const std::string &pathname, +NDArray load(const std::string &pathname, std::array shape) { - Image img{shape}; + NDArray img{shape}; std::ifstream f; f.open(pathname, std::ios::binary); f.read(img.buffer(), img.size() * sizeof(T)); diff --git a/core/include/aare/VariableSizeClusterFinder.hpp b/core/include/aare/VariableSizeClusterFinder.hpp index 89d8269..dcf95f0 100644 --- a/core/include/aare/VariableSizeClusterFinder.hpp +++ b/core/include/aare/VariableSizeClusterFinder.hpp @@ -6,7 +6,7 @@ #include -#include "aare/Image.hpp" +#include "aare/NDArray.hpp" const int MAX_CLUSTER_SIZE = 200; namespace pl { @@ -31,9 +31,9 @@ template class ClusterFinder { private: const std::array shape_; NDView original_; - Image labeled_; - Image peripheral_labeled_; - Image binary_; // over threshold flag + NDArray labeled_; + NDArray peripheral_labeled_; + NDArray binary_; // over threshold flag T threshold_; NDView noiseMap; bool use_noise_map = false; @@ -56,7 +56,7 @@ template class ClusterFinder { hits.reserve(2000); } - Image labeled() { return labeled_; } + NDArray labeled() { return labeled_; } void set_noiseMap(NDView noise_map) { noiseMap = noise_map; use_noise_map = true; } void set_peripheralThresholdFactor(int factor) { peripheralThresholdFactor_ = factor; } diff --git a/core/test/wrappers.test.cpp b/core/test/wrappers.test.cpp index adeeb2b..ae0f12e 100644 --- a/core/test/wrappers.test.cpp +++ b/core/test/wrappers.test.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include @@ -34,29 +34,29 @@ TEST_CASE("NDView") { REQUIRE(ds(i / 10, i % 10) == data[i]); } } - // SECTION("from Frame") { - // Frame f(reinterpret_cast(data), 10, 10, 16); - // View ds = f.view(); - // for (int i = 0; i < 100; i++) { - // REQUIRE(ds(i / 10, i % 10) == data[i]); - // } + SECTION("from Frame") { + Frame f(reinterpret_cast(data), 10, 10, 16); + NDView ds = f.view(); + for (int i = 0; i < 100; i++) { + REQUIRE(ds(i / 10, i % 10) == data[i]); + } - // f.set(0, 0, (uint16_t)44); - // REQUIRE((uint16_t)*f.get(0, 0) == 44); // check that set worked - // REQUIRE(ds(0, 0) == 44); // check that ds is updated - // REQUIRE(data[0] == 0); // check that data is not updated - // } + f.set(0, 0, (uint16_t)44); + REQUIRE((uint16_t)*f.get(0, 0) == 44); // check that set worked + REQUIRE(ds(0, 0) == 44); // check that ds is updated + REQUIRE(data[0] == 0); // check that data is not updated + } delete[] data; } -TEST_CASE("Image") { +TEST_CASE("NDArray") { auto data = new uint16_t[100]; for (int i = 0; i < 100; i++) { data[i] = i; } SECTION("from Frame") { Frame f(reinterpret_cast(data), 10, 10, 16); - Image img = f.image(); + NDArray img = f.image(); for (int i = 0; i < 100; i++) { REQUIRE(img(i / 10, i % 10) == data[i]); } diff --git a/data/jungfrau_double_master_0.json b/data/jungfrau_double_master_0.json index e6a80e3..12f4395 100644 --- a/data/jungfrau_double_master_0.json +++ b/data/jungfrau_double_master_0.json @@ -7,7 +7,7 @@ "x": 1, "y": 2 }, - "Image Size in bytes": 524288, + "NDArray Size in bytes": 524288, "Pixels": { "x": 1024, "y": 256 diff --git a/data/jungfrau_single_master_0.json b/data/jungfrau_single_master_0.json index 547081a..44f5f96 100644 --- a/data/jungfrau_single_master_0.json +++ b/data/jungfrau_single_master_0.json @@ -7,7 +7,7 @@ "x": 1, "y": 1 }, - "Image Size in bytes": 1048576, + "NDArray Size in bytes": 1048576, "Pixels": { "x": 1024, "y": 512 diff --git a/data/m3_master_0.json b/data/m3_master_0.json index f6b4f31..c629f98 100644 --- a/data/m3_master_0.json +++ b/data/m3_master_0.json @@ -7,7 +7,7 @@ "x": 1, "y": 1 }, - "Image Size in bytes": 15360, + "NDArray Size in bytes": 15360, "Pixels": { "x": 3840, "y": 1