MNE-CPP  0.1.9
A Framework for Electrophysiology
network.cpp
Go to the documentation of this file.
1 //=============================================================================================================
36 //=============================================================================================================
37 // INCLUDES
38 //=============================================================================================================
39 
40 #include "network.h"
41 
42 #include "networkedge.h"
43 #include "networknode.h"
44 
45 #include <utils/spectral.h>
46 
47 #include <limits>
48 
49 //=============================================================================================================
50 // QT INCLUDES
51 //=============================================================================================================
52 
53 #include <QDebug>
54 #include <QList>
55 
56 //=============================================================================================================
57 // EIGEN INCLUDES
58 //=============================================================================================================
59 
60 //=============================================================================================================
61 // USED NAMESPACES
62 //=============================================================================================================
63 
64 using namespace CONNECTIVITYLIB;
65 using namespace Eigen;
66 using namespace UTILSLIB;
67 
68 //=============================================================================================================
69 // DEFINE GLOBAL METHODS
70 //=============================================================================================================
71 
72 //=============================================================================================================
73 // DEFINE MEMBER METHODS
74 //=============================================================================================================
75 
76 Network::Network(const QString& sConnectivityMethod,
77  double dThreshold)
78 : m_sConnectivityMethod(sConnectivityMethod)
79 , m_minMaxFullWeights(QPair<double,double>(std::numeric_limits<double>::max(),0.0))
80 , m_minMaxThresholdedWeights(QPair<double,double>(std::numeric_limits<double>::max(),0.0))
81 , m_dThreshold(dThreshold)
82 , m_fSFreq(0.0f)
83 , m_iFFTSize(128)
84 , m_iNumberFreqBins(0)
85 {
86  qRegisterMetaType<CONNECTIVITYLIB::Network>("CONNECTIVITYLIB::Network");
87  qRegisterMetaType<CONNECTIVITYLIB::Network::SPtr>("CONNECTIVITYLIB::Network::SPtr");
88  qRegisterMetaType<QList<CONNECTIVITYLIB::Network> >("QList<CONNECTIVITYLIB::Network>");
89  qRegisterMetaType<QList<CONNECTIVITYLIB::Network::SPtr> >("QList<CONNECTIVITYLIB::Network::SPtr>");
90 }
91 
92 //=============================================================================================================
93 
94 MatrixXd Network::getFullConnectivityMatrix(bool bGetMirroredVersion) const
95 {
96  MatrixXd matDist(m_lNodes.size(), m_lNodes.size());
97  matDist.setZero();
98 
99  for(int i = 0; i < m_lFullEdges.size(); ++i) {
100  int row = m_lFullEdges.at(i)->getStartNodeID();
101  int col = m_lFullEdges.at(i)->getEndNodeID();
102 
103  if(row < matDist.rows() && col < matDist.cols()) {
104  matDist(row,col) = m_lFullEdges.at(i)->getWeight();
105 
106  if(bGetMirroredVersion) {
107  matDist(col,row) = m_lFullEdges.at(i)->getWeight();
108  }
109  }
110  }
111 
112  //IOUtils::write_eigen_matrix(matDist,"eigen.txt");
113  return matDist;
114 }
115 
116 //=============================================================================================================
117 
118 MatrixXd Network::getThresholdedConnectivityMatrix(bool bGetMirroredVersion) const
119 {
120  MatrixXd matDist(m_lNodes.size(), m_lNodes.size());
121  matDist.setZero();
122 
123  for(int i = 0; i < m_lThresholdedEdges.size(); ++i) {
124  int row = m_lThresholdedEdges.at(i)->getStartNodeID();
125  int col = m_lThresholdedEdges.at(i)->getEndNodeID();
126 
127  if(row < matDist.rows() && col < matDist.cols()) {
128  matDist(row,col) = m_lThresholdedEdges.at(i)->getWeight();
129 
130  if(bGetMirroredVersion) {
131  matDist(col,row) = m_lThresholdedEdges.at(i)->getWeight();
132  }
133  }
134  }
135 
136  //IOUtils::write_eigen_matrix(matDist,"eigen.txt");
137  return matDist;
138 }
139 
140 //=============================================================================================================
141 
142 const QList<NetworkEdge::SPtr>& Network::getFullEdges() const
143 {
144  return m_lFullEdges;
145 }
146 
147 //=============================================================================================================
148 
149 const QList<NetworkEdge::SPtr>& Network::getThresholdedEdges() const
150 {
151  return m_lThresholdedEdges;
152 }
153 
154 //=============================================================================================================
155 
156 const QList<NetworkNode::SPtr>& Network::getNodes() const
157 {
158  return m_lNodes;
159 }
160 
161 //=============================================================================================================
162 
164 {
165  return m_lNodes.at(i);
166 }
167 
168 //=============================================================================================================
169 
171 {
172  qint16 distribution = 0;
173 
174  for(int i = 0; i < m_lNodes.size(); ++i) {
175  distribution += m_lNodes.at(i)->getFullDegree();
176  }
177 
178  return distribution;
179 }
180 
181 //=============================================================================================================
182 
184 {
185  qint16 distribution = 0;
186 
187  for(int i = 0; i < m_lNodes.size(); ++i) {
188  distribution += m_lNodes.at(i)->getThresholdedDegree();
189  }
190 
191  return distribution;
192 }
193 
194 //=============================================================================================================
195 
196 void Network::setConnectivityMethod(const QString& sConnectivityMethod)
197 {
198  m_sConnectivityMethod = sConnectivityMethod;
199 }
200 
201 //=============================================================================================================
202 
204 {
205  return m_sConnectivityMethod;
206 }
207 
208 //=============================================================================================================
209 
210 QPair<double, double> Network::getMinMaxFullWeights() const
211 {
212  return m_minMaxFullWeights;
213 }
214 
215 //=============================================================================================================
216 
217 QPair<double, double> Network::getMinMaxThresholdedWeights() const
218 {
220 }
221 
222 //=============================================================================================================
223 
224 QPair<int,int> Network::getMinMaxFullDegrees() const
225 {
226  int maxDegree = 0;
227  int minDegree = 1000000;
228 
229  for(int i = 0; i < m_lNodes.size(); ++i) {
230  if(m_lNodes.at(i)->getFullDegree() > maxDegree){
231  maxDegree = m_lNodes.at(i)->getFullDegree();
232  } else if (m_lNodes.at(i)->getFullDegree() < minDegree){
233  minDegree = m_lNodes.at(i)->getFullDegree();
234  }
235  }
236 
237  return QPair<int,int>(minDegree,maxDegree);
238 }
239 
240 //=============================================================================================================
241 
243 {
244  int maxDegree = 0;
245  int minDegree = 1000000;
246 
247  for(int i = 0; i < m_lNodes.size(); ++i) {
248  if(m_lNodes.at(i)->getThresholdedDegree() > maxDegree){
249  maxDegree = m_lNodes.at(i)->getThresholdedDegree();
250  } else if (m_lNodes.at(i)->getThresholdedDegree() < minDegree){
251  minDegree = m_lNodes.at(i)->getThresholdedDegree();
252  }
253  }
254 
255  return QPair<int,int>(minDegree,maxDegree);
256 }
257 
258 //=============================================================================================================
259 
260 QPair<int,int> Network::getMinMaxFullIndegrees() const
261 {
262  int maxDegree = 0;
263  int minDegree = 1000000;
264 
265  for(int i = 0; i < m_lNodes.size(); ++i) {
266  if(m_lNodes.at(i)->getFullIndegree() > maxDegree){
267  maxDegree = m_lNodes.at(i)->getFullIndegree();
268  } else if (m_lNodes.at(i)->getFullIndegree() < minDegree){
269  minDegree = m_lNodes.at(i)->getFullIndegree();
270  }
271  }
272 
273  return QPair<int,int>(minDegree,maxDegree);
274 }
275 
276 //=============================================================================================================
277 
279 {
280  int maxDegree = 0;
281  int minDegree = 1000000;
282 
283  for(int i = 0; i < m_lNodes.size(); ++i) {
284  if(m_lNodes.at(i)->getThresholdedIndegree() > maxDegree){
285  maxDegree = m_lNodes.at(i)->getThresholdedIndegree();
286  } else if (m_lNodes.at(i)->getThresholdedIndegree() < minDegree){
287  minDegree = m_lNodes.at(i)->getThresholdedIndegree();
288  }
289  }
290 
291  return QPair<int,int>(minDegree,maxDegree);
292 }
293 
294 //=============================================================================================================
295 
296 QPair<int,int> Network::getMinMaxFullOutdegrees() const
297 {
298  int maxDegree = 0;
299  int minDegree = 1000000;
300 
301  for(int i = 0; i < m_lNodes.size(); ++i) {
302  if(m_lNodes.at(i)->getFullOutdegree() > maxDegree){
303  maxDegree = m_lNodes.at(i)->getFullOutdegree();
304  } else if (m_lNodes.at(i)->getFullOutdegree() < minDegree){
305  minDegree = m_lNodes.at(i)->getFullOutdegree();
306  }
307  }
308 
309  return QPair<int,int>(minDegree,maxDegree);
310 }
311 
312 //=============================================================================================================
313 
315 {
316  int maxDegree = 0;
317  int minDegree = 1000000;
318 
319  for(int i = 0; i < m_lNodes.size(); ++i) {
320  if(m_lNodes.at(i)->getThresholdedOutdegree() > maxDegree){
321  maxDegree = m_lNodes.at(i)->getThresholdedOutdegree();
322  } else if (m_lNodes.at(i)->getThresholdedOutdegree() < minDegree){
323  minDegree = m_lNodes.at(i)->getThresholdedOutdegree();
324  }
325  }
326 
327  return QPair<int,int>(minDegree,maxDegree);
328 }
329 
330 //=============================================================================================================
331 
332 void Network::setThreshold(double dThreshold)
333 {
334  m_dThreshold = dThreshold;
335  m_lThresholdedEdges.clear();
336 
337  for(int i = 0; i < m_lFullEdges.size(); ++i) {
338  if(fabs(m_lFullEdges.at(i)->getWeight()) >= m_dThreshold) {
339  m_lFullEdges.at(i)->setActive(true);
340  m_lThresholdedEdges.append(m_lFullEdges.at(i));
341  } else {
342  m_lFullEdges.at(i)->setActive(false);
343  }
344  }
345 
348 }
349 
350 //=============================================================================================================
351 
353 {
354  return m_dThreshold;
355 }
356 
357 //=============================================================================================================
358 
359 void Network::setFrequencyRange(float fLowerFreq, float fUpperFreq)
360 {
361  if(fLowerFreq > fUpperFreq || fUpperFreq < fLowerFreq) {
362  qDebug() << "Network::setFrequencyRange - Upper and lower frequency are out of range from each other. Weights will not be recalculated. Returning.";
363  return;
364  }
365 
366  if(m_fSFreq <= 0.0f) {
367  qDebug() << "Network::setFrequencyRange - Sampling frequency has not been set. Returning.";
368  return;
369  }
370 
371  if(fUpperFreq > m_fSFreq/2.0f) {
372  qDebug() << "Network::setFrequencyRange - Upper frequency is bigger than nyquist frequency. Returning.";
373  return;
374  }
375 
376  if(m_iNumberFreqBins <= 0) {
377  qDebug() << "Network::setFrequencyRange - Number of samples has not been set. Returning.";
378  return;
379  }
380 
381  double dScaleFactor = m_iFFTSize/(m_fSFreq/2);
382 
383  m_minMaxFrequency.first = fLowerFreq;
384  m_minMaxFrequency.second = fUpperFreq;
385 
386  int iLowerBin = fLowerFreq * dScaleFactor;
387  int iUpperBin = fUpperFreq * dScaleFactor;
388 
389  // Update the min max values
390  m_minMaxFullWeights = QPair<double,double>(std::numeric_limits<double>::max(),0.0);
391 
392  for(int i = 0; i < m_lFullEdges.size(); ++i) {
393  m_lFullEdges.at(i)->setFrequencyBins(QPair<int,int>(iLowerBin,iUpperBin));
394 
395  if(fabs(m_lFullEdges.at(i)->getWeight()) < m_minMaxFullWeights.first) {
396  m_minMaxFullWeights.first = fabs(m_lFullEdges.at(i)->getWeight());
397  } else if(fabs(m_lFullEdges.at(i)->getWeight()) > m_minMaxFullWeights.second) {
398  m_minMaxFullWeights.second = fabs(m_lFullEdges.at(i)->getWeight());
399  }
400  }
401 }
402 
403 //=============================================================================================================
404 
405 const QPair<float,float>& Network::getFrequencyRange() const
406 {
407  return m_minMaxFrequency;
408 }
409 
410 //=============================================================================================================
411 
413 {
414  if(newEdge->getEndNodeID() != newEdge->getStartNodeID()) {
415  double dEdgeWeight = newEdge->getWeight();
416  if(dEdgeWeight < m_minMaxFullWeights.first) {
417  m_minMaxFullWeights.first = dEdgeWeight;
418  } else if(dEdgeWeight >= m_minMaxFullWeights.second) {
419  m_minMaxFullWeights.second = dEdgeWeight;
420  }
421 
422  m_lFullEdges << newEdge;
423 
424  if(fabs(newEdge->getWeight()) >= m_dThreshold) {
425  m_lThresholdedEdges << newEdge;
426  }
427  }
428 }
429 
430 //=============================================================================================================
431 
433 {
434  m_lNodes << newNode;
435 }
436 
437 //=============================================================================================================
438 
439 bool Network::isEmpty() const
440 {
441  if(m_lFullEdges.isEmpty() || m_lNodes.isEmpty()) {
442  return true;
443  }
444 
445  return false;
446 }
447 
448 //=============================================================================================================
449 
451 {
452  // Normalize full network
453  if(m_minMaxFullWeights.second == 0.0) {
454  qDebug() << "Network::normalize() - Max weight is 0. Returning.";
455  return;
456  }
457 
458  for(int i = 0; i < m_lFullEdges.size(); ++i) {
459  m_lFullEdges.at(i)->setWeight(m_lFullEdges.at(i)->getWeight()/m_minMaxFullWeights.second);
460  }
461 
463  m_minMaxFullWeights.second = 1.0;
464 
466  m_minMaxThresholdedWeights.second = 1.0;
467 }
468 
469 //=============================================================================================================
470 
472 {
473  return m_visualizationInfo;
474 }
475 
476 //=============================================================================================================
477 
478 void Network::setVisualizationInfo(const VisualizationInfo& visualizationInfo)
479 {
480  m_visualizationInfo = visualizationInfo;
481 }
482 
483 //=============================================================================================================
484 
486 {
487  return m_fSFreq;
488 }
489 
490 //=============================================================================================================
491 
493 {
494  m_fSFreq = fSFreq;
495 }
496 
497 //=============================================================================================================
498 
500 {
501  return m_iNumberFreqBins;
502 }
503 
504 //=============================================================================================================
505 
506 void Network::setUsedFreqBins(int iNumberFreqBins)
507 {
508  m_iNumberFreqBins = iNumberFreqBins;
509 }
510 
511 //=============================================================================================================
512 
513 void Network::setFFTSize(int iFFTSize)
514 {
515  m_iFFTSize = iFFTSize;
516 }
517 
518 //=============================================================================================================
519 
521 {
522  return m_iFFTSize;
523 }
524 
CONNECTIVITYLIB::Network::m_fSFreq
float m_fSFreq
Definition: network.h:411
spectral.h
Declaration of Spectral class.
CONNECTIVITYLIB::Network::getFFTSize
int getFFTSize()
Definition: network.cpp:520
CONNECTIVITYLIB::Network::getMinMaxFullDegrees
QPair< int, int > getMinMaxFullDegrees() const
Definition: network.cpp:224
networknode.h
NetworkNode class declaration.
network.h
Network class declaration.
CONNECTIVITYLIB::Network::setConnectivityMethod
void setConnectivityMethod(const QString &sConnectivityMethod)
Definition: network.cpp:196
CONNECTIVITYLIB::Network::isEmpty
bool isEmpty() const
Definition: network.cpp:439
CONNECTIVITYLIB::Network::getFrequencyRange
const QPair< float, float > & getFrequencyRange() const
Definition: network.cpp:405
CONNECTIVITYLIB::Network::append
void append(QSharedPointer< NetworkEdge > newEdge)
CONNECTIVITYLIB::Network::getUsedFreqBins
int getUsedFreqBins() const
Definition: network.cpp:499
CONNECTIVITYLIB::Network::getNodeAt
QSharedPointer< NetworkNode > getNodeAt(int i)
Definition: network.cpp:163
CONNECTIVITYLIB::Network::getFullConnectivityMatrix
Eigen::MatrixXd getFullConnectivityMatrix(bool bGetMirroredVersion=true) const
Definition: network.cpp:94
CONNECTIVITYLIB::Network::getMinMaxFullWeights
QPair< double, double > getMinMaxFullWeights() const
Definition: network.cpp:210
CONNECTIVITYLIB::Network::getThreshold
double getThreshold()
Definition: network.cpp:352
CONNECTIVITYLIB::Network::getFullEdges
const QList< QSharedPointer< NetworkEdge > > & getFullEdges() const
Definition: network.cpp:142
CONNECTIVITYLIB::Network::normalize
void normalize()
Definition: network.cpp:450
CONNECTIVITYLIB::Network::m_visualizationInfo
VisualizationInfo m_visualizationInfo
Definition: network.h:415
CONNECTIVITYLIB::NetworkEdge::SPtr
QSharedPointer< NetworkEdge > SPtr
Definition: networkedge.h:83
CONNECTIVITYLIB::Network::getMinMaxThresholdedOutdegrees
QPair< int, int > getMinMaxThresholdedOutdegrees() const
Definition: network.cpp:314
CONNECTIVITYLIB::Network::m_lNodes
QList< QSharedPointer< NetworkNode > > m_lNodes
Definition: network.h:400
CONNECTIVITYLIB::Network::getMinMaxFullIndegrees
QPair< int, int > getMinMaxFullIndegrees() const
Definition: network.cpp:260
CONNECTIVITYLIB::Network::m_minMaxFullWeights
QPair< double, double > m_minMaxFullWeights
Definition: network.h:406
CONNECTIVITYLIB::Network::setSamplingFrequency
void setSamplingFrequency(float fSFreq)
Definition: network.cpp:492
CONNECTIVITYLIB::Network::getMinMaxThresholdedDegrees
QPair< int, int > getMinMaxThresholdedDegrees() const
Definition: network.cpp:242
CONNECTIVITYLIB::Network::getNodes
const QList< QSharedPointer< NetworkNode > > & getNodes() const
Definition: network.cpp:156
CONNECTIVITYLIB::Network::m_dThreshold
double m_dThreshold
Definition: network.h:410
CONNECTIVITYLIB::Network::setUsedFreqBins
void setUsedFreqBins(int iNumberFreqBins)
Definition: network.cpp:506
CONNECTIVITYLIB::Network::m_lThresholdedEdges
QList< QSharedPointer< NetworkEdge > > m_lThresholdedEdges
Definition: network.h:398
CONNECTIVITYLIB::Network::getMinMaxThresholdedIndegrees
QPair< int, int > getMinMaxThresholdedIndegrees() const
Definition: network.cpp:278
CONNECTIVITYLIB::Network::Network
Network(const QString &sConnectivityMethod="Unknown", double dThreshold=0.0)
Definition: network.cpp:76
CONNECTIVITYLIB::Network::setFrequencyRange
void setFrequencyRange(float fLowerFreq, float fUpperFreq)
Definition: network.cpp:359
CONNECTIVITYLIB::Network::getSamplingFrequency
float getSamplingFrequency() const
Definition: network.cpp:485
CONNECTIVITYLIB::Network::setFFTSize
void setFFTSize(int iFFTSize)
Definition: network.cpp:513
CONNECTIVITYLIB::VisualizationInfo
Definition: network.h:74
CONNECTIVITYLIB::Network::m_iNumberFreqBins
int m_iNumberFreqBins
Definition: network.h:412
CONNECTIVITYLIB::Network::getConnectivityMethod
QString getConnectivityMethod() const
Definition: network.cpp:203
CONNECTIVITYLIB::Network::setThreshold
void setThreshold(double dThreshold=0.0)
Definition: network.cpp:332
CONNECTIVITYLIB::Network::getFullDistribution
qint16 getFullDistribution() const
Definition: network.cpp:170
CONNECTIVITYLIB::Network::getMinMaxThresholdedWeights
QPair< double, double > getMinMaxThresholdedWeights() const
Definition: network.cpp:217
CONNECTIVITYLIB::Network::m_sConnectivityMethod
QString m_sConnectivityMethod
Definition: network.h:404
CONNECTIVITYLIB::Network::getThresholdedDistribution
qint16 getThresholdedDistribution() const
Definition: network.cpp:183
CONNECTIVITYLIB::Network::setVisualizationInfo
void setVisualizationInfo(const VisualizationInfo &visualizationInfo)
Definition: network.cpp:478
CONNECTIVITYLIB::Network::m_minMaxThresholdedWeights
QPair< double, double > m_minMaxThresholdedWeights
Definition: network.h:407
CONNECTIVITYLIB::Network::getThresholdedEdges
const QList< QSharedPointer< NetworkEdge > > & getThresholdedEdges() const
Definition: network.cpp:149
CONNECTIVITYLIB::Network::m_minMaxFrequency
QPair< float, float > m_minMaxFrequency
Definition: network.h:408
networkedge.h
NetworkEdge class declaration.
std
Definition: event.h:282
CONNECTIVITYLIB::Network::m_iFFTSize
int m_iFFTSize
Definition: network.h:413
CONNECTIVITYLIB::NetworkNode::SPtr
QSharedPointer< NetworkNode > SPtr
Definition: networknode.h:85
CONNECTIVITYLIB::Network::m_lFullEdges
QList< QSharedPointer< NetworkEdge > > m_lFullEdges
Definition: network.h:397
CONNECTIVITYLIB::Network::getThresholdedConnectivityMatrix
Eigen::MatrixXd getThresholdedConnectivityMatrix(bool bGetMirroredVersion=true) const
Definition: network.cpp:118
CONNECTIVITYLIB::Network::getMinMaxFullOutdegrees
QPair< int, int > getMinMaxFullOutdegrees() const
Definition: network.cpp:296
CONNECTIVITYLIB::Network::getVisualizationInfo
VisualizationInfo getVisualizationInfo() const
Definition: network.cpp:471