Skip to content

Commit

Permalink
Further clean up modal bin ranges portion still failing [compiling]
Browse files Browse the repository at this point in the history
  • Loading branch information
nyoungbq committed Sep 19, 2024
1 parent 05941ce commit 9904aa9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class ComputeArrayStatisticsByIndexImpl

HistogramUtilities::serial::FillBinRanges(ranges, std::make_pair(histMin, histMax), m_NumBins);

const T increment = (histMax - histMin) / (m_NumBins);
const float32 increment = HistogramUtilities::serial::CalculateIncrement(histMin, histMax, m_NumBins);
if(std::fabs(increment) < 1E-10)
{
histogram[0] = length[localFeatureIndex];
Expand All @@ -273,63 +273,38 @@ class ComputeArrayStatisticsByIndexImpl
continue;
}
const T value = m_Source[i];
const auto bin = static_cast<int32>(static_cast<uint8>(HistogramUtilities::serial::CalculateBin(value, histMin, increment))); // find bin for this input array value
const auto bin = static_cast<int32>(HistogramUtilities::serial::CalculateBin(value, histMin, increment)); // find bin for this input array value
if((bin >= 0) && (bin < m_NumBins)) // make certain bin is in range
{
++histogram[bin]; // increment histogram element corresponding to this input array value
}
else if(value == histMax)
{
histogram[m_NumBins - 1]++;
histogram[bin]++; // increment histogram element corresponding to this input array value
}
} // end of numTuples loop
} // end of increment else

if(m_ModalBinRanges)
{
bool skip = false;
if constexpr(std::is_floating_point_v<T>)
if(std::fabs(increment) < 1E-10)
{
if(m_HistFullRange && std::fabs(increment) < 1E-10)
{
m_ModalBinRangesArray->addEntry(j, histMin);
m_ModalBinRangesArray->addEntry(j, histMax);

skip = true;
}
m_ModalBinRangesArray->addEntry(j, histMin);
m_ModalBinRangesArray->addEntry(j, histMax);
}
else
{
if(m_HistFullRange && increment == 0)
{
m_ModalBinRangesArray->addEntry(j, histMin);
m_ModalBinRangesArray->addEntry(j, histMax);

skip = true;
}
}

if(!skip)
{
auto modeList = m_ModeArray->getList(j);
for(int i = 0; i < modeList->size(); i++)
{
const T mode = modeList->at(i);
const auto modalBin = HistogramUtilities::serial::CalculateBin(mode, histMin, increment);
T minBinValue;
T maxBinValue;
if((modalBin >= 0) && (modalBin < m_NumBins)) // make certain bin is in range
{
minBinValue = static_cast<T>(histMin + (modalBin * increment));
maxBinValue = static_cast<T>(histMin + ((modalBin + 1) * increment));
m_ModalBinRangesArray->addEntry(j, static_cast<T>(histMin + (modalBin * increment)));
m_ModalBinRangesArray->addEntry(j, static_cast<T>(histMin + ((modalBin + 1) * increment)));
}
else if(mode == histMax)
{
minBinValue = static_cast<T>(histMin + ((modalBin - 1) * increment));
maxBinValue = static_cast<T>(histMin + (modalBin * increment));
m_ModalBinRangesArray->addEntry(j, static_cast<T>(histMin + ((modalBin - 1) * increment)));
m_ModalBinRangesArray->addEntry(j, static_cast<T>(histMin + (modalBin * increment)));
}
m_ModalBinRangesArray->addEntry(j, minBinValue);
m_ModalBinRangesArray->addEntry(j, maxBinValue);
}
}
}
Expand Down
18 changes: 12 additions & 6 deletions src/simplnx/Utilities/HistogramUtilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ namespace nx::core::HistogramUtilities
{
namespace serial
{
template <typename Type>
float32 CalculateIncrement(Type min, Type max, int32 numBins)
{
return static_cast<float32>(max - min) / static_cast<float32>(numBins);
}

/**
* @function FillBinRange
* @brief This function fills a container that is STL compatible and has a bracket operator defined with the bin ranges in the following pattern:
Expand All @@ -21,7 +27,7 @@ namespace serial
* @param increment this is the uniform size of the bins
*/
template <typename Type, class Container>
void FillBinRanges(Container& outputContainer, const std::pair<Type, Type>& rangeMinMax, const int32 numBins, const Type increment)
void FillBinRanges(Container& outputContainer, const std::pair<Type, Type>& rangeMinMax, const int32 numBins, const float32 increment)
{
// WARNING: No bounds checking for type compatibility, it is expected to be done higher up where the type is not abstracted
// EXPECTED CONTAINER SIZE: numBins + 1
Expand Down Expand Up @@ -57,15 +63,15 @@ void FillBinRanges(Container& outputContainer, const std::pair<Type, Type>& rang
// DEV NOTE: this function also serves to act as a jumping off point for implementing logarithmic histograms down the line

// Uniform Bin Sizes
const Type increment = (rangeMinMax.second - rangeMinMax.first) / static_cast<Type>(numBins);
const float32 increment = CalculateIncrement(rangeMinMax.first, rangeMinMax.second, numBins);

FillBinRanges(outputContainer, rangeMinMax, numBins, increment);
}

template <typename Type>
Type CalculateBin(Type value, Type min, Type increment)
Type CalculateBin(Type value, Type min, float32 increment)
{
return std::floor((value - min) / increment);
return static_cast<Type>(std::floor(static_cast<float32>(value - min) / increment));
}

/**
Expand Down Expand Up @@ -119,7 +125,7 @@ Result<> GenerateHistogram(const InputContainer<Type>& inputStore, OutputContain
}
}

const Type increment = (rangeMinMax.second - rangeMinMax.first) / static_cast<Type>(numBins);
const float32 increment = CalculateIncrement(rangeMinMax.first, rangeMinMax.second, numBins);

// Fill Bins
FillBinRanges(binRangesStore, rangeMinMax, numBins, increment);
Expand Down Expand Up @@ -202,7 +208,7 @@ Result<> GenerateHistogramAtComponent(const AbstractDataStore<Type>& inputStore,
}
}

const Type increment = (rangeMinMax.second - rangeMinMax.first) / static_cast<Type>(numBins);
const float32 increment = CalculateIncrement(rangeMinMax.first, rangeMinMax.second, numBins);

// Fill Bins
FillBinRanges(binRangesStore, rangeMinMax, numBins, increment);
Expand Down

0 comments on commit 9904aa9

Please sign in to comment.