GooseEYE 0.9.1
Loading...
Searching...
No Matches
Ensemble_W2.hpp
Go to the documentation of this file.
1
7#ifndef GOOSEEYE_ENSEMBLE_W2_HPP
8#define GOOSEEYE_ENSEMBLE_W2_HPP
9
10#include "GooseEYE.h"
11
12namespace GooseEYE {
13
14template <class T, class M>
15inline void Ensemble::W2(const T& f, const T& g, const M& gmask)
16{
17 using value_type = typename T::value_type;
18 using mask_type = typename M::value_type;
19
20 static_assert(std::is_integral<mask_type>::value, "Integral mask required.");
21
22 GOOSEEYE_ASSERT(xt::has_shape(f, g.shape()), std::out_of_range);
23 GOOSEEYE_ASSERT(xt::has_shape(f, gmask.shape()), std::out_of_range);
24 GOOSEEYE_ASSERT(f.dimension() == m_shape_orig.size(), std::out_of_range);
25 GOOSEEYE_ASSERT(xt::all(xt::equal(gmask, 0) || xt::equal(gmask, 1)), std::out_of_range);
26 GOOSEEYE_ASSERT(m_stat == Type::W2 || m_stat == Type::Unset, std::out_of_range);
27
28 // lock statistic
29 m_stat = Type::W2;
30
31 // not periodic (default): mask padded items
32 xt::pad_mode pad_mode = xt::pad_mode::constant;
33 int mask_value = 1;
34
35 // periodic: unmask padded items
36 if (m_periodic) {
37 pad_mode = xt::pad_mode::periodic;
38 mask_value = 0;
39 }
40
41 // apply padding
42 array_type::tensor<value_type, 3> F = xt::pad(xt::atleast_3d(f), m_pad, pad_mode);
43 array_type::tensor<double, 3> Fd = xt::pad(xt::atleast_3d(f), m_pad, pad_mode);
44 array_type::tensor<double, 3> G = xt::pad(xt::atleast_3d(g), m_pad, pad_mode);
46 xt::pad(xt::atleast_3d(gmask), m_pad, xt::pad_mode::constant, mask_value);
47
48 // compute correlation
49 for (size_t h = m_pad[0][0]; h < F.shape(0) - m_pad[0][1]; ++h) {
50 for (size_t i = m_pad[1][0]; i < F.shape(1) - m_pad[1][1]; ++i) {
51 for (size_t j = m_pad[2][0]; j < F.shape(2) - m_pad[2][1]; ++j) {
52 // - get comparison sub-matrix
53 auto Gi = xt::view(
54 G,
55 xt::range(h - m_pad[0][0], h + m_pad[0][1] + 1),
56 xt::range(i - m_pad[1][0], i + m_pad[1][1] + 1),
57 xt::range(j - m_pad[2][0], j + m_pad[2][1] + 1));
58 // - get inverse of comparison mask
59 auto Gmii = 1.0 - xt::view(
60 Gmask,
61 xt::range(h - m_pad[0][0], h + m_pad[0][1] + 1),
62 xt::range(i - m_pad[1][0], i + m_pad[1][1] + 1),
63 xt::range(j - m_pad[2][0], j + m_pad[2][1] + 1));
64 // - correlation (account for mask)
65 if (F(h, i, j) != 0) {
66 m_first += Fd(h, i, j) * Gi * Gmii;
67 }
68 // - normalisation
69 m_norm += F(h, i, j) * Gmii;
70 }
71 }
72 }
73}
74
75template <class T>
76inline void Ensemble::W2(const T& f, const T& g)
77{
78 array_type::array<int> mask = xt::zeros<int>(f.shape());
79 W2(f, g, mask);
80}
81
82} // namespace GooseEYE
83
84#endif
void W2(const T &w, const T &f)
Add realization to weighted 2-point correlation.
#define GOOSEEYE_ASSERT(expr, assertion)
All assertions are implemented as:
Definition config.h:91
xt::xtensor< T, N > tensor
Fixed (static) rank array.
Definition config.h:155
Toolbox to compute statistics.
Definition config.h:128