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
96,
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.");
135 qInfo(
"Homogeneous model surface loaded.");
138 qCritical(
"Cannot use a homogeneous model in EEG calculations.");
141 qInfo(
"\nLoading the solution matrix...");
144 qInfo(
"Employing the head->MRI coordinate transform with the BEM model.");
148 qInfo(
"BEM model %s is now set up",d->
bem_model->sol_name.toUtf8().constData());
152 if (fit_sphere_to_bem) {
154 float simplex_size = 2e-2;
163 d->
r0 = r0_vec.head<3>();
166 qInfo(
"Fitted sphere model origin : %6.1f %6.1f %6.1f mm rad = %6.1f mm.",
167 1000*d->
r0[0],1000*d->
r0[1],1000*d->
r0[2],1000*
R);
169 d->
bem_funcs = std::make_unique<dipoleFitFuncsRec>();
180 qInfo(
"Compensation setup done.");
182 qInfo(
"MEG solution matrix...");
195 qInfo(
"\tEEG solution matrix...");
205 qCritical(
"EEG sphere model not defined.");
208 d->
sphere_funcs = std::make_unique<dipoleFitFuncsRec>();
233 qInfo(
"Sphere model origin : %6.1f %6.1f %6.1f mm.",
234 1000*d->
r0[0],1000*d->
r0[1],1000*d->
r0[2]);
276 Eigen::VectorXd stds;
280 qInfo(
"Using standard noise values "
281 "(MEG grad : %6.1f fT/cm MEG mag : %6.1f fT EEG : %6.1f uV)\n",
282 1e13*grad_std,1e15*mag_std,1e6*eeg_std);
286 nchan = nchan + meg->
ncoil();
288 nchan = nchan + eeg->
ncoil();
294 for (k = 0; k < meg->
ncoil(); k++, n++) {
295 if (meg->
coils[k]->is_axial_coil()) {
296 stds[n] =
static_cast<double>(mag_std)*mag_std;
301 stds[n] = 1e6*stds[n];
305 stds[n] =
static_cast<double>(grad_std)*grad_std;
310 for (k = 0; k < eeg->
ncoil(); k++, n++) {
311 stds[n] =
static_cast<double>(eeg_std)*eeg_std;
323 float nave_ratio =
static_cast<float>(f->
nave) /
static_cast<float>(
nave);
329 if (f->
noise->cov.size() > 0) {
330 qInfo(
"Decomposing the sensor noise covariance matrix...");
334 for (k = 0; k < f->
noise->ncov*(f->
noise->ncov+1)/2; k++)
335 f->
noise->cov[k] = nave_ratio*f->
noise->cov[k];
336 for (k = 0; k < f->
noise->ncov; k++) {
337 f->
noise->lambda[k] = nave_ratio*f->
noise->lambda[k];
338 if (f->
noise->lambda[k] < 0.0)
339 f->
noise->lambda[k] = 0.0;
345 for (k = 0; k < f->
noise->ncov; k++)
346 f->
noise->cov_diag[k] = nave_ratio*f->
noise->cov_diag[k];
347 qInfo(
"Decomposition not needed for a diagonal noise covariance matrix.");
351 qInfo(
"Effective nave is now %d",
nave);
360 float nave_ratio =
static_cast<float>(f->
nave) /
static_cast<float>(
nave);
368 if (f->
noise->cov.size() > 0) {
372 qInfo(
"Decomposing the noise covariance...");
373 if (f->
noise->cov.size() > 0) {
376 for (k = 0; k < f->
noise->ncov; k++) {
377 if (f->
noise->lambda[k] < 0.0)
378 f->
noise->lambda[k] = 0.0;
381 for (k = 0; k < f->
noise->ncov*(f->
noise->ncov+1)/2; k++)
382 f->
noise->cov[k] = nave_ratio*f->
noise->cov[k];
383 for (k = 0; k < f->
noise->ncov; k++) {
384 f->
noise->lambda[k] = nave_ratio*f->
noise->lambda[k];
385 if (f->
noise->lambda[k] < 0.0)
386 f->
noise->lambda[k] = 0.0;
392 for (k = 0; k < f->
noise->ncov; k++)
393 f->
noise->cov_diag[k] = nave_ratio*f->
noise->cov_diag[k];
394 qInfo(
"Decomposition not needed for a diagonal noise covariance matrix.");
398 qInfo(
"Effective nave is now %d",
nave);
438 float *w = wVec.data();
439 int nomit_meg, nomit_eeg,
nmeg,
neeg;
442 nomit_meg = nomit_eeg = 0;
449 bool selected =
false;
450 for (
int c = 0; c < meas->
nchan; c++) {
452 meas->
chs[c].ch_name,
453 Qt::CaseInsensitive) == 0) {
454 selected = sels[c] != 0;
469 if (
nmeg > 0 &&
nmeg - nomit_meg > 0 &&
nmeg - nomit_meg < min_nchan) {
470 qCritical(
"Too few MEG channels remaining");
473 if (
neeg > 0 &&
neeg - nomit_eeg > 0 &&
neeg - nomit_eeg < min_nchan) {
474 qCritical(
"Too few EEG channels remaining");
478 if (nomit_meg + nomit_eeg > 0) {
479 if (f->
noise->cov.size() > 0) {
480 for (j = 0; j < f->
noise->ncov; j++)
481 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];
507 const QString &measname,
512 const QString &badname,
513 const QString &noisename,
521 const QList<QString> &projnames,
525 auto res = std::make_unique<InvDipoleFitData>();
529 QStringList file_bads;
532 std::unique_ptr<MNECovMatrix> cov;
533 std::unique_ptr<FwdCoilSet> templates;
534 std::unique_ptr<MNECTFCompDataSet> comp_data;
535 std::unique_ptr<FwdCoilSet> comp_coils;
540 if (!mriname.isEmpty()) {
542 if (res->mri_head_t->isEmpty())
546 qWarning(
"Source of MRI / head transform required for the BEM model is missing");
550 float move[] = { 0.0, 0.0, 0.0 };
551 float rot[3][3] = { { 1.0, 0.0, 0.0 },
554 Eigen::Matrix3f rotMat;
555 rotMat << rot[0][0], rot[0][1], rot[0][2],
556 rot[1][0], rot[1][1], rot[1][2],
557 rot[2][0], rot[2][1], rot[2][2];
558 Eigen::Vector3f
moveVec = Eigen::Map<Eigen::Vector3f>(move);
562 res->mri_head_t->print();
564 if (res->meg_head_t->isEmpty())
566 res->meg_head_t->print();
570 if (!badname.isEmpty()) {
573 nbad = badlist.size();
574 qInfo(
"%d bad channels read from %s.", nbad, badname.toUtf8().data());
577 QFile measFile(measname);
579 if (measStream->open()) {
580 file_bads = measStream->read_bad_channels(measStream->dirtree());
581 file_nbad = file_bads.size();
586 if (badlist.isEmpty())
588 for (k = 0; k < file_nbad; k++) {
589 badlist.append(file_bads[k]);
593 qInfo(
"%d bad channels read from the data file.",file_nbad);
595 qInfo(
"%d bad channels total.",nbad);
609 qInfo(
"Will use %3d MEG channels from %s",res->nmeg,measname.toUtf8().data());
611 qInfo(
"Will use %3d EEG channels from %s",res->neeg,measname.toUtf8().data());
613 int nch_total = res->nmeg + res->neeg;
614 res->ch_names.clear();
615 for (
int i = 0; i < nch_total; i++)
616 res->ch_names.append(res->chs[i].ch_name);
632 QString qPath = QString(QCoreApplication::applicationDirPath() +
"/../resources/general/coilDefinitions/coil_def.dat");
634 if ( !QCoreApplication::startingUp() )
635 qPath = QCoreApplication::applicationDirPath() + QString(
"/../resources/general/coilDefinitions/coil_def.dat");
636 else if (!file.exists())
637 qPath =
"../resources/general/coilDefinitions/coil_def.dat";
639 QByteArray coilfileBytes = qPath.toUtf8();
640 const char *coilfile = coilfileBytes.constData();
650 Q_ASSERT(res->meg_head_t);
651 res->meg_coils = templates->create_meg_coils(res->chs,
661 qInfo(
"Head coordinate coil definitions created.");
681 if (comp_data->ncomp > 0) {
682 QList<FiffChInfo> comp_chs;
685 qInfo(
"%d compensation data sets in %s",comp_data->ncomp,measname.toUtf8().data());
687 QFile compFile(measname);
689 if (!compStream->open())
693 if (!compStream->read_meas_info(compStream->dirtree(), compInfo, compInfoNode)) {
698 for (
int k = 0; k < compInfo.
chs.size(); k++) {
700 comp_chs.append(compInfo.
chs[k]);
706 comp_coils = templates->create_meg_coils(comp_chs,
713 qInfo(
"%d compensation channels in %s",comp_coils->ncoil(),measname.toUtf8().data());
733 if (res->proj && res->proj->nitems > 0) {
734 qInfo(
"Final projection operator is:");
735 { QTextStream errStream(stderr); res->proj->report(errStream, QStringLiteral(
"\t")); }
737 if (res->proj->assign_channels(res->ch_names,res->nmeg+res->neeg) ==
FAIL)
739 if (res->proj->make_proj() ==
FAIL)
743 qInfo(
"No projection will be applied to the data.");
748 if (!noisename.isEmpty()) {
751 qInfo(
"Read a %s noise-covariance matrix from %s",
752 cov->cov_diag.size() > 0 ?
"diagonal" :
"full", noisename.toUtf8().data());
755 if ((cov =
ad_hoc_noise(res->meg_coils.get(),res->eeg_els.get(),grad_std,mag_std,eeg_std)) ==
nullptr)
758 res->noise = cov->pick_chs_omit(res->ch_names,
766 qInfo(
"Picked appropriate channels from the noise-covariance matrix.");
772 if (res->proj && res->proj->nitems > 0 && res->proj->nvec > 0) {
773 if (res->proj->apply_cov(res->noise.get()) ==
FAIL)
775 qInfo(
"Projection applied to the covariance matrix.");
782 res->noise->revert_to_diag();
783 qInfo(
"Using only the main diagonal of the noise-covariance matrix.");
789 if (res->noise->cov.size() > 0) {
790 Eigen::Vector3f regs;
799 if (res->noise->classify_channels(res->chs,
800 res->nmeg+res->neeg) ==
FAIL)
805 for (k = 0, do_it = 0; k < res->noise->ncov; k++) {
807 regs[res->noise->ch_class[k]] > 0.0)
814 res->noise->regularize(regs);
816 qInfo(
"No regularization applied to the noise-covariance matrix");
822 qInfo(
"Decomposing the noise covariance...");
823 if (res->noise->cov.size() > 0) {
824 if (res->noise->decompose_eigen() ==
FAIL)
826 qInfo(
"Eigenvalue decomposition done.");
827 for (k = 0; k < res->noise->ncov; k++) {
828 if (res->noise->lambda[k] < 0.0)
829 res->noise->lambda[k] = 0.0;
833 qInfo(
"Decomposition not needed for a diagonal covariance matrix.");
834 if (res->noise->add_inv() ==
FAIL)
839 return res.release();
847 const Eigen::Vector3f& Q,
854 Eigen::VectorXf oneVec(data->
nchan);
859 qWarning(
"Cannot pick time: %7.1f ms",1000*time);
862 for (k = 0; k < data->
nchan; k++)
865 qInfo(
"%g ",1e15*oneVec[k]);
869 Eigen::MatrixXf fwd = Eigen::MatrixXf::Zero(nch, 3);
873 for (k = 0; k < data->
nchan; k++)
876 qInfo(
"%g ",1e15*(Q[0]*fwd(k,0)+Q[1]*fwd(k,1)+Q[2]*fwd(k,2)));
903 delete old; old =
nullptr;
907 res->
fwd.resize(m, nch);
908 res->
uu.resize(m, nch);
909 res->
vv.resize(m, m);
912 res->
rd.resize(ndip, 3);
917 for (k = 0; k < ndip; k++) {
918 res->
rd.row(k) = Eigen::Map<const Eigen::Vector3f>(rd[k]).transpose();
922 Eigen::MatrixXf this_fwd(d->
nmeg + d->
neeg, 3);
923 Eigen::Map<const Eigen::Vector3f> rd_k(rd[k]);
929 for (
int p = 0; p < 3; p++)
930 res->
fwd.row(3*k+p) = this_fwd.col(p).transpose();
936 for (p = 0; p < 3; p++)
937 S[p] = res->
fwd.row(3*k+p).squaredNorm();
939 for (p = 0; p < 3; p++)
940 res->
scales[3*k+p] = sqrt(
S[p]);
948 for (p = 0; p < 3; p++) {
949 if (res->
scales[3*k+p] > 0.0) {
951 res->
fwd.row(3*k+p) *= res->
scales[3*k+p];
972 int udim = std::min(m,n);
973 JacobiSVD<MatrixXf>
svd(res->
fwd, ComputeFullU | ComputeFullV);
974 res->
sing =
svd.singularValues();
975 res->
uu =
svd.matrixV().transpose().topRows(udim);
976 res->
vv =
svd.matrixU().transpose().topRows(udim);
988 const Eigen::Vector3f& rd,
992 rds[0] =
const_cast<float*
>(rd.data());
1001static float fit_eval(
const VectorXf& rd,
const void *user)
1012 qInfo(
"ncomp = %d",ncomp);
1014 Eigen::Map<const VectorXf> Bmap(fuser->
B, fwd->
nch);
1015 for (c = 0, Bm2 = 0.0; c < ncomp; c++) {
1016 one = fwd->
uu.row(c).dot(Bmap);
1017 Bm2 = Bm2 + one*one;
1019 return fuser->
B2-Bm2;
1025static int find_best_guess(
const Eigen::Ref<const Eigen::VectorXf>& B,
1033 double B2,Bm2,this_good,one;
1039 B2 = B.squaredNorm();
1040 for (k = 0; k < guess->
nguess; k++) {
1042 if (fwd->
nch == nch) {
1043 ncomp = fwd->
sing[2]/fwd->
sing[0] > limit ? 3 : 2;
1044 for (c = 0, Bm2 = 0.0; c < ncomp; c++) {
1045 one = fwd->
uu.row(c).dot(B);
1046 Bm2 = Bm2 + one*one;
1048 this_good = 1.0 - (B2 - Bm2)/B2;
1049 if (this_good > good) {
1056 qWarning(
"No reasonable initial guess found.");
1067static MatrixXf make_initial_dipole_simplex(
const Eigen::Vector3f& r0,
1076 float x = sqrt(3.0f)/3.0f;
1077 float r = sqrt(6.0f)/12.0f;
1080 float rr[][3] = { { x , 0.0f, -r },
1083 { 0.0f, 0.0f,
R } };
1085 MatrixXf simplex = MatrixXf::Zero(4, 3);
1087 for (
int j = 0; j < 4; j++) {
1088 simplex.row(j) = Eigen::Map<const Vector3f>(rr[j]).transpose() * size + r0.transpose();
1093static bool dipole_report_func(
int loop,
1094 const VectorXf& fitpar,
1099 qInfo(
"loop %d rd %7.2f %7.2f %7.2f fval %g %g par diff %g",
1100 loop,1000*fitpar[0],1000*fitpar[1],1000*fitpar[2],fval_lo,fval_hi,1000*par_diff);
1109 const Eigen::Ref<const Eigen::VectorXf>& B,
1110 const Eigen::Vector3f& rd,
1123 ncomp = fwd->
sing[2]/fwd->
sing[0] > limit ? 3 : 2;
1126 for (c = 0, Bm2 = 0.0; c < ncomp; c++) {
1127 one = fwd->
uu.row(c).dot(B);
1128 Q += (one/fwd->
sing[c]) * fwd->
vv.row(c).head(3).transpose();
1129 Bm2 = Bm2 + one*one;
1134 for (c = 0; c < 3; c++)
1135 Q[c] = fwd->
scales[c]*Q[c];
1136 res = B.squaredNorm() - Bm2;
1159 Eigen::Ref<Eigen::VectorXf> B,
1167 float ftol[] = { 1e-2f, 1e-2f };
1168 float atol[] = { 0.2e-3f, 0.2e-3f };
1171 int max_eval = 1000;
1172 int report_interval = verbose ? 1 : -1;
1175 float good,final_val;
1176 Eigen::Vector3f rd_final, Q;
1178 int k,neval,neval_tot,nchan,ncomp;
1185 if (fit->
proj && fit->
proj->project_vector(B,
true) ==
FAIL)
1188 if (fit->
noise->whiten_vector(B,B,nchan) ==
FAIL)
1193 if (find_best_guess(B,nchan,guess,limit,best,good) < 0)
1198 user.B2 = B.squaredNorm();
1200 user.report_dim =
false;
1203 rd_guess = guess->
rr.row(best).transpose();
1204 rd_final = rd_guess;
1208 for (k = 0; k < ntol; k++) {
1217 MatrixXf simplexMat = make_initial_dipole_simplex(rd_guess,size);
1218 for (
int p = 0; p < 4; p++)
1219 vals[p] = fit_eval(simplexMat.row(p),fit);
1222 auto cost = [fit](
const VectorXf& x) ->
float {
return fit_eval(x, fit); };
1233 dipole_report_func)) {
1239 float rv = 2.0f*(vals.maxCoeff()-vals.minCoeff())/(vals.maxCoeff()+vals.minCoeff());
1240 qWarning(
"Warning (t = %8.1f ms) : g = %6.1f %% final val = %7.3f rtol = %f",
1241 1000*time,100*(1 - vals[0]/
user.B2),vals[0],rv);
1245 rd_final = simplexMat.row(0).transpose();
1246 rd_guess = simplexMat.row(0).transpose();
1249 final_val = vals[0];
1257 if (fit_Q(fit,B,rd_final,
user.limit,Q,ncomp,final_val) ==
OK) {
1262 res.
good = 1.0 - final_val/
user.B2;
1265 res.
khi2 = final_val;
1267 res.
nfree = nchan-3-ncomp-fit->
proj->nvec;
1269 res.
nfree = nchan-3-ncomp;
1270 res.
neval = neval_tot;
1290 static const Eigen::Vector3f Qx(1.0f, 0.0f, 0.0f);
1291 static const Eigen::Vector3f Qy(0.0f, 1.0f, 0.0f);
1292 static const Eigen::Vector3f Qz(0.0f, 0.0f, 1.0f);
1305 Eigen::MatrixXf vec_meg(3,
nmeg);
1308 fwd.topRows(
nmeg) = vec_meg.transpose();
1310 auto fwd0 = fwd.col(0).head(
nmeg);
1311 auto fwd1 = fwd.col(1).head(
nmeg);
1312 auto fwd2 = fwd.col(2).head(
nmeg);
1329 Eigen::MatrixXf vec_eeg(3,
neeg);
1332 fwd.block(d.
nmeg, 0,
neeg, 3) = vec_eeg.transpose();
1334 auto fwd0 = fwd.col(0).segment(d.
nmeg,
neeg);
1335 auto fwd1 = fwd.col(1).segment(d.
nmeg,
neeg);
1336 auto fwd2 = fwd.col(2).segment(d.
nmeg,
neeg);
1349 for (k = 0; k < 3; k++)
1350 if (d.
proj && d.
proj->project_vector(fwd.col(k),
true) ==
FAIL)
1356 if (d.
noise && whiten) {
1357 for (k = 0; k < 3; k++) {
1358 auto col_k = fwd.col(k);
1359 if (d.
noise->whiten_vector(col_k,col_k,nch) ==
FAIL)
Software-gradiometer compensation wrapper that subtracts the reference-channel contribution from the ...
std::function aliases for the generic dipole field / potential / field-gradient callbacks driving the...
Boundary Element Method (BEM) volume-conductor model — layered triangulated surfaces,...
One condition / averaging slice within a legacy MNELIB::MNEMeasData.
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
Lightweight triangulated surface (vertices, triangles, normals) used by surface-based routines.
Single SSP projection vector with kind/active flag and channel labels.
Legacy MNE-C measurement-data container assembling raw/evoked sets and their projection state.
Eigen::JacobiSVD< Eigen::Matrix3f > svd(S, Eigen::ComputeFullU|Eigen::ComputeFullV)
#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
Full FIFF measurement metadata: everything from FIFFB_MEAS / FIFFB_MEAS_INFO needed to interpret a re...
#define FIFFV_BEM_SURF_ID_BRAIN
4x4 affine FIFF coordinate transform (FIFF_COORD_TRANS) annotated with source/destination coordinate-...
FIFF binary tag-stream layer: wraps a QIODevice to read and write FIFF tags, directories,...
Single equivalent current dipole (ECD) with position, moment and per-fit goodness/χ² metrics.
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
#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.
void print_fields(const Eigen::Vector3f &rd, const Eigen::Vector3f &Q, float time, float integ, InvDipoleFitData *fit, MNEMeasData *data)
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.
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 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).