CMS 3D CMS Logo

XGBooster.cc
Go to the documentation of this file.
2 #include <algorithm>
3 #include <cassert>
4 #include <cmath>
5 #include <stdexcept>
6 
7 #include <iostream>
8 #include <fstream>
9 #include <sstream>
10 #include <vector>
11 #include <stdexcept>
12 
13 using namespace pat;
14 
15 std::vector<std::string> read_features(const std::string& content) {
16  std::vector<std::string> result;
17 
18  std::istringstream stream(content);
19  char ch;
20 
21  // Expect opening '['
22  stream >> ch;
23  if (ch != '[') {
24  throw std::runtime_error("Expected '[' at the beginning of the JSON array!");
25  }
26 
27  while (stream) {
28  stream >> ch;
29 
30  if (ch == ']') {
31  break;
32  } else if (ch == ',') {
33  continue;
34  } else if (ch == '"') {
35  std::string feature;
36  std::getline(stream, feature, '"');
37  result.push_back(feature);
38  } else {
39  throw std::runtime_error("Unexpected character in the JSON array!");
40  }
41  }
42 
43  return result;
44 }
45 
47  int status = XGBoosterCreate(nullptr, 0, &booster_);
48  if (status != 0)
49  throw std::runtime_error("Failed to create XGBooster");
50  status = XGBoosterLoadModel(booster_, model_file.c_str());
51  if (status != 0)
52  throw std::runtime_error("Failed to load XGBoost model");
53  XGBoosterSetParam(booster_, "nthread", "1");
54 }
55 
56 XGBooster::XGBooster(std::string model_file, std::string model_features) : XGBooster(model_file) {
57  std::ifstream file(model_features);
58  if (!file.is_open())
59  throw std::runtime_error("Failed to open file: " + model_features);
60 
61  std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
62  file.close();
63 
64  std::vector<std::string> features = read_features(content);
65 
66  for (const auto& feature : features) {
67  addFeature(feature);
68  }
69 }
70 
71 void XGBooster::reset() { std::fill(features_.begin(), features_.end(), std::nan("")); }
72 
74  features_.push_back(0);
76 }
77 
79 
81  float result(-999.);
82 
83  // check if all feature values are set properly
84  for (unsigned int i = 0; i < features_.size(); ++i)
85  if (std::isnan(features_.at(i))) {
86  std::string feature_name;
87  for (const auto& pair : feature_name_to_index_) {
88  if (pair.second == i) {
89  feature_name = pair.first;
90  break;
91  }
92  }
93  throw std::runtime_error("Feature is not set: " + feature_name);
94  }
95 
96  DMatrixHandle dvalues;
97  XGDMatrixCreateFromMat(&features_[0], 1, features_.size(), 9e99, &dvalues);
98 
99  bst_ulong out_len = 0;
100  const float* score = nullptr;
101 
102  // config json
103  const char* json = R"({
104  "type": 0,
105  "training": false,
106  "iteration_begin": 0,
107  "iteration_end": 0,
108  "strict_shape": false
109  })";
110 
111  // Shape of output prediction
112  bst_ulong const* out_shape = nullptr;
113 
114  auto ret = XGBoosterPredictFromDMatrix(booster_, dvalues, json, &out_shape, &out_len, &score);
115 
116  XGDMatrixFree(dvalues);
117 
118  if (ret == 0) {
119  assert(out_len == 1 && "Unexpected prediction format");
120  result = score[0];
121  }
122 
123  reset();
124 
125  return result;
126 }
void reset()
Reset feature values.
Definition: XGBooster.cc:71
def isnan(num)
XGBooster(std::string model_file)
Definition: XGBooster.cc:46
ret
prodAgent to be discontinued
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t stream
assert(be >=bs)
BoosterHandle booster_
Definition: XGBooster.h:30
nlohmann::json json
Definition: HeavyIon.h:7
float predict()
Definition: XGBooster.cc:80
std::vector< float > features(const reco::PreId &ecal, const reco::PreId &hcal, double rho, const reco::BeamSpot &spot, noZS::EcalClusterLazyTools &ecalTools)
Definition: value.py:1
std::vector< float > features_
Definition: XGBooster.h:28
std::vector< std::string > read_features(const std::string &content)
Definition: XGBooster.cc:15
void set(std::string name, float value)
Definition: XGBooster.cc:78
std::map< std::string, unsigned int > feature_name_to_index_
Definition: XGBooster.h:29
void addFeature(std::string name)
Definition: XGBooster.cc:73