forked from JointCloud/JCC-DeepOD
fix bugs in test files
This commit is contained in:
parent
a1c3f6e893
commit
dbede6f294
|
|
@ -1,5 +1,8 @@
|
|||
from sklearn import metrics
|
||||
import numpy as np
|
||||
from deepod.metrics.affiliation.generics import convert_vector_to_events
|
||||
from deepod.metrics.vus.metrics import get_range_vus_roc
|
||||
from deepod.metrics.affiliation.metrics import pr_from_events
|
||||
|
||||
|
||||
def auc_roc(y_true, y_score):
|
||||
|
|
@ -38,25 +41,42 @@ def get_best_f1(label, score):
|
|||
return best_f1, best_p, best_r
|
||||
|
||||
|
||||
# revised by Yiyuan Yang 2023/11/08
|
||||
# Compored with ts_metrics, this function can return more metrics with one more intput y_test(predictions of events)
|
||||
def ts_metrics_enhanced(y_true, y_score, y_test):
|
||||
"""
|
||||
Use Case Demo:
|
||||
|
||||
from deepod.models.time_series import DCdetector
|
||||
clf = DCdetector()
|
||||
clf.fit(X_train)
|
||||
pred, scores = clf.decision_function(X_test)
|
||||
|
||||
from deepod.metrics import point_adjustment
|
||||
from deepod.metrics import ts_metrics_enhanced
|
||||
adj_eval_metrics = ts_metrics_enhanced(labels, point_adjustment(labels, scores), pred)
|
||||
print('adj_eval_metrics',adj_eval_metrics)
|
||||
Compared with ts_metrics, this function can return more metrics
|
||||
with one more input y_test (predictions of events)
|
||||
revised by @Yiyuan Yang 2023/11/08
|
||||
|
||||
Args:
|
||||
y_true:
|
||||
y_score:
|
||||
y_test
|
||||
|
||||
Returns:
|
||||
auroc:
|
||||
aupr:
|
||||
best_f1:
|
||||
best_p:
|
||||
best_r:
|
||||
affiliation_precision:
|
||||
affiliation_recall:
|
||||
vus_r_auroc:
|
||||
vus_r_aupr:
|
||||
vus_roc:
|
||||
vus_pr:
|
||||
|
||||
Example:
|
||||
from deepod.models.time_series import DCdetector
|
||||
clf = DCdetector()
|
||||
clf.fit(X_train)
|
||||
pred, scores = clf.decision_function(X_test)
|
||||
|
||||
from deepod.metrics import point_adjustment
|
||||
from deepod.metrics import ts_metrics_enhanced
|
||||
adj_eval_metrics = ts_metrics_enhanced(labels, point_adjustment(labels, scores), pred)
|
||||
print('adj_eval_metrics',adj_eval_metrics)
|
||||
"""
|
||||
from .affiliation.generics import convert_vector_to_events
|
||||
from .vus.metrics import get_range_vus_roc
|
||||
from .affiliation.metrics import pr_from_events
|
||||
|
||||
best_f1, best_p, best_r = get_best_f1(y_true, y_score)
|
||||
|
||||
events_pred = convert_vector_to_events(y_test)
|
||||
|
|
@ -64,7 +84,20 @@ def ts_metrics_enhanced(y_true, y_score, y_test):
|
|||
Trange = (0, len(y_test))
|
||||
affiliation = pr_from_events(events_pred, events_gt, Trange)
|
||||
vus_results = get_range_vus_roc(y_score, y_true, 100) # default slidingWindow = 100
|
||||
|
||||
return auc_roc(y_true, y_score), auc_pr(y_true, y_score), best_f1, best_p, best_r, affiliation['Affiliation_Precision'], affiliation['Affiliation_Recall'], vus_results["R_AUC_ROC"], vus_results["R_AUC_PR"], vus_results["VUS_ROC"], vus_results["VUS_PR"]
|
||||
|
||||
auroc = auc_roc(y_true, y_score)
|
||||
aupr = auc_pr(y_true, y_score)
|
||||
|
||||
affiliation_precision = affiliation['Affiliation_Precision']
|
||||
affiliation_recall = affiliation['Affiliation_Recall']
|
||||
vus_r_auroc = vus_results["R_AUC_ROC"]
|
||||
vus_r_aupr = vus_results["R_AUC_PR"]
|
||||
vus_roc = vus_results["VUS_ROC"]
|
||||
vus_pr = vus_results["VUS_PR"]
|
||||
|
||||
return auroc, aupr, best_f1, best_p, best_r, \
|
||||
affiliation_precision, affiliation_recall, \
|
||||
vus_r_auroc, vus_r_aupr, \
|
||||
vus_roc, vus_pr
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -627,7 +627,7 @@ class _SubseqData(Dataset):
|
|||
|
||||
class _DSVDDUncLoss(torch.nn.Module):
|
||||
def __init__(self, c, reduction='mean'):
|
||||
super(DSVDDUncLoss, self).__init__()
|
||||
super(_DSVDDUncLoss, self).__init__()
|
||||
self.c = c
|
||||
self.reduction = reduction
|
||||
|
||||
|
|
|
|||
|
|
@ -80,7 +80,6 @@ class DCdetector(BaseDeepAD):
|
|||
|
||||
return loss_final_pad
|
||||
|
||||
|
||||
def training(self, dataloader):
|
||||
loss_list = []
|
||||
|
||||
|
|
@ -161,11 +160,8 @@ class DCdetector(BaseDeepAD):
|
|||
test_energy = np.array(attens_energy) # anomaly scores
|
||||
|
||||
return test_energy, preds # (n,d)
|
||||
|
||||
|
||||
|
||||
def predict(self, X, return_confidence=True):
|
||||
|
||||
seqs = get_sub_seqs(X, seq_len=self.seq_len, stride=1)
|
||||
dataloader = DataLoader(seqs, batch_size=self.batch_size,
|
||||
shuffle=False, drop_last=True)
|
||||
|
|
@ -237,7 +233,6 @@ class DCdetector(BaseDeepAD):
|
|||
test_energy = np.array(attens_energy) # anomaly scores
|
||||
|
||||
preds = (test_energy > thresh).astype(int)
|
||||
|
||||
|
||||
loss_final = np.mean(test_energy, axis=1) # (n,)
|
||||
loss_final_pad = np.hstack([0 * np.ones(X.shape[0] - loss_final.shape[0]), loss_final])
|
||||
|
|
@ -245,33 +240,11 @@ class DCdetector(BaseDeepAD):
|
|||
preds_final_pad = np.hstack([0 * np.ones(X.shape[0] - preds_final.shape[0]), preds_final])
|
||||
|
||||
if return_confidence:
|
||||
return loss_final_pad, preds_final_pad
|
||||
confidence = self._predict_confidence(loss_final_pad)
|
||||
return preds_final_pad, confidence
|
||||
else:
|
||||
return preds_final_pad
|
||||
|
||||
|
||||
def predict(self, X, return_confidence=False):
|
||||
|
||||
## self.threshold
|
||||
|
||||
|
||||
self.threshold_ = None
|
||||
|
||||
|
||||
# ------------------------------ #
|
||||
|
||||
pred_score = self.decision_function(X)
|
||||
|
||||
prediction = (pred_score > self.threshold_).astype('int').ravel()
|
||||
|
||||
if return_confidence:
|
||||
confidence = self._predict_confidence(pred_score)
|
||||
return prediction, confidence
|
||||
|
||||
return prediction
|
||||
|
||||
|
||||
|
||||
def training_forward(self, batch_x, net, criterion):
|
||||
"""define forward step in training"""
|
||||
return
|
||||
|
|
|
|||
|
|
@ -33,8 +33,8 @@ class TestDCdetector(unittest.TestCase):
|
|||
|
||||
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
||||
self.clf = DCdetector(seq_len=90, stride=1, epochs=2,
|
||||
batch_size=32, lr=1e-4, patch_size=[5],
|
||||
device=device, random_state=42)
|
||||
batch_size=32, lr=1e-4, patch_size=[5],
|
||||
device=device, random_state=42)
|
||||
self.clf.fit(self.Xts_train)
|
||||
|
||||
def test_parameters(self):
|
||||
|
|
|
|||
Loading…
Reference in New Issue