MNE-CPP  0.1.9
A Framework for Electrophysiology
connectivitysettings.cpp
Go to the documentation of this file.
1 //=============================================================================================================
35 //=============================================================================================================
36 // INCLUDES
37 //=============================================================================================================
38 
39 #include "connectivitysettings.h"
40 
42 #include <fs/surfaceset.h>
43 #include <fiff/fiff_info.h>
44 
45 //=============================================================================================================
46 // QT INCLUDES
47 //=============================================================================================================
48 
49 #include <QCommandLineParser>
50 #include <QElapsedTimer>
51 #include <QDebug>
52 
53 //=============================================================================================================
54 // EIGEN INCLUDES
55 //=============================================================================================================
56 
57 //=============================================================================================================
58 // USED NAMESPACES
59 //=============================================================================================================
60 
61 using namespace CONNECTIVITYLIB;
62 using namespace MNELIB;
63 using namespace Eigen;
64 using namespace FIFFLIB;
65 using namespace FSLIB;
66 
67 //=============================================================================================================
68 // DEFINE GLOBAL METHODS
69 //=============================================================================================================
70 
71 //=============================================================================================================
72 // DEFINE MEMBER METHODS
73 //=============================================================================================================
74 
76 : m_fFreqResolution(1.0f)
77 , m_fSFreq(1000.0f)
78 , m_sWindowType("hanning")
79 {
81  qRegisterMetaType<CONNECTIVITYLIB::ConnectivitySettings>("CONNECTIVITYLIB::ConnectivitySettings");
82 }
83 
84 //*******************************************************************************************************
85 
86 void ConnectivitySettings::clearAllData()
87 {
88  m_trialData.clear();
89 
90  clearIntermediateData();
91 }
92 
93 //*******************************************************************************************************
94 
95 void ConnectivitySettings::clearIntermediateData()
96 {
97  for (int i = 0; i < m_trialData.size(); ++i) {
98  m_trialData[i].matPsd.resize(0,0);
99  m_trialData[i].vecPairCsd.clear();
100  m_trialData[i].vecTapSpectra.clear();
101  m_trialData[i].vecPairCsdNormalized.clear();
102  m_trialData[i].vecPairCsdImagSign.clear();
103  m_trialData[i].vecPairCsdImagAbs.clear();
104  m_trialData[i].vecPairCsdImagSqrd.clear();
105  }
106 
107  m_intermediateSumData.matPsdSum.resize(0,0);
108  m_intermediateSumData.vecPairCsdSum.clear();
109  m_intermediateSumData.vecPairCsdNormalizedSum.clear();
110  m_intermediateSumData.vecPairCsdImagSignSum.clear();
111  m_intermediateSumData.vecPairCsdImagAbsSum.clear();
112  m_intermediateSumData.vecPairCsdImagSqrdSum.clear();
113 }
114 
115 //*******************************************************************************************************
116 
117 void ConnectivitySettings::append(const QList<MatrixXd>& matInputData)
118 {
119  for(int i = 0; i < matInputData.size(); ++i) {
120  this->append(matInputData.at(i));
121  }
122 }
123 
124 //*******************************************************************************************************
125 
126 void ConnectivitySettings::append(const MatrixXd& matInputData)
127 {
129  tempData.matData = matInputData;
130 
131  m_trialData.append(tempData);
132 }
133 
134 //*******************************************************************************************************
135 
136 void ConnectivitySettings::append(const ConnectivitySettings::IntermediateTrialData& inputData)
137 {
138  m_trialData.append(inputData);
139 }
140 
141 //*******************************************************************************************************
142 
143 const ConnectivitySettings::IntermediateTrialData& ConnectivitySettings::at(int i) const
144 {
145  return m_trialData.at(i);
146 }
147 
148 //*******************************************************************************************************
149 
150 int ConnectivitySettings::size() const
151 {
152  return m_trialData.size();
153 }
154 
155 //*******************************************************************************************************
156 
157 bool ConnectivitySettings::isEmpty() const
158 {
159  return m_trialData.isEmpty();
160 }
161 
162 //*******************************************************************************************************
163 
164 void ConnectivitySettings::removeFirst(int iAmount)
165 {
166 // QElapsedTimer timer;
167 // qint64 iTime = 0;
168 // timer.start();
169 
170  if(m_trialData.isEmpty()) {
171  qDebug() << "ConnectivitySettings::removeFirst - No elements to delete. Returning.";
172  return;
173  }
174 
175  if(m_trialData.size() < iAmount) {
176  qDebug() << "ConnectivitySettings::removeFirst - Not enough elements stored in list in order to delete them. Returning.";
177  return;
178  }
179 
180  // Substract influence of trials from overall summed up intermediate data and remove from data list
181  for (int j = 0; j < iAmount; ++j) {
182  for (int i = 0; i < m_trialData.first().matData.rows(); ++i) {
183  if(i < m_intermediateSumData.vecPairCsdSum.size() && (m_intermediateSumData.vecPairCsdSum.size() == m_trialData.first().vecPairCsd.size())) {
184  m_intermediateSumData.vecPairCsdSum[i].second -= m_trialData.first().vecPairCsd.at(i).second;
185  }
186  if(i < m_intermediateSumData.vecPairCsdNormalizedSum.size() && (m_intermediateSumData.vecPairCsdNormalizedSum.size() == m_trialData.first().vecPairCsdNormalized.size())) {
187  m_intermediateSumData.vecPairCsdNormalizedSum[i].second -= m_trialData.first().vecPairCsdNormalized.at(i).second;
188  }
189  if(i < m_intermediateSumData.vecPairCsdImagSignSum.size() && (m_intermediateSumData.vecPairCsdImagSignSum.size() == m_trialData.first().vecPairCsdImagSign.size())) {
190  m_intermediateSumData.vecPairCsdImagSignSum[i].second -= m_trialData.first().vecPairCsdImagSign.at(i).second;
191  }
192  if(i < m_intermediateSumData.vecPairCsdImagAbsSum.size() && (m_intermediateSumData.vecPairCsdImagAbsSum.size() == m_trialData.first().vecPairCsdImagAbs.size())) {
193  m_intermediateSumData.vecPairCsdImagAbsSum[i].second -= m_trialData.first().vecPairCsdImagAbs.at(i).second;
194  }
195  if(i < m_intermediateSumData.vecPairCsdImagSqrdSum.size() && (m_intermediateSumData.vecPairCsdImagSqrdSum.size() == m_trialData.first().vecPairCsdImagSqrd.size())) {
196  m_intermediateSumData.vecPairCsdImagSqrdSum[i].second -= m_trialData.first().vecPairCsdImagSqrd.at(i).second;
197  }
198  }
199 
200  if(m_intermediateSumData.matPsdSum.rows() == m_trialData.first().matPsd.rows() &&
201  m_intermediateSumData.matPsdSum.cols() == m_trialData.first().matPsd.cols() ) {
202  m_intermediateSumData.matPsdSum -= m_trialData.first().matPsd;
203  }
204 
205  m_trialData.removeFirst();
206  }
207 
208 // iTime = timer.elapsed();
209 // qDebug() << "ConnectivitySettings::removeFirst" << iTime;
210 // timer.restart();
211 }
212 
213 //*******************************************************************************************************
214 
215 void ConnectivitySettings::removeLast(int iAmount)
216 {
217 // QElapsedTimer timer;
218 // qint64 iTime = 0;
219 // timer.start();
220 
221  if(m_trialData.isEmpty()) {
222  qDebug() << "ConnectivitySettings::removeLast - No elements to delete. Returning.";
223  return;
224  }
225 
226  if(m_trialData.size() < iAmount) {
227  qDebug() << "ConnectivitySettings::removeLast - Not enough elements stored in list in order to delete them. Returning.";
228  return;
229  }
230 
231  // Substract influence of trials from overall summed up intermediate data and remove from data list
232  for (int j = 0; j < iAmount; ++j) {
233  for (int i = 0; i < m_trialData.last().matData.rows(); ++i) {
234  if(i < m_intermediateSumData.vecPairCsdSum.size() && (m_intermediateSumData.vecPairCsdSum.size() == m_trialData.last().vecPairCsd.size())) {
235  m_intermediateSumData.vecPairCsdSum[i].second -= m_trialData.last().vecPairCsd.at(i).second;
236  }
237  if(i < m_intermediateSumData.vecPairCsdNormalizedSum.size() && (m_intermediateSumData.vecPairCsdNormalizedSum.size() == m_trialData.last().vecPairCsdNormalized.size())) {
238  m_intermediateSumData.vecPairCsdNormalizedSum[i].second -= m_trialData.last().vecPairCsdNormalized.at(i).second;
239  }
240  if(i < m_intermediateSumData.vecPairCsdImagSignSum.size() && (m_intermediateSumData.vecPairCsdImagSignSum.size() == m_trialData.last().vecPairCsdImagSign.size())) {
241  m_intermediateSumData.vecPairCsdImagSignSum[i].second -= m_trialData.last().vecPairCsdImagSign.at(i).second;
242  }
243  if(i < m_intermediateSumData.vecPairCsdImagAbsSum.size() && (m_intermediateSumData.vecPairCsdImagAbsSum.size() == m_trialData.last().vecPairCsdImagAbs.size())) {
244  m_intermediateSumData.vecPairCsdImagAbsSum[i].second -= m_trialData.last().vecPairCsdImagAbs.at(i).second;
245  }
246  if(i < m_intermediateSumData.vecPairCsdImagSqrdSum.size() && (m_intermediateSumData.vecPairCsdImagSqrdSum.size() == m_trialData.last().vecPairCsdImagSqrd.size())) {
247  m_intermediateSumData.vecPairCsdImagSqrdSum[i].second -= m_trialData.last().vecPairCsdImagSqrd.at(i).second;
248  }
249  }
250 
251  if(m_intermediateSumData.matPsdSum.rows() == m_trialData.last().matPsd.rows() &&
252  m_intermediateSumData.matPsdSum.cols() == m_trialData.last().matPsd.cols() ) {
253  m_intermediateSumData.matPsdSum -= m_trialData.last().matPsd;
254  }
255 
256  m_trialData.removeLast();
257  }
258 
259 // iTime = timer.elapsed();
260 // qDebug() << "ConnectivitySettings::removeLast" << iTime;
261 // timer.restart();
262 }
263 
264 //*******************************************************************************************************
265 
266 void ConnectivitySettings::setConnectivityMethods(const QStringList& sConnectivityMethods)
267 {
268  m_sConnectivityMethods = sConnectivityMethods;
269 }
270 
271 //*******************************************************************************************************
272 
273 const QStringList& ConnectivitySettings::getConnectivityMethods() const
274 {
275  return m_sConnectivityMethods;
276 }
277 
278 //*******************************************************************************************************
279 
280 void ConnectivitySettings::setSamplingFrequency(int iSFreq)
281 {
282  if(m_fSFreq == iSFreq) {
283  return;
284  }
285 
286  clearIntermediateData();
287 
288  m_fSFreq = iSFreq;
289 
290  if(m_fFreqResolution != 0.0f) {
292  }
293 }
294 
295 //*******************************************************************************************************
296 
297 int ConnectivitySettings::getSamplingFrequency() const
298 {
299  return m_fSFreq;
300 }
301 
302 //*******************************************************************************************************
303 
304 void ConnectivitySettings::setFFTSize(int iNfft)
305 {
306  if(iNfft == 0) {
307  return;
308  }
309 
310  clearIntermediateData();
311 
312  m_iNfft = iNfft;
314 }
315 
316 //*******************************************************************************************************
317 
318 int ConnectivitySettings::getFFTSize() const
319 {
320  return m_iNfft;
321 }
322 
323 //*******************************************************************************************************
324 
325 void ConnectivitySettings::setWindowType(const QString& sWindowType)
326 {
327  // Clear all intermediate data since this will have an effect on the frequency calculation
328  clearIntermediateData();
329 
330  m_sWindowType = sWindowType;
331 }
332 
333 //*******************************************************************************************************
334 
335 const QString& ConnectivitySettings::getWindowType() const
336 {
337  return m_sWindowType;
338 }
339 
340 //*******************************************************************************************************
341 
342 void ConnectivitySettings::setNodePositions(const FiffInfo& fiffInfo,
343  const RowVectorXi& picks)
344 {
345  m_matNodePositions.resize(picks.cols(),3);
346 
347  qint32 kind;
348  for(int i = 0; i < picks.cols(); ++i) {
349  kind = fiffInfo.chs.at(i).kind;
350  if(kind == FIFFV_EEG_CH ||
351  kind == FIFFV_MEG_CH) {
352  m_matNodePositions(i,0) = fiffInfo.chs.at(picks(i)).chpos.r0(0);
353  m_matNodePositions(i,1) = fiffInfo.chs.at(picks(i)).chpos.r0(1);
354  m_matNodePositions(i,2) = fiffInfo.chs.at(picks(i)).chpos.r0(2);
355  }
356  }
357 }
358 
359 //*******************************************************************************************************
360 
361 void ConnectivitySettings::setNodePositions(const MNEForwardSolution& forwardSolution, const SurfaceSet& surfSet)
362 {
363  //Generate node vertices
364  MatrixX3f matNodeVertLeft, matNodeVertRight;
365 
366  if(forwardSolution.isClustered()) {
367  matNodeVertLeft.resize(forwardSolution.src[0].cluster_info.centroidVertno.size(),3);
368  for(int j = 0; j < matNodeVertLeft.rows(); ++j) {
369  matNodeVertLeft.row(j) = surfSet[0].rr().row(forwardSolution.src[0].cluster_info.centroidVertno.at(j)) - surfSet[0].offset().transpose();
370  }
371 
372  matNodeVertRight.resize(forwardSolution.src[1].cluster_info.centroidVertno.size(),3);
373  for(int j = 0; j < matNodeVertRight.rows(); ++j) {
374  matNodeVertRight.row(j) = surfSet[1].rr().row(forwardSolution.src[1].cluster_info.centroidVertno.at(j)) - surfSet[1].offset().transpose();
375  }
376  } else {
377  matNodeVertLeft.resize(forwardSolution.src[0].vertno.rows(),3);
378  for(int j = 0; j < matNodeVertLeft.rows(); ++j) {
379  matNodeVertLeft.row(j) = surfSet[0].rr().row(forwardSolution.src[0].vertno(j)) - surfSet[0].offset().transpose();
380  }
381 
382  matNodeVertRight.resize(forwardSolution.src[1].vertno.rows(),3);
383  for(int j = 0; j < matNodeVertRight.rows(); ++j) {
384  matNodeVertRight.row(j) = surfSet[1].rr().row(forwardSolution.src[1].vertno(j)) - surfSet[1].offset().transpose();
385  }
386  }
387 
388  m_matNodePositions.resize(matNodeVertLeft.rows()+matNodeVertRight.rows(),3);
389  m_matNodePositions << matNodeVertLeft, matNodeVertRight;
390 }
391 
392 //*******************************************************************************************************
393 
394 void ConnectivitySettings::setNodePositions(const MatrixX3f& matNodePositions)
395 {
396  m_matNodePositions = matNodePositions;
397 }
398 
399 //*******************************************************************************************************
400 
401 const MatrixX3f& ConnectivitySettings::getNodePositions() const
402 {
403  return m_matNodePositions;
404 }
405 
406 //*******************************************************************************************************
407 
408 QList<ConnectivitySettings::IntermediateTrialData>& ConnectivitySettings::getTrialData()
409 {
410  return m_trialData;
411 }
412 
413 //*******************************************************************************************************
414 
415 ConnectivitySettings::IntermediateSumData& ConnectivitySettings::getIntermediateSumData()
416 {
417  return m_intermediateSumData;
418 }
CONNECTIVITYLIB::ConnectivitySettings::IntermediateTrialData
Definition: connectivitysettings.h:98
CONNECTIVITYLIB::ConnectivitySettings::m_trialData
QList< IntermediateTrialData > m_trialData
Definition: connectivitysettings.h:185
CONNECTIVITYLIB::ConnectivitySettings::m_iNfft
int m_iNfft
Definition: connectivitysettings.h:179
FIFFLIB::FiffInfo
FIFF measurement file information.
Definition: fiff_info.h:84
MNELIB::MNEForwardSolution
Forward operator.
Definition: mne_forwardsolution.h:170
CONNECTIVITYLIB::ConnectivitySettings::m_fSFreq
float m_fSFreq
Definition: connectivitysettings.h:178
surfaceset.h
SurfaceSet class declaration.
CONNECTIVITYLIB::ConnectivitySettings::ConnectivitySettings
ConnectivitySettings()
Definition: connectivitysettings.cpp:75
fiff_info.h
FiffInfo class declaration.
MNELIB::MNEForwardSolution::src
MNESourceSpace src
Definition: mne_forwardsolution.h:529
FSLIB::SurfaceSet
A hemisphere set of surfaces.
Definition: surfaceset.h:71
FIFFLIB::FiffInfoBase::chs
QList< FiffChInfo > chs
Definition: fiff_info_base.h:223
CONNECTIVITYLIB::ConnectivitySettings::m_intermediateSumData
IntermediateSumData m_intermediateSumData
Definition: connectivitysettings.h:184
CONNECTIVITYLIB::ConnectivitySettings::m_sConnectivityMethods
QStringList m_sConnectivityMethods
Definition: connectivitysettings.h:175
mne_forwardsolution.h
MNEForwardSolution class declaration, which provides the forward solution including the source space ...
CONNECTIVITYLIB::ConnectivitySettings::IntermediateSumData
Definition: connectivitysettings.h:109
CONNECTIVITYLIB::ConnectivitySettings::m_sWindowType
QString m_sWindowType
Definition: connectivitysettings.h:176
MNELIB::MNESourceSpace::size
qint32 size() const
Definition: mne_sourcespace.h:333
CONNECTIVITYLIB::ConnectivitySettings::m_fFreqResolution
float m_fFreqResolution
Definition: connectivitysettings.h:180
MNELIB::MNEForwardSolution::isClustered
bool isClustered() const
Definition: mne_forwardsolution.h:538
CONNECTIVITYLIB::ConnectivitySettings::m_matNodePositions
Eigen::MatrixX3f m_matNodePositions
Definition: connectivitysettings.h:182
connectivitysettings.h
ConnectivitySettings class declaration.