26 #include <boost/algorithm/string.hpp>
29 #include <itkImageFileWriter.h>
40 int main(
int argc,
char* argv[])
65 parser.
setTitle(
"Network Statistics");
69 std::map<std::string, us::Any> parsedArgs = parser.
parseArguments(argc, argv);
70 if (parsedArgs.size()==0)
74 bool noGlobalStatistics(
false );
75 bool binaryConnectivity(
false );
76 bool rescaleConnectivity(
false );
77 bool createConnectivityMatriximage(
false );
79 double startDensity( 1.0 );
80 int thresholdStepSize( 3 );
84 std::string networkName =
us::any_cast<std::string>(parsedArgs[
"inputNetwork"]);
85 std::string outName =
us::any_cast<std::string>(parsedArgs[
"outputFile"]);
89 if(parsedArgs.count(
"localStatistics"))
95 std::map< std::string, std::vector<std::string> > parsedRegions;
96 std::map< std::string, std::vector<std::string> >::iterator parsedRegionsIterator;
98 if(parsedArgs.count(
"regionList"))
102 for(
unsigned int index(0); index < unparsedRegions.size(); index++ )
104 std::vector< std::string > tempRegionVector;
106 boost::split(tempRegionVector, unparsedRegions.at(index), boost::is_any_of(
";"));
108 std::vector< std::string >::const_iterator begin = tempRegionVector.begin();
109 std::vector< std::string >::const_iterator last = tempRegionVector.begin() + tempRegionVector.size();
110 std::vector< std::string > insertRegionVector(begin + 1, last);
112 if( parsedRegions.count( tempRegionVector.at(0) ) == 0 )
114 parsedRegions.insert( std::pair< std::string, std::vector<std::string> >( tempRegionVector.at(0), insertRegionVector) );
118 MITK_ERROR <<
"Region already exists. Skipping second occurrence.";
123 if (parsedArgs.count(
"noGlobalStatistics"))
124 noGlobalStatistics = us::any_cast<bool>(parsedArgs[
"noGlobalStatistics"]);
125 if (parsedArgs.count(
"binaryConnectivity"))
126 binaryConnectivity = us::any_cast<bool>(parsedArgs[
"binaryConnectivity"]);
127 if (parsedArgs.count(
"rescaleConnectivity"))
128 rescaleConnectivity = us::any_cast<bool>(parsedArgs[
"rescaleConnectivity"]);
129 if (parsedArgs.count(
"createConnectivityMatriximage"))
130 createConnectivityMatriximage = us::any_cast<bool>(parsedArgs[
"createConnectivityMatriximage"]);
131 if (parsedArgs.count(
"granularity"))
132 granularity = us::any_cast<int>(parsedArgs[
"granularity"]);
133 if (parsedArgs.count(
"startDensity"))
134 startDensity = us::any_cast<float>(parsedArgs[
"startDensity"]);
135 if (parsedArgs.count(
"thresholdStepSize"))
136 thresholdStepSize = us::any_cast<int>(parsedArgs[
"thresholdStepSize"]);
141 std::vector<mitk::BaseData::Pointer> networkFile =
143 if( networkFile.empty() )
145 std::string errorMessage =
"File at " + networkName +
" could not be read. Aborting.";
154 std::string errorMessage =
"Read file at " + networkName +
" could not be recognized as network. Aborting.";
160 std::stringstream globalHeaderStream;
161 globalHeaderStream <<
"NumberOfVertices "
164 <<
"ConnectionDensity "
165 <<
"NumberOfConnectedComponents "
166 <<
"AverageComponentSize "
167 <<
"LargestComponentSize "
168 <<
"RatioOfNodesInLargestComponent "
169 <<
"HopPlotExponent "
170 <<
"EffectiveHopDiameter "
171 <<
"AverageClusteringCoefficientsC "
172 <<
"AverageClusteringCoefficientsD "
173 <<
"AverageClusteringCoefficientsE "
174 <<
"AverageVertexBetweennessCentrality "
175 <<
"AverageEdgeBetweennessCentrality "
176 <<
"NumberOfIsolatedPoints "
177 <<
"RatioOfIsolatedPoints "
178 <<
"NumberOfEndPoints "
179 <<
"RatioOfEndPoints "
184 <<
"AverageEccentricity "
185 <<
"AverageEccentricity90 "
186 <<
"AveragePathLength "
187 <<
"NumberOfCentralPoints "
188 <<
"RatioOfCentralPoints "
190 <<
"SecondLargestEigenValue "
192 <<
"AdjacencyEnergy "
194 <<
"LaplacianEnergy "
195 <<
"LaplacianSpectralGap "
196 <<
"NormalizedLaplacianTrace "
197 <<
"NormalizedLaplacianEnergy "
198 <<
"NormalizedLaplacianNumberOf2s "
199 <<
"NormalizedLaplacianNumberOf1s "
200 <<
"NormalizedLaplacianNumberOf0s "
201 <<
"NormalizedLaplacianLowerSlope "
202 <<
"NormalizedLaplacianUpperSlope "
206 std::stringstream localHeaderStream;
207 std::stringstream regionalHeaderStream;
209 std::stringstream globalDataStream;
210 std::stringstream localDataStream;
211 std::stringstream regionalDataStream;
213 std::string globalOutName = outName +
"_global.txt";
214 std::string localOutName = outName +
"_local.txt";
215 std::string regionalOutName = outName +
"_regional.txt";
217 bool firstRun(
true );
219 for(
unsigned int method( 0 ); method < 3; method++)
226 for(
unsigned int step( 0 ); step < granularity; step++ )
228 double targetValue( 0.0 );
229 bool newStep(
true );
235 targetValue = startDensity * (1 -
static_cast<double>( step ) / ( granularity + 0.5 ) );
238 targetValue =
static_cast<double>( thresholdStepSize * step );
241 MITK_ERROR <<
"Invalid thresholding method called, aborting.";
247 thresholder->SetNetwork( network );
248 thresholder->SetTargetThreshold( targetValue );
249 thresholder->SetTargetDensity( targetValue );
250 thresholder->SetThresholdingScheme( static_cast<mitk::ConnectomicsNetworkThresholder::ThresholdingSchemes>(method) );
254 statisticsCalculator->SetNetwork( thresholdedNetwork );
255 statisticsCalculator->Update();
258 if( !noGlobalStatistics )
260 globalDataStream << statisticsCalculator->GetNumberOfVertices() <<
" "
261 << statisticsCalculator->GetNumberOfEdges() <<
" "
262 << statisticsCalculator->GetAverageDegree() <<
" "
263 << statisticsCalculator->GetConnectionDensity() <<
" "
264 << statisticsCalculator->GetNumberOfConnectedComponents() <<
" "
265 << statisticsCalculator->GetAverageComponentSize() <<
" "
266 << statisticsCalculator->GetLargestComponentSize() <<
" "
267 << statisticsCalculator->GetRatioOfNodesInLargestComponent() <<
" "
268 << statisticsCalculator->GetHopPlotExponent() <<
" "
269 << statisticsCalculator->GetEffectiveHopDiameter() <<
" "
270 << statisticsCalculator->GetAverageClusteringCoefficientsC() <<
" "
271 << statisticsCalculator->GetAverageClusteringCoefficientsD() <<
" "
272 << statisticsCalculator->GetAverageClusteringCoefficientsE() <<
" "
273 << statisticsCalculator->GetAverageVertexBetweennessCentrality() <<
" "
274 << statisticsCalculator->GetAverageEdgeBetweennessCentrality() <<
" "
275 << statisticsCalculator->GetNumberOfIsolatedPoints() <<
" "
276 << statisticsCalculator->GetRatioOfIsolatedPoints() <<
" "
277 << statisticsCalculator->GetNumberOfEndPoints() <<
" "
278 << statisticsCalculator->GetRatioOfEndPoints() <<
" "
279 << statisticsCalculator->GetDiameter() <<
" "
280 << statisticsCalculator->GetDiameter90() <<
" "
281 << statisticsCalculator->GetRadius() <<
" "
282 << statisticsCalculator->GetRadius90() <<
" "
283 << statisticsCalculator->GetAverageEccentricity() <<
" "
284 << statisticsCalculator->GetAverageEccentricity90() <<
" "
285 << statisticsCalculator->GetAveragePathLength() <<
" "
286 << statisticsCalculator->GetNumberOfCentralPoints() <<
" "
287 << statisticsCalculator->GetRatioOfCentralPoints() <<
" "
288 << statisticsCalculator->GetSpectralRadius() <<
" "
289 << statisticsCalculator->GetSecondLargestEigenValue() <<
" "
290 << statisticsCalculator->GetAdjacencyTrace() <<
" "
291 << statisticsCalculator->GetAdjacencyEnergy() <<
" "
292 << statisticsCalculator->GetLaplacianTrace() <<
" "
293 << statisticsCalculator->GetLaplacianEnergy() <<
" "
294 << statisticsCalculator->GetLaplacianSpectralGap() <<
" "
295 << statisticsCalculator->GetNormalizedLaplacianTrace() <<
" "
296 << statisticsCalculator->GetNormalizedLaplacianEnergy() <<
" "
297 << statisticsCalculator->GetNormalizedLaplacianNumberOf2s() <<
" "
298 << statisticsCalculator->GetNormalizedLaplacianNumberOf1s() <<
" "
299 << statisticsCalculator->GetNormalizedLaplacianNumberOf0s() <<
" "
300 << statisticsCalculator->GetNormalizedLaplacianLowerSlope() <<
" "
301 << statisticsCalculator->GetNormalizedLaplacianUpperSlope() <<
" "
302 << statisticsCalculator->GetSmallWorldness()
308 if( createConnectivityMatriximage )
310 std::string connectivity_png_postfix =
"_connectivity";
311 if( binaryConnectivity )
313 connectivity_png_postfix +=
"_binary";
315 else if( rescaleConnectivity )
317 connectivity_png_postfix +=
"_rescaled";
319 connectivity_png_postfix +=
".png";
325 filter->SetInputNetwork( network );
326 filter->SetBinaryConnectivity( binaryConnectivity );
327 filter->SetRescaleConnectivity( rescaleConnectivity );
334 connectivityWriter->SetInput( filter->GetOutput() );
335 connectivityWriter->SetFileName( outName + connectivity_png_postfix);
336 connectivityWriter->Update();
338 std::cout <<
"Connectivity matrix image written.";
346 std::map< std::string, int > labelToIdMap;
347 std::vector< mitk::ConnectomicsNetwork::NetworkNode > nodeVector = thresholdedNetwork->GetVectorOfAllNodes();
348 for(
int loop(0); loop < nodeVector.size(); loop++)
350 labelToIdMap.insert( std::pair< std::string, int>(nodeVector.at(loop).label, nodeVector.at(loop).id) );
353 std::vector< int > degreeVector = thresholdedNetwork->GetDegreeOfNodes();
354 std::vector< double > ccVector = thresholdedNetwork->GetLocalClusteringCoefficients( );
355 std::vector< double > bcVector = thresholdedNetwork->GetNodeBetweennessVector( );
364 localHeaderStream <<
"Th_method " <<
"Th_target " <<
"density";
367 double density = statisticsCalculator->GetConnectionDensity();
369 localDataStream <<
"\n"
371 << targetValue <<
" "
374 for(
unsigned int loop(0); loop < localLabels.size(); loop++ )
380 localHeaderStream <<
" "
381 << localLabels.at( loop ) <<
"_Degree "
382 << localLabels.at( loop ) <<
"_CC "
383 << localLabels.at( loop ) <<
"_BC";
386 localDataStream <<
" " << degreeVector.at( labelToIdMap.find( localLabels.at( loop ) )->second )
387 <<
" " << ccVector.at( labelToIdMap.find( localLabels.at( loop ) )->second )
388 <<
" " << bcVector.at( labelToIdMap.find( localLabels.at( loop ) )->second );
392 MITK_ERROR <<
"Illegal label. Label: \"" << localLabels.at( loop ) <<
"\" not found.";
403 regionalHeaderStream <<
"Th_method " <<
"Th_target " <<
"density";
406 double density = statisticsCalculator->GetConnectionDensity();
408 regionalDataStream <<
"\n"
410 << targetValue <<
" "
413 for( parsedRegionsIterator = parsedRegions.begin(); parsedRegionsIterator != parsedRegions.end(); parsedRegionsIterator++ )
415 std::vector<std::string> regionLabelsVector = parsedRegionsIterator->second;
416 std::string regionName = parsedRegionsIterator->first;
418 double sumDegree( 0 );
423 for(
int loop(0); loop < regionLabelsVector.size(); loop++ )
425 if( thresholdedNetwork->CheckForLabel(regionLabelsVector.at( loop )) )
427 sumDegree = sumDegree + degreeVector.at( labelToIdMap.find( regionLabelsVector.at( loop ) )->second );
428 sumCC = sumCC + ccVector.at( labelToIdMap.find( regionLabelsVector.at( loop ) )->second );
429 sumBC = sumBC + bcVector.at( labelToIdMap.find( regionLabelsVector.at( loop ) )->second );
434 MITK_ERROR <<
"Illegal label. Label: \"" << regionLabelsVector.at( loop ) <<
"\" not found.";
441 regionalHeaderStream <<
" " << regionName <<
"_LocalAverageDegree "
442 << regionName <<
"_LocalAverageCC "
443 << regionName <<
"_LocalAverageBC "
444 << regionName <<
"_NumberOfNodes";
447 regionalDataStream <<
" " << sumDegree / count
448 <<
" " << sumCC / count
449 <<
" " << sumBC / count
453 std::map< std::string, std::vector<std::string> >::iterator loopRegionsIterator;
454 for (loopRegionsIterator = parsedRegionsIterator; loopRegionsIterator != parsedRegions.end(); loopRegionsIterator++)
456 int numberConnections(0), possibleConnections(0);
457 double summedFiberCount(0.0);
458 std::vector<std::string> loopLabelsVector = loopRegionsIterator->second;
459 std::string loopName = loopRegionsIterator->first;
461 for (
int loop(0); loop < regionLabelsVector.size(); loop++)
463 if (thresholdedNetwork->CheckForLabel(regionLabelsVector.at(loop)))
465 for (
int innerLoop(0); innerLoop < loopLabelsVector.size(); innerLoop++)
467 if (thresholdedNetwork->CheckForLabel(loopLabelsVector.at(loop)))
469 bool exists = thresholdedNetwork->EdgeExists(
470 labelToIdMap.find(regionLabelsVector.at(loop))->second,
471 labelToIdMap.find(loopLabelsVector.at(innerLoop))->second);
472 possibleConnections++;
476 summedFiberCount += thresholdedNetwork->GetEdge(
477 labelToIdMap.find(regionLabelsVector.at(loop))->second,
478 labelToIdMap.find(loopLabelsVector.at(innerLoop))->second).weight;
483 MITK_ERROR <<
"Illegal label. Label: \"" << loopLabelsVector.at(loop) <<
"\" not found.";
489 MITK_ERROR <<
"Illegal label. Label: \"" << regionLabelsVector.at(loop) <<
"\" not found.";
495 regionalHeaderStream <<
" " << regionName <<
"_" << loopName <<
"_Connections "
496 <<
" " << regionName <<
"_" << loopName <<
"_possibleConnections "
497 <<
" " << regionName <<
"_" << loopName <<
"_ConnectingFibers";
500 regionalDataStream <<
" " << numberConnections
501 <<
" " << possibleConnections
502 <<
" " << summedFiberCount;
513 if( !noGlobalStatistics )
515 std::cout <<
"Writing to " << globalOutName;
516 std::ofstream glocalOutFile( globalOutName.c_str(), ios::out );
517 if( ! glocalOutFile.is_open() )
519 std::string errorMessage =
"Could not open " + globalOutName +
" for writing.";
523 glocalOutFile << globalHeaderStream.str() << globalDataStream.str();
524 glocalOutFile.close();
527 if( localLabels.size() > 0 )
529 std::cout <<
"Writing to " << localOutName;
530 std::ofstream localOutFile( localOutName.c_str(), ios::out );
531 if( ! localOutFile.is_open() )
533 std::string errorMessage =
"Could not open " + localOutName +
" for writing.";
537 localOutFile << localHeaderStream.str() << localDataStream.str();
538 localOutFile.close();
541 if( parsedRegions.size() > 0 )
543 std::cout <<
"Writing to " << regionalOutName;
544 std::ofstream regionalOutFile( regionalOutName.c_str(), ios::out );
545 if( ! regionalOutFile.is_open() )
547 std::string errorMessage =
"Could not open " + regionalOutName +
" for writing.";
551 regionalOutFile << regionalHeaderStream.str() << regionalDataStream.str();
552 regionalOutFile.close();
557 catch (itk::ExceptionObject e)
562 catch (std::exception e)
564 std::cout << e.what();
569 std::cout <<
"ERROR!?!";
itk::SmartPointer< Self > Pointer
bool CheckForLabel(std::string targetLabel) const
Base of all data objects.
void setContributor(std::string contributor)
ValueType * any_cast(Any *operand)
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
itk::Image< unsigned short, 2 > OutputImageType
void addArgument(const std::string &longarg, const std::string &shortarg, Type type, const std::string &argLabel, const std::string &argHelp=std::string(), const us::Any &defaultValue=us::Any(), bool optional=true, bool ignoreRest=false, bool deprecated=false)
void setCategory(std::string category)
int main(int argc, char *argv[])
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
std::vector< std::string > StringContainerType
static DataStorage::SetOfObjects::Pointer Load(const std::string &path, DataStorage &storage)
Load a file into the given DataStorage.
void setTitle(std::string title)
Connectomics Network Class.
void setDescription(std::string description)
static std::vector< std::string > & split(const std::string &s, char delim, std::vector< std::string > &elems)
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.