added some more tests and debugged

This commit is contained in:
2025-06-11 11:18:34 +02:00
parent 6bc8b0c4a7
commit ba8778cf44
4 changed files with 34 additions and 54 deletions

View File

@ -205,16 +205,12 @@ class FlatField {
acc.second + 1);
});
std::cout << "sum: " << sum << std::endl;
std::cout << "count: " << count << std::endl;
return sum / count;
}
NDArray<double, 1> inverse_normalized_flatfield(double tolerance = 0.001) {
double mean = calculate_mean(tolerance);
std::cout << "mean: " << mean << std::endl;
NDArray<double, 1> inverse_normalized_flatfield(flat_field.shape());
/*

View File

@ -34,21 +34,22 @@ class MythenFileReader : public HDF5FileReader {
std::string current_file_name =
m_base_path / (file_prefix + std::to_string(frame_index) + ".h5");
MythenFrame frame;
open_file(current_file_name);
auto dataset_photon_count =
get_dataset("/entry/instrument/detector/data");
NDArray photon_counts =
frame.photon_counts =
dataset_photon_count.store_as_ndarray<uint32_t, 1>();
++frame.photon_counts;
auto dataset_detector_angle =
get_dataset("/entry/instrument/NDAttributes/DetectorAngle");
double detector_angle;
dataset_detector_angle.read_into_buffer(
reinterpret_cast<std::byte *>(&detector_angle));
reinterpret_cast<std::byte *>(&frame.detector_angle));
auto dataset_channel_number =
get_dataset("/entry/instrument/NDAttributes/CounterMask");
@ -64,13 +65,13 @@ class MythenFileReader : public HDF5FileReader {
// significant
// bit is ask Anna again
std::array<uint8_t, 3> channel_mask{binary_channel_numbers[0],
binary_channel_numbers[1],
binary_channel_numbers[2]};
frame.channel_mask = std::array<uint8_t, 3>{binary_channel_numbers[0],
binary_channel_numbers[1],
binary_channel_numbers[2]};
close_file();
return MythenFrame{photon_counts, detector_angle, channel_mask};
return frame;
}
private:

View File

@ -236,16 +236,11 @@ void AngleCalibration::redistribute_photon_counts_to_fixed_angle_bins(
throw std::runtime_error("wrong number of strips read");
}
// NDArray<double, 1> diffraction_angles(
// std::array<ssize_t, 1>{mythen_detector->num_strips()}, 0.0);
// NDArray<double, 1> angle_widths(
// std::array<ssize_t, 1>{mythen_detector->num_strips()}, 0.0);
ssize_t num_bins1 = mythen_detector->min_angle() / histogram_bin_width;
ssize_t num_bins2 = mythen_detector->max_angle() / histogram_bin_width;
std::cout << "position: " << frame.detector_angle << std::endl;
std::cout << "position: " << frame.detector_angle
<< std::endl; // replace with log
for (ssize_t strip_index = 0; strip_index < mythen_detector->num_strips();
++strip_index) {
@ -274,8 +269,6 @@ void AngleCalibration::redistribute_photon_counts_to_fixed_angle_bins(
diffraction_angle += (frame.detector_angle + mythen_detector->dtt0() +
mythen_detector->bloffset());
// diffraction_angles[strip_index] = diffraction_angle;
if (diffraction_angle < mythen_detector->min_angle() ||
diffraction_angle > mythen_detector->max_angle())
continue;
@ -283,8 +276,6 @@ void AngleCalibration::redistribute_photon_counts_to_fixed_angle_bins(
double angle_covered_by_strip =
angular_strip_width_from_DG_parameters(strip_index);
// angle_widths[strip_index] = angle_covered_by_strip;
double photon_count_per_bin = histogram_bin_width *
corrected_photon_count /
angle_covered_by_strip;
@ -331,9 +322,6 @@ void AngleCalibration::redistribute_photon_counts_to_fixed_angle_bins(
}
}
}
// std::string filename = "angle_widths.txt";
// save(angle_widths, filename);
}
void AngleCalibration::write_to_file(const std::string &filename) {

View File

@ -10,6 +10,7 @@
#include "test_config.hpp"
#include <iomanip>
#include <type_traits>
#include <catch2/catch_all.hpp>
#include <catch2/catch_test_macros.hpp>
@ -19,7 +20,7 @@ using namespace aare;
template <typename T, ssize_t Ndim = 1>
NDArray<T, Ndim> read_into_array(const std::string &filename,
const std::array<ssize_t, 1> size) {
const std::array<ssize_t, Ndim> size) {
std::string word;
NDArray<T, Ndim> array(size);
try {
@ -45,11 +46,23 @@ NDArray<T, Ndim> read_into_array(const std::string &filename,
return array;
}
template <typename T, bool = std::is_integral_v<T>> struct safe_make_signed {
using type = T;
};
template <typename T> struct safe_make_signed<T, true> {
using type = std::make_signed_t<T>;
};
template <typename T, ssize_t Ndim>
bool check_equality_of_arrays(NDView<T, Ndim> array1, NDView<T, Ndim> array2) {
bool equal = true;
using SignedT = typename safe_make_signed<T>::type;
for (ssize_t i = 0; i < array1.size(); ++i) {
if (std::abs(array1[i] - array2[i]) > 1e-10) {
if (std::abs(static_cast<SignedT>(array1[i]) -
static_cast<SignedT>(array2[i])) > 1e-6) {
std::cout << "index: " << i << std::endl;
std::cout << std::setprecision(15) << array1[i] << std::endl;
std::cout << std::setprecision(15) << array2[i] << std::endl;
@ -348,39 +361,21 @@ TEST_CASE("calculate new fixed angle width bins histogram",
"cpp_new_photon_counts.xye"); // TODO adjust output path
}
TEST_CASE("check diffraction angles") {
auto expected_diffraction_angle_filename = test_data_path() /
"AngleCalibration_Test_Data" /
"diffraction_angle.txt";
REQUIRE(std::filesystem::exists(expected_diffraction_angle_filename));
auto expected_angles =
read_into_array<double, 1>(expected_diffraction_angle_filename.string(),
std::array<ssize_t, 1>{61440});
auto diffraction_angle_filename =
std::filesystem::current_path() / "../build/diffraction_angles.txt";
auto angles = load<double, 1>(diffraction_angle_filename,
std::array<ssize_t, 1>{61440});
CHECK(check_equality_of_arrays(angles.view(), expected_angles.view()));
}
TEST_CASE("check angle widths") {
TEST_CASE("compare result with python code", "[.anglecalibration][.files]") {
auto expected_filename =
test_data_path() / "AngleCalibration_Test_Data" / "angle_width.txt";
test_data_path() / "AngleCalibration_Test_Data" / "out2.xye";
REQUIRE(std::filesystem::exists(expected_filename));
auto expected_array = read_into_array<double, 1>(
expected_filename.string(), std::array<ssize_t, 1>{61440});
auto expected_array = read_into_array<double, 2>(
expected_filename.string(), std::array<ssize_t, 2>{61440, 3});
auto filename =
std::filesystem::current_path() / "../build/angle_widths.txt";
std::filesystem::current_path() / "../build/cpp_new_photon_counts.xye";
auto array = load<double, 1>(filename, std::array<ssize_t, 1>{61440});
// auto array = load<double, 2>(filename, std::array<ssize_t, 2>{61440, 3});
auto array = read_into_array<double, 2>(filename.string(),
std::array<ssize_t, 2>{61440, 3});
CHECK(check_equality_of_arrays(array.view(), expected_array.view()));
}