From ab4e1b134c18b2cdc4791808e1acb10aaebdb3cb Mon Sep 17 00:00:00 2001 From: j1c Date: Tue, 27 Aug 2019 13:50:09 -0400 Subject: [PATCH 01/16] Add case for graph --- Python/rerf/rerfClassifier.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Python/rerf/rerfClassifier.py b/Python/rerf/rerfClassifier.py index bbf3b091..5a5127fd 100644 --- a/Python/rerf/rerfClassifier.py +++ b/Python/rerf/rerfClassifier.py @@ -219,7 +219,7 @@ def fit(self, X, y): else: forestType = "binnedBaseTern" self.method_to_use_ = 1 - elif self.projection_matrix == "S-RerF": + elif self.projection_matrix.isin(["S-RerF", "Graph-RerF"]): if self.oob_score: warn( "OOB is not currently implemented for the S-RerF" @@ -230,7 +230,11 @@ def fit(self, X, y): self.oob_score = False forestType = "binnedBaseTern" # this should change - self.method_to_use_ = 2 + if self.projection_matrix == "S-RerF": + self.method_to_use_ = 2 + else: + self.method_to_use_ = 3 + # Check that image_height and image_width are divisors of # the num_features. This is the most we can do to # prevent an invalid value being passed in. From 5efc02a5b9a4e5176dac0c5d1e7e4432a342222d Mon Sep 17 00:00:00 2001 From: j1c Date: Tue, 27 Aug 2019 13:50:19 -0400 Subject: [PATCH 02/16] initial attempt at graph patch --- .../binnedTree/processingNodeBin.h | 70 ++++++++++++++++++- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/packedForest/src/forestTypes/binnedTree/processingNodeBin.h b/packedForest/src/forestTypes/binnedTree/processingNodeBin.h index f0e7a2e9..a65a2534 100644 --- a/packedForest/src/forestTypes/binnedTree/processingNodeBin.h +++ b/packedForest/src/forestTypes/binnedTree/processingNodeBin.h @@ -19,6 +19,7 @@ #include #include #include +#include namespace fp{ @@ -81,7 +82,7 @@ namespace fp{ inline void calcMtryForNode(std::vector& featuresToTry){ featuresToTry.resize(fpSingleton::getSingleton().returnMtry()); int methodToUse = fpSingleton::getSingleton().returnMethodToUse(); - assert(methodToUse == 1 || methodToUse == 2); + assert(methodToUse == 1 || methodToUse == 2 || methodToUse == 3); switch(methodToUse){ case 1:{ @@ -92,6 +93,9 @@ namespace fp{ randMatImagePatch(featuresToTry, paramsRandMatImagePatch()); break; } + case 3:{ + randMatGraphPatch(featuresToTry, paramsRandMatGraphPatch()); + } } } @@ -175,8 +179,70 @@ namespace fp{ } } // END randMatStructured + inline std::vector paramsRandMatGraphPatch() + { + // Preset parameters + const int &imageHeight = fpSingleton::getSingleton().returnImageHeight(); + const int &imageWidth = fpSingleton::getSingleton().returnImageWidth(); + + // Use height as placeholder for number of nodes to sample + const int &patchHeightMax = fpSingleton::getSingleton().returnPatchHeightMax(); + const int &patchHeightMin = fpSingleton::getSingleton().returnPatchHeightMin(); + + // A vector of vectors that specifies the parameters + // for each patch: < , , > + // std::vector> heightWidthTop(3, std::vector(fpSingleton::getSingleton().returnMtry())); + + // A vector for sampling how many nodes to sample + std::vector numNodes(fpSingleton::getSingleton().returnMtry()); + + // The weight is currently hard-coded to 1. + + // Loop over mtry to load random node sizes + for (int k = 0; k < fpSingleton::getSingleton().returnMtry(); k++) + { + numNodes[k] = randNum->gen(patchHeightMax - patchHeightMin + 1) + patchHeightMin; + //sample from [patchHeightMin, patchHeightMax] + // Using the above, 1-node patches are possible ... [J1C] + } - inline void resetLeftNode(){ + return (numNodes); + } // End paramsRandMatGraphPatch + + inline void randMatGraphPatch(std::vector &featuresToTry, std::vector numNodes) + { + assert((int)(patchPositions[0].size()) == fpSingleton::getSingleton().returnMtry()); + + // Preset parameters + const int &imageWidth = fpSingleton::getSingleton().returnImageWidth(); + + for (int k = 0; k < fpSingleton::getSingleton().returnMtry(); k++) + { + // for each element in numNodes + // sample w/o replacement + // add the index to featuresToTry matrix? + // add 1 to the weights + std::vector subsample(imageWidth); + std::iota(std::begin(subsample), std::end(subsample), 0); + + int tempSwap; + + for (int locationToMove = 0; locationToMove < numNodes[k]; locationToMove++) + { + int randomPosition = randNum->gen(imageWidth - locationToMove) + locationToMove; + tempSwap = subsample[locationToMove]; + subsample[locationToMove] = subsample[randomPosition]; + subsample[randomPosition] = tempSwap; + } + + for (int i = 0; i < numNodes[k]; i++) { + // index magic here + featuresToTry[k].returnFeatures().push_back(); + featuresToTry[k].returnWeights().push_back(1); + } + } // END randMatStructured + + inline void resetLeftNode(){ propertiesOfLeftNode.resetClassTotals(); } From 69a81f28f16dce6d010e97ec28793041bc90b08b Mon Sep 17 00:00:00 2001 From: j1c Date: Tue, 27 Aug 2019 14:24:01 -0400 Subject: [PATCH 03/16] Finish Graph indexing --- .../src/forestTypes/binnedTree/processingNodeBin.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packedForest/src/forestTypes/binnedTree/processingNodeBin.h b/packedForest/src/forestTypes/binnedTree/processingNodeBin.h index a65a2534..80d1dc5a 100644 --- a/packedForest/src/forestTypes/binnedTree/processingNodeBin.h +++ b/packedForest/src/forestTypes/binnedTree/processingNodeBin.h @@ -227,6 +227,7 @@ namespace fp{ int tempSwap; + // Sample w/o replacement numNodes number of times for (int locationToMove = 0; locationToMove < numNodes[k]; locationToMove++) { int randomPosition = randNum->gen(imageWidth - locationToMove) + locationToMove; @@ -237,8 +238,11 @@ namespace fp{ for (int i = 0; i < numNodes[k]; i++) { // index magic here - featuresToTry[k].returnFeatures().push_back(); - featuresToTry[k].returnWeights().push_back(1); + for (int j = i + 1; j < numNodes[k]; j++) { + int featureIndex = subsample[i] * imageWidth + subsample[j]; + featuresToTry[k].returnFeatures().push_back(featureIndex); + featuresToTry[k].returnWeights().push_back(1); + } } } // END randMatStructured From 5cb29ee4a3bccc3fd419d11132c00dc3aba726d5 Mon Sep 17 00:00:00 2001 From: Jesse Leigh Patsolic Date: Tue, 27 Aug 2019 15:55:02 -0400 Subject: [PATCH 04/16] fix stuff --- packedForest/Makefile | 2 +- packedForest/src/forestTypes/binnedTree/processingNodeBin.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packedForest/Makefile b/packedForest/Makefile index 18d184f0..d5d89f2b 100644 --- a/packedForest/Makefile +++ b/packedForest/Makefile @@ -1,6 +1,6 @@ #Compiler and Linker #CC := g++-mp-4.7 -CC := g++ -std=c++11 +CC := g++-8 -std=c++11 #CC := g++-6 -std=c++11 #The Target Binary Program diff --git a/packedForest/src/forestTypes/binnedTree/processingNodeBin.h b/packedForest/src/forestTypes/binnedTree/processingNodeBin.h index 80d1dc5a..6ac87732 100644 --- a/packedForest/src/forestTypes/binnedTree/processingNodeBin.h +++ b/packedForest/src/forestTypes/binnedTree/processingNodeBin.h @@ -211,7 +211,7 @@ namespace fp{ inline void randMatGraphPatch(std::vector &featuresToTry, std::vector numNodes) { - assert((int)(patchPositions[0].size()) == fpSingleton::getSingleton().returnMtry()); + assert((int)(numNodes.size()) == fpSingleton::getSingleton().returnMtry()); // Preset parameters const int &imageWidth = fpSingleton::getSingleton().returnImageWidth(); @@ -244,6 +244,7 @@ namespace fp{ featuresToTry[k].returnWeights().push_back(1); } } + } } // END randMatStructured inline void resetLeftNode(){ From c47ad811cad1a7339fdb2fd744b442d7e861a4f1 Mon Sep 17 00:00:00 2001 From: j1c Date: Tue, 27 Aug 2019 16:00:36 -0400 Subject: [PATCH 05/16] Remove -8 --- packedForest/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packedForest/Makefile b/packedForest/Makefile index d5d89f2b..18d184f0 100644 --- a/packedForest/Makefile +++ b/packedForest/Makefile @@ -1,6 +1,6 @@ #Compiler and Linker #CC := g++-mp-4.7 -CC := g++-8 -std=c++11 +CC := g++ -std=c++11 #CC := g++-6 -std=c++11 #The Target Binary Program From e8410304f88490f79ac468a72d844b45ca82419a Mon Sep 17 00:00:00 2001 From: j1c Date: Tue, 27 Aug 2019 16:04:40 -0400 Subject: [PATCH 06/16] Fix elif statement --- Python/rerf/rerfClassifier.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/rerf/rerfClassifier.py b/Python/rerf/rerfClassifier.py index 5a5127fd..c2b7edbf 100644 --- a/Python/rerf/rerfClassifier.py +++ b/Python/rerf/rerfClassifier.py @@ -219,7 +219,7 @@ def fit(self, X, y): else: forestType = "binnedBaseTern" self.method_to_use_ = 1 - elif self.projection_matrix.isin(["S-RerF", "Graph-RerF"]): + elif self.projection_matrix in ["S-RerF", "Graph-RerF"]: if self.oob_score: warn( "OOB is not currently implemented for the S-RerF" From a34b579590d6b46b3e2f3d97098d85f74cd60482 Mon Sep 17 00:00:00 2001 From: j1c Date: Tue, 27 Aug 2019 16:32:03 -0400 Subject: [PATCH 07/16] add case for graph rand mat --- packedForest/src/fpSingleton/fpInfo.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packedForest/src/fpSingleton/fpInfo.h b/packedForest/src/fpSingleton/fpInfo.h index 26ec4ef4..f8c21792 100644 --- a/packedForest/src/fpSingleton/fpInfo.h +++ b/packedForest/src/fpSingleton/fpInfo.h @@ -324,8 +324,8 @@ namespace fp { useRowMajor = (bool)parameterValue; }else if(parameterName == "methodToUse"){ methodToUse = parameterValue; - if(!(methodToUse == 1 || methodToUse == 2)){ - throw std::runtime_error("methodToUse outside allowable parameters {1,2}."); + if(!(methodToUse == 1 || methodToUse == 2 || methodToUse == 3)){ + throw std::runtime_error("methodToUse outside allowable parameters {1,2,3}."); } }else if(parameterName == "imageHeight"){ imageHeight = parameterValue; From 6c9d03cccb318ac2fb5e252fa4b27bc15428c035 Mon Sep 17 00:00:00 2001 From: j1c Date: Fri, 30 Aug 2019 14:24:27 -0400 Subject: [PATCH 08/16] jovo thingy --- .../binnedTree/processingNodeBin.h | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/packedForest/src/forestTypes/binnedTree/processingNodeBin.h b/packedForest/src/forestTypes/binnedTree/processingNodeBin.h index 6ac87732..de980933 100644 --- a/packedForest/src/forestTypes/binnedTree/processingNodeBin.h +++ b/packedForest/src/forestTypes/binnedTree/processingNodeBin.h @@ -247,6 +247,73 @@ namespace fp{ } } // END randMatStructured + inline std::vector> paramsRandMatGraphEdgePatch() + { + // Preset parameters + const int &imageHeight = fpSingleton::getSingleton().returnImageHeight(); + const int &imageWidth = fpSingleton::getSingleton().returnImageWidth(); + + // Use height as placeholder for number of nodes to sample + const int &patchHeightMax = fpSingleton::getSingleton().returnPatchHeightMax(); + const int &patchHeightMin = fpSingleton::getSingleton().returnPatchHeightMin(); + + // A vector of vectors that specifies the parameters + // for each patch: < , , > + // std::vector> heightWidthTop(3, std::vector(fpSingleton::getSingleton().returnMtry())); + + // for each patch: < , > + std::vector> nodeNumEdges(2, std::vector(fpSingleton::getSingleton().returnMtry())); + + // The weight is currently hard-coded to 1. + + // Loop over mtry to load random node sizes + for (int k = 0; k < fpSingleton::getSingleton().returnMtry(); k++) + { + nodeNumEdges[0][k] = randNum->gen(imageHeight) + nodeNumEdges[1][k] = randNum->gen(patchHeightMax - patchHeightMin + 1) + patchHeightMin; + //sample from [patchHeightMin, patchHeightMax] + // Using the above, 1-node patches are possible ... [J1C] + } + + return (nodeNumEdges); + } // End paramsRandMatGraphEdgePatch + + inline void randMatEdgePatch(std::vector &featuresToTry, std::vector> nodeNumEdges) + { + assert((int)(nodeNumEdges.size()) == fpSingleton::getSingleton().returnMtry()); + + // Preset parameters + const int &imageWidth = fpSingleton::getSingleton().returnImageWidth(); + + for (int k = 0; k < fpSingleton::getSingleton().returnMtry(); k++) + { + // for each element in numEdges + // sample w/o replacement from 1..imageWidth + // add the index to featuresToTry matrix? + // add 1 to the weights + std::vector subsample(imageWidth); + std::iota(std::begin(subsample), std::end(subsample), 0); + + int tempSwap; + + // Sample w/o replacement numEdges number of times + for (int locationToMove = 0; locationToMove < nodeNumEdges[1][k]; locationToMove++) + { + int randomPosition = randNum->gen(imageWidth - locationToMove) + locationToMove; + tempSwap = subsample[locationToMove]; + subsample[locationToMove] = subsample[randomPosition]; + subsample[randomPosition] = tempSwap; + } + + for (int i = 0; i < nodeNumEdges[1][k]; i++) + { + int featureIndex = nodeNumEdges[0][k] * imageWidth + subsample[j]; + featuresToTry[k].returnFeatures().push_back(featureIndex); + featuresToTry[k].returnWeights().push_back(1); + } + } + } // END randMatStructured + inline void resetLeftNode(){ propertiesOfLeftNode.resetClassTotals(); } From 9a2d4edbb137e8e5fc9c8fffe3f597bab17d499f Mon Sep 17 00:00:00 2001 From: j1c Date: Fri, 30 Aug 2019 15:08:01 -0400 Subject: [PATCH 09/16] Add edge patch --- .../forestTypes/binnedTree/processingNodeBin.h | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/packedForest/src/forestTypes/binnedTree/processingNodeBin.h b/packedForest/src/forestTypes/binnedTree/processingNodeBin.h index de980933..568e10e9 100644 --- a/packedForest/src/forestTypes/binnedTree/processingNodeBin.h +++ b/packedForest/src/forestTypes/binnedTree/processingNodeBin.h @@ -82,7 +82,7 @@ namespace fp{ inline void calcMtryForNode(std::vector& featuresToTry){ featuresToTry.resize(fpSingleton::getSingleton().returnMtry()); int methodToUse = fpSingleton::getSingleton().returnMethodToUse(); - assert(methodToUse == 1 || methodToUse == 2 || methodToUse == 3); + assert(methodToUse == 1 || methodToUse == 2 || methodToUse == 3 || methodToUse == 4); switch(methodToUse){ case 1:{ @@ -94,7 +94,12 @@ namespace fp{ break; } case 3:{ - randMatGraphPatch(featuresToTry, paramsRandMatGraphPatch()); + randMatGraphNodePatch(featuresToTry, paramsRandMatGraphPatch()); + break; + } + case 4:{ + randMatGraphEdgePatch(featuresToTry, paramsRandMatGraphEdgePatch()); + break; } } } @@ -179,7 +184,7 @@ namespace fp{ } } // END randMatStructured - inline std::vector paramsRandMatGraphPatch() + inline std::vector paramsRandMatGraphNodePatch() { // Preset parameters const int &imageHeight = fpSingleton::getSingleton().returnImageHeight(); @@ -209,7 +214,7 @@ namespace fp{ return (numNodes); } // End paramsRandMatGraphPatch - inline void randMatGraphPatch(std::vector &featuresToTry, std::vector numNodes) + inline void randMatGraphNodePatch(std::vector &featuresToTry, std::vector numNodes) { assert((int)(numNodes.size()) == fpSingleton::getSingleton().returnMtry()); @@ -269,7 +274,7 @@ namespace fp{ // Loop over mtry to load random node sizes for (int k = 0; k < fpSingleton::getSingleton().returnMtry(); k++) { - nodeNumEdges[0][k] = randNum->gen(imageHeight) + nodeNumEdges[0][k] = randNum->gen(imageHeight); nodeNumEdges[1][k] = randNum->gen(patchHeightMax - patchHeightMin + 1) + patchHeightMin; //sample from [patchHeightMin, patchHeightMax] // Using the above, 1-node patches are possible ... [J1C] @@ -278,7 +283,7 @@ namespace fp{ return (nodeNumEdges); } // End paramsRandMatGraphEdgePatch - inline void randMatEdgePatch(std::vector &featuresToTry, std::vector> nodeNumEdges) + inline void randMatGraphEdgePatch(std::vector &featuresToTry, std::vector> nodeNumEdges) { assert((int)(nodeNumEdges.size()) == fpSingleton::getSingleton().returnMtry()); From 4edd799e626dae66b17c9b85bc20d1ca620ec6dd Mon Sep 17 00:00:00 2001 From: j1c Date: Fri, 30 Aug 2019 15:10:12 -0400 Subject: [PATCH 10/16] Bug fix --- packedForest/src/forestTypes/binnedTree/processingNodeBin.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packedForest/src/forestTypes/binnedTree/processingNodeBin.h b/packedForest/src/forestTypes/binnedTree/processingNodeBin.h index 568e10e9..87e48788 100644 --- a/packedForest/src/forestTypes/binnedTree/processingNodeBin.h +++ b/packedForest/src/forestTypes/binnedTree/processingNodeBin.h @@ -94,7 +94,7 @@ namespace fp{ break; } case 3:{ - randMatGraphNodePatch(featuresToTry, paramsRandMatGraphPatch()); + randMatGraphNodePatch(featuresToTry, paramsRandMatGraphNodePatch()); break; } case 4:{ From bf30ec6ab7b8f3f5b84e61050c717bff73a5936d Mon Sep 17 00:00:00 2001 From: j1c Date: Fri, 30 Aug 2019 15:12:34 -0400 Subject: [PATCH 11/16] more bug fix --- packedForest/src/forestTypes/binnedTree/processingNodeBin.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packedForest/src/forestTypes/binnedTree/processingNodeBin.h b/packedForest/src/forestTypes/binnedTree/processingNodeBin.h index 87e48788..3fea250c 100644 --- a/packedForest/src/forestTypes/binnedTree/processingNodeBin.h +++ b/packedForest/src/forestTypes/binnedTree/processingNodeBin.h @@ -312,7 +312,7 @@ namespace fp{ for (int i = 0; i < nodeNumEdges[1][k]; i++) { - int featureIndex = nodeNumEdges[0][k] * imageWidth + subsample[j]; + int featureIndex = nodeNumEdges[0][k] * imageWidth + subsample[i]; featuresToTry[k].returnFeatures().push_back(featureIndex); featuresToTry[k].returnWeights().push_back(1); } From 76a7ee9d1775d9ace6ad0caf12e4ccf1d676fb7d Mon Sep 17 00:00:00 2001 From: j1c Date: Fri, 30 Aug 2019 15:27:10 -0400 Subject: [PATCH 12/16] Update classifier --- Python/rerf/rerfClassifier.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Python/rerf/rerfClassifier.py b/Python/rerf/rerfClassifier.py index c2b7edbf..6f21f686 100644 --- a/Python/rerf/rerfClassifier.py +++ b/Python/rerf/rerfClassifier.py @@ -219,7 +219,7 @@ def fit(self, X, y): else: forestType = "binnedBaseTern" self.method_to_use_ = 1 - elif self.projection_matrix in ["S-RerF", "Graph-RerF"]: + elif self.projection_matrix in ["S-RerF", "Graph-Node-RerF", "Graph-Edge-RerF"]: if self.oob_score: warn( "OOB is not currently implemented for the S-RerF" @@ -232,8 +232,10 @@ def fit(self, X, y): forestType = "binnedBaseTern" # this should change if self.projection_matrix == "S-RerF": self.method_to_use_ = 2 - else: + elif self.projection_matrix == "Graph-Node-RerF": self.method_to_use_ = 3 + else: + self.method_to_use_ = 4 # Check that image_height and image_width are divisors of # the num_features. This is the most we can do to From cb738a849c2b2df5351f71b76fce07c4eb2b43b1 Mon Sep 17 00:00:00 2001 From: j1c Date: Fri, 30 Aug 2019 16:00:17 -0400 Subject: [PATCH 13/16] Elif --- Python/rerf/rerfClassifier.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/rerf/rerfClassifier.py b/Python/rerf/rerfClassifier.py index 6f21f686..87969a7e 100644 --- a/Python/rerf/rerfClassifier.py +++ b/Python/rerf/rerfClassifier.py @@ -234,7 +234,7 @@ def fit(self, X, y): self.method_to_use_ = 2 elif self.projection_matrix == "Graph-Node-RerF": self.method_to_use_ = 3 - else: + elif self.projection_matrix == "Graph-Edge-RerF": self.method_to_use_ = 4 # Check that image_height and image_width are divisors of From fd1c279c80fd8f32fde4da5897c2430997007026 Mon Sep 17 00:00:00 2001 From: j1c Date: Fri, 30 Aug 2019 16:05:54 -0400 Subject: [PATCH 14/16] Fix error --- packedForest/src/fpSingleton/fpInfo.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packedForest/src/fpSingleton/fpInfo.h b/packedForest/src/fpSingleton/fpInfo.h index f8c21792..b8921e72 100644 --- a/packedForest/src/fpSingleton/fpInfo.h +++ b/packedForest/src/fpSingleton/fpInfo.h @@ -324,8 +324,8 @@ namespace fp { useRowMajor = (bool)parameterValue; }else if(parameterName == "methodToUse"){ methodToUse = parameterValue; - if(!(methodToUse == 1 || methodToUse == 2 || methodToUse == 3)){ - throw std::runtime_error("methodToUse outside allowable parameters {1,2,3}."); + if(!(methodToUse == 1 || methodToUse == 2 || methodToUse == 3 || methodToUse == 4)){ + throw std::runtime_error("methodToUse outside allowable parameters {1,2,3,4}."); } }else if(parameterName == "imageHeight"){ imageHeight = parameterValue; From ab882f59cb56401267aa23974ab93a43a7efcc59 Mon Sep 17 00:00:00 2001 From: j1c Date: Thu, 3 Oct 2019 10:47:30 -0400 Subject: [PATCH 15/16] update randmat options to say MORF --- Python/rerf/rerfClassifier.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Python/rerf/rerfClassifier.py b/Python/rerf/rerfClassifier.py index 87969a7e..03fabfc0 100644 --- a/Python/rerf/rerfClassifier.py +++ b/Python/rerf/rerfClassifier.py @@ -44,7 +44,8 @@ class rerfClassifier(BaseEstimator, ClassifierMixin): The random combination of features to use: either "RerF", "Base", or "S-RerF". "RerF" randomly combines features for each `mtry`. Base is our implementation of Random Forest. "S-RerF" is structured RerF, - combining multiple features together in random patches. + combining multiple features together in random patches. "Graph-Node-MORF" + and "Graph-Edge-MORF" is MORF for graph valued data. See Tomita et al. (2016) [#Tomita]_ for further details. n_estimators : int, optional (default: 500) Number of trees in forest. @@ -219,7 +220,7 @@ def fit(self, X, y): else: forestType = "binnedBaseTern" self.method_to_use_ = 1 - elif self.projection_matrix in ["S-RerF", "Graph-Node-RerF", "Graph-Edge-RerF"]: + elif self.projection_matrix in ["S-RerF", "Graph-Node-MORF", "Graph-Edge-MORF"]: if self.oob_score: warn( "OOB is not currently implemented for the S-RerF" @@ -232,9 +233,9 @@ def fit(self, X, y): forestType = "binnedBaseTern" # this should change if self.projection_matrix == "S-RerF": self.method_to_use_ = 2 - elif self.projection_matrix == "Graph-Node-RerF": + elif self.projection_matrix == "Graph-Node-MORF": self.method_to_use_ = 3 - elif self.projection_matrix == "Graph-Edge-RerF": + elif self.projection_matrix == "Graph-Edge-MORF": self.method_to_use_ = 4 # Check that image_height and image_width are divisors of From b5839d10097adeca031ac529a14b6395049005a9 Mon Sep 17 00:00:00 2001 From: j1c Date: Thu, 3 Oct 2019 11:20:25 -0400 Subject: [PATCH 16/16] Fix indentation --- .../binnedTree/processingNodeBin.h | 244 +++++++++--------- 1 file changed, 122 insertions(+), 122 deletions(-) diff --git a/packedForest/src/forestTypes/binnedTree/processingNodeBin.h b/packedForest/src/forestTypes/binnedTree/processingNodeBin.h index 3fea250c..77c56b55 100644 --- a/packedForest/src/forestTypes/binnedTree/processingNodeBin.h +++ b/packedForest/src/forestTypes/binnedTree/processingNodeBin.h @@ -93,14 +93,14 @@ namespace fp{ randMatImagePatch(featuresToTry, paramsRandMatImagePatch()); break; } - case 3:{ - randMatGraphNodePatch(featuresToTry, paramsRandMatGraphNodePatch()); - break; - } - case 4:{ - randMatGraphEdgePatch(featuresToTry, paramsRandMatGraphEdgePatch()); - break; - } + case 3:{ + randMatGraphNodePatch(featuresToTry, paramsRandMatGraphNodePatch()); + break; + } + case 4:{ + randMatGraphEdgePatch(featuresToTry, paramsRandMatGraphEdgePatch()); + break; + } } } @@ -190,7 +190,75 @@ namespace fp{ const int &imageHeight = fpSingleton::getSingleton().returnImageHeight(); const int &imageWidth = fpSingleton::getSingleton().returnImageWidth(); - // Use height as placeholder for number of nodes to sample + // Use height as placeholder for number of nodes to sample + const int &patchHeightMax = fpSingleton::getSingleton().returnPatchHeightMax(); + const int &patchHeightMin = fpSingleton::getSingleton().returnPatchHeightMin(); + + // A vector of vectors that specifies the parameters + // for each patch: < , , > + // std::vector> heightWidthTop(3, std::vector(fpSingleton::getSingleton().returnMtry())); + + // A vector for sampling how many nodes to sample + std::vector numNodes(fpSingleton::getSingleton().returnMtry()); + + // The weight is currently hard-coded to 1. + + // Loop over mtry to load random node sizes + for (int k = 0; k < fpSingleton::getSingleton().returnMtry(); k++) + { + numNodes[k] = randNum->gen(patchHeightMax - patchHeightMin + 1) + patchHeightMin; + //sample from [patchHeightMin, patchHeightMax] + // Using the above, 1-node patches are possible ... [J1C] + } + + return (numNodes); + } // End paramsRandMatGraphPatch + + inline void randMatGraphNodePatch(std::vector &featuresToTry, std::vector numNodes) + { + assert((int)(numNodes.size()) == fpSingleton::getSingleton().returnMtry()); + + // Preset parameters + const int &imageWidth = fpSingleton::getSingleton().returnImageWidth(); + + for (int k = 0; k < fpSingleton::getSingleton().returnMtry(); k++) + { + // for each element in numNodes + // sample w/o replacement + // add the index to featuresToTry matrix? + // add 1 to the weights + std::vector subsample(imageWidth); + std::iota(std::begin(subsample), std::end(subsample), 0); + + int tempSwap; + + // Sample w/o replacement numNodes number of times + for (int locationToMove = 0; locationToMove < numNodes[k]; locationToMove++) + { + int randomPosition = randNum->gen(imageWidth - locationToMove) + locationToMove; + tempSwap = subsample[locationToMove]; + subsample[locationToMove] = subsample[randomPosition]; + subsample[randomPosition] = tempSwap; + } + + for (int i = 0; i < numNodes[k]; i++) { + // index magic here + for (int j = i + 1; j < numNodes[k]; j++) { + int featureIndex = subsample[i] * imageWidth + subsample[j]; + featuresToTry[k].returnFeatures().push_back(featureIndex); + featuresToTry[k].returnWeights().push_back(1); + } + } + } + } // END randMatStructured + + inline std::vector> paramsRandMatGraphEdgePatch() + { + // Preset parameters + const int &imageHeight = fpSingleton::getSingleton().returnImageHeight(); + const int &imageWidth = fpSingleton::getSingleton().returnImageWidth(); + + // Use height as placeholder for number of nodes to sample const int &patchHeightMax = fpSingleton::getSingleton().returnPatchHeightMax(); const int &patchHeightMin = fpSingleton::getSingleton().returnPatchHeightMin(); @@ -198,128 +266,60 @@ namespace fp{ // for each patch: < , , > // std::vector> heightWidthTop(3, std::vector(fpSingleton::getSingleton().returnMtry())); - // A vector for sampling how many nodes to sample - std::vector numNodes(fpSingleton::getSingleton().returnMtry()); + // for each patch: < , > + std::vector> nodeNumEdges(2, std::vector(fpSingleton::getSingleton().returnMtry())); - // The weight is currently hard-coded to 1. + // The weight is currently hard-coded to 1. // Loop over mtry to load random node sizes for (int k = 0; k < fpSingleton::getSingleton().returnMtry(); k++) { - numNodes[k] = randNum->gen(patchHeightMax - patchHeightMin + 1) + patchHeightMin; - //sample from [patchHeightMin, patchHeightMax] + nodeNumEdges[0][k] = randNum->gen(imageHeight); + nodeNumEdges[1][k] = randNum->gen(patchHeightMax - patchHeightMin + 1) + patchHeightMin; + //sample from [patchHeightMin, patchHeightMax] // Using the above, 1-node patches are possible ... [J1C] } - return (numNodes); - } // End paramsRandMatGraphPatch - - inline void randMatGraphNodePatch(std::vector &featuresToTry, std::vector numNodes) - { - assert((int)(numNodes.size()) == fpSingleton::getSingleton().returnMtry()); - - // Preset parameters - const int &imageWidth = fpSingleton::getSingleton().returnImageWidth(); - - for (int k = 0; k < fpSingleton::getSingleton().returnMtry(); k++) - { - // for each element in numNodes - // sample w/o replacement - // add the index to featuresToTry matrix? - // add 1 to the weights - std::vector subsample(imageWidth); - std::iota(std::begin(subsample), std::end(subsample), 0); - - int tempSwap; - - // Sample w/o replacement numNodes number of times - for (int locationToMove = 0; locationToMove < numNodes[k]; locationToMove++) - { - int randomPosition = randNum->gen(imageWidth - locationToMove) + locationToMove; - tempSwap = subsample[locationToMove]; - subsample[locationToMove] = subsample[randomPosition]; - subsample[randomPosition] = tempSwap; - } - - for (int i = 0; i < numNodes[k]; i++) { - // index magic here - for (int j = i + 1; j < numNodes[k]; j++) { - int featureIndex = subsample[i] * imageWidth + subsample[j]; - featuresToTry[k].returnFeatures().push_back(featureIndex); - featuresToTry[k].returnWeights().push_back(1); - } - } + return (nodeNumEdges); + } // End paramsRandMatGraphEdgePatch + + inline void randMatGraphEdgePatch(std::vector &featuresToTry, std::vector> nodeNumEdges) + { + assert((int)(nodeNumEdges.size()) == fpSingleton::getSingleton().returnMtry()); + + // Preset parameters + const int &imageWidth = fpSingleton::getSingleton().returnImageWidth(); + + for (int k = 0; k < fpSingleton::getSingleton().returnMtry(); k++) + { + // for each element in numEdges + // sample w/o replacement from 1..imageWidth + // add the index to featuresToTry matrix? + // add 1 to the weights + std::vector subsample(imageWidth); + std::iota(std::begin(subsample), std::end(subsample), 0); + + int tempSwap; + + // Sample w/o replacement numEdges number of times + for (int locationToMove = 0; locationToMove < nodeNumEdges[1][k]; locationToMove++) + { + int randomPosition = randNum->gen(imageWidth - locationToMove) + locationToMove; + tempSwap = subsample[locationToMove]; + subsample[locationToMove] = subsample[randomPosition]; + subsample[randomPosition] = tempSwap; + } + + for (int i = 0; i < nodeNumEdges[1][k]; i++) + { + int featureIndex = nodeNumEdges[0][k] * imageWidth + subsample[i]; + featuresToTry[k].returnFeatures().push_back(featureIndex); + featuresToTry[k].returnWeights().push_back(1); + } } - } // END randMatStructured - - inline std::vector> paramsRandMatGraphEdgePatch() - { - // Preset parameters - const int &imageHeight = fpSingleton::getSingleton().returnImageHeight(); - const int &imageWidth = fpSingleton::getSingleton().returnImageWidth(); - - // Use height as placeholder for number of nodes to sample - const int &patchHeightMax = fpSingleton::getSingleton().returnPatchHeightMax(); - const int &patchHeightMin = fpSingleton::getSingleton().returnPatchHeightMin(); - - // A vector of vectors that specifies the parameters - // for each patch: < , , > - // std::vector> heightWidthTop(3, std::vector(fpSingleton::getSingleton().returnMtry())); - - // for each patch: < , > - std::vector> nodeNumEdges(2, std::vector(fpSingleton::getSingleton().returnMtry())); - - // The weight is currently hard-coded to 1. - - // Loop over mtry to load random node sizes - for (int k = 0; k < fpSingleton::getSingleton().returnMtry(); k++) - { - nodeNumEdges[0][k] = randNum->gen(imageHeight); - nodeNumEdges[1][k] = randNum->gen(patchHeightMax - patchHeightMin + 1) + patchHeightMin; - //sample from [patchHeightMin, patchHeightMax] - // Using the above, 1-node patches are possible ... [J1C] - } - - return (nodeNumEdges); - } // End paramsRandMatGraphEdgePatch - - inline void randMatGraphEdgePatch(std::vector &featuresToTry, std::vector> nodeNumEdges) - { - assert((int)(nodeNumEdges.size()) == fpSingleton::getSingleton().returnMtry()); - - // Preset parameters - const int &imageWidth = fpSingleton::getSingleton().returnImageWidth(); - - for (int k = 0; k < fpSingleton::getSingleton().returnMtry(); k++) - { - // for each element in numEdges - // sample w/o replacement from 1..imageWidth - // add the index to featuresToTry matrix? - // add 1 to the weights - std::vector subsample(imageWidth); - std::iota(std::begin(subsample), std::end(subsample), 0); - - int tempSwap; - - // Sample w/o replacement numEdges number of times - for (int locationToMove = 0; locationToMove < nodeNumEdges[1][k]; locationToMove++) - { - int randomPosition = randNum->gen(imageWidth - locationToMove) + locationToMove; - tempSwap = subsample[locationToMove]; - subsample[locationToMove] = subsample[randomPosition]; - subsample[randomPosition] = tempSwap; - } - - for (int i = 0; i < nodeNumEdges[1][k]; i++) - { - int featureIndex = nodeNumEdges[0][k] * imageWidth + subsample[i]; - featuresToTry[k].returnFeatures().push_back(featureIndex); - featuresToTry[k].returnWeights().push_back(1); - } - } - } // END randMatStructured - - inline void resetLeftNode(){ + } // END randMatStructured + + inline void resetLeftNode(){ propertiesOfLeftNode.resetClassTotals(); }