Spaces:
Runtime error
Runtime error
adds ngram-pattern
Browse files- src/read_logs.py +36 -11
- src/test.py +44 -0
src/read_logs.py
CHANGED
@@ -57,6 +57,12 @@ class LogAnalyser:
|
|
57 |
|
58 |
self.update_df()
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
def update_df(self):
|
61 |
reaction_weight = set_input(self.container_param,
|
62 |
label='Reaction Weight w', min_value=0.0, max_value=1.0, value=0.5, step=0.01,
|
@@ -76,16 +82,25 @@ class LogAnalyser:
|
|
76 |
"Table Style:", ["Dataframe", "Table"])
|
77 |
self.show_pe_data = self.container_param.checkbox(
|
78 |
'Show Probability Emote', value=True, key='show_pe_data_log')
|
|
|
|
|
|
|
|
|
79 |
df_reaction_pattern = pd.DataFrame()
|
80 |
reaction_pattern_dict = dict()
|
81 |
for story_id in self.df.Story.unique():
|
82 |
reaction_num = 0
|
|
|
|
|
|
|
|
|
|
|
83 |
def get_subset_condition(data):
|
84 |
return (data.Story == story_id) & (data.Turn == 'user')
|
85 |
subset_condition = get_subset_condition(self.df)
|
86 |
dfs = self.df[subset_condition]
|
87 |
for i, (index, row) in enumerate(dfs.iterrows()):
|
88 |
-
if row.Emotion == 'neutral':
|
89 |
reaction_show = False
|
90 |
else:
|
91 |
reaction_frequency = reaction_num/(i+1)
|
@@ -102,13 +117,19 @@ class LogAnalyser:
|
|
102 |
self.df.at[index, 'random_value'] = random_value
|
103 |
self.df.at[index, 'reaction_show'] = reaction_show
|
104 |
s = ''
|
105 |
-
df_edit = self.df[get_subset_condition(
|
106 |
-
|
|
|
107 |
for v in df_edit:
|
108 |
s += str(int(v))
|
109 |
# df_reaction_pattern.at[story_id]
|
110 |
# reaction_pattern_dict['story_id']=story_id
|
111 |
-
reaction_pattern_dict['
|
|
|
|
|
|
|
|
|
|
|
112 |
df_reaction_pattern = pd.concat(
|
113 |
[df_reaction_pattern, pd.DataFrame(reaction_pattern_dict, index=[f'Story_{story_id}'])])
|
114 |
# st.markdown(df_edit)
|
@@ -132,27 +153,31 @@ class LogAnalyser:
|
|
132 |
dfs = dfs.apply(self.rower, axis=None)
|
133 |
dfs = dfs.set_table_styles([{
|
134 |
'selector': 'tr:hover',
|
135 |
-
'props': '
|
136 |
}]) # .hide_index()
|
137 |
|
138 |
if table_mode == 'Dataframe':
|
139 |
st.dataframe(dfs)
|
140 |
-
|
|
|
141 |
elif table_mode == 'Table':
|
142 |
st.table(dfs)
|
143 |
-
st.table(df_reaction_pattern.iloc[story_id-1]
|
144 |
# print(dfs.render())
|
145 |
if table_mode == 'Dataframe':
|
146 |
st.dataframe(df_reaction_pattern)
|
147 |
elif table_mode == 'Table':
|
148 |
st.table(df_reaction_pattern)
|
149 |
|
150 |
-
@
|
151 |
-
|
152 |
-
def dfstyle_color_text_col(s):
|
153 |
result = ['background-color: white']*len(s)
|
154 |
if s.Emotion == 'neutral' and s.Turn == 'user':
|
155 |
-
result[2:-1] = ['color: #992222']
|
|
|
|
|
|
|
|
|
156 |
if s.reaction_show == 1:
|
157 |
result[-1] = 'color: #222222'
|
158 |
elif s.reaction_show == 0:
|
|
|
57 |
|
58 |
self.update_df()
|
59 |
|
60 |
+
def get_ngram_pattern(self, s, n=2):
|
61 |
+
gnp = ''
|
62 |
+
for i in range(len(s)-(n-1)):
|
63 |
+
gnp += '1' if '1' in s[i:i+n] else '0'
|
64 |
+
return gnp
|
65 |
+
|
66 |
def update_df(self):
|
67 |
reaction_weight = set_input(self.container_param,
|
68 |
label='Reaction Weight w', min_value=0.0, max_value=1.0, value=0.5, step=0.01,
|
|
|
82 |
"Table Style:", ["Dataframe", "Table"])
|
83 |
self.show_pe_data = self.container_param.checkbox(
|
84 |
'Show Probability Emote', value=True, key='show_pe_data_log')
|
85 |
+
self.score_threshold = set_input(self.container_param,
|
86 |
+
label='Score Threshold', min_value=0.0, max_value=1.0, value=0.5, step=0.01,
|
87 |
+
key_slider='score_threshold_slider', key_input='score_threshold_input',)
|
88 |
+
|
89 |
df_reaction_pattern = pd.DataFrame()
|
90 |
reaction_pattern_dict = dict()
|
91 |
for story_id in self.df.Story.unique():
|
92 |
reaction_num = 0
|
93 |
+
reaction_frequency = 0
|
94 |
+
probability_emote = 0
|
95 |
+
random_value = 0
|
96 |
+
reaction_show = False
|
97 |
+
|
98 |
def get_subset_condition(data):
|
99 |
return (data.Story == story_id) & (data.Turn == 'user')
|
100 |
subset_condition = get_subset_condition(self.df)
|
101 |
dfs = self.df[subset_condition]
|
102 |
for i, (index, row) in enumerate(dfs.iterrows()):
|
103 |
+
if row.Emotion == 'neutral' or row.Score < self.score_threshold:
|
104 |
reaction_show = False
|
105 |
else:
|
106 |
reaction_frequency = reaction_num/(i+1)
|
|
|
117 |
self.df.at[index, 'random_value'] = random_value
|
118 |
self.df.at[index, 'reaction_show'] = reaction_show
|
119 |
s = ''
|
120 |
+
df_edit = self.df[get_subset_condition(
|
121 |
+
self.df)].reaction_show.copy()
|
122 |
+
df_edit = df_edit.dropna()
|
123 |
for v in df_edit:
|
124 |
s += str(int(v))
|
125 |
# df_reaction_pattern.at[story_id]
|
126 |
# reaction_pattern_dict['story_id']=story_id
|
127 |
+
reaction_pattern_dict['reaction_length'] = len(s)
|
128 |
+
reaction_pattern_dict['reaction_1'] = s.count('1')
|
129 |
+
reaction_pattern_dict['reaction_pattern'] = s
|
130 |
+
|
131 |
+
for i in range(2, 8):
|
132 |
+
reaction_pattern_dict[f'{i}-gram_pattern'] = self.get_ngram_pattern(s, n=i)
|
133 |
df_reaction_pattern = pd.concat(
|
134 |
[df_reaction_pattern, pd.DataFrame(reaction_pattern_dict, index=[f'Story_{story_id}'])])
|
135 |
# st.markdown(df_edit)
|
|
|
153 |
dfs = dfs.apply(self.rower, axis=None)
|
154 |
dfs = dfs.set_table_styles([{
|
155 |
'selector': 'tr:hover',
|
156 |
+
'props': 'color: #000000' # background-color: #eeee66;font-size: 1.01em;
|
157 |
}]) # .hide_index()
|
158 |
|
159 |
if table_mode == 'Dataframe':
|
160 |
st.dataframe(dfs)
|
161 |
+
# set_na_rep(" ").s
|
162 |
+
# st.dataframe(df_reaction_pattern.iloc[story_id-1])
|
163 |
elif table_mode == 'Table':
|
164 |
st.table(dfs)
|
165 |
+
# st.table(df_reaction_pattern.iloc[story_id-1])
|
166 |
# print(dfs.render())
|
167 |
if table_mode == 'Dataframe':
|
168 |
st.dataframe(df_reaction_pattern)
|
169 |
elif table_mode == 'Table':
|
170 |
st.table(df_reaction_pattern)
|
171 |
|
172 |
+
# @st.cache
|
173 |
+
def dfstyle_color_text_col(self, s):
|
|
|
174 |
result = ['background-color: white']*len(s)
|
175 |
if s.Emotion == 'neutral' and s.Turn == 'user':
|
176 |
+
result[2:-1] = ['color: #992222'] + \
|
177 |
+
['color: #333333']+['color: #fcfcfc']*3
|
178 |
+
if s.Score < self.score_threshold and s.Turn == 'user':
|
179 |
+
result[3:-1] = ['color: #992222'] + ['color: #fcfcfc']*3
|
180 |
+
printj.red(result)
|
181 |
if s.reaction_show == 1:
|
182 |
result[-1] = 'color: #222222'
|
183 |
elif s.reaction_show == 0:
|
src/test.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# %%
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
np.random.seed(24)
|
6 |
+
df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
|
7 |
+
|
8 |
+
df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))],
|
9 |
+
axis=1)
|
10 |
+
df.iloc[0, 2] = np.nan
|
11 |
+
df['reaction_show'] = True
|
12 |
+
df
|
13 |
+
# %%
|
14 |
+
s = ''
|
15 |
+
for v in df.reaction_show:
|
16 |
+
s += str(int(v))
|
17 |
+
s
|
18 |
+
|
19 |
+
|
20 |
+
# %%
|
21 |
+
s = '101100'
|
22 |
+
g2p = ''
|
23 |
+
for i in range(len(s)-1):
|
24 |
+
# print(i, i+2, s)
|
25 |
+
# print(s[0:2])
|
26 |
+
g2p += '1' if '1' in s[i:i+2] else '0'
|
27 |
+
|
28 |
+
g2p
|
29 |
+
|
30 |
+
|
31 |
+
# # def highlight_greaterthan(s,column):
|
32 |
+
# # is_max = pd.Series(data=False, index=s.index)
|
33 |
+
# # is_max[column] = s.loc[column] >= 1
|
34 |
+
# # return ['background-color: red' if is_max.any() else '' for v in is_max]
|
35 |
+
|
36 |
+
# def highlight_greaterthan_1(s):
|
37 |
+
# if s.B > 1.0:
|
38 |
+
# return ['background-color: white']+['background-color: yellow']+['background-color: white']*3
|
39 |
+
# else:
|
40 |
+
# return ['background-color: white']*5
|
41 |
+
|
42 |
+
|
43 |
+
# df.style.apply(highlight_greaterthan_1, axis=1)
|
44 |
+
# %%
|