2017-09-02 22:35:21 +00:00
|
|
|
//============================================================================
|
|
|
|
// 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.
|
|
|
|
//
|
2017-09-20 21:33:44 +00:00
|
|
|
// Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
|
2017-09-02 22:35:21 +00:00
|
|
|
// Copyright 2014 UT-Battelle, LLC.
|
|
|
|
// Copyright 2014 Los Alamos National Security.
|
|
|
|
//
|
2017-09-20 21:33:44 +00:00
|
|
|
// Under the terms of Contract DE-NA0003525 with NTESS,
|
2017-09-02 22:35:21 +00:00
|
|
|
// 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_NDimsHistMarginalization_h
|
|
|
|
#define vtk_m_worklet_NDimsHistMarginalization_h
|
|
|
|
|
|
|
|
#include <vtkm/Math.h>
|
2018-08-30 15:53:18 +00:00
|
|
|
#include <vtkm/cont/Algorithm.h>
|
|
|
|
#include <vtkm/cont/ArrayCopy.h>
|
2017-09-02 22:35:21 +00:00
|
|
|
#include <vtkm/cont/ArrayHandle.h>
|
|
|
|
#include <vtkm/cont/ArrayHandleCounting.h>
|
|
|
|
#include <vtkm/cont/DataSet.h>
|
|
|
|
#include <vtkm/worklet/DispatcherMapField.h>
|
|
|
|
#include <vtkm/worklet/WorkletMapField.h>
|
|
|
|
#include <vtkm/worklet/histogram/ComputeNDHistogram.h>
|
|
|
|
#include <vtkm/worklet/histogram/ComputeNDHistogram.h>
|
|
|
|
#include <vtkm/worklet/histogram/ComputeNDHistogram.h>
|
|
|
|
#include <vtkm/worklet/histogram/MarginalizeNDHistogram.h>
|
|
|
|
#include <vtkm/worklet/histogram/MarginalizeNDHistogram.h>
|
|
|
|
#include <vtkm/worklet/histogram/MarginalizeNDHistogram.h>
|
|
|
|
|
|
|
|
#include <vtkm/cont/Field.h>
|
|
|
|
|
|
|
|
namespace vtkm
|
|
|
|
{
|
|
|
|
namespace worklet
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
class NDimsHistMarginalization
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Execute the histogram (conditional) marginalization,
|
|
|
|
// given the multi-variable histogram(binId, freqIn)
|
|
|
|
// , marginalVariable and marginal condition
|
|
|
|
// Input arguments:
|
|
|
|
// binId, freqsIn: input ND-histogram in the fashion of sparse representation
|
|
|
|
// (definition of binId and frqIn please refer to NDimsHistogram.h),
|
|
|
|
// (binId.size() is the number of variables)
|
|
|
|
// numberOfBins: number of bins of each variable (length of numberOfBins must be the same as binId.size() )
|
|
|
|
// marginalVariables: length is the same as number of variables.
|
|
|
|
// 1 indicates marginal variable, otherwise 0.
|
|
|
|
// conditionFunc: The Condition function for non-marginal variable.
|
|
|
|
// This func takes two arguments (vtkm::Id var, vtkm::Id binId) and return bool
|
2018-10-03 18:51:13 +00:00
|
|
|
// var is index of variable and binId is bin index in the variable var
|
2017-09-02 22:35:21 +00:00
|
|
|
// return true indicates considering this bin into final marginal histogram
|
|
|
|
// more details can refer to example in UnitTestNDimsHistMarginalization.cxx
|
|
|
|
// marginalBinId, marginalFreqs: return marginalized histogram in the fashion of sparse representation
|
|
|
|
// the definition is the same as (binId and freqsIn)
|
2018-08-30 15:53:18 +00:00
|
|
|
template <typename BinaryCompare>
|
2017-09-02 22:35:21 +00:00
|
|
|
void Run(const std::vector<vtkm::cont::ArrayHandle<vtkm::Id>>& binId,
|
|
|
|
vtkm::cont::ArrayHandle<vtkm::Id>& freqsIn,
|
|
|
|
vtkm::cont::ArrayHandle<vtkm::Id>& numberOfBins,
|
|
|
|
vtkm::cont::ArrayHandle<bool>& marginalVariables,
|
|
|
|
BinaryCompare conditionFunc,
|
|
|
|
std::vector<vtkm::cont::ArrayHandle<vtkm::Id>>& marginalBinId,
|
2018-08-30 15:53:18 +00:00
|
|
|
vtkm::cont::ArrayHandle<vtkm::Id>& marginalFreqs)
|
2017-09-02 22:35:21 +00:00
|
|
|
{
|
|
|
|
//total variables
|
|
|
|
vtkm::Id numOfVariable = static_cast<vtkm::Id>(binId.size());
|
|
|
|
|
|
|
|
const vtkm::Id numberOfValues = freqsIn.GetPortalConstControl().GetNumberOfValues();
|
|
|
|
vtkm::cont::ArrayHandleConstant<vtkm::Id> constant0Array(0, numberOfValues);
|
|
|
|
vtkm::cont::ArrayHandle<vtkm::Id> bin1DIndex;
|
2018-08-30 15:53:18 +00:00
|
|
|
vtkm::cont::ArrayCopy(constant0Array, bin1DIndex);
|
2017-09-02 22:35:21 +00:00
|
|
|
|
|
|
|
vtkm::cont::ArrayHandle<vtkm::Id> freqs;
|
2018-08-30 15:53:18 +00:00
|
|
|
vtkm::cont::ArrayCopy(freqsIn, freqs);
|
2017-09-02 22:35:21 +00:00
|
|
|
vtkm::Id numMarginalVariables = 0; //count num of marginal variables
|
|
|
|
for (vtkm::Id i = 0; i < numOfVariable; i++)
|
|
|
|
{
|
|
|
|
if (marginalVariables.GetPortalConstControl().Get(i) == true)
|
|
|
|
{
|
|
|
|
// Worklet to calculate 1D index for marginal variables
|
|
|
|
numMarginalVariables++;
|
|
|
|
const vtkm::Id nFieldBins = numberOfBins.GetPortalControl().Get(i);
|
|
|
|
vtkm::worklet::histogram::To1DIndex binWorklet(nFieldBins);
|
2018-08-28 20:36:50 +00:00
|
|
|
vtkm::worklet::DispatcherMapField<vtkm::worklet::histogram::To1DIndex> to1DIndexDispatcher(
|
|
|
|
binWorklet);
|
2017-09-02 22:35:21 +00:00
|
|
|
size_t vecIndex = static_cast<size_t>(i);
|
|
|
|
to1DIndexDispatcher.Invoke(binId[vecIndex], bin1DIndex, bin1DIndex);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ //non-marginal variable
|
|
|
|
// Worklet to set the frequency of entities which does not meet the condition
|
|
|
|
// to 0 on non-marginal variables
|
|
|
|
vtkm::worklet::histogram::ConditionalFreq<BinaryCompare> conditionalFreqWorklet{
|
|
|
|
conditionFunc
|
|
|
|
};
|
|
|
|
conditionalFreqWorklet.setVar(i);
|
2018-08-28 20:36:50 +00:00
|
|
|
vtkm::worklet::DispatcherMapField<vtkm::worklet::histogram::ConditionalFreq<BinaryCompare>>
|
2017-09-02 22:35:21 +00:00
|
|
|
cfDispatcher(conditionalFreqWorklet);
|
|
|
|
size_t vecIndex = static_cast<size_t>(i);
|
|
|
|
cfDispatcher.Invoke(binId[vecIndex], freqs, freqs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Sort the freq array for counting by key(1DIndex)
|
2018-08-30 15:53:18 +00:00
|
|
|
vtkm::cont::Algorithm::SortByKey(bin1DIndex, freqs);
|
2017-09-02 22:35:21 +00:00
|
|
|
|
|
|
|
// Add frequency within same 1d index bin (this get a nonSparse representation)
|
|
|
|
vtkm::cont::ArrayHandle<vtkm::Id> nonSparseMarginalFreqs;
|
2018-08-30 15:53:18 +00:00
|
|
|
vtkm::cont::Algorithm::ReduceByKey(
|
2017-09-02 22:35:21 +00:00
|
|
|
bin1DIndex, freqs, bin1DIndex, nonSparseMarginalFreqs, vtkm::Add());
|
|
|
|
|
|
|
|
// Convert to sparse representation(remove all zero freqncy entities)
|
|
|
|
vtkm::cont::ArrayHandle<vtkm::Id> sparseMarginal1DBinId;
|
2018-08-30 15:53:18 +00:00
|
|
|
vtkm::cont::Algorithm::CopyIf(bin1DIndex, nonSparseMarginalFreqs, sparseMarginal1DBinId);
|
|
|
|
vtkm::cont::Algorithm::CopyIf(nonSparseMarginalFreqs, nonSparseMarginalFreqs, marginalFreqs);
|
2017-09-02 22:35:21 +00:00
|
|
|
|
|
|
|
//convert back to multi variate binId
|
|
|
|
marginalBinId.resize(static_cast<size_t>(numMarginalVariables));
|
|
|
|
vtkm::Id marginalVarIdx = numMarginalVariables - 1;
|
|
|
|
for (vtkm::Id i = numOfVariable - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if (marginalVariables.GetPortalConstControl().Get(i) == true)
|
|
|
|
{
|
|
|
|
const vtkm::Id nFieldBins = numberOfBins.GetPortalControl().Get(i);
|
|
|
|
vtkm::worklet::histogram::ConvertHistBinToND binWorklet(nFieldBins);
|
2018-08-28 20:36:50 +00:00
|
|
|
vtkm::worklet::DispatcherMapField<vtkm::worklet::histogram::ConvertHistBinToND>
|
|
|
|
convertHistBinToNDDispatcher(binWorklet);
|
2017-09-02 22:35:21 +00:00
|
|
|
size_t vecIndex = static_cast<size_t>(marginalVarIdx);
|
2018-08-28 20:36:50 +00:00
|
|
|
convertHistBinToNDDispatcher.Invoke(
|
2017-09-02 22:35:21 +00:00
|
|
|
sparseMarginal1DBinId, sparseMarginal1DBinId, marginalBinId[vecIndex]);
|
|
|
|
marginalVarIdx--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} //Run()
|
|
|
|
|
|
|
|
// Execute the histogram marginalization WITHOUT CONDITION,
|
|
|
|
// Please refer to the other Run() functions for the definition of input arguments.
|
|
|
|
void Run(const std::vector<vtkm::cont::ArrayHandle<vtkm::Id>>& binId,
|
|
|
|
vtkm::cont::ArrayHandle<vtkm::Id>& freqsIn,
|
|
|
|
vtkm::cont::ArrayHandle<vtkm::Id>& numberOfBins,
|
|
|
|
vtkm::cont::ArrayHandle<bool>& marginalVariables,
|
|
|
|
std::vector<vtkm::cont::ArrayHandle<vtkm::Id>>& marginalBinId,
|
2018-08-30 15:53:18 +00:00
|
|
|
vtkm::cont::ArrayHandle<vtkm::Id>& marginalFreqs)
|
2017-09-02 22:35:21 +00:00
|
|
|
{
|
|
|
|
//total variables
|
|
|
|
vtkm::Id numOfVariable = static_cast<vtkm::Id>(binId.size());
|
|
|
|
|
|
|
|
const vtkm::Id numberOfValues = freqsIn.GetPortalConstControl().GetNumberOfValues();
|
|
|
|
vtkm::cont::ArrayHandleConstant<vtkm::Id> constant0Array(0, numberOfValues);
|
|
|
|
vtkm::cont::ArrayHandle<vtkm::Id> bin1DIndex;
|
2018-08-30 15:53:18 +00:00
|
|
|
vtkm::cont::ArrayCopy(constant0Array, bin1DIndex);
|
2017-09-02 22:35:21 +00:00
|
|
|
|
|
|
|
vtkm::cont::ArrayHandle<vtkm::Id> freqs;
|
2018-08-30 15:53:18 +00:00
|
|
|
vtkm::cont::ArrayCopy(freqsIn, freqs);
|
2018-01-30 00:24:31 +00:00
|
|
|
vtkm::Id numMarginalVariables = 0; //count num of marginal variables
|
2017-09-02 22:35:21 +00:00
|
|
|
for (vtkm::Id i = 0; i < numOfVariable; i++)
|
|
|
|
{
|
|
|
|
if (marginalVariables.GetPortalConstControl().Get(i) == true)
|
|
|
|
{
|
|
|
|
// Worklet to calculate 1D index for marginal variables
|
|
|
|
numMarginalVariables++;
|
|
|
|
const vtkm::Id nFieldBins = numberOfBins.GetPortalControl().Get(i);
|
|
|
|
vtkm::worklet::histogram::To1DIndex binWorklet(nFieldBins);
|
2018-08-28 20:36:50 +00:00
|
|
|
vtkm::worklet::DispatcherMapField<vtkm::worklet::histogram::To1DIndex> to1DIndexDispatcher(
|
|
|
|
binWorklet);
|
2017-09-02 22:35:21 +00:00
|
|
|
size_t vecIndex = static_cast<size_t>(i);
|
|
|
|
to1DIndexDispatcher.Invoke(binId[vecIndex], bin1DIndex, bin1DIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the freq array for counting by key (1DIndex)
|
2018-08-30 15:53:18 +00:00
|
|
|
vtkm::cont::Algorithm::SortByKey(bin1DIndex, freqs);
|
2017-09-02 22:35:21 +00:00
|
|
|
|
|
|
|
// Add frequency within same 1d index bin
|
2018-08-30 15:53:18 +00:00
|
|
|
vtkm::cont::Algorithm::ReduceByKey(bin1DIndex, freqs, bin1DIndex, marginalFreqs, vtkm::Add());
|
2017-09-02 22:35:21 +00:00
|
|
|
|
|
|
|
//convert back to multi variate binId
|
|
|
|
marginalBinId.resize(static_cast<size_t>(numMarginalVariables));
|
|
|
|
vtkm::Id marginalVarIdx = numMarginalVariables - 1;
|
|
|
|
for (vtkm::Id i = numOfVariable - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if (marginalVariables.GetPortalConstControl().Get(i) == true)
|
|
|
|
{
|
|
|
|
const vtkm::Id nFieldBins = numberOfBins.GetPortalControl().Get(i);
|
|
|
|
vtkm::worklet::histogram::ConvertHistBinToND binWorklet(nFieldBins);
|
2018-08-28 20:36:50 +00:00
|
|
|
vtkm::worklet::DispatcherMapField<vtkm::worklet::histogram::ConvertHistBinToND>
|
|
|
|
convertHistBinToNDDispatcher(binWorklet);
|
2017-09-02 22:35:21 +00:00
|
|
|
size_t vecIndex = static_cast<size_t>(marginalVarIdx);
|
2018-08-28 20:36:50 +00:00
|
|
|
convertHistBinToNDDispatcher.Invoke(bin1DIndex, bin1DIndex, marginalBinId[vecIndex]);
|
2017-09-02 22:35:21 +00:00
|
|
|
marginalVarIdx--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} //Run()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} // namespace vtkm::worklet
|
|
|
|
|
|
|
|
#endif // vtk_m_worklet_NDimsHistMarginalization_h
|