Skip to content

Commit

Permalink
Merge branch 'development' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuox5 committed Jun 21, 2024
2 parents 6bc3742 + 95e55ef commit 4c2730f
Show file tree
Hide file tree
Showing 32 changed files with 1,786 additions and 2,020 deletions.
4 changes: 3 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,6 @@ v<1.1.3>, <02/07/2024> -- Minor fix for SUOD changes.
v<1.1.4>, <02/09/2024> -- Drop support of six for Python 2.
v<1.1.4>, <03/16/2024> -- Fix documentation and temporarily restrict to Keras 2 in testing.
v<2.0.0>, <05/21/2024> -- Moving from TF to Torch -- reimplement SO_GAAL.
v<2.0.0>, <05/21/2024> -- Moving from TF to Torch -- implement dl base with more utilities.
v<2.0.0>, <05/21/2024> -- Moving from TF to Torch -- implement dl base with more utilities.
v<2.0.1>, <06/16/2024> -- Moving from TF to Torch -- reimplement DeepSVDD.
v<2.0.1>, <06/17/2024> -- Moving from TF to Torch -- reimplement dl_base.
4 changes: 2 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Welcome to PyOD, a versatile Python library for detecting anomalies in multivari

* **For graph outlier detection**, please use `PyGOD <https://pygod.org/>`_.

* **Performance Comparison \& Datasets**: We have a 45-page, the most comprehensive `anomaly detection benchmark paper <https://www.andrew.cmu.edu/user/yuezhao2/papers/22-neurips-adbench.pdf>`_. The fully `open-sourced ADBench <https://github.com/Minqi824/ADBench>`_ compares 30 anomaly detection algorithms on 57 benchmark datasets.
* **Performance Comparison \& Datasets**: We have a 45-page, the most comprehensive `anomaly detection benchmark paper <https://arxiv.org/pdf/2206.09426>`_. The fully `open-sourced ADBench <https://github.com/Minqi824/ADBench>`_ compares 30 anomaly detection algorithms on 57 benchmark datasets.

* **Learn more about anomaly detection** \@ `Anomaly Detection Resources <https://github.com/yzhao062/anomaly-detection-resources>`_

Expand Down Expand Up @@ -362,4 +362,4 @@ Key Attributes of a fitted model:
.. bibliography::
:cited:
:labelprefix: A
:keyprefix: a-
:keyprefix: a-
8 changes: 6 additions & 2 deletions docs/pyod.models.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pyod.models.auto\_encoder\_torch module

.. automodule:: pyod.models.auto_encoder_torch
:members:
:exclude-members: InnerAutoencoder
:exclude-members:
:show-inheritance:
:inherited-members:
Expand Down Expand Up @@ -100,7 +101,7 @@ pyod.models.deep\_svdd module

.. automodule:: pyod.models.deep_svdd
:members:
:exclude-members:
:exclude-members: InnerDeepSVDD
:undoc-members:
:show-inheritance:
:inherited-members:
Expand All @@ -121,7 +122,7 @@ pyod.models.ecod module

.. automodule:: pyod.models.ecod
:members:
:exclude-members:
:exclude-members: InnerDeepSVDD,
:undoc-members:
:show-inheritance:
:inherited-members:
Expand Down Expand Up @@ -197,6 +198,7 @@ pyod.models.kpca module

.. automodule:: pyod.models.kpca
:members:
:exclude-members: PyODKernelPCA
:undoc-members:
:show-inheritance:
:inherited-members:
Expand Down Expand Up @@ -328,6 +330,7 @@ pyod.models.rod module

.. automodule:: pyod.models.rod
:members:
:exclude-members: angle, euclidean, geometric_median, mad, process_sub, rod_3D, rod_nD, scale_angles, sigmoid
:undoc-members:
:show-inheritance:
:inherited-members:
Expand Down Expand Up @@ -356,6 +359,7 @@ pyod.models.so\_gaal module

.. automodule:: pyod.models.so_gaal
:members:
:exclude-members: Discriminator, Generator
:undoc-members:
:show-inheritance:
:inherited-members:
Expand Down
7 changes: 3 additions & 4 deletions examples/auto_encoder_example.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# -*- coding: utf-8 -*-
"""Example of using AutoEncoder for outlier detection
"""
# Author: Yue Zhao <zhaoy@cmu.edu>
# Author: Tiankai Yang <tiankaiy@usc.edu>
# License: BSD 2 clause

from __future__ import division
from __future__ import print_function

import os
import sys
Expand All @@ -14,6 +12,7 @@
# if pyod is installed, no need to use the following line
sys.path.append(
os.path.abspath(os.path.join(os.path.dirname("__file__"), '..')))
sys.path.append(os.path.abspath(os.path.dirname("__file__")))

from pyod.models.auto_encoder import AutoEncoder
from pyod.utils.data import generate_data
Expand All @@ -35,7 +34,7 @@

# train AutoEncoder detector
clf_name = 'AutoEncoder'
clf = AutoEncoder(epochs=30, contamination=contamination)
clf = AutoEncoder(epoch_num=30, contamination=contamination)
clf.fit(X_train)

# get the prediction labels and outlier scores of the training data
Expand Down
Binary file added examples/data/arrhythmia.mat
Binary file not shown.
Binary file added examples/data/ionosphere.mat
Binary file not shown.
Binary file added examples/data/pima.mat
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/deepsvdd_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

# train DeepSVDD detector (Without-AE)
clf_name = 'DeepSVDD'
clf = DeepSVDD(use_ae=use_ae, epochs=5, contamination=contamination,
clf = DeepSVDD(n_features=n_features, use_ae=use_ae, epochs=5, contamination=contamination,
random_state=random_state)
clf.fit(X_train)

Expand Down
54 changes: 54 additions & 0 deletions examples/perf_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-
"""Example of combining multiple base outlier scores. Four combination
frameworks are demonstrated:
"""
# Author: Yue Zhao <zhaoy@cmu.edu>
# License: BSD 2 clause

from __future__ import division
from __future__ import print_function

import os
import sys

# temporary solution for relative imports in case pyod is not installed
# if pyod is installed, no need to use the following line
sys.path.append(
os.path.abspath(os.path.join(os.path.dirname("__file__"), '..')))

from scipy.io import loadmat

from pyod.models.knn import KNN
from pyod.utils.data import generate_data
from pyod.utils.data import evaluate_print

if __name__ == "__main__":

file_list = ['arrhythmia.mat', 'cardio.mat', 'ionosphere.mat',
'letter.mat', 'pima.mat']
# Define data file and read X and y
# Generate some data if the source data is missing

for mat_file in file_list:

try:
mat = loadmat(os.path.join('data', mat_file))

except TypeError:
print('{data_file} does not exist. Use generated data'.format(
data_file=mat_file))
X, y = generate_data(train_only=True) # load data
except IOError:
print('{data_file} does not exist. Use generated data'.format(
data_file=mat_file))
X, y = generate_data(train_only=True) # load data
else:
X = mat['X']
y = mat['y'].ravel()

clf = KNN() # the algorithm you want to check
# clf = KNN_new()
clf.fit(X) # fit model

# print performance
evaluate_print(mat_file, y, clf.decision_scores_)
2 changes: 0 additions & 2 deletions examples/so_gaal_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
# Author: Winston Li <jk_zhengli@hotmail.com>
# License: BSD 2 clause

from __future__ import division
from __future__ import print_function

import os
import sys
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# -*- coding: utf-8 -*-
"""Example of using Autoencoder for outlier detection (pytorch)
detection
"""Example of using Single-Objective Generative Adversarial Active
Learning (SO_GAAL) for outlier detection
"""
# Author: Yue Zhao <zhaoy@cmu.edu>
# Author: Tiankai Yang <tiankaiy@usc.edu>
# License: BSD 2 clause

from __future__ import division
from __future__ import print_function

import os
import sys
Expand All @@ -16,15 +14,14 @@
sys.path.append(
os.path.abspath(os.path.join(os.path.dirname("__file__"), '..')))

from pyod.models.auto_encoder_torch import AutoEncoder
from pyod.models.so_gaal_new import SO_GAAL
from pyod.utils.data import generate_data
from pyod.utils.data import evaluate_print


if __name__ == "__main__":
contamination = 0.1 # percentage of outliers
n_train = 20000 # number of training points
n_test = 2000 # number of testing points
n_train = 30000 # number of training points
n_test = 3000 # number of testing points
n_features = 300 # number of features

# Generate sample data
Expand All @@ -35,9 +32,9 @@
contamination=contamination,
random_state=42)

# train AutoEncoder detector
clf_name = 'AutoEncoder'
clf = AutoEncoder(epochs=10)
# train SO_GAAL detector
clf_name = 'SO_GAAL'
clf = SO_GAAL(epoch_num=6, contamination=contamination, verbose=2)
clf.fit(X_train)

# get the prediction labels and outlier scores of the training data
Expand Down
6 changes: 4 additions & 2 deletions examples/vae_example.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""Example of using Variational Auto Encoder for outlier detection
"""
# Author: Andrij Vasylenko <andrij@liverpool.ac.uk>
# Author: Tiankai Yang <tiankaiy@usc.edu>
# License: BSD 2 clause

from __future__ import division
Expand All @@ -14,6 +14,7 @@
# if pyod is installed, no need to use the following line
sys.path.append(
os.path.abspath(os.path.join(os.path.dirname("__file__"), '..')))
sys.path.append(os.path.abspath(os.path.dirname("__file__")))

from pyod.models.vae import VAE
from pyod.utils.data import generate_data
Expand All @@ -35,7 +36,8 @@

# train VAE detector (Beta-VAE)
clf_name = 'VAE'
clf = VAE(epochs=30, contamination=contamination, gamma=0.8, capacity=0.2)
clf = VAE(epoch_num=30, contamination=contamination, beta=0.8,
capacity=0.2)
clf.fit(X_train)

# get the prediction labels and outlier scores of the training data
Expand Down
4 changes: 2 additions & 2 deletions pyod/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

from . import models
from . import utils
# TODO: add version information here
from .version import __version__

__all__ = ['models', 'utils']
__all__ = ['models', 'utils', '__version__']
21 changes: 20 additions & 1 deletion pyod/models/alad.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,34 @@

import numpy as np
import pandas as pd
import tensorflow
from matplotlib import pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.utils import check_array
from sklearn.utils.validation import check_is_fitted

from .base import BaseDetector
from .base_dl import _get_tensorflow_version
from ..utils.utility import check_parameter


# Old function, deprecat this in the future
def _get_tensorflow_version(): # pragma: no cover
""" Utility function to decide the version of tensorflow, which will
affect how to import keras models.
Returns
-------
tensorflow version : int
"""

tf_version = str(tensorflow.__version__)
if int(tf_version.split(".")[0]) != 1 and int(
tf_version.split(".")[0]) != 2:
raise ValueError("tensorflow version error")

return int(tf_version.split(".")[0]) * 100 + int(tf_version.split(".")[1])

# if tensorflow 2, import from tf directly
if _get_tensorflow_version() < 200:
raise NotImplementedError('Model not implemented for Tensorflow version 1')
Expand Down
22 changes: 21 additions & 1 deletion pyod/models/anogan.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,34 @@
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow
from sklearn.preprocessing import StandardScaler
from sklearn.utils import check_array
from sklearn.utils.validation import check_is_fitted

from .base import BaseDetector
from .base_dl import _get_tensorflow_version
from ..utils.utility import check_parameter


# Old function, deprecat this in the future
def _get_tensorflow_version(): # pragma: no cover
""" Utility function to decide the version of tensorflow, which will
affect how to import keras models.
Returns
-------
tensorflow version : int
"""

tf_version = str(tensorflow.__version__)
if int(tf_version.split(".")[0]) != 1 and int(
tf_version.split(".")[0]) != 2:
raise ValueError("tensorflow version error")

return int(tf_version.split(".")[0]) * 100 + int(tf_version.split(".")[1])


# if tensorflow 2, import from tf directly
if _get_tensorflow_version() < 200:
raise NotImplementedError('Model not implemented for Tensorflow version 1')
Expand Down
Loading

0 comments on commit 4c2730f

Please sign in to comment.