Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkConnectomicsNetworkThresholder.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 
19 #include "vnl/vnl_random.h"
20 
22  : m_Network( nullptr )
23  , m_ThresholdingScheme( mitk::ConnectomicsNetworkThresholder::ThresholdBased )
24  , m_TargetThreshold( 0.0 )
25  , m_TargetDensity( 1.0 )
26 {
27 }
28 
30 {
31 }
32 
34 {
36 
37  if( ! CheckValidity() )
38  {
39  MITK_ERROR << "Aborting";
40  return m_Network;
41  }
42 
43  switch(m_ThresholdingScheme)
44  {
45  case RandomRemovalOfWeakest :
46  {
47  result = ThresholdByRandomRemoval( m_Network, m_TargetDensity );
48  break;
49  }
50  case LargestLowerThanDensity :
51  {
52  result = ThresholdBelowDensity( m_Network, m_TargetDensity );
53  break;
54  }
55  case ThresholdBased :
56  {
57  result = Threshold( m_Network, m_TargetThreshold );
58  break;
59  }
60  default :
61  {
62  MITK_ERROR << "Specified unknown Thresholding Scheme";
63  result = m_Network;
64  }
65  }
66 
67  return result;
68 }
69 
71 {
72  bool valid(true);
73 
74  if( m_Network.IsNull() )
75  {
76  valid = false;
77  MITK_ERROR << "Network is NULL.";
78  }
79 
80  switch(m_ThresholdingScheme)
81  {
82  case RandomRemovalOfWeakest :
83  case LargestLowerThanDensity :
84  {
85  if( m_TargetDensity < 0.0 )
86  {
87  valid = false;
88  MITK_ERROR << "Target density is negative.";
89  }
90  if( m_TargetDensity > 1.0 )
91  {
92  valid = false;
93  MITK_ERROR << "Target density is larger than 1.";
94  }
95  break;
96  }
97  case ThresholdBased :
98  {
99  if( m_TargetThreshold < 0 )
100  {
101  valid = false;
102  MITK_ERROR << "Target threshold is negative.";
103  }
104  break;
105  }
106  default :
107  {
108  valid = false;
109  MITK_ERROR << "Specified unknown Thresholding Scheme";
110  }
111  }
112 
113  return valid;
114 }
115 
117 {
119  result->ImportNetwort( input );
120 
122 
123  calculator->SetNetwork( result );
124  calculator->Update();
125 
126  //the random number generator
127  vnl_random rng( (unsigned int) rand() );
128 
129  bool notBelow( targetDensity < calculator->GetConnectionDensity() );
130 
131  double minWeight( result->GetMaximumWeight() );
132  int count( 0 );
133 
134  while( notBelow )
135  {
136  // result = Threshold( result, loop );
137  std::vector< EdgeDescriptorType > candidateVector;
138  minWeight = result->GetMaximumWeight();
139  count = 0;
140 
141  // determine minimum weight and number of edges having that weight
142  NetworkType* boostGraph = result->GetBoostGraph();
143  EdgeIteratorType iterator, end;
144  // sets iterator to start end end to end
145  boost::tie(iterator, end) = boost::edges( *boostGraph );
146 
147  for ( ; iterator != end; ++iterator)
148  {
149  double tempWeight;
150 
151  // the value of an iterator is a descriptor
152  tempWeight = (*boostGraph)[ *iterator ].weight;
153 
154  if( mitk::Equal( tempWeight, minWeight ) )
155  {
156  candidateVector.push_back( *iterator );
157  count++;
158  }
159  else if( tempWeight < minWeight )
160  {
161  candidateVector.clear();
162  candidateVector.push_back( *iterator );
163  minWeight = tempWeight;
164  count = 1;
165  }
166  }
167 
168  // Which to delete
169  int deleteNumber( rng.lrand32( count - 1 ) );
170 
171  boost::remove_edge( candidateVector.at( deleteNumber ), *boostGraph );
172 
173  calculator->SetNetwork( result );
174  calculator->Update();
175  notBelow = targetDensity < calculator->GetConnectionDensity();
176  }
177 
178  result->UpdateIDs();
179  return result;
180 }
181 
183 {
185  result->ImportNetwort( input );
186 
188 
189  calculator->SetNetwork( result );
190  calculator->Update();
191 
192  bool notBelow( targetDensity < calculator->GetConnectionDensity() );
193 
194  for( int loop( 1 ); notBelow; loop++ )
195  {
196  result = Threshold( result, loop );
197  calculator->SetNetwork( result );
198  calculator->Update();
199  notBelow = targetDensity < calculator->GetConnectionDensity();
200  }
201 
202  return result;
203 }
204 
206 {
208  result->ImportNetwort( input );
209 
210  NetworkType* boostGraph = result->GetBoostGraph();
211 
212  EdgeIteratorType iterator, end;
213 
214  // set to true if iterators are invalidated by deleting an edge
215  bool edgeHasBeenRemoved( true );
216 
217  // if no edge has been removed in the last loop, we are done
218  while( edgeHasBeenRemoved )
219  {
220  edgeHasBeenRemoved = false;
221  // sets iterator to start and end to end
222  boost::tie(iterator, end) = boost::edges( *boostGraph );
223 
224  bool endReached( false );
225  while( !edgeHasBeenRemoved && !endReached )
226  {
227  if( iterator != end )
228  {
229  // If the edge is below the target threshold it is deleted
230  if( (*boostGraph)[ *iterator ].weight < targetThreshold )
231  {
232  edgeHasBeenRemoved = true;
233  // this invalidates all iterators
234  boost::remove_edge( *iterator, *boostGraph );
235  }
236  else
237  {
238  ++iterator;
239  }
240  }
241  else
242  {
243  endReached = true;
244  }
245  }
246  }
247 
248  result->UpdateIDs();
249 
250  return result;
251 }
itk::SmartPointer< Self > Pointer
boost::graph_traits< NetworkType >::edge_iterator EdgeIteratorType
mitk::ConnectomicsNetwork::Pointer ThresholdBelowDensity(mitk::ConnectomicsNetwork::Pointer input, double targetDensity)
#define MITK_ERROR
Definition: mitkLogMacros.h:24
mitk::ConnectomicsNetwork::Pointer ThresholdByRandomRemoval(mitk::ConnectomicsNetwork::Pointer input, double targetDensity)
DataCollection - Class to facilitate loading/accessing structured data.
mitk::ConnectomicsNetwork::NetworkType NetworkType
mitk::ConnectomicsNetwork::Pointer GetThresholdedNetwork()
Apply thresholding scheme and get resulting network.
static Pointer New()
mitk::ConnectomicsNetwork::Pointer Threshold(mitk::ConnectomicsNetwork::Pointer input, double targetThreshold)
MITKNEWMODULE_EXPORT bool Equal(mitk::ExampleDataStructure *leftHandSide, mitk::ExampleDataStructure *rightHandSide, mitk::ScalarType eps, bool verbose)
Returns true if the example data structures are considered equal.
A class for thresholding connectomics networks.
itk::SmartPointer< Self > Pointer
Definition: mitkBaseData.h:42