File size: 1,295 Bytes
fc0f7bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import numpy as np
import pytest

from fairlearn.postprocessing import ThresholdOptimizer
from fairlearn.postprocessing._threshold_optimizer import _SUPPORTED_CONSTRAINTS
from fairlearn.postprocessing._constants import _MATPLOTLIB_IMPORT_ERROR_MESSAGE


class FakePredictor:
    def predict(self, X):
        return np.random.random(len(X))


@pytest.mark.parametrize("constraints", _SUPPORTED_CONSTRAINTS)
def test_no_matplotlib(constraints):
    n_samples = 50
    n_features = 50
    n_sensitive_feature_values = 2
    n_classes = 2

    threshold_optimizer = ThresholdOptimizer(unconstrained_predictor=FakePredictor(),
                                             constraints=constraints,
                                             plot=True)
    with pytest.raises(RuntimeError) as exc:
        threshold_optimizer.fit(X=np.random.random((n_samples, n_features)),
                                y=np.random.randint(n_classes, size=n_samples),
                                sensitive_features=np.random.randint(n_sensitive_feature_values,
                                                                     size=n_samples))
        assert str(exc.value) == _MATPLOTLIB_IMPORT_ERROR_MESSAGE