Skip to content

visualization module

cat_plot(indices, plot_per='variable', kind='bar', col_wrap=None, **kwargs)

Uses the seaborn.catplot_ to plot the indices.

Parameters:

Name Type Description Default
indices IndicesOutput

computed indices

required
plot_per str

can be either variable or index, when set to variable there is one graph per variable, each graph showing the values of all indices. Respectively setting to index will build one graph per index, each showing the values for all variable.

'variable'
kind str

kind of visualization to produce, can be one of strip, swarm, box, violin, boxen, point, bar.

'bar'
col_wrap Optional(int

“Wrap” the column variable at this width, so that the column facets span multiple rows.

None
**kwargs

extra arguments passed to seaborn.catplot.

{}

Returns:

Type Description

a matplotlib axes object

Source code in deel\fairsense\visualization\plots.py
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
def cat_plot(
    indices: IndicesOutput, plot_per="variable", kind="bar", col_wrap=None, **kwargs
):
    """
    Uses the `seaborn.catplot`_ to plot the indices.

    Args:
        indices (IndicesOutput): computed indices
        plot_per (str): can be either `variable` or `index`, when set to `variable`
            there is one graph per variable, each graph showing the values of all
            indices. Respectively setting to `index` will build one graph per index,
            each showing the values for all variable.
        kind (str): kind of visualization to produce, can be one of `strip`, `swarm`,
            `box`, `violin`, `boxen`, `point`, `bar`.
        col_wrap (Optional(int)): “Wrap” the column variable at this width, so that
            the column facets span multiple rows.
        **kwargs: extra arguments passed to [seaborn.catplot](
            https://seaborn.pydata.org/generated/seaborn.catplot.html#seaborn.catplot).

    Returns:
        a matplotlib axes object

    """
    assert plot_per.lower().strip() in {"variable", "index", "indices"}
    indices_names = indices.values.columns
    variable_names = indices.values.index
    data = indices.runs.stack().reset_index()
    data.rename(
        columns={"level_0": "variable", "level_1": "index", 0: "value"}, inplace=True
    )
    if plot_per == "variable":
        col = "variable"
        x = "index"
        order = indices_names
    else:
        col = "index"
        x = "variable"
        order = variable_names
    ax = sns.catplot(
        data=data,
        x=x,
        y="value",
        col=col,
        kind=kind,
        col_wrap=col_wrap,
        order=order,
        **kwargs
    )
    ax.set(ylim=(0.0, 1.0))
    ax.set_xticklabels(rotation=45, horizontalalignment="right")
    return ax

format_with_intervals(indices_outputs, quantile=0.05)

Pretty print the indices table with confidence intervals. Note that the intervals are displayed even if the indices are computed without confidence intervals. See :mod:fairsense.indices.confidence_intervals for more information.

Parameters:

Name Type Description Default
indices_outputs IndicesOutput

computed indices quantile (float): quantile used to compute confidence intervals. Values must be in [0., 0.5].

required

the table with indices properly displayed. Note that the table values

Type Description

are now string and not float.

Source code in deel\fairsense\visualization\text.py
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
def format_with_intervals(indices_outputs: IndicesOutput, quantile: float = 0.05):
    """
    Pretty print the indices table with confidence intervals. Note that the intervals
    are displayed even if the indices are computed without confidence intervals. See
    :mod:`fairsense.indices.confidence_intervals` for more information.

    Args:
        indices_outputs (IndicesOutput): computed indices
            quantile (float): quantile used to compute confidence intervals. Values must
            be in [0., 0.5].

    Returns: the table with indices properly displayed. Note that the table values
        are now string and not float.

    """
    means = indices_outputs.runs.groupby(level=0).median().clip(0.0, 1.0)
    low = (
        indices_outputs.runs.groupby(level=0)
        .aggregate(partial(np.quantile, q=quantile))
        .clip(0.0, 1.0)
    )
    high = (
        indices_outputs.runs.groupby(level=0)
        .aggregate(partial(np.quantile, q=1 - quantile))
        .clip(0.0, 1.0)
    )
    table = means.copy()
    for index in means.columns:
        table[index] = np.vectorize(
            lambda index_val, index_inf_val, index_sup_val: "%.2f [%.2f, %.2f]"
            % (index_val, index_inf_val, index_sup_val)
        )(means[index], low[index], high[index])
    return table