Skip to content

dataclasses module

IndicesInput

Source code in deel\fairsense\utils\dataclasses.py
 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
class IndicesInput:
    def __init__(
        self,
        model: Optional[Callable] = None,
        x: Optional[DataFrame] = None,
        y_true: Optional[DataFrame] = None,
        objective: Callable = None,
        variable_groups: List[List[str]] = None,
    ):
        """
        Build an IndiceInput object.

        Args:
            model: function that can be applied on x, that return a series with
            same shape as y_true.
            x: a dataframe containing the samples to analyse.
            y_true: a dataframe containing the labels of the samples (in the same
                order)
            objective: one of the target from the utils.fairness_objective module.
            variable_groups: list of list, containing the name of the columns that
                should be grouped.
        """

        self.model = model
        self._variable_groups = variable_groups
        self._x = x
        self._x.columns = [str(c) for c in x.columns]
        self._y_true = y_true
        self.objective = objective

    @property
    def x(self):
        # indice_input.x returns a copy of the data
        return self._x.copy()

    def compute_objective(self, x=None):
        """
        Compute the objective, using available data.
        When objective is y_true, y_true is returned, when objective is y_pred,
        the model is applied on x, and other objective compute the difference between
        y_true and y_pred.

        Args:
            x: the sample to compute the objective on. When None, `self.x` is used.

        Returns:
            the value of the objective.

        """
        return self.objective(self, x)

    @property
    def y_true(self):
        return self._y_true.copy() if self._y_true is not None else None

    @y_true.setter
    def y_true(self, _y):
        # this setter ensures that y_true is a dataframe and not a series
        if _y is None:
            self._y_true = None
        else:
            if len(_y.shape) < 2:
                _y = np.expand_dims(_y, -1)
                self._y_true = DataFrame(_y, columns=["outputs"])
            elif isinstance(_y, DataFrame):
                self._y_true = _y
            else:
                self._y_true = DataFrame(_y, columns=["outputs"])

    @property
    def variable_groups(self):
        if self._variable_groups is None:
            return [[str(var)] for var in self._x.columns]
        else:
            return self._variable_groups

    @property
    def merged_groups(self):
        return [x[0].split("=")[0] for x in self.variable_groups]

__init__(model=None, x=None, y_true=None, objective=None, variable_groups=None)

Build an IndiceInput object.

Parameters:

Name Type Description Default
model Optional[Callable]

function that can be applied on x, that return a series with

None
x Optional[DataFrame]

a dataframe containing the samples to analyse.

None
y_true Optional[DataFrame]

a dataframe containing the labels of the samples (in the same order)

None
objective Callable

one of the target from the utils.fairness_objective module.

None
variable_groups List[List[str]]

list of list, containing the name of the columns that should be grouped.

None
Source code in deel\fairsense\utils\dataclasses.py
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
def __init__(
    self,
    model: Optional[Callable] = None,
    x: Optional[DataFrame] = None,
    y_true: Optional[DataFrame] = None,
    objective: Callable = None,
    variable_groups: List[List[str]] = None,
):
    """
    Build an IndiceInput object.

    Args:
        model: function that can be applied on x, that return a series with
        same shape as y_true.
        x: a dataframe containing the samples to analyse.
        y_true: a dataframe containing the labels of the samples (in the same
            order)
        objective: one of the target from the utils.fairness_objective module.
        variable_groups: list of list, containing the name of the columns that
            should be grouped.
    """

    self.model = model
    self._variable_groups = variable_groups
    self._x = x
    self._x.columns = [str(c) for c in x.columns]
    self._y_true = y_true
    self.objective = objective

compute_objective(x=None)

Compute the objective, using available data. When objective is y_true, y_true is returned, when objective is y_pred, the model is applied on x, and other objective compute the difference between y_true and y_pred.

Parameters:

Name Type Description Default
x

the sample to compute the objective on. When None, self.x is used.

None

Returns:

Type Description

the value of the objective.

Source code in deel\fairsense\utils\dataclasses.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def compute_objective(self, x=None):
    """
    Compute the objective, using available data.
    When objective is y_true, y_true is returned, when objective is y_pred,
    the model is applied on x, and other objective compute the difference between
    y_true and y_pred.

    Args:
        x: the sample to compute the objective on. When None, `self.x` is used.

    Returns:
        the value of the objective.

    """
    return self.objective(self, x)

IndicesOutput

Source code in deel\fairsense\utils\dataclasses.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
class IndicesOutput:
    def __init__(self, values: DataFrame):
        """
        Encapsulate the results of the analysis. Every function from the indices
        module returns an object of this type.
        This object override the `+` operator allorw to combine result more easily.

        Args:
            values: a DataFrame containing the values of the indices. When confidence
            intervals are enabled this dataframe contains the results of each split.
        """
        self.runs: DataFrame = values  # 2D dataframe: lines=variable groups

    @property
    def values(self):
        return self.runs.groupby(level=0).median().clip(0.0, 1.0)

    def __add__(self, other):
        # indices must be computed on same groups
        assert other.runs.shape[0] == self.runs.shape[0]
        new_values = self.runs.copy()
        new_values[other.runs.columns] = other.runs
        return IndicesOutput(new_values)

__init__(values)

Encapsulate the results of the analysis. Every function from the indices module returns an object of this type. This object override the + operator allorw to combine result more easily.

Parameters:

Name Type Description Default
values DataFrame

a DataFrame containing the values of the indices. When confidence

required
Source code in deel\fairsense\utils\dataclasses.py
113
114
115
116
117
118
119
120
121
122
123
def __init__(self, values: DataFrame):
    """
    Encapsulate the results of the analysis. Every function from the indices
    module returns an object of this type.
    This object override the `+` operator allorw to combine result more easily.

    Args:
        values: a DataFrame containing the values of the indices. When confidence
        intervals are enabled this dataframe contains the results of each split.
    """
    self.runs: DataFrame = values  # 2D dataframe: lines=variable groups