-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeatsWpos.py
83 lines (68 loc) · 2.22 KB
/
beatsWpos.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
import tensorflow as tf
import numpy as np
import os
import gc
import time
NORM = np.load('Heartbeats/Normal/N_beats.npy',allow_pickle = True)
ARRH = np.load('Heartbeats/Arrhythmia/A_beats.npy',allow_pickle = True)
norm_p = r'./Folder1/norm/'
arrh_p = r'./Folder1/arrh/'
print('loaded')
NORM = list(NORM)
ARRH = list(ARRH)
print('listed')
NORM = tf.ragged.constant(NORM)
ARRH = tf.ragged.constant(ARRH)
print('ragged')
arrh_labels = np.array(tf.cast(np.array(np.ones(int(ARRH.shape[0]))),tf.float16))
norm_labels = np.array(tf.cast(np.array(np.zeros(int(NORM.shape[0]))),tf.float16))
def one_hot(beat, d_model):
if type(d_model) != int:
raise TypeError("d_model must be an int type")
encodings = []
beat = tf.cast((tf.cast(beat,tf.float16)*int(d_model)),tf.int64)
# print(beat.dtype) # troubleshooting
for i in beat:
# i = tf.round(i)
i = tf.cast(i,tf.int32)
# print(i,i.dtype)
if i == 0:
encodings.append(list(np.zeros(d_model)))
else:
encodings.append(list(np.zeros(i-1)) + list(np.ones(1)) + list(np.zeros(d_model-i)))
return encodings
def input_embeddings(encodings,max_sequence_length,d_model):
temp = int(max_sequence_length) - int(len(list(encodings)))
for i in range(temp):
encodings.append(list(np.zeros(int(d_model))))
return encodings
os.system('cls')
t1 = time.time()
#163323
count = 0
for i in NORM:
temp = np.array(one_hot(i,512)).astype(dtype=np.int8)
np.save(norm_p + str(count),temp)
count += 1
if count % 1000 == 0:
print(count)
# print estimated time for completion
t2 = time.time()
print('Estimated time for completion NORM: ',(t2-t1)*(130665/1000)/60,' minutes')
t1 = time.time()
del temp
gc.collect()
count = 0
for i in ARRH:
temp = np.array(one_hot(i,512)).astype(dtype=np.int8)
np.save(arrh_p + str(count),temp)
count += 1
if count % 1000 == 0:
print(count)
# print estimated time for completion
t2 = time.time()
print('Estimated time for completion ARRH: ',(t2-t1)*(163323/1000)/60,' minutes')
t1 = time.time()
del temp
gc.collect()
print('------------------ Done ------------------')