xbrainnet / CAFNet

IEEE Transactions on Affective Computing 2023
15 stars 4 forks source link

About DEAP dataset preprocessing #4

Closed logicvanlyf closed 3 months ago

logicvanlyf commented 5 months ago

After browsing the code, I found that the two inputs and labels of deap were processed into numpy arrays. Could you please tell me how to get from the original deap data set to these three arrays? Could you please provide the relevant codes? Looking forward to your reply, thank you very much.

kaio-99 commented 5 months ago

Thank you for your interest in our work. Here is an example of processing pipeline using MAHNOB-HCI dataset, you can make minor changes to migrate to DEAP dataset and set looping conditions to batch process all data.

raw = mne.io.read_raw_bdf(file_path, preload=True, exclude=['Status'])        
# get the total length of EEG signal
total_duration = raw.times[-1]

# calculate the time range
start_time = total_duration - 30  
end_time = total_duration      

if start_time < 0:
    break;
raw_last_30s = raw.copy().crop(tmin=start_time, tmax=end_time)

# choose the bands
freq_ranges = {'4-8Hz': (4, 8),
            '8-10Hz': (8, 10),
            '8-12Hz': (8, 12),
            '12-30Hz': (12, 30),
            '30-40Hz': (30, 40)}

# define the window size, number of windows, and overlap ratio
win_size_sec = 3
win_size = int(win_size_sec * raw_last_30s.info['sfreq']) 
overlap = 0 

n_channels = len(raw_last_30s.info['ch_names'])
n_samples = raw_last_30s.n_times
n_windows = int(np.floor((n_samples - win_size) / (win_size - overlap))) + 1

freq_features = np.zeros((len(freq_ranges), n_channels, n_windows))
for i in range(n_windows):
    start = i * (win_size - overlap)
    end = start + win_size
    for ch_idx in range(n_channels):
        data = raw_last_30s.get_data(picks=ch_idx, start=start, stop=end)[0]
        freqs, psd = welch(data, fs=raw_last_30s.info['sfreq'], nperseg=win_size, noverlap=overlap)
        for f_idx, (f_name, f_range) in enumerate(freq_ranges.items()):
            f_mask = (freqs >= f_range[0]) & (freqs <= f_range[1])
            freq_features[f_idx, ch_idx, i] = np.mean(psd[f_mask])
feat = freq_features.reshape(1600)