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