-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract make metric name from ranking metric.
- Extract the metric parsing routine from ranking. - Add test.
- Loading branch information
1 parent
48cefa0
commit e7b9792
Showing
9 changed files
with
141 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Copyright 2023 by XGBoost contributors | ||
*/ | ||
#include "ranking_utils.h" | ||
|
||
#include <cstdint> // std::uint32_t | ||
#include <sstream> // std::ostringstream | ||
#include <string> // std::string,std::sscanf | ||
|
||
#include "xgboost/string_view.h" // StringView | ||
|
||
namespace xgboost { | ||
namespace ltr { | ||
std::string MakeMetricName(StringView name, StringView param, std::uint32_t* topn, bool* minus) { | ||
std::string out_name; | ||
if (!param.empty()) { | ||
std::ostringstream os; | ||
if (std::sscanf(param.c_str(), "%u[-]?", topn) == 1) { | ||
os << name << '@' << param; | ||
out_name = os.str(); | ||
} else { | ||
os << name << param; | ||
out_name = os.str(); | ||
} | ||
if (*param.crbegin() == '-') { | ||
*minus = true; | ||
} | ||
} else { | ||
out_name = name.c_str(); | ||
} | ||
return out_name; | ||
} | ||
} // namespace ltr | ||
} // namespace xgboost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Copyright 2023 by XGBoost contributors | ||
*/ | ||
#ifndef XGBOOST_COMMON_RANKING_UTILS_H_ | ||
#define XGBOOST_COMMON_RANKING_UTILS_H_ | ||
|
||
#include <cstddef> // std::size_t | ||
#include <cstdint> // std::uint32_t | ||
#include <string> // std::string | ||
|
||
#include "xgboost/string_view.h" // StringView | ||
|
||
namespace xgboost { | ||
namespace ltr { | ||
/** | ||
* \brief Construct name for ranking metric given parameters. | ||
* | ||
* \param [in] name Null terminated string for metric name | ||
* \param [in] param Null terminated string for parameter like the `3-` in `ndcg@3-`. | ||
* \param [out] topn Top n documents parsed from param. Unchanged if it's not specified. | ||
* \param [out] minus Whether we should turn the score into loss. Unchanged if it's not | ||
* specified. | ||
* | ||
* \return The name of the metric. | ||
*/ | ||
std::string MakeMetricName(StringView name, StringView param, std::uint32_t* topn, bool* minus); | ||
} // namespace ltr | ||
} // namespace xgboost | ||
#endif // XGBOOST_COMMON_RANKING_UTILS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Copyright 2023 by XGBoost Contributors | ||
*/ | ||
#include <gtest/gtest.h> | ||
|
||
#include <cstdint> // std::uint32_t | ||
|
||
#include "../../../src/common/ranking_utils.h" | ||
|
||
namespace xgboost { | ||
namespace ltr { | ||
TEST(RankingUtils, MakeMetricName) { | ||
std::uint32_t topn{32}; | ||
bool minus{false}; | ||
auto name = MakeMetricName("ndcg", "3-", &topn, &minus); | ||
ASSERT_EQ(name, "ndcg@3-"); | ||
ASSERT_EQ(topn, 3); | ||
ASSERT_TRUE(minus); | ||
|
||
name = MakeMetricName("ndcg", "6", &topn, &minus); | ||
ASSERT_EQ(topn, 6); | ||
ASSERT_TRUE(minus); // unchanged | ||
|
||
name = MakeMetricName("ndcg", "-", &topn, &minus); | ||
ASSERT_EQ(topn, 6); // unchanged | ||
ASSERT_TRUE(minus); | ||
|
||
name = MakeMetricName("ndcg", nullptr, &topn, &minus); | ||
ASSERT_EQ(topn, 6); // unchanged | ||
ASSERT_TRUE(minus); // unchanged | ||
|
||
name = MakeMetricName("ndcg", StringView{}, &topn, &minus); | ||
ASSERT_EQ(topn, 6); // unchanged | ||
ASSERT_TRUE(minus); // unchanged | ||
} | ||
} // namespace ltr | ||
} // namespace xgboost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters