ncrf.fit_ncrf¶
- ncrf.fit_ncrf(meg, stim, lead_field, noise, tstart=0, tstop=0.5, nlevels=1, n_iter=10, n_iterc=10, n_iterf=100, normalize=False, in_place=False, mu='auto', tol=0.001, verbose=False, n_splits=3, n_workers=None, use_ES=False, gaussian_fwhm=20.0, do_post_normalization=True)¶
One shot function for cortical TRF localization.
Estimate both TRFs and source variance from the observed MEG data by solving the Bayesian optimization problem mentioned in [Das et al., 2020].
- Parameters:
- meg
NDVar([case,]sensor,time) |list[NDVar] If multiple trials are the same length they can be specified as
eelbrain.NDVarwith case dimension, if they are different length they can be supplied as list.- stim
eelbrain.NDVar([case, dim,]time) |list[NDVar] One or multiple predictors corresponding to each item in
meg.- lead_field
NDVar forward solution a.k.a. lead field matrix.
- noise
mne.Covariance|NDVar|np.ndarray The empty room noise covariance as
mne.Covariance, or data from which to compute it aseelbrain.NDVarornumpy.ndarray. If supplied as a covariance matrix, the sensors are cross-checked to ensure all the sensors inmegare also present in covaraince matrix, else the check is skipped. Raises error if any sensor is missing in noise covaraince, or in case of shape mismatch.- tstart
float|list[float] Start of the TRF in seconds. Can define multiple tstarts for more than 1 predictor.
- tstop
float|list[float] Stop of the TRF in seconds. Can define multiple tstops for more than 1 predictor.
- nlevels
int Decides the density of Gabor atoms. Bigger nlevel -> less dense basis. By default it is set to 1. nlevesl > 2 should be used with caution.
- n_iter
int Number of out iterations of the algorithm, by default set to 10.
- n_iterc
int Number of Champagne iterations within each outer iteration, by default set to 10.
- n_iterf
int Number of FASTA iterations within each outer iteration, by default set to 100.
- normalize
bool| ‘l2’ | ‘l1’ Scale
stimbefore model fitting: subtract the mean and divide by the standard deviation (whennomrmalize='l2'ornormalize=True) or the mean absolute value (whennormalize='l1'). By default,normalize=Falseleavesstimdata untouched.- in_place: bool
With
in_place=False(default) the originalmegandstimsare left untouched; usein_place=Trueto save memory by using the originalmegandstim.- mu‘auto’ |
float| sequence[float] Choice of regularizer parameters. Specify a single value to fit a model corresponding to that value. Alternatively, specify a range over which cross-validation will be done. By default (
mu='auto') a range of values for cross-validation is chosen based on the data.- tol
float Tolerance factor deciding stopping criterion for the overall algorithm. The iterations are stooped when
norm(trf_new - trf_old)/norm(trf_old) < tolcondition is met. By defaulttol=1e-3.- verbose
bool If True prints intermediate results, by default False.
- n_splits
int Number of cross-validation folds. By default it uses 3-fold cross-validation.
- n_workers
int(optional) Number of workers to spawn for cross-validation. If None, it will use
cpu_count/2.- use_ES
bool(optional) Use estimation stability criterion [Lim and Yu, 2016] to choose the best
mu. (False, by default)- gaussian_fwhm
float(optional) Specifies the full width half maximum (fwmh) for the Gaussian kernel (used as elements of the time basis), the default is 20 ms. The standard deviation (std) is related to the fwmh as following: \(std = fwhm / (2 * (sqrt(2 * log(2))))\).
- do_post_normalization
bool(optional) Scales covariate matrices of different predictor variables by spectral norms to equalize their spectral spread (=1). (True, by default)
- meg
- Returns:
- trf
NCRF The result of the model fit.
- trf
References
[DBSB20]Proloy Das, Christian Brodbeck, Jonathan Z Simon, and Behtash Babadi. Neuro-current response functions: A unified approach to MEG source analysis under the continuous stimuli paradigm. NeuroImage, pages 116528, 2020. Publisher: Elsevier. doi:10.1016/j.neuroimage.2020.116528.
[LY16]Chinghway Lim and Bin Yu. Estimation Stability With Cross-Validation (ESCV). Journal of Computational and Graphical Statistics, 25(2):464–492, April 2016. URL: https://www.tandfonline.com/doi/full/10.1080/10618600.2015.1020159 (visited on 2025-05-13), doi:10.1080/10618600.2015.1020159.
[TBM+11]François Tadel, Sylvain Baillet, John C. Mosher, Dimitrios Pantazis, and Richard M. Leahy. Brainstorm: a user-friendly application for meg/eeg analysis. Computational Intelligence and Neuroscience, 2011(1):879716, 2011. URL: https://onlinelibrary.wiley.com/doi/abs/10.1155/2011/879716, arXiv:https://onlinelibrary.wiley.com/doi/pdf/10.1155/2011/879716, doi:https://doi.org/10.1155/2011/879716.
Examples
MEG data
ywith dimensions (case, sensor, time) and predictorxwith dimensions (case, time):ncrf(y, x, fwd, cov)
xcan always also have an additional predictor dimension, for example, ifxrepresents a spectrogram: (case, frequency, time). The case dimension is optional, i.e. a single contiguous data segment is also accepted, but the case dimension should always match betweenyandx.Multiple distinct predictor variables can be supplied as list; e.g., when modeling simultaneous responses to an attended and an unattended stimulus with
x_attendedandx_unattended:ncrf(y, [x_attended, x_unattended], fwd, cov)
Multiple data segments can also be specified as list. E.g., if
y1andy2are responses to stimulix1andx2, respoectively:ncrf([y1, y2], [x1, x2], fwd, cov)
And with multiple predictors:
ncrf([y1, y2], [[x1_attended, x1_unattended], [x2_attended, x2_unattended]], fwd, cov)