def make_meta_epochs(epochs, y, n_bin=100): from scipy.stats import trim_mean from mne.epochs import EpochsArray meta_data = list() # EEG data meta_y = list() # regressors n = len(epochs) # make continuous y into bins to become categorical if len(np.unique(y)) < n_bin: hist, bin_edge = np.histogram(y, n_bin) y_ = y for low, high in zip(bin_edge[:-1], bin_edge[1:]): sel = np.where((y >= low) & (y < high))[0] y_[sel] = .5 * (high + low) y = y_ # if discrete and few categories if len(np.unique(y)) < n_bin: already_used = list() for this_y in np.unique(y): for ii in range(n / len(np.unique(y)) / n_bin): sel = np.where(y == this_y)[0] sel = [ii for ii in sel if ii not in already_used][:n_bin] if not len(sel): continue meta_data.append(trim_mean(epochs._data[sel, :, :], .05, axis=0)) meta_y.append(this_y) already_used += sel else: hist, bin_edge = np.histogram(y, n_bin) for low, high in zip(bin_edge[:-1], bin_edge[1:]): sel = np.where((y >= low) & (y < high))[0] this_y = .5 * (high + low) if not len(sel): continue meta_data.append(trim_mean(epochs._data[sel, :, :], .05, axis=0)) meta_y.append(this_y) events = np.vstack((np.zeros(len(meta_y)), np.zeros(len(meta_y)), meta_y)).T events = np.array(np.round(events), int) # transform into epochs new_epochs = EpochsArray(meta_data, epochs.info, events=events, verbose=False) new_epochs.events = np.array(new_epochs.events, float) new_epochs.events[:, 2] = meta_y # XXX why change time and sfreq? new_epochs.times = epochs.times new_epochs.info['sfreq'] = epochs.info['sfreq'] return new_epochs
def test_array_epochs(): """Test creating epochs from array """ # creating rng = np.random.RandomState(42) data = rng.random_sample((10, 20, 300)) sfreq = 1e3 ch_names = ["EEG %03d" % (i + 1) for i in range(20)] types = ["eeg"] * 20 info = create_info(ch_names, sfreq, types) events = np.c_[np.arange(1, 600, 60), np.zeros(10), [1, 2] * 5] event_id = {"a": 1, "b": 2} epochs = EpochsArray(data, info, events=events, event_id=event_id, tmin=-0.2) # saving temp_fname = op.join(tempdir, "test-epo.fif") epochs.save(temp_fname) epochs2 = read_epochs(temp_fname) data2 = epochs2.get_data() assert_allclose(data, data2) assert_allclose(epochs.times, epochs2.times) assert_equal(epochs.event_id, epochs2.event_id) assert_array_equal(epochs.events, epochs2.events) # plotting import matplotlib matplotlib.use("Agg") # for testing don't use X server epochs[0].plot() # indexing assert_array_equal(np.unique(epochs["a"].events[:, 2]), np.array([1])) assert_equal(len(epochs[:2]), 2) data[0, 5, 150] = 3000 data[1, :, :] = 0 data[2, 5, 210] = 3000 data[3, 5, 260] = 0 epochs = EpochsArray( data, info, events=events, event_id=event_id, tmin=0, reject=dict(eeg=1000), flat=dict(eeg=1e-1), reject_tmin=0.1, reject_tmax=0.2, ) assert_equal(len(epochs), len(events) - 2) assert_equal(epochs.drop_log[0], ["EEG 006"]) assert_equal(len(events), len(epochs.selection))
def get_data_as_epoch(self, n_samples=1024, picks=None): """Return last n_samples from current time. Parameters ---------- n_samples : int Number of samples to fetch. %(picks_all)s Returns ------- epoch : instance of Epochs The samples fetched as an Epochs object. See Also -------- mne.Epochs.iter_evoked """ # set up timeout in case LSL process hang. wait arb 5x expected time wait_time = n_samples * 5. / self.info['sfreq'] # create an event at the start of the data collection events = np.expand_dims(np.array([0, 1, 1]), axis=0) samples, _ = self.client.pull_chunk(max_samples=n_samples, timeout=wait_time) data = np.vstack(samples).T picks = _picks_to_idx(self.info, picks, 'all', exclude=()) info = pick_info(self.info, picks) return EpochsArray(data[picks][np.newaxis], info, events)
def get_data_as_epoch(self, n_samples=1024, picks=None): """Return last n_samples from current time. Parameters ---------- n_samples : int Number of samples to fetch. %(picks_all)s Returns ------- epoch : instance of Epochs The samples fetched as an Epochs object. See Also -------- mne.Epochs.iter_evoked """ ft_header = self.ft_client.getHeader() last_samp = ft_header.nSamples - 1 start = last_samp - n_samples + 1 stop = last_samp events = np.expand_dims(np.array([start, 1, 1]), axis=0) # get the data data = self.ft_client.getData([start, stop]).transpose() # create epoch from data picks = _picks_to_idx(self.info, picks, 'all', exclude=()) info = pick_info(self.info, picks) return EpochsArray(data[picks][np.newaxis], info, events)
def Evokeds_to_Epochs(inst, info=None, events=None): """Convert list of evoked into single epochs Parameters ---------- inst: list list of evoked objects. info : dict By default copy dict from inst[0] events : np.array (dims: n, 3) Returns ------- epochs: epochs object""" from mne.epochs import EpochsArray from mne.evoked import Evoked if (not (isinstance(inst, list)) or not np.all([isinstance(x, Evoked) for x in inst])): raise ('inst mus be a list of evoked') # concatenate signals data = [x.data for x in inst] # extract meta data if info is None: info = inst[0].info if events is None: n = len(inst) events = np.c_[np.cumsum(np.ones(n)) * info['sfreq'], np.zeros(n), np.ones(n)] return EpochsArray(data, info, events=events, tmin=inst[0].times.min())
def _load_ft_epochs(fname, rawinfo, trialinfo_col=-1): # load Matlab/Fieldtrip data f = h5py.File(fname) list(f.keys()) ft_data = f['data'] ft_data.keys() trialinfo = ft_data['trialinfo'] channels = ft_data['label'] sampleinfo = ft_data['sampleinfo'] time = ft_data['time'] sfreq = np.around(1 / np.diff(time[:].ravel()), 2) assert (len(np.unique(sfreq)) == 1) n_time, n_chans, n_trial = ft_data['trial'].shape data = np.zeros((n_trial, n_chans, n_time)) transposed_data = np.transpose(ft_data['trial']) for trial in range(n_trial): data[trial, :, :] = transposed_data[trial] data = data[:, range(n_chans), :] chan_names = [] for i in range(n_chans): st = channels[0][i] obj = ft_data[st] chan_names.append(''.join(chr(j) for j in obj[:])) #ch_names = [x + '-3705' for x in chan_names] info = create_info(chan_names, sfreq[0]) events = np.zeros((n_trial, 3), int) events[:, 2] = trialinfo[trialinfo_col] events[:, 0] = sampleinfo[0] epochs = EpochsArray(data, info, events=events, tmin=min(time)[0], verbose=False) epochs.info = fix_chs(rawinfo, epochs.info) return epochs
def generate_epoch(n_epochs_cond=5, n_channels=10, n_times=20, sfreq=1000., n_conditions=2): """Generate an epoch instance with an arbitrary number of timepoints, channels, trials, and conditions""" n_epochs = n_epochs_cond * n_conditions data = rng.randn(n_epochs, n_channels, n_times) conditions = np.repeat(np.arange(n_conditions), n_epochs_cond) events = np.array([np.arange(n_epochs), [0] * n_epochs, conditions]).T info = create_info(n_channels, sfreq, 'mag') epochs = EpochsArray(data, info, events) return epochs
def mat2mne(data, chan_names='meg', chan_types=None, sfreq=250, events=None, tmin=0): from mne.epochs import EpochsArray from mne.io.meas_info import create_info data = np.array(data) n_trial, n_chan, n_time = data.shape # chan names if isinstance(chan_names, str): chan_names = [chan_names + '_%02i' % chan for chan in range(n_chan)] if len(chan_names) != n_chan: raise ValueError('chan_names must be a string or a list of' 'n_chan strings') # chan types if isinstance(chan_types, str): chan_types = [chan_types] * n_chan elif chan_types is None: if isinstance(chan_names, str): if chan_names != 'meg': chan_types = [chan_names] * n_chan else: chan_types = ['mag'] * n_chan elif isinstance(chan_names, list): chan_types = ['mag' for chan in chan_names] else: raise ValueError('Specify chan_types') # events if events is None: events = np.c_[np.cumsum(np.ones(n_trial)) * 5 * sfreq, np.zeros(n_trial, int), np.zeros(n_trial)] else: events = np.array(events, int) if events.ndim == 1: events = np.c_[np.cumsum(np.ones(n_trial)) * 5 * sfreq, np.zeros(n_trial), events] elif (events.ndim != 2) or (events.shape[1] != 3): raise ValueError('events shape must be ntrial, or ntrials * 3') info = create_info(chan_names, sfreq, chan_types) return EpochsArray(data, info, events=np.array(events, int), verbose=False, tmin=tmin)
def test_array_epochs(): """Test creating epochs from array """ # creating rng = np.random.RandomState(42) data = rng.random_sample((10, 20, 50)) sfreq = 1e3 ch_names = ['EEG %03d' % (i + 1) for i in range(20)] types = ['eeg'] * 20 info = create_info(ch_names, sfreq, types) events = np.c_[np.arange(1, 600, 60), np.zeros(10), [1, 2] * 5] event_id = {'a': 1, 'b': 2} epochs = EpochsArray(data, info, events=events, event_id=event_id, tmin=-.2) # saving temp_fname = op.join(tempdir, 'epo.fif') epochs.save(temp_fname) epochs2 = read_epochs(temp_fname) data2 = epochs2.get_data() assert_allclose(data, data2) assert_allclose(epochs.times, epochs2.times) assert_equal(epochs.event_id, epochs2.event_id) assert_array_equal(epochs.events, epochs2.events) # plotting import matplotlib matplotlib.use('Agg') # for testing don't use X server epochs[0].plot() # indexing assert_array_equal(np.unique(epochs['a'].events[:, 2]), np.array([1])) assert_equal(len(epochs[:2]), 2)
def make_meta_epochs(epochs, y, n_bin=100): from scipy.stats import trim_mean from mne.epochs import EpochsArray meta_data = list() # EEG data meta_y = list() # regressors n = len(epochs) # make continuous y into bins to become categorical if len(np.unique(y)) < n_bin: hist, bin_edge = np.histogram(y, n_bin) y_ = y for low, high in zip(bin_edge[:-1], bin_edge[1:]): sel = np.where((y >= low) & (y < high))[0] y_[sel] = .5 * (high + low) y = y_ # if discrete and few categories if len(np.unique(y)) < n_bin: already_used = list() for this_y in np.unique(y): for ii in range(n / len(np.unique(y)) / n_bin): sel = np.where(y == this_y)[0] sel = [ii for ii in sel if ii not in already_used][:n_bin] if not len(sel): continue meta_data.append( trim_mean(epochs._data[sel, :, :], .05, axis=0)) meta_y.append(this_y) already_used += sel else: hist, bin_edge = np.histogram(y, n_bin) for low, high in zip(bin_edge[:-1], bin_edge[1:]): sel = np.where((y >= low) & (y < high))[0] this_y = .5 * (high + low) if not len(sel): continue meta_data.append(trim_mean(epochs._data[sel, :, :], .05, axis=0)) meta_y.append(this_y) events = np.vstack( (np.zeros(len(meta_y)), np.zeros(len(meta_y)), meta_y)).T events = np.array(np.round(events), int) # transform into epochs new_epochs = EpochsArray(meta_data, epochs.info, events=events, verbose=False) new_epochs.events = np.array(new_epochs.events, float) new_epochs.events[:, 2] = meta_y # XXX why change time and sfreq? new_epochs.times = epochs.times new_epochs.info['sfreq'] = epochs.info['sfreq'] return new_epochs
def add_channels(inst, data, ch_names, ch_types): from mne.io import _BaseRaw, RawArray from mne.epochs import _BaseEpochs, EpochsArray from mne import create_info if 'meg' in ch_types or 'eeg' in ch_types: return NotImplementedError('Can only add misc, stim and ieeg channels') info = create_info(ch_names=ch_names, sfreq=inst.info['sfreq'], ch_types=ch_types) if isinstance(inst, _BaseRaw): for key in ('buffer_size_sec', 'filename'): info[key] = inst.info[key] new_inst = RawArray(data, info=info, first_samp=inst._first_samps[0]) elif isinstance(inst, _BaseEpochs): new_inst = EpochsArray(data, info=info) else: raise ValueError('unknown inst type') return inst.add_channels([new_inst], copy=True)
def test_array_epochs(): """Test creating evoked from array """ tempdir = _TempDir() # creating rng = np.random.RandomState(42) data1 = rng.randn(20, 60) sfreq = 1e3 ch_names = ['EEG %03d' % (i + 1) for i in range(20)] types = ['eeg'] * 20 info = create_info(ch_names, sfreq, types) evoked1 = EvokedArray(data1, info, tmin=-0.01) # save, read, and compare evokeds tmp_fname = op.join(tempdir, 'evkdary-ave.fif') evoked1.save(tmp_fname) evoked2 = read_evokeds(tmp_fname)[0] data2 = evoked2.data assert_allclose(data1, data2) assert_allclose(evoked1.times, evoked2.times) assert_equal(evoked1.first, evoked2.first) assert_equal(evoked1.last, evoked2.last) assert_equal(evoked1.kind, evoked2.kind) assert_equal(evoked1.nave, evoked2.nave) # now compare with EpochsArray (with single epoch) data3 = data1[np.newaxis, :, :] events = np.c_[10, 0, 1] evoked3 = EpochsArray(data3, info, events=events, tmin=-0.01).average() assert_allclose(evoked1.data, evoked3.data) assert_allclose(evoked1.times, evoked3.times) assert_equal(evoked1.first, evoked3.first) assert_equal(evoked1.last, evoked3.last) assert_equal(evoked1.kind, evoked3.kind) assert_equal(evoked1.nave, evoked3.nave) # test kind check assert_raises(TypeError, EvokedArray, data1, info, tmin=0, kind=1) assert_raises(ValueError, EvokedArray, data1, info, tmin=0, kind='mean') # test match between channels info and data ch_names = ['EEG %03d' % (i + 1) for i in range(19)] types = ['eeg'] * 19 info = create_info(ch_names, sfreq, types) assert_raises(ValueError, EvokedArray, data1, info, tmin=-0.01)
def ieegmat2mne(filename_data, fname_trialinfo): "This function converts data from a fieldtrip structure to mne epochs" #Load Matlab/fieldtrip data mat = sio.loadmat(filename_data, squeeze_me=True, struct_as_record=False) ft_data = mat['data_all'] #Identify basic parameters n_trial = np.size(ft_data.wave, 0) data = ft_data.wave #Add all necessary info sfreq = float(ft_data.fsample) #sampling frequency time = ft_data.time chan_names = ft_data.labels.tolist() chan_types = 'ecog' #Create info and epochs info = create_info(chan_names, sfreq, chan_types) if ft_data.project_name == 'Context' or ft_data.project_name == 'Calculia' or ft_data.project_name == 'Calculia_China': n_events = 5 elif ft_data.project_name == 'UCLA' or ft_data.project_name == 'MMR': n_events = 1 elif ft_data.project_name == 'Number_comparison': n_events = 2 events = np.c_[np.cumsum(np.ones(n_trial)) * n_events * sfreq, np.zeros(n_trial), np.zeros(n_trial)] epochs = EpochsArray(data, info, events=np.array(events, int), tmin=np.min(time), verbose=False) # Import trialinfo trialinfo = pd.read_csv(fname_trialinfo) return epochs, trialinfo
def test_array_epochs(): """Test creating epochs from array """ # creating rng = np.random.RandomState(42) data = rng.random_sample((10, 20, 300)) sfreq = 1e3 ch_names = ['EEG %03d' % (i + 1) for i in range(20)] types = ['eeg'] * 20 info = create_info(ch_names, sfreq, types) events = np.c_[np.arange(1, 600, 60), np.zeros(10), [1, 2] * 5] event_id = {'a': 1, 'b': 2} epochs = EpochsArray(data, info, events=events, event_id=event_id, tmin=-.2) # saving temp_fname = op.join(tempdir, 'test-epo.fif') epochs.save(temp_fname) epochs2 = read_epochs(temp_fname) data2 = epochs2.get_data() assert_allclose(data, data2) assert_allclose(epochs.times, epochs2.times) assert_equal(epochs.event_id, epochs2.event_id) assert_array_equal(epochs.events, epochs2.events) # plotting import matplotlib matplotlib.use('Agg') # for testing don't use X server epochs[0].plot() # indexing assert_array_equal(np.unique(epochs['a'].events[:, 2]), np.array([1])) assert_equal(len(epochs[:2]), 2) data[0, 5, 150] = 3000 data[1, :, :] = 0 data[2, 5, 210] = 3000 data[3, 5, 260] = 0 epochs = EpochsArray(data, info, events=events, event_id=event_id, tmin=0, reject=dict(eeg=1000), flat=dict(eeg=1e-1), reject_tmin=0.1, reject_tmax=0.2) assert_equal(len(epochs), len(events) - 2) assert_equal(epochs.drop_log[0], ['EEG 006']) assert_equal(len(events), len(epochs.selection))
print data_type, analysis['name'] # Load data across all subjects data = list() for s, subject in enumerate(subjects): pkl_fname = paths('evoked', subject=subject, data_type=data_type, analysis=analysis['name']) with open(pkl_fname, 'rb') as f: evoked, sub, _ = pickle.load(f) # FIXME evoked = fix_wrong_channel_names(evoked) data.append(evoked.data) epochs = EpochsArray(np.array(data), evoked.info, events=np.zeros((len(data), 3)), tmin=evoked.times[0]) # TODO warning if subjects has missing condition p_values_chans = list() for chan_type in meg_to_gradmag(chan_types): # FIXME: clean this up by cleaning ch_types definition if chan_type['name'] == 'grad': # XXX JRK: With neuromag, should use virtual sensors. # For now, only apply stats to mag and grad. continue elif chan_type['name'] == 'mag': chan_type_ = dict(meg='mag') else: chan_type_ = dict(meg=chan_type['name'] == 'meg', eeg=chan_type['name'] == 'eeg',
# Apply contrast on each type of epoch for analysis in analyses: print analysis['name'] # Load data across all subjects data = list() for s, subject in enumerate(subjects): evoked, sub, _ = load('evoked', subject=subject, analysis=analysis['name']) evoked.pick_types(meg=True, eeg=False) data.append(evoked.data) data = np.array(data) epochs = EpochsArray(data, evoked.info, events=np.zeros((len(data), 3), dtype=int), tmin=evoked.times[0]) # combine grad at subject level grad = mne.pick_types(evoked.info, 'grad') data[:, grad, :] -= analysis['chance'] # combine grad with RMS data[:, grad[::2], :] = np.sqrt((data[:, grad[::2], :] ** 2 + data[:, grad[1::2], :] ** 2) / 2.) data[:, grad[1::2], :] = data[:, grad[::2], :] data[:, grad, :] += analysis['chance'] # keep robust averaging for plotting epochs = EpochsArray(data, evoked.info, events=np.zeros((len(data), 3), dtype=int), tmin=evoked.times[0]) evoked = epochs.average()