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.NDVar
with 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, or data from which to compute it as
eelbrain.NDVar
.- tstart
float
Start of the TRF in seconds.
- tstop
float
Stop of the TRF in seconds.
- 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
stim
before 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=False
leavesstim
data untouched.- in_place: bool
With
in_place=False
(default) the originalmeg
andstims
are left untouched; usein_place=True
to save memory by using the originalmeg
andstim
.- 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) < tol
condition 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.
Examples
MEG data
y
with dimensions (case, sensor, time) and predictorx
with dimensions (case, time):ncrf(y, x, fwd, cov)
x
can always also have an additional predictor dimension, for example, ifx
represents 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 betweeny
andx
.Multiple distinct predictor variables can be supplied as list; e.g., when modeling simultaneous responses to an attended and an unattended stimulus with
x_attended
andx_unattended
:ncrf(y, [x_attended, x_unattended], fwd, cov)
Multiple data segments can also be specified as list. E.g., if
y1
andy2
are responses to stimulix1
andx2
, 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)