Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions include/stochtree/partition_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <stochtree/openmp_utils.h>
#include <stochtree/tree.h>

#include <cmath>
#include <numeric>

namespace StochTree {
Expand Down Expand Up @@ -495,10 +496,16 @@ class FeaturePresortRoot {
}
std::iota(feature_sort_indices_.begin(), feature_sort_indices_.end(), 0);

// Define a custom comparator to be used with stable_sort:
// For every two indices l and r store as elements of `data_sort_indices_`,
// compare them for sorting purposes by indexing the covariate's raw data with both l and r
auto comp_op = [&](size_t const &l, size_t const &r) { return std::less<double>{}(covariates(l, feature_index_), covariates(r, feature_index_)); };
// Ordinary floating-point less-than is not a strict weak ordering with NaNs.
// Sort NaNs last, consistent with numeric splits routing them right, while
// stable_sort preserves the input order of equal values (including NaNs).
auto comp_op = [&](size_t const &l, size_t const &r) {
const double left_value = covariates(l, feature_index_);
const double right_value = covariates(r, feature_index_);
if (std::isnan(left_value)) return false;
if (std::isnan(right_value)) return true;
return left_value < right_value;
};
std::stable_sort(feature_sort_indices_.begin(), feature_sort_indices_.end(), comp_op);
}

Expand Down
50 changes: 50 additions & 0 deletions test/cpp/test_sorted_partition_tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <stochtree/meta.h>
#include <stochtree/partition_tracker.h>
#include <stochtree/tree.h>
#include <algorithm>
#include <limits>
#include <iostream>
#include <memory>

Expand Down Expand Up @@ -98,3 +100,51 @@ TEST(SortedNodeSampleTracker, BasicOperations) {
expected_result = {3,0,7,4,5};
ASSERT_EQ(sorted_node_sampler_tracker.NodeIndices(4, 1), expected_result);
}

// Exercise the production presort through its public partition view.
TEST(FeaturePresortRoot, MissingValuesSortLastAcrossPermutations) {
const double nan = std::numeric_limits<double>::quiet_NaN();
const std::vector<double> values{-1.0, 0.0, 1.0, nan, nan, 1.0};
std::vector<int> permutation{0, 1, 2, 3, 4, 5};
do {
Eigen::MatrixXd covariates(6, 1);
for (int i = 0; i < 6; ++i) covariates(i, 0) = values[permutation[i]];
// Construct expected order independently; preserve ties and NaN order.
std::vector<StochTree::data_size_t> expected;
for (double value : {-1.0, 0.0, 1.0}) {
for (int i = 0; i < 6; ++i) {
if (covariates(i, 0) == value) expected.push_back(i);
}
}
for (int i = 0; i < 6; ++i) {
if (std::isnan(covariates(i, 0))) expected.push_back(i);
}
StochTree::FeaturePresortRoot root(covariates, 0, StochTree::FeatureType::kNumeric);
StochTree::FeaturePresortPartition partition(&root, covariates, 0, StochTree::FeatureType::kNumeric);
ASSERT_EQ(partition.NodeIndices(0), expected);
// The scored sorted prefix must agree with numeric routing (NaNs go right).
partition.SplitFeatureNumeric(covariates, 0, 0, 0.0);
ASSERT_EQ(partition.NodeIndices(1), (std::vector<StochTree::data_size_t>{expected[0], expected[1]}));
ASSERT_EQ(partition.NodeIndices(2), (std::vector<StochTree::data_size_t>{expected[2], expected[3], expected[4], expected[5]}));
} while (std::next_permutation(permutation.begin(), permutation.end()));
}

TEST(FeaturePresortRoot, NonMissingOrderAndStableTiesArePreserved) {
const double inf = std::numeric_limits<double>::infinity();
Eigen::MatrixXd covariates(8, 1);
covariates << 2.0, -0.0, inf, -2.0, 2.0, -inf, 0.0, -2.0;
StochTree::FeaturePresortRoot root(covariates, 0, StochTree::FeatureType::kNumeric);
StochTree::FeaturePresortPartition partition(&root, covariates, 0, StochTree::FeatureType::kNumeric);
EXPECT_EQ(partition.NodeIndices(0), (std::vector<StochTree::data_size_t>{5, 3, 7, 1, 6, 0, 4, 2}));
}

TEST(FeaturePresortRoot, EmptySingletonAndAllMissingFeatures) {
for (int n : {0, 1, 5}) {
Eigen::MatrixXd covariates = Eigen::MatrixXd::Constant(n, 1, std::numeric_limits<double>::quiet_NaN());
StochTree::FeaturePresortRoot root(covariates, 0, StochTree::FeatureType::kNumeric);
StochTree::FeaturePresortPartition partition(&root, covariates, 0, StochTree::FeatureType::kNumeric);
std::vector<StochTree::data_size_t> expected(n);
std::iota(expected.begin(), expected.end(), 0);
EXPECT_EQ(partition.NodeIndices(0), expected);
}
}
Loading