Skip to content

Commit a25b22d

Browse files
authored
fix #41 (#42)
1 parent 5668cb0 commit a25b22d

File tree

7 files changed

+48
-6
lines changed

7 files changed

+48
-6
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: fairmodels
22
Type: Package
33
Title: Flexible Tool for Bias Detection, Visualization, and Mitigation
4-
Version: 1.1.0
4+
Version: 1.1.1
55
Authors@R:
66
c(person("Jakub", "Wiśniewski", role = c("aut", "cre"),
77
email = "[email protected]"),

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# fairmodels 1.1.1
2+
* Fixed error which appeared when 2 fairness objects had the same labels in them. Now if this appears it throws an error. [(#41)](https://github.com/ModelOriented/fairmodels/issues/41)
3+
* `privileged` parameter is now converted to character. [(#41)](https://github.com/ModelOriented/fairmodels/issues/41)
4+
* `reweight()` function now accepts factors [(#41)](https://github.com/ModelOriented/fairmodels/issues/41)
5+
16
# fairmodels 1.1.0
27
* Added function `fairness_check_regression()` that supports regression models along with 2 plot types [(#38)](https://github.com/ModelOriented/fairmodels/issues/38).
38
* Added additional tests.

R/fairness_check.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ fairness_check <- function(x,
150150
fobjects_fcheck_data <- extract_data(fobjects, "fairness_check_data")
151151
fobjects_cf <- extract_data(fobjects, "groups_confusion_matrices")
152152

153-
fobjects_label <- sapply(fobjects, function(x) x$label)
153+
fobjects_label <- unlist(lapply(fobjects, function(x) x$label))
154154
fobjects_cuttofs <- extract_data(fobjects, "cutoff")
155155
n_exp <- length(explainers)
156156

R/helper_functions.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ check_privileged <- function(privileged, fobjects, verbose) {
232232
} else {
233233
verbose_cat("character (", verbose = verbose)
234234
verbose_cat(color_codes$yellow_start, "changed from", class(privileged), color_codes$yellow_end, ")\n", verbose = verbose)
235+
privileged <- as.character(privileged)
235236
}
236237
}
237238

@@ -302,6 +303,7 @@ check_explainers <- function(all_explainers, protected, verbose){
302303
stop("All explainers must have same values of target variable")
303304
}
304305

306+
305307
if(! all(sapply(all_explainers, function(x) length(x$y) == length(protected)))) {
306308
verbose_cat("(", color_codes$red_start, "not compatible", color_codes$red_end, ")\n", verbose = verbose)
307309
stop("Lengths of protected variable and target variable in explainer differ")
@@ -325,6 +327,12 @@ check_labels <- function(label, explainers, fobjects_label){
325327
( pass paramter \'label\' to fairness_check() or before to explain() function)")
326328
}
327329

330+
# fobjects must have unique labels
331+
if (length(unique(fobjects_label)) != length(fobjects_label) ) {
332+
stop("Fairness objects don't have unique labels
333+
( make sure that Fairness Objects will have unique labels of explainers before running fainress check")
334+
}
335+
328336
# labels must be unique for all explainers, those in fairness objects too
329337
if (any(label %in% fobjects_label)) {
330338
stop("Explainer has the same label as label in fairness_object")

R/reweight.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@
4646

4747

4848
reweight <- function(protected, y){
49+
50+
if (! is.factor(protected)) {
51+
cat("\nchanging protected to factor \n")
52+
protected <- as.factor(protected)
53+
}
54+
55+
4956
stopifnot(is.factor(protected))
5057
stopifnot(is.numeric(y))
5158
stopifnot(length(y) == length(protected))

tests/testthat/test_fairness_check.R

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ test_that("Test fairness_check", {
3434
cutoff = 0.5,
3535
colorize = FALSE))
3636

37+
expect_equal(fairness_check(explainer_glm, explainer_rf,
38+
protected = as.numeric(compas$Sex)-1,
39+
privileged = 0,
40+
cutoff = 0.5,
41+
verbose = FALSE),
42+
fairness_check(explainer_glm, explainer_rf,
43+
protected = as.numeric(compas$Sex)-1,
44+
privileged = "0",
45+
cutoff = 0.5,
46+
colorize = FALSE))
47+
48+
49+
3750
# errors
3851

3952
expect_error(fairness_check(explainer_glm, explainer_rf,
@@ -69,11 +82,15 @@ test_that("Test fairness_check", {
6982
cutoff = list(female = 0.5),
7083
epsilon = c(0.3, 0.5)))
7184

85+
fobject2 <- fobject
86+
expect_error(fairness_check(fobject, fobject2))
87+
88+
7289
exp2 <- explainer_glm
7390
exp2$model_info$type <- 'regression'
7491
expect_error(fairness_check(exp2, fobject))
7592

76-
fobject2 <- fobject
93+
7794
fobject2$protected <- compas$Sex[1:6000]
7895
suppressWarnings( expect_error(fairness_check(fobject, fobject2)))
7996
suppressWarnings( expect_error(fairness_check(fobject, fobject2, privileged = "Female")))

tests/testthat/test_reweight.R

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ expect_equal(round(predicted_weights,2), actual_weights)
99
y_numeric <- as.numeric(compas$Two_yr_Recidivism)-1
1010

1111
# numeric protected
12-
expect_error(reweight(compas$Number_of_Priors, y_numeric))
13-
# non numeric y
14-
expect_error(reweight(compas$Sex, compas$Two_yr_Recidivism))
12+
13+
expect_equal(reweight(compas$Sex, y_numeric),
14+
reweight(as.factor(compas$Sex), y_numeric))
15+
16+
expect_equal(reweight(compas$Sex, y_numeric),
17+
reweight(as.numeric(compas$Sex), y_numeric))
18+
19+
1520
# difference in length
1621
expect_error(reweight(compas$Sex[,-1], y_numeric))
1722
# y must be 0 and 1

0 commit comments

Comments
 (0)