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