mirror of
https://github.com/slsdetectorgroup/aare.git
synced 2025-06-19 18:47:13 +02:00
merged with developer
This commit is contained in:
@ -27,6 +27,7 @@ TEST_CASE("Read one frame from a cluster file", "[.files]") {
|
||||
std::begin(expected_cluster_data)));
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("Read one frame using ROI", "[.files]") {
|
||||
// We know that the frame has 97 clusters
|
||||
auto fpath = test_data_path() / "clust" / "single_frame_97_clustrers.clust";
|
||||
@ -60,6 +61,8 @@ TEST_CASE("Read one frame using ROI", "[.files]") {
|
||||
std::begin(expected_cluster_data)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
TEST_CASE("Read clusters from single frame file", "[.files]") {
|
||||
|
||||
// frame_number, num_clusters [135] 97
|
||||
@ -162,6 +165,7 @@ TEST_CASE("Read clusters from single frame file", "[.files]") {
|
||||
// [ 97 296] [864 865 866 867 868 869 870 871 872]
|
||||
|
||||
auto fpath = test_data_path() / "clust" / "single_frame_97_clustrers.clust";
|
||||
|
||||
REQUIRE(std::filesystem::exists(fpath));
|
||||
|
||||
SECTION("Read fewer clusters than available") {
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <numeric>
|
||||
|
||||
using aare::NDView;
|
||||
using aare::Shape;
|
||||
@ -21,10 +22,8 @@ TEST_CASE("Element reference 1D") {
|
||||
}
|
||||
|
||||
TEST_CASE("Element reference 2D") {
|
||||
std::vector<int> vec;
|
||||
for (int i = 0; i != 12; ++i) {
|
||||
vec.push_back(i);
|
||||
}
|
||||
std::vector<int> vec(12);
|
||||
std::iota(vec.begin(), vec.end(), 0);
|
||||
|
||||
NDView<int, 2> data(vec.data(), Shape<2>{3, 4});
|
||||
REQUIRE(vec.size() == static_cast<size_t>(data.size()));
|
||||
@ -58,10 +57,8 @@ TEST_CASE("Element reference 3D") {
|
||||
}
|
||||
|
||||
TEST_CASE("Plus and miuns with single value") {
|
||||
std::vector<int> vec;
|
||||
for (int i = 0; i != 12; ++i) {
|
||||
vec.push_back(i);
|
||||
}
|
||||
std::vector<int> vec(12);
|
||||
std::iota(vec.begin(), vec.end(), 0);
|
||||
NDView<int, 2> data(vec.data(), Shape<2>{3, 4});
|
||||
data += 5;
|
||||
int i = 0;
|
||||
@ -116,10 +113,8 @@ TEST_CASE("elementwise assign") {
|
||||
}
|
||||
|
||||
TEST_CASE("iterators") {
|
||||
std::vector<int> vec;
|
||||
for (int i = 0; i != 12; ++i) {
|
||||
vec.push_back(i);
|
||||
}
|
||||
std::vector<int> vec(12);
|
||||
std::iota(vec.begin(), vec.end(), 0);
|
||||
NDView<int, 1> data(vec.data(), Shape<1>{12});
|
||||
int i = 0;
|
||||
for (const auto item : data) {
|
||||
@ -167,27 +162,31 @@ TEST_CASE("divide with another span") {
|
||||
}
|
||||
|
||||
TEST_CASE("Retrieve shape") {
|
||||
std::vector<int> vec;
|
||||
for (int i = 0; i != 12; ++i) {
|
||||
vec.push_back(i);
|
||||
}
|
||||
std::vector<int> vec(12);
|
||||
std::iota(vec.begin(), vec.end(), 0);
|
||||
NDView<int, 2> data(vec.data(), Shape<2>{3, 4});
|
||||
REQUIRE(data.shape()[0] == 3);
|
||||
REQUIRE(data.shape()[1] == 4);
|
||||
}
|
||||
|
||||
TEST_CASE("compare two views") {
|
||||
std::vector<int> vec1;
|
||||
for (int i = 0; i != 12; ++i) {
|
||||
vec1.push_back(i);
|
||||
}
|
||||
std::vector<int> vec1(12);
|
||||
std::iota(vec1.begin(), vec1.end(), 0);
|
||||
NDView<int, 2> view1(vec1.data(), Shape<2>{3, 4});
|
||||
|
||||
std::vector<int> vec2;
|
||||
for (int i = 0; i != 12; ++i) {
|
||||
vec2.push_back(i);
|
||||
}
|
||||
std::vector<int> vec2(12);
|
||||
std::iota(vec2.begin(), vec2.end(), 0);
|
||||
NDView<int, 2> view2(vec2.data(), Shape<2>{3, 4});
|
||||
|
||||
REQUIRE((view1 == view2));
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("Create a view over a vector"){
|
||||
std::vector<int> vec(12);
|
||||
std::iota(vec.begin(), vec.end(), 0);
|
||||
auto v = aare::make_view(vec);
|
||||
REQUIRE(v.shape()[0] == 12);
|
||||
REQUIRE(v[0] == 0);
|
||||
REQUIRE(v[11] == 11);
|
||||
}
|
@ -1,9 +1,12 @@
|
||||
#include "aare/RawSubFile.hpp"
|
||||
#include "aare/PixelMap.hpp"
|
||||
#include "aare/utils/ifstream_helpers.hpp"
|
||||
#include <cstring> // memcpy
|
||||
#include <fmt/core.h>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
|
||||
namespace aare {
|
||||
|
||||
RawSubFile::RawSubFile(const std::filesystem::path &fname,
|
||||
@ -20,7 +23,7 @@ RawSubFile::RawSubFile(const std::filesystem::path &fname,
|
||||
}
|
||||
|
||||
if (std::filesystem::exists(fname)) {
|
||||
n_frames = std::filesystem::file_size(fname) /
|
||||
m_num_frames = std::filesystem::file_size(fname) /
|
||||
(sizeof(DetectorHeader) + rows * cols * bitdepth / 8);
|
||||
} else {
|
||||
throw std::runtime_error(
|
||||
@ -35,7 +38,7 @@ RawSubFile::RawSubFile(const std::filesystem::path &fname,
|
||||
}
|
||||
|
||||
#ifdef AARE_VERBOSE
|
||||
fmt::print("Opened file: {} with {} frames\n", m_fname.string(), n_frames);
|
||||
fmt::print("Opened file: {} with {} frames\n", m_fname.string(), m_num_frames);
|
||||
fmt::print("m_rows: {}, m_cols: {}, m_bitdepth: {}\n", m_rows, m_cols,
|
||||
m_bitdepth);
|
||||
fmt::print("file size: {}\n", std::filesystem::file_size(fname));
|
||||
@ -43,8 +46,8 @@ RawSubFile::RawSubFile(const std::filesystem::path &fname,
|
||||
}
|
||||
|
||||
void RawSubFile::seek(size_t frame_index) {
|
||||
if (frame_index >= n_frames) {
|
||||
throw std::runtime_error(LOCATION + fmt::format("Frame index {} out of range in a file with {} frames", frame_index, n_frames));
|
||||
if (frame_index >= m_num_frames) {
|
||||
throw std::runtime_error(LOCATION + fmt::format("Frame index {} out of range in a file with {} frames", frame_index, m_num_frames));
|
||||
}
|
||||
m_file.seekg((sizeof(DetectorHeader) + bytes_per_frame()) * frame_index);
|
||||
}
|
||||
@ -60,6 +63,10 @@ void RawSubFile::read_into(std::byte *image_buf, DetectorHeader *header) {
|
||||
m_file.seekg(sizeof(DetectorHeader), std::ios::cur);
|
||||
}
|
||||
|
||||
if (m_file.fail()){
|
||||
throw std::runtime_error(LOCATION + ifstream_error_msg(m_file));
|
||||
}
|
||||
|
||||
// TODO! expand support for different bitdepths
|
||||
if (m_pixel_map) {
|
||||
// read into a temporary buffer and then copy the data to the buffer
|
||||
@ -79,8 +86,24 @@ void RawSubFile::read_into(std::byte *image_buf, DetectorHeader *header) {
|
||||
// read directly into the buffer
|
||||
m_file.read(reinterpret_cast<char *>(image_buf), bytes_per_frame());
|
||||
}
|
||||
|
||||
if (m_file.fail()){
|
||||
throw std::runtime_error(LOCATION + ifstream_error_msg(m_file));
|
||||
}
|
||||
}
|
||||
|
||||
void RawSubFile::read_into(std::byte *image_buf, size_t n_frames, DetectorHeader *header) {
|
||||
for (size_t i = 0; i < n_frames; i++) {
|
||||
read_into(image_buf, header);
|
||||
image_buf += bytes_per_frame();
|
||||
if (header) {
|
||||
++header;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <typename T>
|
||||
void RawSubFile::read_with_map(std::byte *image_buf) {
|
||||
auto part_buffer = new std::byte[bytes_per_frame()];
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include "aare/decode.hpp"
|
||||
|
||||
#include <cmath>
|
||||
namespace aare {
|
||||
|
||||
uint16_t adc_sar_05_decode64to16(uint64_t input){
|
||||
@ -22,6 +22,10 @@ uint16_t adc_sar_05_decode64to16(uint64_t input){
|
||||
}
|
||||
|
||||
void adc_sar_05_decode64to16(NDView<uint64_t, 2> input, NDView<uint16_t,2> output){
|
||||
if(input.shape() != output.shape()){
|
||||
throw std::invalid_argument(LOCATION + " input and output shapes must match");
|
||||
}
|
||||
|
||||
for(int64_t i = 0; i < input.shape(0); i++){
|
||||
for(int64_t j = 0; j < input.shape(1); j++){
|
||||
output(i,j) = adc_sar_05_decode64to16(input(i,j));
|
||||
@ -49,6 +53,9 @@ uint16_t adc_sar_04_decode64to16(uint64_t input){
|
||||
}
|
||||
|
||||
void adc_sar_04_decode64to16(NDView<uint64_t, 2> input, NDView<uint16_t,2> output){
|
||||
if(input.shape() != output.shape()){
|
||||
throw std::invalid_argument(LOCATION + " input and output shapes must match");
|
||||
}
|
||||
for(int64_t i = 0; i < input.shape(0); i++){
|
||||
for(int64_t j = 0; j < input.shape(1); j++){
|
||||
output(i,j) = adc_sar_04_decode64to16(input(i,j));
|
||||
@ -56,6 +63,40 @@ void adc_sar_04_decode64to16(NDView<uint64_t, 2> input, NDView<uint16_t,2> outpu
|
||||
}
|
||||
}
|
||||
|
||||
double apply_custom_weights(uint16_t input, const NDView<double, 1> weights) {
|
||||
if(weights.size() > 16){
|
||||
throw std::invalid_argument("weights size must be less than or equal to 16");
|
||||
}
|
||||
|
||||
double result = 0.0;
|
||||
for (ssize_t i = 0; i < weights.size(); ++i) {
|
||||
result += ((input >> i) & 1) * std::pow(weights[i], i);
|
||||
}
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
void apply_custom_weights(NDView<uint16_t, 1> input, NDView<double, 1> output, const NDView<double,1> weights) {
|
||||
if(input.shape() != output.shape()){
|
||||
throw std::invalid_argument(LOCATION + " input and output shapes must match");
|
||||
}
|
||||
|
||||
//Calculate weights to avoid repeatedly calling std::pow
|
||||
std::vector<double> weights_powers(weights.size());
|
||||
for (ssize_t i = 0; i < weights.size(); ++i) {
|
||||
weights_powers[i] = std::pow(weights[i], i);
|
||||
}
|
||||
|
||||
// Apply custom weights to each element in the input array
|
||||
for (ssize_t i = 0; i < input.shape(0); i++) {
|
||||
double result = 0.0;
|
||||
for (size_t bit_index = 0; bit_index < weights_powers.size(); ++bit_index) {
|
||||
result += ((input(i) >> bit_index) & 1) * weights_powers[bit_index];
|
||||
}
|
||||
output(i) = result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace aare
|
||||
|
80
src/decode.test.cpp
Normal file
80
src/decode.test.cpp
Normal file
@ -0,0 +1,80 @@
|
||||
#include "aare/decode.hpp"
|
||||
|
||||
#include <catch2/matchers/catch_matchers_floating_point.hpp>
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
#include "aare/NDArray.hpp"
|
||||
using Catch::Matchers::WithinAbs;
|
||||
#include <vector>
|
||||
|
||||
TEST_CASE("test_adc_sar_05_decode64to16"){
|
||||
uint64_t input = 0;
|
||||
uint16_t output = aare::adc_sar_05_decode64to16(input);
|
||||
CHECK(output == 0);
|
||||
|
||||
|
||||
// bit 29 on th input is bit 0 on the output
|
||||
input = 1UL << 29;
|
||||
output = aare::adc_sar_05_decode64to16(input);
|
||||
CHECK(output == 1);
|
||||
|
||||
// test all bits by iteratting through the bitlist
|
||||
std::vector<int> bitlist = {29, 19, 28, 18, 31, 21, 27, 20, 24, 23, 25, 22};
|
||||
for (size_t i = 0; i < bitlist.size(); i++) {
|
||||
input = 1UL << bitlist[i];
|
||||
output = aare::adc_sar_05_decode64to16(input);
|
||||
CHECK(output == (1 << i));
|
||||
}
|
||||
|
||||
|
||||
// test a few "random" values
|
||||
input = 0;
|
||||
input |= (1UL << 29);
|
||||
input |= (1UL << 19);
|
||||
input |= (1UL << 28);
|
||||
output = aare::adc_sar_05_decode64to16(input);
|
||||
CHECK(output == 7UL);
|
||||
|
||||
|
||||
input = 0;
|
||||
input |= (1UL << 18);
|
||||
input |= (1UL << 27);
|
||||
input |= (1UL << 25);
|
||||
output = aare::adc_sar_05_decode64to16(input);
|
||||
CHECK(output == 1096UL);
|
||||
|
||||
input = 0;
|
||||
input |= (1UL << 25);
|
||||
input |= (1UL << 22);
|
||||
output = aare::adc_sar_05_decode64to16(input);
|
||||
CHECK(output == 3072UL);
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("test_apply_custom_weights") {
|
||||
|
||||
uint16_t input = 1;
|
||||
aare::NDArray<double, 1> weights_data({3}, 0.0);
|
||||
weights_data(0) = 1.7;
|
||||
weights_data(1) = 2.1;
|
||||
weights_data(2) = 1.8;
|
||||
|
||||
auto weights = weights_data.view();
|
||||
|
||||
|
||||
double output = aare::apply_custom_weights(input, weights);
|
||||
CHECK_THAT(output, WithinAbs(1.0, 0.001));
|
||||
|
||||
input = 1 << 1;
|
||||
output = aare::apply_custom_weights(input, weights);
|
||||
CHECK_THAT(output, WithinAbs(2.1, 0.001));
|
||||
|
||||
|
||||
input = 1 << 2;
|
||||
output = aare::apply_custom_weights(input, weights);
|
||||
CHECK_THAT(output, WithinAbs(3.24, 0.001));
|
||||
|
||||
input = 0b111;
|
||||
output = aare::apply_custom_weights(input, weights);
|
||||
CHECK_THAT(output, WithinAbs(6.34, 0.001));
|
||||
|
||||
}
|
18
src/utils/ifstream_helpers.cpp
Normal file
18
src/utils/ifstream_helpers.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
#include "aare/utils/ifstream_helpers.hpp"
|
||||
|
||||
namespace aare {
|
||||
|
||||
std::string ifstream_error_msg(std::ifstream &ifs) {
|
||||
std::ios_base::iostate state = ifs.rdstate();
|
||||
if (state & std::ios_base::eofbit) {
|
||||
return " End of file reached";
|
||||
} else if (state & std::ios_base::badbit) {
|
||||
return " Bad file stream";
|
||||
} else if (state & std::ios_base::failbit) {
|
||||
return " File read failed";
|
||||
}else{
|
||||
return " Unknown/no error";
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace aare
|
Reference in New Issue
Block a user