v2.0.0
Loading...
Searching...
No Matches
network.cpp
Go to the documentation of this file.
1//=============================================================================================================
35
36//=============================================================================================================
37// INCLUDES
38//=============================================================================================================
39
40#include "network.h"
41
42#include "networkedge.h"
43#include "networknode.h"
44
45#include <math/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
64using namespace CONNLIB;
65using namespace Eigen;
66using namespace UTILSLIB;
67
68//=============================================================================================================
69// DEFINE GLOBAL METHODS
70//=============================================================================================================
71
72//=============================================================================================================
73// DEFINE MEMBER METHODS
74//=============================================================================================================
75
76Network::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)
85{
86 qRegisterMetaType<CONNLIB::Network>("CONNLIB::Network");
87 qRegisterMetaType<CONNLIB::Network::SPtr>("CONNLIB::Network::SPtr");
88 qRegisterMetaType<QList<CONNLIB::Network> >("QList<CONNLIB::Network>");
89 qRegisterMetaType<QList<CONNLIB::Network::SPtr> >("QList<CONNLIB::Network::SPtr>");
90}
91
92//=============================================================================================================
93
94MatrixXd 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
118MatrixXd 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
142const QList<NetworkEdge::SPtr>& Network::getFullEdges() const
143{
144 return m_lFullEdges;
145}
146
147//=============================================================================================================
148
149const QList<NetworkEdge::SPtr>& Network::getThresholdedEdges() const
150{
151 return m_lThresholdedEdges;
152}
153
154//=============================================================================================================
155
156const 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 return m_lFullEdges.at(i);
173}
174
175//=============================================================================================================
176
178{
179 qint16 distribution = 0;
180
181 for(int i = 0; i < m_lNodes.size(); ++i) {
182 distribution += m_lNodes.at(i)->getFullDegree();
183 }
184
185 return distribution;
186}
187
188//=============================================================================================================
189
191{
192 qint16 distribution = 0;
193
194 for(int i = 0; i < m_lNodes.size(); ++i) {
195 distribution += m_lNodes.at(i)->getThresholdedDegree();
196 }
197
198 return distribution;
199}
200
201//=============================================================================================================
202
203void Network::setConnectivityMethod(const QString& sConnectivityMethod)
204{
205 m_sConnectivityMethod = sConnectivityMethod;
206}
207
208//=============================================================================================================
209
211{
213}
214
215//=============================================================================================================
216
217QPair<double, double> Network::getMinMaxFullWeights() const
218{
219 return m_minMaxFullWeights;
220}
221
222//=============================================================================================================
223
224QPair<double, double> Network::getMinMaxThresholdedWeights() const
225{
227}
228
229//=============================================================================================================
230
231QPair<int,int> Network::getMinMaxFullDegrees() const
232{
233 int maxDegree = 0;
234 int minDegree = 1000000;
235
236 for(int i = 0; i < m_lNodes.size(); ++i) {
237 if(m_lNodes.at(i)->getFullDegree() > maxDegree){
238 maxDegree = m_lNodes.at(i)->getFullDegree();
239 } else if (m_lNodes.at(i)->getFullDegree() < minDegree){
240 minDegree = m_lNodes.at(i)->getFullDegree();
241 }
242 }
243
244 return QPair<int,int>(minDegree,maxDegree);
245}
246
247//=============================================================================================================
248
250{
251 int maxDegree = 0;
252 int minDegree = 1000000;
253
254 for(int i = 0; i < m_lNodes.size(); ++i) {
255 if(m_lNodes.at(i)->getThresholdedDegree() > maxDegree){
256 maxDegree = m_lNodes.at(i)->getThresholdedDegree();
257 } else if (m_lNodes.at(i)->getThresholdedDegree() < minDegree){
258 minDegree = m_lNodes.at(i)->getThresholdedDegree();
259 }
260 }
261
262 return QPair<int,int>(minDegree,maxDegree);
263}
264
265//=============================================================================================================
266
268{
269 int maxDegree = 0;
270 int minDegree = 1000000;
271
272 for(int i = 0; i < m_lNodes.size(); ++i) {
273 if(m_lNodes.at(i)->getFullIndegree() > maxDegree){
274 maxDegree = m_lNodes.at(i)->getFullIndegree();
275 } else if (m_lNodes.at(i)->getFullIndegree() < minDegree){
276 minDegree = m_lNodes.at(i)->getFullIndegree();
277 }
278 }
279
280 return QPair<int,int>(minDegree,maxDegree);
281}
282
283//=============================================================================================================
284
286{
287 int maxDegree = 0;
288 int minDegree = 1000000;
289
290 for(int i = 0; i < m_lNodes.size(); ++i) {
291 if(m_lNodes.at(i)->getThresholdedIndegree() > maxDegree){
292 maxDegree = m_lNodes.at(i)->getThresholdedIndegree();
293 } else if (m_lNodes.at(i)->getThresholdedIndegree() < minDegree){
294 minDegree = m_lNodes.at(i)->getThresholdedIndegree();
295 }
296 }
297
298 return QPair<int,int>(minDegree,maxDegree);
299}
300
301//=============================================================================================================
302
304{
305 int maxDegree = 0;
306 int minDegree = 1000000;
307
308 for(int i = 0; i < m_lNodes.size(); ++i) {
309 if(m_lNodes.at(i)->getFullOutdegree() > maxDegree){
310 maxDegree = m_lNodes.at(i)->getFullOutdegree();
311 } else if (m_lNodes.at(i)->getFullOutdegree() < minDegree){
312 minDegree = m_lNodes.at(i)->getFullOutdegree();
313 }
314 }
315
316 return QPair<int,int>(minDegree,maxDegree);
317}
318
319//=============================================================================================================
320
322{
323 int maxDegree = 0;
324 int minDegree = 1000000;
325
326 for(int i = 0; i < m_lNodes.size(); ++i) {
327 if(m_lNodes.at(i)->getThresholdedOutdegree() > maxDegree){
328 maxDegree = m_lNodes.at(i)->getThresholdedOutdegree();
329 } else if (m_lNodes.at(i)->getThresholdedOutdegree() < minDegree){
330 minDegree = m_lNodes.at(i)->getThresholdedOutdegree();
331 }
332 }
333
334 return QPair<int,int>(minDegree,maxDegree);
335}
336
337//=============================================================================================================
338
339void Network::setThreshold(double dThreshold)
340{
341 m_dThreshold = dThreshold;
342 m_lThresholdedEdges.clear();
343
344 for(int i = 0; i < m_lFullEdges.size(); ++i) {
345 if(fabs(m_lFullEdges.at(i)->getWeight()) >= m_dThreshold) {
346 m_lFullEdges.at(i)->setActive(true);
347 m_lThresholdedEdges.append(m_lFullEdges.at(i));
348 } else {
349 m_lFullEdges.at(i)->setActive(false);
350 }
351 }
352
355}
356
357//=============================================================================================================
358
360{
361 return m_dThreshold;
362}
363
364//=============================================================================================================
365
366void Network::setFrequencyRange(float fLowerFreq, float fUpperFreq)
367{
368 if(fLowerFreq > fUpperFreq || fUpperFreq < fLowerFreq) {
369 qDebug() << "Network::setFrequencyRange - Upper and lower frequency are out of range from each other. Weights will not be recalculated. Returning.";
370 return;
371 }
372
373 if(m_fSFreq <= 0.0f) {
374 qDebug() << "Network::setFrequencyRange - Sampling frequency has not been set. Returning.";
375 return;
376 }
377
378 if(fUpperFreq > m_fSFreq/2.0f) {
379 qDebug() << "Network::setFrequencyRange - Upper frequency is bigger than nyquist frequency. Returning.";
380 return;
381 }
382
383 if(m_iNumberFreqBins <= 0) {
384 qDebug() << "Network::setFrequencyRange - Number of samples has not been set. Returning.";
385 return;
386 }
387
388 double dScaleFactor = m_iFFTSize/(m_fSFreq/2);
389
390 m_minMaxFrequency.first = fLowerFreq;
391 m_minMaxFrequency.second = fUpperFreq;
392
393 int iLowerBin = fLowerFreq * dScaleFactor;
394 int iUpperBin = fUpperFreq * dScaleFactor;
395
396 // Update the min max values
397 m_minMaxFullWeights = QPair<double,double>(std::numeric_limits<double>::max(),0.0);
398
399 for(int i = 0; i < m_lFullEdges.size(); ++i) {
400 m_lFullEdges.at(i)->setFrequencyBins(QPair<int,int>(iLowerBin,iUpperBin));
401
402 if(fabs(m_lFullEdges.at(i)->getWeight()) < m_minMaxFullWeights.first) {
403 m_minMaxFullWeights.first = fabs(m_lFullEdges.at(i)->getWeight());
404 } else if(fabs(m_lFullEdges.at(i)->getWeight()) > m_minMaxFullWeights.second) {
405 m_minMaxFullWeights.second = fabs(m_lFullEdges.at(i)->getWeight());
406 }
407 }
408}
409
410//=============================================================================================================
411
412const QPair<float,float>& Network::getFrequencyRange() const
413{
414 return m_minMaxFrequency;
415}
416
417//=============================================================================================================
418
420{
421 if(newEdge->getEndNodeID() != newEdge->getStartNodeID()) {
422 double dEdgeWeight = newEdge->getWeight();
423 if(dEdgeWeight < m_minMaxFullWeights.first) {
424 m_minMaxFullWeights.first = dEdgeWeight;
425 } else if(dEdgeWeight >= m_minMaxFullWeights.second) {
426 m_minMaxFullWeights.second = dEdgeWeight;
427 }
428
429 m_lFullEdges << newEdge;
430
431 if(fabs(newEdge->getWeight()) >= m_dThreshold) {
432 m_lThresholdedEdges << newEdge;
433 }
434 }
435}
436
437//=============================================================================================================
438
440{
441 m_lNodes << newNode;
442}
443
444//=============================================================================================================
445
447{
448 if(m_lFullEdges.isEmpty() || m_lNodes.isEmpty()) {
449 return true;
450 }
451
452 return false;
453}
454
455//=============================================================================================================
456
458{
459 // Normalize full network
460 if(m_minMaxFullWeights.second == 0.0) {
461 qDebug() << "Network::normalize() - Max weight is 0. Returning.";
462 return;
463 }
464
465 for(int i = 0; i < m_lFullEdges.size(); ++i) {
466 m_lFullEdges.at(i)->setWeight(m_lFullEdges.at(i)->getWeight()/m_minMaxFullWeights.second);
467 }
468
470 m_minMaxFullWeights.second = 1.0;
471
473 m_minMaxThresholdedWeights.second = 1.0;
474}
475
476//=============================================================================================================
477
482
483//=============================================================================================================
484
486{
487 m_visualizationInfo = visualizationInfo;
488}
489
490//=============================================================================================================
491
493{
494 return m_fSFreq;
495}
496
497//=============================================================================================================
498
500{
501 m_fSFreq = fSFreq;
502}
503
504//=============================================================================================================
505
507{
508 return m_iNumberFreqBins;
509}
510
511//=============================================================================================================
512
513void Network::setUsedFreqBins(int iNumberFreqBins)
514{
515 m_iNumberFreqBins = iNumberFreqBins;
516}
517
518//=============================================================================================================
519
520void Network::setFFTSize(int iFFTSize)
521{
522 m_iFFTSize = iFFTSize;
523}
524
525//=============================================================================================================
526
528{
529 return m_iFFTSize;
530}
531
Declaration of Spectral class.
NetworkNode class declaration.
NetworkEdge class declaration.
Network class declaration.
Functional connectivity metrics (coherence, PLV, cross-correlation, etc.).
Shared utilities (I/O helpers, spectral analysis, layout management, warp algorithms).
Holds threshold and color settings for network edge visualization.
Definition network.h:77
double getThreshold()
Definition network.cpp:359
VisualizationInfo getVisualizationInfo() const
Definition network.cpp:478
void setVisualizationInfo(const VisualizationInfo &visualizationInfo)
Definition network.cpp:485
void append(QSharedPointer< NetworkEdge > newEdge)
int getUsedFreqBins() const
Definition network.cpp:506
qint16 getThresholdedDistribution() const
Definition network.cpp:190
void setUsedFreqBins(int iNumberFreqBins)
Definition network.cpp:513
void setConnectivityMethod(const QString &sConnectivityMethod)
Definition network.cpp:203
int m_iNumberFreqBins
Definition network.h:415
QSharedPointer< NetworkEdge > getEdgeAt(int i)
Definition network.cpp:170
QPair< double, double > m_minMaxThresholdedWeights
Definition network.h:410
QPair< int, int > getMinMaxFullDegrees() const
Definition network.cpp:231
Eigen::MatrixXd getThresholdedConnectivityMatrix(bool bGetMirroredVersion=true) const
Definition network.cpp:118
QPair< double, double > getMinMaxThresholdedWeights() const
Definition network.cpp:224
void setFFTSize(int iFFTSize)
Definition network.cpp:520
QPair< double, double > getMinMaxFullWeights() const
Definition network.cpp:217
QList< QSharedPointer< NetworkEdge > > m_lThresholdedEdges
Definition network.h:401
QPair< int, int > getMinMaxFullIndegrees() const
Definition network.cpp:267
Network(const QString &sConnectivityMethod="Unknown", double dThreshold=0.0)
Definition network.cpp:76
void setSamplingFrequency(float fSFreq)
Definition network.cpp:499
const QPair< float, float > & getFrequencyRange() const
Definition network.cpp:412
void setThreshold(double dThreshold=0.0)
Definition network.cpp:339
QString getConnectivityMethod() const
Definition network.cpp:210
Eigen::MatrixXd getFullConnectivityMatrix(bool bGetMirroredVersion=true) const
Definition network.cpp:94
QList< QSharedPointer< NetworkNode > > m_lNodes
Definition network.h:403
QPair< int, int > getMinMaxThresholdedDegrees() const
Definition network.cpp:249
QSharedPointer< NetworkNode > getNodeAt(int i)
Definition network.cpp:163
QPair< double, double > m_minMaxFullWeights
Definition network.h:409
bool isEmpty() const
Definition network.cpp:446
const QList< QSharedPointer< NetworkNode > > & getNodes() const
Definition network.cpp:156
QPair< int, int > getMinMaxThresholdedIndegrees() const
Definition network.cpp:285
float getSamplingFrequency() const
Definition network.cpp:492
void setFrequencyRange(float fLowerFreq, float fUpperFreq)
Definition network.cpp:366
QPair< int, int > getMinMaxFullOutdegrees() const
Definition network.cpp:303
QPair< float, float > m_minMaxFrequency
Definition network.h:411
const QList< QSharedPointer< NetworkEdge > > & getThresholdedEdges() const
Definition network.cpp:149
QString m_sConnectivityMethod
Definition network.h:407
double m_dThreshold
Definition network.h:413
const QList< QSharedPointer< NetworkEdge > > & getFullEdges() const
Definition network.cpp:142
VisualizationInfo m_visualizationInfo
Definition network.h:418
QPair< int, int > getMinMaxThresholdedOutdegrees() const
Definition network.cpp:321
qint16 getFullDistribution() const
Definition network.cpp:177
QList< QSharedPointer< NetworkEdge > > m_lFullEdges
Definition network.h:400
QSharedPointer< NetworkEdge > SPtr
Definition networkedge.h:83
QSharedPointer< NetworkNode > SPtr
Definition networknode.h:85