v2.0.0
Loading...
Searching...
No Matches
mne_ctf_comp_data_set.cpp
Go to the documentation of this file.
1//=============================================================================================================
36
37//=============================================================================================================
38// INCLUDES
39//=============================================================================================================
40
42#include "mne_ctf_comp_data.h"
43
44#include "mne_types.h"
45
47
48#include <fiff/fiff_types.h>
49
50#include <Eigen/Core>
51
52//=============================================================================================================
53// QT INCLUDES
54//=============================================================================================================
55
56#include <QFile>
57
58#ifndef TRUE
59#define TRUE 1
60#endif
61
62#ifndef FALSE
63#define FALSE 0
64#endif
65
66#ifndef FAIL
67#define FAIL -1
68#endif
69
70#ifndef OK
71#define OK 0
72#endif
73
74
75
76//=============================================================================================================
77// USED NAMESPACES
78//=============================================================================================================
79
80using namespace Eigen;
81using namespace FIFFLIB;
82using namespace MNELIB;
83
84//============================= mne_read_forward_solution.c =============================
85
86int mne_read_meg_comp_eeg_ch_info_32(const QString& name,
87 QList<FIFFLIB::FiffChInfo>& megp, /* MEG channels */
88 int *nmegp,
89 QList<FIFFLIB::FiffChInfo>& meg_compp,
90 int *nmeg_compp,
91 QList<FIFFLIB::FiffChInfo>& eegp, /* EEG channels */
92 int *neegp,
93 FiffCoordTrans *meg_head_t,
94 fiffId *idp) /* The measurement ID */
95/*
96 * Read the channel information and split it into three arrays,
97 * one for MEG, one for MEG compensation channels, and one for EEG
98 */
99{
100 QFile file(name);
101 FiffStream::SPtr stream(new FiffStream(&file));
102
103 QList<FIFFLIB::FiffChInfo> chs;
104 int nchan = 0;
105 QList<FIFFLIB::FiffChInfo> meg;
106 int nmeg = 0;
107 QList<FIFFLIB::FiffChInfo> meg_comp;
108 int nmeg_comp = 0;
109 QList<FIFFLIB::FiffChInfo> eeg;
110 int neeg = 0;
111 std::unique_ptr<FiffId> id;
112 QList<FiffDirNode::SPtr> nodes;
114 FiffTag::SPtr t_pTag;
115 FIFFLIB::FiffChInfo this_ch;
117 fiff_int_t kind, pos;
118 int j,k,to_find;
119
120 if(!stream->open())
121 goto bad;
122
123 nodes = stream->dirtree()->dir_tree_find(FIFFB_MNE_PARENT_MEAS_FILE);
124
125 if (nodes.size() == 0) {
126 nodes = stream->dirtree()->dir_tree_find(FIFFB_MEAS_INFO);
127 if (nodes.size() == 0) {
128 qCritical ("Could not find the channel information.");
129 goto bad;
130 }
131 }
132 info = nodes[0];
133 to_find = 0;
134 for (k = 0; k < info->nent(); k++) {
135 kind = info->dir[k]->kind;
136 pos = info->dir[k]->pos;
137 switch (kind) {
138 case FIFF_NCHAN :
139 if (!stream->read_tag(t_pTag,pos))
140 goto bad;
141 nchan = *t_pTag->toInt();
142
143 for (j = 0; j < nchan; j++) {
144 chs.append(FiffChInfo());
145 chs[j].scanNo = -1;
146 }
147 to_find = nchan;
148 break;
149
151 if(!stream->read_tag(t_pTag, pos))
152 goto bad;
153 id = std::make_unique<FiffId>(*(FiffId*)t_pTag->data());
154 break;
155
156 case FIFF_COORD_TRANS :
157 if(!stream->read_tag(t_pTag, pos))
158 goto bad;
159// t = t_pTag->toCoordTrans();
160 t = FiffCoordTrans::readFromTag( t_pTag );
162 t = FiffCoordTrans();
163 break;
164
165 case FIFF_CH_INFO : /* Information about one channel */
166 if(!stream->read_tag(t_pTag, pos))
167 goto bad;
168
169 this_ch = t_pTag->toChInfo();
170 if (this_ch.scanNo <= 0 || this_ch.scanNo > nchan) {
171 printf ("FIFF_CH_INFO : scan # out of range %d (%d)!",this_ch.scanNo,nchan);
172 goto bad;
173 }
174 else
175 chs[this_ch.scanNo-1] = this_ch;
176 to_find--;
177 break;
178 }
179 }
180 if (to_find != 0) {
181 qCritical("Some of the channel information was missing.");
182 goto bad;
183 }
184 if (t.isEmpty() && meg_head_t != NULL) {
185 /*
186 * Try again in a more general fashion
187 */
189 if (t.isEmpty()) {
190 qCritical("MEG -> head coordinate transformation not found.");
191 goto bad;
192 }
193 }
194 /*
195 * Sort out the channels
196 */
197 for (k = 0; k < nchan; k++) {
198 if (chs[k].kind == FIFFV_MEG_CH) {
199 meg.append(chs[k]);
200 nmeg++;
201 } else if (chs[k].kind == FIFFV_REF_MEG_CH) {
202 meg_comp.append(chs[k]);
203 nmeg_comp++;
204 } else if (chs[k].kind == FIFFV_EEG_CH) {
205 eeg.append(chs[k]);
206 neeg++;
207 }
208 }
209// fiff_close(in);
210 stream->close();
211
212 megp = meg;
213 if(nmegp) {
214 *nmegp = nmeg;
215 }
216
217 meg_compp = meg_comp;
218 if(nmeg_compp) {
219 *nmeg_compp = nmeg_comp;
220 }
221
222 eegp = eeg;
223 if(neegp) {
224 *neegp = neeg;
225 }
226
227 if (idp == NULL) {
228 /* id is auto-deleted by unique_ptr */
229 }
230 else
231 *idp = id.release();
232 if (meg_head_t == NULL) {
233 }
234 else
235 *meg_head_t = t;
236
237 return FIFF_OK;
238
239bad : {
240 stream->close();
241 return FIFF_FAIL;
242 }
243}
244
245#define MNE_CTFV_COMP_UNKNOWN -1
246#define MNE_CTFV_COMP_NONE 0
247#define MNE_CTFV_COMP_G1BR 0x47314252
248#define MNE_CTFV_COMP_G2BR 0x47324252
249#define MNE_CTFV_COMP_G3BR 0x47334252
250#define MNE_CTFV_COMP_G2OI 0x47324f49
251#define MNE_CTFV_COMP_G3OI 0x47334f49
252
253static struct {
256} compMap[] = { { MNE_CTFV_NOGRAD, MNE_CTFV_COMP_NONE },
260{ MNE_4DV_COMP1, MNE_4DV_COMP1 }, /* One-to-one mapping for 4D data */
262
264
265{
266 int k;
267
268 for (k = 0; compMap[k].grad_comp >= 0; k++)
269 if (ctf_comp == compMap[k].ctf_comp)
270 return compMap[k].grad_comp;
271 return ctf_comp;
272}
273
274FiffSparseMatrix* mne_convert_to_sparse(const Eigen::MatrixXf& dense, /* The dense matrix to be converted */
275 int stor_type, /* Either FIFFTS_MC_CCS or FIFFTS_MC_RCS */
276 float small) /* How small elements should be ignored? */
277/*
278 * Create the compressed row or column storage sparse matrix representation
279 * including a vector containing the nonzero matrix element values,
280 * the row or column pointer vector and the appropriate index vector(s).
281 */
282{
283 int j,k;
284 int nz;
285 int ptr;
286 int nrow = static_cast<int>(dense.rows());
287 int ncol = static_cast<int>(dense.cols());
288 FiffSparseMatrix* sparse = NULL;
289
290 if (small < 0) { /* Automatic scaling */
291 float maxval = dense.cwiseAbs().maxCoeff();
292 if (maxval > 0)
293 small = maxval*std::fabs(small);
294 else
295 small = std::fabs(small);
296 }
297 for (j = 0, nz = 0; j < nrow; j++)
298 for (k = 0; k < ncol; k++) {
299 if (std::fabs(dense(j,k)) > small)
300 nz++;
301 }
302
303 if (nz <= 0) {
304 qWarning("No nonzero elements found.");
305 return NULL;
306 }
307 if (stor_type != FIFFTS_MC_CCS && stor_type != FIFFTS_MC_RCS) {
308 qWarning("Unknown sparse matrix storage type: %d",stor_type);
309 return NULL;
310 }
311 sparse = new FiffSparseMatrix;
312 sparse->coding = stor_type;
313 sparse->m = nrow;
314 sparse->n = ncol;
315 sparse->nz = nz;
316 sparse->data.resize(nz);
317 sparse->inds.resize(nz);
318
319 if (stor_type == FIFFTS_MC_RCS) {
320 sparse->ptrs.resize(nrow + 1);
321 for (j = 0, nz = 0; j < nrow; j++) {
322 ptr = -1;
323 for (k = 0; k < ncol; k++)
324 if (std::fabs(dense(j,k)) > small) {
325 sparse->data[nz] = dense(j,k);
326 if (ptr < 0)
327 ptr = nz;
328 sparse->inds[nz++] = k;
329 }
330 sparse->ptrs[j] = ptr;
331 }
332 sparse->ptrs[nrow] = nz;
333 for (j = nrow - 1; j >= 0; j--) /* Take care of the empty rows */
334 if (sparse->ptrs[j] < 0)
335 sparse->ptrs[j] = sparse->ptrs[j+1];
336 }
337 else if (stor_type == FIFFTS_MC_CCS) {
338 sparse->ptrs.resize(ncol + 1);
339 for (k = 0, nz = 0; k < ncol; k++) {
340 ptr = -1;
341 for (j = 0; j < nrow; j++)
342 if (std::fabs(dense(j,k)) > small) {
343 sparse->data[nz] = dense(j,k);
344 if (ptr < 0)
345 ptr = nz;
346 sparse->inds[nz++] = j;
347 }
348 sparse->ptrs[k] = ptr;
349 }
350 sparse->ptrs[ncol] = nz;
351 for (k = ncol-1; k >= 0; k--) /* Take care of the empty columns */
352 if (sparse->ptrs[k] < 0)
353 sparse->ptrs[k] = sparse->ptrs[k+1];
354 }
355 return sparse;
356}
357
358int mne_sparse_vec_mult2_32(FiffSparseMatrix* mat, /* The sparse matrix */
359 float *vector, /* Vector to be multiplied */
360 float *res) /* Result of the multiplication */
361/*
362 * Multiply a vector by a sparse matrix.
363 */
364{
365 int i,j;
366
367 if (mat->coding == FIFFTS_MC_RCS) {
368 for (i = 0; i < mat->m; i++) {
369 res[i] = 0.0;
370 for (j = mat->ptrs[i]; j < mat->ptrs[i+1]; j++)
371 res[i] += mat->data[j]*vector[mat->inds[j]];
372 }
373 return 0;
374 }
375 else if (mat->coding == FIFFTS_MC_CCS) {
376 for (i = 0; i < mat->m; i++)
377 res[i] = 0.0;
378 for (i = 0; i < mat->n; i++)
379 for (j = mat->ptrs[i]; j < mat->ptrs[i+1]; j++)
380 res[mat->inds[j]] += mat->data[j]*vector[i];
381 return 0;
382 }
383 else {
384 qWarning("mne_sparse_vec_mult2: unknown sparse matrix storage type: %d",mat->coding);
385 return -1;
386 }
387}
388
389float mne_dot_vectors_32 (float *v1,
390 float *v2,
391 int nn)
392
393{
394#ifdef BLAS
395 int one = 1;
396 float res = sdot(&nn,v1,&one,v2,&one);
397 return res;
398#else
399 float res = 0.0;
400 int k;
401
402 for (k = 0; k < nn; k++)
403 res = res + v1[k]*v2[k];
404 return res;
405#endif
406}
407
408void mne_mat_vec_mult2_32 (float **m,float *v,float *result, int d1,int d2)
409/*
410 * Matrix multiplication
411 * result(d1) = m(d1 x d2) * v(d2)
412 */
413
414{
415 int j;
416
417 for (j = 0; j < d1; j++)
418 result[j] = mne_dot_vectors_32 (m[j],v,d2);
419 return;
420}
421
422//=============================================================================================================
423// DEFINE MEMBER METHODS
424//=============================================================================================================
425
427:ncomp(0)
428,nch(0)
429,undo(nullptr)
430,current(nullptr)
431{
432}
433
434//=============================================================================================================
435
437{
438 if (set.ncomp > 0) {
439 this->ncomp = set.comps.size();
440 for (int k = 0; k < this->ncomp; k++)
441 if(set.comps[k])
442 this->comps.append(new MNECTFCompData(*set.comps[k]));
443 }
444
445 if(set.current)
446 this->current = std::make_unique<MNECTFCompData>(*set.current);
447}
448
449//=============================================================================================================
450
452{
453
454 for (int k = 0; k < comps.size(); k++)
455 if(comps[k])
456 delete comps[k];
457}
458
459//=============================================================================================================
460
461std::unique_ptr<MNECTFCompDataSet> MNECTFCompDataSet::read(const QString &name)
462/*
463 * Read all CTF compensation data from a given file
464 */
465{
466 QFile file(name);
467 FiffStream::SPtr stream(new FiffStream(&file));
468
469 std::unique_ptr<MNECTFCompDataSet> set;
470 MNECTFCompData* one;
471 QList<FiffDirNode::SPtr> nodes;
472 QList<FiffDirNode::SPtr> comps;
473 int ncomp;
474 int kind,k;
475 FiffTag::SPtr t_pTag;
476 QList<FiffChInfo> chs;
477 int nch = 0;
478 int calibrated;
479 /*
480 * Read the channel information
481 */
482 {
483 QList<FiffChInfo> comp_chs, temp;
484 int ncompch = 0;
485
487 chs,
488 &nch,
489 comp_chs,
490 &ncompch,
491 temp,
492 NULL,
493 NULL,
494 NULL) == FAIL)
495 goto bad;
496 if (ncompch > 0) {
497 for (k = 0; k < ncompch; k++)
498 chs.append(comp_chs[k]);
499 nch = nch + ncompch;
500 }
501 }
502 /*
503 * Read the rest of the stuff
504 */
505 if(!stream->open())
506 goto bad;
507 set = std::make_unique<MNECTFCompDataSet>();
508 /*
509 * Locate the compensation data sets
510 */
511 nodes = stream->dirtree()->dir_tree_find(FIFFB_MNE_CTF_COMP);
512 if (nodes.size() == 0)
513 goto good; /* Nothing more to do */
514 comps = nodes[0]->dir_tree_find(FIFFB_MNE_CTF_COMP_DATA);
515 if (comps.size() == 0)
516 goto good;
517 ncomp = comps.size();
518 /*
519 * Set the channel info
520 */
521 set->chs = chs;
522 set->nch = nch;
523 /*
524 * Read each data set
525 */
526 for (k = 0; k < ncomp; k++) {
528 if (!mat)
529 goto bad;
530 comps[k]->find_tag(stream, FIFF_MNE_CTF_COMP_KIND, t_pTag);
531 if (t_pTag) {
532 kind = *t_pTag->toInt();
533 }
534 else
535 goto bad;
536 comps[k]->find_tag(stream, FIFF_MNE_CTF_COMP_CALIBRATED, t_pTag);
537 if (t_pTag) {
538 calibrated = *t_pTag->toInt();
539 }
540 else
541 calibrated = FALSE;
542 /*
543 * Add these data to the set
544 */
545 one = new MNECTFCompData;
546 one->data = std::move(mat);
547 one->kind = kind;
549 one->calibrated = calibrated;
550
551 if (one->calibrate(set->chs,set->nch,TRUE) == FAIL) {
552 printf("Warning: Compensation data for '%s' omitted\n", explain_comp(one->kind).toUtf8().constData());//,err_get_error(),explain_comp(one->kind));
553 if(one)
554 delete one;
555 }
556 else {
557 set->comps.append(one);
558 set->ncomp++;
559 }
560 }
561#ifdef DEBUG
562 printf("%d CTF compensation data sets read from %s\n",set->ncomp,name);
563#endif
564 goto good;
565
566bad : {
567 stream->close();
568 return nullptr;
569 }
570
571good : {
572 stream->close();
573 return set;
574 }
575}
576
577//=============================================================================================================
578
579int MNECTFCompDataSet::make_comp(const QList<FiffChInfo>& chs,
580 int nch,
581 QList<FiffChInfo> compchs,
582 int ncomp) /* How many of these */
583/*
584 * Make compensation data to apply to a set of channels to yield (or uncompensated) compensated data
585 */
586{
587 Eigen::VectorXi comps;
588 int need_comp;
589 int first_comp;
590 MNECTFCompData* this_comp;
591 Eigen::VectorXi comp_sel;
592 QStringList names;
593 QString name;
594 int j,k,p;
595
596 std::unique_ptr<FiffSparseMatrix> presel;
597 std::unique_ptr<FiffSparseMatrix> postsel;
598 std::unique_ptr<MNENamedMatrix> data;
599
600 QStringList emptyList;
601
602 if (compchs.isEmpty()) {
603 compchs = chs;
604 ncomp = nch;
605 }
606 printf("Setting up compensation data...\n");
607 if (nch == 0)
608 return OK;
609 current.reset();
610 comps.resize(nch);
611 for (k = 0, need_comp = 0, first_comp = MNE_CTFV_COMP_NONE; k < nch; k++) {
612 if (chs[k].kind == FIFFV_MEG_CH) {
613 comps[k] = chs[k].chpos.coil_type >> 16;
614 if (comps[k] != MNE_CTFV_COMP_NONE) {
615 if (first_comp == MNE_CTFV_COMP_NONE)
616 first_comp = comps[k];
617 else {
618 if (comps[k] != first_comp) {
619 printf("We do not support nonuniform compensation yet.");
620 return FAIL;
621 }
622 }
623 need_comp++;
624 }
625 }
626 else
628 }
629 if (need_comp == 0) {
630 printf("\tNo compensation set. Nothing more to do.\n");
631 return OK;
632 }
633 printf("\t%d out of %d channels have the compensation set.\n",need_comp,nch);
634 /*
635 * Find the desired compensation data matrix
636 */
637 for (k = 0, this_comp = NULL; k < this->ncomp; k++) {
638 if (this->comps[k]->mne_kind == first_comp) {
639 this_comp = this->comps[k];
640 break;
641 }
642 }
643 if (!this_comp) {
644 printf("Did not find the desired compensation data : %s",
645 explain_comp(map_comp_kind(first_comp)).toUtf8().constData());
646 return FAIL;
647 }
648 printf("\tDesired compensation data (%s) found.\n",explain_comp(map_comp_kind(first_comp)).toUtf8().constData());
649 /*
650 * Find the compensation channels
651 */
652 comp_sel.resize(this_comp->data->ncol);
653 for (k = 0; k < this_comp->data->ncol; k++) {
654 comp_sel[k] = -1;
655 name = this_comp->data->collist[k];
656 for (p = 0; p < ncomp; p++)
657 if (QString::compare(name,compchs[p].ch_name) == 0) {
658 comp_sel[k] = p;
659 break;
660 }
661 if (comp_sel[k] < 0) {
662 printf("Compensation channel %s not found",name.toUtf8().constData());
663 return FAIL;
664 }
665 }
666 printf("\tAll compensation channels found.\n");
667 /*
668 * Create the preselector
669 */
670 {
671 Eigen::MatrixXf sel = Eigen::MatrixXf::Zero(this_comp->data->ncol, ncomp);
672 for (j = 0; j < this_comp->data->ncol; j++)
673 sel(j, comp_sel[j]) = 1.0f;
675 if (!ps)
676 return FAIL;
677 presel.reset(ps);
678 printf("\tPreselector created.\n");
679 }
680 /*
681 * Pick the desired channels
682 */
683 for (k = 0; k < nch; k++) {
684 if (comps[k] != MNE_CTFV_COMP_NONE)
685 names.append(chs[k].ch_name);
686 }
687
688 {
689 auto d = this_comp->data->pick(names, need_comp, emptyList, 0);
690 if (!d)
691 return FAIL;
692 data = std::move(d);
693 }
694 printf("\tCompensation data matrix created.\n");
695 /*
696 * Create the postselector
697 */
698 {
699 Eigen::MatrixXf sel = Eigen::MatrixXf::Zero(nch, data->nrow);
700 for (j = 0, p = 0; j < nch; j++) {
701 if (comps[j] != MNE_CTFV_COMP_NONE)
702 sel(j, p++) = 1.0f;
703 }
705 if (!ps)
706 return FAIL;
707 postsel.reset(ps);
708 printf("\tPostselector created.\n");
709 }
710 current = std::make_unique<MNECTFCompData>();
711 current->kind = this_comp->kind;
712 current->mne_kind = this_comp->mne_kind;
713 current->data = std::move(data);
714 current->presel = std::move(presel);
715 current->postsel = std::move(postsel);
716
717 printf("\tCompensation set up.\n");
718 return OK;
719}
720
721//=============================================================================================================
722
723int MNECTFCompDataSet::set_comp(QList<FIFFLIB::FiffChInfo>& chs,
724 int nch,
725 int comp)
726/*
727 * Set the compensation bits to the desired value
728 */
729{
730 int k;
731 int nset;
732 for (k = 0, nset = 0; k < nch; k++) {
733 if (chs[k].kind == FIFFV_MEG_CH) {
734 chs[k].chpos.coil_type = (chs[k].chpos.coil_type & 0xFFFF) | (comp << 16);
735 nset++;
736 }
737 }
738 printf("A new compensation value (%s) was assigned to %d MEG channels.\n",
739 explain_comp(map_comp_kind(comp)).toUtf8().constData(),nset);
740 return nset;
741}
742
743//=============================================================================================================
744
745int MNECTFCompDataSet::apply(int do_it, Eigen::VectorXf& data)
746{
747 return apply(do_it, data, data);
748}
749
750//=============================================================================================================
751
752int MNECTFCompDataSet::apply(int do_it, Eigen::VectorXf& data, const Eigen::VectorXf& compdata)
753/*
754 * Apply compensation or revert to uncompensated data
755 */
756{
757 MNECTFCompData* this_comp;
758 int ndata = static_cast<int>(data.size());
759 int ncompdata = static_cast<int>(compdata.size());
760
761 if (!current)
762 return OK;
763 this_comp = current.get();
764 /*
765 * Dimension checks
766 */
767 if (this_comp->presel) {
768 if (this_comp->presel->n != ncompdata) {
769 printf("Compensation data dimension mismatch. Expected %d, got %d channels.",
770 this_comp->presel->n,ncompdata);
771 return FAIL;
772 }
773 }
774 else if (this_comp->data->ncol != ncompdata) {
775 printf("Compensation data dimension mismatch. Expected %d, got %d channels.",
776 this_comp->data->ncol,ncompdata);
777 return FAIL;
778 }
779 if (this_comp->postsel) {
780 if (this_comp->postsel->m != ndata) {
781 printf("Data dimension mismatch. Expected %d, got %d channels.",
782 this_comp->postsel->m,ndata);
783 return FAIL;
784 }
785 }
786 else if (this_comp->data->nrow != ndata) {
787 printf("Data dimension mismatch. Expected %d, got %d channels.",
788 this_comp->data->nrow,ndata);
789 return FAIL;
790 }
791 /*
792 * Preselection is optional
793 */
794 const float *presel;
795 if (this_comp->presel) {
796 if (this_comp->presel_data.size() == 0)
797 this_comp->presel_data.resize(this_comp->presel->m);
798 if (mne_sparse_vec_mult2_32(this_comp->presel.get(),const_cast<float*>(compdata.data()),this_comp->presel_data.data()) != OK)
799 return FAIL;
800 presel = this_comp->presel_data.data();
801 }
802 else
803 presel = compdata.data();
804 /*
805 * This always happens
806 */
807 if (this_comp->comp_data.size() == 0)
808 this_comp->comp_data.resize(this_comp->data->nrow);
809 {
810 Eigen::Map<const Eigen::VectorXf> preselVec(presel, this_comp->data->ncol);
811 Eigen::Map<Eigen::VectorXf> compVec(this_comp->comp_data.data(), this_comp->data->nrow);
812 compVec = this_comp->data->data * preselVec;
813 }
814 /*
815 * Optional postselection
816 */
817 const float *comp;
818 if (!this_comp->postsel)
819 comp = this_comp->comp_data.data();
820 else {
821 if (this_comp->postsel_data.size() == 0)
822 this_comp->postsel_data.resize(this_comp->postsel->m);
823 if (mne_sparse_vec_mult2_32(this_comp->postsel.get(),this_comp->comp_data.data(),this_comp->postsel_data.data()) != OK)
824 return FAIL;
825 comp = this_comp->postsel_data.data();
826 }
827 /*
828 * Compensate or revert compensation?
829 */
830 Eigen::Map<const Eigen::VectorXf> compVec(comp, ndata);
831 if (do_it)
832 data -= compVec;
833 else
834 data += compVec;
835 return OK;
836}
837
838//=============================================================================================================
839
840int MNECTFCompDataSet::apply_transpose(int do_it, Eigen::MatrixXf& data)
841/*
842 * Apply compensation or revert to uncompensated data
843 */
844{
845 using RowMatrixXf = Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
846
847 MNECTFCompData* this_comp;
848 int ndata = static_cast<int>(data.rows());
849 int ns = static_cast<int>(data.cols());
850 int ncompdata = ndata;
851
852 if (!current)
853 return OK;
854 this_comp = current.get();
855 /*
856 * Dimension checks
857 */
858 if (this_comp->presel) {
859 if (this_comp->presel->n != ncompdata) {
860 printf("Compensation data dimension mismatch. Expected %d, got %d channels.",
861 this_comp->presel->n,ncompdata);
862 return FAIL;
863 }
864 }
865 else if (this_comp->data->ncol != ncompdata) {
866 printf("Compensation data dimension mismatch. Expected %d, got %d channels.",
867 this_comp->data->ncol,ncompdata);
868 return FAIL;
869 }
870 if (this_comp->postsel) {
871 if (this_comp->postsel->m != ndata) {
872 printf("Data dimension mismatch. Expected %d, got %d channels.",
873 this_comp->postsel->m,ndata);
874 return FAIL;
875 }
876 }
877 else if (this_comp->data->nrow != ndata) {
878 printf("Data dimension mismatch. Expected %d, got %d channels.",
879 this_comp->data->nrow,ndata);
880 return FAIL;
881 }
882 /*
883 * Preselection is optional — sparse matrix * data
884 */
885 Eigen::MatrixXf preselMat;
886 if (this_comp->presel) {
887 preselMat.resize(this_comp->presel->m, ns);
888 FiffSparseMatrix* sp = this_comp->presel.get();
889 if (sp->coding == FIFFTS_MC_RCS) {
890 for (int i = 0; i < sp->m; i++)
891 for (int c = 0; c < ns; c++) {
892 float val = 0.0f;
893 for (int j = sp->ptrs[i]; j < sp->ptrs[i+1]; j++)
894 val += sp->data[j] * data(sp->inds[j], c);
895 preselMat(i, c) = val;
896 }
897 } else if (sp->coding == FIFFTS_MC_CCS) {
898 preselMat.setZero();
899 for (int c = 0; c < ns; c++)
900 for (int i = 0; i < sp->n; i++)
901 for (int j = sp->ptrs[i]; j < sp->ptrs[i+1]; j++)
902 preselMat(sp->inds[j], c) += sp->data[j] * data(i, c);
903 } else {
904 printf("Unknown sparse matrix storage type: %d", sp->coding);
905 return FAIL;
906 }
907 }
908 else {
909 preselMat = data;
910 }
911 /*
912 * Compensation: comp = data_matrix * preselMat
913 * data_matrix is float** (contiguous row-major via mne_cmatrix)
914 */
915 Eigen::MatrixXf comp = this_comp->data->data * preselMat;
916 /*
917 * Optional postselection — sparse matrix * comp
918 */
919 if (this_comp->postsel) {
920 Eigen::MatrixXf postselMat(this_comp->postsel->m, ns);
921 FiffSparseMatrix* sp = this_comp->postsel.get();
922 if (sp->coding == FIFFTS_MC_RCS) {
923 for (int i = 0; i < sp->m; i++)
924 for (int c = 0; c < ns; c++) {
925 float val = 0.0f;
926 for (int j = sp->ptrs[i]; j < sp->ptrs[i+1]; j++)
927 val += sp->data[j] * comp(sp->inds[j], c);
928 postselMat(i, c) = val;
929 }
930 } else if (sp->coding == FIFFTS_MC_CCS) {
931 postselMat.setZero();
932 for (int c = 0; c < ns; c++)
933 for (int i = 0; i < sp->n; i++)
934 for (int j = sp->ptrs[i]; j < sp->ptrs[i+1]; j++)
935 postselMat(sp->inds[j], c) += sp->data[j] * comp(i, c);
936 } else {
937 printf("Unknown sparse matrix storage type: %d", sp->coding);
938 return FAIL;
939 }
940 comp = std::move(postselMat);
941 }
942 /*
943 * Compensate or revert compensation?
944 */
945 if (do_it)
946 data -= comp;
947 else
948 data += comp;
949 return OK;
950}
951
952//=============================================================================================================
953
954int MNECTFCompDataSet::get_comp(const QList<FIFFLIB::FiffChInfo> &chs, int nch)
955{
956 int res = MNE_CTFV_NOGRAD;
957 int first_comp,comp;
958 int k;
959
960 for (k = 0, first_comp = -1; k < nch; k++) {
961 if (chs[k].kind == FIFFV_MEG_CH) {
962 comp = chs[k].chpos.coil_type >> 16;
963 if (first_comp < 0)
964 first_comp = comp;
965 else if (first_comp != comp) {
966 printf("Non uniform compensation not supported.");
967 return FAIL;
968 }
969 }
970 }
971 if (first_comp >= 0)
972 res = first_comp;
973 return res;
974}
975
976//=============================================================================================================
977
979/*
980 * Simple mapping
981 */
982{
983 int k;
984
985 for (k = 0; compMap[k].grad_comp >= 0; k++)
986 if (grad == compMap[k].grad_comp)
987 return compMap[k].ctf_comp;
988 return grad;
989}
990
991//=============================================================================================================
992
994{
995 static const struct {
996 int kind;
997 const char *expl;
998 } explain[] = { { MNE_CTFV_COMP_NONE, "uncompensated" },
999 { MNE_CTFV_COMP_G1BR, "first order gradiometer" },
1000 { MNE_CTFV_COMP_G2BR, "second order gradiometer" },
1001 { MNE_CTFV_COMP_G3BR, "third order gradiometer" },
1002 { MNE_4DV_COMP1, "4D comp 1" },
1003 { MNE_CTFV_COMP_UNKNOWN, "unknown" } };
1004 int k;
1005
1006 for (k = 0; explain[k].kind != MNE_CTFV_COMP_UNKNOWN; k++)
1007 if (explain[k].kind == kind)
1008 return QString::fromLatin1(explain[k].expl);
1009 return QString::fromLatin1(explain[k].expl);
1010}
1011
1012//=============================================================================================================
1013
1015 QList<FiffChInfo>& chs,
1016 int nchan,
1017 QList<FiffChInfo> comp_chs,
1018 int ncomp_chan) /* How many */
1019/*
1020 * Make data which has the third-order gradient compensation applied
1021 */
1022{
1023 int k;
1024 int have_comp_chs;
1025 int comp_was = MNE_CTFV_COMP_UNKNOWN;
1026
1027 if (comp_chs.isEmpty()) {
1028 comp_chs = chs;
1029 ncomp_chan = nchan;
1030 }
1031 undo.reset();
1032 current.reset();
1033 for (k = 0, have_comp_chs = 0; k < ncomp_chan; k++)
1034 if (comp_chs[k].kind == FIFFV_REF_MEG_CH)
1035 have_comp_chs++;
1036 if (have_comp_chs == 0 && compensate_to != MNE_CTFV_NOGRAD) {
1037 printf("No compensation channels in these data.");
1038 return FAIL;
1039 }
1040 /*
1041 * Update the 'current' field to reflect the compensation possibly present in the data now
1042 */
1043 if (make_comp(chs,nchan,comp_chs,ncomp_chan) == FAIL)
1044 goto bad;
1045 /*
1046 * Are we there already?
1047 */
1048 if (current && current->mne_kind == compensate_to) {
1049 printf("No further compensation necessary (comp = %s)\n",explain_comp(current->kind).toUtf8().constData());
1050 current.reset();
1051 return OK;
1052 }
1053 undo = std::move(current);
1054 if (compensate_to == MNE_CTFV_NOGRAD) {
1055 printf("No compensation was requested.\n");
1056 set_comp(chs,nchan,compensate_to);
1057 return OK;
1058 }
1059 if (set_comp(chs,nchan,compensate_to) > 0) {
1060 if (undo)
1061 comp_was = undo->mne_kind;
1062 else
1063 comp_was = MNE_CTFV_NOGRAD;
1064 if (make_comp(chs,nchan,comp_chs,ncomp_chan) == FAIL)
1065 goto bad;
1066 printf("Compensation set up as requested (%s -> %s).\n",
1067 explain_comp(map_comp_kind(comp_was)).toUtf8().constData(),
1068 explain_comp(current->kind).toUtf8().constData());
1069 }
1070 return OK;
1071
1072bad : {
1073 if (comp_was != MNE_CTFV_COMP_UNKNOWN)
1074 set_comp(chs,nchan,comp_was);
1075 return FAIL;
1076 }
1077}
#define FIFFV_EEG_CH
#define FIFF_OK
#define FIFF_MNE_CTF_COMP_KIND
#define FIFFV_COORD_DEVICE
#define FIFFB_MNE_CTF_COMP_DATA
#define FIFF_MNE_CTF_COMP_CALIBRATED
#define FIFF_FAIL
#define FIFFV_REF_MEG_CH
#define FIFFV_MEG_CH
#define FIFF_MNE_CTF_COMP_DATA
#define FIFFV_COORD_HEAD
#define FIFFB_MNE_CTF_COMP
#define FIFFB_MNE_PARENT_MEAS_FILE
#define FIFF_PARENT_BLOCK_ID
Definition fiff_file.h:333
#define FIFF_NCHAN
Definition fiff_file.h:453
#define FIFFTS_MC_RCS
Definition fiff_file.h:270
#define FIFFTS_MC_CCS
Definition fiff_file.h:269
#define FIFF_COORD_TRANS
Definition fiff_file.h:475
#define FIFF_CH_INFO
Definition fiff_file.h:456
#define FIFFB_MEAS_INFO
Definition fiff_file.h:363
Old fiff_type declarations - replace them.
FiffCoordTrans class declaration.
#define MNE_CTFV_COMP_G2BR
#define MNE_CTFV_COMP_NONE
#define MNE_CTFV_COMP_UNKNOWN
#define MNE_CTFV_COMP_G3BR
#define MNE_CTFV_COMP_G1BR
MNECTFCompDataSet class declaration.
Legacy MNE-C constants and common typedefs.
#define MNE_CTFV_GRAD1
Definition mne_types.h:132
#define MNE_CTFV_GRAD2
Definition mne_types.h:133
#define MNE_CTFV_GRAD3
Definition mne_types.h:134
#define MNE_4DV_COMP1
Definition mne_types.h:141
#define MNE_CTFV_NOGRAD
Definition mne_types.h:131
MNECTFCompData class declaration.
float mne_dot_vectors_32(float *v1, float *v2, int nn)
void mne_mat_vec_mult2_32(float **m, float *v, float *result, int d1, int d2)
FiffSparseMatrix * mne_convert_to_sparse(const Eigen::MatrixXf &dense, int stor_type, float small)
int mne_unmap_ctf_comp_kind(int ctf_comp)
int mne_sparse_vec_mult2_32(FiffSparseMatrix *mat, float *vector, float *res)
int mne_read_meg_comp_eeg_ch_info_32(const QString &name, QList< FIFFLIB::FiffChInfo > &megp, int *nmegp, QList< FIFFLIB::FiffChInfo > &meg_compp, int *nmeg_compp, QList< FIFFLIB::FiffChInfo > &eegp, int *neegp, FiffCoordTrans *meg_head_t, fiffId *idp)
#define TRUE
#define FALSE
#define OK
#define FAIL
Core MNE data structures (source spaces, source estimates, hemispheres).
FIFF file I/O and data structures (raw, epochs, evoked, covariance, forward).
qint32 fiff_int_t
Definition fiff_types.h:89
FiffId * fiffId
Backward-compatible pointer typedef for the old fiffId pointer.
Definition fiff_types.h:130
Channel info descriptor.
Coordinate transformation description.
QSharedPointer< FiffDirNode > SPtr
Universally unique identifier.
Definition fiff_id.h:67
FIFF sparse matrix storage.
FIFF File I/O routines.
QSharedPointer< FiffStream > SPtr
QSharedPointer< FiffTag > SPtr
Definition fiff_tag.h:155
Represents a single CTF compensation data element.
std::unique_ptr< FIFFLIB::FiffSparseMatrix > postsel
std::unique_ptr< MNENamedMatrix > data
int calibrate(const QList< FIFFLIB::FiffChInfo > &chs, int nch, int do_it)
std::unique_ptr< FIFFLIB::FiffSparseMatrix > presel
std::unique_ptr< MNECTFCompData > undo
int make_comp(const QList< FIFFLIB::FiffChInfo > &chs, int nch, QList< FIFFLIB::FiffChInfo > compchs, int ncomp)
std::unique_ptr< MNECTFCompData > current
QList< FIFFLIB::FiffChInfo > chs
static int set_comp(QList< FIFFLIB::FiffChInfo > &chs, int nch, int comp)
static std::unique_ptr< MNECTFCompDataSet > read(const QString &name)
int apply(int do_it, Eigen::VectorXf &data, const Eigen::VectorXf &compdata)
int apply_transpose(int do_it, Eigen::MatrixXf &data)
static QString explain_comp(int kind)
QList< MNECTFCompData * > comps
static int get_comp(const QList< FIFFLIB::FiffChInfo > &chs, int nch)
int set_compensation(int compensate_to, QList< FIFFLIB::FiffChInfo > &chs, int nchan, QList< FIFFLIB::FiffChInfo > comp_chs, int ncomp_chan)
static std::unique_ptr< MNENamedMatrix > read(QSharedPointer< FIFFLIB::FiffStream > &stream, const QSharedPointer< FIFFLIB::FiffDirNode > &node, int kind)
Factory: read a named matrix from a FIFF file.
static FiffCoordTrans readMeasTransform(const QString &name)
static FiffCoordTrans readFromTag(const QSharedPointer< FiffTag > &tag)