|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Copyright 2023 Fujitsu Limited |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +import pandas as pd |
| 19 | +import matplotlib.pyplot as plt |
| 20 | +from matplotlib.gridspec import GridSpec |
| 21 | +import seaborn as sns |
| 22 | + |
| 23 | +from aif360.algorithms.isf_helpers.isf_metrics.disparate_impact import DisparateImpact |
| 24 | +from aif360.algorithms.isf_helpers.isf_utils.common import create_multi_group_label |
| 25 | + |
| 26 | + |
| 27 | +def calc_intersectionalbias(dataset, metric="DisparateImpact"): |
| 28 | + """ |
| 29 | + Calculate intersectional bias(DisparateImpact) by more than one sensitive attributes |
| 30 | +
|
| 31 | + Parameters |
| 32 | + ---------- |
| 33 | + dataset : StructuredDataset |
| 34 | + A dataset containing more than one sensitive attributes |
| 35 | +
|
| 36 | + metric : str |
| 37 | + Fairness metric name |
| 38 | + ["DisparateImpact"] |
| 39 | +
|
| 40 | + Returns |
| 41 | + ------- |
| 42 | + df_result : DataFrame |
| 43 | + Intersectional bias(DisparateImpact) |
| 44 | + """ |
| 45 | + |
| 46 | + df = dataset.convert_to_dataframe()[0] |
| 47 | + label_info = {dataset.label_names[0]: dataset.favorable_label} |
| 48 | + |
| 49 | + if metric == "DisparateImpact": |
| 50 | + fs = DisparateImpact() |
| 51 | + else: |
| 52 | + raise ValueError("metric name not in the list of allowed metrics") |
| 53 | + |
| 54 | + df_result = pd.DataFrame(columns=[metric]) |
| 55 | + for multi_group_label in create_multi_group_label(dataset)[0]: |
| 56 | + protected_attr_info = multi_group_label[0] |
| 57 | + di = fs.bias_predict(df, |
| 58 | + protected_attr_info=protected_attr_info, |
| 59 | + label_info=label_info) |
| 60 | + name = '' |
| 61 | + for k, v in protected_attr_info.items(): |
| 62 | + name += k + " = " + str(v) + "," |
| 63 | + df_result.loc[name[:-1]] = di |
| 64 | + |
| 65 | + return df_result |
| 66 | + |
| 67 | + |
| 68 | +def plot_intersectionalbias_compare(ds_bef, ds_aft, vmax=1, vmin=0, center=0, |
| 69 | + metric="DisparateImpact", |
| 70 | + title={"right": "before", "left": "after"}, |
| 71 | + filename=None): |
| 72 | + """ |
| 73 | + Compare drawing of intersectional bias in heat map |
| 74 | +
|
| 75 | + Parameters |
| 76 | + ---------- |
| 77 | + ds_bef : StructuredDataset |
| 78 | + Dataset containing two sensitive attributes (left figure) |
| 79 | + ds_aft : StructuredDataset |
| 80 | + Dataset containing two sensitive attributes (right figure) |
| 81 | + filename : str, optional |
| 82 | + File name(png) |
| 83 | + e.g. "./result/pict.png" |
| 84 | + metric : str |
| 85 | + Fairness metric name |
| 86 | + ["DisparateImpact"] |
| 87 | + title : dictonary, optional |
| 88 | + Graph title (right figure, left figure) |
| 89 | + """ |
| 90 | + |
| 91 | + df_bef = calc_intersectionalbias_matrix(ds_bef, metric) |
| 92 | + df_aft = calc_intersectionalbias_matrix(ds_aft, metric) |
| 93 | + |
| 94 | + gs = GridSpec(1, 2) |
| 95 | + ss1 = gs.new_subplotspec((0, 0)) |
| 96 | + ss2 = gs.new_subplotspec((0, 1)) |
| 97 | + |
| 98 | + ax1 = plt.subplot(ss1) |
| 99 | + ax2 = plt.subplot(ss2) |
| 100 | + |
| 101 | + ax1.set_title(title['right']) |
| 102 | + sns.heatmap(df_bef, ax=ax1, vmax=vmax, vmin=vmin, center=center, annot=True, cmap='hot') |
| 103 | + |
| 104 | + ax2.set_title(title['left']) |
| 105 | + sns.heatmap(df_aft, ax=ax2, vmax=vmax, vmin=vmin, center=center, annot=True, cmap='hot') |
| 106 | + |
| 107 | + if filename is not None: |
| 108 | + plt.savefig(filename, format="png", dpi=300) |
| 109 | + plt.show() |
| 110 | + |
| 111 | + |
| 112 | +def calc_intersectionalbias_matrix(dataset, metric="DisparateImpact"): |
| 113 | + """ |
| 114 | + Comparison drawing of intersectional bias in heat map |
| 115 | +
|
| 116 | + Parameters |
| 117 | + ---------- |
| 118 | + dataset : StructuredDataset |
| 119 | + Dataset containing two sensitive attributes |
| 120 | + metric : str |
| 121 | + Fairness metric name |
| 122 | + ["DisparateImpact"] |
| 123 | +
|
| 124 | + Returns |
| 125 | + ------- |
| 126 | + df_result : DataFrame |
| 127 | + Intersectional bias(DisparateImpact) |
| 128 | + """ |
| 129 | + |
| 130 | + protect_attr = dataset.protected_attribute_names |
| 131 | + |
| 132 | + if len(protect_attr) != 2: |
| 133 | + raise ValueError("specify 2 sensitive attributes.") |
| 134 | + |
| 135 | + if metric == "DisparateImpact": |
| 136 | + fs = DisparateImpact() |
| 137 | + else: |
| 138 | + raise ValueError("metric name not in the list of allowed metrics") |
| 139 | + |
| 140 | + df = dataset.convert_to_dataframe()[0] |
| 141 | + label_info = {dataset.label_names[0]: dataset.favorable_label} |
| 142 | + |
| 143 | + protect_attr0_values = list(set(df[protect_attr[0]])) |
| 144 | + protect_attr1_values = list(set(df[protect_attr[1]])) |
| 145 | + |
| 146 | + df_result = pd.DataFrame(columns=protect_attr1_values) |
| 147 | + |
| 148 | + for val0 in protect_attr0_values: |
| 149 | + tmp_li = [] |
| 150 | + col_list = [] |
| 151 | + for val1 in protect_attr1_values: |
| 152 | + di = fs.bias_predict(df, |
| 153 | + protected_attr_info={protect_attr[0]: val0, protect_attr[1]: val1}, |
| 154 | + label_info=label_info) |
| 155 | + tmp_li += [di] |
| 156 | + col_list += [protect_attr[1]+"="+str(val1)] |
| 157 | + |
| 158 | + df_result.loc[protect_attr[0]+"="+str(val0)] = tmp_li |
| 159 | + df_result = df_result.set_axis(col_list, axis=1) |
| 160 | + |
| 161 | + return df_result |
0 commit comments