OpenWalnut  1.5.0dev
WHtreePartition.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 
50 #ifndef WHTREEPARTITION_H
51 #define WHTREEPARTITION_H
52 
53 // std library
54 #include <vector>
55 #include <list>
56 #include <string>
57 #include <utility>
58 #include <cstdlib>
59 #include <limits>
60 
61 
62 // hClustering
63 #include "WHtree.h"
64 
65 // type of classic partitioning
66 typedef enum
67 {
68  HTP_HOZ, // horizontal cut (distance level)
69  HTP_SIZE, // biggest cluster size limit
70  HTP_HLEVEL // hierarchical level limit
71 }
72 HT_PARTMODE;
73 
74 // type of optimized partitioning
75 typedef enum
76 {
77  HTP2_OPT, // optimized partition spread-separation index
78  HTP2_CSD, // Minimum cluster size difference
79  HTP2_MIAD, // Maximum allowed intra cluster distance
80  HTP2_WIAD, // Maximum allowed weighted intra cluster distance
81  HTP2_MIRD, // Minimum required inter cluster distance
82  HTP2_WIRD // Minimum required weighted inter cluster distance
83 }
84 HT_PARTMODE2;
85 
86 // type of condition to use for partitioning
87 typedef enum
88 {
89  HTC_VALUE, // value (i.e, distance level)
90  HTC_CNUM // number of clusters
91 }
92 HT_CONDITION;
93 
94 
95 /**
96  * this class operates over the class WHtree to extract partitions and analyze partition quality
97  * it may change some non-essential members of the tree class such as saved selected partitions
98  * but the tree structure is never modified.
99  */
101 {
102 public:
103  /**
104  * Constructor
105  * \param tree a pointer to the tree to be partitioned
106  */
107  explicit WHtreePartition( WHtree* const tree );
108 
109  //! Destructor
111 
112  // === PUBLIC MEMBER FUNCTIONS ===
113 
114  /**
115  * Obtain the partition quality values for the optimized (inter vs weighted intra cluster distance) partitions at each granulairty of the tree
116  * \param levelDepth number of levels down where to search for the best branching decision
117  * \param partitionValuesPointer vector where to save the partition quality values
118  * \param patitionVectorPointer vector where to save the partition actual partitions
119  * \param verbose flag to activate debug output
120  */
121  void scanOptimalPartitions( const size_t levelDepth,
122  std::vector< float >* partitionValuesPointer,
123  std::vector< std::vector< size_t> >* patitionVectorPointer,
124  const bool verbose = false );
125 
126  /**
127  * Obtain the partition quality values for the classic partitions at each granulairty of the tree
128  * \param partitionValuesPointer vector where to save the partition quality values
129  * \param patitionVectorPointer vector where to save the partition actual partitions
130  * \param verbose flag to activate debug output
131  */
132  void scanHozPartitions( std::vector< float >* partitionValuesPointer,
133  std::vector< std::vector< size_t> >* patitionVectorPointer,
134  const bool verbose = false );
135 
136  /**
137  * Filter a set of partitions across granularity levels in orther to keep only local maxima within a certain granularity "radius"
138  * \param filterRadius radius (in granularity steps) within to keep the local maxima
139  * \param partValPoint (is a return value) pointer to the partition value vector to be decimated
140  * \param partVectorPoint (is a return value) pointer to the partition vector to be decimated
141  * \return the index of the partition with the absolute maximum of quality vaule
142  */
143  size_t filterMaxPartitions( const unsigned int filterRadius,
144  std::vector< float > *partValPoint,
145  std::vector< std::vector< size_t> > *partVectorPoint );
146 
147  /**
148  * write a set of partitions and their quality values to a text file
149  * \param partFileName file name where to write the partition selection
150  * \param partitionValues partition value vector to be written
151  * \param patitionVector partition vector to be written
152  */
153  void writePartitionSet( std::string partFileName,
154  const std::vector< float > &partitionValues,
155  const std::vector< std::vector< size_t> > &patitionVector );
156 
157 
158  /**
159  * obtain the partition corresponding to the second lowest level of granularity of a tree (using full boolean-integer node ID)
160  * that is, those those clusters correspondign to nodes having only one branching in between them and single leaves
161  * \param partition vector where the return partition is saved
162  */
163  void level2granularity( std::vector<nodeID_t>* partition ) const;
164 
165  /**
166  * obtain the partition corresponding to the second lowest level of granularity of a tree (using only non-leaf node ID)
167  * that is, those those clusters correspondign to nodes having only one branching in between them and single leaves
168  * \param partition vector where the return partition is saved
169  */
170  void level2granularity( std::vector<size_t>* partition ) const;
171 
172  /**
173  * Gets a classic partition for the tree
174  * (those where clusters are further subdivided until a condition is met, based on number of clusters, level or biggest cluster size)
175  * \param compValue comparison value to select partition
176  * \param partition (is a return value) vector where to save the partition
177  * \param mode tipe of data on which to base the partition (distance, size h. level)
178  * \param condition condition on which to build the partition (condition value or number of clusters)
179  * \param excludeLeaves if set base nodes will not be further partitioned
180  * \param root root of the subtree where to find the aprtition
181  * \return value at wich partition was found
182  */
183  float partitionClassic( float compValue, std::vector<nodeID_t>* const partition, const HT_PARTMODE mode,
184  const HT_CONDITION condition, const bool excludeLeaves, const size_t root ) const;
185 
186  /**
187  * Gets an optimized partition for the tree
188  * (those where a partition characteristic must be cosnidered for several partitions and levels at a time to decide which subset of the tree to continue navigating,
189  * until a condition is met)
190  * \param compValue comparison value to select partition
191  * \param partition (is a return value) vector where to save the partition
192  * \param mode tipe of data on which to base the partition ( spread-separation index, minimum size difference,
193  * maximum allowed intra cluster distance, maximum allowed weighted intra cluster distance, minimum required inter cluster distance,
194  * minimum required weighted inter cluster distance)
195  * \param condition condition on which to build the partition (condition value or number of clusters)
196  * \param excludeLeaves if set base nodes will not be further partitioned
197  * \param root root of the subtree where to find the aprtition
198  * \param levelDepth number of levels down where to search for the best branching decision
199  * \return value at wich partition was found
200  */
201  float partitionOptimized( float compValue, std::vector<nodeID_t>* const partition, const HT_PARTMODE2 mode,
202  const HT_CONDITION condition, const bool excludeLeaves, const size_t root, const size_t levelDepth ) const;
203 
204  /**
205  * Finds clusters with sharp boundaries (long branches) the tree
206  * \param compValue comparison value to select partition
207  * \param partition (is a return value) vector where to save the partition
208  * \param excludeLeaves if set base nodes will not be further partitioned
209  * \param root root of the subtree where to find the aprtition
210  * \param normalized flag to indicate whether the absolute length of the brancghwes should be considered of if they should be normalized by distance level in the tree
211  * \return value at wich partition was found
212  */
213  float partitionSharp( const float compValue,
214  std::vector<nodeID_t>* const partition,
215  const bool excludeLeaves,
216  const size_t root,
217  const bool normalized ) const;
218 
219  /**
220  * Finds regions without sharp inner boundaries the tree
221  * \param compValue comparison value to select partition
222  * \param partition (is a return value) vector where to save the partition
223  * \param excludeLeaves if set base nodes will not be further partitioned
224  * \param root root of the subtree where to find the aprtition
225  * \return value at wich partition was found
226  */
227  float partitionSmooth( const float compValue, std::vector<nodeID_t>* const partition, const bool excludeLeaves, const size_t root ) const;
228 
229 
230 private:
231  // === PRIVATE MEMBER DATA ===
232 
233  //! tree object
235 
236  // === PRIVATE MEMBER FUNCTIONS ===
237 
238  /**
239  * Evaluate partition cluster size difference (using only non-leaf node ID)
240  * \param partition vector with the partition to be evaluated
241  * \return cluster size difference value of the partition
242  */
243  std::pair< float, float > evalPartClustSizeDiff( const std::vector<size_t> &partition ) const;
244 
245  /**
246  * Evaluate partition cluster size difference (using full boolean-integer node ID)
247  * \param partition vector with the partition to be evaluated
248  * \return cluster size difference value of the partition
249  */
250  std::pair< float, float > evalPartClustSizeDiff( const std::vector<nodeID_t> &partition ) const;
251 
252  /**
253  * Evaluate partition quality: spread-separation index (using only non-leaf node ID)
254  * \param partition vector with the partition to be evaluated
255  * \return quality value of the partition
256  */
257  float evalPartOptimal( const std::vector<size_t> &partition ) const;
258 
259  /**
260  * Evaluate partition quality: spread-separation index (using full boolean-integer node ID)
261  * \param partition vector with the partition to be evaluated
262  * \return quality value of the partition
263  */
264  float evalPartOptimal( const std::vector<nodeID_t> &partition ) const;
265 
266  /**
267  * Evaluate spread-separation index for selected partition (using only non-leaf node ID)
268  * \param partition vector with the partition to be evaluated
269  * \return spread-separation index value of the partition
270  */
271  float evalSSindex( const std::vector<size_t> &partition ) const;
272 
273  /**
274  * Evaluate spread-separation index for selected partition (using full boolean-integer node ID)
275  * \param partition vector with the partition to be evaluated
276  * \return spread-separation index value of the partition
277  */
278  float evalSSindex( const std::vector<nodeID_t> &partition ) const;
279 
280  /**
281  * Evaluate intra cluster distance factor for selected partition (using only non-leaf node ID)
282  * \param partition vector with the partition to be evaluated
283  * \return intra cluster distance factor value of the partition
284  */
285  float evalPartIntraDist( const std::vector<size_t> &partition ) const;
286 
287  /**
288  * Evaluate intra cluster distance factor for selected partition (using full boolean-integer node ID)
289  * \param partition vector with the partition to be evaluated
290  * \return intra cluster distance factor value of the partition
291  */
292  float evalPartIntraDist( const std::vector<nodeID_t> &partition ) const;
293 
294  /**
295  * Evaluate weighted intra cluster distance factor for selected partition (using only non-leaf node ID)
296  * \param partition vector with the partition to be evaluated
297  * \return weighted intra cluster distance factor value of the partition
298  */
299  float evalPartIntraDistWeighted( const std::vector<size_t> &partition ) const;
300 
301  /**
302  * Evaluate weighted intra cluster distance factor for selected partition (using full boolean-integer node ID)
303  * \param partition vector with the partition to be evaluated
304  * \return weighted intra cluster distance factor value of the partition
305  */
306  float evalPartIntraDistWeighted( const std::vector<nodeID_t> &partition ) const;
307 
308  /**
309  * Evaluate approximated inter cluster distance factor for selected partition (using only non-leaf node ID)
310  * \param partition vector with the partition to be evaluated
311  * \return approximated inter cluster distance factor value of the partition
312  */
313  float evalPartBranchDist( const std::vector<size_t> &partition ) const;
314 
315  /**
316  * Evaluate approximated inter cluster distance factor for selected partition (using full boolean-integer node ID)
317  * \param partition vector with the partition to be evaluated
318  * \return approximated inter cluster distance factor value of the partition
319  */
320  float evalPartBranchDist( const std::vector<nodeID_t> &partition ) const;
321 
322  /**
323  * Evaluate approximated weighted inter cluster distance factor for selected partition (using only non-leaf node ID)
324  * \param partition vector with the partition to be evaluated
325  * \return approximated weighted inter cluster distance value of the partition
326  */
327  float evalPartBranchDistWeighted( const std::vector<size_t> &partition ) const;
328 
329  /**
330  * Evaluate approximated weighted inter cluster distance factor for selected partition (using full boolean-integer node ID)
331  * \param partition vector with the partition to be evaluated
332  * \return approximated weighted inter cluster distance value of the partition
333  */
334  float evalPartBranchDistWeighted( const std::vector<nodeID_t> &partition ) const;
335 
336  // DEPRECATED MEMEBER FUNCTIONS
337 
338  /**
339  * Obtain pairwise intercluster distance matrix for the selected partition, or derive new icd matrix from an old one following the subdivision of one of the clusters (DEPRECATED)
340  * \param oldPartition vector with the partition to be evaluated or the previously evaluated partition
341  * \param branchPos index of the cluster that was further subdivided if considerign a previously calculated matrix, 0 if no previous calculation exists (default)
342  * \param branch vector with sub-clusters to replace original cluster after subdivision, empty if no previous calculation exists (default)
343  * \param oldMatrix icd matrix from previous calculation, empty if no previous calculation exists (default)
344  * \return pairwise inter cluster distance matrix for the selected (or derived) parititon
345  */
346  std::vector< std::vector< dist_t > > getICDmatrix( const std::vector<size_t> &oldPartition,
347  size_t branchPos = 0,
348  std::vector<size_t> branch = std::vector<size_t>(),
349  std::vector< std::vector< dist_t > > oldMatrix = std::vector< std::vector< dist_t > >() );
350 
351  /**
352  * Evaluate full inter cluster distance factor for selected partition (DEPRECATED)
353  * \param partition vector with the partition to be evaluated
354  * \param icdMatrix pairwise inter-cluster distance matrix for the selected partition
355  * \return full inter cluster distance factor value of the partition
356  */
357  float evalPartInterDist( const std::vector<size_t> &partition, const std::vector< std::vector < dist_t > > &icdMatrix ) const;
358 
359  /**
360  * Evaluate full weighted inter cluster distance factor for selected partition (DEPRECATED)
361  * \param partition vector with the partition to be evaluated
362  * \param icdMatrix pairwise inter-cluster distance matrix for the selected partition
363  * \return full weighted inter cluster distance factor value of the partition
364  */
365  float evalPartInterDistWeighted( const std::vector<size_t> &partition, const std::vector< std::vector < dist_t > > &icdMatrix ) const;
366 };
367 
368 #endif // WHTREEPARTITION_H
this class operates over the class WHtree to extract partitions and analyze partition quality it may ...
std::pair< float, float > evalPartClustSizeDiff(const std::vector< size_t > &partition) const
Evaluate partition cluster size difference (using only non-leaf node ID)
void writePartitionSet(std::string partFileName, const std::vector< float > &partitionValues, const std::vector< std::vector< size_t > > &patitionVector)
write a set of partitions and their quality values to a text file
WHtreePartition(WHtree *const tree)
Constructor.
float evalSSindex(const std::vector< size_t > &partition) const
Evaluate spread-separation index for selected partition (using only non-leaf node ID)
float evalPartInterDist(const std::vector< size_t > &partition, const std::vector< std::vector< dist_t > > &icdMatrix) const
Evaluate full inter cluster distance factor for selected partition (DEPRECATED)
void scanOptimalPartitions(const size_t levelDepth, std::vector< float > *partitionValuesPointer, std::vector< std::vector< size_t > > *patitionVectorPointer, const bool verbose=false)
Obtain the partition quality values for the optimized (inter vs weighted intra cluster distance) part...
size_t filterMaxPartitions(const unsigned int filterRadius, std::vector< float > *partValPoint, std::vector< std::vector< size_t > > *partVectorPoint)
Filter a set of partitions across granularity levels in orther to keep only local maxima within a cer...
float evalPartOptimal(const std::vector< size_t > &partition) const
Evaluate partition quality: spread-separation index (using only non-leaf node ID)
float evalPartIntraDist(const std::vector< size_t > &partition) const
Evaluate intra cluster distance factor for selected partition (using only non-leaf node ID)
float evalPartIntraDistWeighted(const std::vector< size_t > &partition) const
Evaluate weighted intra cluster distance factor for selected partition (using only non-leaf node ID)
std::vector< std::vector< dist_t > > getICDmatrix(const std::vector< size_t > &oldPartition, size_t branchPos=0, std::vector< size_t > branch=std::vector< size_t >(), std::vector< std::vector< dist_t > > oldMatrix=std::vector< std::vector< dist_t > >())
Obtain pairwise intercluster distance matrix for the selected partition, or derive new icd matrix fro...
float evalPartInterDistWeighted(const std::vector< size_t > &partition, const std::vector< std::vector< dist_t > > &icdMatrix) const
Evaluate full weighted inter cluster distance factor for selected partition (DEPRECATED)
float partitionClassic(float compValue, std::vector< nodeID_t > *const partition, const HT_PARTMODE mode, const HT_CONDITION condition, const bool excludeLeaves, const size_t root) const
Gets a classic partition for the tree (those where clusters are further subdivided until a condition ...
void level2granularity(std::vector< nodeID_t > *partition) const
obtain the partition corresponding to the second lowest level of granularity of a tree (using full bo...
float evalPartBranchDistWeighted(const std::vector< size_t > &partition) const
Evaluate approximated weighted inter cluster distance factor for selected partition (using only non-l...
float partitionSharp(const float compValue, std::vector< nodeID_t > *const partition, const bool excludeLeaves, const size_t root, const bool normalized) const
Finds clusters with sharp boundaries (long branches) the tree.
float evalPartBranchDist(const std::vector< size_t > &partition) const
Evaluate approximated inter cluster distance factor for selected partition (using only non-leaf node ...
WHtree & m_tree
tree object
float partitionOptimized(float compValue, std::vector< nodeID_t > *const partition, const HT_PARTMODE2 mode, const HT_CONDITION condition, const bool excludeLeaves, const size_t root, const size_t levelDepth) const
Gets an optimized partition for the tree (those where a partition characteristic must be cosnidered f...
float partitionSmooth(const float compValue, std::vector< nodeID_t > *const partition, const bool excludeLeaves, const size_t root) const
Finds regions without sharp inner boundaries the tree.
~WHtreePartition()
Destructor.
void scanHozPartitions(std::vector< float > *partitionValuesPointer, std::vector< std::vector< size_t > > *patitionVectorPointer, const bool verbose=false)
Obtain the partition quality values for the classic partitions at each granulairty of the tree.
this class implements a hierarchical tree and provides functions for creation, partitioning,...
Definition: WHtree.h:81
implements a compare operator for clusters, depending on custom value of the cluster