import numpy as np
from scipy import stats

from oneclass import oneclass

from sklearn.tree import DecisionTreeClassifier

rng = np.random.RandomState(42)

# Example settings
n_samples = 2000
outliers_fraction = 0.25
clusters_separation = [0,1,2]

# define two or more outlier detection tools to be compared
classifiers = {    
    "One-Class": oneclass.OneClassClassifier(contamination=outliers_fraction,base_classifier=DecisionTreeClassifier(max_depth=2),density_only=True)
    }

# Compare given classifiers under given settings
xx, yy = np.meshgrid(np.linspace(-7, 7, 500), np.linspace(-7, 7, 500))
n_inliers = int((1. - outliers_fraction) * n_samples)
n_outliers = int(outliers_fraction * n_samples)
ground_truth = np.ones(n_samples, dtype=int)
ground_truth[-n_outliers:] = -1

# Fit the problem with varying cluster separation
for i, offset in enumerate(clusters_separation):
    np.random.seed(42)
    # Data generation
    X1 = 0.3 * np.random.randn(n_inliers // 2, 2) - offset
    X2 = 0.3 * np.random.randn(n_inliers // 2, 2) + offset
    X = np.r_[X1, X2]
    # Add outliers
    X = np.r_[X, np.random.uniform(low=-6, high=6, size=(n_outliers, 2))]

    # Fit the model
    for i, (clf_name, clf) in enumerate(classifiers.items()):
        # fit the data and tag outliers
        clf.fit(X)
        scores_pred = clf.decision_function(X)
        threshold = stats.scoreatpercentile(scores_pred,
                                            100 * outliers_fraction)
        y_pred = clf.predict(X)
        n_errors = (y_pred != ground_truth).sum()
        # plot the levels lines and the points
        print "Cluster separation: ", offset, " Number of errors: ", n_errors