-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_files.py
139 lines (105 loc) · 3.54 KB
/
read_files.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import os, sys
import numpy as np
from sklearn.preprocessing import MinMaxScaler
def read_cat(file):
data = np.load(file)
data = data.astype(np.float32)
data_input = data.reshape(data.shape[0], -1)
data_label = np.argmax(data, 2)
data_label = data_label.astype('float')
data_label[data.sum(2) == 0] = -1
data_label = data_label + 1
return data, data_label
def read_con(file):
data = np.load(file)
data = data.astype(np.float32)
return data
def read_header(file, mask=None):
with open(file, "r") as f:
h = list()
for line in f:
h.append(line.rstrip())
if not mask is None:
h = np.array(h)
h = h[mask]
return h
def remove_not_obs_cat(pheno, ind, h, p=0.01):
pheno = pheno[:,~np.all(ind == ind[0,:], axis = 0)]
h = np.array(h)[~np.all(ind == ind[0,:], axis = 0)]
ind = ind[:,~np.all(ind == ind[0,:], axis = 0)]
ind_tmp = np.copy(ind)
ind_tmp[ind_tmp == 1] = 0
pheno = pheno[:,np.count_nonzero(ind_tmp, 0) > (pheno.shape[0] * p)]
h = h[np.count_nonzero(ind_tmp, 0) > (pheno.shape[0] * p)]
ind = ind[:,np.count_nonzero(ind_tmp, 0) > (pheno.shape[0] * p)]
return pheno, ind, h
def encode_con(raw_input, p = 0.01, min_v = 0, max_v = 1):
matrix = np.array(raw_input)
tmp_matrix = np.copy(matrix)
tmp_matrix[np.isnan(tmp_matrix)] = 0
# remove less than p% observations
mask_col = np.count_nonzero(tmp_matrix, 0) > (tmp_matrix.shape[0] * p)
scaler = MinMaxScaler((min_v, max_v))
data_input = scaler.fit_transform(matrix)
# remove 0 variance
std = np.nanstd(data_input, axis=0)
mask_col &= std != 0
mean = np.nanmean(data_input, axis = 0)
data_input = np.where(np.isnan(matrix), mean, data_input)
data_input = data_input[:,mask_col]
return data_input, mask_col
def encode_binary(raw_input, p = 0.01):
matrix = np.array(raw_input)
tmp_matrix = np.copy(matrix)
tmp_matrix[np.isnan(tmp_matrix)] = 0
# remove less than p% observations
mask_col = np.count_nonzero(tmp_matrix, 0) > (tmp_matrix.shape[0] * p)
data_input = tmp_matrix
# remove 0 variance
std = np.nanstd(data_input, axis=0)
mask_col &= std != 0
data_input = data_input[:,mask_col]
data_input[data_input != 0] = 1
data_input[np.isnan(matrix[:,mask_col])] = np.nan
data_input = encode_cat(data_input, num_classes = 2, uniques = [0,1], na = np.nan)
data_label = np.argmax(data_input, 2)
data_label = data_label.astype('float')
data_label[data_input.sum(2) == 0] = -1
data_label = data_label + 1
return data_input, data_label, mask_col
def concat_cat_list(cat_list):
n_cat = 0
cat_shapes = list()
first = 0
for cat_d in cat_list:
cat_shapes.append(cat_d.shape)
cat_input = cat_d.reshape(cat_d.shape[0], -1)
if first == 0:
cat_all = cat_input
del cat_input
first = 1
else:
cat_all = np.concatenate((cat_all, cat_input), axis=1)
# Make mask for patients with no measurements
catsum = cat_all.sum(axis=1)
mask = catsum > 1
del catsum
return cat_shapes, mask, cat_all
def concat_con_list(con_list, mask=[]):
n_con_shapes = []
first = 0
for con_d in con_list:
con_d = con_d.astype(np.float32)
n_con_shapes.append(con_d.shape[1])
if first == 0:
con_all = con_d
first = 1
else:
con_all = np.concatenate((con_all, con_d), axis=1)
consum = con_all.sum(axis=1)
if len(mask) == 0:
mask = consum != 0
else:
mask &= consum != 0
del consum
return n_con_shapes, mask, con_all