forked from AllenCellModeling/pytorch_integrated_cell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_variation_model_structure_sampled.py
259 lines (166 loc) · 7.04 KB
/
print_variation_model_structure_sampled.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
#######
### This function prints off the most likely predicted
### channels for each of the cells in our dataset
#######
import argparse
import importlib
import numpy as np
import os
import pickle
import glob
import math
import torch
import torch.nn as nn
from torch.autograd import Variable
import torchvision.utils
import pdb
import pandas as pd
from corr_stats import pearsonr, corrcoef
#have to do this import to be able to use pyplot in the docker image
import time
import model_utils
import torch.backends.cudnn as cudnn
cudnn.benchmark = True
import pdb
from tqdm import tqdm
import itertools
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--parent_dir', help='save dir')
parser.add_argument('--gpu_ids', nargs='+', type=int, default=[0,1,2,3], help='gpu id')
parser.add_argument('--batch_size', type=int, default=500, help='batch_size')
parser.add_argument('--use_current_results', type=bool, default=False, help='if true, dont compute errors, and construct master table')
args = parser.parse_args()
model_ref_dir = args.parent_dir + os.sep + 'ref_model'
model_struct_dir = args.parent_dir + os.sep + 'struct_model'
save_parent = args.parent_dir + os.sep + 'analysis' + os.sep + 'model_structure_variation_sampled'
if not os.path.exists(save_parent):
os.makedirs(save_parent)
opt = pickle.load(open( '{0}/opt.pkl'.format(model_struct_dir), "rb" ))
print(opt)
opt.gpu_ids = args.gpu_ids
torch.manual_seed(opt.myseed)
torch.cuda.manual_seed(opt.myseed)
np.random.seed(opt.myseed)
dp = model_utils.load_data_provider(opt.data_save_path, opt.imdir, opt.dataProvider)
#######
### Load REFERENCE MODEL
#######
opt.channelInds = [0, 1, 2]
dp.opts['channelInds'] = opt.channelInds
opt.nch = len(opt.channelInds)
opt.nClasses = dp.get_n_classes()
opt.nRef = opt.nlatentdim
models, optimizers, _, _, opt = model_utils.load_model(opt.model_name, opt)
enc = models['enc']
dec = models['dec']
enc.train(False)
dec.train(False)
models = None
optimizers = None
print('Done loading model.')
embeddings_ref_path = model_struct_dir + os.sep + 'embeddings.pkl'
embeddings_ref = model_utils.load_embeddings(embeddings_ref_path)
embeddings_struct_path = model_struct_dir + os.sep + 'embeddings.pkl'
embeddings_struct = model_utils.load_embeddings(embeddings_struct_path)
opt.batch_size = args.batch_size
gpu_id = opt.gpu_ids[0]
ndat = dp.get_n_dat('train')
nlabels = dp.get_n_classes()
img_paths_all = list()
err_save_paths = list()
label_names_all = list()
class_list = np.arange(0, nlabels)
for label_id in class_list:
label_name = dp.label_names[label_id]
label_names_all.append(label_name)
npts = 2000
# npts = np.sum(dp.get_classes(np.arange(0, ndat), train_or_test).numpy() == label_id)
err_save_path = save_parent + os.sep + 'var_' + label_name + '.pkl'
err_save_paths.append(err_save_path)
if args.use_current_results or os.path.exists(err_save_path):
continue
if not os.path.exists(save_parent):
os.makedirs(save_parent)
#set the class label so it is correct
img_class_onehot_log = torch.Tensor(nlabels).fill_(-25)
img_class_onehot_log[label_id] = 0
inds_ref = np.arange(0, npts)
iter_ref = [inds_ref[j:j+opt.batch_size] for j in range(0, len(inds_ref), opt.batch_size)]
inds_struct = np.random.choice(ndat, npts)
iter_struct = [inds_struct[j:j+opt.batch_size] for j in range(0, len(inds_struct), opt.batch_size)]
# np.random.shuffle(data_iter)
imgs_out_tmp = list()
for j in range(0, len(iter_ref)):
inds_ref_tmp = iter_ref[j]
ints_struct_tmp = iter_struct[j]
batch_size = len(inds_ref_tmp)
embeddings_ref_tmp = torch.Tensor(batch_size, embeddings_ref['train'].size()[1]).normal_()
embeddings_struct_tmp = torch.Tensor(batch_size, embeddings_struct['train'].size()[1]).normal_()
z = [None] * 3
z[0] = Variable(img_class_onehot_log.repeat(batch_size, 1).float().cuda(gpu_id), volatile=True)
z[1] = Variable(torch.Tensor(embeddings_ref_tmp).cuda(gpu_id), volatile=True)
z[2] = Variable(torch.Tensor(embeddings_struct_tmp).cuda(gpu_id), volatile=True)
imgs_out = dec(z)
imgs_out = imgs_out.index_select(1, Variable(torch.LongTensor([1]).cuda(gpu_id), volatile=True)).cpu()
imgs_out_tmp.append(imgs_out)
imgs_out_tmp = torch.cat(imgs_out_tmp, 0).cpu()
corr_mat = corrcoef(imgs_out_tmp.view(int(npts), -1)).data.cpu().numpy()
_, log_det = np.linalg.slogdet(corr_mat)
log_det_scaled = log_det/npts
data = {'label_id': label_id,
'label_name': label_name,
'train_or_test': 'sampled',
'corr_mat': corr_mat,
'log_det': log_det,
'log_det_scaled': log_det_scaled}
pickle.dump(data, open(err_save_path, 'wb'))
print('Done computing errors.')
save_info_path = save_parent + os.sep + 'info.csv'
info_list = np.concatenate([np.expand_dims(np.array(label_names_all),1),
np.expand_dims(np.array(class_list),1),
np.expand_dims(np.array(err_save_paths),1)], axis=1)
info_list = pd.DataFrame(info_list, columns=['label_name', 'label_id', 'save_path'])
info_list.to_csv(save_info_path, index=False)
save_all_path = save_parent + os.sep + 'all_dat.csv'
save_all_missing_path = save_parent + os.sep + 'all_dat_missing.csv'
# if not os.path.exists(save_all_path):
data_list = list()
data_missing_list = list()
if os.path.exists(save_all_path) & args.use_current_results:
data_list = pd.read_csv(save_all_path)
else:
for err_save_path in tqdm(err_save_paths, 'loading error files', ascii=True):
if os.path.exists(err_save_path):
try:
data = pickle.load(open(err_save_path, 'rb'))
data.pop('corr_mat', None)
data_list.append(data)
except:
data_missing_list.append(err_save_path)
else:
# print('Missing ' + err_save_path)
data_missing_list.append(err_save_path)
data_list = pd.DataFrame(data_list)
print('Writing to ' + save_all_path)
data_list.to_csv(save_all_path)
data_missing_list = pd.DataFrame(data_missing_list)
data_missing_list.to_csv(save_all_missing_path)
# from matplotlib import pyplot as plt
# import seaborn as sns
# errors = data_list['log_det']
# min_bin = np.percentile(errors, 1)
# max_bin = np.percentile(errors, 99)
# c = 0
# pdb.set_trace()
# for train_or_test in train_or_test_split:
# c+=1
# plt.subplot(len(train_or_test_split), 1, c)
# train_inds = data_list['train_or_test'] == train_or_test
# for label in ulabels:
# label_inds = data_list['label'] == label
# inds = np.logical_and(train_inds, label_inds)
# legend_key = label
# sns.kdeplot(errors[inds])
# plt.legend(loc='upper right')
# plt.savefig('{0}/distr.png'.format(save_parent), bbox_inches='tight')