Skip to content

Commit

Permalink
ENH: Update the output from histogram ranges to be a 2 component array
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Jackson <[email protected]>
  • Loading branch information
imikejackson committed Sep 30, 2024
1 parent f25133d commit 57d1b1a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class ComputeArrayStatisticsByIndexImpl

if(m_Histogram && binCountsStorePtr != nullptr && binRangesStorePtr != nullptr)
{
std::vector<T> ranges(m_NumBins + 1);
std::vector<T> ranges(m_NumBins * 2);
std::vector<uint64> histogram(m_NumBins, 0);
if(length[localFeatureIndex] > 0)
{
Expand Down Expand Up @@ -630,7 +630,7 @@ void FindStatisticsImpl(const ContainerType& data, std::vector<IArray*>& arrays,
std::atomic_bool neverCancel{false};
std::atomic<usize> overflow{0};
std::vector<uint64> binCounts(inputValues->NumBins, 0);
std::vector<T> binRanges(inputValues->NumBins + 1);
std::vector<T> binRanges(inputValues->NumBins * 2);

Result<> result = {};
if constexpr(std::is_same_v<DataArray<T>, ContainerType>)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ IFilter::PreflightResult ComputeArrayHistogramFilter::preflightImpl(const DataSt
auto pBinRangeSuffix = filterArgs.value<std::string>(k_HistoBinRangeName_Key);

nx::core::Result<OutputActions> resultOutputActions;
;

if(pNewDataGroupValue)
{
Expand All @@ -131,7 +130,7 @@ IFilter::PreflightResult ComputeArrayHistogramFilter::preflightImpl(const DataSt
}

{
auto createArrayAction = std::make_unique<CreateArrayAction>(dataArray->getDataType(), std::vector<usize>{static_cast<usize>(pNumberOfBinsValue + 1)}, std::vector<usize>{1},
auto createArrayAction = std::make_unique<CreateArrayAction>(dataArray->getDataType(), std::vector<usize>{static_cast<usize>(pNumberOfBinsValue)}, std::vector<usize>{2},
parentPath.createChildPath((dataArray->getName() + pBinRangeSuffix)));
resultOutputActions.value().appendAction(std::move(createArrayAction));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ OutputActions CreateCompatibleArrays(const DataStructure& dataStructure, const A
}
{
auto arrayPath = args.value<std::string>(ComputeArrayStatisticsFilter::k_HistoBinRangeName_Key);
auto action = std::make_unique<CreateArrayAction>(dataType, tupleDims, std::vector<usize>{numBins + 1}, destinationAttributeMatrixValue.createChildPath(arrayPath));
auto action = std::make_unique<CreateArrayAction>(dataType, tupleDims, std::vector<usize>{numBins * 2}, destinationAttributeMatrixValue.createChildPath(arrayPath));
actions.appendAction(std::move(action));
}
{
Expand Down
8 changes: 4 additions & 4 deletions src/Plugins/SimplnxCore/test/ComputeArrayHistogramTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void compareHistograms(const AbstractDataStore<T>& calculated, const std::array<
{
if(calculated.getSize() != actual.size())
{
throw std::runtime_error("Improper sizing of DataStore");
throw std::runtime_error(fmt::format("Improper sizing of DataStore. {} vs {}", calculated.getSize(), actual.size()));
}
for(int32 i = 0; i < N; i++)
{
Expand Down Expand Up @@ -81,23 +81,23 @@ TEST_CASE("SimplnxCore::ComputeArrayHistogram: Valid Filter Execution", "[Simpln
SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result);

{
std::array<float64, 5> binRangesSet = {-56.8, 184.475, 425.75, 667.025, 908.3};
std::array<float64, 8> binRangesSet = {-56.8, 184.475, 184.475, 425.75, 425.75, 667.025, 667.025, 908.3};
std::array<uint64, 4> binCountsSet = {11, 0, 0, 1};
const std::string name = k_Array0Name;

compareHistograms(dataStruct.getDataAs<Float64Array>(dataGPath.createChildPath((name + std::string{k_BinRangesSuffix})))->getDataStoreRef(), binRangesSet);
compareHistograms(dataStruct.getDataAs<UInt64Array>(dataGPath.createChildPath((name + std::string{k_BinCountsSuffix})))->getDataStoreRef(), binCountsSet);
}
{
std::array<int32, 5> binRangesSet = {-90, -44, 2, 48, 94};
std::array<int32, 8> binRangesSet = {-90, -44, -44, 2, 2, 48, 48, 94};
std::array<uint64, 4> binCountsSet = {1, 2, 3, 6};
const std::string name = k_Array1Name;

compareHistograms(dataStruct.getDataAs<Int32Array>(dataGPath.createChildPath((name + std::string{k_BinRangesSuffix})))->getDataStoreRef(), binRangesSet);
compareHistograms(dataStruct.getDataAs<UInt64Array>(dataGPath.createChildPath((name + std::string{k_BinCountsSuffix})))->getDataStoreRef(), binCountsSet);
}
{
std::array<uint32, 5> binRangesSet = {34, 2270, 4506, 6742, 8978};
std::array<uint32, 8> binRangesSet = {34, 2270, 2270, 4506, 4506, 6742, 6742, 8978};
std::array<uint64, 4> binCountsSet = {11, 0, 0, 1};
const std::string name = k_Array2Name;

Expand Down
24 changes: 13 additions & 11 deletions src/Plugins/SimplnxCore/test/ComputeArrayStatisticsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ TEST_CASE("SimplnxCore::ComputeArrayStatisticsFilter: Test Algorithm", "[Simplnx

// Preflight the filter and check result
auto preflightResult = filter.preflight(dataStructure, args);
REQUIRE(preflightResult.outputActions.valid());
SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions);

// Execute the filter and check the result
auto executeResult = filter.execute(dataStructure, args);
REQUIRE(executeResult.result.valid());
SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result);
}

// Check resulting values
Expand Down Expand Up @@ -170,7 +170,7 @@ TEST_CASE("SimplnxCore::ComputeArrayStatisticsFilter: Test Algorithm", "[Simplnx
REQUIRE(modeVals[0] == 1);
REQUIRE(modalBinRangesVals.size() == 2);
REQUIRE(modalBinRangesVals[0] == 1);
REQUIRE(modalBinRangesVals[1] == 10);
REQUIRE(modalBinRangesVals[1] == 6);
REQUIRE(std::fabs(meanVal - 14.3333f) < UnitTest::EPSILON);
REQUIRE(std::fabs(medianVal - 10.0f) < UnitTest::EPSILON);
REQUIRE(std::fabs(stdVal - 13.02f) < UnitTest::EPSILON);
Expand Down Expand Up @@ -315,11 +315,11 @@ TEST_CASE("SimplnxCore::ComputeArrayStatisticsFilter: Test Algorithm By Index",

// Preflight the filter and check result
auto preflightResult = filter.preflight(dataStructure, args);
REQUIRE(preflightResult.outputActions.valid());
SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions);

// Execute the filter and check the result
auto executeResult = filter.execute(dataStructure, args);
REQUIRE(executeResult.result.valid());
SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result);
}

// Check resulting values
Expand Down Expand Up @@ -493,13 +493,15 @@ TEST_CASE("SimplnxCore::ComputeArrayStatisticsFilter: Test Algorithm By Index",
REQUIRE((*mostPopulatedBinArray)[5] == 2);
REQUIRE(modalBinRange0[0] == 1);
REQUIRE(modalBinRange0[1] == 15);
REQUIRE(modalBinRange0[2] == 59);
REQUIRE(modalBinRange0[3] == 74);
REQUIRE(modalBinRange1[0] == 17);
REQUIRE(modalBinRange1[1] == 21);
REQUIRE(modalBinRange0[2] == 30);
REQUIRE(modalBinRange0[3] == 44);

REQUIRE(modalBinRange1[0] == 11);
REQUIRE(modalBinRange1[1] == 14);

REQUIRE(modalBinRange2[0] == 10);
REQUIRE(modalBinRange2[1] == 12);
REQUIRE(modalBinRange2[2] == 20);
REQUIRE(modalBinRange2[3] == 23);
REQUIRE(modalBinRange2[2] == 15);
REQUIRE(modalBinRange2[3] == 17);
}
}
9 changes: 5 additions & 4 deletions src/simplnx/Utilities/HistogramUtilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ void FillBinRanges(Container& outputContainer, const std::pair<Type, Type>& rang
}

// iterate through loading the middle values of the sequence considering `lower bound inclusive, upper bound exclusive`
// We are filling a 2 component array. For STL containers this means the size of the container is 2*numbins
// We are going to put all the lower bin values into a component and the upper bin values in another component
for(int32 i = 0; i < numBins; i++)
{
outputContainer[i] = rangeMinMax.first + static_cast<Type>(increment * i);
outputContainer[i * 2 + 0] = rangeMinMax.first + static_cast<Type>(increment * i);
outputContainer[i * 2 + 1] = rangeMinMax.first + static_cast<Type>(increment * (i + 1.0F));
}

outputContainer[numBins] = rangeMinMax.second;
}

/**
Expand Down Expand Up @@ -108,7 +109,7 @@ Result<> GenerateHistogram(const InputContainer& inputStore, RangesContainer& bi
static_assert(std::is_same_v<typename InputContainer::value_type, typename RangesContainer::value_type>,
"HistogramUtilities::GenerateHistogram: inputStore and binRangesStore must be of the same type. HistogramUtilities:99");

if(binRangesStore.size() < numBins + 1)
if(binRangesStore.size() < numBins * 2)
{
return MakeErrorResult(-23761, fmt::format("HistogramUtilities::{}: binRangesStore is too small to hold ranges. Needed: {} | Current Size: {}. {}:{}", __func__, numBins + 1, binRangesStore.size(),
__FILE__, __LINE__));
Expand Down

0 comments on commit 57d1b1a

Please sign in to comment.