| 29 | } |
| 30 | |
| 31 | void DatasetLoader::SetHeader(const char* filename) { |
| 32 | std::unordered_map<std::string, int> name2idx; |
| 33 | std::string name_prefix("name:"); |
| 34 | if (filename != nullptr) { |
| 35 | TextReader<data_size_t> text_reader(filename, config_.header); |
| 36 | |
| 37 | // get column names |
| 38 | if (config_.header) { |
| 39 | std::string first_line = text_reader.first_line(); |
| 40 | feature_names_ = Common::Split(first_line.c_str(), "\t,"); |
| 41 | } |
| 42 | |
| 43 | // load label idx first |
| 44 | if (config_.label_column.size() > 0) { |
| 45 | if (Common::StartsWith(config_.label_column, name_prefix)) { |
| 46 | std::string name = config_.label_column.substr(name_prefix.size()); |
| 47 | label_idx_ = -1; |
| 48 | for (int i = 0; i < static_cast<int>(feature_names_.size()); ++i) { |
| 49 | if (name == feature_names_[i]) { |
| 50 | label_idx_ = i; |
| 51 | break; |
| 52 | } |
| 53 | } |
| 54 | if (label_idx_ >= 0) { |
| 55 | Log::Info("Using column %s as label", name.c_str()); |
| 56 | } else { |
| 57 | Log::Fatal("Could not find label column %s in data file \n" |
| 58 | "or data file doesn't contain header", name.c_str()); |
| 59 | } |
| 60 | } else { |
| 61 | if (!Common::AtoiAndCheck(config_.label_column.c_str(), &label_idx_)) { |
| 62 | Log::Fatal("label_column is not a number,\n" |
| 63 | "if you want to use a column name,\n" |
| 64 | "please add the prefix \"name:\" to the column name"); |
| 65 | } |
| 66 | Log::Info("Using column number %d as label", label_idx_); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if (!feature_names_.empty()) { |
| 71 | // erase label column name |
| 72 | feature_names_.erase(feature_names_.begin() + label_idx_); |
| 73 | for (size_t i = 0; i < feature_names_.size(); ++i) { |
| 74 | name2idx[feature_names_[i]] = static_cast<int>(i); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // load ignore columns |
| 79 | if (config_.ignore_column.size() > 0) { |
| 80 | if (Common::StartsWith(config_.ignore_column, name_prefix)) { |
| 81 | std::string names = config_.ignore_column.substr(name_prefix.size()); |
| 82 | for (auto name : Common::Split(names.c_str(), ',')) { |
| 83 | if (name2idx.count(name) > 0) { |
| 84 | int tmp = name2idx[name]; |
| 85 | ignore_features_.emplace(tmp); |
| 86 | } else { |
| 87 | Log::Fatal("Could not find ignore column %s in data file", name.c_str()); |
| 88 | } |
nothing calls this directly
no test coverage detected