Spaces:
Build error
Build error
File size: 6,800 Bytes
90f4ec6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# --- Visualization ---
import altair as alt
import streamlit as st
import plotly.graph_objects as go
from streamlit_vega_lite import altair_component
# --- Data ---
import pandas as pd
def base_chart(df, linked_vis=False, max_width=150, col_val=None,min_size=100,size_domain=[]):
''' Visualize the model's performance across susbets of the data'''
#Defining populations in the data
pop_domain = ["Overall Performance","Custom Slice","User Custom Sentence","US Protected Class"]
color_range = ["#5778a4", "#e49444", "#b8b0ac","#85b6b2"]
#being chart
base = alt.Chart(df)
if linked_vis:
selected = alt.selection_single(
on="click", empty="none", fields=["name", "source"]
)
base = base.add_selection(selected)
base = (
base.mark_bar().encode(
alt.X("metric_value",
scale=alt.Scale(domain=(0, 1)), title=""
),
alt.Y("displayName", title=""),
alt.Column("metric_type", title=""),
alt.StrokeWidth("size:N",
scale=alt.Scale(domain=size_domain,range=[0,1.25]),
title="#sentences"
),
alt.StrokeOpacity("size:N",
scale=alt.Scale(domain=size_domain,range=[0,1])
),
alt.Stroke("size:N",
scale=alt.Scale(domain=size_domain,range=["white","red"]),
),
alt.Fill("source",
scale = alt.Scale(domain = pop_domain,
range=color_range),
title = "Data Subpopulation"),
opacity=alt.condition(selected, alt.value(1), alt.value(0.5)),
tooltip=["name", "metric_type", "metric_value"]
).properties(width=125
).configure_axis(
labelFontSize=14
).
configure_legend(
labelFontSize=14
)
)
else:
#This is now depracted and should never occur
base = (
base.mark_bar()
.encode(
alt.X("metric_value", scale=alt.Scale(domain=(0, 1)), title=""),
alt.Y(
"metric_type",
title="",
sort=["Overall Performance", "Your Sentences"],
),
# alt.Row("metric_type",title=""),
color=alt.value(col_val),
tooltip=["name", "metric_type", "metric_value"],
)
.properties(width=max_width)
)
return base
@st.cache(allow_output_mutation=True)
def visualize_metrics(metrics, max_width=150, linked_vis=False, col_val="#1f77b4",min_size=1000):
"""
Visualize the metrics of the model.
"""
metric_df = pd.DataFrame()
for key in metrics.keys():
metric_types = []
metric_values = []
tmp = metrics[key]["metrics"]
# get individual metrics
for mt in tmp.keys():
metric_types = metric_types + [mt]
metric_values = metric_values + [tmp[mt]]
name = [key] * len(metric_types)
size = [metrics[key]["size"]] * len(metric_types)
source = [metrics[key]["source"]] * len(metric_types)
metric_df = metric_df.append(
pd.DataFrame(
{
"name": name,
"metric_type": metric_types,
"metric_value": metric_values,
"source": source,
"size" : [ f">={min_size} sentences" if x >= min_size else f"<{min_size} sentences" for x in size]
}
)
)
#adding a human friendly display name (not RG's backend-name)
tmp = [i.split("->") for i in metric_df['name']]
metric_df['displayName']=[i.split("@")[0] for i in [j[0] if len(j)<=1 else j[1] for j in tmp ]]
#passing the size domain
size_domain = [f">={min_size} sentences", f"<{min_size} sentences"]
# generic metric chart
base = base_chart(metric_df, linked_vis, col_val=col_val,size_domain=size_domain)
# layered chart with line
"""
# vertical line
vertline = alt.Chart().mark_rule().encode(x="a:Q")
metric_chart = (
alt.layer(base, vertline,data=metric_df)
.transform_calculate(a="0.5")
.facet(
alt.Column("metric_type", title=""))
.configure_header(labelFontSize=12
)
)
"""
return base
#@st.cache(allow_output_mutation=True)
def data_comparison(df):
#set up a dropdown select bindinf
#input_dropdown = alt.binding_select(options=['Negative Sentiment','Positive Sentiment'])
selection = alt.selection_multi(fields=['name','sentiment'])
#pop_domain = ["Overall Performance","Custom Slice","User Custom Sentence","US Protected Class"]
#color_range = ["#5778a4", "#e49444", "#b8b0ac","#85b6b2",""]
#highlight colors on select
color = alt.condition(selection,
alt.Color('source:N', legend=None),
#scale = alt.Scale(domain = pop_domain,range=color_range)),
alt.value('lightgray'))
opacity = alt.condition(selection,alt.value(0.7),alt.value(0.25))
#basic chart
scatter = alt.Chart(df).mark_point(size=100,filled=True).encode(
x=alt.X('x',axis=None),
y=alt.Y('y',axis=None),
color = color,
shape=alt.Shape('sentiment', scale=alt.Scale(range=['circle', 'diamond'])),
tooltip=['source','name','sentence','sentiment'],
opacity=opacity
).properties(
width= 600,
height = 700
).interactive()
legend = alt.Chart(df).mark_point().encode(
y=alt.Y('name:N', axis=alt.Axis(orient='right'),title=""),
x=alt.X("sentiment"),
shape=alt.Shape('sentiment', scale=alt.Scale(range=['circle', 'diamond']),legend=None),
color=color
).add_selection(
selection
)
layered = scatter | legend
layered = layered.configure_axis(
grid=False
).configure_view(
strokeOpacity=0
)
return layered
def vis_table(df, userInput=False):
""" DEPRECATED : Visualize table data more effectively """
fig = go.Figure(
data=[
go.Table(
header=dict(
values=list(df.columns), fill_color="paleturquoise", align="left"
),
columnwidth=[400, 50, 50],
cells=dict(
values=[df["sentence"], df["model label"], df["probability"]],
fill_color="lavender",
align="left",
),
)
]
)
return fig
|