-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_files.py
265 lines (209 loc) · 6.95 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
from sklearn.preprocessing import power_transform
import umap
from scipy.stats.stats import pearsonr
from sklearn.metrics.pairwise import cosine_similarity
from scipy import stats
from statsmodels.stats.multitest import multipletests
import pandas as pd
from sklearn.decomposition import PCA
from collections import defaultdict
from sklearn.preprocessing import MinMaxScaler
import os, sys
import numpy as np
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 remove_not_obs_ordinal(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):
matrix = np.array(raw_input)
tmp_matrix = np.copy(matrix)
tmp_matrix[np.isnan(tmp_matrix)] = 0
# remove less than 1% observations
mask_col = np.count_nonzero(tmp_matrix, 0) > (tmp_matrix.shape[0] * p)
std = np.nanstd(matrix, axis=0)
mask_col &= std != 0
# z-score normalize
mean = np.nanmean(matrix, axis=0)
std = np.nanstd(matrix, axis=0)
data_input = matrix
data_input -= mean
data_input /= std
data_input[np.isnan(data_input)] = 0
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 1% 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 encode_cat(raw_input, num_classes=None, uniques=None, na='NA'):
matrix = np.array(raw_input)
n_labels = matrix.shape[1]
n_samples = matrix.shape[0]
# make endocding dict
encodings = defaultdict(dict)
count = 0
no_unique = 0
if uniques is None:
no_unique = 1
encodings = defaultdict(dict)
for lab in range(0,n_labels):
uniques = np.unique(matrix[:,lab])
uniques = sorted(uniques)
num_classes = len(uniques[uniques != na])
count = 0
for u in uniques:
if u == na:
encodings[lab][u] = np.zeros(num_classes)
continue
encodings[lab][u] = np.zeros(num_classes)
encodings[lab][u][count] = 1
count += 1
else:
for u in uniques:
if u == na:
encodings[u] = np.zeros(num_classes)
continue
encodings[u] = np.zeros(num_classes)
encodings[u][count] = 1
count += 1
# encode the data
data_input = np.zeros((n_samples,n_labels,num_classes))
i = 0
for patient in matrix:
data_sparse = np.zeros((n_labels, num_classes))
count = 0
for lab in patient:
if no_unique == 1:
data_sparse[count] = encodings[count][lab]
else:
if lab != na:
lab = int(float(lab))
data_sparse[count] = encodings[lab]
count += 1
data_input[i] = data_sparse
i += 1
return data_input.astype(np.float32)
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 measurments
catsum = cat_all.sum(axis=1)
mask = catsum > 1
del catsum
return cat_shapes, mask, cat_all
def concat_cat_list_ordinal(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)
cat_input = np.argmax(cat_d, 2)
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 measurments
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
def get_categories(sorted_data):
sorted_data_cat = np.copy(sorted_data.astype(str))
mask = list()
for col in range(sorted_data.shape[1]):
tmp = sorted_data[:,col]
if len(tmp[tmp != 0]) == 0:
mask.append(False)
sorted_data_cat[:,col] = tmp
continue
mask.append(True)
new_cat = np.array(pd.qcut(tmp[tmp != 0], 4, labels=False, duplicates = 'drop'))
new_cat = new_cat + 1
tmp[tmp != 0] = new_cat
tmp_new = np.copy(tmp).astype(str)
sorted_data_cat[:,col] = tmp
return sorted_data_cat, mask