54 QList<FIFFLIB::FiffChInfo>& megp,
56 QList<FIFFLIB::FiffChInfo>& meg_compp,
58 QList<FIFFLIB::FiffChInfo>& eegp,
70 QList<FIFFLIB::FiffChInfo> chs;
72 QList<FIFFLIB::FiffChInfo> meg;
74 QList<FIFFLIB::FiffChInfo> meg_comp;
76 QList<FIFFLIB::FiffChInfo> eeg;
78 std::unique_ptr<FiffId> id;
79 QList<FiffDirNode::SPtr> nodes;
93 if (nodes.size() == 0) {
95 if (nodes.size() == 0) {
96 qCritical (
"Could not find the channel information.");
102 for (k = 0; k < info->nent(); k++) {
103 kind = info->dir[k]->kind;
104 pos = info->dir[k]->pos;
107 if (!stream->read_tag(t_pTag,pos)) {
110 nchan = *t_pTag->toInt();
112 for (j = 0; j < nchan; j++) {
120 if(!stream->read_tag(t_pTag, pos)) {
123 id = std::make_unique<FiffId>(*(
FiffId*)t_pTag->data());
127 if(!stream->read_tag(t_pTag, pos)) {
137 if(!stream->read_tag(t_pTag, pos)) {
141 this_ch = t_pTag->toChInfo();
143 qCritical (
"FIFF_CH_INFO : scan # out of range %d (%d)!",this_ch.
scanNo,nchan);
147 chs[this_ch.
scanNo-1] = this_ch;
153 qCritical(
"Some of the channel information was missing.");
156 if (t.
isEmpty() && meg_head_t !=
nullptr) {
162 qCritical(
"MEG -> head coordinate transformation not found.");
169 for (k = 0; k < nchan; k++) {
174 meg_comp.append(chs[k]);
189 meg_compp = meg_comp;
191 *nmeg_compp = nmeg_comp;
199 if (idp ==
nullptr) {
204 if (meg_head_t ==
nullptr) {
212#define MNE_CTFV_COMP_UNKNOWN -1
213#define MNE_CTFV_COMP_NONE 0
214#define MNE_CTFV_COMP_G1BR 0x47314252
215#define MNE_CTFV_COMP_G2BR 0x47324252
216#define MNE_CTFV_COMP_G3BR 0x47334252
217#define MNE_CTFV_COMP_G2OI 0x47324f49
218#define MNE_CTFV_COMP_G3OI 0x47334f49
235 for (k = 0; compMap[k].grad_comp >= 0; k++)
237 return compMap[k].grad_comp;
251 float maxval = dense.cwiseAbs().maxCoeff();
253 small = maxval*std::fabs(small);
255 small = std::fabs(small);
258 Eigen::SparseMatrix<float> eigenSparse = dense.sparseView(small, 1.0f);
259 eigenSparse.makeCompressed();
261 if (eigenSparse.nonZeros() <= 0) {
262 qWarning(
"No nonzero elements found.");
266 return std::make_unique<FiffSparseMatrix>(std::move(eigenSparse),
FIFFTS_MC_RCS);
276 Eigen::Map<const Eigen::VectorXf> vecIn(vector, mat->
cols());
277 Eigen::Map<Eigen::VectorXf> vecOut(res, mat->
rows());
278 vecOut = mat->
eigen() * vecIn;
303 for (int k = 0; k < set.ncomp; k++)
305 this->comps.push_back(std::make_unique<MNECTFCompData>(*set.comps[k]));
306 this->ncomp = this->comps.size();
312 this->undo = std::make_unique<MNECTFCompData>(*set.
undo);
315 this->current = std::make_unique<MNECTFCompData>(*set.
current);
334 std::unique_ptr<MNECTFCompDataSet> set;
335 QList<FiffDirNode::SPtr> nodes;
336 QList<FiffDirNode::SPtr>
comps;
340 QList<FiffChInfo>
chs;
347 QList<FiffChInfo> comp_chs, temp;
361 for (k = 0; k < ncompch; k++)
362 chs.append(comp_chs[k]);
369 if(!stream->open()) {
370 stream->close();
return nullptr;
372 set = std::make_unique<MNECTFCompDataSet>();
377 if (nodes.size() == 0) {
378 stream->close();
return set;
381 if (
comps.size() == 0) {
382 stream->close();
return set;
393 for (k = 0; k <
ncomp; k++) {
396 stream->close();
return nullptr;
400 kind = *t_pTag->toInt();
403 stream->close();
return nullptr;
407 calibrated = *t_pTag->toInt();
414 auto one = std::make_unique<MNECTFCompData>();
415 one->data = std::move(mat);
418 one->calibrated = calibrated;
420 if (one->calibrate(set->chs,set->nch,
true) ==
FAIL) {
421 qWarning(
"Warning: Compensation data for '%s' omitted\n",
explain_comp(one->kind).toUtf8().constData());
424 set->comps.push_back(std::move(one));
429 qInfo(
"%d CTF compensation data sets read from %s\n",set->ncomp,name);
439 QList<FiffChInfo> compchs,
445 Eigen::VectorXi
comps;
449 Eigen::VectorXi comp_sel;
454 std::unique_ptr<FiffSparseMatrix> presel;
455 std::unique_ptr<FiffSparseMatrix> postsel;
456 std::unique_ptr<MNENamedMatrix> data;
458 QStringList emptyList;
460 if (compchs.isEmpty()) {
464 qInfo(
"Setting up compensation data...\n");
471 comps[k] =
chs[k].chpos.coil_type >> 16;
474 first_comp =
comps[k];
476 if (
comps[k] != first_comp) {
477 qCritical(
"We do not support nonuniform compensation yet.");
487 if (need_comp == 0) {
488 qInfo(
"\tNo compensation set. Nothing more to do.\n");
491 qInfo(
"\t%d out of %d channels have the compensation set.\n",need_comp,
nch);
495 for (k = 0, this_comp =
nullptr; k < this->ncomp; k++) {
496 if (this->comps[k]->mne_kind == first_comp) {
497 this_comp = this->comps[k].get();
502 qCritical(
"Did not find the desired compensation data : %s",
510 comp_sel.resize(this_comp->
data->ncol);
511 for (k = 0; k < this_comp->
data->ncol; k++) {
513 name = this_comp->
data->collist[k];
514 for (p = 0; p <
ncomp; p++)
515 if (QString::compare(name,compchs[p].ch_name) == 0) {
519 if (comp_sel[k] < 0) {
520 qCritical(
"Compensation channel %s not found",name.toUtf8().constData());
524 qInfo(
"\tAll compensation channels found.\n");
529 Eigen::MatrixXf sel = Eigen::MatrixXf::Zero(this_comp->
data->ncol,
ncomp);
530 for (j = 0; j < this_comp->
data->ncol; j++)
531 sel(j, comp_sel[j]) = 1.0f;
535 qInfo(
"\tPreselector created.\n");
540 for (k = 0; k <
nch; k++) {
542 names.append(
chs[k].ch_name);
546 auto d = this_comp->
data->pick(names, need_comp, emptyList, 0);
551 qInfo(
"\tCompensation data matrix created.\n");
556 Eigen::MatrixXf sel = Eigen::MatrixXf::Zero(
nch, data->nrow);
557 for (j = 0, p = 0; j <
nch; j++) {
564 qInfo(
"\tPostselector created.\n");
566 current = std::make_unique<MNECTFCompData>();
569 current->data = std::move(data);
570 current->presel = std::move(presel);
571 current->postsel = std::move(postsel);
573 qInfo(
"\tCompensation set up.\n");
588 for (k = 0, nset = 0; k <
nch; k++) {
590 chs[k].chpos.coil_type = (
chs[k].chpos.coil_type & 0xFFFF) | (comp << 16);
594 qInfo(
"A new compensation value (%s) was assigned to %d MEG channels.\n",
603 return apply(do_it, data, data);
614 int ndata =
static_cast<int>(data.size());
615 int ncompdata =
static_cast<int>(compdata.size());
624 if (this_comp->
presel->cols() != ncompdata) {
625 qCritical(
"Compensation data dimension mismatch. Expected %d, got %d channels.",
626 this_comp->
presel->cols(),ncompdata);
630 else if (this_comp->
data->ncol != ncompdata) {
631 qCritical(
"Compensation data dimension mismatch. Expected %d, got %d channels.",
632 this_comp->
data->ncol,ncompdata);
636 if (this_comp->
postsel->rows() != ndata) {
637 qCritical(
"Data dimension mismatch. Expected %d, got %d channels.",
638 this_comp->
postsel->rows(),ndata);
642 else if (this_comp->
data->nrow != ndata) {
643 qCritical(
"Data dimension mismatch. Expected %d, got %d channels.",
644 this_comp->
data->nrow,ndata);
659 presel = compdata.data();
666 Eigen::Map<const Eigen::VectorXf> preselVec(presel, this_comp->
data->ncol);
667 Eigen::Map<Eigen::VectorXf> compVec(this_comp->
comp_data.data(), this_comp->
data->nrow);
668 compVec = this_comp->
data->data * preselVec;
686 Eigen::Map<const Eigen::VectorXf> compVec(comp, ndata);
701 using RowMatrixXf = Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
704 int ndata =
static_cast<int>(data.rows());
705 int ns =
static_cast<int>(data.cols());
706 int ncompdata = ndata;
715 if (this_comp->
presel->cols() != ncompdata) {
716 qCritical(
"Compensation data dimension mismatch. Expected %d, got %d channels.",
717 this_comp->
presel->cols(),ncompdata);
721 else if (this_comp->
data->ncol != ncompdata) {
722 qCritical(
"Compensation data dimension mismatch. Expected %d, got %d channels.",
723 this_comp->
data->ncol,ncompdata);
727 if (this_comp->
postsel->rows() != ndata) {
728 qCritical(
"Data dimension mismatch. Expected %d, got %d channels.",
729 this_comp->
postsel->rows(),ndata);
733 else if (this_comp->
data->nrow != ndata) {
734 qCritical(
"Data dimension mismatch. Expected %d, got %d channels.",
735 this_comp->
data->nrow,ndata);
741 Eigen::MatrixXf preselMat;
743 preselMat = this_comp->
presel->eigen() * data;
752 Eigen::MatrixXf comp = this_comp->
data->data * preselMat;
757 comp = this_comp->
postsel->eigen() * comp;
777 for (k = 0, first_comp = -1; k <
nch; k++) {
779 comp =
chs[k].chpos.coil_type >> 16;
782 else if (first_comp != comp) {
783 qCritical(
"Non uniform compensation not supported.");
802 for (k = 0; compMap[k].grad_comp >= 0; k++)
804 return compMap[k].ctf_comp;
812 static const struct {
824 if (explain[k].kind == kind)
825 return QString::fromLatin1(explain[k].expl);
826 return QString::fromLatin1(explain[k].expl);
832 QList<FiffChInfo>&
chs,
834 QList<FiffChInfo> comp_chs,
844 if (comp_chs.isEmpty()) {
850 for (k = 0, have_comp_chs = 0; k < ncomp_chan; k++)
854 qWarning(
"No compensation channels in these data.");
866 qInfo(
"No further compensation necessary (comp = %s)\n",
explain_comp(
current->kind).toUtf8().constData());
872 qInfo(
"No compensation was requested.\n");
878 comp_was =
undo->mne_kind;
886 qInfo(
"Compensation set up as requested (%s -> %s).\n",
Set of CTF compensation matrices plus the currently active grade.
std::unique_ptr< 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)
Single CTF reference-sensor compensation matrix labelled by kind.
#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
Legacy MNE-C constants and shared typedefs used across MNELIB structures.
return FiffCoordTrans(from_frame, to_frame, R, moveVec)
#define FIFF_MNE_CTF_COMP_KIND
#define FIFFV_COORD_DEVICE
#define FIFFB_MNE_CTF_COMP_DATA
#define FIFF_MNE_CTF_COMP_CALIBRATED
#define FIFF_MNE_CTF_COMP_DATA
#define FIFFB_MNE_CTF_COMP
#define FIFFB_MNE_PARENT_MEAS_FILE
#define FIFF_PARENT_BLOCK_ID
4x4 affine FIFF coordinate transform (FIFF_COORD_TRANS) annotated with source/destination coordinate-...
Primitive scalar typedefs and forward-compatible aliases backing the FIFF type system.
FiffId * fiffId
Backward-compatible pointer typedef for the old fiffId pointer.
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 readMeasTransform(const QString &name)
static FiffCoordTrans readFromTag(const std::unique_ptr< FiffTag > &tag)
QSharedPointer< FiffDirNode > SPtr
128-bit FIFF identifier: hardware machine ID plus creation time, stamped on every file and block.
Sparse FIFF matrix: CCS or RCS storage with the value / index / pointer triple as written by FiffStre...
Eigen::SparseMatrix< float > & eigen()
FIFF tag-stream reader/writer: wraps a QIODevice and exposes typed read_* / write_* methods for every...
QSharedPointer< FiffStream > SPtr
std::unique_ptr< FiffTag > UPtr
Represents a single CTF compensation data element.
std::unique_ptr< FIFFLIB::FiffSparseMatrix > postsel
std::unique_ptr< MNENamedMatrix > data
Eigen::VectorXf comp_data
std::unique_ptr< FIFFLIB::FiffSparseMatrix > presel
Eigen::VectorXf postsel_data
Eigen::VectorXf presel_data
std::vector< std::unique_ptr< MNECTFCompData > > comps
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(bool do_it, Eigen::Ref< Eigen::VectorXf > data, Eigen::Ref< const Eigen::VectorXf > compdata)
int apply_transpose(bool do_it, Eigen::MatrixXf &data)
static QString explain_comp(int kind)
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 int map_comp_kind(int grad)
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.