v2.0.0
Loading...
Searching...
No Matches
inv_source_estimate.cpp
Go to the documentation of this file.
1//=============================================================================================================
36
37//=============================================================================================================
38// INCLUDES
39//=============================================================================================================
40
41#include "inv_source_estimate.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 INVLIB;
57using namespace FSLIB;
58using namespace Eigen;
59
60//=============================================================================================================
61// DEFINE MEMBER METHODS
62//=============================================================================================================
63
72
73//=============================================================================================================
74
75InvSourceEstimate::InvSourceEstimate(const MatrixXd &p_sol, const VectorXi &p_vertices, float p_tmin, float p_tstep)
76: data(p_sol)
77, vertices(p_vertices)
78, tmin(p_tmin)
79, tstep(p_tstep)
81, sourceSpaceType(InvSourceSpaceType::Unknown)
82, orientationType(InvOrientationType::Unknown)
83{
84 this->update_times();
85}
86
87//=============================================================================================================
88
90: data(p_SourceEstimate.data)
91, vertices(p_SourceEstimate.vertices)
92, times(p_SourceEstimate.times)
93, tmin(p_SourceEstimate.tmin)
94, tstep(p_SourceEstimate.tstep)
95, method(p_SourceEstimate.method)
96, sourceSpaceType(p_SourceEstimate.sourceSpaceType)
97, orientationType(p_SourceEstimate.orientationType)
98, positions(p_SourceEstimate.positions)
99, couplings(p_SourceEstimate.couplings)
100, focalDipoles(p_SourceEstimate.focalDipoles)
101, connectivity(p_SourceEstimate.connectivity)
102{
103}
104
105//=============================================================================================================
106
108: tmin(0)
109, tstep(-1)
113{
114 if(!read(p_IODevice, *this))
115 {
116 qWarning("Source estimation not found.");//ToDo Throw here
117 return;
118 }
119}
120
121//=============================================================================================================
122
124{
125 data = MatrixXd();
126 vertices = VectorXi();
127 times = RowVectorXf();
128 tmin = 0;
129 tstep = 0;
133 positions = MatrixX3f();
134 couplings.clear();
135 focalDipoles.clear();
136 connectivity.clear();
137}
138
139//=============================================================================================================
140
142{
143 InvSourceEstimate p_sourceEstimateReduced;
144
145 qint32 rows = this->data.rows();
146
147 p_sourceEstimateReduced.data = MatrixXd::Zero(rows,n);
148 p_sourceEstimateReduced.data = this->data.block(0, start, rows, n);
149 p_sourceEstimateReduced.vertices = this->vertices;
150 p_sourceEstimateReduced.times = RowVectorXf::Zero(n);
151 p_sourceEstimateReduced.times = this->times.block(0,start,1,n);
152 p_sourceEstimateReduced.tmin = p_sourceEstimateReduced.times(0);
153 p_sourceEstimateReduced.tstep = this->tstep;
154 p_sourceEstimateReduced.method = this->method;
155 p_sourceEstimateReduced.sourceSpaceType = this->sourceSpaceType;
156 p_sourceEstimateReduced.orientationType = this->orientationType;
157 p_sourceEstimateReduced.positions = this->positions;
158 p_sourceEstimateReduced.couplings = this->couplings;
159 p_sourceEstimateReduced.focalDipoles = this->focalDipoles;
160 p_sourceEstimateReduced.connectivity = this->connectivity;
161
162 return p_sourceEstimateReduced;
163}
164
165//=============================================================================================================
166
167bool InvSourceEstimate::read(QIODevice &p_IODevice, InvSourceEstimate& p_stc)
168{
169 QSharedPointer<QDataStream> t_pStream(new QDataStream(&p_IODevice));
170
171 t_pStream->setFloatingPointPrecision(QDataStream::SinglePrecision);
172 t_pStream->setByteOrder(QDataStream::BigEndian);
173 t_pStream->setVersion(QDataStream::Qt_5_0);
174
175 if(!t_pStream->device()->open(QIODevice::ReadOnly))
176 return false;
177
178 QFile* t_pFile = qobject_cast<QFile*>(&p_IODevice);
179 if(t_pFile)
180 qInfo("Reading source estimate from %s...", t_pFile->fileName().toUtf8().constData());
181 else
182 qInfo("Reading source estimate...");
183
184 // read start time in ms
185 *t_pStream >> p_stc.tmin;
186 p_stc.tmin /= 1000;
187 // read sampling rate in ms
188 *t_pStream >> p_stc.tstep;
189 p_stc.tstep /= 1000;
190 // read number of vertices
191 quint32 t_nVertices;
192 *t_pStream >> t_nVertices;
193 p_stc.vertices = VectorXi(t_nVertices);
194 // read the vertex indices
195 for(quint32 i = 0; i < t_nVertices; ++i)
196 *t_pStream >> p_stc.vertices[i];
197 // read the number of timepts
198 quint32 t_nTimePts;
199 *t_pStream >> t_nTimePts;
200 //
201 // read the data
202 //
203 p_stc.data = MatrixXd(t_nVertices, t_nTimePts);
204 for(qint32 i = 0; i < p_stc.data.array().size(); ++i)
205 {
206 float value;
207 *t_pStream >> value;
208 p_stc.data.array()(i) = value;
209 }
210
211 //Update time vector
212 p_stc.update_times();
213
214 // close the file
215 t_pStream->device()->close();
216
217 qInfo("[done]");
218
219 return true;
220}
221
222//=============================================================================================================
223
224bool InvSourceEstimate::write(QIODevice &p_IODevice)
225{
226 // Create the file and save the essentials
227 QSharedPointer<QDataStream> t_pStream(new QDataStream(&p_IODevice));
228
229 t_pStream->setFloatingPointPrecision(QDataStream::SinglePrecision);
230 t_pStream->setByteOrder(QDataStream::BigEndian);
231 t_pStream->setVersion(QDataStream::Qt_5_0);
232
233 if(!t_pStream->device()->open(QIODevice::WriteOnly))
234 {
235 qWarning("Failed to write source estimate!");
236 return false;
237 }
238
239 QFile* t_pFile = qobject_cast<QFile*>(&p_IODevice);
240 if(t_pFile)
241 qInfo("Write source estimate to %s...", t_pFile->fileName().toUtf8().constData());
242 else
243 qInfo("Write source estimate...");
244
245 // write start time in ms
246 *t_pStream << static_cast<float>(1000*this->tmin);
247 // write sampling rate in ms
248 *t_pStream << static_cast<float>(1000*this->tstep);
249 // write number of vertices
250 *t_pStream << static_cast<quint32>(this->vertices.size());
251 // write the vertex indices
252 for(qint32 i = 0; i < this->vertices.size(); ++i)
253 *t_pStream << static_cast<quint32>(this->vertices[i]);
254 // write the number of timepts
255 *t_pStream << static_cast<quint32>(this->data.cols());
256 //
257 // write the data
258 //
259 for(qint32 i = 0; i < this->data.array().size(); ++i)
260 *t_pStream << static_cast<float>(this->data.array()(i));
261
262 // close the file
263 t_pStream->device()->close();
264
265 qInfo("[done]");
266 return true;
267}
268
269//=============================================================================================================
270
271void InvSourceEstimate::update_times()
272{
273 if(data.cols() > 0)
274 {
275 this->times = RowVectorXf(data.cols());
276 this->times[0] = this->tmin;
277 for(float i = 1; i < this->times.size(); ++i)
278 this->times[i] = this->times[i-1] + this->tstep;
279 }
280 else
281 this->times = RowVectorXf();
282}
283
284//=============================================================================================================
285
287{
288 if (this != &rhs) // protect against invalid self-assignment
289 {
290 data = rhs.data;
291 vertices = rhs.vertices;
292 times = rhs.times;
293 tmin = rhs.tmin;
294 tstep = rhs.tstep;
295 method = rhs.method;
298 positions = rhs.positions;
299 couplings = rhs.couplings;
302 }
303 // to support chained assignment operators (a=b=c), always return *this
304 return *this;
305}
306
307//=============================================================================================================
308
310{
311 return data.cols();
312}
313
314//=============================================================================================================
315
316VectorXi InvSourceEstimate::getIndicesByLabel(const QList<FsLabel> &lPickedLabels, bool bIsClustered) const
317{
318 VectorXi vIndexSourceLabels;
319
320 if(lPickedLabels.isEmpty()) {
321 qWarning() << "InvSourceEstimate::getIndicesByLabel - picked label list is empty. Returning.";
322 return vIndexSourceLabels;
323 }
324
325 if(bIsClustered) {
326 for(int i = 0; i < this->vertices.rows(); i++) {
327 for(int k = 0; k < lPickedLabels.size(); k++) {
328 if(this->vertices(i) == lPickedLabels.at(k).label_id) {
329 vIndexSourceLabels.conservativeResize(vIndexSourceLabels.rows()+1,1);
330 vIndexSourceLabels(vIndexSourceLabels.rows()-1) = i;
331 break;
332 }
333 }
334 }
335 } else {
336 int hemi = 0;
337
338 for(int i = 0; i < this->vertices.rows(); i++) {
339 // Detect left right hemi separation
340 if(i > 0){
341 if(this->vertices(i) < this->vertices(i-1)){
342 hemi = 1;
343 }
344 }
345
346 for(int k = 0; k < lPickedLabels.size(); k++) {
347 for(int l = 0; l < lPickedLabels.at(k).vertices.rows(); l++) {
348 if(this->vertices(i) == lPickedLabels.at(k).vertices(l) && lPickedLabels.at(k).hemi == hemi) {
349 vIndexSourceLabels.conservativeResize(vIndexSourceLabels.rows()+1,1);
350 vIndexSourceLabels(vIndexSourceLabels.rows()-1) = i;
351 break;
352 }
353 }
354 }
355 }
356 }
357
358 return vIndexSourceLabels;
359}
InvSourceEstimate class declaration.
FreeSurfer surface and annotation I/O.
Inverse source estimation (MNE, dSPM, sLORETA, dipole fitting).
InvEstimateMethod
Definition inv_types.h:50
InvOrientationType
Definition inv_types.h:84
InvSourceSpaceType
Definition inv_types.h:71
std::vector< InvSourceCoupling > couplings
std::vector< InvFocalDipole > focalDipoles
Eigen::VectorXi getIndicesByLabel(const QList< FSLIB::FsLabel > &lPickedLabels, bool bIsClustered) const
static bool read(QIODevice &p_IODevice, InvSourceEstimate &p_stc)
InvSourceEstimate & operator=(const InvSourceEstimate &rhs)
bool write(QIODevice &p_IODevice)
InvSourceSpaceType sourceSpaceType
InvOrientationType orientationType
std::vector< InvConnectivity > connectivity
InvSourceEstimate reduce(qint32 start, qint32 n)