-
Notifications
You must be signed in to change notification settings - Fork 0
/
tester.py
146 lines (115 loc) · 4.96 KB
/
tester.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
# -*- coding: utf-8 -*-
"""
Created on Wed May 1 16:46:43 2019
@author: c1307135
"""
import torch
from torch.utils.data import DataLoader
from tqdm import tqdm
import numpy as np
import pandas as pd
from data_loader import FITSCubeDataset
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from CAE import CovNet2, LinearNet_5
data_path = '/home/anubis/c1307135/ARIEL/flux_files/NO_NAN/TEST/'
save_path = '/home/anubis/c1307135/ARIEL/features/'
batch_size=640
total = int(29360/batch_size)
total2 = 29360
blind_test = True
if blind_test == True:
total = int(62900/batch_size)
total2 = 62900
data_path = '/home/anubis/c1307135/ARIEL/flux_files/NO_NAN/BLIND/'
model_path = '/home/anubis/c1307135/ARIEL/models/'
perc_path = '/home/corona/c1307135/Holy_Grail/test_images/percentiles_no_nan.pkl'
#model2 = LinearNet_5(55)
#model2.load_state_dict(torch.load(model_path+'ARIEL_LinearNet_nonan_r_75.pt'))
model = CovNet2(55)
model.load_state_dict(torch.load(model_path+'CovNet2_meta_test.pt'))
print('-'*73)
print('MODEL INFORMATION: \n',model)
print('-'*73)
model = model.cpu()
model.train(False)
_filenames = FITSCubeDataset(data_path).make_dataset()
test_loader = DataLoader(dataset=FITSCubeDataset(data_path,perc_path=perc_path,return_filename=True),
batch_size=batch_size,num_workers=16,shuffle=False)
all_names, all_features, all_targets = [],[],[]
print('-'*73)
print('BEGINNING TESTING PHASE')
for idx, (batch, radius, target,_l,_m,_k,_p,_nspot, _nphot, names) in tqdm(enumerate(test_loader),total=total):
_radius=np.vstack([radius,_l,_m,_k,_p]).T
_radius=torch.tensor(_radius)
_features = model(batch.float(),_radius.float()).detach().numpy()
all_names.append(names)
all_features.append(_features)
if blind_test == False:
all_targets.append(target)
print('-'*73)
all_features = np.vstack(all_features)
all_names = np.hstack(all_names)
print(all_features.shape)
#=============================================================================#
second_wind = False
if second_wind == True:
indices = []
for k in tqdm(range(all_features.shape[0])):
if len(np.where(all_features[k,:]>0.175)[0])>0:
indices.append(k)
print(len(indices))
test_loader2 = DataLoader(dataset=FITSCubeDataset(data_path,perc_path=perc_path,return_filename=True),
batch_size=total2,num_workers=16,shuffle=False)
for idx, (batch, radius, target, names) in tqdm(enumerate(test_loader2),total=1):
batch = batch[indices,:]; radius = radius[indices]
_features = model2(batch.float(),radius.float()).detach().numpy()
for j in range(len(indices)):
all_features[indices[j],:] = _features[j]
print('-'*73)
#=============================================================================#
if blind_test == False:
all_targets = np.vstack(all_targets)
print(all_targets.shape)
average_err = np.mean(np.abs(all_targets.reshape(-1)-all_features.reshape(-1)))
suspects = []
for i in range(all_targets.shape[0]):
if len(np.where((all_targets[i,:]>0.08)&(all_targets[i,:]<0.1))[0])>(0.5*(all_targets.shape[1])):
suspects.append(i)
suspects2 = []
for i in suspects:
if len(np.where(all_features[i,:]<0.06)[0]) > (0.5*(all_features.shape[1])):
suspects2.append(int(i))
# print(len(suspects2))
# print(all_names[suspects2[0]])
plt.figure()
plt.scatter(all_targets.reshape(-1),all_features.reshape(-1),c='k',s=0.2)
plt.scatter(all_targets[suspects2].reshape(-1),all_features[suspects2].reshape(-1),c='r',s=0.2)
plt.plot([0,1],[0,1],'r--',alpha=0.5)
plt.xlabel('True'); plt.ylabel('Predicted')
plt.text(0.1,0.7,str('%.6f' % (average_err)),bbox=dict(facecolor='grey',alpha=0.5))
print(average_err)
plt.xlim(0,0.8)
plt.ylim(0,0.8)
plt.savefig('/home/corona/c1307135/Holy_Grail/test_images/CovNet2_meta_test.png')
if blind_test == True:
df = pd.DataFrame(all_features)
df.insert(loc=0,column='NAME',value=all_names)
df = df.sort_values(by=['NAME'])
df = df.drop(['NAME'],axis=1)
df.to_pickle(save_path+'ARIEL_features_noisy_test.pkl')
df = pd.read_pickle(save_path+'ARIEL_features_noisy_test.pkl')
df.to_csv('/home/corona/c1307135/Holy_Grail/test_images/results/params_test_CovNet2_meta_test.txt',
sep=' ',header=False,index=False)
#=============================================================================#
"""
for idx, (batch, radius, target, names) in tqdm(enumerate(test_loader2),total=1):
for i in tqdm(range(batch.shape[0])):
_features = model(batch[i,:,:,:].float(),radius[i].float().unsqueeze(0)).detach().numpy()
all_features.append(_features)
if blind_test == False:
all_targets.append(target[i])
all_names.append(names)
print('-'*73)
"""