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;
87 if (!stream->open()) {
94 if (nodes.size() == 0) {
96 if (nodes.size() == 0) {
97 qCritical(
"Could not find the channel information.");
104 for (k = 0; k < info->nent(); k++) {
105 kind = info->dir[k]->kind;
106 pos = info->dir[k]->pos;
109 if (!stream->read_tag(t_pTag, pos)) {
113 nchan = *t_pTag->toInt();
115 for (j = 0; j < nchan; j++) {
123 if (!stream->read_tag(t_pTag, pos)) {
127 id = std::make_unique<FiffId>(*(
FiffId*)t_pTag->data());
131 if (!stream->read_tag(t_pTag, pos)) {
142 if (!stream->read_tag(t_pTag, pos)) {
147 this_ch = t_pTag->toChInfo();
149 qCritical(
"FIFF_CH_INFO : scan # out of range %d (%d)!", this_ch.
scanNo, nchan);
153 chs[this_ch.
scanNo - 1] = this_ch;
159 qCritical(
"Some of the channel information was missing.");
163 if (t.
isEmpty() && meg_head_t !=
nullptr) {
169 qCritical(
"MEG -> head coordinate transformation not found.");
177 for (k = 0; k < nchan; k++) {
182 meg_comp.append(chs[k]);
197 meg_compp = meg_comp;
199 *nmeg_compp = nmeg_comp;
207 if (idp ==
nullptr) {
211 if (meg_head_t ==
nullptr) {
218#define MNE_CTFV_COMP_UNKNOWN -1
219#define MNE_CTFV_COMP_NONE 0
220#define MNE_CTFV_COMP_G1BR 0x47314252
221#define MNE_CTFV_COMP_G2BR 0x47324252
222#define MNE_CTFV_COMP_G3BR 0x47334252
223#define MNE_CTFV_COMP_G2OI 0x47324f49
224#define MNE_CTFV_COMP_G3OI 0x47334f49
242 for (k = 0; compMap[k].grad_comp >= 0; k++)
244 return compMap[k].grad_comp;
258 float maxval = dense.cwiseAbs().maxCoeff();
260 small = maxval * std::fabs(small);
262 small = std::fabs(small);
265 Eigen::SparseMatrix<float> eigenSparse = dense.sparseView(small, 1.0f);
266 eigenSparse.makeCompressed();
268 if (eigenSparse.nonZeros() <= 0) {
269 qWarning(
"No nonzero elements found.");
273 return std::make_unique<FiffSparseMatrix>(std::move(eigenSparse),
FIFFTS_MC_RCS);
283 Eigen::Map<const Eigen::VectorXf> vecIn(vector, mat->
cols());
284 Eigen::Map<Eigen::VectorXf> vecOut(res, mat->
rows());
285 vecOut = mat->
eigen() * vecIn;
310 for (int k = 0; k < set.ncomp; k++)
312 this->comps.push_back(std::make_unique<MNECTFCompData>(*set.comps[k]));
313 this->ncomp = static_cast<int>(this->comps.size());
319 this->undo = std::make_unique<MNECTFCompData>(*set.
undo);
322 this->current = std::make_unique<MNECTFCompData>(*set.
current);
341 std::unique_ptr<MNECTFCompDataSet> set;
342 QList<FiffDirNode::SPtr> nodes;
343 QList<FiffDirNode::SPtr>
comps;
347 QList<FiffChInfo>
chs;
354 QList<FiffChInfo> comp_chs, temp;
368 for (k = 0; k < ncompch; k++)
369 chs.append(comp_chs[k]);
376 if (!stream->open()) {
380 set = std::make_unique<MNECTFCompDataSet>();
385 if (nodes.size() == 0) {
390 if (
comps.size() == 0) {
403 for (k = 0; k <
ncomp; k++) {
411 kind = *t_pTag->toInt();
418 calibrated = *t_pTag->toInt();
424 auto one = std::make_unique<MNECTFCompData>();
425 one->data = std::move(mat);
428 one->calibrated = calibrated;
430 if (one->calibrate(set->chs, set->nch,
true) ==
FAIL) {
431 qWarning(
"Warning: Compensation data for '%s' omitted\n",
explain_comp(one->kind).toUtf8().constData());
433 set->comps.push_back(std::move(one));
438 qInfo(
"%d CTF compensation data sets read from %s\n", set->ncomp, name);
448 QList<FiffChInfo> compchs,
454 Eigen::VectorXi compGrades;
458 Eigen::VectorXi comp_sel;
463 std::unique_ptr<FiffSparseMatrix> presel;
464 std::unique_ptr<FiffSparseMatrix> postsel;
465 std::unique_ptr<MNENamedMatrix> data;
467 QStringList emptyList;
469 if (compchs.isEmpty()) {
473 qInfo(
"Setting up compensation data...\n");
477 compGrades.resize(nChan);
480 compGrades[k] = chList[k].chpos.coil_type >> 16;
483 first_comp = compGrades[k];
485 if (compGrades[k] != first_comp) {
486 qCritical(
"We do not support nonuniform compensation yet.");
495 if (need_comp == 0) {
496 qInfo(
"\tNo compensation set. Nothing more to do.\n");
499 qInfo(
"\t%d out of %d channels have the compensation set.\n", need_comp, nChan);
503 for (k = 0, this_comp =
nullptr; k < this->
ncomp; k++) {
504 if (this->
comps[k]->mne_kind == first_comp) {
505 this_comp = this->
comps[k].get();
510 qCritical(
"Did not find the desired compensation data : %s",
518 comp_sel.resize(this_comp->
data->ncol);
519 for (k = 0; k < this_comp->
data->ncol; k++) {
521 name = this_comp->
data->collist[k];
522 for (p = 0; p < nCompChan; p++)
523 if (QString::compare(name, compchs[p].ch_name) == 0) {
527 if (comp_sel[k] < 0) {
528 qCritical(
"Compensation channel %s not found", name.toUtf8().constData());
532 qInfo(
"\tAll compensation channels found.\n");
537 Eigen::MatrixXf sel = Eigen::MatrixXf::Zero(this_comp->
data->ncol, nCompChan);
538 for (j = 0; j < this_comp->
data->ncol; j++)
539 sel(j, comp_sel[j]) = 1.0f;
543 qInfo(
"\tPreselector created.\n");
548 for (k = 0; k < nChan; k++) {
550 names.append(chList[k].ch_name);
554 auto d = this_comp->
data->pick(names, need_comp, emptyList, 0);
559 qInfo(
"\tCompensation data matrix created.\n");
564 Eigen::MatrixXf sel = Eigen::MatrixXf::Zero(nChan, data->nrow);
565 for (j = 0, p = 0; j < nChan; j++) {
572 qInfo(
"\tPostselector created.\n");
574 current = std::make_unique<MNECTFCompData>();
577 current->data = std::move(data);
578 current->presel = std::move(presel);
579 current->postsel = std::move(postsel);
581 qInfo(
"\tCompensation set up.\n");
596 for (k = 0, nset = 0; k <
nch; k++) {
598 chs[k].chpos.coil_type = (
chs[k].chpos.coil_type & 0xFFFF) | (comp << 16);
602 qInfo(
"A new compensation value (%s) was assigned to %d MEG channels.\n",
611 return apply(do_it, data, data);
622 int ndata =
static_cast<int>(data.size());
623 int ncompdata =
static_cast<int>(compdata.size());
632 if (this_comp->
presel->cols() != ncompdata) {
633 qCritical(
"Compensation data dimension mismatch. Expected %d, got %d channels.",
634 this_comp->
presel->cols(), ncompdata);
637 }
else if (this_comp->
data->ncol != ncompdata) {
638 qCritical(
"Compensation data dimension mismatch. Expected %d, got %d channels.",
639 this_comp->
data->ncol, ncompdata);
643 if (this_comp->
postsel->rows() != ndata) {
644 qCritical(
"Data dimension mismatch. Expected %d, got %d channels.",
645 this_comp->
postsel->rows(), ndata);
648 }
else if (this_comp->
data->nrow != ndata) {
649 qCritical(
"Data dimension mismatch. Expected %d, got %d channels.",
650 this_comp->
data->nrow, ndata);
664 presel = compdata.data();
671 Eigen::Map<const Eigen::VectorXf> preselVec(presel, this_comp->
data->ncol);
672 Eigen::Map<Eigen::VectorXf> compVec(this_comp->
comp_data.data(), this_comp->
data->nrow);
673 compVec = this_comp->
data->data * preselVec;
691 Eigen::Map<const Eigen::VectorXf> compVec(comp, ndata);
707 int ndata =
static_cast<int>(data.rows());
708 int ncompdata = ndata;
717 if (this_comp->
presel->cols() != ncompdata) {
718 qCritical(
"Compensation data dimension mismatch. Expected %d, got %d channels.",
719 this_comp->
presel->cols(), ncompdata);
722 }
else if (this_comp->
data->ncol != ncompdata) {
723 qCritical(
"Compensation data dimension mismatch. Expected %d, got %d channels.",
724 this_comp->
data->ncol, ncompdata);
728 if (this_comp->
postsel->rows() != ndata) {
729 qCritical(
"Data dimension mismatch. Expected %d, got %d channels.",
730 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;
751 Eigen::MatrixXf comp = this_comp->
data->data * preselMat;
756 comp = this_comp->
postsel->eigen() * comp;
773 int first_comp, comp;
776 for (k = 0, first_comp = -1; k <
nch; k++) {
778 comp =
chs[k].chpos.coil_type >> 16;
781 else if (first_comp != comp) {
782 qCritical(
"Non uniform compensation not supported.");
801 for (k = 0; compMap[k].grad_comp >= 0; k++)
803 return compMap[k].ctf_comp;
824 if (explain[k].kind == kind)
825 return QString::fromLatin1(explain[k].expl);
826 return QString::fromLatin1(explain[k].expl);
832 QList<FiffChInfo>& chList,
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");
873 set_comp(chList, nchan, compensate_to);
876 if (
set_comp(chList, nchan, compensate_to) > 0) {
878 comp_was =
undo->mne_kind;
881 if (
make_comp(chList, nchan, comp_chs, ncomp_chan) ==
FAIL) {
886 qInfo(
"Compensation set up as requested (%s -> %s).\n",
#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
4x4 affine FIFF coordinate transform (FIFF_COORD_TRANS) annotated with source/destination coordinate-...
#define FIFF_PARENT_BLOCK_ID
return FiffCoordTrans(from_frame, to_frame, R, moveVec)
Primitive scalar typedefs and forward-compatible aliases backing the FIFF type system.
Set of CTF compensation matrices plus the currently active grade.
Legacy MNE-C constants and shared typedefs used across MNELIB structures.
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
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)
Core MNE data structures (source spaces, source estimates, hemispheres).
FIFF file I/O, in-memory data structures and high-level readers/writers.
FiffId * fiffId
Backward-compatible pointer typedef for the old fiffId pointer.
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
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 make_comp(const QList< FIFFLIB::FiffChInfo > &chList, int nChan, QList< FIFFLIB::FiffChInfo > compchs, int nCompChan)
int apply(bool do_it, Eigen::Ref< Eigen::VectorXf > data, Eigen::Ref< const Eigen::VectorXf > compdata)
int set_compensation(int compensate_to, QList< FIFFLIB::FiffChInfo > &chList, int nchan, QList< FIFFLIB::FiffChInfo > comp_chs, int ncomp_chan)
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)
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.