pennmem / ptsa

PTSA - Python Time Series Analysis
https://pennmem.github.io/ptsa/
GNU General Public License v3.0
20 stars 9 forks source link

Implement converter of MNE Python *.fif format to PTSA TimeSeriesX #59

Closed maciekswat closed 6 years ago

jpazdera commented 7 years ago

Here is a code sample to demonstrate some basic MNE functionality:

import mne

# Read data from one of our raw EGI recording files. EOG channels are specified here.
raw = mne.io.read_raw_egi('/data/eeg/scalp/ltp/example_data/LTP123 20140912 1452.1.raw', eog=['EEG 008', 'EEG 025', 'EEG 126', 'EEG127'], preload=True)
# We can plot the time series with raw.plot()... ooh, pretty!
raw.plot()

# Create a projection that can be used later to apply a common average reference
raw.set_eeg_reference(ref_channels=None)
# We can apply a filter with raw.filter(). Here we apply a 1 Hz high pass filter.
raw.filter(1, None)
# We can see metadata about the recording (including our filter and projection) in raw.info
print raw.info
# If we just want to get the time series as a numpy array, we can use raw.get_data()
# Note that we can specify which channels we want, as well as what range of samples 
x = raw.get_data(picks=None, start=20000, stop = 25000)

# We can save the Raw object to a .fif file, then load it back in
save_path = '/data/eeg/scalp/ltp/example_data/LTP123_4_raw.fif'
raw.save(save_path, fmt='single', overwrite=True)
raw = None
raw = mne.io.read_raw_fif(save_path, preload=True)

# We can apply the common average reference we created earlier (this modifies raw in place)
raw.apply_proj()
# If we look at raw.info again, we can see that Average EEG reference is now on
print raw.info
# Note that this method of loading the data and applying the re-reference projection would
# replace the current system of loading re-referenced channel files!
jpazdera commented 7 years ago

To add an example of ICA as it will be used in the pipeline, assume we have just run the code above; then we run the following:

# Initialize the ICA object and run on our Raw object (NOTE: THIS CAN TAKE OVER AN HOUR!)
ica = mne.preprocessing.ICA(method='fastica')
ica.fit(raw, picks=mne.pick_types(raw.info, eeg=True, eog=True))
# Detect blink components based on their correlation with an EOG channel
ica.exclude = ica.find_bads_eog(raw, threshold=3.)[0]
# Save ICA solution as a .fif file
ica_path = '/data/eeg/scalp/ltp/example_data/LTP123_4-ica.fif'
ica.save(ica_path)

# Load our saved ICA object
ica = mne.preprocessing.read_ica(ica_path)
# Remove the components we previously marked for exclusion
ica.apply(raw)

Our Raw object has now (hopefully!) been cleaned of blinks. The option to load the ICA solution and apply it to our re-referenced time series could be added as a parameter to the proposed PTSA "read_mne" function. In other words we could have three load options - load non-re-referenced data, load re-referenced data, and load re-referenced and ICA cleaned data. (ICA will be run on the re-referenced data, so the data will need the common average reference applied before applying ICA!)

mivade commented 6 years ago

I'm closing this since we should focus on loading data with cmlreaders. When loading EEG data there, it returns a simple container type which has methods to convert to PTSA or MNE formats.