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