OpenWalnut  1.5.0dev
WHtree.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 // For more reference on the underlying algorithm and research they have been used for refer to:
37 // - Moreno-Dominguez, D., Anwander, A., & Knoesche, T. R. (2014).
38 // A hierarchical method for whole-brain connectivity-based parcellation.
39 // Human Brain Mapping, 35(10), 5000-5025. doi: http://dx.doi.org/10.1002/hbm.22528
40 // - Moreno-Dominguez, D. (2014).
41 // Whole-brain cortical parcellation: A hierarchical method based on dMRI tractography.
42 // PhD Thesis, Max Planck Institute for Human Cognitive and Brain Sciences, Leipzig.
43 // ISBN 978-3-941504-45-5
44 //
45 // hClustering is free software: you can redistribute it and/or modify
46 // it under the terms of the GNU Lesser General Public License as published by
47 // the Free Software Foundation, either version 3 of the License, or
48 // (at your option) any later version.
49 // http://creativecommons.org/licenses/by-nc/3.0
50 //
51 // hClustering is distributed in the hope that it will be useful,
52 // but WITHOUT ANY WARRANTY; without even the implied warranty of
53 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
54 // GNU Lesser General Public License for more details.
55 //
56 //---------------------------------------------------------------------------
57 
58 
59 #ifndef WHTREE_H
60 #define WHTREE_H
61 
62 // std library
63 #include <utility>
64 #include <vector>
65 #include <list>
66 #include <string>
67 
68 
69 #include <boost/filesystem.hpp>
70 
71 // hClustering
72 #include "WHcoord.h"
73 #include "WHnode.h"
74 #include "WFileParser.h"
75 
76 
77 /**
78  * this class implements a hierarchical tree and provides functions for creation, partitioning, selection and navigation
79  */
80 class WHtree
81 {
82 public:
83  /**
84  * Constructor
85  */
86  explicit WHtree();
87 
88  /**
89  * Constructor
90  * \param filename string containing the name of the file with the tree data
91  */
92  explicit WHtree( std::string filename );
93 
94  /**
95  * Constructor
96  * \param treeName name of the tree
97  * \param datasetGridInit type of grid coordinate space
98  * \param datasetSizeInit size of the brain dataset
99  * \param numStreamlinesInit number of streamlines per voxel on the tractograms that were processed to build this tree
100  * \param logFactorInit logarithmic normalization factor that was applied on the tracts when building the tree
101  * \param leavesInit vector of leaves
102  * \param nodesInit vector of nodes
103  * \param trackidInit vector of seed track ids
104  * \param coordInit vector of seed voxel coordinates
105  * \param discardInit vector of discarded seed voxel coordinates
106  * \param cpccInit cpcc value of the tree
107  */
108  explicit WHtree( std::string treeName, HC_GRID datasetGridInit, WHcoord datasetSizeInit, size_t numStreamlinesInit, float logFactorInit,
109  std::vector<WHnode> leavesInit, std::vector<WHnode> nodesInit, std::vector<size_t> trackidInit, std::vector<WHcoord> coordInit,
110  std::list<WHcoord> discardInit, float cpccInit = 0 );
111 
112  /**
113  * Constructor
114  * \param object tree object
115  */
116  explicit WHtree( const WHtree &object );
117 
118  /**
119  * Destructor
120  */
121  ~WHtree();
122 
123 
124 
125  // === PUBLIC MEMBER FUNCTIONS ===
126 
127 
128 
129  /**
130  * Returns the tree name
131  * \return value containing the tree name
132  */
133  std::string getName() const;
134 
135  /**
136  * Returns the loading status of the tree
137  * \return load indicator
138  */
139  bool isLoaded() const;
140 
141  /**
142  * Returns the number of leaves of the tree
143  * \return number of leaves
144  */
145  size_t getNumLeaves() const;
146 
147  /**
148  * Returns the number of nodes of the tree
149  * \return number of leaves
150  */
151  size_t getNumNodes() const;
152 
153  /**
154  * Returns the leaves of the tree
155  * \return leaves vector
156  */
157  std::vector<WHnode> getLeaves() const;
158 
159  /**
160  * Returns the nodes of the tree
161  * \return nodes vector
162  */
163  std::vector<WHnode> getNodes() const;
164 
165  /**
166  * Returns the number of leaves of the tree
167  * \return number of leaves
168  */
169  size_t getNumDiscarded() const;
170 
171  /**
172  * Returns the dataset size
173  * \return coordinate containing the dataset dimensions
174  */
175  WHcoord getDataSize() const;
176 
177  /**
178  * Returns the dataset grid type
179  * \return enum containing the grid type
180  */
181  HC_GRID getDataGrid() const;
182 
183  /**
184  * Returns the cpcc value of the tree
185  * \return value containing the cpcc
186  */
187  float getCpcc() const;
188 
189  /**
190  * Returns the roi coordinate vector
191  * \return vector containing the roi coordinates
192  */
193  std::vector<WHcoord> getRoi() const;
194 
195  /**
196  * Returns the tractogram IDs vector
197  * \return vector containing the tractogram ids per leaf
198  */
199  std::vector<size_t> getTrackids() const;
200 
201  /**
202  * Returns the discarded list
203  * \return list containing the discarded coordinates
204  */
205  std::list<WHcoord> getDiscarded() const;
206 
207  /**
208  * Returns the cutting values for the selected partitions
209  * \return vector containing the values for the selected partitions
210  */
211  std::vector<float> getSelectedValues() const;
212 
213  /**
214  * Returns the cutting values for the selected partition
215  * \param index index of the desired partition
216  * \return cutting values for the selected partition
217  */
218  float getSelectedValues( size_t index ) const;
219 
220  /**
221  * Returns the set of the selected partitions
222  * \return vector containing the selected partitions
223  */
224  std::vector< std::vector< size_t > > getSelectedPartitions() const;
225 
226  /**
227  * Returns the indicated selected partition from the saved set
228  * \param index index of the desired partition
229  * \return vector containing the selected partition
230  */
231  std::vector< size_t > getSelectedPartitions( size_t index ) const;
232 
233  /**
234  * Returns the set of the selected partition colors
235  * \return vector containing the selected partitions colors
236  */
237  std::vector< std::vector< WHcoord > > getSelectedColors() const;
238 
239  /**
240  * Returns the selected partition colors from the saved set
241  * \param index index of the desired partition
242  * \return vector containing the selected partition colors
243  */
244  std::vector< WHcoord > getSelectedColors( size_t index ) const;
245 
246  /**
247  * writes out a small report status of the tree
248  * \param longMsg if set a longer two-line message will be output
249  * \return string with the status message
250  */
251  std::string getReport( bool longMsg = true ) const;
252 
253  /**
254  * checks tree integrity
255  * \return tree integrity indicator
256  */
257  bool check() const;
258 
259  /**
260  * returns a const pointer to the node indicated by the ID
261  * \param thisNode full id of the required node
262  * \return const reference to desired node/leaf object
263  */
264  const WHnode& getNode( const nodeID_t &thisNode ) const;
265 
266  /**
267  * returns a const pointer to the node indicated by the ID
268  * \param thisNode id of the required node
269  * \return const reference to desired node object
270  */
271  const WHnode& getNode( const size_t thisNode ) const;
272 
273  /**
274  * returns a const pointer to the leaf indicated by the ID
275  * \param thisLeaf id of the required leaf
276  * \return const reference to desired leaf object
277  */
278  const WHnode& getLeaf( const size_t thisLeaf ) const;
279 
280  /**
281  * returns a const reference to the root node
282  * \return a const reference to the root node
283 
284  */
285  const WHnode& getRoot() const;
286 
287  /**
288  * returns the leaf ID of a coordinate in the tree
289  * \param thisCoord coordinate of the required leaf
290  * \return ID of the leaf
291  */
292  size_t getLeafID( const WHcoord &thisCoord ) const;
293 
294  /**
295  * returns the corresponding track ID to a leaf ID on the tree
296  * \param leafID input leaf ID
297  * \return ID of seed tractogram corresponding to that leaf
298  */
299  size_t getTrackID( const size_t &leafID ) const;
300 
301  /**
302  * Returns a vector with all the leaf IDs contained in that cluster
303  * \param nodeID id of the selected node
304  * \return vector with all the leaf IDs contained in that cluster
305  */
306  std::vector<size_t> getLeaves4node( const size_t nodeID ) const;
307 
308  /**
309  * Returns a vector with all the leaf IDs contained in that cluster
310  * \param nodeID full id of the selected node
311  * \return vector with all the leaf IDs contained in that cluster
312  */
313  std::vector<size_t> getLeaves4node( const nodeID_t &nodeID ) const;
314 
315  /**
316  * Returns a vector with all the node IDs contained in that branch
317  * \param nodeID id of the selected node
318  * \return vector with all the node IDs contained in that brnach
319  */
320  std::vector<size_t> getBranchNodes( const size_t nodeID ) const;
321 
322  /**
323  * the coordinates of a particular leaf
324  * \param leafID id of the selected leaf
325  * \return coordinate object with the coordinates of that leaf
326  */
327  WHcoord getCoordinate4leaf( const size_t leafID ) const;
328 
329  /**
330  * Returns a vector with all the seed voxel coordinates contained in that cluster
331  * \param nodeID id of the selected node
332  * \return vector with all the seed voxel coordinates contained in that cluster
333  */
334  std::vector<WHcoord> getCoordinates4node( const size_t nodeID ) const;
335 
336  /**
337  * Returns a vector with all the seed voxel coordinates contained in that cluster
338  * \param nodeID full id of the selected node
339  * \return vector with all the seed voxel coordinates contained in that cluster
340  */
341  std::vector<WHcoord> getCoordinates4node( const nodeID_t &nodeID ) const;
342 
343  /**
344  * Returns the mean coordinate of all the seed voxel coordinates contained in that cluster
345  * \param nodeID id of the selected node
346  * \return coordinate object with the mean coordinates of that node
347  */
348  WHcoord getMeanCoordinate4node( const size_t nodeID ) const;
349 
350  /**
351  * Returns the mean coordinate of all the seed voxel coordinates contained in that cluster
352  * \param nodeID full id of the selected node
353  * \return coordinate object with the mean coordinates of that node
354  */
355  WHcoord getMeanCoordinate4node( const nodeID_t &nodeID ) const;
356 
357  /**
358  * retrieves the the first common ancestor node of two given nodes
359  * \param nodeID1 first node id
360  * \param nodeID2 second node id
361  * \return ID of ancestor
362  */
363  size_t getCommonAncestor( const size_t nodeID1, const size_t nodeID2 ) const;
364 
365  /**
366  * retrieves the the first common ancestor node of two given nodes
367  * \param nodeID1 first node/leaf full id
368  * \param nodeID2 second node/leaf full id
369  * \return ID of ancestor
370  */
371  nodeID_t getCommonAncestor( const nodeID_t &nodeID1, const nodeID_t &nodeID2 ) const;
372 
373  /**
374  * node route to root node
375  * \param nodeID full id of the selected node
376  * \return nodeId vector of nodes in the route
377  */
378  std::vector<nodeID_t> getRoute2Root( const nodeID_t &nodeID ) const;
379 
380  /**
381  * checks the joining order of a given triplet
382  * \param nodeIDa first node/leaf of the triplet
383  * \param nodeIDb second node/leaf of the triplet
384  * \param nodeIDc second node/leaf of the triplet
385  * \return joining order: 0="non resolved" (a,b,c join at the same time), 1="ab before c", 2="ac before b", 3="bc before a"
386  */
387  unsigned int getTripletOrder( const nodeID_t &nodeIDa, const nodeID_t &nodeIDb, const nodeID_t &nodeIDc ) const;
388 
389  /**
390  * returns a vector with all the nodes that have direct link to leaves fro the indicated subtree
391  * \param root id of the selected subtree root node
392  * \return nodeId vector of base nodes
393  */
394  std::vector<size_t> getBaseNodes( const size_t root ) const;
395 
396  /**
397  * returns a vector with all the nodes that have direct link to leaves from the indicated subtree
398  * \param root full id of the selected subtree root node
399  * \return nodeId vector of base nodes
400  */
401  std::vector<nodeID_t> getBaseNodes( const nodeID_t &root ) const;
402 
403  /**
404  * returns a vector with all the nodes that have direct link to leaves from the main root
405  * \return nodeId vector of base nodes
406  */
407  std::vector<size_t> getRootBaseNodes() const;
408 
409  /**
410  * tests if all base nodes have only leaf children
411  * \return true if all base nodes have only leaf children
412  */
413  bool testRootBaseNodes() const;
414 
415  /**
416  * computes the cophenetic distance between 2 nodes
417  * \param nodeID1 first node to compute distance from
418  * \param nodeID2 second node to compute distance from
419  * \return cophenetic distance
420  */
421  dist_t getDistance( const size_t nodeID1, const size_t nodeID2 ) const;
422 
423  /**
424  * computes the cophenetic distance between 2 leaf/nodes
425  * \param nodeID1 first node/leaf to compute distance from
426  * \param nodeID2 second node/leaf to compute distance from
427  * \return cophenetic distance
428  */
429  dist_t getDistance( const nodeID_t &nodeID1, const nodeID_t &nodeID2 ) const;
430 
431  /**
432  * computes the cophenetic distance between 2 leaves
433  * \param coord1 first coordinate to compute distance from
434  * \param coord2 second ncoordinate to compute distance from
435  * \return cophenetic distance
436  */
437  dist_t getDistance( const WHcoord &coord1, const WHcoord &coord2 ) const;
438 
439  /**
440  * computes the cophenetic distance between 2 leaves
441  * \param leafID1 first leaf to compute distance from
442  * \param leafID2 second leaf to compute distance from
443  * \return cophenetic distance
444  */
445  dist_t getLeafDistance( const size_t leafID1, const size_t leafID2 ) const;
446 
447 
448 
449  /**
450  * Reorders the nodes in the vector according to their size
451  * \param nodeVector vector of nodes to be reordered
452  */
453  void sortBySize( std::vector<size_t>* const nodeVector ) const;
454 
455  /**
456  * Reorders the nodes in the vector according to their size
457  * \param nodeVector vector of nodes to be reordered
458  */
459  void sortBySize( std::vector<nodeID_t>* const nodeVector ) const;
460 
461  /**
462  * Reorders the nodes in the list according to their size
463  * \param nodeList list of nodes to be reordered
464  */
465  void sortBySize( std::list<size_t>* const nodeList ) const;
466 
467  /**
468  * Reorders the nodes in the list according to their size
469  * \param nodeList vector of nodes to be reordered
470  */
471  void sortBySize( std::list<nodeID_t>* const nodeList ) const;
472 
473 
474  /**
475  * Reorders the nodes in the vector according to their hierarchical level
476  * \param nodeVector vector of nodes to be reordered
477  */
478  void sortByHLevel( std::vector<size_t>* const nodeVector ) const;
479 
480  /**
481  * Reorders the nodes in the list according to their hierarchical level
482  * \param nodeList list of nodes to be reordered
483  */
484  void sortByHLevel( std::list<size_t>* const nodeList ) const;
485 
486 
487  /**
488  * Reorders the nodes in the vector according to their hierarchical level
489  * \param nodeVector vector of nodes to be reordered
490  */
491  void sortByHLevel( std::vector<nodeID_t>* const nodeVector ) const;
492 
493  /**
494  * Reorders the nodes in the list according to their hierarchical level
495  * \param nodeList vector of nodes to be reordered
496  */
497  void sortByHLevel( std::list<nodeID_t>* const nodeList ) const;
498 
499 
500  /**
501  * stores the contained leaves for each node
502  */
503  void loadContainedLeaves();
504 
505  /**
506  * frees the memory of m_containedLeaves
507  */
508  void clearContainedLeaves();
509 
510  /**
511  * converts all the coordinates to the grid format indicated
512  * \param newGrid coordinate space in which to convert
513  * \return flag indicating if the change was performed
514  */
515  bool convert2grid( const HC_GRID newGrid );
516 
517  /**
518  * Reads the the tree data from a file and creates the structure, containing it in the leaves, nodes and coordinates vectors
519  * \param filename string containing std::vector<hNode> m_leavesthe name of the file with the tree data
520  * \return success indicator
521  */
522  bool readTree( const std::string &filename );
523 
524  /**
525  * writes the current tree data
526  * \param filename string containing the name of the file to be written
527  * \param isNifti a flag indicating whether the tree should be written in nifti coordinates
528  * \return success indicator
529  */
530  bool writeTree( const std::string &filename, const bool isNifti = true ) const;
531 
532  /**
533  * writes all redundant tree data for debugging purposes
534  * \param filename string containing the name of the file to be written
535  * \return success indicator
536  */
537  bool writeTreeDebug( const std::string &filename ) const;
538 
539  /**
540  * writes dta for working with the old walnut module
541  * \param filename string containing the name of the file to be written
542  * \return success indicator
543  */
544  bool writeTreeOldWalnut( const std::string &filename ) const;
545 
546  /**
547  * writes only join data without coordinates or reading labels
548  * \param filename string containing the name of the file to be written
549  * \return success indicator
550  */
551  bool writeTreeSimple( const std::string &filename ) const;
552 
553  /**
554  * insert a set of partitions into the tree data
555  * \param selectedPartitions a set of partitions
556  * \param selectedValues the quality values for those partitions
557  * \param selectedColors the colors for each cluster of each partitions
558  */
559  void insertPartitions( const std::vector<std::vector<size_t> > &selectedPartitions, const std::vector< float > &selectedValues,
560  const std::vector<std::vector<WHcoord> > &selectedColors = std::vector<std::vector<WHcoord> >() );
561 
562  /**
563  * insert a set of partitions colors into the tree data in case a set of saved partitions is already present
564  * \param selectedColors the set of new colors for the included partitions
565  */
566  void insertPartColors( const std::vector<std::vector<WHcoord> > &selectedColors );
567 
568  /**
569  * delete all data related to saved partitions
570  */
571  void clearPartitions();
572 
573  /**
574  * delete only color data of the saved partitions
575  */
576  void clearPartColors();
577 
578  /**
579  * gets all possble sets of sub-partitions in a tree given an initial partition and a depth level (where leaves/nodes are identified with boolean-integer pair)
580  \param thisPartition the starting partition
581  \param depthLevel the number of levels down to look for sub-partitions
582  \param excludeLeaves if set (1 ), the the algorithm will not further subdivide a base-node (where all its children are single leaves)
583  \param partitionSet (is a return value) the vector of returned sub-partitions
584  \return a set of integers identifying each of the clusters from the resulting sub-partitions with its parent cluster from the starting partition
585  */
586  std::vector< std::vector< unsigned int > > getBranching( const std::vector < nodeID_t > &thisPartition,
587  size_t depthLevel,
588  std::vector< std::vector < nodeID_t > > *partitionSet,
589  const bool excludeLeaves );
590 
591  /**
592  * gets all possble sets of sub-partitions in a tree given an initial partition and a depth level (where only non-leaf nodes are considered and are identified by a single integer)
593  \param thisPartition the starting partition
594  \param depthLevel the number of levels down to look for sub-partitions
595  \param partitionSet (is a return value) the vector of returned sub-partitions
596  \return a set of integers identifying each of the clusters from the resulting sub-partitions with its parent cluster from the starting partition
597  */
598  std::vector< std::vector< unsigned int > > getBranching( const std::vector < size_t > &thisPartition,
599  size_t depthLevel,
600  std::vector< std::vector < size_t > > *partitionSet );
601 
602 
603 
604  friend class treeManager; //!< friend class
605  friend class WHtreeProcesser; //!< friend class
606  friend class WHtreePartition; //!< friend class
607 
608  friend class CnbTreeBuilder; //!< friend class
609  friend class graphTreeBuilder; //!< friend class
610  friend class randCnbTreeBuilder; //!< friend class
611  friend class treeComparer; //!< friend class
612  friend class pruneTree; //!< friend class
613  friend class image2treeBuilder; //!< friend class
614 
615 
616 protected:
617 private:
618  // === PRIVATE MEMBER DATA ===
619 
620 
621  //! Stores all the leaves of the tree (represents a single voxel, for several purposes a leaf also counts as a node
623 
624  //! Stores the size of the dataset this tree was built from
626 
627  //! Stores the the type of coordinate grid of the dataset
628  HC_GRID m_datasetGrid;
629 
630  //! number of streamlines generated form each seed voxel in the tractograms used to build the tree
632 
633  //! logarithmic normalization factor used in the tractograms when building the tree
634  float m_logFactor;
635 
636  //! Stores the cpcc value of the tree
637  float m_cpcc;
638 
639  //! Stores the name of the tree file
640  std::string m_treeName;
641 
642  //! Stores all the leaves of the tree (represents a single voxel, for several purposes a leaf also counts as a node
643  std::vector<WHnode> m_leaves;
644 
645  //! Stores all the nodes (non-leaves) of the tree
646  std::vector<WHnode> m_nodes;
647 
648  //! Stores the coordinates of the leaf voxels
649  std::vector<WHcoord> m_coordinates;
650 
651  //! Stores the ids of the seed tracts correesponding to each leaf
652  std::vector<size_t> m_trackids;
653 
654  //! Stores the coordinates of the voxels that were discarded during the process of building the tree
655  std::list<WHcoord> m_discarded;
656 
657  //! Stores the contained leafs of each node
658  std::vector<std::vector<size_t> > m_containedLeaves;
659 
660  //! Stores a set of selected partitions
661  std::vector< std::vector<size_t> > m_selectedPartitions;
662 
663  //! Stores the quality values of the selected partitions
664  std::vector< float > m_selectedValues;
665 
666  //! Stores the colors of the selected partitions
667  std::vector< std::vector< WHcoord > > m_selectedColors;
668 
669  // === PRIVATE MEMBER FUNCTIONS ===
670 
671 
672  /**
673  * returns a pointer to the node indicated by the ID
674  * \param thisNode full id of the required node
675  * \return pointer to desired node/leaf object
676  */
677  WHnode* fetchNode( const nodeID_t &thisNode );
678 
679  /**
680  * returns a pointer to the node indicated by the ID
681  * \param thisNode id of the required node
682  * \return pointer to desired node/leaf object
683  */
684  WHnode* fetchNode( const size_t thisNode );
685 
686  /**
687  * returns a pointer to the leaf indicated by the ID
688  * \param thisLeaf id of the required leaf
689  * \return pointer to desired leaf object
690  */
691  WHnode* fetchLeaf( const size_t thisLeaf );
692 
693  /**
694  * returns a pointer to the root node
695  * \return a pointer to root node object
696  */
697  WHnode* fetchRoot();
698 
699  /**
700  * cleans leaves/nodes set to be pruned
701  * \param outLookup pointer to a vector where to store the lookup table of changes done to nodes
702  * \return pair with the number of leaves and nodes pruned
703  */
704  std::pair<size_t, size_t> cleanup( std::vector<size_t> *outLookup = 0 );
705 
706  /**
707  * merges nodes joining binary at the same level into non-binary structures
708  * \param keepBaseNodes if set basenodes will not be marged
709  * \return number of nodes collapsed
710  */
711  size_t debinarize( const bool keepBaseNodes = false );
712 
713  /**
714  * eliminates non monotonic steps in the tree while keeping as much information as possible.
715  * it searches bottom-up for non-monotonic steps. When detected a corrected value for the parent node is computed
716  * 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
717  * in orther to avoid infinite loops an error tolerance is included. default value is 1*E-5
718  * \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)
719  */
720  void forceMonotonicity( double errorMult = 1 );
721 
722  /**
723  * eliminates non monotonic steps in the tree bottom-up, lowering the level of children involved in the non-monotonic steps to the parent level
724  */
725  void forceMonotonicityUp();
726 
727  /**
728  * eliminates non monotonic steps in the tree top-down, raising the level of parents involved in the non-monotonic steps to the children level
729  */
730  void forceMonotonicityDown();
731 
732  //! implements a compare operator for nodes inside a tree structure, depending on the Size
733  struct compSize
734  {
735  const WHtree* const m_tree; //!< stores pointer to tree we work on
736 
737  /**
738  * structure constructor, stores the tree pointer into the data member
739  * \param tree the tree pointer
740  */
741  explicit compSize( const WHtree* const tree ): m_tree( tree )
742  {
743  }
744 
745  /**
746  * the comparison operator result when using nodeID_t types
747  * \param lhs the left hand side object
748  * \param rhs the right hand side object
749  * \return the result of the comparison operation
750  */
751  bool operator()( const nodeID_t &lhs, const nodeID_t &rhs ) const
752  {
753  return ( ( ( m_tree->getNode( lhs ) ).getSize() ) < ( ( m_tree->getNode( rhs ) ).getSize() ) );
754  }
755 
756  /**
757  * the comparison operator result when using size_t types
758  * \param lhs the left hand side object
759  * \param rhs the right hand side object
760  * \return the result of the comparison operation
761  */
762  bool operator()( const size_t lhs, const size_t rhs ) const
763  {
764  return ( ( ( m_tree->getNode( lhs ) ).getSize() ) < ( ( m_tree->getNode( rhs ) ).getSize() ) );
765  }
766  };
767 
768  //! implements a compare operator for nodes, depending on the hierarchical level
769  struct compHLevel
770  {
771  const WHtree* const m_tree; //!< stores pointer to tree we work on
772 
773  /**
774  * structure constructor, stores the tree pointer into the data member
775  * \param tree the tree pointer
776  */
777  explicit compHLevel( const WHtree* const tree ): m_tree( tree )
778  {
779  }
780 
781  /**
782  * the comparison operator result when using nodeID_t types
783  * \param lhs the left hand side object
784  * \param rhs the right hand side object
785  * \return the result of the comparison operation
786  */
787  bool operator()( const nodeID_t &lhs, const nodeID_t &rhs ) const
788  {
789  if( ( ( m_tree->getNode( lhs ) ).getHLevel() ) == ( ( m_tree->getNode( rhs ) ).getHLevel() ) )
790  {
791  return( lhs < rhs );
792  }
793  else
794  {
795  return( ( ( m_tree->getNode( lhs ) ).getHLevel() ) < ( ( m_tree->getNode( rhs ) ).getHLevel() ) );
796  }
797  }
798 
799  /**
800  * the comparison operator result when using size_t types
801  * \param lhs the left hand side object
802  * \param rhs the right hand side object
803  * \return the result of the comparison operation
804  */
805  bool operator()( const size_t lhs, const size_t rhs ) const
806  {
807  if( ( ( m_tree->getNode( lhs ) ).getHLevel() ) == ( ( m_tree->getNode( rhs ) ).getHLevel() ) )
808  {
809  return( lhs < rhs );
810  }
811  else
812  {
813  return( ( ( m_tree->getNode( lhs ) ).getHLevel() ) < ( ( m_tree->getNode( rhs ) ).getHLevel() ) );
814  }
815  }
816  };
817 };
818 
819 // === IN-LINE PUBLIC MEMBERS ===
820 
821 inline std::string WHtree::getName() const
822 {
823  return m_treeName;
824 }
825 
826 inline bool WHtree::isLoaded() const
827 {
828  return m_loadStatus;
829 }
830 
831 inline size_t WHtree::getNumLeaves() const
832 {
833  return m_leaves.size();
834 }
835 
836 inline size_t WHtree::getNumNodes() const
837 {
838  return m_nodes.size();
839 }
840 
841 inline std::vector<WHnode> WHtree::getLeaves() const
842 {
843  return m_leaves;
844 }
845 
846 inline std::vector<WHnode> WHtree::getNodes() const
847 {
848  return m_nodes;
849 }
850 inline size_t WHtree::getNumDiscarded() const
851 {
852  return m_discarded.size();
853 }
854 
856 {
857  return m_datasetSize;
858 }
859 
860 inline HC_GRID WHtree::getDataGrid() const
861 {
862  return m_datasetGrid;
863 }
864 
865 inline float WHtree::getCpcc() const
866 {
867  return m_cpcc;
868 }
869 
870 inline std::vector<WHcoord> WHtree::getRoi() const
871 {
872  return m_coordinates;
873 }
874 
875 inline std::vector<size_t> WHtree::getTrackids() const
876 {
877  return m_trackids;
878 }
879 
880 inline std::list<WHcoord> WHtree::getDiscarded() const
881 {
882  return m_discarded;
883 }
884 
885 inline std::vector<float> WHtree::getSelectedValues() const
886 {
887  return m_selectedValues;
888 }
889 
890 inline float WHtree::getSelectedValues( size_t index ) const
891 {
892  if( index >= m_selectedValues.size() )
893  {
894  return -1;
895  }
896  else
897  {
898  return m_selectedValues[ index ];
899  }
900 }
901 
902 inline std::vector< std::vector< size_t > > WHtree::getSelectedPartitions() const
903 {
904  return m_selectedPartitions;
905 }
906 
907 inline std::vector< size_t > WHtree::getSelectedPartitions( size_t index ) const
908 {
909  if( index >= m_selectedPartitions.size() )
910  {
911  return std::vector< size_t >();
912  }
913  else
914  {
915  return m_selectedPartitions[ index ];
916  }
917 }
918 
919 inline std::vector< std::vector< WHcoord > > WHtree::getSelectedColors() const
920 {
921  return m_selectedColors;
922 }
923 
924 inline std::vector< WHcoord > WHtree::getSelectedColors( size_t index ) const
925 {
926  if( index >= m_selectedColors.size() )
927  {
928  return std::vector< WHcoord >();
929  }
930  else
931  {
932  return m_selectedColors[ index ];
933  }
934 }
935 
936 #endif // WHTREE_H
this class to contain a seed voxel coordinate.
Definition: WHcoord.h:76
this class implements a hierarchical tree node with several relevant attributes
Definition: WHnode.h:69
this class operates over the class WHtree to extract partitions and analyze partition quality it may ...
this class implements methods for dendrogram processing, such as pruning, node collapsing and decimat...
this class implements a hierarchical tree and provides functions for creation, partitioning,...
Definition: WHtree.h:81
WHtree()
Constructor.
Definition: WHtree.cpp:73
size_t getNumDiscarded() const
Returns the number of leaves of the tree.
Definition: WHtree.h:850
void loadContainedLeaves()
stores the contained leaves for each node
Definition: WHtree.cpp:838
std::vector< size_t > getRootBaseNodes() const
returns a vector with all the nodes that have direct link to leaves from the main root
Definition: WHtree.cpp:706
const WHnode & getRoot() const
returns a const reference to the root node
Definition: WHtree.cpp:348
std::vector< WHnode > getNodes() const
Returns the nodes of the tree.
Definition: WHtree.h:846
HC_GRID getDataGrid() const
Returns the dataset grid type.
Definition: WHtree.h:860
size_t getCommonAncestor(const size_t nodeID1, const size_t nodeID2) const
retrieves the the first common ancestor node of two given nodes
Definition: WHtree.cpp:535
float m_cpcc
Stores the cpcc value of the tree.
Definition: WHtree.h:637
friend class graphTreeBuilder
friend class
Definition: WHtree.h:609
std::vector< std::vector< WHcoord > > m_selectedColors
Stores the colors of the selected partitions.
Definition: WHtree.h:667
bool writeTreeDebug(const std::string &filename) const
writes all redundant tree data for debugging purposes
Definition: WHtree.cpp:1380
std::vector< std::vector< size_t > > m_containedLeaves
Stores the contained leafs of each node.
Definition: WHtree.h:658
std::vector< size_t > getBaseNodes(const size_t root) const
returns a vector with all the nodes that have direct link to leaves fro the indicated subtree
Definition: WHtree.cpp:642
size_t getNumNodes() const
Returns the number of nodes of the tree.
Definition: WHtree.h:836
dist_t getDistance(const size_t nodeID1, const size_t nodeID2) const
computes the cophenetic distance between 2 nodes
Definition: WHtree.cpp:730
std::vector< WHcoord > m_coordinates
Stores the coordinates of the leaf voxels.
Definition: WHtree.h:649
std::vector< WHcoord > getCoordinates4node(const size_t nodeID) const
Returns a vector with all the seed voxel coordinates contained in that cluster.
Definition: WHtree.cpp:480
std::vector< size_t > getLeaves4node(const size_t nodeID) const
Returns a vector with all the leaf IDs contained in that cluster.
Definition: WHtree.cpp:380
WHcoord m_datasetSize
Stores the size of the dataset this tree was built from.
Definition: WHtree.h:625
friend class pruneTree
friend class
Definition: WHtree.h:612
std::vector< WHnode > m_nodes
Stores all the nodes (non-leaves) of the tree.
Definition: WHtree.h:646
void insertPartitions(const std::vector< std::vector< size_t > > &selectedPartitions, const std::vector< float > &selectedValues, const std::vector< std::vector< WHcoord > > &selectedColors=std::vector< std::vector< WHcoord > >())
insert a set of partitions into the tree data
Definition: WHtree.cpp:1473
friend class image2treeBuilder
friend class
Definition: WHtree.h:613
const WHnode & getNode(const nodeID_t &thisNode) const
returns a const pointer to the node indicated by the ID
Definition: WHtree.cpp:307
friend class CnbTreeBuilder
friend class
Definition: WHtree.h:608
std::vector< std::vector< size_t > > getSelectedPartitions() const
Returns the set of the selected partitions.
Definition: WHtree.h:902
size_t getLeafID(const WHcoord &thisCoord) const
returns the leaf ID of a coordinate in the tree
Definition: WHtree.cpp:354
bool m_loadStatus
Stores all the leaves of the tree (represents a single voxel, for several purposes a leaf also counts...
Definition: WHtree.h:622
std::list< WHcoord > getDiscarded() const
Returns the discarded list.
Definition: WHtree.h:880
void clearPartitions()
delete all data related to saved partitions
Definition: WHtree.cpp:1534
void clearPartColors()
delete only color data of the saved partitions
Definition: WHtree.cpp:1545
bool writeTreeOldWalnut(const std::string &filename) const
writes dta for working with the old walnut module
Definition: WHtree.cpp:1416
friend class treeComparer
friend class
Definition: WHtree.h:611
void forceMonotonicityDown()
eliminates non monotonic steps in the tree top-down, raising the level of parents involved in the non...
Definition: WHtree.cpp:2226
dist_t getLeafDistance(const size_t leafID1, const size_t leafID2) const
computes the cophenetic distance between 2 leaves
Definition: WHtree.cpp:748
std::vector< std::vector< size_t > > m_selectedPartitions
Stores a set of selected partitions.
Definition: WHtree.h:661
unsigned int getTripletOrder(const nodeID_t &nodeIDa, const nodeID_t &nodeIDb, const nodeID_t &nodeIDc) const
checks the joining order of a given triplet
Definition: WHtree.cpp:612
size_t debinarize(const bool keepBaseNodes=false)
merges nodes joining binary at the same level into non-binary structures
Definition: WHtree.cpp:1985
const WHnode & getLeaf(const size_t thisLeaf) const
returns a const pointer to the leaf indicated by the ID
Definition: WHtree.cpp:333
std::vector< WHnode > m_leaves
Stores all the leaves of the tree (represents a single voxel, for several purposes a leaf also counts...
Definition: WHtree.h:643
bool writeTreeSimple(const std::string &filename) const
writes only join data without coordinates or reading labels
Definition: WHtree.cpp:1457
void clearContainedLeaves()
frees the memory of m_containedLeaves
Definition: WHtree.cpp:860
WHnode * fetchNode(const nodeID_t &thisNode)
returns a pointer to the node indicated by the ID
Definition: WHtree.cpp:1684
std::string getReport(bool longMsg=true) const
writes out a small report status of the tree
Definition: WHtree.cpp:132
friend class randCnbTreeBuilder
friend class
Definition: WHtree.h:610
std::pair< size_t, size_t > cleanup(std::vector< size_t > *outLookup=0)
cleans leaves/nodes set to be pruned
Definition: WHtree.cpp:1716
void sortByHLevel(std::vector< size_t > *const nodeVector) const
Reorders the nodes in the vector according to their hierarchical level.
Definition: WHtree.cpp:817
size_t getTrackID(const size_t &leafID) const
returns the corresponding track ID to a leaf ID on the tree
Definition: WHtree.cpp:367
void insertPartColors(const std::vector< std::vector< WHcoord > > &selectedColors)
insert a set of partitions colors into the tree data in case a set of saved partitions is already pre...
Definition: WHtree.cpp:1511
WHcoord getMeanCoordinate4node(const size_t nodeID) const
Returns the mean coordinate of all the seed voxel coordinates contained in that cluster.
Definition: WHtree.cpp:504
WHnode * fetchRoot()
returns a pointer to the root node
Definition: WHtree.cpp:1710
void sortBySize(std::vector< size_t > *const nodeVector) const
Reorders the nodes in the vector according to their size.
Definition: WHtree.cpp:755
bool isLoaded() const
Returns the loading status of the tree.
Definition: WHtree.h:826
std::vector< float > getSelectedValues() const
Returns the cutting values for the selected partitions.
Definition: WHtree.h:885
float m_logFactor
logarithmic normalization factor used in the tractograms when building the tree
Definition: WHtree.h:634
size_t m_numStreamlines
number of streamlines generated form each seed voxel in the tractograms used to build the tree
Definition: WHtree.h:631
std::string getName() const
Returns the tree name.
Definition: WHtree.h:821
void forceMonotonicity(double errorMult=1)
eliminates non monotonic steps in the tree while keeping as much information as possible.
Definition: WHtree.cpp:2247
std::vector< size_t > getTrackids() const
Returns the tractogram IDs vector.
Definition: WHtree.h:875
std::string m_treeName
Stores the name of the tree file.
Definition: WHtree.h:640
bool testRootBaseNodes() const
tests if all base nodes have only leaf children
Definition: WHtree.cpp:712
bool writeTree(const std::string &filename, const bool isNifti=true) const
writes the current tree data
Definition: WHtree.cpp:1233
std::vector< nodeID_t > getRoute2Root(const nodeID_t &nodeID) const
node route to root node
Definition: WHtree.cpp:589
bool check() const
checks tree integrity
Definition: WHtree.cpp:152
HC_GRID m_datasetGrid
Stores the the type of coordinate grid of the dataset.
Definition: WHtree.h:628
std::vector< size_t > m_trackids
Stores the ids of the seed tracts correesponding to each leaf.
Definition: WHtree.h:652
WHcoord getDataSize() const
Returns the dataset size.
Definition: WHtree.h:855
bool convert2grid(const HC_GRID newGrid)
converts all the coordinates to the grid format indicated
Definition: WHtree.cpp:867
std::vector< size_t > getBranchNodes(const size_t nodeID) const
Returns a vector with all the node IDs contained in that branch.
Definition: WHtree.cpp:434
float getCpcc() const
Returns the cpcc value of the tree.
Definition: WHtree.h:865
WHcoord getCoordinate4leaf(const size_t leafID) const
the coordinates of a particular leaf
Definition: WHtree.cpp:466
std::vector< WHnode > getLeaves() const
Returns the leaves of the tree.
Definition: WHtree.h:841
bool readTree(const std::string &filename)
Reads the the tree data from a file and creates the structure, containing it in the leaves,...
Definition: WHtree.cpp:911
WHnode * fetchLeaf(const size_t thisLeaf)
returns a pointer to the leaf indicated by the ID
Definition: WHtree.cpp:1697
std::vector< std::vector< WHcoord > > getSelectedColors() const
Returns the set of the selected partition colors.
Definition: WHtree.h:919
std::vector< float > m_selectedValues
Stores the quality values of the selected partitions.
Definition: WHtree.h:664
~WHtree()
Destructor.
Definition: WHtree.cpp:121
friend class treeManager
friend class
Definition: WHtree.h:604
size_t getNumLeaves() const
Returns the number of leaves of the tree.
Definition: WHtree.h:831
std::list< WHcoord > m_discarded
Stores the coordinates of the voxels that were discarded during the process of building the tree.
Definition: WHtree.h:655
std::vector< WHcoord > getRoi() const
Returns the roi coordinate vector.
Definition: WHtree.h:870
std::vector< std::vector< unsigned int > > getBranching(const std::vector< nodeID_t > &thisPartition, size_t depthLevel, std::vector< std::vector< nodeID_t > > *partitionSet, const bool excludeLeaves)
gets all possble sets of sub-partitions in a tree given an initial partition and a depth level (where...
Definition: WHtree.cpp:1552
void forceMonotonicityUp()
eliminates non monotonic steps in the tree bottom-up, lowering the level of children involved in the ...
Definition: WHtree.cpp:2205
implements a compare operator for nodes, depending on the hierarchical level
Definition: WHtree.h:770
bool operator()(const size_t lhs, const size_t rhs) const
the comparison operator result when using size_t types
Definition: WHtree.h:805
bool operator()(const nodeID_t &lhs, const nodeID_t &rhs) const
the comparison operator result when using nodeID_t types
Definition: WHtree.h:787
compHLevel(const WHtree *const tree)
structure constructor, stores the tree pointer into the data member
Definition: WHtree.h:777
const WHtree *const m_tree
stores pointer to tree we work on
Definition: WHtree.h:771
implements a compare operator for nodes inside a tree structure, depending on the Size
Definition: WHtree.h:734
bool operator()(const size_t lhs, const size_t rhs) const
the comparison operator result when using size_t types
Definition: WHtree.h:762
bool operator()(const nodeID_t &lhs, const nodeID_t &rhs) const
the comparison operator result when using nodeID_t types
Definition: WHtree.h:751
compSize(const WHtree *const tree)
structure constructor, stores the tree pointer into the data member
Definition: WHtree.h:741
const WHtree *const m_tree
stores pointer to tree we work on
Definition: WHtree.h:735