v2.0.0
Loading...
Searching...
No Matches
mne_raw_info.cpp
Go to the documentation of this file.
1//=============================================================================================================
16
17//=============================================================================================================
18// INCLUDES
19//=============================================================================================================
20
21#include "mne_raw_info.h"
22
23#include <fiff/fiff_tag.h>
24
25#include <Eigen/Core>
26
27#include <QFile>
28#include <QDebug>
29
30//=============================================================================================================
31// USED NAMESPACES
32//=============================================================================================================
33
34using namespace Eigen;
35using namespace FIFFLIB;
36using namespace MNELIB;
37
38//=============================================================================================================
39// DEFINE MEMBER METHODS
40//=============================================================================================================
41
43: nchan(0)
44, coord_frame(0)
45, sfreq(0.0f)
46, lowpass(0.0f)
47, highpass(0.0f)
48, buf_size(0)
50, ndir(0)
51{
52}
53
54//=============================================================================================================
55
59
60//=============================================================================================================
61
63{
64 FiffDirNode::SPtr empty_node;
65 FiffDirNode::SPtr tmp_node = node;
66
67 while (tmp_node->type != FIFFB_MEAS) {
68 if (tmp_node->parent == nullptr)
69 return empty_node;
70 tmp_node = tmp_node->parent;
71 }
72 return tmp_node;
73}
74
75//=============================================================================================================
76
78{
79 int k;
80 FiffDirNode::SPtr empty_node;
81 FiffDirNode::SPtr tmp_node = node;
82
83 while (tmp_node->type != FIFFB_MEAS) {
84 if (tmp_node->parent == nullptr)
85 return empty_node;
86 tmp_node = tmp_node->parent;
87 }
88 for (k = 0; k < tmp_node->nchild(); k++)
89 if (tmp_node->children[k]->type == FIFFB_MEAS_INFO)
90 return (tmp_node->children[k]);
91 return empty_node;
92}
93
94//=============================================================================================================
95
97{
99 QList<FiffDirNode::SPtr> temp;
100 temp = node->dir_tree_find(FIFFB_RAW_DATA);
101 if (temp.size() == 0) {
102 temp = node->dir_tree_find(FIFFB_CONTINUOUS_DATA);
103 if (temp.size() > 0)
104 raw = temp[0];
105 }
106 else
107 raw = temp[0];
108 return raw;
109}
110
111//=============================================================================================================
112
114
115{
117 QList<FiffDirNode::SPtr> temp;
118 temp = node->dir_tree_find(FIFFB_SMSH_RAW_DATA);
119 if (temp.size() > 0)
120 raw = temp[0];
121 return (raw);
122}
123
124//=============================================================================================================
125
127 FiffDirNode::SPtr &node,
128 std::unique_ptr<FiffId>& id,
129 int *nchan,
130 float *sfreq,
131 float *highpass,
132 float *lowpass,
133 QList<FiffChInfo>& chp,
135{
136 FiffTime dummy;
137 return get_meas_info(stream, node, id, nchan, sfreq, highpass, lowpass, chp, trans, dummy);
138}
139
140//=============================================================================================================
141
143 FiffDirNode::SPtr &node,
144 std::unique_ptr<FiffId>& id,
145 int *nchan,
146 float *sfreq,
147 float *highpass,
148 float *lowpass,
149 QList<FiffChInfo>& chp,
152{
153 FiffTag::UPtr t_pTag;
154 QList<FiffChInfo> ch;
155 FiffChInfo this_ch;
157 int j,k;
158 int to_find = 4;
159 QList<FiffDirNode::SPtr> hpi;
161 FiffDirNode::SPtr meas_info;
162 fiff_int_t kind, pos;
163 bool found_meas_date = false;
164
166 id.reset();
168 /*
169 * Find desired parents
170 */
171 if (!(meas = find_meas(node))) {
172 qCritical ("Meas. block not found!");
173 return (-1);
174 }
175
176 if (!(meas_info = find_meas_info(node))) {
177 qCritical ("Meas. info not found!");
178 return (-1);
179 }
180 /*
181 * Is there a block id is in the FIFFB_MEAS node?
182 */
183 if (!meas->id.isEmpty()) {
184 id = std::make_unique<FiffId>();
185 id->version = meas->id.version;
186 id->machid[0] = meas->id.machid[0];
187 id->machid[1] = meas->id.machid[1];
188 id->time = meas->id.time;
189 }
190 /*
191 * Others from FIFFB_MEAS_INFO
192 */
193 *lowpass = -1;
194 *highpass = -1;
195 for (k = 0; k < meas_info->nent(); k++) {
196 kind = meas_info->dir[k]->kind;
197 pos = meas_info->dir[k]->pos;
198 switch (kind) {
199
200 case FIFF_NCHAN :
201 if (!stream->read_tag(t_pTag,pos))
202 return (-1);
203 *nchan = *t_pTag->toInt();
204
205 for (j = 0; j < *nchan; j++) {
206 ch.append(FiffChInfo());
207 ch[j].scanNo = -1;
208 }
209 to_find = to_find + *nchan - 1;
210 break;
211
212 case FIFF_SFREQ :
213 if (!stream->read_tag(t_pTag,pos))
214 return (-1);
215 *sfreq = *t_pTag->toFloat();
216 to_find--;
217 break;
218
219 case FIFF_LOWPASS :
220 if (!stream->read_tag(t_pTag,pos))
221 return (-1);
222 *lowpass = *t_pTag->toFloat();
223 to_find--;
224 break;
225
226 case FIFF_HIGHPASS :
227 if (!stream->read_tag(t_pTag,pos))
228 return (-1);
229 *highpass = *t_pTag->toFloat();
230 to_find--;
231 break;
232
233 case FIFF_CH_INFO :
234 if (!stream->read_tag(t_pTag,pos))
235 return (-1);
236
237 this_ch = t_pTag->toChInfo();
238 if (this_ch.scanNo <= 0 || this_ch.scanNo > *nchan) {
239 qCritical ("FIFF_CH_INFO : scan # out of range!");
240 return (-1);
241 }
242 else
243 ch[this_ch.scanNo-1] = this_ch;
244 to_find--;
245 break;
246
247 case FIFF_MEAS_DATE :
248 if (!stream->read_tag(t_pTag,pos))
249 return (-1);
250 {
251 const FiffTime* pTime = reinterpret_cast<const FiffTime*>(t_pTag->data());
252 start_time = FiffTime(pTime->secs, pTime->usecs);
253 found_meas_date = true;
254 }
255 break;
256
257 case FIFF_COORD_TRANS :
258 if (!stream->read_tag(t_pTag,pos))
259 return (-1);
260 t = FiffCoordTrans::readFromTag( t_pTag );
261 /*
262 * Require this particular transform!
263 */
264 if (t.from == FIFFV_COORD_DEVICE && t.to == FIFFV_COORD_HEAD) {
265 trans = t;
266 break;
267 }
268 }
269 }
270 /*
271 * Search for the coordinate transformation from
272 * HPI_RESULT block if it was not previously found
273 */
274 hpi = meas_info->dir_tree_find(FIFFB_HPI_RESULT);
275
276 if (hpi.size() > 0 && trans.isEmpty())
277 for (k = 0; k < hpi[0]->nent(); k++)
278 if (hpi[0]->dir[k]->kind == FIFF_COORD_TRANS) {
279 if (!stream->read_tag(t_pTag,hpi[0]->dir[k]->pos))
280 return (-1);
281 t = FiffCoordTrans::readFromTag( t_pTag );
282 if (t.from == FIFFV_COORD_DEVICE && t.to == FIFFV_COORD_HEAD) {
283 trans = t;
284 break;
285 }
286 }
287 if (to_find < 3) {
288 if (*lowpass < 0) {
289 *lowpass = *sfreq/2.0;
290 to_find--;
291 }
292 if (*highpass < 0) {
293 *highpass = 0.0;
294 to_find--;
295 }
296 }
297 if (to_find != 0) {
298 qCritical ("Not all essential tags were found!");
299 return (-1);
300 }
301 chp = ch;
302 /*
303 * Resolve the measurement start time using the best available source
304 */
305 if (!found_meas_date) {
306 if (id)
307 start_time = id->time;
308 else
310 }
311 return (0);
312}
313
314//=============================================================================================================
315
316int MNERawInfo::load(const QString& name, int allow_maxshield, std::unique_ptr<MNERawInfo>& infop)
317{
318 QFile file(name);
319 FiffStream::SPtr stream(new FiffStream(&file));
320
321 int res = FIFF_FAIL;
322 QList<FiffChInfo> chs; /* Channel info */
323 FiffCoordTrans trans; /* The coordinate transformation */
324 std::unique_ptr<FiffId> id; /* Measurement id */
325 QList<FiffDirEntry::SPtr> rawDir; /* Directory of raw data tags */
326 std::unique_ptr<MNERawInfo> info;
327 int nchan = 0; /* Number of channels */
328 float sfreq = 0.0; /* Sampling frequency */
329 float highpass; /* Highpass filter frequency */
330 float lowpass; /* Lowpass filter frequency */
333 int k;
334 int maxshield_data = false;
335 /*
336 * Open file
337 */
338 if(!stream->open()) {
339 stream->close(); return FIFF_FAIL;
340 }
341 raw = find_raw(stream->dirtree());
342 if (raw->isEmpty()) {
343 if (allow_maxshield) {
344 raw = find_maxshield(stream->dirtree());
345 if (raw->isEmpty()) {
346 qCritical("No raw data in this file.");
347 stream->close(); return FIFF_FAIL;
348 }
349 maxshield_data = true;
350 }
351 else {
352 qCritical("No raw data in this file.");
353 stream->close(); return FIFF_FAIL;
354 }
355 }
356 /*
357 * Get the essential measurement information
358 */
359 if (get_meas_info (stream,
360 raw,
361 id,
362 &nchan,
363 &sfreq,
364 &highpass,
365 &lowpass,
366 chs,
367 trans,
368 start_time) < 0) {
369 stream->close(); return FIFF_FAIL;
370 }
371 /*
372 * Get the raw directory
373 */
374 rawDir = raw->dir;
375 /*
376 * Ready to put everything together
377 */
378 info = std::make_unique<MNERawInfo>();
379 info->filename = name;
380 info->nchan = nchan;
381 info->chInfo = chs;
382 info->coord_frame = FIFFV_COORD_DEVICE;
383 info->trans = std::make_unique<FiffCoordTrans>(trans);
384 info->sfreq = sfreq;
385 info->lowpass = lowpass;
386 info->highpass = highpass;
387 info->maxshield_data = maxshield_data;
388 if (id) {
389 info->id = std::make_unique<FiffId>(*id);
390 }
391 /*
392 * Getting starting time from measurement ID is not too accurate...
393 */
394 info->start_time = start_time;
395 info->buf_size = 0;
396 for (k = 0; k < raw->nent(); k++) {
397 if (raw->dir[k]->kind == FIFF_DATA_BUFFER) {
398 if (raw->dir[k]->type == FIFFT_DAU_PACK16 || raw->dir[k]->type == FIFFT_SHORT)
399 info->buf_size = raw->dir[k]->size/(nchan*sizeof(fiff_short_t));
400 else if (raw->dir[k]->type == FIFFT_FLOAT)
401 info->buf_size = raw->dir[k]->size/(nchan*sizeof(fiff_float_t));
402 else if (raw->dir[k]->type == FIFFT_INT)
403 info->buf_size = raw->dir[k]->size/(nchan*sizeof(fiff_int_t));
404 else {
405 qCritical("We are not prepared to handle raw data type: %d",raw->dir[k]->type);
406 stream->close(); return FIFF_FAIL;
407 }
408 break;
409 }
410 }
411 if (info->buf_size <= 0) {
412 qCritical("No raw data buffers available.");
413 stream->close(); return FIFF_FAIL;
414 }
415 info->rawDir = rawDir;
416 info->ndir = raw->nent();
417 infop = std::move(info);
418 res = FIFF_OK;
419
420 stream->close();
421 return res;
422}
Subset of FIFF measurement info needed by the legacy raw-data path.
return FiffCoordTrans(from_frame, to_frame, R, moveVec)
#define FIFF_OK
#define FIFFV_COORD_DEVICE
#define FIFF_FAIL
#define FIFFV_COORD_HEAD
#define FIFF_DATA_BUFFER
Definition fiff_file.h:549
#define FIFF_NCHAN
Definition fiff_file.h:446
#define FIFF_HIGHPASS
Definition fiff_file.h:469
#define FIFFT_INT
Definition fiff_file.h:224
#define FIFFT_SHORT
Definition fiff_file.h:223
#define FIFFB_MEAS
Definition fiff_file.h:355
#define FIFFB_RAW_DATA
Definition fiff_file.h:357
#define FIFFB_SMSH_RAW_DATA
Definition fiff_file.h:375
#define FIFFT_DAU_PACK16
Definition fiff_file.h:236
#define FIFF_COORD_TRANS
Definition fiff_file.h:468
#define FIFFT_FLOAT
Definition fiff_file.h:225
#define FIFFB_CONTINUOUS_DATA
Definition fiff_file.h:368
#define FIFF_MEAS_DATE
Definition fiff_file.h:450
#define FIFF_CH_INFO
Definition fiff_file.h:449
#define FIFFB_HPI_RESULT
Definition fiff_file.h:365
#define FIFF_LOWPASS
Definition fiff_file.h:465
#define FIFFB_MEAS_INFO
Definition fiff_file.h:356
#define FIFF_SFREQ
Definition fiff_file.h:447
FIFF tag: the 16-byte tag header (kind, type, size, next) plus its decoded payload.
Core MNE data structures (source spaces, source estimates, hemispheres).
FIFF file I/O, in-memory data structures and high-level readers/writers.
Per-channel FIFF descriptor: identifiers, kind, calibration, coil type, channel-frame coil position a...
Labelled 4x4 FIFF affine: source frame, destination frame, rotation, translation and cached inverse.
static FiffCoordTrans readFromTag(const std::unique_ptr< FiffTag > &tag)
QSharedPointer< FiffDirNode > SPtr
FIFF tag-stream reader/writer: wraps a QIODevice and exposes typed read_* / write_* methods for every...
QSharedPointer< FiffStream > SPtr
std::unique_ptr< FiffTag > UPtr
Definition fiff_tag.h:164
FIFF time stamp: Unix seconds plus a microsecond fraction matching the on-disk fiffTimeRec record.
Definition fiff_time.h:50
FIFFLIB::FiffTime start_time
QList< FIFFLIB::FiffDirEntry::SPtr > rawDir
MNERawInfo()
Constructs a default MNERawInfo.
static FIFFLIB::FiffDirNode::SPtr find_maxshield(const FIFFLIB::FiffDirNode::SPtr &node)
static FIFFLIB::FiffDirNode::SPtr find_meas_info(const FIFFLIB::FiffDirNode::SPtr &node)
static int load(const QString &name, int allow_maxshield, std::unique_ptr< MNERawInfo > &infop)
static int get_meas_info(FIFFLIB::FiffStream::SPtr &stream, FIFFLIB::FiffDirNode::SPtr &node, std::unique_ptr< FIFFLIB::FiffId > &id, int *nchan, float *sfreq, float *highpass, float *lowpass, QList< FIFFLIB::FiffChInfo > &chp, FIFFLIB::FiffCoordTrans &trans, FIFFLIB::FiffTime &start_time)
static FIFFLIB::FiffDirNode::SPtr find_raw(const FIFFLIB::FiffDirNode::SPtr &node)
static FIFFLIB::FiffDirNode::SPtr find_meas(const FIFFLIB::FiffDirNode::SPtr &node)
std::unique_ptr< FIFFLIB::FiffId > id
~MNERawInfo()
Destroys the MNERawInfo and releases owned resources.
std::unique_ptr< FIFFLIB::FiffCoordTrans > trans