-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1_MIT_BIH2img.py
78 lines (60 loc) · 1.94 KB
/
1_MIT_BIH2img.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
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
import cv2
train_df = pd.read_csv(r"mitbih_train.csv", header = None)
test_df = pd.read_csv(r"mitbih_test.csv", header = None)
# print(train_df.head(3))
# print(train_df.info())
# print(train_df[187].value_counts())
train_X = train_df.drop(187, axis = 1)
train_Y = train_df[187]
train_Y = train_Y.to_numpy()
# print(train_Y.dtype)
# print(test_df.info())
test_X = test_df.drop(187, axis = 1)
test_Y = test_df[187]
test_Y = test_Y.to_numpy()
x = []
print(train_X.shape)
for i in range(187):
x.append(i)
# The image size was set to be 187 x 187
train_image = np.zeros((87553, 187, 187))
test_image = np.zeros((21892, 187, 187))
plt.figure()
# This part plots the ECG signal, then save it, loads it again to make is a dataset in numpy array
for i in range(0, 87554):
if i % 100 == 0:
print(i)
plt.plot(x, train_X.loc[i,:])
plt.savefig("test_temp_pulse.png")
plt.clf()
print(plt.rcParams["figure.figsize"])
img = cv2.imread("test_temp_pulse.png", cv2.IMREAD_GRAYSCALE)
print(img.shape)
img = img[61:424, 82:574]
img = cv2.resize(img, (187,187))
train_image[i] = img/256.0 # normalize value to make it 0-1
if i == 0:
break
for i in range(0, 21892):
if i % 100 == 0:
print(i)
plt.plot(x, test_X.loc[i,:])
plt.savefig("temp_pulse_test.png")
plt.clf()
img = cv2.imread("temp_pulse_test.png", cv2.IMREAD_GRAYSCALE)
img = img[61:424, 82:574]
img = cv2.resize(img, (187,187))
test_image[i] = img/256.0
# Shuffle
train_sequence = np.arange(train_X.shape[0])
np.random.shuffle(train_sequence)
train_X = train_X[train_sequence]
train_Y = train_Y[train_sequence]
# Save Dataset
np.save("train_image.npy", train_image)
np.save("test_image.npy", test_image)
np.save("train_label.npy", train_Y)
np.save("test_label.npy", test_Y)