|
import streamlit as st |
|
import matplotlib |
|
import matplotlib.pyplot as plt |
|
import plotly.graph_objects as go |
|
import plotly.express as px |
|
|
|
def indicator_plot(value, title, value_range, domain): |
|
|
|
plot = go.Indicator( |
|
mode = 'gauge+delta', |
|
value = value, |
|
domain = domain, |
|
title = title, |
|
delta = { |
|
'reference': 0, |
|
'decreasing': {'color': '#ec4899'}, |
|
'increasing': {'color': '#36def1'} |
|
}, |
|
gauge = { |
|
'axis': {'range': value_range, 'tickwidth': 1, 'tickcolor': 'black'}, |
|
'bar': {'color': '#4361ee'}, |
|
'bgcolor': 'white', |
|
'borderwidth': 2, |
|
'bordercolor': '#efefef', |
|
'steps': [ |
|
{'range': [value_range[0], 0], 'color': '#efefef'}, |
|
{'range': [0, value_range[1]], 'color': '#efefef'} |
|
], |
|
'threshold': { |
|
'line': {'color': '#4361ee', 'width': 8}, |
|
'thickness': 0.75, |
|
'value': value |
|
} |
|
} |
|
) |
|
|
|
return plot |
|
|
|
def scatter_plot(df, group_var): |
|
|
|
colors = ['#36def1', '#4361ee'] if group_var else ['#4361ee'] |
|
|
|
plot = px.scatter( |
|
df, |
|
x='Machine-ratings', |
|
y='Human-ratings', |
|
color=group_var, |
|
facet_col='x_group', |
|
facet_col_wrap=2, |
|
trendline='ols', |
|
trendline_scope='trace', |
|
hover_data={ |
|
'Text': df.text, |
|
'Language': False, |
|
'x_group': False, |
|
'Human-ratings': ':.2f', |
|
'Machine-ratings': ':.2f', |
|
'Study': df.study, |
|
'Instrument': df.instrument, |
|
}, |
|
width=400, |
|
height=400, |
|
color_discrete_sequence=colors |
|
) |
|
|
|
plot.for_each_annotation(lambda a: a.update(text=a.text.split('=')[-1])) |
|
plot.update_layout( |
|
legend={ |
|
'orientation':'h', |
|
'yanchor': 'bottom', |
|
'y': -.30 |
|
}) |
|
plot.update_xaxes(title_standoff = 0) |
|
|
|
return plot |
|
|
|
def show_scores(sentiment, desirability, input_text): |
|
with st.container(): |
|
|
|
num_steps = 10 |
|
colorscale = plt.get_cmap('viridis', num_steps) |
|
|
|
steps_sentiment = [{ |
|
'range': [i/num_steps*2-1, (i+1)/num_steps*2-1], |
|
'color': matplotlib.colors.rgb2hex(colorscale(i)[:3]) |
|
} for i in range(num_steps)] |
|
|
|
steps_desirability = [{ |
|
'range': [i/num_steps*8-4, (i+1)/num_steps*8-4], |
|
'color': matplotlib.colors.rgb2hex(colorscale(i)[:3]) |
|
} for i in range(num_steps)] |
|
|
|
plot1 = go.Indicator( |
|
mode = 'number+gauge', |
|
value = sentiment, |
|
domain = {'x': [0.25, 1], 'y': [0.45, 0.65]}, |
|
title = {'text': 'Sentiment', 'font': {'color': 'black', 'size': 22}}, |
|
number={'font': {'color': 'black', 'size': 26}, 'valueformat': '.2f'}, |
|
gauge = { |
|
'shape': 'bullet', |
|
'axis': {'range': [-1, 1]}, |
|
'threshold': { |
|
'line': {'color': '#36def1', 'width': 10}, |
|
'thickness': 0.75, |
|
'value': sentiment}, |
|
'steps': steps_sentiment, |
|
'bar': {'color': 'rgba(0,0,0,0)'} |
|
} |
|
) |
|
|
|
plot2 = go.Indicator( |
|
mode = 'number+gauge', |
|
value = desirability, |
|
domain = {'x': [0.25, 1], 'y': [0.15, 0.35]}, |
|
title = {'text': 'Desirability', 'font': {'color': 'black', 'size': 22}}, |
|
number={'font': {'color': 'black', 'size': 26}, 'valueformat': '.2f'}, |
|
gauge = { |
|
'shape': 'bullet', |
|
'axis': {'range': [-4, 4]}, |
|
'threshold': { |
|
'line': {'color': '#36def1', 'width': 10}, |
|
'thickness': 0.75, |
|
'value': desirability}, |
|
'steps': steps_desirability, |
|
'bar': {'color': 'rgba(0,0,0,0)'} |
|
} |
|
) |
|
|
|
fig = go.Figure() |
|
fig.add_trace(plot1) |
|
fig.add_trace(plot2) |
|
|
|
plot_title = f'Estimated Sentiment and Desirability for <br><i>"{input_text}</i>"' |
|
|
|
fig.update_layout( |
|
annotations=[ |
|
go.layout.Annotation( |
|
text=plot_title, |
|
align='center', |
|
showarrow=False, |
|
xref='paper', |
|
yref='paper', |
|
x=0.5, |
|
y=.85, |
|
xanchor='center', |
|
yanchor='bottom', |
|
font=dict(size=22) |
|
), |
|
go.layout.Annotation( |
|
text="Negative", |
|
showarrow=False, |
|
xref='paper', |
|
yref='paper', |
|
x=0.25, |
|
y=0.8, |
|
font=dict(size=18) |
|
), |
|
go.layout.Annotation( |
|
text="Positive", |
|
showarrow=False, |
|
xref='paper', |
|
yref='paper', |
|
x=.81, |
|
y=.8, |
|
font=dict(size=18) |
|
), |
|
], |
|
|
|
font = {'color': 'black', 'family': 'Arial'}, |
|
height=300, |
|
margin={'t': 50, 'b': 0, 'l': 0} |
|
) |
|
|
|
st.plotly_chart(fig, theme=None, use_container_width=True) |
|
|
|
st.markdown(""" |
|
Item sentiment: Absolute differences between positive and negative sentiment. |
|
Item desirability: z-transformed values, 0 indicated "neutral". |
|
""") |