Merge topic 'off_topic'
3b0081b5 Merge branch 'master' of gitlab.kitware.com:vtk/vtk-m 06f400fd add .h .hxx into CMakeLists a7c14c93 fix trailing whitespace b9405680 Merge remote-tracking branch 'vtkmUpstream/master' f4494413 resolve the code review issue (2nd) a9e05698 add new line at the end of UnitTestEntropyFilter.cxx f295251d resolve the issue from the code review 7391d516 Add UnitTest for EntropyFilter ... Acked-by: Kitware Robot <kwrobot@kitware.com> Merge-request: !774
This commit is contained in:
commit
74f40e2026
@ -24,6 +24,7 @@ set(headers
|
||||
ClipWithField.h
|
||||
ClipWithImplicitFunction.h
|
||||
ContourTreeUniform.h
|
||||
Entropy.h
|
||||
ExternalFaces.h
|
||||
ExtractGeometry.h
|
||||
ExtractPoints.h
|
||||
@ -60,6 +61,7 @@ set(header_template_sources
|
||||
ClipWithField.hxx
|
||||
ClipWithImplicitFunction.hxx
|
||||
ContourTreeUniform.hxx
|
||||
Entropy.hxx
|
||||
ExternalFaces.hxx
|
||||
ExtractGeometry.hxx
|
||||
ExtractPoints.hxx
|
||||
|
65
vtkm/filter/Entropy.h
Normal file
65
vtkm/filter/Entropy.h
Normal file
@ -0,0 +1,65 @@
|
||||
//============================================================================
|
||||
// Copyright (c) Kitware, Inc.
|
||||
// All rights reserved.
|
||||
// See LICENSE.txt for details.
|
||||
// This software is distributed WITHOUT ANY WARRANTY; without even
|
||||
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
||||
// PURPOSE. See the above copyright notice for more information.
|
||||
//
|
||||
// Copyright 2014 Sandia Corporation.
|
||||
// Copyright 2014 UT-Battelle, LLC.
|
||||
// Copyright 2014 Los Alamos National Security.
|
||||
//
|
||||
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
|
||||
// the U.S. Government retains certain rights in this software.
|
||||
//
|
||||
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
|
||||
// Laboratory (LANL), the U.S. Government retains certain rights in
|
||||
// this software.
|
||||
//============================================================================
|
||||
|
||||
#ifndef vtk_m_filter_Entropy_h
|
||||
#define vtk_m_filter_Entropy_h
|
||||
|
||||
#include <vtkm/filter/FilterField.h>
|
||||
|
||||
namespace vtkm {
|
||||
namespace filter {
|
||||
|
||||
class Entropy : public vtkm::filter::FilterField<Entropy>
|
||||
{
|
||||
public:
|
||||
//Construct a histogram which is used to compute the entropy with a default of 10 bins
|
||||
VTKM_CONT
|
||||
Entropy();
|
||||
|
||||
VTKM_CONT
|
||||
void SetNumberOfBins(vtkm::Id count){ this->NumberOfBins = count; }
|
||||
|
||||
template<typename T, typename StorageType, typename DerivedPolicy, typename DeviceAdapter>
|
||||
VTKM_CONT
|
||||
vtkm::filter::ResultField DoExecute(const vtkm::cont::DataSet& input,
|
||||
const vtkm::cont::ArrayHandle<T, StorageType>& field,
|
||||
const vtkm::filter::FieldMetadata& fieldMeta,
|
||||
const vtkm::filter::PolicyBase<DerivedPolicy>& policy,
|
||||
const DeviceAdapter& tag);
|
||||
|
||||
private:
|
||||
vtkm::Id NumberOfBins;
|
||||
};
|
||||
|
||||
template<>
|
||||
class FilterTraits<Entropy>
|
||||
{
|
||||
public:
|
||||
typedef TypeListTagScalarAll InputFieldTypeList;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
} // namespace vtkm::filter
|
||||
|
||||
|
||||
#include <vtkm/filter/Entropy.hxx>
|
||||
|
||||
#endif // vtk_m_filter_Entropy_h
|
64
vtkm/filter/Entropy.hxx
Normal file
64
vtkm/filter/Entropy.hxx
Normal file
@ -0,0 +1,64 @@
|
||||
//============================================================================
|
||||
// Copyright (c) Kitware, Inc.
|
||||
// All rights reserved.
|
||||
// See LICENSE.txt for details.
|
||||
// This software is distributed WITHOUT ANY WARRANTY; without even
|
||||
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
||||
// PURPOSE. See the above copyright notice for more information.
|
||||
//
|
||||
// Copyright 2014 Sandia Corporation.
|
||||
// Copyright 2014 UT-Battelle, LLC.
|
||||
// Copyright 2014 Los Alamos National Security.
|
||||
//
|
||||
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
|
||||
// the U.S. Government retains certain rights in this software.
|
||||
//
|
||||
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
|
||||
// Laboratory (LANL), the U.S. Government retains certain rights in
|
||||
// this software.
|
||||
//============================================================================
|
||||
|
||||
#include <vtkm/worklet/DispatcherMapField.h>
|
||||
#include <vtkm/worklet/FieldEntropy.h>
|
||||
|
||||
namespace vtkm {
|
||||
namespace filter {
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
inline VTKM_CONT
|
||||
Entropy::Entropy():
|
||||
NumberOfBins(10)
|
||||
{
|
||||
this->SetOutputFieldName("entropy");
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template<typename T,
|
||||
typename StorageType,
|
||||
typename DerivedPolicy,
|
||||
typename DeviceAdapter>
|
||||
inline VTKM_CONT
|
||||
vtkm::filter::ResultField
|
||||
Entropy::DoExecute(const vtkm::cont::DataSet &inDataSet,
|
||||
const vtkm::cont::ArrayHandle<T,StorageType> &field,
|
||||
const vtkm::filter::FieldMetadata &fieldMetadata,
|
||||
const vtkm::filter::PolicyBase<DerivedPolicy>&,
|
||||
const DeviceAdapter& device)
|
||||
{
|
||||
vtkm::worklet::FieldEntropy worklet;
|
||||
|
||||
vtkm::Float64 e = worklet.Run(field, this->NumberOfBins, device);
|
||||
|
||||
//the entropy vector only contain one element, the entorpy of the input field
|
||||
vtkm::cont::ArrayHandle<vtkm::Float64> entropy;
|
||||
entropy.Allocate(1);
|
||||
entropy.GetPortalControl().Set(0, e);
|
||||
|
||||
return vtkm::filter::ResultField(inDataSet,
|
||||
entropy,
|
||||
this->GetOutputFieldName(),
|
||||
fieldMetadata.GetAssociation(),
|
||||
fieldMetadata.GetCellSetName());
|
||||
}
|
||||
}
|
||||
} // namespace vtkm::filter
|
@ -24,6 +24,7 @@ set(unit_tests
|
||||
UnitTestClipWithFieldFilter.cxx
|
||||
UnitTestClipWithImplicitFunctionFilter.cxx
|
||||
UnitTestContourTreeUniformFilter.cxx
|
||||
UnitTestEntropyFilter.cxx
|
||||
UnitTestExternalFacesFilter.cxx
|
||||
UnitTestExtractGeometryFilter.cxx
|
||||
UnitTestExtractPointsFilter.cxx
|
||||
|
129
vtkm/filter/testing/UnitTestEntropyFilter.cxx
Normal file
129
vtkm/filter/testing/UnitTestEntropyFilter.cxx
Normal file
@ -0,0 +1,129 @@
|
||||
//============================================================================
|
||||
// Copyright (c) Kitware, Inc.
|
||||
// All rights reserved.
|
||||
// See LICENSE.txt for details.
|
||||
// This software is distributed WITHOUT ANY WARRANTY; without even
|
||||
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
||||
// PURPOSE. See the above copyright notice for more information.
|
||||
//
|
||||
// Copyright 2014 Sandia Corporation.
|
||||
// Copyright 2014 UT-Battelle, LLC.
|
||||
// Copyright 2014 Los Alamos National Security.
|
||||
//
|
||||
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
|
||||
// the U.S. Government retains certain rights in this software.
|
||||
//
|
||||
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
|
||||
// Laboratory (LANL), the U.S. Government retains certain rights in
|
||||
// this software.
|
||||
//============================================================================
|
||||
|
||||
#include <vtkm/filter/Entropy.h>
|
||||
|
||||
#include <vtkm/cont/DataSet.h>
|
||||
#include <vtkm/cont/testing/Testing.h>
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
///// dataset and field generator from "vtkm-m/vtkm/rendering/testing/UnitTestMapperVolume.cxx" /////
|
||||
class TangleField : public vtkm::worklet::WorkletMapField
|
||||
{
|
||||
public:
|
||||
typedef void ControlSignature(FieldIn<IdType> vertexId, FieldOut<Scalar> v);
|
||||
typedef void ExecutionSignature(_1, _2);
|
||||
typedef _1 InputDomain;
|
||||
|
||||
const vtkm::Id xdim, ydim, zdim;
|
||||
const vtkm::Float32 xmin, ymin, zmin, xmax, ymax, zmax;
|
||||
const vtkm::Id cellsPerLayer;
|
||||
|
||||
VTKM_CONT
|
||||
TangleField(const vtkm::Id3 dims, const vtkm::Float32 mins[3], const vtkm::Float32 maxs[3]) : xdim(dims[0]), ydim(dims[1]), zdim(dims[2]),
|
||||
xmin(mins[0]), ymin(mins[1]), zmin(mins[2]), xmax(maxs[0]), ymax(maxs[1]), zmax(maxs[2]), cellsPerLayer((xdim) * (ydim)) { };
|
||||
|
||||
VTKM_EXEC
|
||||
void operator()(const vtkm::Id &vertexId, vtkm::Float32 &v) const
|
||||
{
|
||||
const vtkm::Id x = vertexId % (xdim);
|
||||
const vtkm::Id y = (vertexId / (xdim)) % (ydim);
|
||||
const vtkm::Id z = vertexId / cellsPerLayer;
|
||||
|
||||
const vtkm::Float32 fx = static_cast<vtkm::Float32>(x) / static_cast<vtkm::Float32>(xdim-1);
|
||||
const vtkm::Float32 fy = static_cast<vtkm::Float32>(y) / static_cast<vtkm::Float32>(xdim-1);
|
||||
const vtkm::Float32 fz = static_cast<vtkm::Float32>(z) / static_cast<vtkm::Float32>(xdim-1);
|
||||
|
||||
const vtkm::Float32 xx = 3.0f*(xmin+(xmax-xmin)*(fx));
|
||||
const vtkm::Float32 yy = 3.0f*(ymin+(ymax-ymin)*(fy));
|
||||
const vtkm::Float32 zz = 3.0f*(zmin+(zmax-zmin)*(fz));
|
||||
|
||||
v = (xx*xx*xx*xx - 5.0f*xx*xx + yy*yy*yy*yy - 5.0f*yy*yy + zz*zz*zz*zz - 5.0f*zz*zz + 11.8f) * 0.2f + 0.5f;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Construct an input data set using the tangle field worklet
|
||||
vtkm::cont::DataSet MakeIsosurfaceTestDataSet(vtkm::Id3 dims)
|
||||
{
|
||||
vtkm::cont::DataSet dataSet;
|
||||
|
||||
const vtkm::Id3 vdims(dims[0] + 1, dims[1] + 1, dims[2] + 1);
|
||||
|
||||
vtkm::Float32 mins[3] = {-1.0f, -1.0f, -1.0f};
|
||||
vtkm::Float32 maxs[3] = {1.0f, 1.0f, 1.0f};
|
||||
|
||||
vtkm::cont::ArrayHandle<vtkm::Float32> fieldArray;
|
||||
vtkm::cont::ArrayHandleCounting<vtkm::Id> vertexCountImplicitArray(0, 1, vdims[0]*vdims[1]*vdims[2]);
|
||||
vtkm::worklet::DispatcherMapField<TangleField> tangleFieldDispatcher(TangleField(vdims, mins, maxs));
|
||||
tangleFieldDispatcher.Invoke(vertexCountImplicitArray, fieldArray);
|
||||
|
||||
vtkm::Vec<vtkm::FloatDefault,3> origin(0.0f, 0.0f, 0.0f);
|
||||
vtkm::Vec<vtkm::FloatDefault,3> spacing(
|
||||
1.0f/static_cast<vtkm::FloatDefault>(dims[0]),
|
||||
1.0f/static_cast<vtkm::FloatDefault>(dims[2]),
|
||||
1.0f/static_cast<vtkm::FloatDefault>(dims[1]));
|
||||
|
||||
vtkm::cont::ArrayHandleUniformPointCoordinates
|
||||
coordinates(vdims, origin, spacing);
|
||||
dataSet.AddCoordinateSystem(
|
||||
vtkm::cont::CoordinateSystem("coordinates", coordinates));
|
||||
|
||||
dataSet.AddField(vtkm::cont::Field("nodevar", vtkm::cont::Field::ASSOC_POINTS, fieldArray));
|
||||
|
||||
static const vtkm::IdComponent ndim = 3;
|
||||
vtkm::cont::CellSetStructured<ndim> cellSet("cells");
|
||||
cellSet.SetPointDimensions(vdims);
|
||||
dataSet.AddCellSet(cellSet);
|
||||
|
||||
return dataSet;
|
||||
}
|
||||
|
||||
void TestEntropy()
|
||||
{
|
||||
///// make a data set /////
|
||||
vtkm::Id3 dims(32, 32, 32);
|
||||
vtkm::cont::DataSet dataSet = MakeIsosurfaceTestDataSet(dims);
|
||||
|
||||
vtkm::filter::ResultField resultEntropy;
|
||||
vtkm::filter::Entropy entropyFilter;
|
||||
|
||||
///// calculate entropy of "nodevar" field of the data set /////
|
||||
entropyFilter.SetNumberOfBins(50);//set number of bins
|
||||
resultEntropy = entropyFilter.Execute(dataSet, "nodevar");
|
||||
|
||||
///// get entropy from resultEntropy /////
|
||||
vtkm::cont::ArrayHandle<vtkm::Float64> entropy;
|
||||
resultEntropy.FieldAs(entropy);
|
||||
vtkm::cont::ArrayHandle<vtkm::Float64>::PortalConstControl portal = entropy.GetPortalConstControl();
|
||||
vtkm::Float64 entropyFromFilter = portal.Get(0);
|
||||
|
||||
/////// check if calculating entopry is close enough to ground truth value /////
|
||||
VTKM_TEST_ASSERT(fabs(entropyFromFilter - 4.59093) < 0.001, "Entropy calculation is incorrect");
|
||||
} // TestFieldEntropy
|
||||
|
||||
}
|
||||
|
||||
int UnitTestEntropyFilter(int, char *[])
|
||||
{
|
||||
return vtkm::cont::testing::Testing::Run(TestEntropy);
|
||||
}
|
@ -32,6 +32,7 @@ set(headers
|
||||
ExtractGeometry.h
|
||||
ExtractPoints.h
|
||||
ExtractStructured.h
|
||||
FieldEntropy.h
|
||||
FieldHistogram.h
|
||||
FieldStatistics.h
|
||||
Gradient.h
|
||||
|
105
vtkm/worklet/FieldEntropy.h
Normal file
105
vtkm/worklet/FieldEntropy.h
Normal file
@ -0,0 +1,105 @@
|
||||
//============================================================================
|
||||
// Copyright (c) Kitware, Inc.
|
||||
// All rights reserved.
|
||||
// See LICENSE.txt for details.
|
||||
// This software is distributed WITHOUT ANY WARRANTY; without even
|
||||
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
||||
// PURPOSE. See the above copyright notice for more information.
|
||||
//
|
||||
// Copyright 2014 Sandia Corporation.
|
||||
// Copyright 2014 UT-Battelle, LLC.
|
||||
// Copyright 2014 Los Alamos National Security.
|
||||
//
|
||||
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
|
||||
// the U.S. Government retains certain rights in this software.
|
||||
//
|
||||
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
|
||||
// Laboratory (LANL), the U.S. Government retains certain rights in
|
||||
// this software.
|
||||
//============================================================================
|
||||
|
||||
#ifndef vtk_m_worklet_FieldEntropy_h
|
||||
#define vtk_m_worklet_FieldEntropy_h
|
||||
|
||||
#include <vtkm/Math.h>
|
||||
#include <vtkm/cont/ArrayHandle.h>
|
||||
#include <vtkm/cont/ArrayHandleCounting.h>
|
||||
#include <vtkm/worklet/DispatcherMapField.h>
|
||||
#include <vtkm/worklet/WorkletMapField.h>
|
||||
#include <vtkm/cont/DeviceAdapter.h>
|
||||
#include <vtkm/worklet/FieldHistogram.h>
|
||||
|
||||
#include <vtkm/cont/Field.h>
|
||||
|
||||
namespace vtkm {
|
||||
namespace worklet {
|
||||
|
||||
//simple functor that returns basic statistics
|
||||
class FieldEntropy
|
||||
{
|
||||
public:
|
||||
// For each bin, calculate its information content (log2)
|
||||
class SetBinInformationContent : public vtkm::worklet::WorkletMapField
|
||||
{
|
||||
public:
|
||||
typedef void ControlSignature(FieldIn<> freq,
|
||||
FieldOut<> informationContent);
|
||||
typedef void ExecutionSignature(_1,_2);
|
||||
|
||||
vtkm::Float64 FreqSum;
|
||||
|
||||
VTKM_CONT
|
||||
SetBinInformationContent(vtkm::Float64 _freqSum) : FreqSum(_freqSum) {}
|
||||
|
||||
template<typename FreqType>
|
||||
VTKM_EXEC
|
||||
void operator()(const FreqType &freq, vtkm::Float64 &informationContent) const
|
||||
{
|
||||
vtkm::Float64 p = ( (vtkm::Float64)freq ) / FreqSum;
|
||||
if( p > 0 )
|
||||
informationContent = -1 * p * vtkm::Log2(p);
|
||||
else
|
||||
informationContent = 0;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Execute the entropy computation filter given data(a field) and number of bins
|
||||
// Returns:
|
||||
// Entropy (log2) of the field of the data
|
||||
template<typename FieldType, typename Storage, typename DeviceAdapter>
|
||||
vtkm::Float64 Run(vtkm::cont::ArrayHandle<FieldType, Storage> fieldArray,
|
||||
vtkm::Id numberOfBins,
|
||||
DeviceAdapter device)
|
||||
{
|
||||
typedef typename vtkm::cont::DeviceAdapterAlgorithm<DeviceAdapter> DeviceAlgorithms;
|
||||
|
||||
///// calculate histogram using FieldHistogram worklet /////
|
||||
vtkm::Range range;
|
||||
FieldType delta;
|
||||
vtkm::cont::ArrayHandle<vtkm::Id> binArray;
|
||||
vtkm::worklet::FieldHistogram histogram;
|
||||
histogram.Run(fieldArray, numberOfBins, range, delta, binArray, device);
|
||||
|
||||
///// calculate sum of frequency of the histogram /////
|
||||
vtkm::Id initFreqSumValue = 0;
|
||||
vtkm::Id freqSum = DeviceAlgorithms::Reduce(binArray, initFreqSumValue, vtkm::Sum());
|
||||
|
||||
///// calculate information content of each bin using self-define worklet /////
|
||||
vtkm::cont::ArrayHandle<vtkm::Float64> informationContent;
|
||||
SetBinInformationContent binWorklet( static_cast<vtkm::Float64> (freqSum) );
|
||||
vtkm::worklet::DispatcherMapField< SetBinInformationContent > setBinInformationContentDispatcher(binWorklet);
|
||||
setBinInformationContentDispatcher.Invoke(binArray, informationContent);
|
||||
|
||||
///// calculate entropy by summing up information conetent of all bins /////
|
||||
vtkm::Float64 initEntropyValue = 0;
|
||||
vtkm::Float64 entropy = DeviceAlgorithms::Reduce(informationContent, initEntropyValue, vtkm::Sum());
|
||||
|
||||
return entropy;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace vtkm::worklet
|
||||
|
||||
#endif // vtk_m_worklet_FieldEntropy_h
|
@ -483,6 +483,7 @@ public:
|
||||
{
|
||||
nthVal *= -1.0;
|
||||
}
|
||||
|
||||
typedef vtkm::worklet::wavelets::ThresholdWorklet ThresholdType;
|
||||
ThresholdType thresholdWorklet(nthVal);
|
||||
vtkm::worklet::DispatcherMapField<ThresholdType, DeviceTag> dispatcher(thresholdWorklet);
|
||||
|
Loading…
Reference in New Issue
Block a user