File size: 7,759 Bytes
20684dc
441ec1f
20684dc
9372e1c
441ec1f
 
 
 
 
bf902a7
441ec1f
 
20684dc
441ec1f
 
 
 
bf902a7
441ec1f
 
20684dc
441ec1f
 
 
 
bf902a7
441ec1f
 
f5d1284
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20684dc
441ec1f
 
 
20684dc
441ec1f
 
 
 
20684dc
441ec1f
 
20684dc
441ec1f
20684dc
 
 
 
 
 
 
 
 
 
 
 
441ec1f
 
20684dc
441ec1f
f5d1284
 
441ec1f
f5d1284
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
441ec1f
 
20684dc
 
 
 
f5d1284
bfa0c67
20684dc
 
 
 
 
bfa0c67
20684dc
 
 
 
 
441ec1f
20684dc
441ec1f
 
 
 
 
 
bfa0c67
20684dc
 
 
 
 
bfa0c67
 
20684dc
 
 
 
441ec1f
20684dc
 
 
f5d1284
441ec1f
 
 
 
 
 
 
20684dc
 
 
 
441ec1f
20684dc
 
 
441ec1f
 
20684dc
 
 
441ec1f
f5d1284
 
 
 
441ec1f
f5d1284
441ec1f
 
 
20684dc
 
441ec1f
 
 
 
 
 
 
 
 
 
20684dc
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
209
210
211
import numpy as np
import plotly.graph_objects as go
import streamlit as st
from .lib import set_input

@st.cache
def get_w(f, ec=0.86, rv=0.50):
    result = (rv + f-1)/(ec + f-1)
    result = np.clip(result, 0, 1)
    # print(f'w = {result}')
    return result


@st.cache
def get_f(w, ec=0.86, rv=0.50):
    result = 1+(ec*w-rv)/(1-w)
    result = np.clip(result, 0, 1)
    # print(f'f = {result}')
    return result


@st.cache
def get_pe(w, ec=0.86, f=0.50):
    result = ec*w+(1-w)*(1-f)
    result = np.clip(result, 0, 1)
    # print(f'f = {result}')
    return result

@st.cache
def get_pe2(w, ec=0.86, f=0.50):
    result = 1-np.exp(-w*ec/(f+0.0001))
    result = result
    # result = np.clip(result, 0, 1)
    # print(f'f = {result}')
    return result

@st.cache(suppress_st_warning=True)
def get_pe3(w, ec=0.86, f=0.50):
    result_np = np.zeros((len(f), len(f)))
    st.markdown(result_np)
    st.markdown(result_np[0])
    for i, fi in enumerate(f):
        result = 1-np.exp(-ec/f)
        result = w*result
        if i ==0:
            result_np[i]=w*result
        else:
            result_np[i]=w*result + (1-w)*result_np[i-1]
    # result = np.clip(result, 0, 1)
    # print(f'f = {result}')
    return result


xdata1 = np.arange(0, 1.1, step=0.01)
# rand = np.random.random_sample()


@st.cache
def proper_float(i):
    return np.round(i, 2)


@st.cache
def get_text():
    return '''  
                 
    ### Eric's proposal
    > I would propose a scoring metric something like this:
    > * `probability_emote = w * emotion_confidence + (1 - w) * frequency_penalty`
    > 
    > Where:
    > * emotion_confidence: a score from 0.0 to 1.0 representing the emotion model’s confidence in it’s classification results
    > * frequency_penalty: a score from 0.0 to 1.0 where a high score penalizes frequent emotes
    > * `frequency_penalty = 1 - emotion_frequency`
    > * w: a weight from 0.0 to 1.0 that controls the balance between emotion_confidence and frequency_penalty
    > * Then you generate a random number between 0.0 and 1.0 and emote if it is greater than probability_emote
    > * You will have to set frequency_penalty and w through trial and error, but you can start with setting w=0.5 and giving the emotion classifier and frequency penalty equal weight.
    > * Setting w=1.0 would disable the frequency penalty altogether
    '''


@st.cache
def get_equation_text(w=0.5, ec=0.7, rand=None, emotion_frequency=None, graph_eq='PE1'):
    if graph_eq=='PE1':
        text = f''' 
        #### Equation
        ```
        frequency_penalty = 1 - emotion_frequency 
        probability_emote = w * emotion_confidence + (1 - w) * frequency_penalty
        ```
        **probability_emote** = {proper_float(w)} * {proper_float(ec)} + {proper_float(1-w)} * frequency_penalty

        '''
        if rand is not None:
            frequency_penalty = proper_float(1-emotion_frequency)
            probability_emote = proper_float((w)*(ec)+(1-w)*frequency_penalty)
            text = f''' 
        #### Equation
        ```
        frequency_penalty = 1 - emotion_frequency = 1 - {proper_float(emotion_frequency)}  = {frequency_penalty} 
        probability_emote = w * emotion_confidence + (1 - w) * frequency_penalty
        probability_emote = {proper_float(w)} * {proper_float(ec)} + {proper_float(1-w)} * {frequency_penalty}
        ```
        **probability_emote** = {probability_emote}
        ```
        Show_Emotion 
        = probability_emote > (Random value between 0 and 1)
        Random value = {rand}
        Show_Emotion = {probability_emote} > {rand}
        ```
        **Show_Emotion** = {probability_emote > rand}
        '''
    elif graph_eq=='PE2' or graph_eq=='PE3':
        # probability_emote = 1 - e ^ (frequency / emotion_confidence)
        text = f''' 
        #### Equation
        ```
        probability_emote = 1 - e ^ (- w * emotion_confidence / frequency)
        ```
        **probability_emote** = 1 - e ^ (- {proper_float(w)} * {proper_float(ec)} / frequency)

        '''
        
    return text




def run_probability_emote(container_param):
    graph_eq = container_param.selectbox('Select Equation', ['PE1', 'PE2', 'PE3'])
    w = set_input(container_param,
                            label='Weight w', key_slider='w_slider', key_input='w_input',
                            min_value=0.,
                            max_value=1.,
                            value=.5,
                            step=.01,)
    score = set_input(container_param,
                                    label='Confidence Score', key_slider='score_slider', key_input='score_input',
                                    min_value=0.,
                                    max_value=1.,
                                    value=.5,
                                    step=.01,)
    # score = container_param.slider(
    #     label='Confidence Score',
    #     min_value=0.,
    #     max_value=1.,
    #     value=.5,
    #     step=.01)
    calculate_check = container_param.checkbox(label='Calculate', value=False)
    if calculate_check:
        emotion_frequency = set_input(container_param,
                                                                label='Emotion Frequency', key_slider='emotion_frequency_slider_slider', key_input='emotion_frequency_slider_input',
                                                                min_value=0.,
                                                                max_value=1.,
                                                                value=.5,
                                                                step=.01,)
        rand = set_input(container_param,
                                      label='Random Value', key_slider='rand_slider', key_input='rand_input',
                                      min_value=0.,
                                      max_value=1.,
                                      value=.5,
                                      step=.01,)
    else:
        emotion_frequency = 'emotion_frequency'
        rand = None
    st.markdown(get_equation_text(w=w, ec=score, rand=rand,
                emotion_frequency=emotion_frequency, graph_eq=graph_eq))
    fig = go.Figure()
    # fig.add_trace(go.Scatter(x=xdata1, y=np.ones_like(xdata1)*rand,
    #                       mode='markers', name='Random',
    #     line=dict(color='#ff8300', width=2)
    #                       ))
    if calculate_check:
        dd = 0.01
        fig.add_hline(y=rand, line_width=3,
                      line_dash="dash", line_color="#ff8300")
        fig.add_vline(x=emotion_frequency, line_width=3,
                      line_dash="dash", line_color="green")
        fig.add_trace(go.Scatter(
            x=[emotion_frequency-dd, emotion_frequency+dd], y=[rand-dd, rand+dd],
            mode='lines',
            line=dict(color='#ee00ee', width=8)
        ),)
        fig.add_trace(go.Scatter(
            x=[emotion_frequency+dd, emotion_frequency-dd], y=[rand-dd, rand+dd],
            mode='lines',
            line=dict(color='#ee00ee', width=8)
        ),)
    
    if graph_eq=='PE1': get_result = get_pe
    elif graph_eq=='PE2': get_result = get_pe2
    elif graph_eq=='PE3': get_result = get_pe3
    fig.add_trace(go.Scatter(
        x=xdata1, y=get_result(w=w, f=xdata1, ec=score),
        mode='lines',
        name='Probability-Emote',
        line=dict(color='#00eeee', width=4)
    ),
    )

    fig.update_layout(
        template='plotly_dark',
        xaxis_range=[0., 1.],
        yaxis_range=[0., 1.],
        xaxis_title="Emotion Frequency",
        yaxis_title="Probability Emote",
        showlegend=False
    )
    st.plotly_chart(fig, use_container_width=True)
    st.markdown(get_text())