File size: 2,954 Bytes
d2a8669
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#' AIF360 dataset
#' @description
#' Function to create AIF compatible dataset.
#' @param data_path Path to the input CSV file or a R dataframe.
#' @param favor_label Label value which is considered favorable (i.e. “positive”).
#' @param unfavor_label Label value which is considered unfavorable (i.e. “negative”).
#' @param unprivileged_protected_attribute A unprotected attribute value which is considered privileged from a fairness perspective.
#' @param privileged_protected_attribute A protected attribute value which is considered privileged from a fairness perspective.
#' @param target_column Name describing the label.
#' @param protected_attribute A feature for which fairness is desired.
#' @usage
#' binary_label_dataset(data_path,  favor_label, unfavor_label,
#'                      unprivileged_protected_attribute,
#'                      privileged_protected_attribute,
#'                      target_column, protected_attribute)
#' @examples
#' \dontrun{
#' load_aif360_lib()
#' # Input dataset
#' data <- data.frame("feat" = c(0,0,1,1,1,1,0,1,1,0), "label" = c(1,0,0,1,0,0,1,0,1,1))
#' # Create aif compatible input dataset
#' act <- aif360::binary_label_dataset(data_path = data,  favor_label=0, unfavor_label=1,
#'                             unprivileged_protected_attribute=0,
#'                             privileged_protected_attribute=1,
#'                             target_column="label", protected_attribute="feat")
#' }
#' @seealso
#' \href{https://aif360.readthedocs.io/en/latest/modules/datasets.html#binary-label-dataset}{More about AIF binary dataset.}
#' @export
#' @importFrom reticulate py_suppress_warnings py_dict r_to_py
#' @importFrom utils file_test
#'
binary_label_dataset <- function(data_path, favor_label,
                        unfavor_label, unprivileged_protected_attribute,
                        privileged_protected_attribute,
                        target_column, protected_attribute) {

  if (is.data.frame(data_path)) {
    dataframe <- r_to_py(data_path)
  } else if (file_test("-f", data_path) == TRUE) {
    dataframe = input_data(data_path)
  }
  unprivileged_protected_list <- list_of_list(unprivileged_protected_attribute)
  privileged_protected_list <- list_of_list(privileged_protected_attribute)
  target_column_list <- list_fn(target_column)
  protected_attribute_list <- list_fn(protected_attribute)

  return(datasets$BinaryLabelDataset(df = dataframe,
                                     favorable_label = favor_label,
                                     unfavorable_label = unfavor_label,
                                     unprivileged_protected_attributes = unprivileged_protected_list,
                                     privileged_protected_attributes = privileged_protected_list,
                                     label_names = target_column_list,
                                     protected_attribute_names = protected_attribute_list))

}