pythontimewhile-loopeuropean-data-format

Python: How do I add a While time loop code to a EEG CSV export for better clarity


I am currently having trouble with my EEG data that I have from the Siena Scalp Database from physio. The EEG data that I have has around 30 channels and a text file that displays when a seizure occurred. I have written some code that exports the results into a excel file with the correct channel names, but I do not have a time record. [This is my code for exporting the EEG data to CSV][1]

Code Exporting EEG Data

import numpy as np

import mne
 
edf = mne.io.read_raw_edf(r"Path name from external drive\PN00\PN00-1.edf")

np.savetxt('PN00-1Testv2.csv', edf.get_data().T, delimiter=',')

I have created a simple time code, but I do not know how to input that code into my current code. If anyone could help me out that would be great [This is my Time loop code:][2]

Time Loop Code

import datetime

import numpy as np

import mne

import pandas as pd

import xlwt

import openpyxl as op


Time_start = datetime.datetime(100,1,1,19,39,33,00) 

Time_increment = datetime.timedelta(milliseconds=0.00195)


x_TimeEnd = datetime.datetime(100,1,1,20,22,58) 


while Time_start <= x_TimeEnd:

    Time_start += datetime.timedelta(milliseconds=0.00195)

    print(Time_start)

The above code loops until Time_start is less than or equal to x_TimeEnd. I am hoping to get something like this into my excel output so that for each record of EEG data I have a corresponding Time output.

The sampling rate of the EEG Data is 512Hz And the channels in the EDF Files are Channel 1: Fp1 Channel 2: F3 Channel 3: C3 Channel 4: P3 Channel 5: 1 Channel 6: F7
Channel 7: T3 Channel 8: T5 Channel 9: Fc1 Channel 10: Fc5 Channel 11: Cp1
Channel 12: Cp5 Channel 13: F9 Channel 14: Fz Channel 15: Cz Channel 16: Pz
Channel 17: Fp2 Channel 18: F4 Channel 19: C4 Channel 20: P4 Channel 21: O2
Channel 22: F8 Channel 23: T4 Channel 24: T6 Channel 25: Fc2 Channel 26: Fc6
Channel 27: Cp2 Channel 28: Cp6 Channel 29: F10 Channel 33: EKG 1
Channel 34: EKG 2

Please excuse any formatting errors as this is my first time using Stack Overflow. I have included images in the hopes that it would [1]: https://i.sstatic.net/O38tF.png [2]: https://i.sstatic.net/PnZkg.png


Solution

  • I assume that the edf files already have timestamps which can be read using the mne module. Therefore, I would propose to make use of it instead of generating new timestamps. The mne.io.Raw method to_data_frame converts the object to a pandas data frame and allows you to specify the time format.

    For example:

    import mne
    edf = mne.io.read_raw_edf(r"Path name from external drive\PN00\PN00-1.edf")
    data = edf.to_data_frame(index="time", time_format="datetime")
    data.to_csv("PN00-1Testv3.csv")
    

    The "time_format" parameter takes None, "ms", "datetime", or "timedelta" as a value. For more information, I would like to recommend reading the documentation.