forked from sogNok/SNN-with-KIST
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintrapre.py
122 lines (94 loc) · 2.87 KB
/
intrapre.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import wfdb
import matplotlib.pyplot as plt
import os
import os.path
import numpy as np
import torch
'''
record_data_dic = wfdb.rdrecord("1.0.0/800", channels=[0])
record_data = record_data_dic.__dict__
ecg = record_data["p_signal"]
annotation_dic = wfdb.rdann("1.0.0/800", "atr")
annotation = annotation_dic.__dict__
R = annotation["sample"]
label = annotation["symbol"]
'''
for idx in range(800, 900):
path=str("1.0.0/"+str(idx))
print(path)
if os.path.isfile(path+".atr"): record_data_dic = wfdb.rdrecord(path, channels=[0])
else: continue
record_data = record_data_dic.__dict__
data = torch.zeros(1, 1280, dtype=torch.uint8)
label = torch.zeros(1, dtype=torch.int64)
ecg = record_data["p_signal"]
'''
# min_max Normalization
Emin = np.min(ecg)
Emax = np.max(ecg)
Emax = Emax-Emin
ecg = (ecg - Emin) / Emax
ecg = ecg * 255
'''
# sampling by 10s
ecg = ecg.reshape(-1, 1280)
annotation_dic = wfdb.rdann(path, "atr")
annotation = annotation_dic.__dict__
R = annotation["sample"]
R = np.append(R, R[-1]+1280) # for count last label
ecg = torch.from_numpy(ecg)
ecg = ecg.to(torch.uint8)
# for labeling
last_idx = 0
last_count = 0
for i, peak in enumerate((R)):
if int(peak / 1280) > last_count:
rate = (i - last_idx) * 6
if rate < 60: rate = 0
elif rate < 70: rate = 1
elif rate < 80: rate = 2
elif rate < 90: rate = 3
elif rate < 100: rate = 4
else: rate = 5
last_count += 1
last_idx = i
rate_tensor = torch.tensor([rate], dtype=torch.int64)
label = torch.cat([label, rate_tensor], dim=0)
if(idx == 860) : label = label[:-1] # sub 860 has R peak when 00:30:00(last)
data = torch.cat([data,ecg], dim=0)
data = data[1: ,]
label = label[1:]
training = (data[:130,], label[:130])
test = (data[130: ,], label[130:])
torch.save(training, os.path.join('.','intrasub','tr'+str(idx)+'.pt'))
torch.save(test, os.path.join('.','intrasub','te'+str(idx)+'.pt'))
'''
print(training[0].shape)
print(training[1].shape)
print(test[0].shape)
print(test[1].shape)
c1=0
c2=0
c3=0
c4=0
c5=0
for idx, val in enumerate((training[1][:250])):
if val == 0: c0+=1
elif val == 1: c1+=1
elif val == 2: c2+=1
elif val == 3: c3+=1
elif val == 4: c4+=1
else: c5+=1
print("0: ",c0," 1: ",c1," 2: ",c2," 3: ",c3," 4: ",c4," 5: ",c5)
print(training[1][:250])
print(training[0][0][:128])
print(training[0][0][128:256])
print(training[0][0][256:384])
print(training[0][0][384:512])
print(training[0][0][512:640])
print(training[0][0][640:768])
print(training[0][0][768:896])
print(training[0][0][896:1024])
print(training[0][0][1024:1152])
print(training[0][0][1152:1280])
'''