Skip to content

Instantly share code, notes, and snippets.

@pravsripad
Created September 22, 2016 07:46
Show Gist options
  • Save pravsripad/d1608ee5885b763aa08c87652e109ac8 to your computer and use it in GitHub Desktop.
Save pravsripad/d1608ee5885b763aa08c87652e109ac8 to your computer and use it in GitHub Desktop.
Script to read raw data, epoch it into arbitrary events and save it.
#!/usr/bin/env python
'''
Script to read raw data, epoch it into arbitrary events and save it.
- pravsripad@gmail.com
'''
import numpy as np
import mne
empty_fname = '109925_CAU01A_100715_0844_2_c,rfDC,bp1-45Hz-empty.fif'
# read the file into mne python object
raw = mne.io.Raw(empty_fname, preload=True)
# to plot and have a look at the data
# you can also mark channels as bad by clicking on them
# raw.plot()
# check if any bad channels are marked
print raw.info['bads']
# pick the channels of interest
# we are interested in meg channels only (there are 248 MEG channels)
picks = mne.pick_types(raw.info, meg=True, exclude='bads')
# now read the MEG data only into a numpy array
raw_data, times = raw[picks, :]
# check shape of data
# here 248 is the number of channels and 122412 are the number of time points
print raw_data.shape, times.shape
# 122412 time points is approx 120s
print times.shape[0] / raw.info['sfreq']
# make artificial events to chop the data into 10 second epochs
# so we get 12 pieces approx
events = mne.make_fixed_length_events(raw, 42, start=0, stop=None, duration=10.)
# actually the do the epoching
epochs = mne.Epochs(raw, events, event_id=42, tmin=-5, tmax=5, picks=picks)
# extract the data from epochs into numpy array
epochs_data = epochs.get_data()
# here (11, 248, 10173) means 12 epochs, 248 channels, 10173 time points each
print epochs_data.shape
# save the chopped array
np.save('epochs_data.npy', epochs_data)
# to read the data
# epochs_data = np.load('epochs_data.npy')
# do machine learning voodoo below
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment