MNE-CPP 0.1.9
A Framework for Electrophysiology
Loading...
Searching...
No Matches
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
64using namespace CONNECTIVITYLIB;
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)
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
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 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
196void Network::setConnectivityMethod(const QString& sConnectivityMethod)
197{
198 m_sConnectivityMethod = sConnectivityMethod;
199}
200
201//=============================================================================================================
202
204{
206}
207
208//=============================================================================================================
209
210QPair<double, double> Network::getMinMaxFullWeights() const
211{
212 return m_minMaxFullWeights;
213}
214
215//=============================================================================================================
216
217QPair<double, double> Network::getMinMaxThresholdedWeights() const
218{
220}
221
222//=============================================================================================================
223
224QPair<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
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
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
332void 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
359void 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
405const 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
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
475
476//=============================================================================================================
477
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
506void Network::setUsedFreqBins(int iNumberFreqBins)
507{
508 m_iNumberFreqBins = iNumberFreqBins;
509}
510
511//=============================================================================================================
512
513void Network::setFFTSize(int iFFTSize)
514{
515 m_iFFTSize = iFFTSize;
516}
517
518//=============================================================================================================
519
521{
522 return m_iFFTSize;
523}
524
NetworkEdge class declaration.
NetworkNode class declaration.
Network class declaration.
Declaration of Spectral class.
Definition event.h:282
VisualizationInfo getVisualizationInfo() const
Definition network.cpp:471
void setVisualizationInfo(const VisualizationInfo &visualizationInfo)
Definition network.cpp:478
int getUsedFreqBins() const
Definition network.cpp:499
qint16 getThresholdedDistribution() const
Definition network.cpp:183
void setUsedFreqBins(int iNumberFreqBins)
Definition network.cpp:506
void setConnectivityMethod(const QString &sConnectivityMethod)
Definition network.cpp:196
void append(QSharedPointer< NetworkEdge > newEdge)
QPair< double, double > m_minMaxFullWeights
Definition network.h:406
QPair< int, int > getMinMaxFullDegrees() const
Definition network.cpp:224
Eigen::MatrixXd getThresholdedConnectivityMatrix(bool bGetMirroredVersion=true) const
Definition network.cpp:118
QPair< double, double > getMinMaxThresholdedWeights() const
Definition network.cpp:217
void setFFTSize(int iFFTSize)
Definition network.cpp:513
QPair< double, double > getMinMaxFullWeights() const
Definition network.cpp:210
QList< QSharedPointer< NetworkNode > > m_lNodes
Definition network.h:400
QPair< int, int > getMinMaxFullIndegrees() const
Definition network.cpp:260
Network(const QString &sConnectivityMethod="Unknown", double dThreshold=0.0)
Definition network.cpp:76
void setSamplingFrequency(float fSFreq)
Definition network.cpp:492
const QPair< float, float > & getFrequencyRange() const
Definition network.cpp:405
void setThreshold(double dThreshold=0.0)
Definition network.cpp:332
QString getConnectivityMethod() const
Definition network.cpp:203
Eigen::MatrixXd getFullConnectivityMatrix(bool bGetMirroredVersion=true) const
Definition network.cpp:94
VisualizationInfo m_visualizationInfo
Definition network.h:415
QPair< int, int > getMinMaxThresholdedDegrees() const
Definition network.cpp:242
QSharedPointer< NetworkNode > getNodeAt(int i)
Definition network.cpp:163
QPair< double, double > m_minMaxThresholdedWeights
Definition network.h:407
const QList< QSharedPointer< NetworkNode > > & getNodes() const
Definition network.cpp:156
QPair< int, int > getMinMaxThresholdedIndegrees() const
Definition network.cpp:278
float getSamplingFrequency() const
Definition network.cpp:485
void setFrequencyRange(float fLowerFreq, float fUpperFreq)
Definition network.cpp:359
QPair< int, int > getMinMaxFullOutdegrees() const
Definition network.cpp:296
const QList< QSharedPointer< NetworkEdge > > & getThresholdedEdges() const
Definition network.cpp:149
QPair< float, float > m_minMaxFrequency
Definition network.h:408
QList< QSharedPointer< NetworkEdge > > m_lThresholdedEdges
Definition network.h:398
const QList< QSharedPointer< NetworkEdge > > & getFullEdges() const
Definition network.cpp:142
QPair< int, int > getMinMaxThresholdedOutdegrees() const
Definition network.cpp:314
QList< QSharedPointer< NetworkEdge > > m_lFullEdges
Definition network.h:397
qint16 getFullDistribution() const
Definition network.cpp:170
QString m_sConnectivityMethod
Definition network.h:404
QSharedPointer< NetworkEdge > SPtr
Definition networkedge.h:83
QSharedPointer< NetworkNode > SPtr
Definition networknode.h:85