update README.rst

This commit is contained in:
xuhongzuo 2023-11-09 20:06:56 +08:00
parent f56e1082d0
commit 02781744da
2 changed files with 142 additions and 15 deletions

View File

@ -11,7 +11,7 @@ Python Deep Outlier/Anomaly Detection (DeepOD)
.. image:: https://app.codacy.com/project/badge/Grade/2c587126aac2441abb917c032189fbe8
:target: https://app.codacy.com/gh/xuhongzuo/DeepOD/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade
:alt: coveralls
:alt: codacy
.. image:: https://coveralls.io/repos/github/xuhongzuo/DeepOD/badge.svg?branch=main
:target: https://coveralls.io/github/xuhongzuo/DeepOD?branch=main
@ -292,17 +292,3 @@ Reference
.. [#Xu2023RoSAS] Xu, Hongzuo et al. "RoSAS: Deep semi-supervised anomaly detection with contamination-resilient continuous supervision". IP&M. 2023.
Contributors
~~~~~~~~~~~~~~~~~
Thanks goes to these wonderful people (`emoji key`_):
.. _emoji key: https://allcontributors.org/docs/en/emoji-key
.. raw:: html
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<table>
# ...
</table>
<!-- ALL-CONTRIBUTORS-LIST:END -->

141
deepod/test/test_ncad.py Normal file
View File

@ -0,0 +1,141 @@
# -*- coding: utf-8 -*-
from __future__ import division
from __future__ import print_function
import os
import sys
import unittest
# noinspection PyProtectedMember
from numpy.testing import assert_equal
from sklearn.metrics import roc_auc_score
import torch
import pandas as pd
# temporary solution for relative imports in case pyod is not installed
# if deepod is installed, no need to use the following line
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from deepod.models.time_series.ncad import NCAD
class TestDCdetector(unittest.TestCase):
def setUp(self):
train_file = 'data/omi-1/omi-1_train.csv'
test_file = 'data/omi-1/omi-1_test.csv'
train_df = pd.read_csv(train_file, sep=',', index_col=0)
test_df = pd.read_csv(test_file, index_col=0)
y = test_df['label'].values
train_df, test_df = train_df.drop('label', axis=1), test_df.drop('label', axis=1)
self.Xts_train = train_df.values
self.Xts_test = test_df.values
self.yts_test = y
device = 'cuda' if torch.cuda.is_available() else 'cpu'
self.clf = NCAD(seq_len=100, stride=1, epochs=2,
batch_size=32, lr=1e-4,
device=device, random_state=42)
self.clf.fit(self.Xts_train)
def test_parameters(self):
assert (hasattr(self.clf, 'decision_scores_') and
self.clf.decision_scores_ is not None)
assert (hasattr(self.clf, 'labels_') and
self.clf.labels_ is not None)
assert (hasattr(self.clf, 'threshold_') and
self.clf.threshold_ is not None)
def test_train_scores(self):
assert_equal(len(self.clf.decision_scores_), self.Xts_train.shape[0])
def test_prediction_scores(self):
pred_scores = self.clf.decision_function(self.Xts_test)
assert_equal(pred_scores.shape[0], self.Xts_test.shape[0])
def test_prediction_labels(self):
pred_labels = self.clf.predict(self.Xts_test)
assert_equal(pred_labels.shape, self.yts_test.shape)
# def test_prediction_proba(self):
# pred_proba = self.clf.predict_proba(self.X_test)
# assert (pred_proba.min() >= 0)
# assert (pred_proba.max() <= 1)
#
# def test_prediction_proba_linear(self):
# pred_proba = self.clf.predict_proba(self.X_test, method='linear')
# assert (pred_proba.min() >= 0)
# assert (pred_proba.max() <= 1)
#
# def test_prediction_proba_unify(self):
# pred_proba = self.clf.predict_proba(self.X_test, method='unify')
# assert (pred_proba.min() >= 0)
# assert (pred_proba.max() <= 1)
#
# def test_prediction_proba_parameter(self):
# with assert_raises(ValueError):
# self.clf.predict_proba(self.X_test, method='something')
def test_prediction_labels_confidence(self):
pred_labels, confidence = self.clf.predict(self.Xts_test, return_confidence=True)
assert_equal(pred_labels.shape, self.yts_test.shape)
assert_equal(confidence.shape, self.yts_test.shape)
assert (confidence.min() >= 0)
assert (confidence.max() <= 1)
# def test_prediction_proba_linear_confidence(self):
# pred_proba, confidence = self.clf.predict_proba(self.X_test,
# method='linear',
# return_confidence=True)
# assert (pred_proba.min() >= 0)
# assert (pred_proba.max() <= 1)
#
# assert_equal(confidence.shape, self.y_test.shape)
# assert (confidence.min() >= 0)
# assert (confidence.max() <= 1)
#
# def test_fit_predict(self):
# pred_labels = self.clf.fit_predict(self.X_train)
# assert_equal(pred_labels.shape, self.y_train.shape)
#
# def test_fit_predict_score(self):
# self.clf.fit_predict_score(self.X_test, self.y_test)
# self.clf.fit_predict_score(self.X_test, self.y_test,
# scoring='roc_auc_score')
# self.clf.fit_predict_score(self.X_test, self.y_test,
# scoring='prc_n_score')
# with assert_raises(NotImplementedError):
# self.clf.fit_predict_score(self.X_test, self.y_test,
# scoring='something')
#
# def test_predict_rank(self):
# pred_socres = self.clf.decision_function(self.X_test)
# pred_ranks = self.clf._predict_rank(self.X_test)
#
# # assert the order is reserved
# assert_allclose(rankdata(pred_ranks), rankdata(pred_socres), atol=3)
# assert_array_less(pred_ranks, self.X_train.shape[0] + 1)
# assert_array_less(-0.1, pred_ranks)
#
# def test_predict_rank_normalized(self):
# pred_socres = self.clf.decision_function(self.X_test)
# pred_ranks = self.clf._predict_rank(self.X_test, normalized=True)
#
# # assert the order is reserved
# assert_allclose(rankdata(pred_ranks), rankdata(pred_socres), atol=3)
# assert_array_less(pred_ranks, 1.01)
# assert_array_less(-0.1, pred_ranks)
# def test_plot(self):
# os, cutoff1, cutoff2 = self.clf.explain_outlier(ind=1)
# assert_array_less(0, os)
# def test_model_clone(self):
# clone_clf = clone(self.clf)
def tearDown(self):
pass
if __name__ == '__main__':
unittest.main()