MNE-CPP  0.1.9
A Framework for Electrophysiology
averaging.cpp
Go to the documentation of this file.
1 //=============================================================================================================
35 //=============================================================================================================
36 // INCLUDES
37 //=============================================================================================================
38 
39 #include "averaging.h"
40 
41 #include "helpers/filterkernel.h"
42 #include "filter.h"
43 
45 
46 //=============================================================================================================
47 // QT INCLUDES
48 //=============================================================================================================
49 
50 //=============================================================================================================
51 // EIGEN INCLUDES
52 //=============================================================================================================
53 
54 //=============================================================================================================
55 // USED NAMESPACES
56 //=============================================================================================================
57 
58 using namespace FIFFLIB;
59 using namespace Eigen;
60 using namespace MNELIB;
61 
62 //=============================================================================================================
63 // DEFINE GLOBAL RTPROCESSINGLIB METHODS
64 //=============================================================================================================
65 
67  const MatrixXi& matEvents,
68  float fTMinS,
69  float fTMaxS,
70  qint32 eventType,
71  bool bApplyBaseline,
72  float fTBaselineFromS,
73  float fTBaselineToS,
74  const QMap<QString,double>& mapReject,
75  const QStringList& lExcludeChs,
76  const RowVectorXi& picks)
77 {
78 
79  MNEEpochDataList lstEpochDataList = MNEEpochDataList::readEpochs(raw,
80  matEvents,
81  fTMinS,
82  fTMaxS,
83  eventType,
84  mapReject,
85  lExcludeChs,
86  picks);
87 
88  if(bApplyBaseline){
89  QPair<float, float> baselinePair(fTBaselineFromS, fTBaselineToS);
90  lstEpochDataList.applyBaselineCorrection(baselinePair);
91  }
92 
93  if(!mapReject.isEmpty()){
94  lstEpochDataList.dropRejected();
95  }
96 
97  return lstEpochDataList.average(raw.info,
98  0,
99  lstEpochDataList.first()->epoch.cols());
100 }
101 
102 //=============================================================================================================
103 
105  const MatrixXi& matEvents,
106  float fTMinS,
107  float fTMaxS,
108  qint32 eventType,
109  bool bApplyBaseline,
110  float fTBaselineFromS,
111  float fTBaselineToS,
112  const QMap<QString,double>& mapReject,
113  const FilterKernel& filterKernel,
114  const QStringList& lExcludeChs,
115  const RowVectorXi& picks)
116 {
117  MNEEpochDataList lstEpochDataList;
118 
119  // Select the desired events
120  qint32 count = 0;
121  qint32 p;
122  MatrixXi selected = MatrixXi::Zero(1, matEvents.rows());
123  for (p = 0; p < matEvents.rows(); ++p)
124  {
125  if (matEvents(p,1) == 0 && matEvents(p,2) == eventType)
126  {
127  selected(0,count) = p;
128  ++count;
129  }
130  }
131  selected.conservativeResize(1, count);
132  if (count > 0) {
133  qInfo("[RTPROCESSINGLIB::computeFilteredAverage] %d matching events found",count);
134  }
135 
136  // If picks are empty, pick all
137  RowVectorXi picksNew = picks;
138  if(picks.cols() <= 0) {
139  picksNew.resize(raw.info.chs.size());
140  for(int i = 0; i < raw.info.chs.size(); ++i) {
141  picksNew(i) = i;
142  }
143  }
144 
145  fiff_int_t event_samp, from, to;
146  fiff_int_t dropCount = 0;
147  MatrixXd timesDummy;
148  MatrixXd times;
149 
150  QScopedPointer<MNEEpochData> epoch(Q_NULLPTR);
151  int iFilterDelay = filterKernel.getFilterOrder()/2;
152 
153  for (p = 0; p < count; ++p) {
154  // Read a data segment
155  event_samp = matEvents(selected(p),0);
156  from = event_samp + fTMinS*raw.info.sfreq;
157  to = event_samp + floor(fTMaxS*raw.info.sfreq + 0.5);
158 
159  epoch.reset(new MNEEpochData());
160 
161  if(raw.read_raw_segment(epoch->epoch, timesDummy, from - iFilterDelay, to + iFilterDelay, picksNew)) {
162  // Filter the data
163  epoch->epoch = RTPROCESSINGLIB::filterData(epoch->epoch,filterKernel).block(0, iFilterDelay, epoch->epoch.rows(), to-from);
164 
165  if (p == 0) {
166  times.resize(1, to-from+1);
167  for (qint32 i = 0; i < times.cols(); ++i)
168  times(0, i) = ((float)(from-event_samp+i)) / raw.info.sfreq;
169  }
170 
171  epoch->event = eventType;
172  epoch->tmin = fTMinS;
173  epoch->tmax = fTMaxS;
174 
175  epoch->bReject = MNEEpochDataList::checkForArtifact(epoch->epoch,
176  raw.info,
177  mapReject,
178  lExcludeChs);
179 
180  if (epoch->bReject) {
181  dropCount++;
182  }
183 
184  //Check if data block has the same size as the previous one
185  if(!lstEpochDataList.isEmpty()) {
186  if(epoch->epoch.size() == lstEpochDataList.last()->epoch.size()) {
187  lstEpochDataList.append(MNEEpochData::SPtr(epoch.take()));//List takes ownwership of the pointer - no delete need
188  }
189  } else {
190  lstEpochDataList.append(MNEEpochData::SPtr(epoch.take()));//List takes ownwership of the pointer - no delete need
191  }
192  } else {
193  qWarning("[MNEEpochDataList::readEpochs] Can't read the event data segments.");
194  }
195  }
196 
197  qInfo().noquote() << "[MNEEpochDataList::readEpochs] Read a total of"<< lstEpochDataList.size() <<"epochs of type" << eventType << "and marked"<< dropCount <<"for rejection.";
198 
199  if(bApplyBaseline){
200  QPair<float, float> baselinePair(fTBaselineFromS, fTBaselineToS);
201  lstEpochDataList.applyBaselineCorrection(baselinePair);
202  }
203 
204  if(!mapReject.isEmpty()){
205  lstEpochDataList.dropRejected();
206  }
207 
208  return lstEpochDataList.average(raw.info,
209  0,
210  lstEpochDataList.first()->epoch.cols());
211 }
MNELIB::MNEEpochDataList::average
FIFFLIB::FiffEvoked average(const FIFFLIB::FiffInfo &p_info, FIFFLIB::fiff_int_t first, FIFFLIB::fiff_int_t last, Eigen::VectorXi sel=FIFFLIB::defaultVectorXi, bool proj=false)
Definition: mne_epoch_data_list.cpp:179
FIFFLIB::FiffRawData
FIFF raw measurement data.
Definition: fiff_raw_data.h:78
MNELIB::MNEEpochData::SPtr
QSharedPointer< MNEEpochData > SPtr
Definition: mne_epoch_data.h:77
FIFFLIB::FiffRawData::read_raw_segment
bool read_raw_segment(Eigen::MatrixXd &data, Eigen::MatrixXd &times, fiff_int_t from=-1, fiff_int_t to=-1, const Eigen::RowVectorXi &sel=defaultRowVectorXi, bool do_debug=false) const
RTPROCESSINGLIB::computeAverage
RTPROCESINGSHARED_EXPORT FIFFLIB::FiffEvoked computeAverage(const FIFFLIB::FiffRawData &raw, const Eigen::MatrixXi &matEvents, float fTMinS, float fTMaxS, qint32 eventType, bool bApplyBaseline, float fTBaselineFromS, float fTBaselineToS, const QMap< QString, double > &mapReject, const QStringList &lExcludeChs=QStringList(), const Eigen::RowVectorXi &vecPicks=Eigen::RowVectorXi())
filter.h
Filter declarations.
RTPROCESSINGLIB::computeFilteredAverage
RTPROCESINGSHARED_EXPORT FIFFLIB::FiffEvoked computeFilteredAverage(const FIFFLIB::FiffRawData &raw, const Eigen::MatrixXi &matEvents, float fTMinS, float fTMaxS, qint32 eventType, bool bApplyBaseline, float fTBaselineFromS, float fTBaselineToS, const QMap< QString, double > &mapReject, const FilterKernel &filterKernel, const QStringList &lExcludeChs=QStringList(), const Eigen::RowVectorXi &vecPicks=Eigen::RowVectorXi())
MNELIB::MNEEpochDataList::applyBaselineCorrection
void applyBaselineCorrection(const QPair< float, float > &baseline)
Definition: mne_epoch_data_list.cpp:240
MNELIB::MNEEpochDataList::dropRejected
void dropRejected()
Definition: mne_epoch_data_list.cpp:251
FIFFLIB::FiffInfo::sfreq
float sfreq
Definition: fiff_info.h:254
MNELIB::MNEEpochData
epoch data
Definition: mne_epoch_data.h:73
RTPROCESSINGLIB::filterData
RTPROCESINGSHARED_EXPORT Eigen::MatrixXd filterData(const Eigen::MatrixXd &mataData, const RTPROCESSINGLIB::FilterKernel &filterKernel, const Eigen::RowVectorXi &vecPicks=Eigen::RowVectorXi(), bool bUseThreads=true, bool bKeepOverhead=false)
FIFFLIB::FiffRawData::info
FiffInfo info
Definition: fiff_raw_data.h:197
FIFFLIB::FiffInfoBase::chs
QList< FiffChInfo > chs
Definition: fiff_info_base.h:223
MNELIB::MNEEpochDataList
Epoch data list.
Definition: mne_epoch_data_list.h:78
averaging.h
Averaging declarations.
FIFFLIB::FiffEvoked
evoked data
Definition: fiff_evoked.h:77
mne_epoch_data_list.h
MNEEpochDataList class declaration.
filterkernel.h
The FilterKernel class represents a filter object that generates the FIR filter coefficients using Pa...