forked from mne-tools/mne-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmne.cpp
308 lines (271 loc) · 10.6 KB
/
mne.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//=============================================================================================================
/**
* @file mne.cpp
* @author Lorenz Esch <[email protected]>;
* Matti Hamalainen <[email protected]>;
* Christoph Dinh <[email protected]>
* Gabriel Motta <[email protected]>
* @since 0.1.0
* @date July, 2012
*
* @section LICENSE
*
* Copyright (C) 2012, Lorenz Esch, Matti Hamalainen, Christoph Dinh, Gabriel Motta. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that
* the following conditions are met:
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
* * Neither the name of MNE-CPP authors nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*
* @brief Definition of the MNE Wrapper Class.
*
*/
//=============================================================================================================
// INCLUDES
//=============================================================================================================
#include "mne.h"
#include <fiff/fiff.h>
#include <iostream>
//=============================================================================================================
// QT INCLUDES
//=============================================================================================================
#include <QFile>
#include <QDebug>
//=============================================================================================================
// USED NAMESPACES
//=============================================================================================================
using namespace MNELIB;
using namespace Eigen;
using namespace FIFFLIB;
//=============================================================================================================
// DEFINE MEMBER METHODS
//=============================================================================================================
bool MNE::read_events(QString t_sEventName,
QString t_fileRawName,
MatrixXi& events)
{
QFile t_EventFile;
qint32 p;
bool status = false;
if (t_sEventName.isEmpty()) {
p = t_fileRawName.indexOf(".fif");
if (p > 0) {
t_sEventName = t_fileRawName.replace(p, 4, "-eve.fif");
} else {
printf("Raw file name does not end properly\n");
return 0;
}
t_EventFile.setFileName(t_sEventName);
if(!MNE::read_events_from_fif(t_EventFile, events)) {
printf("Error while read events.\n");
return false;
}
printf("Events read from %s\n",t_sEventName.toUtf8().constData());
} else {
// Binary file
if (t_sEventName.contains(".fif")) {
t_EventFile.setFileName(t_sEventName);
if(!MNE::read_events_from_fif(t_EventFile, events)) {
printf("Error while read events.\n");
return false;
}
printf("Binary event file %s read\n",t_sEventName.toUtf8().constData());
} else if(t_sEventName.contains(".eve")){
} else {
// Text file
printf("Text file %s is not supported jet.\n",t_sEventName.toUtf8().constData());
// try
// events = load(eventname);
// catch
// error(me,mne_omit_first_line(lasterr));
// end
// if size(events,1) < 1
// error(me,'No data in the event file');
// end
// //
// // Convert time to samples if sample number is negative
// //
// for p = 1:size(events,1)
// if events(p,1) < 0
// events(p,1) = events(p,2)*raw.info.sfreq;
// end
// end
// //
// // Select the columns of interest (convert to integers)
// //
// events = int32(events(:,[1 3 4]));
// //
// // New format?
// //
// if events(1,2) == 0 && events(1,3) == 0
// fprintf(1,'The text event file %s is in the new format\n',eventname);
// if events(1,1) ~= raw.first_samp
// error(me,'This new format event file is not compatible with the raw data');
// end
// else
// fprintf(1,'The text event file %s is in the old format\n',eventname);
// //
// // Offset with first sample
// //
// events(:,1) = events(:,1) + raw.first_samp;
// end
}
}
return true;
}
//=============================================================================================================
bool MNE::read_events_from_fif(QIODevice &p_IODevice,
MatrixXi& eventlist)
{
//
// Open file
//
FiffStream::SPtr t_pStream(new FiffStream(&p_IODevice));
if(!t_pStream->open()) {
return false;
}
//
// Find the desired block
//
QList<FiffDirNode::SPtr> events = t_pStream->dirtree()->dir_tree_find(FIFFB_MNE_EVENTS);
if (events.size() == 0)
{
printf("Could not find event data\n");
return false;
}
qint32 k, nelem;
fiff_int_t kind, pos;
FiffTag::SPtr t_pTag;
quint32* serial_eventlist_uint = NULL;
qint32* serial_eventlist_int = NULL;
for(k = 0; k < events[0]->nent(); ++k)
{
kind = events[0]->dir[k]->kind;
pos = events[0]->dir[k]->pos;
if (kind == FIFF_MNE_EVENT_LIST)
{
t_pStream->read_tag(t_pTag,pos);
if(t_pTag->type == FIFFT_UINT)
{
serial_eventlist_uint = t_pTag->toUnsignedInt();
nelem = t_pTag->size()/4;
}
if(t_pTag->type == FIFFT_INT)
{
serial_eventlist_int = t_pTag->toInt();
nelem = t_pTag->size()/4;
}
break;
}
}
if(serial_eventlist_uint == NULL && serial_eventlist_int == NULL)
{
printf("Could not find any events\n");
return false;
}
else
{
eventlist.resize(nelem/3,3);
if(serial_eventlist_uint != NULL)
{
for(k = 0; k < nelem/3; ++k)
{
eventlist(k,0) = serial_eventlist_uint[k*3];
eventlist(k,1) = serial_eventlist_uint[k*3+1];
eventlist(k,2) = serial_eventlist_uint[k*3+2];
}
}
if(serial_eventlist_int != NULL)
{
for(k = 0; k < nelem/3; ++k)
{
eventlist(k,0) = serial_eventlist_int[k*3];
eventlist(k,1) = serial_eventlist_int[k*3+1];
eventlist(k,2) = serial_eventlist_int[k*3+2];
}
}
}
return true;
}
//=============================================================================================================
void MNE::setup_compensators(FiffRawData& raw,
fiff_int_t dest_comp,
bool keep_comp)
{
// Set up projection
if (raw.info.projs.size() == 0) {
printf("No projector specified for these data\n");
} else {
// Activate the projection items
for (qint32 k = 0; k < raw.info.projs.size(); ++k) {
raw.info.projs[k].active = true;
}
printf("%d projection items activated\n",raw.info.projs.size());
// Create the projector
// fiff_int_t nproj = MNE::make_projector_info(raw.info, raw.proj); Using the member function instead
fiff_int_t nproj = raw.info.make_projector(raw.proj);
if (nproj == 0) {
printf("The projection vectors do not apply to these channels\n");
} else {
printf("Created an SSP operator (subspace dimension = %d)\n",nproj);
}
}
// Set up the CTF compensator
// qint32 current_comp = MNE::get_current_comp(raw.info);
qint32 current_comp = raw.info.get_current_comp();
if (current_comp > 0)
printf("Current compensation grade : %d\n",current_comp);
if (keep_comp)
dest_comp = current_comp;
if (current_comp != dest_comp)
{
qDebug() << "This part needs to be debugged";
if(MNE::make_compensator(raw.info, current_comp, dest_comp, raw.comp))
{
// raw.info.chs = MNE::set_current_comp(raw.info.chs,dest_comp);
raw.info.set_current_comp(dest_comp);
printf("Appropriate compensator added to change to grade %d.\n",dest_comp);
}
else
{
printf("Could not make the compensator\n");
return;
}
}
}
//=============================================================================================================
bool MNE::read_events_from_ascii(QIODevice &p_IODevice,
Eigen::MatrixXi& eventlist)
{
if (!p_IODevice.open(QIODevice::ReadOnly | QIODevice::Text)){
return false;
}
QTextStream textStream(&p_IODevice);
QList<int> simpleList;
while(!textStream.atEnd()){
int iSample;
textStream >> iSample;
simpleList.append(iSample);
textStream.readLine();
qDebug() << "Added event:" << iSample;
}
eventlist.resize(simpleList.size(), 1);
for(int i = 0; i < simpleList.size(); i++){
eventlist(i,0) = simpleList[i];
}
return true;
}