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)
64, 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 }
220 if (m_lNodes.at(i)->getFullDegree() < minDegree) {
221 minDegree = m_lNodes.at(i)->getFullDegree();
222 }
223 }
224
225 return QPair<int, int>(minDegree, maxDegree);
226}
227
228//=============================================================================================================
229
231{
232 int maxDegree = 0;
233 int minDegree = 1000000;
234
235 for (int i = 0; i < m_lNodes.size(); ++i) {
236 if (m_lNodes.at(i)->getThresholdedDegree() > maxDegree) {
237 maxDegree = m_lNodes.at(i)->getThresholdedDegree();
238 }
239 if (m_lNodes.at(i)->getThresholdedDegree() < minDegree) {
240 minDegree = m_lNodes.at(i)->getThresholdedDegree();
241 }
242 }
243
244 return QPair<int, int>(minDegree, maxDegree);
245}
246
247//=============================================================================================================
248
249QPair<int, int> Network::getMinMaxFullIndegrees() const
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)->getFullIndegree() > maxDegree) {
256 maxDegree = m_lNodes.at(i)->getFullIndegree();
257 }
258 if (m_lNodes.at(i)->getFullIndegree() < minDegree) {
259 minDegree = m_lNodes.at(i)->getFullIndegree();
260 }
261 }
262
263 return QPair<int, int>(minDegree, maxDegree);
264}
265
266//=============================================================================================================
267
269{
270 int maxDegree = 0;
271 int minDegree = 1000000;
272
273 for (int i = 0; i < m_lNodes.size(); ++i) {
274 if (m_lNodes.at(i)->getThresholdedIndegree() > maxDegree) {
275 maxDegree = m_lNodes.at(i)->getThresholdedIndegree();
276 }
277 if (m_lNodes.at(i)->getThresholdedIndegree() < minDegree) {
278 minDegree = m_lNodes.at(i)->getThresholdedIndegree();
279 }
280 }
281
282 return QPair<int, int>(minDegree, maxDegree);
283}
284
285//=============================================================================================================
286
287QPair<int, int> Network::getMinMaxFullOutdegrees() const
288{
289 int maxDegree = 0;
290 int minDegree = 1000000;
291
292 for (int i = 0; i < m_lNodes.size(); ++i) {
293 if (m_lNodes.at(i)->getFullOutdegree() > maxDegree) {
294 maxDegree = m_lNodes.at(i)->getFullOutdegree();
295 }
296 if (m_lNodes.at(i)->getFullOutdegree() < minDegree) {
297 minDegree = m_lNodes.at(i)->getFullOutdegree();
298 }
299 }
300
301 return QPair<int, int>(minDegree, maxDegree);
302}
303
304//=============================================================================================================
305
307{
308 int maxDegree = 0;
309 int minDegree = 1000000;
310
311 for (int i = 0; i < m_lNodes.size(); ++i) {
312 if (m_lNodes.at(i)->getThresholdedOutdegree() > maxDegree) {
313 maxDegree = m_lNodes.at(i)->getThresholdedOutdegree();
314 }
315 if (m_lNodes.at(i)->getThresholdedOutdegree() < minDegree) {
316 minDegree = m_lNodes.at(i)->getThresholdedOutdegree();
317 }
318 }
319
320 return QPair<int, int>(minDegree, maxDegree);
321}
322
323//=============================================================================================================
324
325void Network::setThreshold(double dThreshold)
326{
327 m_dThreshold = dThreshold;
328 m_lThresholdedEdges.clear();
329
330 for (int i = 0; i < m_lFullEdges.size(); ++i) {
331 if (fabs(m_lFullEdges.at(i)->getWeight()) >= m_dThreshold) {
332 m_lFullEdges.at(i)->setActive(true);
333 m_lThresholdedEdges.append(m_lFullEdges.at(i));
334 } else {
335 m_lFullEdges.at(i)->setActive(false);
336 }
337 }
338
341}
342
343//=============================================================================================================
344
346{
347 return m_dThreshold;
348}
349
350//=============================================================================================================
351
352void Network::setFrequencyRange(float fLowerFreq, float fUpperFreq)
353{
354 if (fLowerFreq > fUpperFreq) {
355 qDebug() << "Network::setFrequencyRange - Upper and lower frequency are out of range from each other. Weights will not be recalculated. Returning.";
356 return;
357 }
358
359 if (m_fSFreq <= 0.0f) {
360 qDebug() << "Network::setFrequencyRange - Sampling frequency has not been set. Returning.";
361 return;
362 }
363
364 if (fUpperFreq > m_fSFreq / 2.0f) {
365 qDebug() << "Network::setFrequencyRange - Upper frequency is bigger than nyquist frequency. Returning.";
366 return;
367 }
368
369 if (m_iNumberFreqBins <= 0) {
370 qDebug() << "Network::setFrequencyRange - Number of samples has not been set. Returning.";
371 return;
372 }
373
374 double dScaleFactor = m_iFFTSize / (m_fSFreq / 2);
375
376 m_minMaxFrequency.first = fLowerFreq;
377 m_minMaxFrequency.second = fUpperFreq;
378
379 int iLowerBin = fLowerFreq * dScaleFactor;
380 int iUpperBin = fUpperFreq * dScaleFactor;
381
382 // Update the min max values
383 m_minMaxFullWeights = QPair<double, double>(std::numeric_limits<double>::max(), 0.0);
384
385 for (int i = 0; i < m_lFullEdges.size(); ++i) {
386 m_lFullEdges.at(i)->setFrequencyBins(QPair<int, int>(iLowerBin, iUpperBin));
387
388 if (fabs(m_lFullEdges.at(i)->getWeight()) < m_minMaxFullWeights.first) {
389 m_minMaxFullWeights.first = fabs(m_lFullEdges.at(i)->getWeight());
390 }
391 if (fabs(m_lFullEdges.at(i)->getWeight()) > m_minMaxFullWeights.second) {
392 m_minMaxFullWeights.second = fabs(m_lFullEdges.at(i)->getWeight());
393 }
394 }
395}
396
397//=============================================================================================================
398
399const QPair<float, float>& Network::getFrequencyRange() const
400{
401 return m_minMaxFrequency;
402}
403
404//=============================================================================================================
405
407{
408 if (newEdge->getEndNodeID() != newEdge->getStartNodeID()) {
409 double dEdgeWeight = newEdge->getWeight();
410 if (dEdgeWeight < m_minMaxFullWeights.first) {
411 m_minMaxFullWeights.first = dEdgeWeight;
412 }
413 if (dEdgeWeight >= m_minMaxFullWeights.second) {
414 m_minMaxFullWeights.second = dEdgeWeight;
415 }
416
417 m_lFullEdges << newEdge;
418
419 if (fabs(newEdge->getWeight()) >= m_dThreshold) {
420 m_lThresholdedEdges << newEdge;
421 }
422 }
423}
424
425//=============================================================================================================
426
428{
429 m_lNodes << newNode;
430}
431
432//=============================================================================================================
433
435{
436 if (m_lFullEdges.isEmpty() || m_lNodes.isEmpty()) {
437 return true;
438 }
439
440 return false;
441}
442
443//=============================================================================================================
444
446{
447 // Normalize full network
448 if (m_minMaxFullWeights.second == 0.0) {
449 qDebug() << "Network::normalize() - Max weight is 0. Returning.";
450 return;
451 }
452
453 for (int i = 0; i < m_lFullEdges.size(); ++i) {
454 m_lFullEdges.at(i)->setWeight(m_lFullEdges.at(i)->getWeight() / m_minMaxFullWeights.second);
455 }
456
458 m_minMaxFullWeights.second = 1.0;
459
461 m_minMaxThresholdedWeights.second = 1.0;
462}
463
464//=============================================================================================================
465
470
471//=============================================================================================================
472
474{
475 m_visualizationInfo = visualizationInfo;
476}
477
478//=============================================================================================================
479
481{
482 return m_fSFreq;
483}
484
485//=============================================================================================================
486
488{
489 m_fSFreq = fSFreq;
490}
491
492//=============================================================================================================
493
495{
496 return m_iNumberFreqBins;
497}
498
499//=============================================================================================================
500
501void Network::setUsedFreqBins(int iNumberFreqBins)
502{
503 m_iNumberFreqBins = iNumberFreqBins;
504}
505
506//=============================================================================================================
507
508void Network::setFFTSize(int iFFTSize)
509{
510 m_iFFTSize = iFFTSize;
511}
512
513//=============================================================================================================
514
516{
517 return m_iFFTSize;
518}
Multi-taper spectral estimation: tapered FFT, power and cross-spectral density, DPSS weighting.
Node of a connectivity CONNECTIVITYLIB::Network; carries a 3D position and the lists of incident (in ...
Weighted edge between two CONNECTIVITYLIB::NetworkNode instances; stores the full per-frequency weigh...
Graph container that stores the result of one functional-connectivity metric as nodes (sources/sensor...
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:80
VisualizationInfo getVisualizationInfo() const
Definition network.cpp:466
void setVisualizationInfo(const VisualizationInfo &visualizationInfo)
Definition network.cpp:473
int getUsedFreqBins() const
Definition network.cpp:494
qint16 getThresholdedDistribution() const
Definition network.cpp:170
void setUsedFreqBins(int iNumberFreqBins)
Definition network.cpp:501
void setConnectivityMethod(const QString &sConnectivityMethod)
Definition network.cpp:183
QSharedPointer< NetworkEdge > getEdgeAt(int i)
Definition network.cpp:150
void append(QSharedPointer< NetworkEdge > newEdge)
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
QPair< double, double > m_minMaxThresholdedWeights
Definition network.h:423
void setFFTSize(int iFFTSize)
Definition network.cpp:508
QPair< double, double > getMinMaxFullWeights() const
Definition network.cpp:197
QPair< float, float > m_minMaxFrequency
Definition network.h:424
QPair< double, double > m_minMaxFullWeights
Definition network.h:422
QList< QSharedPointer< NetworkNode > > m_lNodes
Definition network.h:416
QPair< int, int > getMinMaxFullIndegrees() const
Definition network.cpp:249
Network(const QString &sConnectivityMethod="Unknown", double dThreshold=0.0)
Definition network.cpp:56
void setSamplingFrequency(float fSFreq)
Definition network.cpp:487
const QPair< float, float > & getFrequencyRange() const
Definition network.cpp:399
void setThreshold(double dThreshold=0.0)
Definition network.cpp:325
QString getConnectivityMethod() const
Definition network.cpp:190
Eigen::MatrixXd getFullConnectivityMatrix(bool bGetMirroredVersion=true) const
Definition network.cpp:74
VisualizationInfo m_visualizationInfo
Definition network.h:431
QPair< int, int > getMinMaxThresholdedDegrees() const
Definition network.cpp:230
QSharedPointer< NetworkNode > getNodeAt(int i)
Definition network.cpp:143
const QList< QSharedPointer< NetworkNode > > & getNodes() const
Definition network.cpp:136
QPair< int, int > getMinMaxThresholdedIndegrees() const
Definition network.cpp:268
float getSamplingFrequency() const
Definition network.cpp:480
void setFrequencyRange(float fLowerFreq, float fUpperFreq)
Definition network.cpp:352
QPair< int, int > getMinMaxFullOutdegrees() const
Definition network.cpp:287
const QList< QSharedPointer< NetworkEdge > > & getThresholdedEdges() const
Definition network.cpp:129
QList< QSharedPointer< NetworkEdge > > m_lThresholdedEdges
Definition network.h:414
const QList< QSharedPointer< NetworkEdge > > & getFullEdges() const
Definition network.cpp:122
QPair< int, int > getMinMaxThresholdedOutdegrees() const
Definition network.cpp:306
QList< QSharedPointer< NetworkEdge > > m_lFullEdges
Definition network.h:413
qint16 getFullDistribution() const
Definition network.cpp:157
QString m_sConnectivityMethod
Definition network.h:420
QSharedPointer< NetworkEdge > SPtr
Definition networkedge.h:87
QSharedPointer< NetworkNode > SPtr
Definition networknode.h:85