-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathOpening and sorting the files.py
92 lines (76 loc) · 3.32 KB
/
Opening and sorting the files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import numpy as np
import mne
from os.path import exists
from sklearn import preprocessing
# Define the number of healthy and MDD (Major Depressive Disorder) cases
H_num = # Number of healthy cases
MDD_num = # Number of MDD cases
# Initialize lists to store data and labels
v = []
Data = []
Label = []
# Define the categories for healthy and MDD cases
HorMDD = ['H', 'MDD']
# Define the eye states: Open (O) and Closed (C)
eye = ['O', 'C']
# Iterate over each category (healthy and MDD)
for x in HorMDD:
# Iterate over each eye state
for y in eye:
# Process healthy cases
if x == 'H':
for i in range(1, H_num):
# Check if the file exists
if exists('/your directory/H S{} E{}.edf'.format(i, y)):
file = '/your directory/H S{} E{}.edf'.format(i, y)
# Read the EEG data from the .edf file
data = mne.io.read_raw_edf(file)
raw_data = data.get_data()
num_rows, num_cols = raw_data.shape
# Ensure the number of rows is correct
if num_rows > 19:
print(num_rows)
raw_data = np.delete(raw_data, 19, 0)
# Ensure the data length is sufficient
if len(raw_data[1]) < 61440:
v.append(file)
continue
# Trim the data to the desired length
raw_data = raw_data[:, :61440]
Data.append(raw_data)
Label.append(0) # Label 0 for healthy cases
# Save the processed data and labels
np.save('/your directory/Data/H S{} E{}.npy'.format(i, y), raw_data)
np.save('/your directory/Label/H S{} E{}.npy'.format(i, y), 0)
else:
continue
# Process MDD cases
if x == 'MDD':
for j in range(1, MDD_num):
# Check if the file exists
if exists('/your directory/MDD S{} E{}.edf'.format(j, y)):
file = '/your directory/MDD S{} E{}.edf'.format(j, y)
# Read the EEG data from the .edf file
data = mne.io.read_raw_edf(file)
raw_data = data.get_data()
num_rows, num_cols = raw_data.shape
# Ensure the number of rows is correct
if num_rows > 19:
print(num_rows)
raw_data = np.delete(raw_data, 19, 0)
# Ensure the data length is sufficient
if len(raw_data[1]) < 61440:
v.append(file)
continue
# Trim the data to the desired length
raw_data = raw_data[:, :61440]
Data.append(raw_data)
Label.append(1) # Label 1 for MDD cases
# Save the processed data and labels
np.save('/your directory/Data/MDD S{} E{}.npy'.format(j, y), raw_data)
np.save('/your directory/Label/MDD S{} E{}.npy'.format(j, y), 1)
else:
continue
# Convert the lists to numpy arrays
Data = np.asarray(Data)
Label = np.asarray(Label)