def __create_tooltip_variables(attribute_df, metric, fairness_threshold): """ Creates disparity explanation and formatted group size tooltip variables. """ # PARITY TEST EXPLANATION attribute_df[ f"tooltip_parity_test_explanation_{metric}"] = attribute_df.apply( lambda row: get_tooltip_text_parity_test_explanation( row[f"{metric}_parity_result"], metric, fairness_threshold, ), axis=1, ) # DISPARITY EXPLANATION ref_group = attribute_df["ref_group_value"].iloc[0] attribute_df[ f"tooltip_disparity_explanation_{metric}"] = attribute_df.apply( lambda row: get_tooltip_text_disparity_explanation( row[f"{metric}_disparity_scaled"], row["attribute_value"], metric, ref_group, ), axis=1, ) # FORMATTED GROUP SIZE attribute_df["tooltip_group_size"] = attribute_df.apply( lambda row: get_tooltip_text_group_size(row["group_size"], row[ "total_entities"]), axis=1, )
def __draw_bubbles( plot_table, metrics, ref_group, scales, selection, ): """Draws the bubbles for all metrics.""" # X AXIS GRIDLINES axis_values = [0.25, 0.5, 0.75] x_axis = alt.Axis( values=axis_values, ticks=False, domain=False, labels=False, title=None ) # COLOR bubble_color_encoding = alt.condition( selection, alt.Color("attribute_value:N", scale=scales["color"], legend=None), alt.value(Bubble.color_faded), ) # CHART INITIALIZATION bubble_centers = alt.Chart().mark_point() bubble_areas = alt.Chart().mark_circle() plot_table["tooltip_group_size"] = plot_table.apply( lambda row: get_tooltip_text_group_size( row["group_size"], row["total_entities"] ), axis=1, ) # LAYERING THE METRICS for metric in metrics: # TOOLTIP plot_table[f"tooltip_disparity_explanation_{metric}"] = plot_table.apply( lambda row: get_tooltip_text_disparity_explanation( row[f"{metric}_disparity_scaled"], row["attribute_value"], metric, ref_group, ), axis=1, ) bubble_tooltip_encoding = [ alt.Tooltip(field="attribute_value", type="nominal", title="Group"), alt.Tooltip(field="tooltip_group_size", type="nominal", title="Group Size"), alt.Tooltip( field=f"tooltip_disparity_explanation_{metric}", type="nominal", title="Disparity", ), alt.Tooltip( field=f"{metric}", type="quantitative", format=".2f", title=f"{metric}".upper(), ), ] # BUBBLE CENTERS trigger_centers = alt.selection_multi(empty="all", fields=["attribute_value"]) bubble_centers += ( alt.Chart(plot_table) .transform_calculate(metric_variable=f"'{metric.upper()}'") .mark_point(filled=True, size=Bubble.center_size) .encode( x=alt.X(f"{metric}:Q", scale=scales["x"], axis=x_axis), y=alt.Y("metric_variable:N", scale=scales["y"], axis=no_axis()), tooltip=bubble_tooltip_encoding, color=bubble_color_encoding, shape=alt.Shape( "attribute_value:N", scale=scales["shape"], legend=None ), ) .add_selection(trigger_centers) ) # BUBBLE AREAS trigger_areas = alt.selection_multi(empty="all", fields=["attribute_value"]) bubble_areas += ( alt.Chart(plot_table) .mark_circle(opacity=Bubble.opacity) .transform_calculate(metric_variable=f"'{metric.upper()}'") .encode( x=alt.X(f"{metric}:Q", scale=scales["x"], axis=x_axis), y=alt.Y("metric_variable:N", scale=scales["y"], axis=no_axis()), tooltip=bubble_tooltip_encoding, color=bubble_color_encoding, size=alt.Size("group_size:Q", legend=None, scale=scales["bubble_size"]), ) .add_selection(trigger_areas) ) return bubble_areas + bubble_centers