MNE-CPP  beta 1.0
main.cpp
1 //=============================================================================================================
36 //*************************************************************************************************************
37 //=============================================================================================================
38 // INCLUDES
39 //=============================================================================================================
40 
41 #include <iostream>
42 #include <vector>
43 #include <math.h>
44 
45 
46 //*************************************************************************************************************
47 //=============================================================================================================
48 // MNE INCLUDES
49 //=============================================================================================================
50 
51 #include <fiff/fiff.h>
52 #include <mne/mne.h>
53 
55 
56 
57 //*************************************************************************************************************
58 //=============================================================================================================
59 // QT INCLUDES
60 //=============================================================================================================
61 
62 #include <QtCore/QCoreApplication>
63 
64 
65 //*************************************************************************************************************
66 //=============================================================================================================
67 // USED NAMESPACES
68 //=============================================================================================================
69 
70 using namespace FIFFLIB;
71 using namespace MNELIB;
72 
73 
74 //*************************************************************************************************************
75 //=============================================================================================================
76 // MAIN
77 //=============================================================================================================
78 
79 //=============================================================================================================
88 int main(int argc, char *argv[])
89 {
90  QCoreApplication a(argc, argv);
91 
92  QFile t_fileRaw("./MNE-sample-data/MEG/sample/sample_audvis_raw.fif");
93 
94  qint32 event = 1;
95  QString t_sEventName = "./MNE-sample-data/MEG/sample/sample_audvis_raw-eve.fif";
96  float tmin = -1.5;
97  float tmax = 1.5;
98 
99  bool keep_comp = false;
100  fiff_int_t dest_comp = 0;
101  bool pick_all = true;
102 
103  qint32 k, p;
104 
105  //
106  // Setup for reading the raw data
107  //
108  FiffRawData raw(t_fileRaw);
109 
110  RowVectorXi picks;
111  if (pick_all)
112  {
113  //
114  // Pick all
115  //
116  picks.resize(raw.info.nchan);
117 
118  for(k = 0; k < raw.info.nchan; ++k)
119  picks(k) = k;
120  //
121  }
122  else
123  {
124  QStringList include;
125  include << "STI 014";
126  bool want_meg = true;
127  bool want_eeg = false;
128  bool want_stim = false;
129 
130 // picks = Fiff::pick_types(raw.info, want_meg, want_eeg, want_stim, include, raw.info.bads);
131  picks = raw.info.pick_types(want_meg, want_eeg, want_stim, include, raw.info.bads);//prefer member function
132  }
133 
134  QStringList ch_names;
135  for(k = 0; k < picks.cols(); ++k)
136  ch_names << raw.info.ch_names[picks(0,k)];
137 
138  //
139  // Set up projection
140  //
141  if (raw.info.projs.size() == 0)
142  printf("No projector specified for these data\n");
143  else
144  {
145  //
146  // Activate the projection items
147  //
148  for (k = 0; k < raw.info.projs.size(); ++k)
149  raw.info.projs[k].active = true;
150 
151  printf("%d projection items activated\n",raw.info.projs.size());
152  //
153  // Create the projector
154  //
155 // fiff_int_t nproj = MNE::make_projector_info(raw.info, raw.proj); Using the member function instead
156  fiff_int_t nproj = raw.info.make_projector(raw.proj);
157 
158  if (nproj == 0)
159  {
160  printf("The projection vectors do not apply to these channels\n");
161  }
162  else
163  {
164  printf("Created an SSP operator (subspace dimension = %d)\n",nproj);
165  }
166  }
167 
168  //
169  // Set up the CTF compensator
170  //
171 // qint32 current_comp = MNE::get_current_comp(raw.info);
172  qint32 current_comp = raw.info.get_current_comp();
173  if (current_comp > 0)
174  printf("Current compensation grade : %d\n",current_comp);
175 
176  if (keep_comp)
177  dest_comp = current_comp;
178 
179  if (current_comp != dest_comp)
180  {
181  qDebug() << "This part needs to be debugged";
182  if(MNE::make_compensator(raw.info, current_comp, dest_comp, raw.comp))
183  {
184 // raw.info.chs = MNE::set_current_comp(raw.info.chs,dest_comp);
185  raw.info.set_current_comp(dest_comp);
186  printf("Appropriate compensator added to change to grade %d.\n",dest_comp);
187  }
188  else
189  {
190  printf("Could not make the compensator\n");
191  return 0;
192  }
193  }
194  //
195  // Read the events
196  //
197  QFile t_EventFile;
198  MatrixXi events;
199  if (t_sEventName.size() == 0)
200  {
201  p = t_fileRaw.fileName().indexOf(".fif");
202  if (p > 0)
203  {
204  t_sEventName = t_fileRaw.fileName().replace(p, 4, "-eve.fif");
205  }
206  else
207  {
208  printf("Raw file name does not end properly\n");
209  return 0;
210  }
211 // events = mne_read_events(t_sEventName);
212 
213  t_EventFile.setFileName(t_sEventName);
214  MNE::read_events(t_EventFile, events);
215  printf("Events read from %s\n",t_sEventName.toUtf8().constData());
216  }
217  else
218  {
219  //
220  // Binary file
221  //
222  p = t_fileRaw.fileName().indexOf(".fif");
223  if (p > 0)
224  {
225  t_EventFile.setFileName(t_sEventName);
226  if(!MNE::read_events(t_EventFile, events))
227  {
228  printf("Error while read events.\n");
229  return 0;
230  }
231  printf("Binary event file %s read\n",t_sEventName.toUtf8().constData());
232  }
233  else
234  {
235  //
236  // Text file
237  //
238  printf("Text file %s is not supported jet.\n",t_sEventName.toUtf8().constData());
239 // try
240 // events = load(eventname);
241 // catch
242 // error(me,mne_omit_first_line(lasterr));
243 // end
244 // if size(events,1) < 1
245 // error(me,'No data in the event file');
246 // end
247 // //
248 // // Convert time to samples if sample number is negative
249 // //
250 // for p = 1:size(events,1)
251 // if events(p,1) < 0
252 // events(p,1) = events(p,2)*raw.info.sfreq;
253 // end
254 // end
255 // //
256 // // Select the columns of interest (convert to integers)
257 // //
258 // events = int32(events(:,[1 3 4]));
259 // //
260 // // New format?
261 // //
262 // if events(1,2) == 0 && events(1,3) == 0
263 // fprintf(1,'The text event file %s is in the new format\n',eventname);
264 // if events(1,1) ~= raw.first_samp
265 // error(me,'This new format event file is not compatible with the raw data');
266 // end
267 // else
268 // fprintf(1,'The text event file %s is in the old format\n',eventname);
269 // //
270 // // Offset with first sample
271 // //
272 // events(:,1) = events(:,1) + raw.first_samp;
273 // end
274  }
275  }
276  //
277  // Select the desired events
278  //
279  qint32 count = 0;
280  MatrixXi selected = MatrixXi::Zero(1, events.rows());
281  for (p = 0; p < events.rows(); ++p)
282  {
283  if (events(p,1) == 0 && events(p,2) == event)
284  {
285  selected(0,count) = p;
286  ++count;
287  }
288  }
289  selected.conservativeResize(1, count);
290  if (count > 0)
291  printf("%d matching events found\n",count);
292  else
293  {
294  printf("No desired events found.\n");
295  return 0;
296  }
297 
298 
299  fiff_int_t event_samp, from, to;
300  MatrixXd timesDummy;
301 
302  MNEEpochDataList data;
303 
304  MNEEpochData* epoch = NULL;
305 
306  MatrixXd times;
307 
308  for (p = 0; p < count; ++p)
309  {
310  //
311  // Read a data segment
312  //
313  event_samp = events(selected(p),0);
314  from = event_samp + tmin*raw.info.sfreq;
315  to = event_samp + floor(tmax*raw.info.sfreq + 0.5);
316 
317  epoch = new MNEEpochData();
318 
319  if(raw.read_raw_segment(epoch->epoch, timesDummy, from, to, picks))
320  {
321  if (p == 0)
322  {
323  times.resize(1, to-from+1);
324  for (qint32 i = 0; i < times.cols(); ++i)
325  times(0, i) = ((float)(from-event_samp+i)) / raw.info.sfreq;
326  }
327 
328  epoch->event = event;
329  epoch->tmin = ((float)(from)-(float)(raw.first_samp))/raw.info.sfreq;
330  epoch->tmax = ((float)(to)-(float)(raw.first_samp))/raw.info.sfreq;
331 
332  data.append(MNEEpochData::SPtr(epoch));//List takes ownwership of the pointer - no delete need
333  }
334  else
335  {
336  printf("Can't read the event data segments");
337  return 0;
338  }
339  }
340 
341  //Example for average_epochs
342  data.average(raw.info,raw.first_samp,raw.last_samp);
343 
344  return a.exec();
345 }
346 
347 //*************************************************************************************************************
348 //=============================================================================================================
349 // STATIC DEFINITIONS
350 //=============================================================================================================
MNEEpochDataList class declaration.
FIFF raw measurement data.
Definition: fiff_raw_data.h:94
Definition: fiff.h:98
FiffEvoked average(FiffInfo &p_info, fiff_int_t first, fiff_int_t last, VectorXi sel=defaultVectorXi, bool proj=false)
QSharedPointer< MNEEpochData > SPtr
FIFF class declaration, which provides static wrapper functions to stay consistent with mne matlab to...