-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataGenerator.py
74 lines (59 loc) · 2.14 KB
/
DataGenerator.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
import numpy as np
import keras
from tensorflow.keras.utils import Sequence
class DataGenerator(Sequence):
#generates data without creating RAM error
def __init__(self, data, label, shuffle=True, fit=True):
self.data = data
self.label = label
#add reverse complement
print("creating completes")
seq_counter = 0
comp_seqs = []
complement = {'A': 'T', 'T': 'A', 'G': 'C', 'C': 'G', 'N' : 'N'}
for seq in self.data:
if seq_counter % 100000 == 0:
print(f"complemented {seq_counter} of {self.data.shape[0]}")
comp_seq = ''
for base in seq:
comp_seq += complement[base]
comp_seqs.append(comp_seq)
seq_counter += 1
print("finsihed complements")
np_comp_seqs = np.array(comp_seqs, dtype=object)
self.data = np.append(self.data, np_comp_seqs, axis=0)
self.label = np.append(self.label, self.label, axis=0)
self.shuffle = shuffle
self.on_epoch_end()
self.fit = fit
def __len__(self):
#tells the number of batches in each epoch
return self.data.shape[0]
def __getitem__(self, index):
#generate one batch of data
#get indexes of the batch
label = self.label[index]
seq = self.data[index]
row_index = 0
feature = np.zeros((len(seq),4))
for base in seq:
if base == 'A':
feature[row_index, 0] = 1
elif base == 'T':
feature[row_index, 1] = 1
elif base == 'G':
feature[row_index, 2] = 1
elif base == 'C':
feature[row_index, 3] = 1
row_index += 1
feature = np.array([feature], dtype='float32')
if(self,self.fit):
return feature,label
else:
return feature
def on_epoch_end(self):
pass
#update the indexes after each epoch
self.indexes = np.arange(self.data.shape[0])
if self.shuffle == True:
np.random.shuffle(self.indexes)