-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
31 lines (26 loc) · 940 Bytes
/
utils.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
import numpy as np
def prob_to_sample(probs, batch_size, num_steps):
outputs = []
for i in range(batch_size):
seq = []
for j in range(num_steps):
char = np.random.multinomial(1, probs[i, j]/(np.sum(probs[i,j])+1e-5))
char = np.argmax(char)
seq.append(char)
outputs.append(num_to_string(seq).strip())
return outputs
def num_to_string(num_list):
string_list = []
char_dict = {0: 'A',1: 'G',2: 'C',3: 'T',4: ' ',5:'|',6:' ',7:' '}
for num in num_list:
if num == 6:
break
char = char_dict[num]
string_list.append(char)
if not string_list:
string_list.append(' ')
return ''.join(string_list)
def rolling_window(a, window):
shape = a.shape[:-1] + (a.shape[-1] - window + 1, window)
strides = a.strides + (a.strides[-1],)
return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)