forked from Trusted-AI/AIF360
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_lfr.py
126 lines (96 loc) · 3.88 KB
/
test_lfr.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import os
import sys
import numpy as np
import pytest
from aif360.algorithms.preprocessing import LFR
from aif360.algorithms.preprocessing.optim_preproc_helpers.data_preproc_functions import load_preproc_data_adult
sys.path.append("../../../")
sys.path.append(os.path.dirname(__file__))
@pytest.fixture(scope="module")
def lfrAlgoInstance():
"""This fixture creates two functions with the scope module lfrAlgoInstance creates an instance of the LFR that can
used by both fit and transform functions.
ad creates a adult data set that will be used by the fit and the transform functions.
"""
privileged_groups = [{'sex': 1.0}]
unprivileged_groups = [{'sex': 0.0}]
lfrAlgoInstance = LFR(unprivileged_groups=unprivileged_groups, privileged_groups=privileged_groups)
return lfrAlgoInstance
@pytest.fixture(scope="module")
def ad():
return load_preproc_data_adult().split([0.7], shuffle=True)[0]
@pytest.fixture(scope="module")
def lfrfitmodel():
privileged_groups = [{'sex': 1.0}]
unprivileged_groups = [{'sex': 0.0}]
lfrAlgoInstance = LFR(unprivileged_groups=unprivileged_groups, privileged_groups=privileged_groups)
ad = load_preproc_data_adult().split([0.7], shuffle=True)[0]
TR = lfrAlgoInstance.fit(ad)
return TR
def test_fit_isnumpy(lfrfitmodel):
"""The Fit function returns a numpy and it should asserted whether it really returned a numpy precision 64 bits.
"""
expected = True
if type(lfrfitmodel.learned_model) is np.ndarray:
res = True
else:
res = False
assert res == expected
def test_fit_notnull(lfrfitmodel):
"""Should not be null.
"""
expected = False
if lfrfitmodel.learned_model is None:
res = True
else:
res = False
print("numpy:" + str(res))
assert res == expected
def test_fit_notallzeros(lfrfitmodel):
"""Should not be all zeros.
"""
expected = False
all_zeros = not np.any(lfrfitmodel)
print("allzeros:" + str(all_zeros))
assert all_zeros == expected
def test_fit_notNaN(lfrfitmodel):
"""Should not have nan's in it.
"""
expected = False
res = np.isnan(lfrfitmodel.learned_model).any()
print("nan:" + str(res))
assert res == expected
# --------------------------------------------------------------------#
# Transform function testing methods
# --------------------------------------------------------------------#
def test_transform_protecteddataset(lfrfitmodel, ad):
"""After transformation - it should not change protected attributes - it should be same as input.
"""
lftransformeddataset = lfrfitmodel.transform(ad, threshold=0.3)
# print( ad.protected_attributes)
print(type(lftransformeddataset.protected_attributes))
# print("transformeddataset:" + lfttransformeddataset.protected_attributes)
expected = True
res = np.array_equal(lftransformeddataset.protected_attributes, ad.protected_attributes)
assert res == expected
def test_transform_notNaN(lfrfitmodel, ad):
"""The transformed data should not have any columns or rows summing upto zero.
"""
lftransformeddataset = lfrfitmodel.transform(ad, threshold=0.3)
lstrowsum = np.sum(lftransformeddataset.features, axis=1).tolist()
expected = False
allrow_zeros = not np.any(lstrowsum)
lstcolsum = np.sum(lftransformeddataset.features, axis=0).tolist()
allcol_zeros = not np.any(lstcolsum)
assert (allrow_zeros and allcol_zeros) == expected
def test_transform_notNaN2(lfrfitmodel, ad):
"""The transformed data should not contain nan's. Using the threshold value of 0.3.
"""
lftransformeddataset = lfrfitmodel.transform(ad, threshold=0.3)
expected = False
res = np.isnan(lftransformeddataset.features).any()
assert res == expected