VTK-m  2.2
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 //simple functor that prints basic statistics
31 template <typename FieldType>
32 class VTKM_DEPRECATED(2.1, "Use DescriptiveStatistics or the statistics filter.") FieldStatistics
33 {
34 public:
35  // For moments readability
36  static constexpr vtkm::Id FIRST = 0;
37  static constexpr vtkm::Id SECOND = 1;
38  static constexpr vtkm::Id THIRD = 2;
39  static constexpr vtkm::Id FOURTH = 3;
40  static constexpr vtkm::Id NUM_POWERS = 4;
41 
42  struct StatInfo
43  {
44  FieldType minimum;
45  FieldType maximum;
46  FieldType median;
47  FieldType mean;
48  FieldType variance;
49  FieldType stddev;
50  FieldType skewness;
51  FieldType kurtosis;
52  FieldType rawMoment[4];
53  FieldType centralMoment[4];
54  };
55 
57  {
58  public:
59  using ControlSignature = void(FieldIn value,
60  FieldOut pow1Array,
61  FieldOut pow2Array,
62  FieldOut pow3Array,
63  FieldOut pow4Array);
64  using ExecutionSignature = void(_1, _2, _3, _4, _5);
65  using InputDomain = _1;
66 
68 
69  VTKM_CONT
71  : numPowers(num)
72  {
73  }
74 
75  VTKM_EXEC
76  void operator()(const FieldType& value,
77  FieldType& pow1,
78  FieldType& pow2,
79  FieldType& pow3,
80  FieldType& pow4) const
81  {
82  pow1 = value;
83  pow2 = pow1 * value;
84  pow3 = pow2 * value;
85  pow4 = pow3 * value;
86  }
87  };
88 
90  {
91  public:
92  using ControlSignature = void(FieldIn value, FieldOut diff);
93  using ExecutionSignature = _2(_1);
94  using InputDomain = _1;
95 
96  FieldType constant;
97 
98  VTKM_CONT
99  SubtractConst(const FieldType& constant0)
100  : constant(constant0)
101  {
102  }
103 
104  VTKM_EXEC
105  FieldType operator()(const FieldType& value) const { return (value - constant); }
106  };
107 
108  template <typename Storage>
110  {
111  using DeviceAlgorithms = vtkm::cont::Algorithm;
112 
113  // Copy original data to array for sorting
115  DeviceAlgorithms::Copy(fieldArray, tempArray);
116  DeviceAlgorithms::Sort(tempArray);
117 
118  vtkm::Id dataSize = tempArray.GetNumberOfValues();
119  FieldType numValues = static_cast<FieldType>(dataSize);
120  const auto firstAndMedian = vtkm::cont::ArrayGetValues({ 0, dataSize / 2 }, tempArray);
121 
122  // Median
123  statinfo.median = firstAndMedian[1];
124 
125  // Minimum and maximum
126  const vtkm::Vec<FieldType, 2> initValue(firstAndMedian[0]);
127  vtkm::Vec<FieldType, 2> result =
128  DeviceAlgorithms::Reduce(fieldArray, initValue, vtkm::MinAndMax<FieldType>());
129  statinfo.minimum = result[0];
130  statinfo.maximum = result[1];
131 
132  // Mean
133  FieldType sum = DeviceAlgorithms::ScanInclusive(fieldArray, tempArray);
134  statinfo.mean = sum / numValues;
135  statinfo.rawMoment[FIRST] = sum / numValues;
136 
137  // Create the power sum vector for each value
138  vtkm::cont::ArrayHandle<FieldType> pow1Array, pow2Array, pow3Array, pow4Array;
139  pow1Array.Allocate(dataSize);
140  pow2Array.Allocate(dataSize);
141  pow3Array.Allocate(dataSize);
142  pow4Array.Allocate(dataSize);
143 
144  // Raw moments via Worklet
145  vtkm::worklet::DispatcherMapField<CalculatePowers> calculatePowersDispatcher(
146  CalculatePowers(4));
147  calculatePowersDispatcher.Invoke(fieldArray, pow1Array, pow2Array, pow3Array, pow4Array);
148 
149  // Accumulate the results using ScanInclusive
150  statinfo.rawMoment[FIRST] = DeviceAlgorithms::ScanInclusive(pow1Array, pow1Array) / numValues;
151  statinfo.rawMoment[SECOND] = DeviceAlgorithms::ScanInclusive(pow2Array, pow2Array) / numValues;
152  statinfo.rawMoment[THIRD] = DeviceAlgorithms::ScanInclusive(pow3Array, pow3Array) / numValues;
153  statinfo.rawMoment[FOURTH] = DeviceAlgorithms::ScanInclusive(pow4Array, pow4Array) / numValues;
154 
155  // Subtract the mean from every value and leave in tempArray
156  vtkm::worklet::DispatcherMapField<SubtractConst> subtractConstDispatcher(
157  SubtractConst(statinfo.mean));
158  subtractConstDispatcher.Invoke(fieldArray, tempArray);
159 
160  // Calculate sums of powers on the (value - mean) array
161  calculatePowersDispatcher.Invoke(tempArray, pow1Array, pow2Array, pow3Array, pow4Array);
162 
163  // Accumulate the results using ScanInclusive
164  statinfo.centralMoment[FIRST] =
165  DeviceAlgorithms::ScanInclusive(pow1Array, pow1Array) / numValues;
166  statinfo.centralMoment[SECOND] =
167  DeviceAlgorithms::ScanInclusive(pow2Array, pow2Array) / numValues;
168  statinfo.centralMoment[THIRD] =
169  DeviceAlgorithms::ScanInclusive(pow3Array, pow3Array) / numValues;
170  statinfo.centralMoment[FOURTH] =
171  DeviceAlgorithms::ScanInclusive(pow4Array, pow4Array) / numValues;
172 
173  // Statistics from the moments
174  statinfo.variance = statinfo.centralMoment[SECOND];
175  statinfo.stddev = Sqrt(statinfo.variance);
176  statinfo.skewness =
177  statinfo.centralMoment[THIRD] / Pow(statinfo.stddev, static_cast<FieldType>(3.0));
178  statinfo.kurtosis =
179  statinfo.centralMoment[FOURTH] / Pow(statinfo.stddev, static_cast<FieldType>(4.0));
180  }
181 };
182 }
183 } // namespace vtkm::worklet
184 
185 #endif // vtk_m_worklet_FieldStatistics_h
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:300
ArrayHandle.h
vtkm::worklet::FieldStatistics::StatInfo::median
FieldType median
Definition: FieldStatistics.h:46
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::SubtractConst::SubtractConst
SubtractConst(const FieldType &constant0)
Definition: FieldStatistics.h:99
vtkm::Sqrt
vtkm::Float32 Sqrt(vtkm::Float32 x)
Definition: Math.h:943
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::worklet::WorkletMapField::FieldOut
A control signature tag for output fields.
Definition: WorkletMapField.h:80
vtkm::worklet::FieldStatistics::CalculatePowers::CalculatePowers
CalculatePowers(vtkm::Id num)
Definition: FieldStatistics.h:70
vtkm::worklet::FieldStatistics::SubtractConst::constant
FieldType constant
Definition: FieldStatistics.h:96
vtkm::worklet::FieldStatistics::SubtractConst
Definition: FieldStatistics.h:89
vtkm::worklet::FieldStatistics
Definition: FieldStatistics.h:32
vtkm::worklet::FieldStatistics::CalculatePowers::ExecutionSignature
void(_1, _2, _3, _4, _5) ExecutionSignature
Definition: FieldStatistics.h:64
vtkm::worklet::FieldStatistics::StatInfo::stddev
FieldType stddev
Definition: FieldStatistics.h:49
vtkm::worklet::FieldStatistics::CalculatePowers::InputDomain
_1 InputDomain
Definition: FieldStatistics.h:65
vtkm::cont::ArrayHandle::GetNumberOfValues
vtkm::Id GetNumberOfValues() const
Returns the number of entries in the array.
Definition: ArrayHandle.h:468
vtkm::worklet::FieldStatistics::StatInfo::minimum
FieldType minimum
Definition: FieldStatistics.h:44
vtkm::worklet::FieldStatistics::StatInfo::centralMoment
FieldType centralMoment[4]
Definition: FieldStatistics.h:53
DeviceAdapterAlgorithm.h
vtkm::worklet::FieldStatistics::StatInfo
Definition: FieldStatistics.h:42
DispatcherMapField.h
vtkm::worklet::FieldStatistics::StatInfo::kurtosis
FieldType kurtosis
Definition: FieldStatistics.h:51
vtkm::worklet::FieldStatistics::StatInfo::variance
FieldType variance
Definition: FieldStatistics.h:48
vtkm::worklet::FieldStatistics::CalculatePowers::ControlSignature
void(FieldIn value, FieldOut pow1Array, FieldOut pow2Array, FieldOut pow3Array, FieldOut pow4Array) ControlSignature
Definition: FieldStatistics.h:63
vtkm::worklet::FieldStatistics::Run
void Run(vtkm::cont::ArrayHandle< FieldType, Storage > fieldArray, StatInfo &statinfo)
Definition: FieldStatistics.h:109
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:52
Math.h
Algorithm.h
vtkm::worklet::WorkletMapField::FieldIn
A control signature tag for input fields.
Definition: WorkletMapField.h:60
vtkm::worklet::FieldStatistics::SubtractConst::ExecutionSignature
_2(_1) ExecutionSignature
Definition: FieldStatistics.h:93
vtkm::worklet::FieldStatistics::CalculatePowers
Definition: FieldStatistics.h:56
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
Field.h
vtkm::Id
vtkm::Int64 Id
Base type to use to index arrays.
Definition: Types.h:227
vtkm::worklet::FieldStatistics::SubtractConst::InputDomain
_1 InputDomain
Definition: FieldStatistics.h:94
ArrayGetValues.h
vtkm::worklet::FieldStatistics::StatInfo::skewness
FieldType skewness
Definition: FieldStatistics.h:50
vtkm::cont::Algorithm
Definition: Algorithm.h:386
vtkm::cont::ArrayGetValues
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::Vec
A short fixed-length array.
Definition: Types.h:357
vtkm::worklet::FieldStatistics::SubtractConst::operator()
FieldType operator()(const FieldType &value) const
Definition: FieldStatistics.h:105
vtkm::worklet::FieldStatistics::StatInfo::maximum
FieldType maximum
Definition: FieldStatistics.h:45
vtkm::worklet::FieldStatistics::CalculatePowers::numPowers
vtkm::Id numPowers
Definition: FieldStatistics.h:67
vtkm::worklet::DescriptiveStatistics
Definition: DescriptiveStatistics.h:22
vtkm::worklet::FieldStatistics::CalculatePowers::operator()
void operator()(const FieldType &value, FieldType &pow1, FieldType &pow2, FieldType &pow3, FieldType &pow4) const
Definition: FieldStatistics.h:76
vtkm::cont::ArrayHandle::Allocate
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:490
vtkm::worklet::FieldStatistics::SubtractConst::ControlSignature
void(FieldIn value, FieldOut diff) ControlSignature
Definition: FieldStatistics.h:92
vtkm::worklet::FieldStatistics::StatInfo::mean
FieldType mean
Definition: FieldStatistics.h:47
VTKM_DEPRECATED
#define VTKM_DEPRECATED(...)
Definition: Deprecated.h:145
vtkm::worklet::WorkletMapField
Base class for worklets that do a simple mapping of field arrays.
Definition: WorkletMapField.h:39