1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
/*-------------------------------------------------------------------------------
This file is part of ranger.
Copyright (c) [2014-2018] [Marvin N. Wright]
This software may be modified and distributed under the terms of the MIT license.
Please note that the C++ core of ranger is distributed under MIT license and the
R package "ranger" under GPL3 license.
#-------------------------------------------------------------------------------*/
#include <set>
#include <algorithm>
#include <cmath>
#include <stdexcept>
#include <string>
#include "utility.h"
#include "ForestSurvival.h"
#include "Data.h"
namespace ranger {
void ForestSurvival::loadForest(size_t num_trees, std::vector<std::vector<std::vector<size_t>> >& forest_child_nodeIDs,
std::vector<std::vector<size_t>>& forest_split_varIDs, std::vector<std::vector<double>>& forest_split_values,
std::vector<std::vector<std::vector<double>> >& forest_chf, std::vector<double>& unique_timepoints,
std::vector<bool>& is_ordered_variable) {
this->num_trees = num_trees;
this->unique_timepoints = unique_timepoints;
data->setIsOrderedVariable(is_ordered_variable);
// Create trees
trees.reserve(num_trees);
for (size_t i = 0; i < num_trees; ++i) {
trees.push_back(
std::make_unique<TreeSurvival>(forest_child_nodeIDs[i], forest_split_varIDs[i], forest_split_values[i],
forest_chf[i], &this->unique_timepoints, &response_timepointIDs));
}
// Create thread ranges
equalSplit(thread_ranges, 0, num_trees - 1, num_threads);
}
void ForestSurvival::setUniqueTimepoints(const std::vector<double>& time_interest) {
if (time_interest.empty()) {
// Use all observed unique time points
std::set<double> unique_timepoint_set;
for (size_t i = 0; i < num_samples; ++i) {
if (data->get_y(i, 1) > 0) {
unique_timepoint_set.insert(data->get_y(i, 0));
}
}
unique_timepoints.reserve(unique_timepoint_set.size());
for (auto& t : unique_timepoint_set) {
unique_timepoints.push_back(t);
}
} else {
// Use the supplied time points of interest
unique_timepoints = time_interest;
}
// Create response_timepointIDs
for (size_t i = 0; i < num_samples; ++i) {
double value = data->get_y(i, 0);
// If timepoint is already in unique_timepoints, use ID. Else create a new one.
uint timepointID = 0;
if (value > unique_timepoints[unique_timepoints.size() - 1]) {
timepointID = unique_timepoints.size() - 1;
} else if (value > unique_timepoints[0]) {
timepointID = std::lower_bound(unique_timepoints.begin(), unique_timepoints.end(), value) - unique_timepoints.begin();
}
if (timepointID < 0) {
timepointID = 0;
}
response_timepointIDs.push_back(timepointID);
}
}
std::vector<std::vector<std::vector<double>>> ForestSurvival::getChf() const {
std::vector<std::vector<std::vector<double>>> result;
result.reserve(num_trees);
for (const auto& tree : trees) {
const auto& temp = dynamic_cast<const TreeSurvival&>(*tree);
result.push_back(temp.getChf());
}
return result;
}
void ForestSurvival::initInternal() {
// If mtry not set, use floored square root of number of independent variables.
if (mtry == 0) {
unsigned long temp = ceil(sqrt((double) num_independent_variables));
mtry = std::max((unsigned long) 1, temp);
}
// Set minimal node size
if (min_node_size.size() == 1 && min_node_size[0] == 0) {
min_node_size[0] = DEFAULT_MIN_NODE_SIZE_SURVIVAL;
}
// Set minimal bucket size
if (min_bucket.size() == 1 && min_bucket[0] == 0) {
min_bucket[0] = DEFAULT_MIN_BUCKET_SURVIVAL;
}
// Sort data if extratrees and not memory saving mode
if (splitrule == EXTRATREES && !memory_saving_splitting) {
data->sort();
}
}
void ForestSurvival::growInternal() {
// If unique time points not set, use observed times
if (unique_timepoints.empty()) {
setUniqueTimepoints(std::vector<double>());
}
trees.reserve(num_trees);
for (size_t i = 0; i < num_trees; ++i) {
trees.push_back(std::make_unique<TreeSurvival>(&unique_timepoints, &response_timepointIDs));
}
}
void ForestSurvival::allocatePredictMemory() {
size_t num_prediction_samples = data->getNumRows();
size_t num_timepoints = unique_timepoints.size();
if (predict_all) {
predictions = std::vector<std::vector<std::vector<double>>>(num_prediction_samples,
std::vector<std::vector<double>>(num_timepoints, std::vector<double>(num_trees, 0)));
} else if (prediction_type == TERMINALNODES) {
predictions = std::vector<std::vector<std::vector<double>>>(1,
std::vector<std::vector<double>>(num_prediction_samples, std::vector<double>(num_trees, 0)));
} else {
predictions = std::vector<std::vector<std::vector<double>>>(1,
std::vector<std::vector<double>>(num_prediction_samples, std::vector<double>(num_timepoints, 0)));
}
}
void ForestSurvival::predictInternal(size_t sample_idx) {
// For each timepoint sum over trees
if (predict_all) {
for (size_t j = 0; j < unique_timepoints.size(); ++j) {
for (size_t k = 0; k < num_trees; ++k) {
predictions[sample_idx][j][k] = getTreePrediction(k, sample_idx)[j];
}
}
} else if (prediction_type == TERMINALNODES) {
for (size_t k = 0; k < num_trees; ++k) {
predictions[0][sample_idx][k] = getTreePredictionTerminalNodeID(k, sample_idx);
}
} else {
for (size_t j = 0; j < unique_timepoints.size(); ++j) {
double sample_time_prediction = 0;
for (size_t k = 0; k < num_trees; ++k) {
sample_time_prediction += getTreePrediction(k, sample_idx)[j];
}
predictions[0][sample_idx][j] = sample_time_prediction / num_trees;
}
}
}
void ForestSurvival::computePredictionErrorInternal() {
size_t num_timepoints = unique_timepoints.size();
// For each sample sum over trees where sample is OOB
std::vector<size_t> samples_oob_count;
samples_oob_count.resize(num_samples, 0);
predictions = std::vector<std::vector<std::vector<double>>>(1,
std::vector<std::vector<double>>(num_samples, std::vector<double>(num_timepoints, 0)));
for (size_t tree_idx = 0; tree_idx < num_trees; ++tree_idx) {
for (size_t sample_idx = 0; sample_idx < trees[tree_idx]->getNumSamplesOob(); ++sample_idx) {
size_t sampleID = trees[tree_idx]->getOobSampleIDs()[sample_idx];
std::vector<double> tree_sample_chf = getTreePrediction(tree_idx, sample_idx);
for (size_t time_idx = 0; time_idx < tree_sample_chf.size(); ++time_idx) {
predictions[0][sampleID][time_idx] += tree_sample_chf[time_idx];
}
++samples_oob_count[sampleID];
}
}
// Divide sample predictions by number of trees where sample is oob and compute summed chf for samples
std::vector<double> sum_chf;
sum_chf.reserve(predictions[0].size());
std::vector<size_t> oob_sampleIDs;
oob_sampleIDs.reserve(predictions[0].size());
for (size_t i = 0; i < predictions[0].size(); ++i) {
if (samples_oob_count[i] > 0) {
double sum = 0;
for (size_t j = 0; j < predictions[0][i].size(); ++j) {
predictions[0][i][j] /= samples_oob_count[i];
sum += predictions[0][i][j];
}
sum_chf.push_back(sum);
oob_sampleIDs.push_back(i);
}
}
// Use all samples which are OOB at least once
overall_prediction_error = 1 - computeConcordanceIndex(*data, sum_chf, oob_sampleIDs, NULL);
}
// #nocov start
void ForestSurvival::writeOutputInternal() {
if (verbose_out) {
*verbose_out << "Tree type: " << "Survival" << std::endl;
if (dependent_variable_names.size() >= 2) {
*verbose_out << "Status variable name: " << dependent_variable_names[1] << std::endl;
}
}
}
void ForestSurvival::writeConfusionFile() {
// Open confusion file for writing
std::string filename = output_prefix + ".confusion";
std::ofstream outfile;
outfile.open(filename, std::ios::out);
if (!outfile.good()) {
throw std::runtime_error("Could not write to confusion file: " + filename + ".");
}
// Write confusion to file
outfile << "Overall OOB prediction error (1 - C): " << overall_prediction_error << std::endl;
outfile.close();
if (verbose_out)
*verbose_out << "Saved prediction error to file " << filename << "." << std::endl;
}
void ForestSurvival::writePredictionFile() {
// Open prediction file for writing
std::string filename = output_prefix + ".prediction";
std::ofstream outfile;
outfile.open(filename, std::ios::out);
if (!outfile.good()) {
throw std::runtime_error("Could not write to prediction file: " + filename + ".");
}
// Write
outfile << "Unique timepoints: " << std::endl;
for (auto& timepoint : unique_timepoints) {
outfile << timepoint << " ";
}
outfile << std::endl << std::endl;
outfile << "Cumulative hazard function, one row per sample: " << std::endl;
if (predict_all) {
for (size_t k = 0; k < num_trees; ++k) {
outfile << "Tree " << k << ":" << std::endl;
for (size_t i = 0; i < predictions.size(); ++i) {
for (size_t j = 0; j < predictions[i].size(); ++j) {
outfile << predictions[i][j][k] << " ";
}
outfile << std::endl;
}
outfile << std::endl;
}
} else {
for (size_t i = 0; i < predictions.size(); ++i) {
for (size_t j = 0; j < predictions[i].size(); ++j) {
for (size_t k = 0; k < predictions[i][j].size(); ++k) {
outfile << predictions[i][j][k] << " ";
}
outfile << std::endl;
}
}
}
if (verbose_out)
*verbose_out << "Saved predictions to file " << filename << "." << std::endl;
}
void ForestSurvival::saveToFileInternal(std::ofstream& outfile) {
// Write num_variables
outfile.write((char*) &num_independent_variables, sizeof(num_independent_variables));
// Write treetype
TreeType treetype = TREE_SURVIVAL;
outfile.write((char*) &treetype, sizeof(treetype));
// Write unique timepoints
saveVector1D(unique_timepoints, outfile);
}
void ForestSurvival::loadFromFileInternal(std::ifstream& infile) {
// Read number of variables
size_t num_variables_saved;
infile.read((char*) &num_variables_saved, sizeof(num_variables_saved));
// Read treetype
TreeType treetype;
infile.read((char*) &treetype, sizeof(treetype));
if (treetype != TREE_SURVIVAL) {
throw std::runtime_error("Wrong treetype. Loaded file is not a survival forest.");
}
// Read unique timepoints
unique_timepoints.clear();
readVector1D(unique_timepoints, infile);
for (size_t i = 0; i < num_trees; ++i) {
// Read data
std::vector<std::vector<size_t>> child_nodeIDs;
readVector2D(child_nodeIDs, infile);
std::vector<size_t> split_varIDs;
readVector1D(split_varIDs, infile);
std::vector<double> split_values;
readVector1D(split_values, infile);
// Read chf
std::vector<size_t> terminal_nodes;
readVector1D(terminal_nodes, infile);
std::vector<std::vector<double>> chf_vector;
readVector2D(chf_vector, infile);
// Convert chf to vector with empty elements for non-terminal nodes
std::vector<std::vector<double>> chf;
chf.resize(child_nodeIDs[0].size(), std::vector<double>());
// for (size_t i = 0; i < child_nodeIDs.size(); ++i) {
// chf.push_back(std::vector<double>());
// }
for (size_t j = 0; j < terminal_nodes.size(); ++j) {
chf[terminal_nodes[j]] = chf_vector[j];
}
// If dependent variable not in test data, throw error
if (num_variables_saved != num_independent_variables) {
throw std::runtime_error("Number of independent variables in data does not match with the loaded forest.");
}
// Create tree
trees.push_back(
std::make_unique<TreeSurvival>(child_nodeIDs, split_varIDs, split_values, chf, &unique_timepoints,
&response_timepointIDs));
}
}
const std::vector<double>& ForestSurvival::getTreePrediction(size_t tree_idx, size_t sample_idx) const {
const auto& tree = dynamic_cast<const TreeSurvival&>(*trees[tree_idx]);
return tree.getPrediction(sample_idx);
}
size_t ForestSurvival::getTreePredictionTerminalNodeID(size_t tree_idx, size_t sample_idx) const {
const auto& tree = dynamic_cast<const TreeSurvival&>(*trees[tree_idx]);
return tree.getPredictionTerminalNodeID(sample_idx);
}
// #nocov end
}// namespace ranger
|