MNE-CPP 0.1.9
A Framework for Electrophysiology
Loading...
Searching...
No Matches
mne.cpp
Go to the documentation of this file.
1//=============================================================================================================
38//=============================================================================================================
39// INCLUDES
40//=============================================================================================================
41
42#include "mne.h"
43#include <fiff/fiff.h>
44#include <iostream>
45
46//=============================================================================================================
47// QT INCLUDES
48//=============================================================================================================
49
50#include <QFile>
51#include <QDebug>
52
53//=============================================================================================================
54// USED NAMESPACES
55//=============================================================================================================
56
57using namespace MNELIB;
58using namespace Eigen;
59using namespace FIFFLIB;
60
61//=============================================================================================================
62// DEFINE MEMBER METHODS
63//=============================================================================================================
64
65bool MNE::read_events(QString t_sEventName,
66 QString t_fileRawName,
67 MatrixXi& events)
68{
69 QFile t_EventFile;
70 qint32 p;
71
72 if (t_sEventName.isEmpty()) {
73 p = t_fileRawName.indexOf(".fif");
74 if (p > 0) {
75 t_sEventName = t_fileRawName.replace(p, 4, "-eve.fif");
76 } else {
77 printf("Raw file name does not end properly\n");
78 return 0;
79 }
80
81 t_EventFile.setFileName(t_sEventName);
82 if(!MNE::read_events_from_fif(t_EventFile, events)) {
83 printf("Error while read events.\n");
84 return false;
85 }
86 printf("Events read from %s\n",t_sEventName.toUtf8().constData());
87 } else {
88 // Binary file
89 if (t_sEventName.contains(".fif")) {
90 t_EventFile.setFileName(t_sEventName);
91 if(!MNE::read_events_from_fif(t_EventFile, events)) {
92 printf("Error while read events.\n");
93 return false;
94 }
95 printf("Binary event file %s read\n",t_sEventName.toUtf8().constData());
96 } else if(t_sEventName.contains(".eve")){
97
98 } else {
99 // Text file
100 printf("Text file %s is not supported jet.\n",t_sEventName.toUtf8().constData());
101// try
102// events = load(eventname);
103// catch
104// error(me,mne_omit_first_line(lasterr));
105// end
106// if size(events,1) < 1
107// error(me,'No data in the event file');
108// end
109// //
110// // Convert time to samples if sample number is negative
111// //
112// for p = 1:size(events,1)
113// if events(p,1) < 0
114// events(p,1) = events(p,2)*raw.info.sfreq;
115// end
116// end
117// //
118// // Select the columns of interest (convert to integers)
119// //
120// events = int32(events(:,[1 3 4]));
121// //
122// // New format?
123// //
124// if events(1,2) == 0 && events(1,3) == 0
125// fprintf(1,'The text event file %s is in the new format\n',eventname);
126// if events(1,1) ~= raw.first_samp
127// error(me,'This new format event file is not compatible with the raw data');
128// end
129// else
130// fprintf(1,'The text event file %s is in the old format\n',eventname);
131// //
132// // Offset with first sample
133// //
134// events(:,1) = events(:,1) + raw.first_samp;
135// end
136 }
137 }
138
139 return true;
140}
141
142//=============================================================================================================
143
144bool MNE::read_events_from_fif(QIODevice &p_IODevice,
145 MatrixXi& eventlist)
146{
147 //
148 // Open file
149 //
150 FiffStream::SPtr t_pStream(new FiffStream(&p_IODevice));
151
152 if(!t_pStream->open()) {
153 return false;
154 }
155
156 //
157 // Find the desired block
158 //
159 QList<FiffDirNode::SPtr> events = t_pStream->dirtree()->dir_tree_find(FIFFB_MNE_EVENTS);
160
161 if (events.size() == 0)
162 {
163 printf("Could not find event data\n");
164 return false;
165 }
166
167 qint32 k, nelem;
168 fiff_int_t kind, pos;
169 FiffTag::SPtr t_pTag;
170 quint32* serial_eventlist_uint = NULL;
171 qint32* serial_eventlist_int = NULL;
172
173 for(k = 0; k < events[0]->nent(); ++k)
174 {
175 kind = events[0]->dir[k]->kind;
176 pos = events[0]->dir[k]->pos;
177 if (kind == FIFF_MNE_EVENT_LIST)
178 {
179 t_pStream->read_tag(t_pTag,pos);
180 if(t_pTag->type == FIFFT_UINT)
181 {
182 serial_eventlist_uint = t_pTag->toUnsignedInt();
183 nelem = t_pTag->size()/4;
184 }
185
186 if(t_pTag->type == FIFFT_INT)
187 {
188 serial_eventlist_int = t_pTag->toInt();
189 nelem = t_pTag->size()/4;
190 }
191
192 break;
193 }
194 }
195
196 if(serial_eventlist_uint == NULL && serial_eventlist_int == NULL)
197 {
198 printf("Could not find any events\n");
199 return false;
200 }
201 else
202 {
203 eventlist.resize(nelem/3,3);
204 if(serial_eventlist_uint != NULL)
205 {
206 for(k = 0; k < nelem/3; ++k)
207 {
208 eventlist(k,0) = serial_eventlist_uint[k*3];
209 eventlist(k,1) = serial_eventlist_uint[k*3+1];
210 eventlist(k,2) = serial_eventlist_uint[k*3+2];
211 }
212 }
213
214 if(serial_eventlist_int != NULL)
215 {
216 for(k = 0; k < nelem/3; ++k)
217 {
218 eventlist(k,0) = serial_eventlist_int[k*3];
219 eventlist(k,1) = serial_eventlist_int[k*3+1];
220 eventlist(k,2) = serial_eventlist_int[k*3+2];
221 }
222 }
223 }
224
225 return true;
226}
227
228//=============================================================================================================
229
230void MNE::setup_compensators(FiffRawData& raw,
231 fiff_int_t dest_comp,
232 bool keep_comp)
233{
234 // Set up projection
235 if (raw.info.projs.size() == 0) {
236 printf("No projector specified for these data\n");
237 } else {
238 // Activate the projection items
239 for (qint32 k = 0; k < raw.info.projs.size(); ++k) {
240 raw.info.projs[k].active = true;
241 }
242
243 printf("%lld projection items activated\n",raw.info.projs.size());
244 // Create the projector
245// fiff_int_t nproj = MNE::make_projector_info(raw.info, raw.proj); Using the member function instead
246 fiff_int_t nproj = raw.info.make_projector(raw.proj);
247
248 if (nproj == 0) {
249 printf("The projection vectors do not apply to these channels\n");
250 } else {
251 printf("Created an SSP operator (subspace dimension = %d)\n",nproj);
252 }
253 }
254
255 // Set up the CTF compensator
256// qint32 current_comp = MNE::get_current_comp(raw.info);
257 qint32 current_comp = raw.info.get_current_comp();
258 if (current_comp > 0)
259 printf("Current compensation grade : %d\n",current_comp);
260
261 if (keep_comp)
262 dest_comp = current_comp;
263
264 if (current_comp != dest_comp)
265 {
266 qDebug() << "This part needs to be debugged";
267 if(MNE::make_compensator(raw.info, current_comp, dest_comp, raw.comp))
268 {
269// raw.info.chs = MNE::set_current_comp(raw.info.chs,dest_comp);
270 raw.info.set_current_comp(dest_comp);
271 printf("Appropriate compensator added to change to grade %d.\n",dest_comp);
272 }
273 else
274 {
275 printf("Could not make the compensator\n");
276 return;
277 }
278 }
279}
280
281//=============================================================================================================
282
283bool MNE::read_events_from_ascii(QIODevice &p_IODevice,
284 Eigen::MatrixXi& eventlist)
285{
286 if (!p_IODevice.open(QIODevice::ReadOnly | QIODevice::Text)){
287 return false;
288 }
289 QTextStream textStream(&p_IODevice);
290
291 QList<int> simpleList;
292
293 while(!textStream.atEnd()){
294 int iSample;
295 textStream >> iSample;
296 simpleList.append(iSample);
297 textStream.readLine();
298 qDebug() << "Added event:" << iSample;
299 }
300
301 eventlist.resize(simpleList.size(), 1);
302
303 for(int i = 0; i < simpleList.size(); i++){
304 eventlist(i,0) = simpleList[i];
305 }
306 return true;
307}
FIFF class declaration, which provides static wrapper functions to stay consistent with mne matlab to...
#define FIFF_MNE_EVENT_LIST
int k
Definition fiff_tag.cpp:324
MNE class declaration, which provides static wrapper functions to stay consistent with mne matlab too...
void set_current_comp(fiff_int_t value)
Definition fiff_info.h:292
qint32 make_projector(Eigen::MatrixXd &proj) const
Definition fiff_info.h:278
qint32 get_current_comp()
QList< FiffProj > projs
Definition fiff_info.h:268
FIFF raw measurement data.
Eigen::MatrixXd proj
FIFF File I/O routines.
QSharedPointer< FiffStream > SPtr
QSharedPointer< FiffTag > SPtr
Definition fiff_tag.h:152
static bool make_compensator(const FIFFLIB::FiffInfo &info, FIFFLIB::fiff_int_t from, FIFFLIB::fiff_int_t to, FIFFLIB::FiffCtfComp &ctf_comp, bool exclude_comp_chs=false)
Definition mne.h:207
static bool read_events_from_fif(QIODevice &p_IODevice, Eigen::MatrixXi &eventlist)
Definition mne.cpp:144
static bool read_events_from_ascii(QIODevice &p_IODevice, Eigen::MatrixXi &eventlist)
Definition mne.cpp:283