Skip to content

Commit

Permalink
Merge pull request #1026 from mrapp-ke/util-namespace
Browse files Browse the repository at this point in the history
Use namespace "util" for utility functions
  • Loading branch information
michael-rapp authored Aug 15, 2024
2 parents fe5f210 + 26c81b6 commit bf0dc62
Show file tree
Hide file tree
Showing 100 changed files with 1,054 additions and 1,010 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace boosting {
* @return The element at the given index
*/
reference operator[](uint32 index) const {
return iterator_[triangularNumber(index + 1) - 1];
return iterator_[util::triangularNumber(index + 1) - 1];
}

/**
Expand All @@ -74,7 +74,7 @@ namespace boosting {
* @return The element, the iterator currently refers to
*/
reference operator*() const {
return iterator_[triangularNumber(index_ + 1) - 1];
return iterator_[util::triangularNumber(index_ + 1) - 1];
}

/**
Expand Down
24 changes: 12 additions & 12 deletions cpp/subprojects/boosting/include/mlrl/boosting/learner_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,46 +126,46 @@ namespace boosting {
virtual ~BoostedRuleLearnerConfig() override {}

Property<IHeadConfig> getHeadConfig() override final {
return property(headConfigPtr_);
return util::property(headConfigPtr_);
}

ReadableProperty<IStatisticsConfig> getStatisticsConfig() const override final {
return readableProperty<IStatisticsConfig, IClassificationStatisticsConfig,
IRegressionStatisticsConfig>(classificationStatisticsConfigPtr_,
regressionStatisticsConfigPtr_);
return util::readableProperty<IStatisticsConfig, IClassificationStatisticsConfig,
IRegressionStatisticsConfig>(classificationStatisticsConfigPtr_,
regressionStatisticsConfigPtr_);
}

SharedProperty<IClassificationStatisticsConfig> getClassificationStatisticsConfig() override final {
return sharedProperty(classificationStatisticsConfigPtr_);
return util::sharedProperty(classificationStatisticsConfigPtr_);
}

SharedProperty<IRegressionStatisticsConfig> getRegressionStatisticsConfig() override final {
return sharedProperty(regressionStatisticsConfigPtr_);
return util::sharedProperty(regressionStatisticsConfigPtr_);
}

Property<IRegularizationConfig> getL1RegularizationConfig() override final {
return property(l1RegularizationConfigPtr_);
return util::property(l1RegularizationConfigPtr_);
}

Property<IRegularizationConfig> getL2RegularizationConfig() override final {
return property(l2RegularizationConfigPtr_);
return util::property(l2RegularizationConfigPtr_);
}

ReadableProperty<ILossConfig> getLossConfig() const override final {
return readableProperty<ILossConfig, IClassificationLossConfig, IRegressionLossConfig>(
return util::readableProperty<ILossConfig, IClassificationLossConfig, IRegressionLossConfig>(
classificationLossConfigPtr_, regressionLossConfigPtr_);
}

SharedProperty<IClassificationLossConfig> getClassificationLossConfig() override final {
return sharedProperty(classificationLossConfigPtr_);
return util::sharedProperty(classificationLossConfigPtr_);
}

SharedProperty<IRegressionLossConfig> getRegressionLossConfig() override final {
return sharedProperty(regressionLossConfigPtr_);
return util::sharedProperty(regressionLossConfigPtr_);
}

Property<ILabelBinningConfig> getLabelBinningConfig() override final {
return property(labelBinningConfigPtr_);
return util::property(labelBinningConfigPtr_);
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ namespace boosting {
typename Model::const_iterator rulesEnd, uint32 threadIndex,
uint32 exampleIndex, uint32 predictionIndex) const override {
CContiguousView<float64>::value_iterator realIterator = realMatrix_.values_begin(threadIndex);
setViewToZeros(realIterator, realMatrix_.numCols);
util::setViewToZeros(realIterator, realMatrix_.numCols);
ScorePredictionDelegate<FeatureMatrix, Model>(realMatrix_)
.predictForExample(featureMatrix, rulesBegin, rulesEnd, threadIndex, exampleIndex,
threadIndex);
Expand Down Expand Up @@ -188,7 +188,7 @@ namespace boosting {
*/
std::unique_ptr<IIncrementalPredictor<DensePredictionMatrix<uint8>>> createIncrementalPredictor(
uint32 maxRules) const override {
if (maxRules != 0) assertGreaterOrEqual<uint32>("maxRules", maxRules, 1);
if (maxRules != 0) util::assertGreaterOrEqual<uint32>("maxRules", maxRules, 1);
return std::make_unique<IncrementalPredictor>(*this, maxRules, binaryTransformationPtr_);
}
};
Expand Down Expand Up @@ -306,7 +306,7 @@ namespace boosting {
typename Model::const_iterator rulesEnd, uint32 threadIndex,
uint32 exampleIndex, uint32 predictionIndex) const override {
CContiguousView<float64>::value_iterator realIterator = realMatrix_.values_begin(threadIndex);
setViewToZeros(realIterator, realMatrix_.numCols);
util::setViewToZeros(realIterator, realMatrix_.numCols);
ScorePredictionDelegate<FeatureMatrix, Model>(realMatrix_)
.predictForExample(featureMatrix, rulesBegin, rulesEnd, threadIndex, exampleIndex,
threadIndex);
Expand Down Expand Up @@ -376,7 +376,7 @@ namespace boosting {
*/
std::unique_ptr<IIncrementalPredictor<BinarySparsePredictionMatrix>> createIncrementalPredictor(
uint32 maxRules) const override {
if (maxRules != 0) assertGreaterOrEqual<uint32>("maxRules", maxRules, 1);
if (maxRules != 0) util::assertGreaterOrEqual<uint32>("maxRules", maxRules, 1);
return std::make_unique<IncrementalPredictor>(*this, maxRules, binaryTransformationPtr_);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ namespace boosting {
*/
std::unique_ptr<IIncrementalPredictor<DensePredictionMatrix<float64>>> createIncrementalPredictor(
uint32 maxRules) const override {
if (maxRules != 0) assertGreaterOrEqual<uint32>("maxRules", maxRules, 1);
if (maxRules != 0) util::assertGreaterOrEqual<uint32>("maxRules", maxRules, 1);
return std::make_unique<IncrementalPredictor>(*this, maxRules, probabilityTransformationPtr_);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ namespace boosting {
*/
std::unique_ptr<IIncrementalPredictor<DensePredictionMatrix<float64>>> createIncrementalPredictor(
uint32 maxRules) const override {
if (maxRules != 0) assertGreaterOrEqual<uint32>("maxRules", maxRules, 1);
if (maxRules != 0) util::assertGreaterOrEqual<uint32>("maxRules", maxRules, 1);
return std::make_unique<IncrementalPredictor>(*this, maxRules);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ namespace boosting {
// Normalize joint probabilities...
for (uint32 i = 0; i < numLabelVectors; i++) {
float64 jointProbability = jointProbabilityIterator[i];
jointProbabilityIterator[i] = divideOrZero(jointProbability, sumOfJointProbabilities);
jointProbabilityIterator[i] = util::divideOrZero(jointProbability, sumOfJointProbabilities);
}

return jointProbabilityVectorPtr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "mlrl/common/data/types.hpp"

namespace boosting {
namespace util {

/**
* Calculates and returns the n-th triangular number, i.e., the number of elements in a n times n triangle.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace boosting {
: binRatio_(binRatio), minBins_(minBins), maxBins_(maxBins) {}

uint32 getMaxBins(uint32 numLabels) const override {
return calculateBoundedFraction(numLabels, binRatio_, minBins_, maxBins_) + 1;
return util::calculateBoundedFraction(numLabels, binRatio_, minBins_, maxBins_) + 1;
}

LabelInfo getLabelInfo(View<float64>::const_iterator criteria, uint32 numCriteria) const override {
Expand Down Expand Up @@ -80,12 +80,12 @@ namespace boosting {

if (labelInfo.numNegativeBins > 0) {
labelInfo.numNegativeBins =
calculateBoundedFraction(labelInfo.numNegativeBins, binRatio_, minBins_, maxBins_);
util::calculateBoundedFraction(labelInfo.numNegativeBins, binRatio_, minBins_, maxBins_);
}

if (labelInfo.numPositiveBins > 0) {
labelInfo.numPositiveBins =
calculateBoundedFraction(labelInfo.numPositiveBins, binRatio_, minBins_, maxBins_);
util::calculateBoundedFraction(labelInfo.numPositiveBins, binRatio_, minBins_, maxBins_);
}
}

Expand Down Expand Up @@ -174,8 +174,8 @@ namespace boosting {
}

IEqualWidthLabelBinningConfig& EqualWidthLabelBinningConfig::setBinRatio(float32 binRatio) {
assertGreater<float32>("binRatio", binRatio, 0);
assertLess<float32>("binRatio", binRatio, 1);
util::assertGreater<float32>("binRatio", binRatio, 0);
util::assertLess<float32>("binRatio", binRatio, 1);
binRatio_ = binRatio;
return *this;
}
Expand All @@ -185,7 +185,7 @@ namespace boosting {
}

IEqualWidthLabelBinningConfig& EqualWidthLabelBinningConfig::setMinBins(uint32 minBins) {
assertGreaterOrEqual<uint32>("minBins", minBins, 1);
util::assertGreaterOrEqual<uint32>("minBins", minBins, 1);
minBins_ = minBins;
return *this;
}
Expand All @@ -195,7 +195,7 @@ namespace boosting {
}

IEqualWidthLabelBinningConfig& EqualWidthLabelBinningConfig::setMaxBins(uint32 maxBins) {
if (maxBins != 0) assertGreaterOrEqual<uint32>("maxBins", maxBins, minBins_);
if (maxBins != 0) util::assertGreaterOrEqual<uint32>("maxBins", maxBins, minBins_);
maxBins_ = maxBins;
return *this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,64 +8,65 @@ namespace boosting {

DenseDecomposableStatisticVector::DenseDecomposableStatisticVector(const DenseDecomposableStatisticVector& other)
: DenseDecomposableStatisticVector(other.getNumElements()) {
copyView(other.cbegin(), this->begin(), this->getNumElements());
util::copyView(other.cbegin(), this->begin(), this->getNumElements());
}

void DenseDecomposableStatisticVector::add(const DenseDecomposableStatisticVector& vector) {
addToView(this->begin(), vector.cbegin(), this->getNumElements());
util::addToView(this->begin(), vector.cbegin(), this->getNumElements());
}

void DenseDecomposableStatisticVector::add(const CContiguousView<Tuple<float64>>& view, uint32 row) {
addToView(this->begin(), view.values_cbegin(row), this->getNumElements());
util::addToView(this->begin(), view.values_cbegin(row), this->getNumElements());
}

void DenseDecomposableStatisticVector::add(const CContiguousView<Tuple<float64>>& view, uint32 row,
float64 weight) {
addToView(this->begin(), view.values_cbegin(row), this->getNumElements(), weight);
util::addToView(this->begin(), view.values_cbegin(row), this->getNumElements(), weight);
}

void DenseDecomposableStatisticVector::remove(const CContiguousView<Tuple<float64>>& view, uint32 row) {
removeFromView(this->begin(), view.values_cbegin(row), this->getNumElements());
util::removeFromView(this->begin(), view.values_cbegin(row), this->getNumElements());
}

void DenseDecomposableStatisticVector::remove(const CContiguousView<Tuple<float64>>& view, uint32 row,
float64 weight) {
removeFromView(this->begin(), view.values_cbegin(row), this->getNumElements(), weight);
util::removeFromView(this->begin(), view.values_cbegin(row), this->getNumElements(), weight);
}

void DenseDecomposableStatisticVector::addToSubset(const CContiguousView<Tuple<float64>>& view, uint32 row,
const CompleteIndexVector& indices) {
addToView(this->begin(), view.values_cbegin(row), this->getNumElements());
util::addToView(this->begin(), view.values_cbegin(row), this->getNumElements());
}

void DenseDecomposableStatisticVector::addToSubset(const CContiguousView<Tuple<float64>>& view, uint32 row,
const PartialIndexVector& indices) {
PartialIndexVector::const_iterator indexIterator = indices.cbegin();
addToView(this->begin(), view.values_cbegin(row), indexIterator, this->getNumElements());
util::addToView(this->begin(), view.values_cbegin(row), indexIterator, this->getNumElements());
}

void DenseDecomposableStatisticVector::addToSubset(const CContiguousView<Tuple<float64>>& view, uint32 row,
const CompleteIndexVector& indices, float64 weight) {
addToView(this->begin(), view.values_cbegin(row), this->getNumElements(), weight);
util::addToView(this->begin(), view.values_cbegin(row), this->getNumElements(), weight);
}

void DenseDecomposableStatisticVector::addToSubset(const CContiguousView<Tuple<float64>>& view, uint32 row,
const PartialIndexVector& indices, float64 weight) {
PartialIndexVector::const_iterator indexIterator = indices.cbegin();
addToView(this->begin(), view.values_cbegin(row), indexIterator, this->getNumElements(), weight);
util::addToView(this->begin(), view.values_cbegin(row), indexIterator, this->getNumElements(), weight);
}

void DenseDecomposableStatisticVector::difference(const DenseDecomposableStatisticVector& first,
const CompleteIndexVector& firstIndices,
const DenseDecomposableStatisticVector& second) {
setViewToDifference(this->begin(), first.cbegin(), second.cbegin(), this->getNumElements());
util::setViewToDifference(this->begin(), first.cbegin(), second.cbegin(), this->getNumElements());
}

void DenseDecomposableStatisticVector::difference(const DenseDecomposableStatisticVector& first,
const PartialIndexVector& firstIndices,
const DenseDecomposableStatisticVector& second) {
PartialIndexVector::const_iterator indexIterator = firstIndices.cbegin();
setViewToDifference(this->begin(), first.cbegin(), second.cbegin(), indexIterator, this->getNumElements());
util::setViewToDifference(this->begin(), first.cbegin(), second.cbegin(), indexIterator,
this->getNumElements());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace boosting {

SparseDecomposableStatisticVector::SparseDecomposableStatisticVector(const SparseDecomposableStatisticVector& other)
: SparseDecomposableStatisticVector(other.getNumElements()) {
copyView(other.view.cbegin(), this->view.begin(), this->getNumElements());
util::copyView(other.view.cbegin(), this->view.begin(), this->getNumElements());
sumOfWeights_ = other.sumOfWeights_;
}

Expand All @@ -78,7 +78,7 @@ namespace boosting {

void SparseDecomposableStatisticVector::add(const SparseDecomposableStatisticVector& vector) {
sumOfWeights_ += vector.sumOfWeights_;
addToView(this->view.begin(), vector.view.cbegin(), this->getNumElements());
util::addToView(this->view.begin(), vector.view.cbegin(), this->getNumElements());
}

void SparseDecomposableStatisticVector::add(const SparseSetView<Tuple<float64>>& view, uint32 row) {
Expand Down Expand Up @@ -171,15 +171,16 @@ namespace boosting {
const CompleteIndexVector& firstIndices,
const SparseDecomposableStatisticVector& second) {
sumOfWeights_ = first.sumOfWeights_ - second.sumOfWeights_;
setViewToDifference(this->view.begin(), first.view.cbegin(), second.view.cbegin(), this->getNumElements());
util::setViewToDifference(this->view.begin(), first.view.cbegin(), second.view.cbegin(),
this->getNumElements());
}

void SparseDecomposableStatisticVector::difference(const SparseDecomposableStatisticVector& first,
const PartialIndexVector& firstIndices,
const SparseDecomposableStatisticVector& second) {
sumOfWeights_ = first.sumOfWeights_ - second.sumOfWeights_;
setViewToDifference(this->view.begin(), first.view.cbegin(), second.view.cbegin(), firstIndices.cbegin(),
this->getNumElements());
util::setViewToDifference(this->view.begin(), first.view.cbegin(), second.view.cbegin(), firstIndices.cbegin(),
this->getNumElements());
}

void SparseDecomposableStatisticVector::clear() {
Expand Down
Loading

0 comments on commit bf0dc62

Please sign in to comment.