MNE-CPP 0.1.9
A Framework for Electrophysiology
Loading...
Searching...
No Matches
mne_sourceestimate.cpp
Go to the documentation of this file.
1//=============================================================================================================
37//=============================================================================================================
38// INCLUDES
39//=============================================================================================================
40
41#include "mne_sourceestimate.h"
42
43//=============================================================================================================
44// QT INCLUDES
45//=============================================================================================================
46
47#include <QFile>
48#include <QDataStream>
49#include <QSharedPointer>
50#include <QDebug>
51
52//=============================================================================================================
53// USED NAMESPACES
54//=============================================================================================================
55
56using namespace MNELIB;
57using namespace FSLIB;
58using namespace Eigen;
59
60//=============================================================================================================
61// DEFINE MEMBER METHODS
62//=============================================================================================================
63
65: tmin(0)
66, tstep(-1)
67{
68}
69
70//=============================================================================================================
71
72MNESourceEstimate::MNESourceEstimate(const MatrixXd &p_sol, const VectorXi &p_vertices, float p_tmin, float p_tstep)
73: data(p_sol)
74, vertices(p_vertices)
75, tmin(p_tmin)
76, tstep(p_tstep)
77{
78 this->update_times();
79}
80
81//=============================================================================================================
82
84: data(p_SourceEstimate.data)
85, vertices(p_SourceEstimate.vertices)
86, times(p_SourceEstimate.times)
87, tmin(p_SourceEstimate.tmin)
88, tstep(p_SourceEstimate.tstep)
89{
90}
91
92//=============================================================================================================
93
95: tmin(0)
96, tstep(-1)
97{
98 if(!read(p_IODevice, *this))
99 {
100 printf("\tSource estimation not found.\n");//ToDo Throw here
101 return;
102 }
103}
104
105//=============================================================================================================
106
108{
109 data = MatrixXd();
110 vertices = VectorXi();
111 times = RowVectorXf();
112 tmin = 0;
113 tstep = 0;
114}
115
116//=============================================================================================================
117
119{
120 MNESourceEstimate p_sourceEstimateReduced;
121
122 qint32 rows = this->data.rows();
123
124 p_sourceEstimateReduced.data = MatrixXd::Zero(rows,n);
125 p_sourceEstimateReduced.data = this->data.block(0, start, rows, n);
126 p_sourceEstimateReduced.vertices = this->vertices;
127 p_sourceEstimateReduced.times = RowVectorXf::Zero(n);
128 p_sourceEstimateReduced.times = this->times.block(0,start,1,n);
129 p_sourceEstimateReduced.tmin = p_sourceEstimateReduced.times(0);
130 p_sourceEstimateReduced.tstep = this->tstep;
131
132 return p_sourceEstimateReduced;
133}
134
135//=============================================================================================================
136
137bool MNESourceEstimate::read(QIODevice &p_IODevice, MNESourceEstimate& p_stc)
138{
139 QSharedPointer<QDataStream> t_pStream(new QDataStream(&p_IODevice));
140
141 t_pStream->setFloatingPointPrecision(QDataStream::SinglePrecision);
142 t_pStream->setByteOrder(QDataStream::BigEndian);
143 t_pStream->setVersion(QDataStream::Qt_5_0);
144
145 if(!t_pStream->device()->open(QIODevice::ReadOnly))
146 return false;
147
148 QFile* t_pFile = qobject_cast<QFile*>(&p_IODevice);
149 if(t_pFile)
150 printf("Reading source estimate from %s...", t_pFile->fileName().toUtf8().constData());
151 else
152 printf("Reading source estimate...");
153
154 // read start time in ms
155 *t_pStream >> p_stc.tmin;
156 p_stc.tmin /= 1000;
157 // read sampling rate in ms
158 *t_pStream >> p_stc.tstep;
159 p_stc.tstep /= 1000;
160 // read number of vertices
161 quint32 t_nVertices;
162 *t_pStream >> t_nVertices;
163 p_stc.vertices = VectorXi(t_nVertices);
164 // read the vertex indices
165 for(quint32 i = 0; i < t_nVertices; ++i)
166 *t_pStream >> p_stc.vertices[i];
167 // read the number of timepts
168 quint32 t_nTimePts;
169 *t_pStream >> t_nTimePts;
170 //
171 // read the data
172 //
173 p_stc.data = MatrixXd(t_nVertices, t_nTimePts);
174 for(qint32 i = 0; i < p_stc.data.array().size(); ++i)
175 {
176 float value;
177 *t_pStream >> value;
178 p_stc.data.array()(i) = value;
179 }
180
181 //Update time vector
182 p_stc.update_times();
183
184 // close the file
185 t_pStream->device()->close();
186
187 printf("[done]\n");
188
189 return true;
190}
191
192//=============================================================================================================
193
194bool MNESourceEstimate::write(QIODevice &p_IODevice)
195{
196 // Create the file and save the essentials
197 QSharedPointer<QDataStream> t_pStream(new QDataStream(&p_IODevice));
198
199 t_pStream->setFloatingPointPrecision(QDataStream::SinglePrecision);
200 t_pStream->setByteOrder(QDataStream::BigEndian);
201 t_pStream->setVersion(QDataStream::Qt_5_0);
202
203 if(!t_pStream->device()->open(QIODevice::WriteOnly))
204 {
205 printf("Failed to write source estimate!\n");
206 return false;
207 }
208
209 QFile* t_pFile = qobject_cast<QFile*>(&p_IODevice);
210 if(t_pFile)
211 printf("Write source estimate to %s...", t_pFile->fileName().toUtf8().constData());
212 else
213 printf("Write source estimate...");
214
215 // write start time in ms
216 *t_pStream << (float)1000*this->tmin;
217 // write sampling rate in ms
218 *t_pStream << (float)1000*this->tstep;
219 // write number of vertices
220 *t_pStream << (quint32)this->vertices.size();
221 // write the vertex indices
222 for(qint32 i = 0; i < this->vertices.size(); ++i)
223 *t_pStream << (quint32)this->vertices[i];
224 // write the number of timepts
225 *t_pStream << (quint32)this->data.cols();
226 //
227 // write the data
228 //
229 for(qint32 i = 0; i < this->data.array().size(); ++i)
230 *t_pStream << (float)this->data.array()(i);
231
232 // close the file
233 t_pStream->device()->close();
234
235 printf("[done]\n");
236 return true;
237}
238
239//=============================================================================================================
240
241void MNESourceEstimate::update_times()
242{
243 if(data.cols() > 0)
244 {
245 this->times = RowVectorXf(data.cols());
246 this->times[0] = this->tmin;
247 for(float i = 1; i < this->times.size(); ++i)
248 this->times[i] = this->times[i-1] + this->tstep;
249 }
250 else
251 this->times = RowVectorXf();
252}
253
254//=============================================================================================================
255
257{
258 if (this != &rhs) // protect against invalid self-assignment
259 {
260 data = rhs.data;
261 vertices = rhs.vertices;
262 times = rhs.times;
263 tmin = rhs.tmin;
264 tstep = rhs.tstep;
265 }
266 // to support chained assignment operators (a=b=c), always return *this
267 return *this;
268}
269
270//=============================================================================================================
271
273{
274 return data.cols();
275}
276
277//=============================================================================================================
278
279VectorXi MNESourceEstimate::getIndicesByLabel(const QList<Label> &lPickedLabels, bool bIsClustered) const
280{
281 VectorXi vIndexSourceLabels;
282
283 if(lPickedLabels.isEmpty()) {
284 qWarning() << "MNESourceEstimate::getIndicesByLabel - picked label list is empty. Returning.";
285 return vIndexSourceLabels;
286 }
287
288 if(bIsClustered) {
289 for(int i = 0; i < this->vertices.rows(); i++) {
290 for(int k = 0; k < lPickedLabels.size(); k++) {
291 if(this->vertices(i) == lPickedLabels.at(k).label_id) {
292 vIndexSourceLabels.conservativeResize(vIndexSourceLabels.rows()+1,1);
293 vIndexSourceLabels(vIndexSourceLabels.rows()-1) = i;
294 break;
295 }
296 }
297 }
298 } else {
299 int hemi = 0;
300
301 for(int i = 0; i < this->vertices.rows(); i++) {
302 // Detect left right hemi separation
303 if(i > 0){
304 if(this->vertices(i) < this->vertices(i-1)){
305 hemi = 1;
306 }
307 }
308
309 for(int k = 0; k < lPickedLabels.size(); k++) {
310 for(int l = 0; l < lPickedLabels.at(k).vertices.rows(); l++) {
311 if(this->vertices(i) == lPickedLabels.at(k).vertices(l) && lPickedLabels.at(k).hemi == hemi) {
312 vIndexSourceLabels.conservativeResize(vIndexSourceLabels.rows()+1,1);
313 vIndexSourceLabels(vIndexSourceLabels.rows()-1) = i;
314 break;
315 }
316 }
317 }
318 }
319 }
320
321 return vIndexSourceLabels;
322}
int k
Definition fiff_tag.cpp:324
MNESourceEstimate class declaration.
static bool read(QIODevice &p_IODevice, MNESourceEstimate &p_stc)
Eigen::VectorXi getIndicesByLabel(const QList< FSLIB::Label > &lPickedLabels, bool bIsClustered) const
MNESourceEstimate reduce(qint32 start, qint32 n)
bool write(QIODevice &p_IODevice)
MNESourceEstimate & operator=(const MNESourceEstimate &rhs)