def assert_indexing(info, picks_by_type, ref_meg=False, all_data=True): """Assert our indexing functions work properly.""" # First that our old and new channel typing functions are equivalent _assert_channel_types(info) # Next that channel_indices_by_type works if not ref_meg: idx = channel_indices_by_type(info) for key in idx: for p in picks_by_type: if key == p[0]: assert_array_equal(idx[key], p[1]) break else: assert len(idx[key]) == 0 # Finally, picks_by_type (if relevant) if not all_data: picks_by_type = [p for p in picks_by_type if p[0] in _DATA_CH_TYPES_SPLIT] picks_by_type = [(p[0], np.array(p[1], int)) for p in picks_by_type] actual = _picks_by_type(info, ref_meg=ref_meg) assert_object_equal(actual, picks_by_type) if not ref_meg and idx['hbo']: # our old code had a bug with pytest.raises(TypeError, match='unexpected keyword argument'): _picks_by_type_old(info, ref_meg=ref_meg) else: old = _picks_by_type_old(info, ref_meg=ref_meg) assert_object_equal(old, picks_by_type) # test bads info = info.copy() info['bads'] = [info['chs'][picks_by_type[0][1][0]]['ch_name']] picks_by_type = deepcopy(picks_by_type) picks_by_type[0] = (picks_by_type[0][0], picks_by_type[0][1][1:]) actual = _picks_by_type(info, ref_meg=ref_meg) assert_object_equal(actual, picks_by_type)
def test_picks_by_channels(): """Test creating pick_lists.""" rng = np.random.RandomState(909) test_data = rng.random_sample((4, 2000)) ch_names = ['MEG %03d' % i for i in [1, 2, 3, 4]] ch_types = ['grad', 'mag', 'mag', 'eeg'] sfreq = 250.0 info = create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types) _assert_channel_types(info) raw = RawArray(test_data, info) pick_list = _picks_by_type(raw.info) assert_equal(len(pick_list), 3) assert_equal(pick_list[0][0], 'mag') pick_list2 = _picks_by_type(raw.info, meg_combined=False) assert_equal(len(pick_list), len(pick_list2)) assert_equal(pick_list2[0][0], 'mag') pick_list2 = _picks_by_type(raw.info, meg_combined=True) assert_equal(len(pick_list), len(pick_list2) + 1) assert_equal(pick_list2[0][0], 'meg') test_data = rng.random_sample((4, 2000)) ch_names = ['MEG %03d' % i for i in [1, 2, 3, 4]] ch_types = ['mag', 'mag', 'mag', 'mag'] sfreq = 250.0 info = create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types) raw = RawArray(test_data, info) # This acts as a set, not an order assert_array_equal(pick_channels(info['ch_names'], ['MEG 002', 'MEG 001']), [0, 1]) # Make sure checks for list input work. pytest.raises(ValueError, pick_channels, ch_names, 'MEG 001') pytest.raises(ValueError, pick_channels, ch_names, ['MEG 001'], 'hi') pick_list = _picks_by_type(raw.info) assert_equal(len(pick_list), 1) assert_equal(pick_list[0][0], 'mag') pick_list2 = _picks_by_type(raw.info, meg_combined=True) assert_equal(len(pick_list), len(pick_list2)) assert_equal(pick_list2[0][0], 'mag') # pick_types type check with pytest.raises(ValueError, match='must be of type'): raw.pick_types(eeg='string') # duplicate check names = ['MEG 002', 'MEG 002'] assert len(pick_channels(raw.info['ch_names'], names)) == 1 assert len(raw.copy().pick_channels(names)[0][0]) == 1
def test_picks_by_channels(): """Test creating pick_lists.""" rng = np.random.RandomState(909) test_data = rng.random_sample((4, 2000)) ch_names = ['MEG %03d' % i for i in [1, 2, 3, 4]] ch_types = ['grad', 'mag', 'mag', 'eeg'] sfreq = 250.0 info = create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types) _assert_channel_types(info) raw = RawArray(test_data, info) pick_list = _picks_by_type(raw.info) assert_equal(len(pick_list), 3) assert_equal(pick_list[0][0], 'mag') pick_list2 = _picks_by_type(raw.info, meg_combined=False) assert_equal(len(pick_list), len(pick_list2)) assert_equal(pick_list2[0][0], 'mag') pick_list2 = _picks_by_type(raw.info, meg_combined=True) assert_equal(len(pick_list), len(pick_list2) + 1) assert_equal(pick_list2[0][0], 'meg') test_data = rng.random_sample((4, 2000)) ch_names = ['MEG %03d' % i for i in [1, 2, 3, 4]] ch_types = ['mag', 'mag', 'mag', 'mag'] sfreq = 250.0 info = create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types) raw = RawArray(test_data, info) # This acts as a set, not an order assert_array_equal(pick_channels(info['ch_names'], ['MEG 002', 'MEG 001']), [0, 1]) # Make sure checks for list input work. pytest.raises(ValueError, pick_channels, ch_names, 'MEG 001') pytest.raises(ValueError, pick_channels, ch_names, ['MEG 001'], 'hi') pick_list = _picks_by_type(raw.info) assert_equal(len(pick_list), 1) assert_equal(pick_list[0][0], 'mag') pick_list2 = _picks_by_type(raw.info, meg_combined=True) assert_equal(len(pick_list), len(pick_list2)) assert_equal(pick_list2[0][0], 'mag') # pick_types type check pytest.raises(ValueError, raw.pick_types, eeg='string') # duplicate check names = ['MEG 002', 'MEG 002'] assert len(pick_channels(raw.info['ch_names'], names)) == 1 assert len(raw.copy().pick_channels(names)[0][0]) == 1
def test_raw_rank_estimation(fname, ref_meg, scalings): """Test raw rank estimation.""" if ref_meg and scalings != 'norm': # Adjust for CTF data (scale factors are quite different) scalings = dict(mag=1e31, grad=1e11) raw = read_raw_fif(fname) raw.crop(0, min(4., raw.times[-1])).load_data() out = _picks_by_type(raw.info, ref_meg=ref_meg, meg_combined=True) has_eeg = 'eeg' in raw if has_eeg: (_, picks_meg), (_, picks_eeg) = out else: (_, picks_meg), = out picks_eeg = [] n_meg = len(picks_meg) n_eeg = len(picks_eeg) if len(raw.info['proc_history']) == 0: expected_rank = n_meg + n_eeg else: expected_rank = _get_rank_sss(raw.info) + n_eeg got_rank = _estimate_rank_raw(raw, scalings=scalings, with_ref_meg=ref_meg) assert got_rank == expected_rank if has_eeg: with pytest.deprecated_call(): assert raw.estimate_rank(picks=picks_eeg, scalings=scalings) == n_eeg if 'sss' in fname: raw.add_proj(compute_proj_raw(raw)) raw.apply_proj() n_proj = len(raw.info['projs']) want_rank = expected_rank - (0 if 'sss' in fname else n_proj) got_rank = _estimate_rank_raw(raw, scalings=scalings, with_ref_meg=ref_meg) assert got_rank == want_rank
def test_rank_estimation(): """Test raw rank estimation """ iter_tests = itt.product([fif_fname, hp_fif_fname], ["norm", dict(mag=1e11, grad=1e9, eeg=1e5)]) # sss for fname, scalings in iter_tests: raw = Raw(fname) (_, picks_meg), (_, picks_eeg) = _picks_by_type(raw.info, meg_combined=True) n_meg = len(picks_meg) n_eeg = len(picks_eeg) raw = Raw(fname, preload=True) if "proc_history" not in raw.info: expected_rank = n_meg + n_eeg else: mf = raw.info["proc_history"][0]["max_info"] expected_rank = _get_sss_rank(mf) + n_eeg assert_array_equal(raw.estimate_rank(scalings=scalings), expected_rank) assert_array_equal(raw.estimate_rank(picks=picks_eeg, scalings=scalings), n_eeg) raw = Raw(fname, preload=False) if "sss" in fname: tstart, tstop = 0.0, 30.0 raw.add_proj(compute_proj_raw(raw)) raw.apply_proj() else: tstart, tstop = 10.0, 20.0 raw.apply_proj() n_proj = len(raw.info["projs"]) assert_array_equal( raw.estimate_rank(tstart=tstart, tstop=tstop, scalings=scalings), expected_rank - (1 if "sss" in fname else n_proj), )
def test_cov_scaling(): """Test rescaling covs""" evoked = read_evokeds(ave_fname, condition=0, baseline=(None, 0), proj=True) cov = read_cov(cov_fname)['data'] cov2 = read_cov(cov_fname)['data'] assert_array_equal(cov, cov2) evoked.pick_channels([evoked.ch_names[k] for k in pick_types( evoked.info, meg=True, eeg=True )]) picks_list = _picks_by_type(evoked.info) scalings = dict(mag=1e15, grad=1e13, eeg=1e6) _apply_scaling_cov(cov2, picks_list, scalings=scalings) _apply_scaling_cov(cov, picks_list, scalings=scalings) assert_array_equal(cov, cov2) assert_true(cov.max() > 1) _undo_scaling_cov(cov2, picks_list, scalings=scalings) _undo_scaling_cov(cov, picks_list, scalings=scalings) assert_array_equal(cov, cov2) assert_true(cov.max() < 1) data = evoked.data.copy() _apply_scaling_array(data, picks_list, scalings=scalings) _undo_scaling_array(data, picks_list, scalings=scalings) assert_allclose(data, evoked.data, atol=1e-20)
def test_rank_estimation(): """Test raw rank estimation.""" iter_tests = itt.product( [fif_fname, hp_fif_fname], # sss ['norm', dict(mag=1e11, grad=1e9, eeg=1e5)]) for fname, scalings in iter_tests: raw = read_raw_fif(fname).crop(0, 4.).load_data() (_, picks_meg), (_, picks_eeg) = _picks_by_type(raw.info, meg_combined=True) n_meg = len(picks_meg) n_eeg = len(picks_eeg) if len(raw.info['proc_history']) == 0: expected_rank = n_meg + n_eeg else: expected_rank = _get_rank_sss(raw.info) + n_eeg assert _estimate_rank_raw(raw, scalings=scalings) == expected_rank with pytest.deprecated_call(): assert raw.estimate_rank(picks=picks_eeg, scalings=scalings) == n_eeg if 'sss' in fname: raw.add_proj(compute_proj_raw(raw)) raw.apply_proj() n_proj = len(raw.info['projs']) want_rank = expected_rank - (0 if 'sss' in fname else n_proj) assert _estimate_rank_raw(raw, scalings=scalings) == want_rank
def test_raw_rank_estimation(fname, ref_meg, scalings): """Test raw rank estimation.""" if ref_meg and scalings != 'norm': # Adjust for CTF data (scale factors are quite different) scalings = dict(mag=1e31, grad=1e11) raw = read_raw_fif(fname) raw.crop(0, min(4., raw.times[-1])).load_data() out = _picks_by_type(raw.info, ref_meg=ref_meg, meg_combined=True) has_eeg = 'eeg' in raw if has_eeg: (_, picks_meg), (_, picks_eeg) = out else: (_, picks_meg), = out picks_eeg = [] n_meg = len(picks_meg) n_eeg = len(picks_eeg) if len(raw.info['proc_history']) == 0: expected_rank = n_meg + n_eeg else: expected_rank = _get_rank_sss(raw.info) + n_eeg got_rank = _estimate_rank_raw(raw, scalings=scalings, with_ref_meg=ref_meg) assert got_rank == expected_rank if 'sss' in fname: raw.add_proj(compute_proj_raw(raw)) raw.apply_proj() n_proj = len(raw.info['projs']) want_rank = expected_rank - (0 if 'sss' in fname else n_proj) got_rank = _estimate_rank_raw(raw, scalings=scalings, with_ref_meg=ref_meg) assert got_rank == want_rank
def test_cov_scaling(): """Test rescaling covs.""" evoked = read_evokeds(ave_fname, condition=0, baseline=(None, 0), proj=True) cov = read_cov(cov_fname)['data'] cov2 = read_cov(cov_fname)['data'] assert_array_equal(cov, cov2) evoked.pick_channels([ evoked.ch_names[k] for k in pick_types(evoked.info, meg=True, eeg=True) ]) picks_list = _picks_by_type(evoked.info) scalings = dict(mag=1e15, grad=1e13, eeg=1e6) _apply_scaling_cov(cov2, picks_list, scalings=scalings) _apply_scaling_cov(cov, picks_list, scalings=scalings) assert_array_equal(cov, cov2) assert cov.max() > 1 _undo_scaling_cov(cov2, picks_list, scalings=scalings) _undo_scaling_cov(cov, picks_list, scalings=scalings) assert_array_equal(cov, cov2) assert cov.max() < 1 data = evoked.data.copy() _apply_scaling_array(data, picks_list, scalings=scalings) _undo_scaling_array(data, picks_list, scalings=scalings) assert_allclose(data, evoked.data, atol=1e-20)
def _find_bad_channels(epochs, picks, use_metrics, thresh, max_iter): """Implements the first step of the FASTER algorithm. This function attempts to automatically mark bad EEG channels by performing outlier detection. It operated on epoched data, to make sure only relevant data is analyzed. Additional Parameters --------------------- use_metrics : list of str List of metrics to use. Can be any combination of: 'variance', 'correlation', 'hurst', 'kurtosis', 'line_noise' Defaults to all of them. thresh : float The threshold value, in standard deviations, to apply. A channel crossing this threshold value is marked as bad. Defaults to 3. max_iter : int The maximum number of iterations performed during outlier detection (defaults to 1, as in the original FASTER paper). """ from scipy.stats import kurtosis metrics = { 'variance': lambda x: np.var(x, axis=1), 'correlation': lambda x: np.mean(np.ma.masked_array(np.corrcoef(x), np.identity(len(x), dtype=bool)), axis=0), 'hurst': lambda x: _hurst(x), 'kurtosis': lambda x: kurtosis(x, axis=1), 'line_noise': lambda x: _freqs_power(x, epochs.info['sfreq'], [50, 60]), } if use_metrics is None: use_metrics = metrics.keys() # Concatenate epochs in time data = epochs.get_data()[:, picks] data = data.transpose(1, 0, 2).reshape(data.shape[1], -1) # Find bad channels bads = defaultdict(list) info = pick_info(epochs.info, picks, copy=True) for ch_type, chs in _picks_by_type(info): logger.info('Bad channel detection on %s channels:' % ch_type.upper()) for metric in use_metrics: scores = metrics[metric](data[chs]) bad_channels = [ epochs.ch_names[picks[chs[i]]] for i in find_outliers(scores, thresh, max_iter) ] logger.info('\tBad by %s: %s' % (metric, bad_channels)) bads[metric].append(bad_channels) bads = dict((k, np.concatenate(v).tolist()) for k, v in bads.items()) return bads
def test_picks_by_channels(): """Test creating pick_lists""" rng = np.random.RandomState(909) test_data = rng.random_sample((4, 2000)) ch_names = ['MEG %03d' % i for i in [1, 2, 3, 4]] ch_types = ['grad', 'mag', 'mag', 'eeg'] sfreq = 250.0 info = create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types) raw = RawArray(test_data, info) pick_list = _picks_by_type(raw.info) assert_equal(len(pick_list), 3) assert_equal(pick_list[0][0], 'mag') pick_list2 = _picks_by_type(raw.info, meg_combined=False) assert_equal(len(pick_list), len(pick_list2)) assert_equal(pick_list2[0][0], 'mag') pick_list2 = _picks_by_type(raw.info, meg_combined=True) assert_equal(len(pick_list), len(pick_list2) + 1) assert_equal(pick_list2[0][0], 'meg') test_data = rng.random_sample((4, 2000)) ch_names = ['MEG %03d' % i for i in [1, 2, 3, 4]] ch_types = ['mag', 'mag', 'mag', 'mag'] sfreq = 250.0 info = create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types) raw = RawArray(test_data, info) # Make sure checks for list input work. assert_raises(ValueError, pick_channels, ch_names, 'MEG 001') assert_raises(ValueError, pick_channels, ch_names, ['MEG 001'], 'hi') pick_list = _picks_by_type(raw.info) assert_equal(len(pick_list), 1) assert_equal(pick_list[0][0], 'mag') pick_list2 = _picks_by_type(raw.info, meg_combined=True) assert_equal(len(pick_list), len(pick_list2)) assert_equal(pick_list2[0][0], 'mag') # pick_types type check assert_raises(ValueError, raw.pick_types, eeg='string')
def _find_bad_channels(epochs, picks, use_metrics, thresh, max_iter): """Implements the first step of the FASTER algorithm. This function attempts to automatically mark bad EEG channels by performing outlier detection. It operated on epoched data, to make sure only relevant data is analyzed. Additional Parameters --------------------- use_metrics : list of str List of metrics to use. Can be any combination of: 'variance', 'correlation', 'hurst', 'kurtosis', 'line_noise' Defaults to all of them. thresh : float The threshold value, in standard deviations, to apply. A channel crossing this threshold value is marked as bad. Defaults to 3. max_iter : int The maximum number of iterations performed during outlier detection (defaults to 1, as in the original FASTER paper). """ from scipy.stats import kurtosis metrics = { 'variance': lambda x: np.var(x, axis=1), 'correlation': lambda x: np.mean( np.ma.masked_array(np.corrcoef(x), np.identity(len(x), dtype=bool)), axis=0), 'hurst': lambda x: _hurst(x), 'kurtosis': lambda x: kurtosis(x, axis=1), 'line_noise': lambda x: _freqs_power(x, epochs.info['sfreq'], [50, 60]), } if use_metrics is None: use_metrics = metrics.keys() # Concatenate epochs in time data = epochs.get_data()[:, picks] data = data.transpose(1, 0, 2).reshape(data.shape[1], -1) # Find bad channels bads = defaultdict(list) info = pick_info(epochs.info, picks, copy=True) for ch_type, chs in _picks_by_type(info): logger.info('Bad channel detection on %s channels:' % ch_type.upper()) for metric in use_metrics: scores = metrics[metric](data[chs]) bad_channels = [epochs.ch_names[picks[chs[i]]] for i in find_outliers(scores, thresh, max_iter)] logger.info('\tBad by %s: %s' % (metric, bad_channels)) bads[metric].append(bad_channels) bads = dict((k, np.concatenate(v).tolist()) for k, v in bads.items()) return bads
def epochs_compute_cnv(epochs, tmin=None, tmax=None): """Compute contingent negative variation (CNV) Parameters ---------- epochs : instance of Epochs The input data. tmin : float | None The first time point to include, if None, all samples form the first sample of the epoch will be used. Defaults to None. tmax : float | None The last time point to include, if None, all samples up to the last sample of the epoch wi ll be used. Defaults to None. return_epochs : bool Whether to compute an average or not. If False, data will be averaged and put in an Evoked object. Defaults to False. Returns ------- cnv : ndarray of float (n_channels, n_epochs) | instance of Evoked The regression slopes (betas) represewnting contingent negative variation. """ picks = mne.pick_types(epochs.info, meg=True, eeg=True) n_epochs = len(epochs.events) n_channels = len(picks) # we reduce over time samples slopes = np.zeros((n_epochs, n_channels)) intercepts = np.zeros((n_epochs, n_channels)) if tmax is None: tmax = epochs.times[-1] if tmin is None: tmin = epochs.times[0] fit_range = np.where(_time_mask(epochs.times, tmin, tmax))[0] # design: intercept + increasing time design_matrix = np.c_[np.ones(len(fit_range)), epochs.times[fit_range] - tmin] # estimate single trial regression over time samples scales = np.zeros(n_channels) info_ = pick_info(epochs.info, picks) for this_type, this_picks in _picks_by_type(info_): scales[this_picks] = _handle_default('scalings')[this_type] for ii, epoch in enumerate(epochs): y = epoch[picks][:, fit_range].T # time is samples betas, _, _, _ = linalg.lstsq(a=design_matrix, b=y * scales) intercepts[ii] = betas[0] slopes[ii] = betas[1] return slopes, intercepts
def _find_bad_channels_in_epochs(epochs, picks, use_metrics, thresh, max_iter): """Implements the fourth step of the FASTER algorithm. This function attempts to automatically mark bad channels in each epochs by performing outlier detection. Additional Parameters --------------------- use_metrics : list of str List of metrics to use. Can be any combination of: 'amplitude', 'variance', 'deviation', 'median_gradient' Defaults to all of them. thresh : float The threshold value, in standard deviations, to apply. A channel crossing this threshold value is marked as bad. Defaults to 3. max_iter : int The maximum number of iterations performed during outlier detection (defaults to 1, as in the original FASTER paper). """ metrics = { 'amplitude': lambda x: np.ptp(x, axis=2), 'deviation': lambda x: _deviation(x), 'variance': lambda x: np.var(x, axis=2), 'median_gradient': lambda x: np.median(np.abs(np.diff(x)), axis=2), 'line_noise': lambda x: _freqs_power(x, epochs.info['sfreq'], [50, 60]), } if use_metrics is None: use_metrics = metrics.keys() info = pick_info(epochs.info, picks, copy=True) data = epochs.get_data()[:, picks] bads = dict( (m, np.zeros((len(data), len(picks)), dtype=bool)) for m in metrics) for ch_type, chs in _picks_by_type(info): ch_names = [info['ch_names'][k] for k in chs] chs = np.array(chs) for metric in use_metrics: logger.info('Bad channel-in-epoch detection on %s channels:' % ch_type.upper()) s_epochs = metrics[metric](data[:, chs]) for i_epochs, epoch in enumerate(s_epochs): outliers = find_outliers(epoch, thresh, max_iter) if len(outliers) > 0: bad_segment = [ch_names[k] for k in outliers] logger.info('Epoch %d, Bad by %s:\n\t%s' % (i_epochs, metric, bad_segment)) bads[metric][i_epochs, chs[outliers]] = True return bads
def _find_bad_channels_in_epochs(epochs, picks, use_metrics, thresh, max_iter): """Implements the fourth step of the FASTER algorithm. This function attempts to automatically mark bad channels in each epochs by performing outlier detection. Additional Parameters --------------------- use_metrics : list of str List of metrics to use. Can be any combination of: 'amplitude', 'variance', 'deviation', 'median_gradient' Defaults to all of them. thresh : float The threshold value, in standard deviations, to apply. A channel crossing this threshold value is marked as bad. Defaults to 3. max_iter : int The maximum number of iterations performed during outlier detection (defaults to 1, as in the original FASTER paper). """ metrics = { 'amplitude': lambda x: np.ptp(x, axis=2), 'deviation': lambda x: _deviation(x), 'variance': lambda x: np.var(x, axis=2), 'median_gradient': lambda x: np.median(np.abs(np.diff(x)), axis=2), 'line_noise': lambda x: _freqs_power(x, epochs.info['sfreq'], [50, 60]), } if use_metrics is None: use_metrics = metrics.keys() info = pick_info(epochs.info, picks, copy=True) data = epochs.get_data()[:, picks] bads = dict((m, np.zeros((len(data), len(picks)), dtype=bool)) for m in metrics) for ch_type, chs in _picks_by_type(info): ch_names = [info['ch_names'][k] for k in chs] chs = np.array(chs) for metric in use_metrics: logger.info('Bad channel-in-epoch detection on %s channels:' % ch_type.upper()) s_epochs = metrics[metric](data[:, chs]) for i_epochs, epoch in enumerate(s_epochs): outliers = find_outliers(epoch, thresh, max_iter) if len(outliers) > 0: bad_segment = [ch_names[k] for k in outliers] logger.info('Epoch %d, Bad by %s:\n\t%s' % ( i_epochs, metric, bad_segment)) bads[metric][i_epochs, chs[outliers]] = True return bads
def test_picks_by_channels(): """Test creating pick_lists""" rng = np.random.RandomState(909) test_data = rng.random_sample((4, 2000)) ch_names = ['MEG %03d' % i for i in [1, 2, 3, 4]] ch_types = ['grad', 'mag', 'mag', 'eeg'] sfreq = 250.0 info = create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types) raw = RawArray(test_data, info) pick_list = _picks_by_type(raw.info) assert_equal(len(pick_list), 3) assert_equal(pick_list[0][0], 'mag') pick_list2 = _picks_by_type(raw.info, meg_combined=False) assert_equal(len(pick_list), len(pick_list2)) assert_equal(pick_list2[0][0], 'mag') pick_list2 = _picks_by_type(raw.info, meg_combined=True) assert_equal(len(pick_list), len(pick_list2) + 1) assert_equal(pick_list2[0][0], 'meg') test_data = rng.random_sample((4, 2000)) ch_names = ['MEG %03d' % i for i in [1, 2, 3, 4]] ch_types = ['mag', 'mag', 'mag', 'mag'] sfreq = 250.0 info = create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types) raw = RawArray(test_data, info) pick_list = _picks_by_type(raw.info) assert_equal(len(pick_list), 1) assert_equal(pick_list[0][0], 'mag') pick_list2 = _picks_by_type(raw.info, meg_combined=True) assert_equal(len(pick_list), len(pick_list2)) assert_equal(pick_list2[0][0], 'mag')
def _find_bad_epochs(epochs, picks, use_metrics, thresh, max_iter): """Implements the second step of the FASTER algorithm. This function attempts to automatically mark bad epochs by performing outlier detection. Additional Parameters --------------------- use_metrics : list of str List of metrics to use. Can be any combination of: 'amplitude', 'variance', 'deviation'. Defaults to all of them. thresh : float The threshold value, in standard deviations, to apply. A channel crossing this threshold value is marked as bad. Defaults to 3. max_iter : int The maximum number of iterations performed during outlier detection (defaults to 1, as in the original FASTER paper). """ metrics = { 'amplitude': lambda x: np.mean(np.ptp(x, axis=2), axis=1), 'deviation': lambda x: np.mean(_deviation(x), axis=1), 'variance': lambda x: np.mean(np.var(x, axis=2), axis=1), } if use_metrics is None: use_metrics = metrics.keys() info = pick_info(epochs.info, picks, copy=True) data = epochs.get_data()[:, picks] bads = defaultdict(list) for ch_type, chs in _picks_by_type(info): logger.info('Bad epoch detection on %s channels:' % ch_type.upper()) for metric in use_metrics: scores = metrics[metric](data[:, chs]) bad_epochs = find_outliers(scores, thresh, max_iter) logger.info('\tBad by %s: %s' % (metric, bad_epochs)) bads[metric].append(bad_epochs) bads = dict((k, np.concatenate(v).tolist()) for k, v in bads.items()) return bads
def test_rank_estimation(): """Test raw rank estimation """ iter_tests = itt.product( [fif_fname, hp_fif_fname], # sss ['norm', dict(mag=1e11, grad=1e9, eeg=1e5)] ) for fname, scalings in iter_tests: raw = Raw(fname) (_, picks_meg), (_, picks_eeg) = _picks_by_type(raw.info, meg_combined=True) n_meg = len(picks_meg) n_eeg = len(picks_eeg) raw = Raw(fname, preload=True) if 'proc_history' not in raw.info: expected_rank = n_meg + n_eeg else: mf = raw.info['proc_history'][0]['max_info'] expected_rank = _get_sss_rank(mf) + n_eeg assert_array_equal(raw.estimate_rank(scalings=scalings), expected_rank) assert_array_equal(raw.estimate_rank(picks=picks_eeg, scalings=scalings), n_eeg) raw = Raw(fname, preload=False) if 'sss' in fname: tstart, tstop = 0., 30. raw.add_proj(compute_proj_raw(raw)) raw.apply_proj() else: tstart, tstop = 10., 20. raw.apply_proj() n_proj = len(raw.info['projs']) assert_array_equal(raw.estimate_rank(tstart=tstart, tstop=tstop, scalings=scalings), expected_rank - (1 if 'sss' in fname else n_proj))