Demea9000 commited on
Commit
461802c
1 Parent(s): 8c603b9

changed naming conventions for easier reading

Browse files
Files changed (1) hide show
  1. app.py +26 -27
app.py CHANGED
@@ -49,12 +49,12 @@ def show_all_stats(see_full_stats):
49
  return pd.DataFrame()
50
 
51
 
52
- def fixChoicesCorrectOrder(choices):
53
- ListChoices = [x for x in Columns if x in choices]
54
- return ListChoices
55
 
56
 
57
- def MatchNameToUser(user_names):
58
  users = []
59
  for N in user_names:
60
  users.append(UserNameDict[N])
@@ -69,9 +69,9 @@ def convert_back_plot_choices(plot_choices_raw):
69
  return [PLOT_CHOICES_REVERSE_DICT[choice] for choice in plot_choices_raw]
70
 
71
 
72
- def main(From,
73
- To,
74
- UserNameChoices,
75
  plot_choice,
76
  save_selected,
77
  rb1, rb2, rb3, rb4, rb5, rb6, rb7, rb8,
@@ -106,10 +106,11 @@ def main(From,
106
  return pie_charts
107
 
108
  def bar(db: pd.DataFrame, title):
109
- '''This method adds a stacked bar diagram for each target and each sentiment
 
110
  NOTE: The tweets without any target are not shown in the plot, we just show distribution of tweets that have a
111
  target.
112
- '''
113
  if db.empty:
114
  return None
115
  else:
@@ -161,7 +162,7 @@ def main(From,
161
  return None
162
  else:
163
  # db = db[col_name].value_counts()[:5] # Lägg till "Others sedan"
164
- db = piechart_input(db, col_name, LIMIT)
165
  labels = db[col_name].to_list()
166
  sizes = db['frequency'].values
167
  # explode = (0, 0.1, 0, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
@@ -171,7 +172,7 @@ def main(From,
171
  plt.title(title, fontdict=font1)
172
  return fig
173
 
174
- text_classifier = tc.TextClassifier(from_date=From, to_date=To, user_list=MatchNameToUser(UserNameChoices),
175
  num_tweets=num_tweet)
176
  text_classifier.run_main_pipeline()
177
  dataframe = text_classifier.get_dataframe()
@@ -180,7 +181,7 @@ def main(From,
180
  df = dataframe
181
 
182
  if save_selected:
183
- user_list = MatchNameToUser(UserNameChoices)
184
  df_l = []
185
  for user in user_list:
186
  df_l.append(pd.DataFrame(df.loc[df['username'] == user]))
@@ -192,7 +193,7 @@ def main(From,
192
  else:
193
  save_selected_checkbox = [gr.Checkbox.update(interactive=True)]
194
 
195
- pycharts = add_pie_chart(df, UserNameChoices, convert_plot_choices(plot_choice))
196
 
197
  rb_components = [rb1, rb2, rb3, rb4, rb5, rb6, rb7, rb8] # radio_buttons
198
  df_visibility_check = [v1, v2, v3, v4, v5, v6, v7, v8]
@@ -214,13 +215,13 @@ def main(From,
214
  save_file_components_list.append(gr.Checkbox.update(visible=u_bool))
215
 
216
  if u_bool and visibility[i]:
217
- df_list.append(get_exemple_df(user_df, PLOT_CHOICES_DICT[radio[i]]))
218
  else:
219
  df_list.append(None)
220
 
221
  return df_list + number_tweets + save_file_components_list
222
 
223
- return pycharts + save_selected_checkbox + get_selected_df_list(df, save_file_bool, list(UserNameChoices),
224
  rb_components, df_visibility_check)
225
 
226
 
@@ -232,7 +233,7 @@ def main(From,
232
  '''
233
 
234
 
235
- def get_exemple_df(df: pd.DataFrame, column: str):
236
  print(column)
237
  df = df[SELECTED_COLUMN_DICT[column]]
238
  unique_labels = df[column].value_counts().keys()
@@ -243,11 +244,11 @@ def get_exemple_df(df: pd.DataFrame, column: str):
243
  df_temp = df_temp[0:5]
244
  stat.append(df_temp)
245
 
246
- exemple_df = pd.concat(stat)
247
 
248
  # stat =stat.reset_index(drop=True) just in case u want to reset indexing
249
 
250
- return exemple_df
251
 
252
 
253
  def export_to_download(_data_frame, _type: str):
@@ -272,7 +273,7 @@ def export_to_download(_data_frame, _type: str):
272
  # , pie_chart(df, "main_topic"), pie_chart("target")
273
 
274
 
275
- def piechart_input(df, column, limit):
276
  df_len = len(df)
277
  df_v = df[column].value_counts()
278
  df_len = len(df)
@@ -285,9 +286,7 @@ def piechart_input(df, column, limit):
285
  df_v = ds_target.value_counts()
286
  freq = df_v.to_list()
287
  labels = df_v.keys().to_list
288
- freq_dict = {}
289
- freq_dict[column] = labels
290
- freq_dict["frequency"] = freq
291
  return pd.DataFrame.from_dict(freq_dict)
292
 
293
  else:
@@ -326,7 +325,7 @@ def update_window(leaders: list, plot_choices: list,
326
  leader_bool_list, plot_bool_list = convert_to_boolean(leaders, plot_choices)
327
 
328
  bool_list = []
329
- df_visiblity_bool = [v1, v2, v3, v4, v5, v6, v7, v8]
330
 
331
  # this loop sets boolean for plots
332
  for leader in leader_bool_list:
@@ -353,8 +352,8 @@ def update_window(leaders: list, plot_choices: list,
353
  update_blocks.append(gr.Row.update(visible=vis_or_not))
354
  update_blocks.append(gr.Row.update(visible=vis_or_not))
355
  if vis_or_not:
356
- update_blocks.append(gr.Row.update(visible=df_visiblity_bool[i]))
357
- update_df.append(gr.DataFrame.update(visible=df_visiblity_bool[i]))
358
  else:
359
 
360
  update_blocks.append(gr.Row.update(visible=False))
@@ -390,8 +389,8 @@ if __name__ == "__main__":
390
  with gr.Row():
391
  with gr.Column():
392
  with gr.Row():
393
- date1 = gr.Textbox(label="From", value='2022-05-10')
394
- date2 = gr.Textbox(label="To", value='2022-05-30')
395
  leaders = gr.Checkboxgroup(choices=USER_NAMES,
396
  label="")
397
  plot_choices = gr.CheckboxGroup(choices=CHOICE_LIST, label='Choose what to show')
 
49
  return pd.DataFrame()
50
 
51
 
52
+ def fix_choices_correct_order(choices):
53
+ list_choices = [x for x in Columns if x in choices]
54
+ return list_choices
55
 
56
 
57
+ def match_name_lower_case(user_names):
58
  users = []
59
  for N in user_names:
60
  users.append(UserNameDict[N])
 
69
  return [PLOT_CHOICES_REVERSE_DICT[choice] for choice in plot_choices_raw]
70
 
71
 
72
+ def main(from_date,
73
+ to_date,
74
+ usr_name_choices,
75
  plot_choice,
76
  save_selected,
77
  rb1, rb2, rb3, rb4, rb5, rb6, rb7, rb8,
 
106
  return pie_charts
107
 
108
  def bar(db: pd.DataFrame, title):
109
+ """
110
+ This method adds a stacked bar diagram for each target and each sentiment
111
  NOTE: The tweets without any target are not shown in the plot, we just show distribution of tweets that have a
112
  target.
113
+ """
114
  if db.empty:
115
  return None
116
  else:
 
162
  return None
163
  else:
164
  # db = db[col_name].value_counts()[:5] # Lägg till "Others sedan"
165
+ db = pie_chart_input(db, col_name, LIMIT)
166
  labels = db[col_name].to_list()
167
  sizes = db['frequency'].values
168
  # explode = (0, 0.1, 0, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
 
172
  plt.title(title, fontdict=font1)
173
  return fig
174
 
175
+ text_classifier = tc.TextClassifier(from_date=from_date, to_date=to_date, user_list=match_name_lower_case(usr_name_choices),
176
  num_tweets=num_tweet)
177
  text_classifier.run_main_pipeline()
178
  dataframe = text_classifier.get_dataframe()
 
181
  df = dataframe
182
 
183
  if save_selected:
184
+ user_list = match_name_lower_case(usr_name_choices)
185
  df_l = []
186
  for user in user_list:
187
  df_l.append(pd.DataFrame(df.loc[df['username'] == user]))
 
193
  else:
194
  save_selected_checkbox = [gr.Checkbox.update(interactive=True)]
195
 
196
+ pycharts = add_pie_chart(df, usr_name_choices, convert_plot_choices(plot_choice))
197
 
198
  rb_components = [rb1, rb2, rb3, rb4, rb5, rb6, rb7, rb8] # radio_buttons
199
  df_visibility_check = [v1, v2, v3, v4, v5, v6, v7, v8]
 
215
  save_file_components_list.append(gr.Checkbox.update(visible=u_bool))
216
 
217
  if u_bool and visibility[i]:
218
+ df_list.append(get_example_df(user_df, PLOT_CHOICES_DICT[radio[i]]))
219
  else:
220
  df_list.append(None)
221
 
222
  return df_list + number_tweets + save_file_components_list
223
 
224
+ return pycharts + save_selected_checkbox + get_selected_df_list(df, save_file_bool, list(usr_name_choices),
225
  rb_components, df_visibility_check)
226
 
227
 
 
233
  '''
234
 
235
 
236
+ def get_example_df(df: pd.DataFrame, column: str):
237
  print(column)
238
  df = df[SELECTED_COLUMN_DICT[column]]
239
  unique_labels = df[column].value_counts().keys()
 
244
  df_temp = df_temp[0:5]
245
  stat.append(df_temp)
246
 
247
+ example_df = pd.concat(stat)
248
 
249
  # stat =stat.reset_index(drop=True) just in case u want to reset indexing
250
 
251
+ return example_df
252
 
253
 
254
  def export_to_download(_data_frame, _type: str):
 
273
  # , pie_chart(df, "main_topic"), pie_chart("target")
274
 
275
 
276
+ def pie_chart_input(df, column, limit):
277
  df_len = len(df)
278
  df_v = df[column].value_counts()
279
  df_len = len(df)
 
286
  df_v = ds_target.value_counts()
287
  freq = df_v.to_list()
288
  labels = df_v.keys().to_list
289
+ freq_dict = {column: labels, "frequency": freq}
 
 
290
  return pd.DataFrame.from_dict(freq_dict)
291
 
292
  else:
 
325
  leader_bool_list, plot_bool_list = convert_to_boolean(leaders, plot_choices)
326
 
327
  bool_list = []
328
+ df_visibility_bool = [v1, v2, v3, v4, v5, v6, v7, v8]
329
 
330
  # this loop sets boolean for plots
331
  for leader in leader_bool_list:
 
352
  update_blocks.append(gr.Row.update(visible=vis_or_not))
353
  update_blocks.append(gr.Row.update(visible=vis_or_not))
354
  if vis_or_not:
355
+ update_blocks.append(gr.Row.update(visible=df_visibility_bool[i]))
356
+ update_df.append(gr.DataFrame.update(visible=df_visibility_bool[i]))
357
  else:
358
 
359
  update_blocks.append(gr.Row.update(visible=False))
 
389
  with gr.Row():
390
  with gr.Column():
391
  with gr.Row():
392
+ date1 = gr.Textbox(label="from_date", value='2022-05-10')
393
+ date2 = gr.Textbox(label="to_date", value='2022-05-30')
394
  leaders = gr.Checkboxgroup(choices=USER_NAMES,
395
  label="")
396
  plot_choices = gr.CheckboxGroup(choices=CHOICE_LIST, label='Choose what to show')