<commit_before>import numpy as np
from unittest import TestCase
from sklearn.utils.validation import check_is_fitted
from sklearn.exceptions import NotFittedError

from diffprivlib.models.forest import DecisionTreeClassifier, get_cat_features, get_feature_domains, calc_tree_depth
from diffprivlib.utils import PrivacyLeakWarning, global_seed


class TestDecisionTreeClassifier(TestCase):
    def setUp(self):
        global_seed(2718281828)

    def test_not_none(self):
        self.assertIsNotNone(DecisionTreeClassifier)

    def test_bad_params(self):
        X = [[1]]
        y = [0]
        
        with self.assertRaises(ValueError):
            DecisionTreeClassifier(cat_feature_threshold="5").fit(X, y)

    def test_bad_data(self):
        with self.assertRaises(ValueError):
            DecisionTreeClassifier(feature_domains={'0': [0, 1]}).fit([[1]], None)

        with self.assertRaises(ValueError):
            DecisionTreeClassifier(feature_domains={'0': [0, 2]}).fit([[1], [2]], [1])

        with self.assertRaises(ValueError):
            DecisionTreeClassifier(feature_domains={'0': [0, 2]}).fit([[1], [2]], [[1, 2], [2, 4]])

    def test_simple(self):
        X = np.array([[12, 3, 14], [12, 3, 4], [12, 3, 4], [2, 13, 4], [2, 13, 14], [2, 3, 14], [3, 5, 15]])
        y = np.array([1, 1, 1, 0, 0, 0, 1])
        model = DecisionTreeClassifier(epsilon=5, cat_feature_threshold=2, max_depth=5)
        with self.assertRaises(NotFittedError):
            check_is_fitted(model)
        # when `feature_domains` is not provided, we should get a privacy leakage warning
        with self.assertWarns(PrivacyLeakWarning):
            model.fit(X, y)
        check_is_fitted(model)
        self.assertTrue(model.predict(np.array([[12, 3, 14]])))

    def test_with_feature_domains(self):
        X = np.array([[12, 3, 14], [12, 3, 4], [12, 3, 4], [2, 13, 4], [2, 13, 14], [2, 3, 14], [3, 5, 15]] * 3)
        y = np.array([1, 1, 1, 0, 0, 0, 1] * 3)
        model = DecisionTreeClassifier(epsilon=5, cat_feature_threshold=2, max_depth=5,
                                       feature_domains={'0': [2.0, 12.0], '1': [3.0, 13.0], '2': [4.0, 15.0]})
        with self.assertRaises(NotFittedError):
            check_is_fitted(model)
        model.fit(X, y)
        check_is_fitted(model)
        self.assertTrue(model.predict(np.array([[12, 3, 14]])))

    def test_with_non_binary_labels(self):
        X = np.array([[12, 3, 14], [12, 3, 4], [12, 3, 4], [2, 13, 4], [2, 13, 14], [2, 3, 14], [3, 5, 15]])
        y = np.array([3, 3, 3, 3, 5, 5, 3])
        model = DecisionTreeClassifier(epsilon=5, cat_feature_threshold=2, max_depth=5,
                                       feature_domains={'0': [2.0, 12.0], '1': [3.0, 13.0], '2': [4.0, 15.0]})
        with self.assertRaises(NotFittedError):
            check_is_fitted(model)
        model.fit(X, y)
        check_is_fitted(model)
        self.assertEqual(model.predict(np.array([[12, 3, 14]])), 3)

<commit_msg>More test fixes<commit_after>import numpy as np
from unittest import TestCase
from sklearn.utils.validation import check_is_fitted
from sklearn.exceptions import NotFittedError

from diffprivlib.models.forest import DecisionTreeClassifier, get_cat_features, get_feature_domains, calc_tree_depth
from diffprivlib.utils import PrivacyLeakWarning, global_seed


class TestDecisionTreeClassifier(TestCase):
    def setUp(self):
        global_seed(2718281828)

    def test_not_none(self):
        self.assertIsNotNone(DecisionTreeClassifier)

    def test_bad_params(self):
        X = [[1]]
        y = [0]
        
        with self.assertRaises(ValueError):
            DecisionTreeClassifier(cat_feature_threshold="5").fit(X, y)

    def test_bad_data(self):
        with self.assertRaises(ValueError):
            DecisionTreeClassifier(feature_domains={'0': [0, 1]}).fit([[1]], None)

        with self.assertRaises(ValueError):
            DecisionTreeClassifier(feature_domains={'0': [0, 2]}).fit([[1], [2]], [1])

        with self.assertRaises(ValueError):
            DecisionTreeClassifier(feature_domains={'0': [0, 2]}).fit([[1], [2]], [[1, 2], [2, 4]])

    def test_simple(self):
        X = np.array([[12, 3, 14], [12, 3, 4], [12, 3, 4], [2, 13, 4], [2, 13, 14], [2, 3, 14], [3, 5, 15]] * 3)
        y = np.array([1, 1, 1, 0, 0, 0, 1] * 3)
        model = DecisionTreeClassifier(epsilon=5, cat_feature_threshold=2, max_depth=5, random_state=25)
        with self.assertRaises(NotFittedError):
            check_is_fitted(model)
        # when `feature_domains` is not provided, we should get a privacy leakage warning
        with self.assertWarns(PrivacyLeakWarning):
            model.fit(X, y)
        check_is_fitted(model)
        self.assertTrue(model.predict(np.array([[12, 3, 14]])))

    def test_with_feature_domains(self):
        X = np.array([[12, 3, 14], [12, 3, 4], [12, 3, 4], [2, 13, 4], [2, 13, 14], [2, 3, 14], [3, 5, 15]] * 3)
        y = np.array([1, 1, 1, 0, 0, 0, 1] * 3)
        model = DecisionTreeClassifier(epsilon=5, cat_feature_threshold=2, max_depth=5, random_state=25,
                                       feature_domains={'0': [2.0, 12.0], '1': [3.0, 13.0], '2': [4.0, 15.0]})
        with self.assertRaises(NotFittedError):
            check_is_fitted(model)
        model.fit(X, y)
        check_is_fitted(model)
        self.assertTrue(model.predict(np.array([[12, 3, 14]])))

    def test_with_non_binary_labels(self):
        X = np.array([[12, 3, 14], [12, 3, 4], [12, 3, 4], [2, 13, 4], [2, 13, 14], [2, 3, 14], [3, 5, 15]] * 3)
        y = np.array([3, 3, 3, 3, 5, 5, 3] * 3)
        model = DecisionTreeClassifier(epsilon=5, cat_feature_threshold=2, max_depth=5, random_state=25,
                                       feature_domains={'0': [2.0, 12.0], '1': [3.0, 13.0], '2': [4.0, 15.0]})
        with self.assertRaises(NotFittedError):
            check_is_fitted(model)
        model.fit(X, y)
        check_is_fitted(model)
        self.assertEqual(model.predict(np.array([[12, 3, 14]])), 3)


{'fraction_non_alphanumeric': 0.16095380029806258, 'fraction_numerical': 0.06987911905944692, 'mean_word_length': 2.901808785529716, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5553329', 'n_tokens_mistral': 2433, 'n_tokens_neox': 2147, 'n_words': 491}