50#include <QCoreApplication>
63#ifndef FIFFV_COIL_CTF_GRAD
64#define FIFFV_COIL_CTF_GRAD 5001
67#ifndef FIFFV_COIL_CTF_REF_MAG
68#define FIFFV_COIL_CTF_REF_MAG 5002
71#ifndef FIFFV_COIL_CTF_REF_GRAD
72#define FIFFV_COIL_CTF_REF_GRAD 5003
75#ifndef FIFFV_COIL_CTF_OFFDIAG_REF_GRAD
76#define FIFFV_COIL_CTF_OFFDIAG_REF_GRAD 5004
90,
r0(Eigen::Vector3f::Zero())
116 int fit_sphere_to_bem =
true;
125 qInfo(
"\nSetting up the BEM model using %s...", d->
bemname.toUtf8().constData());
126 qInfo(
"\nLoading surfaces...");
129 qInfo(
"Three-layer model surfaces loaded.");
134 qInfo(
"Homogeneous model surface loaded.");
137 qCritical(
"Cannot use a homogeneous model in EEG calculations.");
140 qInfo(
"\nLoading the solution matrix...");
143 qInfo(
"Employing the head->MRI coordinate transform with the BEM model.");
147 qInfo(
"BEM model %s is now set up", d->
bem_model->sol_name.toUtf8().constData());
151 if (fit_sphere_to_bem) {
153 float simplex_size = 2e-2f;
162 d->
r0 = r0_vec.head<3>();
165 qInfo(
"Fitted sphere model origin : %6.1f %6.1f %6.1f mm rad = %6.1f mm.",
166 1000 * d->
r0[0], 1000 * d->
r0[1], 1000 * d->
r0[2], 1000 *
R);
168 d->
bem_funcs = std::make_unique<dipoleFitFuncsRec>();
179 qInfo(
"Compensation setup done.");
181 qInfo(
"MEG solution matrix...");
196 qInfo(
"\tEEG solution matrix...");
206 qCritical(
"EEG sphere model not defined.");
209 d->
sphere_funcs = std::make_unique<dipoleFitFuncsRec>();
236 qInfo(
"Sphere model origin : %6.1f %6.1f %6.1f mm.",
237 1000 * d->
r0[0], 1000 * d->
r0[1], 1000 * d->
r0[2]);
280 Eigen::VectorXd stds;
284 qInfo(
"Using standard noise values "
285 "(MEG grad : %6.1f fT/cm MEG mag : %6.1f fT EEG : %6.1f uV)\n",
286 1e13 * grad_std, 1e15 * mag_std, 1e6 * eeg_std);
290 nchan = nchan + meg->
ncoil();
292 nchan = nchan + eeg->
ncoil();
298 for (k = 0; k < meg->
ncoil(); k++, n++) {
299 if (meg->
coils[k]->is_axial_coil()) {
300 stds[n] =
static_cast<double>(mag_std) * mag_std;
305 stds[n] = 1e6 * stds[n];
308 stds[n] =
static_cast<double>(grad_std) * grad_std;
313 for (k = 0; k < eeg->
ncoil(); k++, n++) {
314 stds[n] =
static_cast<double>(eeg_std) * eeg_std;
326 float nave_ratio =
static_cast<float>(f->
nave) /
static_cast<float>(
nave);
332 if (f->
noise->cov.size() > 0) {
333 qInfo(
"Decomposing the sensor noise covariance matrix...");
337 for (k = 0; k < f->
noise->ncov * (f->
noise->ncov + 1) / 2; k++)
338 f->
noise->cov[k] = nave_ratio * f->
noise->cov[k];
339 for (k = 0; k < f->
noise->ncov; k++) {
340 f->
noise->lambda[k] = nave_ratio * f->
noise->lambda[k];
341 if (f->
noise->lambda[k] < 0.0)
342 f->
noise->lambda[k] = 0.0;
347 for (k = 0; k < f->
noise->ncov; k++)
348 f->
noise->cov_diag[k] = nave_ratio * f->
noise->cov_diag[k];
349 qInfo(
"Decomposition not needed for a diagonal noise covariance matrix.");
353 qInfo(
"Effective nave is now %d",
nave);
362 float nave_ratio =
static_cast<float>(f->
nave) /
static_cast<float>(
nave);
370 if (f->
noise->cov.size() > 0) {
374 qInfo(
"Decomposing the noise covariance...");
375 if (f->
noise->cov.size() > 0) {
378 for (k = 0; k < f->
noise->ncov; k++) {
379 if (f->
noise->lambda[k] < 0.0)
380 f->
noise->lambda[k] = 0.0;
383 for (k = 0; k < f->
noise->ncov * (f->
noise->ncov + 1) / 2; k++)
384 f->
noise->cov[k] = nave_ratio * f->
noise->cov[k];
385 for (k = 0; k < f->
noise->ncov; k++) {
386 f->
noise->lambda[k] = nave_ratio * f->
noise->lambda[k];
387 if (f->
noise->lambda[k] < 0.0)
388 f->
noise->lambda[k] = 0.0;
393 for (k = 0; k < f->
noise->ncov; k++)
394 f->
noise->cov_diag[k] = nave_ratio * f->
noise->cov_diag[k];
395 qInfo(
"Decomposition not needed for a diagonal noise covariance matrix.");
399 qInfo(
"Effective nave is now %d",
nave);
439 float* w = wVec.data();
440 int nomit_meg, nomit_eeg,
nmeg,
neeg;
443 nomit_meg = nomit_eeg = 0;
450 bool selected =
false;
451 for (
int c = 0; c < meas->
nchan; c++) {
453 meas->
chs[c].ch_name,
454 Qt::CaseInsensitive) == 0) {
455 selected = sels[c] != 0;
470 if (
nmeg > 0 &&
nmeg - nomit_meg > 0 &&
nmeg - nomit_meg < min_nchan) {
471 qCritical(
"Too few MEG channels remaining");
474 if (
neeg > 0 &&
neeg - nomit_eeg > 0 &&
neeg - nomit_eeg < min_nchan) {
475 qCritical(
"Too few EEG channels remaining");
479 if (nomit_meg + nomit_eeg > 0) {
480 if (f->
noise->cov.size() > 0) {
481 for (j = 0; j < f->
noise->ncov; j++)
482 for (k = 0; k <= j; k++) {
486 for (j = 0; j < f->
noise->ncov; j++) {
487 f->
noise->cov_diag[j] *=
static_cast<double>(w[j]) * w[j];
506 const QString& measname,
511 const QString& badname,
512 const QString& noisename,
520 const QList<QString>& projnames,
524 auto res = std::make_unique<InvDipoleFitData>();
527 QStringList file_bads;
530 std::unique_ptr<MNECovMatrix> cov;
531 std::unique_ptr<FwdCoilSet> templates;
532 std::unique_ptr<MNECTFCompDataSet> comp_data;
533 std::unique_ptr<FwdCoilSet> comp_coils;
538 if (!mriname.isEmpty()) {
540 if (res->mri_head_t->isEmpty())
542 }
else if (!
bemname.isEmpty()) {
543 qWarning(
"Source of MRI / head transform required for the BEM model is missing");
546 float move[] = {0.0, 0.0, 0.0};
547 float rot[3][3] = {{1.0, 0.0, 0.0},
550 Eigen::Matrix3f rotMat;
551 rotMat << rot[0][0], rot[0][1], rot[0][2],
552 rot[1][0], rot[1][1], rot[1][2],
553 rot[2][0], rot[2][1], rot[2][2];
554 Eigen::Vector3f
moveVec = Eigen::Map<Eigen::Vector3f>(move);
558 res->mri_head_t->print();
560 if (res->meg_head_t->isEmpty())
562 res->meg_head_t->print();
566 if (!badname.isEmpty()) {
569 nbad = badlist.size();
570 qInfo(
"%d bad channels read from %s.", nbad, badname.toUtf8().data());
573 QFile measFile(measname);
575 if (measStream->open()) {
576 file_bads = measStream->read_bad_channels(measStream->dirtree());
577 file_nbad = file_bads.size();
582 if (badlist.isEmpty())
584 for (
int k = 0; k < file_nbad; k++) {
585 badlist.append(file_bads[k]);
589 qInfo(
"%d bad channels read from the data file.", file_nbad);
591 qInfo(
"%d bad channels total.", nbad);
605 qInfo(
"Will use %3d MEG channels from %s", res->nmeg, measname.toUtf8().data());
607 qInfo(
"Will use %3d EEG channels from %s", res->neeg, measname.toUtf8().data());
609 int nch_total = res->nmeg + res->neeg;
610 res->ch_names.clear();
611 for (
int i = 0; i < nch_total; i++)
612 res->ch_names.append(res->chs[i].ch_name);
628 QString qPath = QString(QCoreApplication::applicationDirPath() +
"/../resources/general/coilDefinitions/coil_def.dat");
630 if (!QCoreApplication::startingUp())
631 qPath = QCoreApplication::applicationDirPath() + QString(
"/../resources/general/coilDefinitions/coil_def.dat");
632 else if (!file.exists())
633 qPath =
"../resources/general/coilDefinitions/coil_def.dat";
635 QByteArray coilfileBytes = qPath.toUtf8();
636 const char* coilfile = coilfileBytes.constData();
646 Q_ASSERT(res->meg_head_t);
647 res->meg_coils = templates->create_meg_coils(res->chs,
657 qInfo(
"Head coordinate coil definitions created.");
676 if (comp_data->ncomp > 0) {
677 QList<FiffChInfo> comp_chs;
680 qInfo(
"%d compensation data sets in %s", comp_data->ncomp, measname.toUtf8().data());
682 QFile compFile(measname);
684 if (!compStream->open())
688 if (!compStream->read_meas_info(compStream->dirtree(), compInfo, compInfoNode)) {
693 for (
int k = 0; k < compInfo.
chs.size(); k++) {
695 comp_chs.append(compInfo.
chs[k]);
701 comp_coils = templates->create_meg_coils(comp_chs,
708 qInfo(
"%d compensation channels in %s", comp_coils->ncoil(), measname.toUtf8().data());
724 res->nmeg + res->neeg,
727 if (res->proj && res->proj->nitems > 0) {
728 qInfo(
"Final projection operator is:");
730 QTextStream errStream(stderr);
731 res->proj->report(errStream, QStringLiteral(
"\t"));
734 if (res->proj->assign_channels(res->ch_names, res->nmeg + res->neeg) ==
FAIL)
736 if (res->proj->make_proj() ==
FAIL)
739 qInfo(
"No projection will be applied to the data.");
744 if (!noisename.isEmpty()) {
747 qInfo(
"Read a %s noise-covariance matrix from %s",
748 cov->cov_diag.size() > 0 ?
"diagonal" :
"full", noisename.toUtf8().data());
750 if ((cov =
ad_hoc_noise(res->meg_coils.get(), res->eeg_els.get(), grad_std, mag_std, eeg_std)) ==
nullptr)
753 res->noise = cov->pick_chs_omit(res->ch_names,
754 res->nmeg + res->neeg,
761 qInfo(
"Picked appropriate channels from the noise-covariance matrix.");
767 if (res->proj && res->proj->nitems > 0 && res->proj->nvec > 0) {
768 if (res->proj->apply_cov(res->noise.get()) ==
FAIL)
770 qInfo(
"Projection applied to the covariance matrix.");
777 res->noise->revert_to_diag();
778 qInfo(
"Using only the main diagonal of the noise-covariance matrix.");
784 if (res->noise->cov.size() > 0) {
785 Eigen::Vector3f regs;
794 if (res->noise->classify_channels(res->chs,
795 res->nmeg + res->neeg) ==
FAIL)
801 for (
int k = 0; k < res->noise->ncov; k++) {
803 regs[res->noise->ch_class[k]] > 0.0)
810 res->noise->regularize(regs);
812 qInfo(
"No regularization applied to the noise-covariance matrix");
818 qInfo(
"Decomposing the noise covariance...");
819 if (res->noise->cov.size() > 0) {
820 if (res->noise->decompose_eigen() ==
FAIL)
822 qInfo(
"Eigenvalue decomposition done.");
823 for (
int k = 0; k < res->noise->ncov; k++) {
824 if (res->noise->lambda[k] < 0.0)
825 res->noise->lambda[k] = 0.0;
828 qInfo(
"Decomposition not needed for a diagonal covariance matrix.");
829 if (res->noise->add_inv() ==
FAIL)
834 return res.release();
840 const Eigen::Vector3f& Q,
847 const int nch = fit.
nmeg + fit.
neeg;
849 qWarning(
"print_fields: the data do not hold the %d fit channels", nch);
852 Eigen::VectorXf measured(nch);
854 qWarning(
"Cannot pick time: %7.1f ms", 1000 * time);
857 if (fit.
proj && fit.
proj->project_vector(measured,
true) ==
FAIL)
860 Eigen::MatrixXf fwd(nch, 3);
863 const Eigen::VectorXf predicted = fwd * Q;
865 for (
int k = 0; k < nch; ++k) {
866 const double scale = k < fit.
nmeg ? 1e15 : 1e6;
867 out << fit.
ch_names[k] <<
'\t' << scale * measured[k] <<
'\t' << scale * predicted[k] <<
'\n';
896 res->
fwd.resize(m, nch);
897 res->
uu.resize(m, nch);
898 res->
vv.resize(m, m);
901 res->
rd.resize(ndip, 3);
906 for (k = 0; k < ndip; k++) {
907 res->
rd.row(k) = Eigen::Map<const Eigen::Vector3f>(rd[k]).transpose();
911 Eigen::MatrixXf this_fwd(d->
nmeg + d->
neeg, 3);
912 Eigen::Map<const Eigen::Vector3f> rd_k(rd[k]);
918 for (
int p = 0; p < 3; p++)
919 res->
fwd.row(3 * k + p) = this_fwd.col(p).transpose();
925 for (
int p = 0; p < 3; p++)
926 S[p] = res->
fwd.row(3 * k + p).squaredNorm();
928 for (
int p = 0; p < 3; p++)
929 res->
scales[3 * k + p] = sqrt(
S[p]);
934 res->
scales[3 * k + 0] = res->
scales[3 * k + 1] = res->
scales[3 * k + 2] = sqrt(
S[0] +
S[1] +
S[2]) / 3.0;
936 for (
int p = 0; p < 3; p++) {
937 if (res->
scales[3 * k + p] > 0.0) {
939 res->
fwd.row(3 * k + p) *= res->
scales[3 * k + p];
941 res->
scales[3 * k + p] = 1.0;
945 res->
scales[3 * k + 1] = 1.0;
946 res->
scales[3 * k + 2] = 1.0;
958 int udim = std::min(m, n);
959 JacobiSVD<MatrixXf>
svd(res->
fwd, ComputeFullU | ComputeFullV);
960 res->
sing =
svd.singularValues();
961 res->
uu =
svd.matrixV().transpose().topRows(udim);
962 res->
vv =
svd.matrixU().transpose().topRows(udim);
974 const Eigen::Vector3f& rd,
978 rds[0] =
const_cast<float*
>(rd.data());
987static float fit_eval(
const VectorXf& rd,
const void* user)
998 qInfo(
"ncomp = %d", ncomp);
1000 Eigen::Map<const VectorXf> Bmap(fuser->
B, fwd->
nch);
1001 for (c = 0, Bm2 = 0.0; c < ncomp; c++) {
1002 one = fwd->
uu.row(c).dot(Bmap);
1003 Bm2 = Bm2 + one * one;
1005 return fuser->
B2 - Bm2;
1011static int find_best_guess(
const Eigen::Ref<const Eigen::VectorXf>& B,
1019 double B2, Bm2, this_good, one;
1025 B2 = B.squaredNorm();
1026 for (k = 0; k < guess->
nguess; k++) {
1028 if (fwd->
nch == nch) {
1029 ncomp = fwd->
sing[2] / fwd->
sing[0] > limit ? 3 : 2;
1030 for (c = 0, Bm2 = 0.0; c < ncomp; c++) {
1031 one = fwd->
uu.row(c).dot(B);
1032 Bm2 = Bm2 + one * one;
1034 this_good = 1.0 - (B2 - Bm2) / B2;
1035 if (this_good > good) {
1042 qWarning(
"No reasonable initial guess found.");
1053static MatrixXf make_initial_dipole_simplex(
const Eigen::Vector3f& r0,
1062 float x = sqrt(3.0f) / 3.0f;
1063 float r = sqrt(6.0f) / 12.0f;
1066 float rr[][3] = {{x, 0.0f, -r},
1071 MatrixXf simplex = MatrixXf::Zero(4, 3);
1073 for (
int j = 0; j < 4; j++) {
1074 simplex.row(j) = Eigen::Map<const Vector3f>(rr[j]).transpose() * size + r0.transpose();
1079static bool dipole_report_func(
int loop,
1080 const VectorXf& fitpar,
1085 qInfo(
"loop %d rd %7.2f %7.2f %7.2f fval %g %g par diff %g",
1086 loop, 1000 * fitpar[0], 1000 * fitpar[1], 1000 * fitpar[2], fval_lo, fval_hi, 1000 * par_diff);
1095 const Eigen::Ref<const Eigen::VectorXf>& B,
1096 const Eigen::Vector3f& rd,
1109 ncomp = fwd->
sing[2] / fwd->
sing[0] > limit ? 3 : 2;
1112 for (c = 0, Bm2 = 0.0; c < ncomp; c++) {
1113 one = fwd->
uu.row(c).dot(B);
1114 Q += (one / fwd->
sing[c]) * fwd->
vv.row(c).head(3).transpose();
1115 Bm2 = Bm2 + one * one;
1120 for (c = 0; c < 3; c++)
1121 Q[c] = fwd->
scales[c] * Q[c];
1122 res = B.squaredNorm() - Bm2;
1145 Eigen::Ref<Eigen::VectorXf> B,
1152 float ftol[] = {1e-2f, 1e-2f};
1153 float atol[] = {0.2e-3f, 0.2e-3f};
1156 int max_eval = 1000;
1157 int report_interval = verbose ? 1 : -1;
1160 float good, final_val;
1161 Eigen::Vector3f rd_final, Q;
1163 int k, neval, neval_tot, nchan, ncomp;
1170 if (fit->
proj && fit->
proj->project_vector(B,
true) ==
FAIL)
1173 if (fit->
noise->whiten_vector(B, B, nchan) ==
FAIL)
1178 if (find_best_guess(B, nchan, guess, limit, best, good) < 0)
1183 user.B2 = B.squaredNorm();
1185 user.report_dim =
false;
1188 rd_guess = guess->
rr.row(best).transpose();
1189 rd_final = rd_guess;
1193 for (k = 0; k < ntol; k++) {
1202 MatrixXf simplexMat = make_initial_dipole_simplex(rd_guess, size);
1203 for (
int p = 0; p < 4; p++)
1204 vals[p] = fit_eval(simplexMat.row(p), fit);
1207 auto cost = [fit](
const VectorXf& x) ->
float {
1208 return fit_eval(x, fit);
1220 dipole_report_func)) {
1225 float rv = 2.0f * (vals.maxCoeff() - vals.minCoeff()) / (vals.maxCoeff() + vals.minCoeff());
1226 qWarning(
"Warning (t = %8.1f ms) : g = %6.1f %% final val = %7.3f rtol = %f",
1227 1000 * time, 100 * (1 - vals[0] /
user.B2), vals[0], rv);
1231 rd_final = simplexMat.row(0).transpose();
1232 rd_guess = simplexMat.row(0).transpose();
1235 final_val = vals[0];
1243 if (fit_Q(fit, B, rd_final,
user.limit, Q, ncomp, final_val) ==
OK) {
1248 res.
good = 1.0 - final_val /
user.B2;
1251 res.
khi2 = final_val;
1253 res.
nfree = nchan - 3 - ncomp - fit->
proj->nvec;
1255 res.
nfree = nchan - 3 - ncomp;
1256 res.
neval = neval_tot;
1275 static const Eigen::Vector3f Qx(1.0f, 0.0f, 0.0f);
1276 static const Eigen::Vector3f Qy(0.0f, 1.0f, 0.0f);
1277 static const Eigen::Vector3f Qz(0.0f, 0.0f, 1.0f);
1290 Eigen::MatrixXf vec_meg(3,
nmeg);
1293 fwd.topRows(
nmeg) = vec_meg.transpose();
1295 auto fwd0 = fwd.col(0).head(
nmeg);
1296 auto fwd1 = fwd.col(1).head(
nmeg);
1297 auto fwd2 = fwd.col(2).head(
nmeg);
1314 Eigen::MatrixXf vec_eeg(3,
neeg);
1317 fwd.block(d.
nmeg, 0,
neeg, 3) = vec_eeg.transpose();
1319 auto fwd0 = fwd.col(0).segment(d.
nmeg,
neeg);
1320 auto fwd1 = fwd.col(1).segment(d.
nmeg,
neeg);
1321 auto fwd2 = fwd.col(2).segment(d.
nmeg,
neeg);
1334 for (k = 0; k < 3; k++)
1335 if (d.
proj && d.
proj->project_vector(fwd.col(k),
true) ==
FAIL)
1341 if (d.
noise && whiten) {
1342 for (k = 0; k < 3; k++) {
1343 auto col_k = fwd.col(k);
1344 if (d.
noise->whiten_vector(col_k, col_k, nch) ==
FAIL)
#define FIFFV_MNE_SENSOR_COV
#define FIFFV_COIL_CTF_REF_GRAD
#define FIFFV_MNE_NOISE_COV
#define FIFFV_COIL_CTF_REF_MAG
#define FIFFV_COORD_UNKNOWN
FIFF binary tag-stream layer: wraps a QIODevice to read and write FIFF tags, directories,...
Full FIFF measurement metadata: everything from FIFFB_MEAS / FIFFB_MEAS_INFO needed to interpret a re...
4x4 affine FIFF coordinate transform (FIFF_COORD_TRANS) annotated with source/destination coordinate-...
#define FIFFV_BEM_SURF_ID_BRAIN
Eigen::JacobiSVD< Eigen::Matrix3f > svd(S, Eigen::ComputeFullU|Eigen::ComputeFullV)
Software-gradiometer compensation wrapper that subtracts the reference-channel contribution from the ...
Boundary Element Method (BEM) volume-conductor model — layered triangulated surfaces,...
std::function aliases for the generic dipole field / potential / field-gradient callbacks driving the...
#define FIFFV_COIL_CTF_OFFDIAG_REF_GRAD
InvDipoleForward * dipole_forward(InvDipoleFitData *d, float **rd, int ndip, InvDipoleForward *old)
Compute the forward solution for one or more dipoles, applying projections and whitening.
Initial-guess grid for the dipole-fit optimiser, with per-guess forward fields pre-computed.
Dipole-fit workspace bundling sensor geometry, forward-model function pointers, noise covariance and ...
constexpr int COLUMN_NORM_NONE
constexpr int COLUMN_NORM_COMP
constexpr int COLUMN_NORM_LOC
Single equivalent current dipole (ECD) with position, moment and per-fit goodness/χ² metrics.
Best-fit sphere from a 3-D point cloud with closed-form and Nelder–Mead solvers.
Header-only Nelder–Mead simplex minimiser with pluggable cost and report callables.
One condition / averaging slice within a legacy MNELIB::MNEMeasData.
Legacy MNE-C measurement-data container assembling raw/evoked sets and their projection state.
Single SSP projection vector with kind/active flag and channel labels.
Lightweight triangulated surface (vertices, triangles, normals) used by surface-based routines.
Legacy MNE-C noise covariance container preserved for cross-toolchain compatibility.
#define MNE_COV_CH_MEG_MAG
#define MNE_COV_CH_MEG_GRAD
#define MNE_COV_CH_UNKNOWN
Core MNE data structures (source spaces, source estimates, hemispheres).
FIFF file I/O, in-memory data structures and high-level readers/writers.
Inverse source estimation (MNE, dSPM, sLORETA, dipole fitting).
Forward modelling — BEM solver, spherical models, sensor/coil definitions and the lead-field assembly...
constexpr int FWD_COIL_ACCURACY_NORMAL
constexpr int FWD_COIL_ACCURACY_ACCURATE
constexpr int FWD_BEM_UNKNOWN
static QString frame_name(int frame)
static FiffCoordTrans readMeasTransform(const QString &name)
static FiffCoordTrans readMriTransform(const QString &name)
Eigen::MatrixX3f apply_trans(const Eigen::MatrixX3f &rr, bool do_move=true) const
QSharedPointer< FiffDirNode > SPtr
Full FIFF measurement info: per-channel descriptors, sampling and filter setup, projectors,...
static bool readMegEegChannels(const QString &name, bool do_meg, bool do_eeg, const QStringList &bads, QList< FiffChInfo > &chsp, int &nmegp, int &neegp)
static bool readBadChannelsFromFile(const QString &name, QStringList &listOut)
FIFF tag-stream reader/writer: wraps a QIODevice and exposes typed read_* / write_* methods for every...
QSharedPointer< FiffStream > SPtr
static FwdBemModel::UPtr fwd_bem_load_three_layer_surfaces(const QString &name)
Load a three-layer BEM model (scalp, outer skull, inner skull) from a FIFF file.
static int fwd_mag_dipole_field_vec(const Eigen::Vector3f &rm, FwdCoilSet &coils, Eigen::Ref< Eigen::MatrixXf > Bval, void *client)
Callback: compute the vector magnetic field of a magnetic dipole at coils.
static QString fwd_bem_make_bem_sol_name(const QString &name)
Build a standard BEM solution file name from a model name.
static int fwd_mag_dipole_field(const Eigen::Vector3f &rm, const Eigen::Vector3f &M, FwdCoilSet &coils, Eigen::Ref< Eigen::VectorXf > Bval, void *client)
Callback: compute the magnetic field of a magnetic dipole at coils.
static int fwd_sphere_field(const Eigen::Vector3f &rd, const Eigen::Vector3f &Q, FwdCoilSet &coils, Eigen::Ref< Eigen::VectorXf > Bval, void *client)
Callback: compute the spherical-model magnetic field at coils.
static int fwd_bem_field(const Eigen::Vector3f &rd, const Eigen::Vector3f &Q, FwdCoilSet &coils, Eigen::Ref< Eigen::VectorXf > B, void *client)
Callback: compute BEM magnetic fields at coils for a dipole.
static int fwd_bem_pot_els(const Eigen::Vector3f &rd, const Eigen::Vector3f &Q, FwdCoilSet &els, Eigen::Ref< Eigen::VectorXf > pot, void *client)
Callback: compute BEM potentials at electrodes for a dipole.
static FwdBemModel::UPtr fwd_bem_load_homog_surface(const QString &name)
Load a single-layer (homogeneous) BEM model from a FIFF file.
static int fwd_sphere_field_vec(const Eigen::Vector3f &rd, FwdCoilSet &coils, Eigen::Ref< Eigen::MatrixXf > Bval, void *client)
Callback: compute the spherical-model vector magnetic field at coils.
Container of FwdCoil instances acting both as the in-memory image of the coil_def....
static FwdCoilSet::UPtr read_coil_defs(const QString &name)
static FwdCoilSet::UPtr create_eeg_els(const QList< FIFFLIB::FiffChInfo > &chs, int nch, const FIFFLIB::FiffCoordTrans &t=FIFFLIB::FiffCoordTrans())
std::vector< FwdCoil::UPtr > coils
CTF / 4D software-gradiometer wrapper that re-evaluates the primary field callback on a separate refe...
static int fwd_comp_field_vec(const Eigen::Vector3f &rd, FwdCoilSet &coils, Eigen::Ref< Eigen::MatrixXf > res, void *client)
static int fwd_comp_field(const Eigen::Vector3f &rd, const Eigen::Vector3f &Q, FwdCoilSet &coils, Eigen::Ref< Eigen::VectorXf > res, void *client)
static FwdCompData * fwd_make_comp_data(MNELIB::MNECTFCompDataSet *set, FwdCoilSet *coils, FwdCoilSet *comp_coils, fwdFieldFunc field, fwdVecFieldFunc vec_field, fwdFieldGradFunc field_grad, void *client)
Multi-shell concentric-sphere head model holding the Berg-Scherg equivalent-source parameters that ac...
static int fwd_eeg_spherepot_coil_vec(const Eigen::Vector3f &rd, FwdCoilSet &els, Eigen::Ref< Eigen::MatrixXf > Vval_vec, void *client)
static int fwd_eeg_spherepot_coil(const Eigen::Vector3f &rd, const Eigen::Vector3f &Q, FwdCoilSet &els, Eigen::Ref< Eigen::VectorXf > Vval, void *client)
Forward field computation function pointers and client data for MEG and EEG dipole fitting.
fwdVecFieldFunc eeg_vec_pot
fwdVecFieldFunc meg_vec_field
MNELIB::mneUserFreeFunc meg_client_free
Workspace for the dipole fitting objective function, holding forward model, measured field,...
Dipole fit workspace holding sensor geometry, forward model, noise covariance, and projection data.
std::unique_ptr< FWDLIB::FwdEegSphereModel > eeg_model
virtual ~InvDipoleFitData()
std::unique_ptr< FWDLIB::FwdCoilSet > meg_coils
static InvDipoleFitData * setup_dipole_fit_data(const QString &mriname, const QString &measname, const QString &bemname, Eigen::Vector3f *r0, FWDLIB::FwdEegSphereModel *eeg_model, int accurate_coils, const QString &badname, const QString &noisename, float grad_std, float mag_std, float eeg_std, float mag_reg, float grad_reg, float eeg_reg, int diagnoise, const QList< QString > &projnames, int include_meg, int include_eeg)
Master setup: read all inputs and build a ready-to-use fit workspace.
std::unique_ptr< FIFFLIB::FiffCoordTrans > mri_head_t
static int scale_dipole_fit_noise_cov(InvDipoleFitData *f, int nave)
Scale dipole-fit noise covariance for a given number of averages.
std::unique_ptr< MNELIB::MNEProjOp > proj
static bool print_fields(const Eigen::Vector3f &rd, const Eigen::Vector3f &Q, float time, float integ, InvDipoleFitData &fit, const MNELIB::MNEMeasData &data, QTextStream &out)
Write the measured and the predicted field of a dipole, channel by channel.
static InvDipoleForward * dipole_forward_one(InvDipoleFitData *d, const Eigen::Vector3f &rd, InvDipoleForward *old)
Compute the forward solution for a single dipole position.
std::unique_ptr< MNELIB::MNECovMatrix > noise
std::unique_ptr< dipoleFitFuncsRec > bem_funcs
static int scale_noise_cov(InvDipoleFitData *f, int nave)
Scale the noise-covariance matrix for a given number of averages.
std::unique_ptr< dipoleFitFuncsRec > sphere_funcs
static bool fit_one(InvDipoleFitData *fit, InvGuessData *guess, float time, Eigen::Ref< Eigen::VectorXf > B, int verbose, InvEcd &res)
Fit a single dipole to the given data.
static int compute_dipole_field(InvDipoleFitData &d, const Eigen::Vector3f &rd, int whiten, Eigen::Ref< Eigen::MatrixXf > fwd)
Compute the forward field for a dipole at the given location.
static int setup_forward_model(InvDipoleFitData *d, MNELIB::MNECTFCompDataSet *comp_data, FWDLIB::FwdCoilSet *comp_coils)
Set up the sphere-model and (optionally) BEM forward functions.
std::unique_ptr< FWDLIB::FwdBemModel > bem_model
static int select_dipole_fit_noise_cov(InvDipoleFitData *f, MNELIB::MNEMeasData *meas, int nave, const int *sels)
Select and weight the noise-covariance for the active channel set.
std::unique_ptr< FWDLIB::FwdCoilSet > eeg_els
std::unique_ptr< dipoleFitFuncsRec > mag_dipole_funcs
static std::unique_ptr< MNELIB::MNECovMatrix > ad_hoc_noise(FWDLIB::FwdCoilSet *meg, FWDLIB::FwdCoilSet *eeg, float grad_std, float mag_std, float eeg_std)
Create an ad-hoc diagonal noise-covariance matrix.
std::unique_ptr< MNELIB::MNECovMatrix > noise_orig
dipoleFitFuncsRec * funcs
Stores forward field matrices and SVD decomposition for magnetic dipole fitting.
Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > uu
Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > vv
Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > fwd
Single equivalent current dipole with position, orientation, amplitude, and goodness-of-fit.
Precomputed guess point grid with forward fields for initial dipole position candidates.
std::vector< InvDipoleForward::UPtr > guess_fwd
Eigen::Matrix< float, Eigen::Dynamic, 3, Eigen::RowMajor > rr
static bool simplex_minimize(Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &p, Eigen::Matrix< T, Eigen::Dynamic, 1 > &y, T ftol, T stol, CostFunc &&func, int max_eval, int &neval, int report, ReportFunc &&report_func)
static bool fit_sphere_to_points(const Eigen::MatrixXf &rr, float simplex_size, Eigen::VectorXf &r0, float &R)
static std::unique_ptr< MNECovMatrix > read(const QString &name, int kind)
static std::unique_ptr< MNECovMatrix > create(int kind, int ncov, const QStringList &names, const Eigen::VectorXd &cov, const Eigen::VectorXd &cov_diag)
static int lt_packed_index(int j, int k)
Collection of CTF third-order gradient compensation operators.
static std::unique_ptr< MNECTFCompDataSet > read(const QString &name)
Measurement data container for MNE inverse and dipole-fit computations.
QList< FIFFLIB::FiffChInfo > chs
int getValuesAtTime(float time, float integ, int nch, bool use_abs, float *value) const
static bool makeProjection(const QList< QString > &projnames, const QList< FIFFLIB::FiffChInfo > &chs, int nch, std::unique_ptr< MNEProjOp > &result)
Load and combine SSP projection operators from files for the selected channels.
Lightweight triangulated surface (vertices, triangles, normals).