v2.0.0
Loading...
Searching...
No Matches
correlation.cpp
Go to the documentation of this file.
1//=============================================================================================================
14
15//=============================================================================================================
16// INCLUDES
17//=============================================================================================================
18
19#include "correlation.h"
22#include "../network/network.h"
23
24//=============================================================================================================
25// QT INCLUDES
26//=============================================================================================================
27
28#include <QDebug>
29#include <QElapsedTimer>
30#include <QtConcurrent>
31
32//=============================================================================================================
33// EIGEN INCLUDES
34//=============================================================================================================
35
36#include <unsupported/Eigen/FFT>
37
38//=============================================================================================================
39// USED NAMESPACES
40//=============================================================================================================
41
42using namespace CONNECTIVITYLIB;
43using namespace Eigen;
44
45//=============================================================================================================
46// DEFINE GLOBAL METHODS
47//=============================================================================================================
48
49//=============================================================================================================
50// DEFINE MEMBER METHODS
51//=============================================================================================================
52
56
57//=============================================================================================================
58
60{
61 // QElapsedTimer timer;
62 // qint64 iTime = 0;
63 // timer.start();
64
65 Network finalNetwork("COR");
66
67 if (connectivitySettings.isEmpty()) {
68 qDebug() << "Correlation::calculate - Input data is empty";
69 return finalNetwork;
70 }
71
72 finalNetwork.setSamplingFrequency(connectivitySettings.getSamplingFrequency());
73
74 //Create nodes
75 int rows = connectivitySettings.at(0).matData.rows();
76 RowVectorXf rowVert = RowVectorXf::Zero(3);
77
78 for (int i = 0; i < rows; ++i) {
79 rowVert = RowVectorXf::Zero(3);
80
81 if (connectivitySettings.getNodePositions().rows() != 0 && i < connectivitySettings.getNodePositions().rows()) {
82 rowVert(0) = connectivitySettings.getNodePositions().row(i)(0);
83 rowVert(1) = connectivitySettings.getNodePositions().row(i)(1);
84 rowVert(2) = connectivitySettings.getNodePositions().row(i)(2);
85 }
86
87 finalNetwork.append(NetworkNode::SPtr(new NetworkNode(i, rowVert)));
88 }
89
90 // iTime = timer.elapsed();
91 // qWarning() << "Preparation" << iTime;
92 // timer.restart();
93
94 // Calculate connectivity matrix over epochs and average afterwards
95 // double dScalingStep = 1.0/matDataList.size();
96 // dataTemp.matInputData = dScalingStep * (i+1) * matDataList.at(i);
97
98 QFuture<MatrixXd> resultMat = QtConcurrent::mappedReduced(connectivitySettings.getTrialData(),
99 compute,
100 reduce);
101 resultMat.waitForFinished();
102
103 MatrixXd matDist = resultMat.result() / connectivitySettings.size();
104
105 // MatrixXd matDist;
106
107 // for(int i = 0; i < connectivitySettings.getTrialData().size(); ++i) {
108 // reduce(matDist, compute(connectivitySettings.getTrialData().at(i)));
109 // }
110
111 // matDist /= connectivitySettings.size();
112
113 // iTime = timer.elapsed();
114 // qWarning() << "ComputeSpectraPSDCSD" << iTime;
115 // timer.restart();
116
117 //Add edges to network
118 MatrixXd matWeight(1, 1);
119 QSharedPointer<NetworkEdge> pEdge;
120 int j;
121
122 for (int i = 0; i < matDist.rows(); ++i) {
123 for (j = i; j < matDist.cols(); ++j) {
124 matWeight << matDist(i, j);
125
126 pEdge = QSharedPointer<NetworkEdge>(new NetworkEdge(i, j, matWeight));
127
128 finalNetwork.getNodeAt(i)->append(pEdge);
129 finalNetwork.getNodeAt(j)->append(pEdge);
130 finalNetwork.append(pEdge);
131 }
132 }
133
134 // iTime = timer.elapsed();
135 // qWarning() << "Compute" << iTime;
136 // timer.restart();
137
138 return finalNetwork;
139}
140
141//=============================================================================================================
142
144{
145 const MatrixXd centered = inputData.matData.colwise() - inputData.matData.rowwise().mean();
146 const VectorXd norms = centered.rowwise().norm();
147 MatrixXd matDist = centered * centered.transpose();
148
149 for (int i = 0; i < matDist.rows(); ++i) {
150 for (int j = 0; j < matDist.cols(); ++j) {
151 const double denom = norms(i) * norms(j);
152 matDist(i, j) = denom > 0.0 ? matDist(i, j) / denom : 0.0;
153 }
154 }
155
156 return matDist;
157}
158
159//=============================================================================================================
160
161void Correlation::reduce(MatrixXd& resultData,
162 const MatrixXd& data)
163{
164 if (resultData.rows() != data.rows() || resultData.cols() != data.cols()) {
165 resultData.resize(data.rows(), data.cols());
166 resultData.setZero();
167 }
168
169 resultData += data;
170}
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...
Pearson correlation coefficient between every pair of zero-lag time-domain channels.
Functional connectivity metrics (coherence, PLV, cross-correlation, etc.).
Aggregates trial data, spectral cache and node geometry shared by all CONNECTIVITYLIB metrics.
QList< IntermediateTrialData > & getTrialData()
const IntermediateTrialData & at(int i) const
const Eigen::MatrixX3f & getNodePositions() const
Per-trial intermediate frequency-domain data used during connectivity computation.
static Eigen::MatrixXd compute(const ConnectivitySettings::IntermediateTrialData &inputData)
static Network calculate(ConnectivitySettings &connectivitySettings)
static void reduce(Eigen::MatrixXd &resultData, const Eigen::MatrixXd &data)
Graph container for one connectivity metric; nodes + weighted edges + threshold/visualisation state.
Definition network.h:106
void append(QSharedPointer< NetworkEdge > newEdge)
void setSamplingFrequency(float fSFreq)
Definition network.cpp:479
QSharedPointer< NetworkNode > getNodeAt(int i)
Definition network.cpp:143
Weighted, directional edge in a Network; carries per-frequency weights plus a band-averaged scalar.
Definition networkedge.h:85
Graph node carrying a 3D position and its incident in/out, full/thresholded edge lists.
Definition networknode.h:83
QSharedPointer< NetworkNode > SPtr
Definition networknode.h:85