Reading specific channels data from edf file in excel file

329 Views Asked by At

I am reading specific 21 channels data from .edf file from TUH corpus. I am using following code snippet in python.

import numpy as np
import mne
import pandas as pd
import matplotlib.pyplot as plt

raw = mne.io.read_raw_edf('aaaaaibz_s001_t000.edf', preload=True)

edf = raw.pick_channels(['EEG FP1-REF','EEG FP2-REF','EEG F3-REF','EEG F4-REF','EEG C3- 
REF','EEG C4-REF','EEG P3-REF','EEG P4-REF','EEG O1-REF','EEG O2-REF','EEG F7-REF','EEG F8- 
REF','EEG T3-REF','EEG T4-REF','EEG T5-REF','EEG T6-REF','EEG A1-REF','EEG A2-REF','EEG FZ- 
REF','EEG CZ-REF','EEG PZ-REF']) # list of common 21 channel names

x = edf.get_data()

trans = np.transpose(x)
my_df = pd.DataFrame(trans)  # converting it to a pandas dataframe
my_df.to_excel('ne1-ibz.xlsx')

This dataset has multiple edf files with different channels and sequence of channels is also different. In different excel file (for different edf files), I am getting channels in different sequence. I want all excel file should have channels data in same sequence. i.e. I want to read channels in fixed sequence every time. also I want channel names in first row in excel file. How to do this?

Link for two edf files with different channel sequence https://drive.google.com/drive/folders/1XQj890SLmwhn7ADkDr8Txax2xZEFCN6F?usp=share_link

1

There are 1 best solutions below

0
DarthSwagger On

If I'm reading your question correctly, you're loading a bunch of channels in your already specified channel order? Since you're transposing later on, it looks like you're getting the data from channels in rows? If so, I believe this will address your question. It could be combined to be fewer lines, but spaced out for clarity.

df = pd.DataFrame([[1,2],[3,4], [3,1], [7,4], [8, 0]])
columns = ['col1', 'col2', 'col3', 'col4', 'col5']

This is what I imagine your original data looks like. The columns is your list of channel names that you're using to pull data from the file. The df is your data in the channel order, since that's the order you pulled it from the file.

df.index = columns

This makes the channels you used to pull be linked to the data you pulled. Example:

Out: 
      0  1
col1  1  2
col2  3  4
col3  3  1
col4  7  4
col5  8  0

Would use this to transpose the data to the table that you actually want with the channels set to be column names.

df = df.transpose()
df2 = df[['col1', 'col5', 'col3']]

You can then make a new dataframe that has only the channels you actually want and in the right order.

In: df2
Out: 
   col1  col5  col3
0     1     8     3
1     2     0     1

After that it's just your normal df2.to_excel('whatever.xlsx') that will automatically save those column names as headers.