MEEG time-frequency searchlight

This example shows MVPA analyses performed on MEEG data, using a searchlight across the time, frequency and channel dimensions

The input dataset involved a paradigm with electrical median nerve stimulation for durations of 2s at 20Hz.

The code presented here can be adapted for other MEEG analyses, but there are a few potential caveats: * assignment of targets (labels of conditions) is based here on stimulation periods versus pre-stimulation periods. In typical analyses the targets should be based on different trial conditions, for example as set a FieldTrip .trialinfo field. * assignment of chunks (parts of the data that are assumed to be independent) is based on a trial-by-trial basis. For cross-validation, the number of chunks is reduced to two to speed up the analysis. * the time window used for analyses is rather small. This means that in particular for time-freq analysis a lot of data is missing, especially for early and late timepoints in the lower frequency bands. For typical analyses it may be preferred to use a wider time window. * the current examples do not perform baseline corrections or signal normalizations, which may reduce discriminatory power.

Note: running this code requires FieldTrip.

  1. For CoSMoMVPA's copyright information and license terms, #
  2. see the COPYING file distributed with CoSMoMVPA. #


get timelock data in CoSMoMVPA format

% set configuration

% show dataset information

% reset citation list

% load data

% convert to cosmomvpa struct

% set the target (trial condition),1); % 1=pre, 2=post

% set the chunks (independent measurements)
% in this dataset, the first half of the samples (in order)
% are the post-trials;
% the second half the pre-trials[(1:145) (1:145)]';

% in addition give a label to each trial
index2label={'pre','post'}; % 1=pre, 2=peri/post,num2cell(;

% just to check everything is ok

% get rid of features with at least one NaN value across samples
fprintf('%d / %d features have NaN\n', ...
            sum(fa_nan_mask), numel(fa_nan_mask));
ds_tf=cosmo_slice(ds_tf, ~fa_nan_mask, 2);
MEG data in raw, time-locked and time-frequency formats.

- subj102_B01_20Hz.fif            raw MEG recording
- preproc.m                       Matlab preprocessing script (based on 
- subj102_B01_20Hz_timefreq.mat   Time-locked data (generated by preproc.m)
- subj102_B01_20Hz_timelock.mat   Time-frequency data  (generated by preproc.m)
- LICENSE                         License file 
- README                          This file

The dataset involved a paradigm with electrical median nerve stimulation with a 
human participant for durations of 2s at 20Hz. Data was acquired at 1khz using 
a neuromag306 system. 
Trial info in the .mat files: 1=pre-stimulus, 2=peri/post-stimulus

The contents are made available by Nathan Weisz <nathanweisz |at|> and 
Gianpaolo Demarchi <gianpaolo.demarchi |at|> under the Creative 
Commons CC0 1.0 Universal Public Domain Dedication ("CC0"). See the LICENSE 
file for details, or visit

Thanks to Nathan Weisz and Gianpaolo Demarchi for providing this dataset, and 
an anonymous participant for volunteering during the recordings.

Nikolaas N. Oosterhof <nikolaas.oosterhof |at|>
16968 / 92718 features have NaN

set MVPA parameters

fprintf('The input has feature dimensions %s\n', ...
                cosmo_strjoin(ds_tf.a.fdim.labels,', '));

% set chunks
% again for speed just two chunks
% (targets were already set above)
nchunks=2;, nchunks);

% define neighborhood parameters for each dimension

% channel neighborhood uses meg_combined_from_planar, which means that the
% input are planar channels but the output has combined-planar channels.
% to use the magnetometers, use 'meg_axial'
chan_count=10;        % use 10 channel locations (relative to the combined
                      % planar channels)
                      % as we use meg_combined_from_planar there are
                      % 20 channels in each searchlight because
                      % gradiometers are paired
time_radius=2; % 2*2+1=5 time bines
freq_radius=4; % 4*2+1=9 freq bins

% define the neighborhood for each dimensions
chan_nbrhood=cosmo_meeg_chan_neighborhood(ds_tf, 'count', chan_count, ...
                                                'chantype', chan_type);

% cross neighborhoods for chan-time-freq searchlight

% print some info
fprintf('Features have on average %.1f +/- %.1f neighbors\n', ...
            mean(nbrhood_nfeatures), std(nbrhood_nfeatures));

% only keep features with at least 10 neighbors
% (some have zero neighbors - in particular, those with low frequencies
% early or late in time)

% for illustration purposes use the split-half measure because it is
% relatively fast - but clasifiers can also be used

% split-half, as there are just two chunks
% (when using a classifier, do not use 'half' but the number of chunks to
% leave out for testing, e.g. 1).
The input has feature dimensions chan, freq, time
+00:00:05 [####################] -00:00:00  crossing neighborhoods
Features have on average 625.9 +/- 244.2 neighbors

run searchlight

+00:01:35 [####################] -00:00:00  

visualize results

% deduce layout from output
fprintf('The output uses layout %s\n',;

% map to FT struct for visualization

% show figure
cfg = [];
if cosmo_wtf('is_octave')
    % GNU Octave does not show data when labels are shown
    % Matlab supports interactive viewing and labels
    cfg.interactive = 'yes';
    cfg.showlabels = 'yes';
cfg.layout       = layout;
ft_multiplotTFR(cfg, sl_tf_ft);

% Show citation information
The output uses layout neuromag306cmb.lay
the call to "ft_prepare_layout" took 0 seconds and required the additional allocation of an estimated 3 MB
the call to "ft_multiplotTFR" took 15 seconds and required the additional allocation of an estimated 93 MB
If you use CoSMoMVPA and/or some other toolboxes for a publication, please cite:

R. Oostenveld, P. Fries, E. Maris, J.-M. Schoffelen (2011). FieldTrip: Open Source Software for Advanced Analysis of MEG, EEG, and Invasive Electrophysiological Data, Computational Intelligence and Neuroscience, vol. 2011, Article ID 156869, 9 pages.doi:10.1155/2011/156869. FieldTrip toolbox available online from

N. N. Oosterhof, A. C. Connolly, J. V. Haxby (2016). CoSMoMVPA: multi-modal multivariate pattern analysis of neuroimaging data in Matlab / GNU Octave. Frontiers in Neuroinformatics, doi:10.3389/fninf.2016.00027.. CoSMoMVPA toolbox available online from

The Mathworks, Natick, MA, United States. Matlab (R2016b) (September 7, 2016). available online from