-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathutils_helper.py
executable file
·63 lines (49 loc) · 2.73 KB
/
utils_helper.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
import numpy as np
import random
##### USER-DEFINED FUNCTIONS
def f_get_fc_mask1(meas_time, num_Event, num_Category):
'''
mask1 is required to get the contional probability (to calculate the denominator part)
mask1 size is [N, num_Event, num_Category]. 1's until the last measurement time
'''
mask = np.zeros([np.shape(meas_time)[0], num_Event, num_Category]) # for denominator
for i in range(np.shape(meas_time)[0]):
mask[i, :, :int(meas_time[i, 0]+1)] = 1 # last measurement time
return mask
def f_get_minibatch(mb_size, x, x_mi, label, time, mask1, mask2, mask3):
idx = range(np.shape(x)[0])
idx = random.sample(idx, mb_size)
x_mb = x[idx, :, :].astype(float)
x_mi_mb = x_mi[idx, :, :].astype(float)
k_mb = label[idx, :].astype(float) # censoring(0)/event(1,2,..) label
t_mb = time[idx, :].astype(float)
m1_mb = mask1[idx, :, :].astype(float) #fc_mask
m2_mb = mask2[idx, :, :].astype(float) #fc_mask
m3_mb = mask3[idx, :].astype(float) #fc_mask
return x_mb, x_mi_mb, k_mb, t_mb, m1_mb, m2_mb, m3_mb
def f_get_boosted_trainset(x, x_mi, time, label, mask1, mask2, mask3):
_, num_Event, num_Category = np.shape(mask1) # dim of mask3: [subj, Num_Event, Num_Category]
meas_time = np.concatenate([np.zeros([np.shape(x)[0], 1]), np.cumsum(x[:, :, 0], axis=1)[:, :-1]], axis=1)
total_sample = 0
for i in range(np.shape(x)[0]):
total_sample += np.sum(np.sum(x[i], axis=1) != 0)
new_label = np.zeros([total_sample, np.shape(label)[1]])
new_time = np.zeros([total_sample, np.shape(time)[1]])
new_x = np.zeros([total_sample, np.shape(x)[1], np.shape(x)[2]])
new_x_mi = np.zeros([total_sample, np.shape(x_mi)[1], np.shape(x_mi)[2]])
new_mask1 = np.zeros([total_sample, np.shape(mask1)[1], np.shape(mask1)[2]])
new_mask2 = np.zeros([total_sample, np.shape(mask2)[1], np.shape(mask2)[2]])
new_mask3 = np.zeros([total_sample, np.shape(mask3)[1]])
tmp_idx = 0
for i in range(np.shape(x)[0]):
max_meas = np.sum(np.sum(x[i], axis=1) != 0)
for t in range(max_meas):
new_label[tmp_idx+t, 0] = label[i,0]
new_time[tmp_idx+t, 0] = time[i,0]
new_x[tmp_idx+t,:(t+1), :] = x[i,:(t+1), :]
new_x_mi[tmp_idx+t,:(t+1), :] = x_mi[i,:(t+1), :]
new_mask1[tmp_idx+t, :, :] = f_get_fc_mask1(meas_time[i,t].reshape([-1,1]), num_Event, num_Category) #age at the measurement
new_mask2[tmp_idx+t, :, :] = mask2[i, :, :]
new_mask3[tmp_idx+t, :] = mask3[i, :]
tmp_idx += max_meas
return(new_x, new_x_mi, new_time, new_label, new_mask1, new_mask2, new_mask3)