File: forest.h

package info (click to toggle)
itksnap 3.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,196 kB
  • ctags: 9,196
  • sloc: cpp: 62,895; sh: 175; makefile: 13
file content (114 lines) | stat: -rwxr-xr-x 2,874 bytes parent folder | download | duplicates (3)
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
/**
 * Define decision forest as a vector of decision tree's pointer,
 * testing processing runs in parallel by openMP.
 */

#ifndef FOREST_H
#define FOREST_H

#include "tree.h"

template<class S, class C, class dataT>
class DecisionForest
{
public:
  typedef DecisionTree<S, C, dataT> DecisionTreeT;

  DecisionForest(): verbose_(false) {}
  DecisionForest(bool verbose): verbose_(verbose) {}
  ~DecisionForest()
  {
    for (size_t i = 0; i < trees_.size(); ++i)
      {
        delete trees_[i];
      }
  }

  DecisionTreeT* AddTree()
  {
    DecisionTreeT* ctree = new DecisionTreeT(verbose_);
    ctree->idx_ = trees_.size();
    trees_.push_back(ctree);
    return ctree;
  }

  void Apply(MLData<dataT, S*>& testingData,
             Vector<Vector<S*> >& testingResult)
  {
    size_t treeNum = trees_.size();
    size_t dataNum = testingData.Size();

    testingResult.Resize(treeNum);
    #pragma omp parallel for
    for (index_t i = 0; i < treeNum; ++i)
      {
        testingResult[i].Resize(dataNum);
        trees_[i]->Apply(testingData, testingResult[i]);
      }
  }

  // Added by Paul. This version of the method allows the same testingResult
  // vector to be reused. For speed, it does not check that the size of the
  // testing result is sufficient
  // This version of the method takes a preallocated vector
  // index and a preallocated vector response_vec of the same size as
  // testingData. It does not do allocation to save on computation time
  void ApplyFast(MLData<dataT, S*>& testingData,
                 Vector<Vector<S*> >& testingResult,
                 std::vector<index_t> &index,
                 std::vector<bool> &response)
  {
    size_t treeNum = trees_.size();
    for (index_t i = 0; i < treeNum; ++i)
        trees_[i]->ApplyFast(testingData, testingResult[i], index, response);
  }

  void Print(int level)
  {
    for (index_t i = 0; i < trees_.size(); ++i)
      {
        std::cout << "tree " << i << " information:" << std::endl;
        trees_[i]->Print(level);
      }
  }

  void Read(std::istream& is)
  {
    size_t treeNum = 0;
    readBasicType(is, verbose_);
    readBasicType(is, treeNum);

    size_t treeNumBefore = trees_.size();
    if (treeNumBefore != 0)
      {
        for (index_t i = 0; i < treeNumBefore; ++i)
          {
            delete trees_[i];
          }
      }
    trees_.resize(treeNum);
    for (index_t i = 0; i < treeNum; ++i)
      {
        trees_[i] = new DecisionTreeT(verbose_);
        trees_[i]->Read(is);
      }
  }

  void Write(std::ostream& os)
  {
    size_t treeNum = trees_.size();
    writeBasicType(os, verbose_);
    writeBasicType(os, treeNum);
    for (index_t i = 0; i < treeNum; ++i)
      {
        trees_[i]->Write(os);
      }
  }

  int GetForestSize() { return trees_.size(); }

  std::vector<DecisionTreeT*> trees_;
  bool verbose_;
};

#endif // FOREST_H