VTK-m  2.0
FieldStatistics.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 
11 #ifndef vtk_m_worklet_FieldStatistics_h
12 #define vtk_m_worklet_FieldStatistics_h
13 
14 #include <vtkm/Math.h>
15 #include <vtkm/cont/Algorithm.h>
17 #include <vtkm/cont/ArrayHandle.h>
21 
22 #include <vtkm/cont/Field.h>
23 
24 #include <stdio.h>
25 
26 namespace vtkm
27 {
28 namespace worklet
29 {
30 
31 //simple functor that prints basic statistics
32 template <typename FieldType>
34 {
35 public:
36  // For moments readability
37  static constexpr vtkm::Id FIRST = 0;
38  static constexpr vtkm::Id SECOND = 1;
39  static constexpr vtkm::Id THIRD = 2;
40  static constexpr vtkm::Id FOURTH = 3;
41  static constexpr vtkm::Id NUM_POWERS = 4;
42 
43  struct StatInfo
44  {
45  FieldType minimum;
46  FieldType maximum;
47  FieldType median;
48  FieldType mean;
49  FieldType variance;
50  FieldType stddev;
51  FieldType skewness;
52  FieldType kurtosis;
53  FieldType rawMoment[4];
54  FieldType centralMoment[4];
55  };
56 
58  {
59  public:
60  using ControlSignature = void(FieldIn value,
61  FieldOut pow1Array,
62  FieldOut pow2Array,
63  FieldOut pow3Array,
64  FieldOut pow4Array);
65  using ExecutionSignature = void(_1, _2, _3, _4, _5);
66  using InputDomain = _1;
67 
69 
70  VTKM_CONT
72  : numPowers(num)
73  {
74  }
75 
76  VTKM_EXEC
77  void operator()(const FieldType& value,
78  FieldType& pow1,
79  FieldType& pow2,
80  FieldType& pow3,
81  FieldType& pow4) const
82  {
83  pow1 = value;
84  pow2 = pow1 * value;
85  pow3 = pow2 * value;
86  pow4 = pow3 * value;
87  }
88  };
89 
91  {
92  public:
93  using ControlSignature = void(FieldIn value, FieldOut diff);
94  using ExecutionSignature = _2(_1);
95  using InputDomain = _1;
96 
97  FieldType constant;
98 
99  VTKM_CONT
100  SubtractConst(const FieldType& constant0)
101  : constant(constant0)
102  {
103  }
104 
105  VTKM_EXEC
106  FieldType operator()(const FieldType& value) const { return (value - constant); }
107  };
108 
109  template <typename Storage>
111  {
112  using DeviceAlgorithms = vtkm::cont::Algorithm;
113 
114  // Copy original data to array for sorting
116  DeviceAlgorithms::Copy(fieldArray, tempArray);
117  DeviceAlgorithms::Sort(tempArray);
118 
119  vtkm::Id dataSize = tempArray.GetNumberOfValues();
120  FieldType numValues = static_cast<FieldType>(dataSize);
121  const auto firstAndMedian = vtkm::cont::ArrayGetValues({ 0, dataSize / 2 }, tempArray);
122 
123  // Median
124  statinfo.median = firstAndMedian[1];
125 
126  // Minimum and maximum
127  const vtkm::Vec<FieldType, 2> initValue(firstAndMedian[0]);
128  vtkm::Vec<FieldType, 2> result =
129  DeviceAlgorithms::Reduce(fieldArray, initValue, vtkm::MinAndMax<FieldType>());
130  statinfo.minimum = result[0];
131  statinfo.maximum = result[1];
132 
133  // Mean
134  FieldType sum = DeviceAlgorithms::ScanInclusive(fieldArray, tempArray);
135  statinfo.mean = sum / numValues;
136  statinfo.rawMoment[FIRST] = sum / numValues;
137 
138  // Create the power sum vector for each value
139  vtkm::cont::ArrayHandle<FieldType> pow1Array, pow2Array, pow3Array, pow4Array;
140  pow1Array.Allocate(dataSize);
141  pow2Array.Allocate(dataSize);
142  pow3Array.Allocate(dataSize);
143  pow4Array.Allocate(dataSize);
144 
145  // Raw moments via Worklet
146  vtkm::worklet::DispatcherMapField<CalculatePowers> calculatePowersDispatcher(
147  CalculatePowers(4));
148  calculatePowersDispatcher.Invoke(fieldArray, pow1Array, pow2Array, pow3Array, pow4Array);
149 
150  // Accumulate the results using ScanInclusive
151  statinfo.rawMoment[FIRST] = DeviceAlgorithms::ScanInclusive(pow1Array, pow1Array) / numValues;
152  statinfo.rawMoment[SECOND] = DeviceAlgorithms::ScanInclusive(pow2Array, pow2Array) / numValues;
153  statinfo.rawMoment[THIRD] = DeviceAlgorithms::ScanInclusive(pow3Array, pow3Array) / numValues;
154  statinfo.rawMoment[FOURTH] = DeviceAlgorithms::ScanInclusive(pow4Array, pow4Array) / numValues;
155 
156  // Subtract the mean from every value and leave in tempArray
157  vtkm::worklet::DispatcherMapField<SubtractConst> subtractConstDispatcher(
158  SubtractConst(statinfo.mean));
159  subtractConstDispatcher.Invoke(fieldArray, tempArray);
160 
161  // Calculate sums of powers on the (value - mean) array
162  calculatePowersDispatcher.Invoke(tempArray, pow1Array, pow2Array, pow3Array, pow4Array);
163 
164  // Accumulate the results using ScanInclusive
165  statinfo.centralMoment[FIRST] =
166  DeviceAlgorithms::ScanInclusive(pow1Array, pow1Array) / numValues;
167  statinfo.centralMoment[SECOND] =
168  DeviceAlgorithms::ScanInclusive(pow2Array, pow2Array) / numValues;
169  statinfo.centralMoment[THIRD] =
170  DeviceAlgorithms::ScanInclusive(pow3Array, pow3Array) / numValues;
171  statinfo.centralMoment[FOURTH] =
172  DeviceAlgorithms::ScanInclusive(pow4Array, pow4Array) / numValues;
173 
174  // Statistics from the moments
175  statinfo.variance = statinfo.centralMoment[SECOND];
176  statinfo.stddev = Sqrt(statinfo.variance);
177  statinfo.skewness =
178  statinfo.centralMoment[THIRD] / Pow(statinfo.stddev, static_cast<FieldType>(3.0));
179  statinfo.kurtosis =
180  statinfo.centralMoment[FOURTH] / Pow(statinfo.stddev, static_cast<FieldType>(4.0));
181  }
182 };
183 }
184 } // namespace vtkm::worklet
185 
186 #endif // vtk_m_worklet_FieldStatistics_h
vtkm::worklet::FieldStatistics::FOURTH
static constexpr vtkm::Id FOURTH
Definition: FieldStatistics.h:40
vtkm::cont::ArrayHandle::GetNumberOfValues
VTKM_CONT vtkm::Id GetNumberOfValues() const
Returns the number of entries in the array.
Definition: ArrayHandle.h:448
vtkm::Sqrt
VTKM_EXEC_CONT vtkm::Float32 Sqrt(vtkm::Float32 x)
Compute the square root of x.
Definition: Math.h:958
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:283
ArrayHandle.h
vtkm::worklet::FieldStatistics::StatInfo::median
FieldType median
Definition: FieldStatistics.h:47
vtkm::worklet::FieldStatistics::CalculatePowers::CalculatePowers
VTKM_CONT CalculatePowers(vtkm::Id num)
Definition: FieldStatistics.h:71
VTKM_EXEC
#define VTKM_EXEC
Definition: ExportMacros.h:51
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::worklet::FieldStatistics::CalculatePowers::operator()
VTKM_EXEC void operator()(const FieldType &value, FieldType &pow1, FieldType &pow2, FieldType &pow3, FieldType &pow4) const
Definition: FieldStatistics.h:77
WorkletMapField.h
vtkm::MinAndMax
Binary Predicate that takes two arguments argument x, and y and returns a vtkm::Vec<T,...
Definition: BinaryOperators.h:112
vtkm::cont::ArrayHandle::Allocate
VTKM_CONT void Allocate(vtkm::Id numberOfValues, vtkm::CopyFlag preserve, vtkm::cont::Token &token) const
Allocates an array large enough to hold the given number of values.
Definition: ArrayHandle.h:465
vtkm::worklet::WorkletMapField::FieldOut
A control signature tag for output fields.
Definition: WorkletMapField.h:60
vtkm::worklet::FieldStatistics::SubtractConst::constant
FieldType constant
Definition: FieldStatistics.h:97
vtkm::worklet::FieldStatistics::NUM_POWERS
static constexpr vtkm::Id NUM_POWERS
Definition: FieldStatistics.h:41
vtkm::worklet::FieldStatistics::SubtractConst
Definition: FieldStatistics.h:90
vtkm::worklet::FieldStatistics
Definition: FieldStatistics.h:33
vtkm::worklet::FieldStatistics::THIRD
static constexpr vtkm::Id THIRD
Definition: FieldStatistics.h:39
vtkm::worklet::FieldStatistics::CalculatePowers::ExecutionSignature
void(_1, _2, _3, _4, _5) ExecutionSignature
Definition: FieldStatistics.h:65
vtkm::worklet::FieldStatistics::StatInfo::stddev
FieldType stddev
Definition: FieldStatistics.h:50
vtkm::worklet::FieldStatistics::CalculatePowers::InputDomain
_1 InputDomain
Definition: FieldStatistics.h:66
vtkm::cont::ArrayGetValues
VTKM_CONT void ArrayGetValues(const vtkm::cont::ArrayHandle< vtkm::Id, SIds > &ids, const vtkm::cont::ArrayHandle< T, SData > &data, vtkm::cont::ArrayHandle< T, SOut > &output)
Obtain a small set of values from an ArrayHandle with minimal device transfers.
Definition: ArrayGetValues.h:119
vtkm::worklet::FieldStatistics::StatInfo::minimum
FieldType minimum
Definition: FieldStatistics.h:45
vtkm::worklet::FieldStatistics::StatInfo::centralMoment
FieldType centralMoment[4]
Definition: FieldStatistics.h:54
DeviceAdapterAlgorithm.h
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
vtkm::worklet::FieldStatistics::StatInfo
Definition: FieldStatistics.h:43
DispatcherMapField.h
vtkm::worklet::FieldStatistics::StatInfo::kurtosis
FieldType kurtosis
Definition: FieldStatistics.h:52
vtkm::worklet::FieldStatistics::StatInfo::variance
FieldType variance
Definition: FieldStatistics.h:49
vtkm::worklet::FieldStatistics::CalculatePowers::ControlSignature
void(FieldIn value, FieldOut pow1Array, FieldOut pow2Array, FieldOut pow3Array, FieldOut pow4Array) ControlSignature
Definition: FieldStatistics.h:64
vtkm::worklet::FieldStatistics::Run
void Run(vtkm::cont::ArrayHandle< FieldType, Storage > fieldArray, StatInfo &statinfo)
Definition: FieldStatistics.h:110
vtkm::worklet::DispatcherMapField
Dispatcher for worklets that inherit from WorkletMapField.
Definition: DispatcherMapField.h:25
vtkm::worklet::FieldStatistics::StatInfo::rawMoment
FieldType rawMoment[4]
Definition: FieldStatistics.h:53
Math.h
Algorithm.h
vtkm::worklet::WorkletMapField::FieldIn
A control signature tag for input fields.
Definition: WorkletMapField.h:49
vtkm::worklet::FieldStatistics::SubtractConst::ExecutionSignature
_2(_1) ExecutionSignature
Definition: FieldStatistics.h:94
vtkm::worklet::FieldStatistics::CalculatePowers
Definition: FieldStatistics.h:57
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::worklet::FieldStatistics::SubtractConst::InputDomain
_1 InputDomain
Definition: FieldStatistics.h:95
ArrayGetValues.h
vtkm::worklet::FieldStatistics::StatInfo::skewness
FieldType skewness
Definition: FieldStatistics.h:51
vtkm::cont::Algorithm
Definition: Algorithm.h:385
vtkm::Vec
A short fixed-length array.
Definition: Types.h:767
vtkm::worklet::FieldStatistics::FIRST
static constexpr vtkm::Id FIRST
Definition: FieldStatistics.h:37
vtkm::worklet::FieldStatistics::StatInfo::maximum
FieldType maximum
Definition: FieldStatistics.h:46
vtkm::worklet::FieldStatistics::CalculatePowers::numPowers
vtkm::Id numPowers
Definition: FieldStatistics.h:68
Field.h
vtkm::worklet::FieldStatistics::SubtractConst::SubtractConst
VTKM_CONT SubtractConst(const FieldType &constant0)
Definition: FieldStatistics.h:100
vtkm::worklet::FieldStatistics::SubtractConst::ControlSignature
void(FieldIn value, FieldOut diff) ControlSignature
Definition: FieldStatistics.h:93
vtkm::worklet::FieldStatistics::StatInfo::mean
FieldType mean
Definition: FieldStatistics.h:48
vtkm::worklet::FieldStatistics::SubtractConst::operator()
VTKM_EXEC FieldType operator()(const FieldType &value) const
Definition: FieldStatistics.h:106
vtkm::worklet::FieldStatistics::SECOND
static constexpr vtkm::Id SECOND
Definition: FieldStatistics.h:38
vtkm::worklet::WorkletMapField
Base class for worklets that do a simple mapping of field arrays.
Definition: WorkletMapField.h:38