mirror of
https://github.com/slsdetectorgroup/aare.git
synced 2026-02-19 23:58:43 +01:00
added safe_divide to NDArray and used it for pedestal
This commit is contained in:
@@ -217,6 +217,7 @@ class NDArray : public ArrayExpr<NDArray<T, Ndim>, Ndim> {
|
|||||||
std::fill(shape_.begin(), shape_.end(), 0);
|
std::fill(shape_.begin(), shape_.end(), 0);
|
||||||
std::fill(strides_.begin(), strides_.end(), 0);
|
std::fill(strides_.begin(), strides_.end(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Move assign
|
// Move assign
|
||||||
@@ -380,12 +381,8 @@ NDArray<T, Ndim> NDArray<T, Ndim>::operator*(const T &value) {
|
|||||||
result *= value;
|
result *= value;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
// template <typename T, ssize_t Ndim> void NDArray<T, Ndim>::Print() {
|
|
||||||
// if (shape_[0] < 20 && shape_[1] < 20)
|
|
||||||
// Print_all();
|
|
||||||
// else
|
|
||||||
// Print_some();
|
|
||||||
// }
|
|
||||||
|
|
||||||
template <typename T, ssize_t Ndim>
|
template <typename T, ssize_t Ndim>
|
||||||
std::ostream &operator<<(std::ostream &os, const NDArray<T, Ndim> &arr) {
|
std::ostream &operator<<(std::ostream &os, const NDArray<T, Ndim> &arr) {
|
||||||
@@ -437,4 +434,22 @@ NDArray<T, Ndim> load(const std::string &pathname,
|
|||||||
return img;
|
return img;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template <typename RT, typename NT, typename DT, ssize_t Ndim>
|
||||||
|
NDArray<RT, Ndim> safe_divide(const NDArray<NT, Ndim> &numerator,
|
||||||
|
const NDArray<DT, Ndim> &denominator) {
|
||||||
|
if (numerator.shape() != denominator.shape()) {
|
||||||
|
throw std::runtime_error("Shapes of numerator and denominator must match");
|
||||||
|
}
|
||||||
|
NDArray<RT, Ndim> result(numerator.shape());
|
||||||
|
for (ssize_t i = 0; i < numerator.size(); ++i) {
|
||||||
|
if (denominator[i] != 0) {
|
||||||
|
result[i] = static_cast<RT>(numerator[i]) / static_cast<RT>(denominator[i]);
|
||||||
|
} else {
|
||||||
|
result[i] = RT{0}; // or handle division by zero as needed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace aare
|
} // namespace aare
|
||||||
@@ -161,6 +161,9 @@ NDArray<T, 2> calculate_pedestal_g0(NDView<uint16_t, 3> raw_data) {
|
|||||||
}
|
}
|
||||||
return pedestal;
|
return pedestal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
NDArray<T, 2> calculate_pedestal_g0(NDView<uint16_t, 3> raw_data,
|
NDArray<T, 2> calculate_pedestal_g0(NDView<uint16_t, 3> raw_data,
|
||||||
ssize_t n_threads) {
|
ssize_t n_threads) {
|
||||||
@@ -191,20 +194,8 @@ NDArray<T, 2> calculate_pedestal_g0(NDView<uint16_t, 3> raw_data,
|
|||||||
accumulator += acc;
|
accumulator += acc;
|
||||||
count += cnt;
|
count += cnt;
|
||||||
}
|
}
|
||||||
NDArray<T, 2> pedestal(
|
|
||||||
std::array<ssize_t,2>{raw_data.shape(1), raw_data.shape(2)}, 0);
|
return safe_divide<T>(accumulator, count);
|
||||||
for (int gain = 0; gain < 3; ++gain) {
|
|
||||||
for (int row = 0; row < raw_data.shape(1); ++row) {
|
|
||||||
for (int col = 0; col < raw_data.shape(2); ++col) {
|
|
||||||
if (count(row, col) != 0) {
|
|
||||||
pedestal(row, col) =
|
|
||||||
static_cast<T>(accumulator(row, col)) /
|
|
||||||
static_cast<T>(count(row, col));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return pedestal;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,8 +203,6 @@ NDArray<T, 2> calculate_pedestal_g0(NDView<uint16_t, 3> raw_data,
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
NDArray<T, 3> calculate_pedestal(NDView<uint16_t, 3> raw_data,
|
NDArray<T, 3> calculate_pedestal(NDView<uint16_t, 3> raw_data,
|
||||||
ssize_t n_threads) {
|
ssize_t n_threads) {
|
||||||
NDArray<int, 2> switched(
|
|
||||||
std::array<ssize_t, 2>{raw_data.shape(1), raw_data.shape(2)}, 0);
|
|
||||||
std::vector<std::future<std::pair<NDArray<size_t, 3>, NDArray<size_t, 3>>>>
|
std::vector<std::future<std::pair<NDArray<size_t, 3>, NDArray<size_t, 3>>>>
|
||||||
futures;
|
futures;
|
||||||
futures.reserve(n_threads);
|
futures.reserve(n_threads);
|
||||||
@@ -244,20 +233,7 @@ NDArray<T, 3> calculate_pedestal(NDView<uint16_t, 3> raw_data,
|
|||||||
count += cnt;
|
count += cnt;
|
||||||
}
|
}
|
||||||
|
|
||||||
NDArray<T, 3> pedestal(
|
return safe_divide<T>(accumulator, count);
|
||||||
std::array<ssize_t, 3>{3, raw_data.shape(1), raw_data.shape(2)}, 0);
|
|
||||||
for (int gain = 0; gain < 3; ++gain) {
|
|
||||||
for (int row = 0; row < raw_data.shape(1); ++row) {
|
|
||||||
for (int col = 0; col < raw_data.shape(2); ++col) {
|
|
||||||
if (count(gain, row, col) != 0) {
|
|
||||||
pedestal(gain, row, col) =
|
|
||||||
static_cast<T>(accumulator(gain, row, col)) /
|
|
||||||
static_cast<T>(count(gain, row, col));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return pedestal;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user