Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
mitkConnectomicsShortestPathHistogram.cpp
Go to the documentation of this file.
1 
2 /*===================================================================
3 
4 The Medical Imaging Interaction Toolkit (MITK)
5 
6 Copyright (c) German Cancer Research Center,
7 Division of Medical and Biological Informatics.
8 All rights reserved.
9 
10 This software is distributed WITHOUT ANY WARRANTY; without
11 even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE.
13 
14 See LICENSE.txt or http://www.mitk.org for details.
15 
16 ===================================================================*/
17 
19 
20 #include <boost/graph/dijkstra_shortest_paths.hpp>
21 
23 
25 : m_Mode( UnweightedUndirectedMode )
26 , m_EverythingConnected( true )
27 {
28  m_Subject = "Shortest path";
29 }
30 
32 {
33 }
34 
36 {
37  m_Mode = mode;
38 }
39 
41 {
42  return m_Mode;
43 }
44 
46 {
47 
48  NetworkType* boostGraph = source->GetBoostGraph();
49 
50  switch( m_Mode )
51  {
52  case UnweightedUndirectedMode:
53  {
54  CalculateUnweightedUndirectedShortestPaths( boostGraph );
55  break;
56  }
57  case WeightedUndirectedMode:
58  {
59  CalculateWeightedUndirectedShortestPaths( boostGraph );
60  break;
61  }
62  }
63 
64  ConvertDistanceMapToHistogram();
65 }
66 
68 {
69  std::vector< DescriptorType > predecessorMap( boost::num_vertices( *boostGraph ) );
70  int numberOfNodes( boost::num_vertices( *boostGraph ) );
71 
72  m_DistanceMatrix.resize( numberOfNodes );
73  for(unsigned int index(0); index < m_DistanceMatrix.size(); index++ )
74  {
75  m_DistanceMatrix[ index ].resize( numberOfNodes );
76  }
77 
78  IteratorType iterator, end;
79  boost::tie(iterator, end) = boost::vertices( *boostGraph );
80 
81  for ( int index(0) ; iterator != end; ++iterator, index++)
82  {
83  boost::dijkstra_shortest_paths(*boostGraph, *iterator, boost::predecessor_map(&predecessorMap[ 0 ]).distance_map(&m_DistanceMatrix[ index ][ 0 ]).weight_map( boost::get( &mitk::ConnectomicsNetwork::NetworkEdge::edge_weight ,*boostGraph ) ) ) ;
84  }
85 }
86 
88 {
90 }
91 
93 {
94  // get the longest path between any two nodes in the network
95  // we assume that no nodes are farther apart than there are nodes,
96  // this is to filter unconnected nodes
97  int longestPath( 0 );
98  int numberOfNodes( m_DistanceMatrix.size() );
99  m_EverythingConnected = true;
100 
101  for(unsigned int index(0); index < m_DistanceMatrix.size(); index++ )
102  {
103  for(unsigned int innerIndex(0); innerIndex < m_DistanceMatrix[ index ].size(); innerIndex++ )
104  {
105  if( m_DistanceMatrix[ index ][ innerIndex ] > longestPath )
106  {
107  if( m_DistanceMatrix[ index ][ innerIndex ] < numberOfNodes )
108  {
109  longestPath = m_DistanceMatrix[ index ][ innerIndex ];
110  }
111  else
112  {
113  // these nodes are not connected
114  m_EverythingConnected = false;
115  }
116  }
117  }
118  }
119 
120  m_HistogramVector.resize( longestPath + 1 );
121 
122  for(unsigned int index(0); index < m_DistanceMatrix.size(); index++ )
123  {
124  for( unsigned int innerIndex(0); innerIndex < m_DistanceMatrix[ index ].size(); innerIndex++ )
125  {
126  if( m_DistanceMatrix[ index ][ innerIndex ] < numberOfNodes )
127  {
128  m_HistogramVector[ m_DistanceMatrix[ index ][ innerIndex ] ]++;
129  }
130  }
131  }
132 
133  // correct for every path being counted twice
134 
135  for( unsigned int index(1); index < m_HistogramVector.size(); index++ )
136  {
137  m_HistogramVector[ index ] = m_HistogramVector[ index ] / 2;
138  }
139 
140  // correct for every node being distance zero to itself
141  if( m_HistogramVector[ 0 ] >= numberOfNodes )
142  {
143  m_HistogramVector[ 0 ] = m_HistogramVector[ 0 ] - numberOfNodes;
144  }
145  else
146  {
148  }
149 
150  UpdateYMax();
151 
152  this->m_Valid = true;
153 }
154 
156 {
157  if( !this->m_Valid )
158  {
160  return 0.0;
161  }
162 
163  if( !m_EverythingConnected )
164  { // efficiency of disconnected graphs is 0
166  return 0.0;
167  }
168 
169  double efficiency( 0.0 );
170 
171  double overallDistance( 0.0 );
172  double numberOfPairs( 0.0 );
173  // add up all distances
174  for( unsigned int index(0); index < m_HistogramVector.size(); index++ )
175  {
176  overallDistance = overallDistance + m_HistogramVector[ index ] * index;
177  numberOfPairs = numberOfPairs + m_HistogramVector[ index ];
178  }
179 
180  // efficiency = 1 / averageDistance = 1 / ( overallDistance / numberofPairs )
181  efficiency = numberOfPairs / overallDistance;
182 
183  return efficiency;
184 }
boost::graph_traits< NetworkType >::vertex_iterator IteratorType
#define MBI_INFO
Macros for different message levels. Creates an instance of class PseudoStream with the corresponding...
Definition: mbilog.h:221
void SetShortestPathCalculationMode(const ShortestPathCalculationMode &)
virtual void ComputeFromConnectomicsNetwork(ConnectomicsNetwork *source) override
Creates a new histogram from the network source.
Connectomics Network Class.
#define MBI_WARN
Definition: mbilog.h:222
std::string m_Subject
Subject of the histogram as a string.