def get_window(master: EpochsArray, config, start): fps = config['fps'] end = start + config['window_duration'] - 1. / fps window = master.copy().crop(start, end) return window
def test_decim(): """Test evoked decimation.""" rng = np.random.RandomState(0) n_epochs, n_channels, n_times = 5, 10, 20 dec_1, dec_2 = 2, 3 decim = dec_1 * dec_2 sfreq = 1000. sfreq_new = sfreq / decim data = rng.randn(n_epochs, n_channels, n_times) events = np.array([np.arange(n_epochs), [0] * n_epochs, [1] * n_epochs]).T info = create_info(n_channels, sfreq, 'eeg') info['lowpass'] = sfreq_new / float(decim) epochs = EpochsArray(data, info, events) data_epochs = epochs.copy().decimate(decim).get_data() data_epochs_2 = epochs.copy().decimate(decim, offset=1).get_data() data_epochs_3 = epochs.decimate(dec_1).decimate(dec_2).get_data() assert_array_equal(data_epochs, data[:, :, ::decim]) assert_array_equal(data_epochs_2, data[:, :, 1::decim]) assert_array_equal(data_epochs, data_epochs_3) # Now let's do it with some real data raw = read_raw_fif(raw_fname, add_eeg_ref=False) events = read_events(event_name) sfreq_new = raw.info['sfreq'] / decim raw.info['lowpass'] = sfreq_new / 4. # suppress aliasing warnings picks = pick_types(raw.info, meg=True, eeg=True, exclude=()) epochs = Epochs(raw, events, 1, -0.2, 0.5, picks=picks, preload=True, add_eeg_ref=False) for offset in (0, 1): ev_ep_decim = epochs.copy().decimate(decim, offset).average() ev_decim = epochs.average().decimate(decim, offset) expected_times = epochs.times[offset::decim] assert_allclose(ev_decim.times, expected_times) assert_allclose(ev_ep_decim.times, expected_times) expected_data = epochs.get_data()[:, :, offset::decim].mean(axis=0) assert_allclose(ev_decim.data, expected_data) assert_allclose(ev_ep_decim.data, expected_data) assert_equal(ev_decim.info['sfreq'], sfreq_new) assert_array_equal(ev_decim.times, expected_times)
def _compute_fold(metric_fx, targets, train, test, epoch, cv_normalize_noise=None, mean_groups=False, time_diag_only=False): """ Computes pairwise metric across time for one fold Arguments --------- metric_fx : either an allowed string (e.g., 'correlation') or an object with attributes 'fit' and 'score' (similar to scikit-learn estimators) targets : array (n_trials,) target (condition) for each trials; they must be integers train : array-like of int indices of the training data test : array-like of int indices of the testing data epoch : instance of mne.Epoch cv_normalize_noise : str | None (default None) Multivariately normalize the trials before applying the metric (distance or classification). Normalization is performed with cross-validation, that is the covariance matrix is estimated in the training set, and then applied to the test set. Normalization is always performed on single trials, thus before averaging within each class if `mean_groups` is set to True. Valid values for `cv_normalize_noise` are 'epoch' | 'baseline' | None: - 'epoch' computes the covariance matrix on the entire epoch; - 'baseline' uses only the baseline condition; it requires `epoch` to have a valid baseline (i.e., to have performed baseline correction) mean_groups : bool (default False) Whether the trials belonging to each target should be averaged prior to running the metric. This is useful if the metric is a distance metric. Should be set to False for classification. time_diag_only : bool (default False) Whether to run only for the diagonal in time, e.g. for train_time == test_time Returns ------- rdms: array (n_pairwise_targets, n_pairwise_times) the cross-validated RDM over time targets_pairs : list of len n_pairwise_targets the labels corresponding to each element in rdms """ targets = np.asarray(targets) if targets.dtype != np.dtype('int64'): raise ValueError("targets must be integers, " "not {0}".format(targets.dtype)) # impose conditions in epoch as targets, so that covariance # matrix is computed within each target events_ = epoch.events.copy() events_[:, 2] = targets epoch_ = EpochsArray(epoch.get_data(), info=epoch.info, events=events_, tmin=epoch.times[0]) epoch_.baseline = epoch.baseline # get training and testing data epoch_train = epoch_.copy()[train] targets_train = targets[train] assert (len(epoch_train) == len(targets_train)) epoch_test = epoch_.copy()[test] targets_test = targets[test] assert (len(epoch_test) == len(targets_test)) # perform multi variate noise normalization epoch_train, epoch_test = _multiv_normalize(epoch_train, epoch_test, cv_normalize_noise) if mean_groups: # average within train and test for each target epoch_train, targets_train = mean_group(epoch_train, targets_train) epoch_test, targets_test = mean_group(epoch_test, targets_test) # the targets should be the same in both training and testing # set or else we are correlating weird things together assert_array_equal(targets_train, targets_test) rdms = _run_metric(metric_fx, epoch_train, targets_train, epoch_test, targets_test, time_diag_only=time_diag_only) # return also the pairs labels. since we are taking triu, we loop first # across rows unique_targets = _get_unique_targets(targets_train, targets_test) targets_pairs = [] for tr_lbl, te_lbl in _get_combinations_triu(unique_targets): targets_pairs.append('+'.join(map(str, [tr_lbl, te_lbl]))) return rdms, targets_pairs