def get_fairlearn_page()

in assets/responsibleai/tabular/components/src/_score_card/classification_components.py [0:0]


def get_fairlearn_page(data):
    heading = div(
        p(
            "Understand your model's fairness issues "
            "using group-fairness metrics across sensitive features and cohorts. "
            "Pay particular attention to the cohorts who receive worse treatments "
            "(predictions) by your model."
        ),
        _class="left",
    )

    left_elems = []

    for f in data:
        left_elems.append(h3('Feature "{}"'.format(f)))
        metric_section = []
        for metric_key, metric_details in data[f]["metrics"].items():
            if metric_key in ["false_positive", "false_negative"]:
                continue
            left_elems.append(
                p(
                    '"{}" has the highest {}: {}'.format(
                        metric_details["group_max"][0],
                        metric_key,
                        round(metric_details["group_max"][1], 2),
                    )
                )
            )
            left_elems.append(
                p(
                    '"{}" has the lowest {}: {}'.format(
                        metric_details["group_min"][0],
                        metric_key,
                        round(metric_details["group_min"][1], 2),
                    )
                )
            )
            if metric_details["kind"] == "difference":
                metric_section.append(
                    p(
                        "Maximum difference in {} is {}".format(
                            metric_key,
                            round(
                                metric_details["group_max"][1] -
                                metric_details["group_min"][1],
                                2,
                            ),
                        )
                    )
                )
            elif metric_details["kind"] == "ratio":
                metric_section.append(
                    p(
                        "Minimum ratio of {} is {}".format(
                            metric_key,
                            round(
                                metric_details["group_min"][1] /
                                metric_details["group_max"][1],
                                2,
                            ),
                        )
                    )
                )
        left_elems.append(div(metric_section, _class="nobreak_div"))

    left_container = div(left_elems, _class="left")

    def get_fairness_bar_plot(data):
        y_data = [
            str(c) + "<br>" + str(int(100 * data[c]["population"])) + "% n"
            for c in data
        ]
        x_data = [
            100 *
            get_metric(
                metric="selection_rate",
                y_test=data[c]["y_test"],
                y_pred=data[c]["y_pred"],
                pos_label=data[c]["pos_label"],
            )
            for c in data
        ]
        x_data = [[x, 100 - x] for x in x_data]

        tickvals = [0, 25, 50, 75, 100]
        ticktext = [str(x) + "%" for x in tickvals]

        png_base64 = cc.get_bar_plot(
            y_data, x_data, tickvals=tickvals, ticktext=ticktext, tickappend="%"
        )

        return div(
            img(_src="data:image/png;base64,{}".format(png_base64)),
            _class="image_div",
        )

    def get_table_row(heading, data):
        table_row_elems = []
        table_row_elems.append(th(heading, _class="header_cell"))
        for v in data:
            table_row_elems.append(td(v, _class="cell"))
        return tr(table_row_elems, _class="row")

    def get_table(data):
        metric_list = [d for d in data["metrics"]]
        horizontal_headings = [d.replace("_", "<br>") for d in metric_list]
        vertical_headings = list(data["statistics"].keys())

        headings_td = [td(_class="header_cell")] + [
            td(x, _class="header_cell") for x in horizontal_headings
        ]
        headings = thead(tr(headings_td, _class="row"), _class="table-head")

        rows_elems = []
        for vh in vertical_headings:
            row_data = []
            for m in metric_list:
                row_data.append(round(data["metrics"][m]["group_metric"][vh], 2))
            rows_elems.append(get_table_row(vh, row_data))

        body = tbody(rows_elems, _class="table-body")

        return table(headings, body, _class="table")

    main_elems = []
    # Selection rate
    for f in data:
        main_elems.append(
            div(
                h3("Selection rate"),
                get_fairness_bar_plot(data[f]["statistics"]),
                _class="nobreak_div",
            )
        )

        main_elems.append(
            div(
                h3("Analysis across cohorts"),
                get_table(data[f]),
                _class="nobreak_div",
            )
        )

    main_container = div(main_elems, _class="main")

    return str(
        div(
            cc.get_page_divider("Fairness Assessment"),
            div(heading, _class="container"),
            div(left_container, main_container, _class="container"),
            _class="container nobreak_div",
        )
    )