-
Notifications
You must be signed in to change notification settings - Fork 0
/
EDF_extract.py
60 lines (53 loc) · 1.98 KB
/
EDF_extract.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
import pyedflib
import numpy as np
import matplotlib.pyplot as plt
import os
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
import joblib
#Samples are represented in 16-bit 2's complement
# Creating a MinMaxScaler object
scaler = MinMaxScaler()
# Get the directory where the script is located
script_dir = os.path.dirname(__file__)
# Construct the full path to the file
file_path = os.path.join(script_dir, 'EDF', 'short.edf')
signalList = []
signalListNorm = []
f = pyedflib.EdfReader(file_path)
n = f.signals_in_file
n=n-14
signal_labels = f.getSignalLabels()
sample_f = f.getSampleFrequencies()
number_of_samples = f.getNSamples()[0]
Nblocks = int(number_of_samples/64)
sigbufs = np.zeros(number_of_samples)
full_data64 = np.ndarray(shape=(Nblocks*n, 64, 1))
X = np.zeros(n*number_of_samples)
maxList = f.getDigitalMaximum()
minList = f.getDigitalMinimum()
for i in np.arange(n):
sigbufs[:] = f.readSignal(i, digital=False)
signalList.append(f.readSignal(i, digital=False))
# Normalizing the list
X[i*number_of_samples:(i+1)*number_of_samples] = sigbufs[:]
signalListNorm.append(scaler.fit_transform(sigbufs[:].reshape(-1, 1)))
for j in np.arange(Nblocks):
full_data64[i*Nblocks+j] = signalListNorm[i][j*64:(j+1)*64]
train_data, test_data = train_test_split(full_data64, test_size=0.1, random_state=42)
joblib.dump(scaler, 'scaler.pkl') #save the normalisation parameters for running inference
print(train_data.shape, test_data.shape)
print(maxList)
print(minList)
for j in range(3):
plt.figure(j+1)
# Loop through each subplot and plot some data
fig, axs = plt.subplots(7, 1, figsize=(10, 20), sharex=True)
for i in range(7):
axs[i].plot(signalList[(j)*7+i])
axs[i].set_title(f'CH {signal_labels[(j)*7+i]}')
# Adjust layout
plt.tight_layout()
# Show the plot
plt.show()
f.close()