]> git.donarmstrong.com Git - mothur.git/blob - rftreenode.cpp
changes while testing
[mothur.git] / rftreenode.cpp
1 //
2 //  rftreenode.cpp
3 //  Mothur
4 //
5 //  Created by Sarah Westcott on 10/2/12.
6 //  Copyright (c) 2012 Schloss Lab. All rights reserved.
7 //
8
9 #include "rftreenode.hpp"
10
11 /***********************************************************************/
12 RFTreeNode::RFTreeNode(vector< vector<int> > bootstrappedTrainingSamples,
13            vector<int> globalDiscardedFeatureIndices,
14            int numFeatures,
15            int numSamples,
16            int numOutputClasses,
17            int generation)
18
19 : bootstrappedTrainingSamples(bootstrappedTrainingSamples),
20 globalDiscardedFeatureIndices(globalDiscardedFeatureIndices),
21 numFeatures(numFeatures),
22 numSamples(numSamples),
23 numOutputClasses(numOutputClasses),
24 generation(generation),
25 isLeaf(false),
26 outputClass(-1),
27 splitFeatureIndex(-1),
28 splitFeatureValue(-1),
29 splitFeatureEntropy(-1.0),
30 ownEntropy(-1.0),
31 bootstrappedFeatureVectors(numFeatures, vector<int>(numSamples, 0)),
32 bootstrappedOutputVector(numSamples, 0),
33 leftChildNode(NULL),
34 rightChildNode(NULL),
35 parentNode(NULL) {
36     m = MothurOut::getInstance();
37     
38     for (int i = 0; i < numSamples; i++) {    // just doing a simple transpose of the matrix
39         if (m->control_pressed) { break; }
40         for (int j = 0; j < numFeatures; j++) { bootstrappedFeatureVectors[j][i] = bootstrappedTrainingSamples[i][j]; }
41     }
42     
43     for (int i = 0; i < numSamples; i++) { if (m->control_pressed) { break; } bootstrappedOutputVector[i] = bootstrappedTrainingSamples[i][numFeatures]; }
44     
45     createLocalDiscardedFeatureList();
46     updateNodeEntropy();
47 }
48 /***********************************************************************/
49 int RFTreeNode::createLocalDiscardedFeatureList(){
50     try {
51
52         for (int i = 0; i < numFeatures; i++) {
53             if (m->control_pressed) { return 0; } 
54             vector<int>::iterator it = find(globalDiscardedFeatureIndices.begin(), globalDiscardedFeatureIndices.end(), i);
55             if (it == globalDiscardedFeatureIndices.end()){                           // NOT FOUND
56                 double standardDeviation = m->getStandardDeviation(bootstrappedFeatureVectors[i]);  
57                 if (standardDeviation <= 0){ localDiscardedFeatureIndices.push_back(i); }
58             }
59         }
60         
61         return 0;
62     }
63     catch(exception& e) {
64         m->errorOut(e, "RFTreeNode", "createLocalDiscardedFeatureList");
65         exit(1);
66     }  
67 }
68 /***********************************************************************/
69 int RFTreeNode::updateNodeEntropy() {
70     try {
71         
72         vector<int> classCounts(numOutputClasses, 0);
73         for (int i = 0; i < bootstrappedOutputVector.size(); i++) { classCounts[bootstrappedOutputVector[i]]++; }
74         int totalClassCounts = accumulate(classCounts.begin(), classCounts.end(), 0);
75         double nodeEntropy = 0.0;
76         for (int i = 0; i < classCounts.size(); i++) {
77             if (m->control_pressed) { return 0; }
78             if (classCounts[i] == 0) continue;
79             double probability = (double)classCounts[i] / (double)totalClassCounts;
80             nodeEntropy += -(probability * log2(probability));
81         }
82         ownEntropy = nodeEntropy;
83         
84         return 0;
85     }
86     catch(exception& e) {
87         m->errorOut(e, "RFTreeNode", "updateNodeEntropy");
88         exit(1);
89     } 
90 }
91
92 /***********************************************************************/