OpenWalnut  1.5.0dev
WHtreeProcesser.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 //---------------------------------------------------------------------------
26 //
27 // Project: hClustering
28 //
29 // Whole-Brain Connectivity-Based Hierarchical Parcellation Project
30 // David Moreno-Dominguez
31 // d.mor.dom@gmail.com
32 // moreno@cbs.mpg.de
33 // www.cbs.mpg.de/~moreno//
34 // This file is also part of OpenWalnut ( http://www.openwalnut.org ).
35 //
36 // hClustering is free software: you can redistribute it and/or modify
37 // it under the terms of the GNU Lesser General Public License as published by
38 // the Free Software Foundation, either version 3 of the License, or
39 // (at your option) any later version.
40 // http://creativecommons.org/licenses/by-nc/3.0
41 //
42 // hClustering is distributed in the hope that it will be useful,
43 // but WITHOUT ANY WARRANTY; without even the implied warranty of
44 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 // GNU Lesser General Public License for more details.
46 //
47 //---------------------------------------------------------------------------
48 
49 #ifndef WHTREEPROCESSER_H
50 #define WHTREEPROCESSER_H
51 
52 // std library
53 #include <vector>
54 #include <list>
55 #include <string>
56 #include <utility>
57 #include <cstdlib>
58 
59 // hClustering
60 #include "WHtree.h"
61 
62 // type of tree pruning
63 typedef enum
64 {
65  HTPR_SIZERATIO, // based on size ratio of joining nodes
66  HTPR_JOINSIZE, // based on absolute size values of joinign nodes
67  HTPR_JOINLEVEL // based on joining distance level
68 } HTPROC_MODE;
69 
70 // type of of node collapsing
71 typedef enum
72 {
73  HTPR_C_CONSTANT, // collapse all node pairs with distances lower than a constant distance difference
74  HTPR_C_LINEAR, // normalize node distance difference linearly depending on their distance level
75  HTPR_C_SQ // normalize node distance difference squarely depending on their distance level
76 } HTPROC_COLLAPSE;
77 
78 /**
79  * this class implements methods for dendrogram processing, such as pruning, node collapsing and decimation
80  * it operates over the class WHtree.
81  * As a result of its operations the tree structure is changed and previous structure cannot be recovered unless a class copy is previously made.
82  */
84 {
85 public:
86  /**
87  * Constructor
88  * \param tree a pointer to the tree to be processed
89  */
90  explicit WHtreeProcesser( WHtree* const tree );
91 
92  //! Destructor
94 
95  // === PUBLIC MEMBER FUNCTIONS ===
96 
97  /**
98  * Prunes the tree according to the options specified
99  * \param condition value of the condition to choose when pruning (size of joining cluster, size ratio or joining level)
100  * \param safeSize maximum size that a cluster can have in order to be pruned, bigger clusters wont be prune regardless of the condition
101  * \param pruneType type of pruning method (max size, maxlevel, sizeratio)
102  * \return pair with the number of nodes and leaves eliminated
103  */
104  std::pair< size_t, size_t > pruneTree( float condition, size_t safeSize, const HTPROC_MODE pruneType );
105 
106  /**
107  * Prunes the tree from randomly chosen leaves
108  * \param numberPruned number of leaves to be pruned
109  * \param seed seed for the random number generator to choose the leaves to be pruned
110  * \return pair with the number of nodes and leaves eliminated
111  */
112  std::pair< size_t, size_t > pruneRandom( const size_t numberPruned, unsigned int seed = 0 );
113 
114  /**
115  * Collapses the nodes of the tree that have branch lenghts equal or lower to the one indicated
116  * \param flatGap branch lenght limit to be collapsed
117  * \param distLevelLimit distance level under which no collapsing will take place
118  * \param keepBaseNodes flag to avoid collapsing base nodes (those that have only single-leaf children)
119  * \return number of nodes eliminated
120  */
121  size_t collapseTree( const dist_t flatGap, const dist_t distLevelLimit, const bool keepBaseNodes );
122 
123  /**
124  * Collapses the nodes of the tree that have branch lenghts smoller than the distance level multiplied by the coefficient indicated
125  * \param coefficient coefficient for collapsing depending on distance level
126  * \param keepBaseNodes flag to avoid collapsing base nodes (those that have only single-leaf children)
127  * \return number of nodes eliminated
128  */
129  size_t collapseTreeLinear( const dist_t coefficient, const bool keepBaseNodes );
130 
131  /**
132  * Collapses the nodes of the tree that have branch lenghts smoller than the squared distance level multiplied by the coefficient indicated
133  * \param coefficient coefficient for collapsing depending on distance level
134  * \param keepBaseNodes flag to avoid collapsing base nodes (those that have only single-leaf children)
135  * \return number of nodes eliminated
136  */
137  size_t collapseTreeSquare( const dist_t coefficient, const bool keepBaseNodes );
138 
139  /**
140  * Collapses the nodes of the subtree that have branch lenghts equal or lower to the one indicated
141  * \param flatGap branch lenght limit to be collapsed
142  * \param distLevelLimit distance level under which no collapsing will take place
143  * \param root root node of the subtree
144  * \param keepBaseNodes flag to avoid collapsing base nodes (those that have only single-leaf children)
145  * \return number of nodes eliminated
146  */
147  size_t collapseBranch( const dist_t flatGap, const dist_t distLevelLimit, size_t root, const bool keepBaseNodes );
148 
149  /**
150  * Collapses all the nodes of the subtrees indicated (using only non-leaf node ID)
151  * \param selection list with the root nodes of the subtree
152  * \param keepBaseNodes flag to avoid collapsing base nodes (those that have only single-leaf children)
153  * \return number of nodes eliminated
154  */
155  size_t flattenSelection( const std::list< size_t > selection, const bool keepBaseNodes );
156 
157  /**
158  * Collapses all the nodes of the subtrees indicated (using only non-leaf node ID)
159  * \param selection vector with the root nodes of the subtree
160  * \param keepBaseNodes flag to avoid collapsing base nodes (those that have only single-leaf children)
161  * \return number of nodes eliminated
162  */
163  size_t flattenSelection( const std::vector< size_t > &selection, const bool keepBaseNodes );
164 
165  /**
166  * Collapses all the nodes of the subtrees indicated (using full boolean-integer node ID)
167  * \param selection vector with the root nodes of the subtree
168  * \param keepBaseNodes flag to avoid collapsing base nodes (those that have only single-leaf children)
169  * \return number of nodes eliminated
170  */
171  size_t flattenSelection( const std::vector< nodeID_t > &selection, const bool keepBaseNodes );
172 
173  /**
174  * Prunes all of the subtrees indicated (using only non-leaf node ID)
175  * \param selection vector with the nodes to be pruned
176  * \return pair with the number of nodes and leaves eliminated
177  */
178  std::pair< size_t, size_t > pruneSelection( const std::vector< size_t > &selection );
179 
180  /**
181  * Prunes all of the subtrees indicated (using full boolean-integer node ID)
182  * \param selection vector with the nodes to be pruned
183  * \return pair with the number of nodes and leaves eliminated
184  */
185  std::pair< size_t, size_t > pruneSelection( const std::vector< nodeID_t > &selection );
186 
187  /**
188  * set flags for pruning all of the subtrees indicated (using only non-leaf node ID)
189  * \param selection vector with the nodes to be pruned
190  */
191  void flagSelection( const std::vector< size_t > &selection );
192 
193  /**
194  * set flags for pruning all of the subtrees indicated (using full boolean-integer node ID)
195  * \param selection vector with the nodes to be pruned
196  */
197  void flagSelection( const std::vector< nodeID_t > &selection );
198 
199  /**
200  * set flags for pruning all of the leaves indicated
201  * \param selection vector with the leaves to be pruned
202  */
203  void flagLeaves( const std::vector< size_t > &selection );
204 
205  /**
206  * Reduces the tree grid size by the ratio specified (as if it had been obtained from an image of lower resolution)
207  * \param coarseRatio coarsing ratio
208  */
209  void coarseTree( const unsigned int coarseRatio );
210 
211  /**
212  * converts the base nodes into leaves, pruning out all excess leaves and information
213  * \return new number of leaves of the tree
214  */
215  size_t baseNodes2Leaves();
216 
217  /**
218  * calls on private WHtree member to eliminate non monotonic steps in the tree while keeping as much information as possible.
219  * it searches bottom-up for non-monotonic steps. When detected a corrected value for the parent node is computed
220  * this value consists of a average between the non-monotonic children levels weighted by their sizes in number of leaves and the parent level weighted by the sizes of the remaining monotonic children
221  * in orther to avoid infinite loops an error tolerance is included. default value is 1*E-5
222  * \param errorMult multiplier to increase the base error tolerance (default value is 1 makign the tolerance 1*E-5, maximum value is 100 making the tolerance 1*E-3)
223  */
224  void forceMonotonicity( double errorMult = 1 );
225 
226  /**
227  * calls on private WHtree member to eliminate non monotonic steps in the tree bottom-up, lowering the level of children involved in the non-monotonic steps to the parent level
228  */
229  void forceMonotonicityUp();
230 
231  /**
232  * calls on private WHtree member to eliminate non monotonic steps in the tree top-down, raising the level of parents involved in the non-monotonic steps to the children level
233  */
234  void forceMonotonicityDown();
235 
236  /**
237  * calls on private WHtree member to merge nodes joining binary at the same level into non-binary structures
238  * \param keepBaseNodes if set base nodes ( nodes with only single leaves as children) will not be merged
239  * \return number of nodes collapsed
240  */
241  size_t debinarize( const bool keepBaseNodes );
242 
243  // /**
244  // * performs the tree smoothin
245  // * \param safeSize minimum size of the clusters to be pruned in the first stage
246  // * \param smoothSize size of the clusters looked for
247  // * \param smoothGap maximum gap to be smoothed
248  // * \return pair with the number of nodes and leaves eliminated
249  // */
250  // std::pair< size_t, size_t > smoothTree( const size_t safeSize, const size_t smoothSize, const dist_t smoothGap );
251 
252 private:
253  // === PRIVATE MEMBER DATA ===
254 
255  WHtree &m_tree; //!< tree object
256 
257  // === PRIVATE MEMBER FUNCTIONS ===
258 
259  /**
260  * Collapses all the nodes of the subtree
261  * \param root root node of the subtree
262  * \param keepBaseNodes if set base nodes ( nodes with only single leaves as children) will not be merged
263  * \return number of nodes eliminated
264  */
265  size_t flattenBranch( size_t root, const bool keepBaseNodes );
266 
267  /**
268  * eliminates branchings in the tree, raising them to the parent level
269  * \param thisNodeID id of the sub-branch node where to start the flatteing
270  * \param coefficient coefficient for collapsing depending on distance level
271  * \param collapseMode collapsing mode
272  */
273  void collapseNode( const size_t thisNodeID, const dist_t coefficient, HTPROC_COLLAPSE collapseMode = HTPR_C_CONSTANT );
274 
275  /**
276  * eliminates non monotonic steps in the tree, raising them to the parent level
277  * \param thisNodeID id of the sub-branch node where to start the flatteing
278  * \param coefficient coefficient for collapsing depending on distance level
279  * \param collapseMode collapsing mode
280  */
281  void collapseNode( const nodeID_t &thisNodeID, const dist_t coefficient, HTPROC_COLLAPSE collapseMode = HTPR_C_CONSTANT );
282 };
283 
284 #endif // WHTREEPROCESSER_H
this class implements methods for dendrogram processing, such as pruning, node collapsing and decimat...
std::pair< size_t, size_t > pruneRandom(const size_t numberPruned, unsigned int seed=0)
Prunes the tree from randomly chosen leaves.
WHtree & m_tree
tree object
void forceMonotonicityDown()
calls on private WHtree member to eliminate non monotonic steps in the tree top-down,...
size_t debinarize(const bool keepBaseNodes)
calls on private WHtree member to merge nodes joining binary at the same level into non-binary struct...
void flagSelection(const std::vector< size_t > &selection)
set flags for pruning all of the subtrees indicated (using only non-leaf node ID)
size_t collapseTreeLinear(const dist_t coefficient, const bool keepBaseNodes)
Collapses the nodes of the tree that have branch lenghts smoller than the distance level multiplied b...
size_t collapseTreeSquare(const dist_t coefficient, const bool keepBaseNodes)
Collapses the nodes of the tree that have branch lenghts smoller than the squared distance level mult...
size_t baseNodes2Leaves()
converts the base nodes into leaves, pruning out all excess leaves and information
size_t flattenSelection(const std::list< size_t > selection, const bool keepBaseNodes)
Collapses all the nodes of the subtrees indicated (using only non-leaf node ID)
size_t collapseBranch(const dist_t flatGap, const dist_t distLevelLimit, size_t root, const bool keepBaseNodes)
Collapses the nodes of the subtree that have branch lenghts equal or lower to the one indicated.
~WHtreeProcesser()
Destructor.
void flagLeaves(const std::vector< size_t > &selection)
set flags for pruning all of the leaves indicated
size_t collapseTree(const dist_t flatGap, const dist_t distLevelLimit, const bool keepBaseNodes)
Collapses the nodes of the tree that have branch lenghts equal or lower to the one indicated.
void forceMonotonicityUp()
calls on private WHtree member to eliminate non monotonic steps in the tree bottom-up,...
void coarseTree(const unsigned int coarseRatio)
Reduces the tree grid size by the ratio specified (as if it had been obtained from an image of lower ...
WHtreeProcesser(WHtree *const tree)
Constructor.
size_t flattenBranch(size_t root, const bool keepBaseNodes)
Collapses all the nodes of the subtree.
void collapseNode(const size_t thisNodeID, const dist_t coefficient, HTPROC_COLLAPSE collapseMode=HTPR_C_CONSTANT)
eliminates branchings in the tree, raising them to the parent level
std::pair< size_t, size_t > pruneSelection(const std::vector< size_t > &selection)
Prunes all of the subtrees indicated (using only non-leaf node ID)
std::pair< size_t, size_t > pruneTree(float condition, size_t safeSize, const HTPROC_MODE pruneType)
Prunes the tree according to the options specified.
void forceMonotonicity(double errorMult=1)
calls on private WHtree member to eliminate non monotonic steps in the tree while keeping as much inf...
this class implements a hierarchical tree and provides functions for creation, partitioning,...
Definition: WHtree.h:81