VTK-m  2.0
FieldEntropy.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_FieldEntropy_h
12 #define vtk_m_worklet_FieldEntropy_h
13 
14 #include <vtkm/Math.h>
15 #include <vtkm/cont/Algorithm.h>
16 #include <vtkm/cont/ArrayHandle.h>
21 
22 #include <vtkm/cont/Field.h>
23 
24 namespace vtkm
25 {
26 namespace worklet
27 {
28 
29 //simple functor that returns basic statistics
31 {
32 public:
33  // For each bin, calculate its information content (log2)
35  {
36  public:
37  using ControlSignature = void(FieldIn freq, FieldOut informationContent);
38  using ExecutionSignature = void(_1, _2);
39 
41 
42  VTKM_CONT
44  : FreqSum(_freqSum)
45  {
46  }
47 
48  template <typename FreqType>
49  VTKM_EXEC void operator()(const FreqType& freq, vtkm::Float64& informationContent) const
50  {
51  vtkm::Float64 p = ((vtkm::Float64)freq) / FreqSum;
52  if (p > 0)
53  informationContent = -1 * p * vtkm::Log2(p);
54  else
55  informationContent = 0;
56  }
57  };
58 
59 
60  // Execute the entropy computation filter given data(a field) and number of bins
61  // Returns:
62  // Entropy (log2) of the field of the data
63  template <typename FieldType, typename Storage>
65  {
67  vtkm::Range range;
68  FieldType delta;
71  histogram.Run(fieldArray, numberOfBins, range, delta, binArray);
72 
74  vtkm::Id initFreqSumValue = 0;
75  vtkm::Id freqSum = vtkm::cont::Algorithm::Reduce(binArray, initFreqSumValue, vtkm::Sum());
76 
78  vtkm::cont::ArrayHandle<vtkm::Float64> informationContent;
79  SetBinInformationContent binWorklet(static_cast<vtkm::Float64>(freqSum));
80  vtkm::worklet::DispatcherMapField<SetBinInformationContent> setBinInformationContentDispatcher(
81  binWorklet);
82  setBinInformationContentDispatcher.Invoke(binArray, informationContent);
83 
85  vtkm::Float64 initEntropyValue = 0;
86  vtkm::Float64 entropy =
87  vtkm::cont::Algorithm::Reduce(informationContent, initEntropyValue, vtkm::Sum());
88 
89  return entropy;
90  }
91 };
92 }
93 } // namespace vtkm::worklet
94 
95 #endif // vtk_m_worklet_FieldEntropy_h
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:283
ArrayHandle.h
vtkm::worklet::FieldEntropy::Run
vtkm::Float64 Run(vtkm::cont::ArrayHandle< FieldType, Storage > fieldArray, vtkm::Id numberOfBins)
Definition: FieldEntropy.h:64
VTKM_EXEC
#define VTKM_EXEC
Definition: ExportMacros.h:51
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
WorkletMapField.h
vtkm::worklet::WorkletMapField::FieldOut
A control signature tag for output fields.
Definition: WorkletMapField.h:60
vtkm::worklet::FieldEntropy::SetBinInformationContent
Definition: FieldEntropy.h:34
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
DispatcherMapField.h
vtkm::Log2
VTKM_EXEC_CONT vtkm::Float32 Log2(vtkm::Float32 x)
Computes the logarithm base 2 of x.
Definition: Math.h:1514
vtkm::Sum
Binary Predicate that takes two arguments argument x, and y and returns sum (addition) of the two val...
Definition: BinaryOperators.h:33
vtkm::worklet::DispatcherMapField
Dispatcher for worklets that inherit from WorkletMapField.
Definition: DispatcherMapField.h:25
Math.h
Algorithm.h
vtkm::worklet::WorkletMapField::FieldIn
A control signature tag for input fields.
Definition: WorkletMapField.h:49
vtkm::worklet::FieldEntropy::SetBinInformationContent::SetBinInformationContent
VTKM_CONT SetBinInformationContent(vtkm::Float64 _freqSum)
Definition: FieldEntropy.h:43
vtkm::worklet::FieldEntropy::SetBinInformationContent::operator()
VTKM_EXEC void operator()(const FreqType &freq, vtkm::Float64 &informationContent) const
Definition: FieldEntropy.h:49
vtkm::worklet::FieldEntropy::SetBinInformationContent::ExecutionSignature
void(_1, _2) ExecutionSignature
Definition: FieldEntropy.h:38
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::worklet::FieldHistogram::Run
void Run(vtkm::cont::ArrayHandle< FieldType, Storage > fieldArray, vtkm::Id numberOfBins, vtkm::Range &rangeOfValues, FieldType &binDelta, vtkm::cont::ArrayHandle< vtkm::Id > &binArray)
Definition: FieldHistogram.h:114
vtkm::cont::Algorithm::Reduce
static VTKM_CONT U Reduce(vtkm::cont::DeviceAdapterId devId, const vtkm::cont::ArrayHandle< T, CIn > &input, U initialValue)
Definition: Algorithm.h:656
FieldHistogram.h
ArrayHandleCounting.h
vtkm::worklet::FieldEntropy::SetBinInformationContent::FreqSum
vtkm::Float64 FreqSum
Definition: FieldEntropy.h:40
Field.h
vtkm::Float64
double Float64
Definition: Types.h:155
vtkm::worklet::FieldEntropy::SetBinInformationContent::ControlSignature
void(FieldIn freq, FieldOut informationContent) ControlSignature
Definition: FieldEntropy.h:37
vtkm::worklet::FieldHistogram
Definition: FieldHistogram.h:53
vtkm::worklet::FieldEntropy
Definition: FieldEntropy.h:30
vtkm::worklet::WorkletMapField
Base class for worklets that do a simple mapping of field arrays.
Definition: WorkletMapField.h:38
vtkm::Range
Represent a continuous scalar range of values.
Definition: Range.h:31