forked from imbs-hl/ranger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForest.cpp
951 lines (806 loc) · 31.1 KB
/
Forest.cpp
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
/*-------------------------------------------------------------------------------
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 <math.h>
#include <algorithm>
#include <stdexcept>
#include <string>
#include <ctime>
#include <functional>
#ifndef OLD_WIN_R_BUILD
#include <thread>
#include <chrono>
#endif
#include "utility.h"
#include "Forest.h"
#include "DataChar.h"
#include "DataDouble.h"
#include "DataFloat.h"
namespace ranger {
Forest::Forest() :
verbose_out(0), num_trees(DEFAULT_NUM_TREE), mtry(0), min_node_size(0), num_variables(0), num_independent_variables(
0), seed(0), dependent_varID(0), num_samples(0), prediction_mode(false), memory_mode(MEM_DOUBLE), sample_with_replacement(
true), memory_saving_splitting(false), splitrule(DEFAULT_SPLITRULE), predict_all(false), keep_inbag(false), sample_fraction(
{ 1 }), holdout(false), prediction_type(DEFAULT_PREDICTIONTYPE), num_random_splits(DEFAULT_NUM_RANDOM_SPLITS), alpha(
DEFAULT_ALPHA), minprop(DEFAULT_MINPROP), num_threads(DEFAULT_NUM_THREADS), data { }, overall_prediction_error(
NAN), importance_mode(DEFAULT_IMPORTANCE_MODE), progress(0) {
}
// #nocov start
std::unique_ptr<Data> load_data_from_file(const std::string& data_path, const MemoryMode memory_mode,
std::ostream* verbose_out = nullptr) {
std::unique_ptr<Data> result { };
switch (memory_mode) {
case MEM_DOUBLE:
result = make_unique<DataDouble>();
break;
case MEM_FLOAT:
result = make_unique<DataFloat>();
break;
case MEM_CHAR:
result = make_unique<DataChar>();
break;
}
if (verbose_out)
*verbose_out << "Loading input file: " << data_path << "." << std::endl;
bool found_rounding_error = result->loadFromFile(data_path);
if (found_rounding_error && verbose_out) {
*verbose_out << "Warning: Rounding or Integer overflow occurred. Use FLOAT or DOUBLE precision to avoid this."
<< std::endl;
}
return result;
}
void Forest::initCpp(std::string dependent_variable_name, MemoryMode memory_mode, std::string input_file, uint mtry,
std::string output_prefix, uint num_trees, std::ostream* verbose_out, uint seed, uint num_threads,
std::string load_forest_filename, ImportanceMode importance_mode, uint min_node_size,
std::string split_select_weights_file, const std::vector<std::string>& always_split_variable_names,
std::string status_variable_name, bool sample_with_replacement,
const std::vector<std::string>& unordered_variable_names, bool memory_saving_splitting, SplitRule splitrule,
std::string case_weights_file, bool predict_all, double sample_fraction, double alpha, double minprop, bool holdout,
PredictionType prediction_type, uint num_random_splits) {
this->verbose_out = verbose_out;
// Set prediction mode
bool prediction_mode = false;
if (!load_forest_filename.empty()) {
prediction_mode = true;
}
// Sample fraction to vector
std::vector<double> sample_fraction_vector = { sample_fraction };
// Call other init function
init(dependent_variable_name, memory_mode, load_data_from_file(input_file, memory_mode, verbose_out), mtry,
output_prefix, num_trees, seed, num_threads, importance_mode, min_node_size, status_variable_name,
prediction_mode, sample_with_replacement, unordered_variable_names, memory_saving_splitting, splitrule,
predict_all, sample_fraction_vector, alpha, minprop, holdout, prediction_type, num_random_splits, false);
if (prediction_mode) {
loadFromFile(load_forest_filename);
}
// Set variables to be always considered for splitting
if (!always_split_variable_names.empty()) {
setAlwaysSplitVariables(always_split_variable_names);
}
// TODO: Read 2d weights for tree-wise split select weights
// Load split select weights from file
if (!split_select_weights_file.empty()) {
std::vector<std::vector<double>> split_select_weights;
split_select_weights.resize(1);
loadDoubleVectorFromFile(split_select_weights[0], split_select_weights_file);
if (split_select_weights[0].size() != num_variables - 1) {
throw std::runtime_error("Number of split select weights is not equal to number of independent variables.");
}
setSplitWeightVector(split_select_weights);
}
// Load case weights from file
if (!case_weights_file.empty()) {
loadDoubleVectorFromFile(case_weights, case_weights_file);
if (case_weights.size() != num_samples) {
throw std::runtime_error("Number of case weights is not equal to number of samples.");
}
}
// Sample from non-zero weights in holdout mode
if (holdout && !case_weights.empty()) {
size_t nonzero_weights = 0;
for (auto& weight : case_weights) {
if (weight > 0) {
++nonzero_weights;
}
}
this->sample_fraction[0] = this->sample_fraction[0] * ((double) nonzero_weights / (double) num_samples);
}
// Check if all catvars are coded in integers starting at 1
if (!unordered_variable_names.empty()) {
std::string error_message = checkUnorderedVariables(*data, unordered_variable_names);
if (!error_message.empty()) {
throw std::runtime_error(error_message);
}
}
}
// #nocov end
void Forest::initR(std::string dependent_variable_name, std::unique_ptr<Data> input_data, uint mtry, uint num_trees,
std::ostream* verbose_out, uint seed, uint num_threads, ImportanceMode importance_mode, uint min_node_size,
std::vector<std::vector<double>>& split_select_weights, const std::vector<std::string>& always_split_variable_names,
std::string status_variable_name, bool prediction_mode, bool sample_with_replacement,
const std::vector<std::string>& unordered_variable_names, bool memory_saving_splitting, SplitRule splitrule,
std::vector<double>& case_weights, bool predict_all, bool keep_inbag, std::vector<double>& sample_fraction,
double alpha, double minprop, bool holdout, PredictionType prediction_type, uint num_random_splits,
bool order_snps) {
this->verbose_out = verbose_out;
// Call other init function
init(dependent_variable_name, MEM_DOUBLE, std::move(input_data), mtry, "", num_trees, seed, num_threads,
importance_mode, min_node_size, status_variable_name, prediction_mode, sample_with_replacement,
unordered_variable_names, memory_saving_splitting, splitrule, predict_all, sample_fraction, alpha, minprop,
holdout, prediction_type, num_random_splits, order_snps);
// Set variables to be always considered for splitting
if (!always_split_variable_names.empty()) {
setAlwaysSplitVariables(always_split_variable_names);
}
// Set split select weights
if (!split_select_weights.empty()) {
setSplitWeightVector(split_select_weights);
}
// Set case weights
if (!case_weights.empty()) {
if (case_weights.size() != num_samples) {
throw std::runtime_error("Number of case weights not equal to number of samples.");
}
this->case_weights = case_weights;
}
// Keep inbag counts
this->keep_inbag = keep_inbag;
}
void Forest::init(std::string dependent_variable_name, MemoryMode memory_mode, std::unique_ptr<Data> input_data,
uint mtry, std::string output_prefix, uint num_trees, uint seed, uint num_threads, ImportanceMode importance_mode,
uint min_node_size, std::string status_variable_name, bool prediction_mode, bool sample_with_replacement,
const std::vector<std::string>& unordered_variable_names, bool memory_saving_splitting, SplitRule splitrule,
bool predict_all, std::vector<double>& sample_fraction, double alpha, double minprop, bool holdout,
PredictionType prediction_type, uint num_random_splits, bool order_snps) {
// Initialize data with memmode
this->data = std::move(input_data);
// Initialize random number generator and set seed
if (seed == 0) {
std::random_device random_device;
random_number_generator.seed(random_device());
} else {
random_number_generator.seed(seed);
}
// Set number of threads
if (num_threads == DEFAULT_NUM_THREADS) {
#ifdef OLD_WIN_R_BUILD
this->num_threads = 1;
#else
this->num_threads = std::thread::hardware_concurrency();
#endif
} else {
this->num_threads = num_threads;
}
// Set member variables
this->num_trees = num_trees;
this->mtry = mtry;
this->seed = seed;
this->output_prefix = output_prefix;
this->importance_mode = importance_mode;
this->min_node_size = min_node_size;
this->memory_mode = memory_mode;
this->prediction_mode = prediction_mode;
this->sample_with_replacement = sample_with_replacement;
this->memory_saving_splitting = memory_saving_splitting;
this->splitrule = splitrule;
this->predict_all = predict_all;
this->sample_fraction = sample_fraction;
this->holdout = holdout;
this->alpha = alpha;
this->minprop = minprop;
this->prediction_type = prediction_type;
this->num_random_splits = num_random_splits;
// Set number of samples and variables
num_samples = data->getNumRows();
num_variables = data->getNumCols();
// Convert dependent variable name to ID
if (!prediction_mode && !dependent_variable_name.empty()) {
dependent_varID = data->getVariableID(dependent_variable_name);
}
// Set unordered factor variables
if (!prediction_mode) {
data->setIsOrderedVariable(unordered_variable_names);
}
data->addNoSplitVariable(dependent_varID);
initInternal(status_variable_name);
num_independent_variables = num_variables - data->getNoSplitVariables().size();
// Init split select weights
split_select_weights.push_back(std::vector<double>());
// Check if mtry is in valid range
if (this->mtry > num_variables - 1) {
throw std::runtime_error("mtry can not be larger than number of variables in data.");
}
// Check if any observations samples
if ((size_t) num_samples * sample_fraction[0] < 1) {
throw std::runtime_error("sample_fraction too small, no observations sampled.");
}
// Permute samples for corrected Gini importance
if (importance_mode == IMP_GINI_CORRECTED) {
data->permuteSampleIDs(random_number_generator);
}
// Order SNP levels if in "order" splitting
if (!prediction_mode && order_snps) {
data->orderSnpLevels(dependent_variable_name, (importance_mode == IMP_GINI_CORRECTED));
}
}
void Forest::run(bool verbose, bool compute_oob_error) {
if (prediction_mode) {
if (verbose && verbose_out) {
*verbose_out << "Predicting .." << std::endl;
}
predict();
} else {
if (verbose && verbose_out) {
*verbose_out << "Growing trees .." << std::endl;
}
grow();
if (verbose && verbose_out) {
*verbose_out << "Computing prediction error .." << std::endl;
}
if (compute_oob_error) {
computePredictionError();
}
if (importance_mode == IMP_PERM_BREIMAN || importance_mode == IMP_PERM_LIAW || importance_mode == IMP_PERM_RAW) {
if (verbose && verbose_out) {
*verbose_out << "Computing permutation variable importance .." << std::endl;
}
computePermutationImportance();
}
}
}
// #nocov start
void Forest::writeOutput() {
if (verbose_out)
*verbose_out << std::endl;
writeOutputInternal();
if (verbose_out) {
*verbose_out << "Dependent variable name: " << data->getVariableNames()[dependent_varID] << std::endl;
*verbose_out << "Dependent variable ID: " << dependent_varID << std::endl;
*verbose_out << "Number of trees: " << num_trees << std::endl;
*verbose_out << "Sample size: " << num_samples << std::endl;
*verbose_out << "Number of independent variables: " << num_independent_variables << std::endl;
*verbose_out << "Mtry: " << mtry << std::endl;
*verbose_out << "Target node size: " << min_node_size << std::endl;
*verbose_out << "Variable importance mode: " << importance_mode << std::endl;
*verbose_out << "Memory mode: " << memory_mode << std::endl;
*verbose_out << "Seed: " << seed << std::endl;
*verbose_out << "Number of threads: " << num_threads << std::endl;
*verbose_out << std::endl;
}
if (prediction_mode) {
writePredictionFile();
} else {
if (verbose_out) {
*verbose_out << "Overall OOB prediction error: " << overall_prediction_error << std::endl;
*verbose_out << std::endl;
}
if (!split_select_weights.empty() & !split_select_weights[0].empty()) {
if (verbose_out) {
*verbose_out
<< "Warning: Split select weights used. Variable importance measures are only comparable for variables with equal weights."
<< std::endl;
}
}
if (importance_mode != IMP_NONE) {
writeImportanceFile();
}
writeConfusionFile();
}
}
void Forest::writeImportanceFile() {
// Open importance file for writing
std::string filename = output_prefix + ".importance";
std::ofstream importance_file;
importance_file.open(filename, std::ios::out);
if (!importance_file.good()) {
throw std::runtime_error("Could not write to importance file: " + filename + ".");
}
// Write importance to file
for (size_t i = 0; i < variable_importance.size(); ++i) {
size_t varID = i;
for (auto& skip : data->getNoSplitVariables()) {
if (varID >= skip) {
++varID;
}
}
std::string variable_name = data->getVariableNames()[varID];
importance_file << variable_name << ": " << variable_importance[i] << std::endl;
}
importance_file.close();
if (verbose_out)
*verbose_out << "Saved variable importance to file " << filename << "." << std::endl;
}
void Forest::saveToFile() {
// Open file for writing
std::string filename = output_prefix + ".forest";
std::ofstream outfile;
outfile.open(filename, std::ios::binary);
if (!outfile.good()) {
throw std::runtime_error("Could not write to output file: " + filename + ".");
}
// Write dependent_varID
outfile.write((char*) &dependent_varID, sizeof(dependent_varID));
// Write num_trees
outfile.write((char*) &num_trees, sizeof(num_trees));
// Write is_ordered_variable
saveVector1D(data->getIsOrderedVariable(), outfile);
saveToFileInternal(outfile);
// Write tree data for each tree
for (auto& tree : trees) {
tree->appendToFile(outfile);
}
// Close file
outfile.close();
if (verbose_out)
*verbose_out << "Saved forest to file " << filename << "." << std::endl;
}
// #nocov end
void Forest::grow() {
// Create thread ranges
equalSplit(thread_ranges, 0, num_trees - 1, num_threads);
// Call special grow functions of subclasses. There trees must be created.
growInternal();
// Init trees, create a seed for each tree, based on main seed
std::uniform_int_distribution<uint> udist;
for (size_t i = 0; i < num_trees; ++i) {
uint tree_seed;
if (seed == 0) {
tree_seed = udist(random_number_generator);
} else {
tree_seed = (i + 1) * seed;
}
// Get split select weights for tree
std::vector<double>* tree_split_select_weights;
if (split_select_weights.size() > 1) {
tree_split_select_weights = &split_select_weights[i];
} else {
tree_split_select_weights = &split_select_weights[0];
}
trees[i]->init(data.get(), mtry, dependent_varID, num_samples, tree_seed, &deterministic_varIDs,
&split_select_varIDs, tree_split_select_weights, importance_mode, min_node_size, sample_with_replacement,
memory_saving_splitting, splitrule, &case_weights, keep_inbag, &sample_fraction, alpha, minprop, holdout,
num_random_splits);
}
// Init variable importance
variable_importance.resize(num_independent_variables, 0);
// Grow trees in multiple threads
#ifdef OLD_WIN_R_BUILD
progress = 0;
clock_t start_time = clock();
clock_t lap_time = clock();
for (size_t i = 0; i < num_trees; ++i) {
trees[i]->grow(&variable_importance);
progress++;
showProgress("Growing trees..", start_time, lap_time);
}
#else
progress = 0;
#ifdef R_BUILD
aborted = false;
aborted_threads = 0;
#endif
std::vector<std::thread> threads;
threads.reserve(num_threads);
// Initailize importance per thread
std::vector<std::vector<double>> variable_importance_threads(num_threads);
for (uint i = 0; i < num_threads; ++i) {
if (importance_mode == IMP_GINI || importance_mode == IMP_GINI_CORRECTED) {
variable_importance_threads[i].resize(num_independent_variables, 0);
}
threads.emplace_back(&Forest::growTreesInThread, this, i, &(variable_importance_threads[i]));
}
showProgress("Growing trees..", num_trees);
for (auto &thread : threads) {
thread.join();
}
#ifdef R_BUILD
if (aborted_threads > 0) {
throw std::runtime_error("User interrupt.");
}
#endif
// Sum thread importances
if (importance_mode == IMP_GINI || importance_mode == IMP_GINI_CORRECTED) {
variable_importance.resize(num_independent_variables, 0);
for (size_t i = 0; i < num_independent_variables; ++i) {
for (uint j = 0; j < num_threads; ++j) {
variable_importance[i] += variable_importance_threads[j][i];
}
}
variable_importance_threads.clear();
}
#endif
// Divide importance by number of trees
if (importance_mode == IMP_GINI || importance_mode == IMP_GINI_CORRECTED) {
for (auto& v : variable_importance) {
v /= num_trees;
}
}
}
void Forest::predict() {
// Predict trees in multiple threads and join the threads with the main thread
#ifdef OLD_WIN_R_BUILD
progress = 0;
clock_t start_time = clock();
clock_t lap_time = clock();
for (size_t i = 0; i < num_trees; ++i) {
trees[i]->predict(data.get(), false);
progress++;
showProgress("Predicting..", start_time, lap_time);
}
// For all samples get tree predictions
allocatePredictMemory();
for (size_t sample_idx = 0; sample_idx < data->getNumRows(); ++sample_idx) {
predictInternal(sample_idx);
}
#else
progress = 0;
#ifdef R_BUILD
aborted = false;
aborted_threads = 0;
#endif
// Predict
std::vector<std::thread> threads;
threads.reserve(num_threads);
for (uint i = 0; i < num_threads; ++i) {
threads.emplace_back(&Forest::predictTreesInThread, this, i, data.get(), false);
}
showProgress("Predicting..", num_trees);
for (auto &thread : threads) {
thread.join();
}
// Aggregate predictions
allocatePredictMemory();
threads.clear();
threads.reserve(num_threads);
progress = 0;
for (uint i = 0; i < num_threads; ++i) {
threads.emplace_back(&Forest::predictInternalInThread, this, i);
}
showProgress("Aggregating predictions..", num_samples);
for (auto &thread : threads) {
thread.join();
}
#ifdef R_BUILD
if (aborted_threads > 0) {
throw std::runtime_error("User interrupt.");
}
#endif
#endif
}
void Forest::computePredictionError() {
// Predict trees in multiple threads
#ifdef OLD_WIN_R_BUILD
progress = 0;
clock_t start_time = clock();
clock_t lap_time = clock();
for (size_t i = 0; i < num_trees; ++i) {
trees[i]->predict(data.get(), true);
progress++;
showProgress("Predicting..", start_time, lap_time);
}
#else
std::vector<std::thread> threads;
threads.reserve(num_threads);
progress = 0;
for (uint i = 0; i < num_threads; ++i) {
threads.emplace_back(&Forest::predictTreesInThread, this, i, data.get(), true);
}
showProgress("Computing prediction error..", num_trees);
for (auto &thread : threads) {
thread.join();
}
#ifdef R_BUILD
if (aborted_threads > 0) {
throw std::runtime_error("User interrupt.");
}
#endif
#endif
// Call special function for subclasses
computePredictionErrorInternal();
}
void Forest::computePermutationImportance() {
// Compute tree permutation importance in multiple threads
#ifdef OLD_WIN_R_BUILD
progress = 0;
clock_t start_time = clock();
clock_t lap_time = clock();
// Initailize importance and variance
variable_importance.resize(num_independent_variables, 0);
std::vector<double> variance;
if (importance_mode == IMP_PERM_BREIMAN || importance_mode == IMP_PERM_LIAW) {
variance.resize(num_independent_variables, 0);
}
// Compute importance
for (size_t i = 0; i < num_trees; ++i) {
trees[i]->computePermutationImportance(variable_importance, variance);
progress++;
showProgress("Computing permutation importance..", start_time, lap_time);
}
#else
progress = 0;
#ifdef R_BUILD
aborted = false;
aborted_threads = 0;
#endif
std::vector<std::thread> threads;
threads.reserve(num_threads);
// Initailize importance and variance
std::vector<std::vector<double>> variable_importance_threads(num_threads);
std::vector<std::vector<double>> variance_threads(num_threads);
// Compute importance
for (uint i = 0; i < num_threads; ++i) {
variable_importance_threads[i].resize(num_independent_variables, 0);
if (importance_mode == IMP_PERM_BREIMAN || importance_mode == IMP_PERM_LIAW) {
variance_threads[i].resize(num_independent_variables, 0);
}
threads.emplace_back(&Forest::computeTreePermutationImportanceInThread, this, i,
std::ref(variable_importance_threads[i]), std::ref(variance_threads[i]));
}
showProgress("Computing permutation importance..", num_trees);
for (auto &thread : threads) {
thread.join();
}
#ifdef R_BUILD
if (aborted_threads > 0) {
throw std::runtime_error("User interrupt.");
}
#endif
// Sum thread importances
variable_importance.resize(num_independent_variables, 0);
for (size_t i = 0; i < num_independent_variables; ++i) {
for (uint j = 0; j < num_threads; ++j) {
variable_importance[i] += variable_importance_threads[j][i];
}
}
variable_importance_threads.clear();
// Sum thread variances
std::vector<double> variance(num_independent_variables, 0);
if (importance_mode == IMP_PERM_BREIMAN || importance_mode == IMP_PERM_LIAW) {
for (size_t i = 0; i < num_independent_variables; ++i) {
for (uint j = 0; j < num_threads; ++j) {
variance[i] += variance_threads[j][i];
}
}
variance_threads.clear();
}
#endif
for (size_t i = 0; i < variable_importance.size(); ++i) {
variable_importance[i] /= num_trees;
// Normalize by variance for scaled permutation importance
if (importance_mode == IMP_PERM_BREIMAN || importance_mode == IMP_PERM_LIAW) {
if (variance[i] != 0) {
variance[i] = variance[i] / num_trees - variable_importance[i] * variable_importance[i];
variable_importance[i] /= sqrt(variance[i] / num_trees);
}
}
}
}
#ifndef OLD_WIN_R_BUILD
void Forest::growTreesInThread(uint thread_idx, std::vector<double>* variable_importance) {
if (thread_ranges.size() > thread_idx + 1) {
for (size_t i = thread_ranges[thread_idx]; i < thread_ranges[thread_idx + 1]; ++i) {
trees[i]->grow(variable_importance);
// Check for user interrupt
#ifdef R_BUILD
if (aborted) {
std::unique_lock<std::mutex> lock(mutex);
++aborted_threads;
condition_variable.notify_one();
return;
}
#endif
// Increase progress by 1 tree
std::unique_lock<std::mutex> lock(mutex);
++progress;
condition_variable.notify_one();
}
}
}
void Forest::predictTreesInThread(uint thread_idx, const Data* prediction_data, bool oob_prediction) {
if (thread_ranges.size() > thread_idx + 1) {
for (size_t i = thread_ranges[thread_idx]; i < thread_ranges[thread_idx + 1]; ++i) {
trees[i]->predict(prediction_data, oob_prediction);
// Check for user interrupt
#ifdef R_BUILD
if (aborted) {
std::unique_lock<std::mutex> lock(mutex);
++aborted_threads;
condition_variable.notify_one();
return;
}
#endif
// Increase progress by 1 tree
std::unique_lock<std::mutex> lock(mutex);
++progress;
condition_variable.notify_one();
}
}
}
void Forest::predictInternalInThread(uint thread_idx) {
// Create thread ranges
std::vector<uint> predict_ranges;
equalSplit(predict_ranges, 0, num_samples - 1, num_threads);
if (predict_ranges.size() > thread_idx + 1) {
for (size_t i = predict_ranges[thread_idx]; i < predict_ranges[thread_idx + 1]; ++i) {
predictInternal(i);
// Check for user interrupt
#ifdef R_BUILD
if (aborted) {
std::unique_lock<std::mutex> lock(mutex);
++aborted_threads;
condition_variable.notify_one();
return;
}
#endif
// Increase progress by 1 tree
std::unique_lock<std::mutex> lock(mutex);
++progress;
condition_variable.notify_one();
}
}
}
void Forest::computeTreePermutationImportanceInThread(uint thread_idx, std::vector<double>& importance,
std::vector<double>& variance) {
if (thread_ranges.size() > thread_idx + 1) {
for (size_t i = thread_ranges[thread_idx]; i < thread_ranges[thread_idx + 1]; ++i) {
trees[i]->computePermutationImportance(importance, variance);
// Check for user interrupt
#ifdef R_BUILD
if (aborted) {
std::unique_lock<std::mutex> lock(mutex);
++aborted_threads;
condition_variable.notify_one();
return;
}
#endif
// Increase progress by 1 tree
std::unique_lock<std::mutex> lock(mutex);
++progress;
condition_variable.notify_one();
}
}
}
#endif
// #nocov start
void Forest::loadFromFile(std::string filename) {
if (verbose_out)
*verbose_out << "Loading forest from file " << filename << "." << std::endl;
// Open file for reading
std::ifstream infile;
infile.open(filename, std::ios::binary);
if (!infile.good()) {
throw std::runtime_error("Could not read from input file: " + filename + ".");
}
// Read dependent_varID and num_trees
infile.read((char*) &dependent_varID, sizeof(dependent_varID));
infile.read((char*) &num_trees, sizeof(num_trees));
// Read is_ordered_variable
readVector1D(data->getIsOrderedVariable(), infile);
// Read tree data. This is different for tree types -> virtual function
loadFromFileInternal(infile);
infile.close();
// Create thread ranges
equalSplit(thread_ranges, 0, num_trees - 1, num_threads);
}
// #nocov end
void Forest::setSplitWeightVector(std::vector<std::vector<double>>& split_select_weights) {
// Size should be 1 x num_independent_variables or num_trees x num_independent_variables
if (split_select_weights.size() != 1 && split_select_weights.size() != num_trees) {
throw std::runtime_error("Size of split select weights not equal to 1 or number of trees.");
}
// Reserve space
if (split_select_weights.size() == 1) {
this->split_select_weights[0].resize(num_independent_variables);
} else {
this->split_select_weights.clear();
this->split_select_weights.resize(num_trees, std::vector<double>(num_independent_variables));
}
this->split_select_varIDs.resize(num_independent_variables);
deterministic_varIDs.reserve(num_independent_variables);
// Split up in deterministic and weighted variables, ignore zero weights
for (size_t i = 0; i < split_select_weights.size(); ++i) {
// Size should be 1 x num_independent_variables or num_trees x num_independent_variables
if (split_select_weights[i].size() != num_independent_variables) {
throw std::runtime_error("Number of split select weights not equal to number of independent variables.");
}
for (size_t j = 0; j < split_select_weights[i].size(); ++j) {
double weight = split_select_weights[i][j];
if (i == 0) {
size_t varID = j;
for (auto& skip : data->getNoSplitVariables()) {
if (varID >= skip) {
++varID;
}
}
if (weight == 1) {
deterministic_varIDs.push_back(varID);
} else if (weight < 1 && weight > 0) {
this->split_select_varIDs[j] = varID;
this->split_select_weights[i][j] = weight;
} else if (weight < 0 || weight > 1) {
throw std::runtime_error("One or more split select weights not in range [0,1].");
}
} else {
if (weight < 1 && weight > 0) {
this->split_select_weights[i][j] = weight;
} else if (weight < 0 || weight > 1) {
throw std::runtime_error("One or more split select weights not in range [0,1].");
}
}
}
}
if (deterministic_varIDs.size() > this->mtry) {
throw std::runtime_error("Number of ones in split select weights cannot be larger than mtry.");
}
if (deterministic_varIDs.size() + split_select_varIDs.size() < mtry) {
throw std::runtime_error("Too many zeros in split select weights. Need at least mtry variables to split at.");
}
}
void Forest::setAlwaysSplitVariables(const std::vector<std::string>& always_split_variable_names) {
deterministic_varIDs.reserve(num_independent_variables);
for (auto& variable_name : always_split_variable_names) {
size_t varID = data->getVariableID(variable_name);
deterministic_varIDs.push_back(varID);
}
if (deterministic_varIDs.size() + this->mtry > num_independent_variables) {
throw std::runtime_error(
"Number of variables to be always considered for splitting plus mtry cannot be larger than number of independent variables.");
}
}
#ifdef OLD_WIN_R_BUILD
void Forest::showProgress(std::string operation, clock_t start_time, clock_t& lap_time) {
// Check for user interrupt
if (checkInterrupt()) {
throw std::runtime_error("User interrupt.");
}
double elapsed_time = (clock() - lap_time) / CLOCKS_PER_SEC;
if (elapsed_time > STATUS_INTERVAL) {
double relative_progress = (double) progress / (double) num_trees;
double time_from_start = (clock() - start_time) / CLOCKS_PER_SEC;
uint remaining_time = (1 / relative_progress - 1) * time_from_start;
if (verbose_out) {
*verbose_out << operation << " Progress: " << round(100 * relative_progress)
<< "%. Estimated remaining time: " << beautifyTime(remaining_time) << "." << std::endl;
}
lap_time = clock();
}
}
#else
void Forest::showProgress(std::string operation, size_t max_progress) {
using std::chrono::steady_clock;
using std::chrono::duration_cast;
using std::chrono::seconds;
steady_clock::time_point start_time = steady_clock::now();
steady_clock::time_point last_time = steady_clock::now();
std::unique_lock<std::mutex> lock(mutex);
// Wait for message from threads and show output if enough time elapsed
while (progress < max_progress) {
condition_variable.wait(lock);
seconds elapsed_time = duration_cast<seconds>(steady_clock::now() - last_time);
// Check for user interrupt
#ifdef R_BUILD
if (!aborted && checkInterrupt()) {
aborted = true;
}
if (aborted && aborted_threads >= num_threads) {
return;
}
#endif
if (progress > 0 && elapsed_time.count() > STATUS_INTERVAL) {
double relative_progress = (double) progress / (double) max_progress;
seconds time_from_start = duration_cast<seconds>(steady_clock::now() - start_time);
uint remaining_time = (1 / relative_progress - 1) * time_from_start.count();
if (verbose_out) {
*verbose_out << operation << " Progress: " << round(100 * relative_progress) << "%. Estimated remaining time: "
<< beautifyTime(remaining_time) << "." << std::endl;
}
last_time = steady_clock::now();
}
}
}
#endif
} // namespace ranger