in aiops/AnomalyDetection/model/Model.py [0:0]
def test(dataloader,model,loss_fn,parameters):
num_batches = len(dataloader)
print(num_batches)
model.eval()
test_loss = 0
labels=[]
reconSeq=[]
origiSeq=[]
diffSeq=[]
coherences=[]
labelsShow=[]
reconSigs=[]
reconAggs=[]
with torch.no_grad():
for x,y in dataloader:
x, y = x.to(parameters.device), y.to(parameters.device)
labelsShow.append(y[:, -1])
y = y == 1
labels.append(y[:, -2:].any(dim=-1))
recons,coherence,costs,reconSig,reconAgg = model(x,"test")
#torch.true_divide(torch.abs(x-xu),xstd).sum(dim=(-1))
test_loss += loss_fn(recons,x,coherence,costs,parameters)
reconSeq.append(recons[:,-1,:])
origiSeq.append(x[:,-1,:])
diffSeq.append(x[:,-1,:]-recons[:,-1,:])
coherences.append(coherence[:,-1])
reconSigs.append(reconSig[:,:,-1])
reconAggs.append(reconAgg[:,:,-1])
test_loss /= num_batches
reconSeq=torch.cat(reconSeq,dim=0).cpu().detach().numpy()
origiSeq=torch.cat(origiSeq,dim=0).cpu().detach().numpy()
reconSeq=reconAdjust(reconSeq,origiSeq)
diffSeq=torch.cat(diffSeq,dim=0).cpu().detach().numpy()
coherences=torch.cat(coherences,dim=0).cpu().detach().numpy()
reconSigs=np.concatenate(reconSigs,axis=1)
reconAggs=np.concatenate(reconAggs,axis=1)
scores=getAnomScore2(origiSeq,reconSeq,parameters)
divideScores=getAnomScoreDivide(origiSeq,reconSeq,parameters)
print("score shape",len(scores))
print("recon shape",reconSeq.shape)
print("coherences shape", coherences.shape)
#scores=slideWindow(scores,coherences,parameters)
scores=np.array(scores)
labels=torch.cat(labels,dim=0).cpu().detach().numpy()
labelsShow=torch.cat(labelsShow,dim=0).cpu().detach().numpy()
pot_result = searchThreshold(-scores, labels)
precision = pot_result['pot-precision']
recall = pot_result['pot-recall']
F1 = pot_result['pot-f1']
threshold = pot_result['pot-threshold']
accuracy = torch.true_divide(pot_result['pot-TP'] + pot_result['pot-TN'],
pot_result['pot-TP'] + pot_result['pot-TN']
+ pot_result['pot-FP'] + pot_result['pot-FN']).item()
if parameters.needplot:
"""plt.figure(figsize=(10, 7))
plt.subplot(7, 1, 1)
plt.plot(reconSeq)
plt.subplot(7,1,2)
plt.plot(origiSeq)
plt.subplot(7,1,3)
if parameters.omit:
avgSeq=(origiSeq*parameters.exeTime[1:]).sum(axis=-1)
else:
avgSeq=(origiSeq*parameters.exeTime).sum(axis=-1)
plt.plot(avgSeq)
plt.subplot(7,1,4)
plt.plot(scores)
plt.axhline(y=-threshold, color='grey', linestyle='--')
plt.subplot(7,1,5)
plt.plot(coherences)
plt.subplot(7,1,6)
plt.plot(divideScores)
plt.subplot(7,1,7)
plt.plot(labelsShow)
plt.show()"""
colors=["#14517C","#2F7fC1","#E7EFFA","#9673CD","#F3D266","#D8383A","#F7E1ED","#F8F3F9","#C497B2","#A9B8C6","#934B43","#9394E7","#D76364","#EF7A6D","#F1D77E","#B1CE46","#5F97D2","#9DC3E7"]
colors=colors[:parameters.fealen]
#colors = plt.cm.magma(np.linspace(0, 1, parameters.fealen))
plt.figure(figsize=(10,9))
plt.subplot(4,1,1)
#plt.plot(reconSeq,color=colors)
plotSeries(reconSeq,colors)
plt.xlabel("Time slots")
#plt.ylabel("Reconstruction series")
plt.title("Reconstruction series")
plt.subplot(4,1,2)
#plt.plot(origiSeq,color=colors)
plotSeries(origiSeq, colors)
plt.title("Original series")
plt.xlabel("Time slots")
#plt.ylabel("Original series")
plt.subplot(4,1,3)
lbound=np.min(scores)
ubound=np.max(scores)
y_ceil=np.ones(scores.shape)*ubound
y_bottom=np.ones(scores.shape)*lbound
x=np.arange(0,len(scores))
labelMask=labelsShow!=1.
y_ceil[labelMask]=y_bottom[labelMask]
plt.plot(scores,label="Anomaly score",color="#8E8BFE")
plt.axhline(y=-threshold, color='grey', linestyle='--',label="Threshold")
plt.fill_between(x,y_bottom,y_ceil,color="#FEA3E2",alpha=0.5,edgecolor="none",label="Anomaly")
plt.legend()
plt.title("Anomaly score")
plt.xlabel("Time slots")
#plt.ylabel("Anomaly score")
plt.subplot(4,1,4)
plt.plot(coherences[parameters.klen:],color="#8E8BFE",label="weight")
lbound = np.min(coherences)
ubound = np.max(coherences)
y_ceil = np.ones(scores.shape) * ubound
y_bottom = np.ones(scores.shape) * lbound
y_ceil[labelMask] = y_bottom[labelMask]
plt.fill_between(x, y_bottom, y_ceil, color="#FEA3E2", alpha=0.5, edgecolor="none", label="Anomaly")
plt.title("Weight in loss function")
plt.xlabel("Time slots")
plt.legend()
plt.tight_layout()
plt.savefig('MCVisualize.pdf', bbox_inches='tight')
plt.show()
plt.figure(figsize=(10, 9))
for i in range(parameters.moduleNum):
plt.subplot(parameters.moduleNum, 3, i*3+1)
plotSeries(origiSeq,colors)
plt.subplot(parameters.moduleNum,3,i*3+2)
plotSeries(reconAggs[i], colors)
plt.subplot(parameters.moduleNum,3,i*3+3)
plotSeries(reconSigs[i],colors)
plt.tight_layout()
plt.savefig('LayerAtt.pdf', bbox_inches='tight')
plt.show()
print(f"Test Error: \n Avg loss: {test_loss:>8f} \n")
print("precision:%.6f, recall:%.6f, F1 score:%.6f, accuracy:%.6f\n" % (precision, recall, F1, accuracy))
print("average score:%f"%np.mean(scores))
return test_loss, precision, recall, F1, accuracy