-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path40x40_Predict_With_Metrics.py
202 lines (159 loc) · 6.85 KB
/
40x40_Predict_With_Metrics.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
import sys
import time
import multiprocessing
import numpy as np
from torch.utils.data import TensorDataset, DataLoader
from tqdm import tqdm
from math import log10
import torch
import Models.DeepHiC as DeepHiC
import Models.HiCARN_1 as HiCARN_1
import Models.HiCARN_2 as HiCARN_2
from Utils.SSIM import ssim
from Utils.GenomeDISCO import compute_reproducibility
from Utils.io import spreadM, together
from Arg_Parser import *
def dataloader(data, batch_size=64):
inputs = torch.tensor(data['data'], dtype=torch.float)
target = torch.tensor(data['target'], dtype=torch.float)
inds = torch.tensor(data['inds'], dtype=torch.long)
dataset = TensorDataset(inputs, target, inds)
loader = DataLoader(dataset, batch_size=batch_size, shuffle=False)
return loader
def get_chr_nums(data):
inds = torch.tensor(data['inds'], dtype=torch.long)
chr_nums = sorted(list(np.unique(inds[:, 0])))
return chr_nums
def data_info(data):
indices = data['inds']
compacts = data['compacts'][()]
sizes = data['sizes'][()]
return indices, compacts, sizes
get_digit = lambda x: int(''.join(list(filter(str.isdigit, x))))
def filename_parser(filename):
info_str = filename.split('.')[0].split('_')[2:-1]
chunk = get_digit(info_str[0])
stride = get_digit(info_str[1])
bound = get_digit(info_str[2])
scale = 1 if info_str[3] == 'nonpool' else get_digit(info_str[3])
return chunk, stride, bound, scale
def hicarn_predictor(model, hicarn_loader, ckpt_file, device, data_file):
deepmodel = model.Generator(num_channels=64).to(device)
if not os.path.isfile(ckpt_file):
ckpt_file = f'save/{ckpt_file}'
deepmodel.load_state_dict(torch.load(ckpt_file, map_location=torch.device('cpu')))
print(f'Loading checkpoint file from "{ckpt_file}"')
result_data = []
result_inds = []
chr_nums = get_chr_nums(data_file)
results_dict = dict()
test_metrics = dict()
for chr in chr_nums:
test_metrics[f'{chr}'] = {'mse': 0, 'ssims': 0, 'psnr': 0, 'ssim': 0, 'nsamples': 0}
results_dict[f'{chr}'] = [[], [], [], []] # Make respective lists for ssim, psnr, mse, and repro
deepmodel.eval()
with torch.no_grad():
for batch in tqdm(hicarn_loader, desc='HiCARN Predicting: '):
lr, hr, inds = batch
batch_size = lr.size(0)
ind = f'{(inds[0][0]).item()}'
test_metrics[ind]['nsamples'] += batch_size
lr = lr.to(device)
hr = hr.to(device)
out = deepmodel(lr)
batch_mse = ((out - hr) ** 2).mean()
test_metrics[ind]['mse'] += batch_mse * batch_size
batch_ssim = ssim(out, hr)
test_metrics[ind]['ssims'] += batch_ssim * batch_size
test_metrics[ind]['psnr'] = 10 * log10(1 / (test_metrics[ind]['mse'] / test_metrics[ind]['nsamples']))
test_metrics[ind]['ssim'] = test_metrics[ind]['ssims'] / test_metrics[ind]['nsamples']
((results_dict[ind])[0]).append((test_metrics[ind]['ssim']).item())
((results_dict[ind])[1]).append(batch_mse.item())
((results_dict[ind])[2]).append(test_metrics[ind]['psnr'])
for i, j in zip(hr, out):
out1 = torch.squeeze(j, dim=0)
hr1 = torch.squeeze(i, dim=0)
out2 = out1.cpu().detach().numpy()
hr2 = hr1.cpu().detach().numpy()
genomeDISCO = compute_reproducibility(out2, hr2, transition=True)
((results_dict[ind])[3]).append(genomeDISCO)
result_data.append(out.to('cpu').numpy())
result_inds.append(inds.numpy())
result_data = np.concatenate(result_data, axis=0)
result_inds = np.concatenate(result_inds, axis=0)
mean_ssims = []
mean_mses = []
mean_psnrs = []
mean_gds = []
for key, value in results_dict.items():
value[0] = sum(value[0])/len(value[0])
value[1] = sum(value[1])/len(value[1])
value[2] = sum(value[2])/len(value[2])
value[3] = sum(value[3])/len(value[3])
mean_ssims.append(value[0])
mean_mses.append(value[1])
mean_psnrs.append(value[2])
mean_gds.append(value[3])
print("\n")
print("Chr", key, "SSIM: ", value[0])
print("Chr", key, "MSE: ", value[1])
print("Chr", key, "PSNR: ", value[2])
print("Chr", key, "GenomeDISCO: ", value[3])
print("\n")
print("___________________________________________")
print("Means across chromosomes")
print("SSIM: ", sum(mean_ssims) / len(mean_ssims))
print("MSE: ", sum(mean_mses) / len(mean_mses))
print("PSNR: ", sum(mean_psnrs) / len(mean_psnrs))
print("GenomeDISCO: ", sum(mean_gds) / len(mean_gds))
print("___________________________________________")
print("\n")
hicarn_hics = together(result_data, result_inds, tag='Reconstructing: ')
return hicarn_hics
def save_data(carn, compact, size, file):
hicarn = spreadM(carn, compact, size, convert_int=False, verbose=True)
np.savez_compressed(file, hicarn=hicarn, compact=compact)
print('Saving file:', file)
if __name__ == '__main__':
args = data_predict_parser().parse_args(sys.argv[1:])
cell_line = args.cell_line
low_res = args.low_res
ckpt_file = args.checkpoint
cuda = args.cuda
model = args.model
HiCARN_file = args.file_name
print('WARNING: Predict process requires large memory, thus ensure that your machine has ~150G memory.')
if multiprocessing.cpu_count() > 23:
pool_num = 23
else:
exit()
in_dir = os.path.join(root_dir, 'data')
out_dir = os.path.join(root_dir, 'predict', cell_line)
mkdir(out_dir)
files = [f for f in os.listdir(in_dir) if f.find(low_res) >= 0]
chunk, stride, bound, scale = filename_parser(HiCARN_file)
device = torch.device(
f'cuda:{cuda}' if (torch.cuda.is_available() and cuda > -1 and cuda < torch.cuda.device_count()) else 'cpu')
print(f'Using device: {device}')
start = time.time()
print(f'Loading data[HiCARN]: {HiCARN_file}')
hicarn_data = np.load(os.path.join(in_dir, HiCARN_file), allow_pickle=True)
hicarn_loader = dataloader(hicarn_data)
indices, compacts, sizes = data_info(hicarn_data)
if model == "HiCARN_1":
model = HiCARN_1
if model == "HiCARN_2":
model = HiCARN_2
if model == "DeepHiC":
model = DeepHiC
hicarn_hics = hicarn_predictor(model, hicarn_loader, ckpt_file, device, hicarn_data)
def save_data_n(key):
file = os.path.join(out_dir, f'predict_chr{key}_{low_res}.npz')
save_data(hicarn_hics[key], compacts[key], sizes[key], file)
pool = multiprocessing.Pool(processes=pool_num)
print(f'Start a multiprocess pool with process_num = {pool_num} for saving predicted data')
for key in compacts.keys():
pool.apply_async(save_data_n, (key,))
pool.close()
pool.join()
print(f'All data saved. Running cost is {(time.time() - start) / 60:.1f} min.')