MNE-CPP  beta 1.0
mne_sourceestimate.cpp
Go to the documentation of this file.
1 //=============================================================================================================
36 //*************************************************************************************************************
37 //=============================================================================================================
38 // INCLUDES
39 //=============================================================================================================
40 
41 #include "mne_sourceestimate.h"
42 
43 #include <QFile>
44 #include <QDataStream>
45 #include <QSharedPointer>
46 
47 
48 //*************************************************************************************************************
49 //=============================================================================================================
50 // USED NAMESPACES
51 //=============================================================================================================
52 
53 using namespace MNELIB;
54 
55 
56 //*************************************************************************************************************
57 //=============================================================================================================
58 // DEFINE MEMBER METHODS
59 //=============================================================================================================
60 
62 : tmin(0)
63 , tstep(-1)
64 {
65 }
66 
67 
68 //*************************************************************************************************************
69 
70 MNESourceEstimate::MNESourceEstimate(const MatrixXd &p_sol, const VectorXi &p_vertices, float p_tmin, float p_tstep)
71 : data(p_sol)
72 , vertices(p_vertices)
73 , tmin(p_tmin)
74 , tstep(p_tstep)
75 {
76  this->update_times();
77 }
78 
79 
80 //*************************************************************************************************************
81 
83 : data(p_SourceEstimate.data)
84 , vertices(p_SourceEstimate.vertices)
85 , times(p_SourceEstimate.times)
86 , tmin(p_SourceEstimate.tmin)
87 , tstep(p_SourceEstimate.tstep)
88 {
89 
90 }
91 
92 
93 //*************************************************************************************************************
94 
96 : tmin(0)
97 , tstep(-1)
98 {
99  if(!read(p_IODevice, *this))
100  {
101  printf("\tSource estimation not found.\n");//ToDo Throw here
102  return;
103  }
104 }
105 
106 
107 //*************************************************************************************************************
108 
110 {
111  data = MatrixXd();
112  vertices = VectorXi();
113  times = RowVectorXf();
114  tmin = 0;
115  tstep = 0;
116 }
117 
118 
119 //*************************************************************************************************************
120 
122 {
123  MNESourceEstimate p_sourceEstimateReduced;
124 
125  qint32 rows = this->data.rows();
126 
127  p_sourceEstimateReduced.data = MatrixXd::Zero(rows,n);
128  p_sourceEstimateReduced.data = this->data.block(0, start, rows, n);
129  p_sourceEstimateReduced.vertices = this->vertices;
130  p_sourceEstimateReduced.times = RowVectorXf::Zero(n);
131  p_sourceEstimateReduced.times = this->times.block(0,start,1,n);
132  p_sourceEstimateReduced.tmin = p_sourceEstimateReduced.times(0);
133  p_sourceEstimateReduced.tstep = this->tstep;
134 
135  return p_sourceEstimateReduced;
136 }
137 
138 
139 //*************************************************************************************************************
140 
141 bool MNESourceEstimate::read(QIODevice &p_IODevice, MNESourceEstimate& p_stc)
142 {
143  QSharedPointer<QDataStream> t_pStream(new QDataStream(&p_IODevice));
144 
145  t_pStream->setFloatingPointPrecision(QDataStream::SinglePrecision);
146  t_pStream->setByteOrder(QDataStream::BigEndian);
147  t_pStream->setVersion(QDataStream::Qt_5_0);
148 
149  if(!t_pStream->device()->open(QIODevice::ReadOnly))
150  return false;
151 
152  QFile* t_pFile = qobject_cast<QFile*>(&p_IODevice);
153  if(t_pFile)
154  printf("Reading source estimate from %s...", t_pFile->fileName().toUtf8().constData());
155  else
156  printf("Reading source estimate...");
157 
158  // read start time in ms
159  *t_pStream >> p_stc.tmin;
160  p_stc.tmin /= 1000;
161  // read sampling rate in ms
162  *t_pStream >> p_stc.tstep;
163  p_stc.tstep /= 1000;
164  // read number of vertices
165  quint32 t_nVertices;
166  *t_pStream >> t_nVertices;
167  p_stc.vertices = VectorXi(t_nVertices);
168  // read the vertex indices
169  for(quint32 i = 0; i < t_nVertices; ++i)
170  *t_pStream >> p_stc.vertices[i];
171  // read the number of timepts
172  quint32 t_nTimePts;
173  *t_pStream >> t_nTimePts;
174  //
175  // read the data
176  //
177  p_stc.data = MatrixXd(t_nVertices, t_nTimePts);
178  for(qint32 i = 0; i < p_stc.data.array().size(); ++i)
179  {
180  float value;
181  *t_pStream >> value;
182  p_stc.data.array()(i) = value;
183  }
184 
185  //Update time vector
186  p_stc.update_times();
187 
188  // close the file
189  t_pStream->device()->close();
190 
191  printf("[done]\n");
192 
193  return true;
194 }
195 
196 
197 //*************************************************************************************************************
198 
199 bool MNESourceEstimate::write(QIODevice &p_IODevice)
200 {
201  // Create the file and save the essentials
202  QSharedPointer<QDataStream> t_pStream(new QDataStream(&p_IODevice));
203 
204  t_pStream->setFloatingPointPrecision(QDataStream::SinglePrecision);
205  t_pStream->setByteOrder(QDataStream::BigEndian);
206  t_pStream->setVersion(QDataStream::Qt_5_0);
207 
208  if(!t_pStream->device()->open(QIODevice::WriteOnly))
209  {
210  printf("Failed to write source estimate!\n");
211  return false;
212  }
213 
214  QFile* t_pFile = qobject_cast<QFile*>(&p_IODevice);
215  if(t_pFile)
216  printf("Write source estimate to %s...", t_pFile->fileName().toUtf8().constData());
217  else
218  printf("Write source estimate...");
219 
220  // write start time in ms
221  *t_pStream << (float)1000*this->tmin;
222  // write sampling rate in ms
223  *t_pStream << (float)1000*this->tstep;
224  // write number of vertices
225  *t_pStream << (quint32)this->vertices.size();
226  // write the vertex indices
227  for(qint32 i = 0; i < this->vertices.size(); ++i)
228  *t_pStream << (quint32)this->vertices[i];
229  // write the number of timepts
230  *t_pStream << (quint32)this->data.cols();
231  //
232  // write the data
233  //
234  for(qint32 i = 0; i < this->data.array().size(); ++i)
235  *t_pStream << (float)this->data.array()(i);
236 
237  // close the file
238  t_pStream->device()->close();
239 
240  printf("[done]\n");
241  return true;
242 }
243 
244 
245 //*************************************************************************************************************
246 
247 void MNESourceEstimate::update_times()
248 {
249  if(data.cols() > 0)
250  {
251  this->times = RowVectorXf(data.cols());
252  this->times[0] = this->tmin;
253  for(float i = 1; i < this->times.size(); ++i)
254  this->times[i] = this->times[i-1] + this->tstep;
255  }
256  else
257  this->times = RowVectorXf();
258 }
259 
260 
261 //*************************************************************************************************************
262 
264 {
265  if (this != &rhs) // protect against invalid self-assignment
266  {
267  data = rhs.data;
268  vertices = rhs.vertices;
269  times = rhs.times;
270  tmin = rhs.tmin;
271  tstep = rhs.tstep;
272  }
273  // to support chained assignment operators (a=b=c), always return *this
274  return *this;
275 }
MNESourceEstimate reduce(qint32 start, qint32 n)
MNESourceEstimate & operator=(const MNESourceEstimate &rhs)
bool write(QIODevice &p_IODevice)
MNESourceEstimate class declaration.
static bool read(QIODevice &p_IODevice, MNESourceEstimate &p_stc)