File size: 5,754 Bytes
28183db
464b09c
 
28183db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
464b09c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28183db
 
 
464b09c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28183db
 
 
 
 
 
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
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".        
        """)