Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkConnectomicsSimulatedAnnealingCostFunctionModularity.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 
20 {
21 }
22 
24 {
25 }
26 
28 {
29  double cost( 0.0 );
30  cost = 100.0 * ( 1.0 - CalculateModularity( network, vertexToModuleMap ) );
31  return cost;
32 }
33 
35 {
36  double modularity( 0.0 );
37  int numberOfModules = getNumberOfModules( vertexToModuleMap );
38 
39  if( network->GetNumberOfVertices() != (int)vertexToModuleMap->size() )
40  {
41  MBI_ERROR << "Number of vertices and vertex to module map size do not match!";
42  return modularity;
43  }
44 
45  int numberOfLinksInNetwork( 0 );
46  std::vector< int > numberOfLinksInModule, sumOfDegreesInModule;
47  numberOfLinksInModule.resize( numberOfModules, 0 );
48  sumOfDegreesInModule.resize( numberOfModules, 0 );
49  // get vector of all vertex descriptors in the network
50 
51  const std::vector< VertexDescriptorType > allNodesVector
52  = network->GetVectorOfAllVertexDescriptors();
53 
54  for( unsigned int nodeNumber( 0 ); nodeNumber < allNodesVector.size() ; nodeNumber++)
55  {
56  int correspondingModule = vertexToModuleMap->find( allNodesVector[ nodeNumber ] )->second;
57  const std::vector< VertexDescriptorType > adjacentNodexVector
58  = network->GetVectorOfAdjacentNodes( allNodesVector[ nodeNumber ] );
59  numberOfLinksInNetwork += adjacentNodexVector.size();
60  sumOfDegreesInModule[ correspondingModule ] += adjacentNodexVector.size();
61 
62  for( unsigned int adjacentNodeNumber( 0 ); adjacentNodeNumber < adjacentNodexVector.size() ; adjacentNodeNumber++)
63  {
64  if( correspondingModule == vertexToModuleMap->find( adjacentNodexVector[ adjacentNodeNumber ] )->second )
65  {
66  numberOfLinksInModule[ correspondingModule ]++;
67  }
68  }
69  }
70 
71  // the numbers for links have to be halved, as each edge was counted twice
72  numberOfLinksInNetwork = numberOfLinksInNetwork / 2;
73 
74  // if the network contains no links return 0
75  if( numberOfLinksInNetwork < 1)
76  {
77  return 0;
78  }
79 
80  for( int index( 0 ); index < numberOfModules ; index++)
81  {
82  numberOfLinksInModule[ index ] = numberOfLinksInModule[ index ] / 2;
83  }
84 
85  //Calculate modularity M:
86  //M = sum_{s=1}^{N_{M}} [ (l_{s} / L) - (d_{s} / ( 2L ))^2 ]
87  //where N_{M} is the number of modules
88  //L is the number of links in the network
89  //l_{s} is the number of links between nodes in the module
90  //s is the module
91  //d_{s} is the sum of degrees of the nodes in the module
92  //( taken from Guimera, R. AND Amaral, L. A. N.
93  // Cartography of complex networks: modules and universal roles
94  // Journal of Statistical Mechanics: Theory and Experiment, 2005, 2005, P02001 )
95 
96  for( int moduleID( 0 ); moduleID < numberOfModules; moduleID++ )
97  {
98  modularity += (((double) numberOfLinksInModule[ moduleID ]) / ((double) numberOfLinksInNetwork)) -
99  (
100  (((double) sumOfDegreesInModule[ moduleID ]) / ((double) 2 * numberOfLinksInNetwork) ) *
101  (((double) sumOfDegreesInModule[ moduleID ]) / ((double) 2 * numberOfLinksInNetwork) )
102  );
103  }
104 
105  return modularity;
106 }
107 
109  ToModuleMapType *vertexToModuleMap ) const
110 {
111  int maxModule( 0 );
112  auto iter = vertexToModuleMap->begin();
113  auto end = vertexToModuleMap->end();
114  while( iter != end )
115  {
116  if( iter->second > maxModule )
117  {
118  maxModule = iter->second;
119  }
120 
121  iter++;
122  }
123 
124  return maxModule + 1;
125 }
double Evaluate(mitk::ConnectomicsNetwork::Pointer network, ToModuleMapType *vertexToModuleMap) const
double CalculateModularity(mitk::ConnectomicsNetwork::Pointer network, ToModuleMapType *vertexToModuleMap) const
#define MBI_ERROR
Definition: mbilog.h:223