Spaces:
Runtime error
Runtime error
added nested pie chart
Browse files
app.py
CHANGED
@@ -103,8 +103,10 @@ def main(from_date,
|
|
103 |
for col in PLOT_CHOICES_REVERSE_DICT: # plot_choices:
|
104 |
if col == 'merged_target':
|
105 |
pie_charts.append(bar(db[0], col + ": " + db[1]))
|
106 |
-
|
107 |
pie_charts.append(pie_chart(db[0], col, col + ": " + db[1]))
|
|
|
|
|
108 |
return pie_charts
|
109 |
|
110 |
def bar(db: pd.DataFrame, title):
|
@@ -162,7 +164,7 @@ def main(from_date,
|
|
162 |
if db.empty:
|
163 |
return None
|
164 |
else:
|
165 |
-
#
|
166 |
db = pie_chart_input(db, col_name, LIMIT)
|
167 |
labels = db[col_name].to_list()
|
168 |
sizes = db['frequency'].values
|
@@ -173,6 +175,48 @@ def main(from_date,
|
|
173 |
plt.title(title, fontdict=font1)
|
174 |
return fig
|
175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
# text_classifier = tc.TextClassifier(from_date=from_date, to_date=to_date,
|
177 |
# user_list=match_name_lower_case(usr_name_choices),
|
178 |
# num_tweets=NUM_TWEETS)
|
@@ -188,9 +232,9 @@ def main(from_date,
|
|
188 |
# Remove entries from df where 'tweet' starts with '@'
|
189 |
df = df[df['tweet'].str.startswith('@') == False]
|
190 |
# change 'merged_topic' to 'Other' if it is 'ERROR_9000' or 'ERROR_496'
|
191 |
-
df[
|
192 |
# change 'merged_topic' to 'Government' if it is 's'
|
193 |
-
df[
|
194 |
if save_selected:
|
195 |
user_list = match_name_lower_case(usr_name_choices)
|
196 |
df_l = []
|
@@ -204,7 +248,7 @@ def main(from_date,
|
|
204 |
else:
|
205 |
save_selected_checkbox = [gr.Checkbox.update(interactive=True)]
|
206 |
|
207 |
-
|
208 |
|
209 |
rb_components = [rb1, rb2, rb3, rb4, rb5, rb6, rb7, rb8] # radio_buttons
|
210 |
df_visibility_check = [v1, v2, v3, v4, v5, v6, v7, v8]
|
@@ -232,8 +276,8 @@ def main(from_date,
|
|
232 |
|
233 |
return df_list + number_tweets + save_file_components_list
|
234 |
|
235 |
-
return
|
236 |
-
|
237 |
|
238 |
|
239 |
''' END OF MAIN
|
@@ -285,8 +329,6 @@ def export_to_download(_data_frame, _type: str):
|
|
285 |
|
286 |
|
287 |
def pie_chart_input(df, column, limit):
|
288 |
-
df_len = len(df)
|
289 |
-
df_v = df[column].value_counts()
|
290 |
df_len = len(df)
|
291 |
if column == "sentiment":
|
292 |
ds_sentiment = df[column].apply(lambda x: re.sub("\s+", "", str(x)))
|
@@ -319,7 +361,7 @@ def pie_chart_input(df, column, limit):
|
|
319 |
else:
|
320 |
ind_other = freq_dict[column].index("other")
|
321 |
freq_dict["frequency"][ind_other] += freq_other
|
322 |
-
|
323 |
return pd.DataFrame.from_dict(freq_dict)
|
324 |
|
325 |
|
|
|
103 |
for col in PLOT_CHOICES_REVERSE_DICT: # plot_choices:
|
104 |
if col == 'merged_target':
|
105 |
pie_charts.append(bar(db[0], col + ": " + db[1]))
|
106 |
+
elif col == 'sentiment':
|
107 |
pie_charts.append(pie_chart(db[0], col, col + ": " + db[1]))
|
108 |
+
elif col == TOPIC:
|
109 |
+
pie_charts.append(nested_pie_chart(db[0], col, col + ": " + db[1]))
|
110 |
return pie_charts
|
111 |
|
112 |
def bar(db: pd.DataFrame, title):
|
|
|
164 |
if db.empty:
|
165 |
return None
|
166 |
else:
|
167 |
+
# df = df[col_name].value_counts()[:5] # Lägg till "Others sedan"
|
168 |
db = pie_chart_input(db, col_name, LIMIT)
|
169 |
labels = db[col_name].to_list()
|
170 |
sizes = db['frequency'].values
|
|
|
175 |
plt.title(title, fontdict=font1)
|
176 |
return fig
|
177 |
|
178 |
+
def nested_pie_chart(df, col_name, title):
|
179 |
+
if df.empty:
|
180 |
+
return None
|
181 |
+
else:
|
182 |
+
count_dict = {}
|
183 |
+
sent_dict = {'positive': 0, 'negative': 1, 'neutral': 2, 'other': 3}
|
184 |
+
tot_sum = len(df)
|
185 |
+
for i in range(df.shape[0]):
|
186 |
+
topic = df.iloc[i][TOPIC]
|
187 |
+
sentiment = df.iloc[i]['sentiment'] if df.iloc[i]['sentiment'] in sent_dict else 'other'
|
188 |
+
if topic not in count_dict:
|
189 |
+
count_dict[topic] = [0, 0, 0, 0]
|
190 |
+
count_dict[topic][sent_dict[sentiment]] += 1
|
191 |
+
else:
|
192 |
+
count_dict[topic][sent_dict[sentiment]] += 1
|
193 |
+
count_list = []
|
194 |
+
other_list = np.array([0, 0, 0, 0])
|
195 |
+
labels = []
|
196 |
+
for topic in count_dict:
|
197 |
+
if tot_sum > 0 and np.sum(count_dict[topic]) / tot_sum > LIMIT:
|
198 |
+
count_list.append(count_dict[topic])
|
199 |
+
labels.append(topic)
|
200 |
+
else:
|
201 |
+
other_list += np.array(count_dict[topic])
|
202 |
+
|
203 |
+
count_list.append(list(other_list))
|
204 |
+
labels.append('Other')
|
205 |
+
fig, ax = plt.subplots()
|
206 |
+
|
207 |
+
size = 0.3
|
208 |
+
vals = np.array(count_list)
|
209 |
+
inner_colors = ['green', 'red', 'blue', 'yellow'] * len(count_dict)
|
210 |
+
if vals.shape[0] == 0:
|
211 |
+
pass
|
212 |
+
else:
|
213 |
+
ax.pie(vals.sum(axis=1), radius=1, labels=labels, pctdistance=0.9,
|
214 |
+
wedgeprops=dict(width=size, edgecolor='w'))
|
215 |
+
ax.pie(vals.flatten(), radius=1 - size, colors=inner_colors,
|
216 |
+
wedgeprops=dict(width=size, edgecolor='w'))
|
217 |
+
ax.set(aspect='equal', title='Nested sentiment plot')
|
218 |
+
return fig
|
219 |
+
|
220 |
# text_classifier = tc.TextClassifier(from_date=from_date, to_date=to_date,
|
221 |
# user_list=match_name_lower_case(usr_name_choices),
|
222 |
# num_tweets=NUM_TWEETS)
|
|
|
232 |
# Remove entries from df where 'tweet' starts with '@'
|
233 |
df = df[df['tweet'].str.startswith('@') == False]
|
234 |
# change 'merged_topic' to 'Other' if it is 'ERROR_9000' or 'ERROR_496'
|
235 |
+
df[TOPIC] = df[TOPIC].apply(lambda x: "N/A" if x == "ERROR_9000" or x == "ERROR_496" else x)
|
236 |
# change 'merged_topic' to 'Government' if it is 's'
|
237 |
+
df[TOPIC] = df[TOPIC].apply(lambda x: "The Government" if x == "s" else x)
|
238 |
if save_selected:
|
239 |
user_list = match_name_lower_case(usr_name_choices)
|
240 |
df_l = []
|
|
|
248 |
else:
|
249 |
save_selected_checkbox = [gr.Checkbox.update(interactive=True)]
|
250 |
|
251 |
+
pie_charts = add_pie_chart(df, usr_name_choices, convert_plot_choices(plot_choice))
|
252 |
|
253 |
rb_components = [rb1, rb2, rb3, rb4, rb5, rb6, rb7, rb8] # radio_buttons
|
254 |
df_visibility_check = [v1, v2, v3, v4, v5, v6, v7, v8]
|
|
|
276 |
|
277 |
return df_list + number_tweets + save_file_components_list
|
278 |
|
279 |
+
return pie_charts + save_selected_checkbox + get_selected_df_list(df, save_file_bool, list(usr_name_choices),
|
280 |
+
rb_components, df_visibility_check)
|
281 |
|
282 |
|
283 |
''' END OF MAIN
|
|
|
329 |
|
330 |
|
331 |
def pie_chart_input(df, column, limit):
|
|
|
|
|
332 |
df_len = len(df)
|
333 |
if column == "sentiment":
|
334 |
ds_sentiment = df[column].apply(lambda x: re.sub("\s+", "", str(x)))
|
|
|
361 |
else:
|
362 |
ind_other = freq_dict[column].index("other")
|
363 |
freq_dict["frequency"][ind_other] += freq_other
|
364 |
+
test_frame = pd.DataFrame.from_dict(freq_dict)
|
365 |
return pd.DataFrame.from_dict(freq_dict)
|
366 |
|
367 |
|