thak123 commited on
Commit
1a34b46
·
verified ·
1 Parent(s): dd1e68b

Update index.py

Browse files
Files changed (1) hide show
  1. index.py +71 -70
index.py CHANGED
@@ -152,81 +152,82 @@ def update_output(selected_topic, selected_domain, start_date, end_date):
152
  mask_1 = ((df["Topic"] == selected_topic) & (df['date'] >= start_date) & (df['date'] <= end_date))
153
  df_filtered = df.loc[mask_1]
154
  print(df_filtered.shape)
155
- #create line graphs based on filtered dataframes
156
- line_fig_1 = px.line(df_filtered, x="date", y="normalised results",
157
- color='Veículos de notícias', title="O gráfico mostra a evolução temporal de sentimento dos títulos de notícias. Numa escala de -1 (negativo) a 1 (positivo), sendo 0 (neutro).")
158
-
159
- # Veículos de notícias
160
- #set x-axis title and y-axis title in line graphs
161
- line_fig_1.update_layout(
162
- xaxis_title='Data',
163
- yaxis_title='Classificação de Sentimento')
164
-
165
- #set label format on y-axis in line graphs
166
- line_fig_1.update_xaxes(tickformat="%b %d<br>%Y")
167
-
168
- # Bar Graph start
169
- grouped_df = df_filtered.groupby(['date', 'Veículos de notícias']).size().reset_index(name='occurrences')
170
 
171
- # Sort DataFrame by 'period' column
172
- grouped_df = grouped_df.sort_values(by='date')
 
 
 
173
 
174
- # Create a list of all unique media
175
- all_media = df_filtered['domain_folder_name'].unique()
176
 
177
- # Create a date range from Jan/2000 to the last month in the dataset
178
- date_range = pd.date_range(start=df_filtered['date'].min().date(), end=df_filtered['date'].max().date(), freq='MS')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
 
180
- # Create a MultiIndex with all combinations of date_range and all_media
181
- idx = pd.MultiIndex.from_product([date_range, all_media], names=['date', 'Veículos de notícias'])
 
 
 
 
 
 
 
 
 
 
 
182
 
183
- # Reindex the DataFrame to include all periods and media
184
- grouped_df = grouped_df.set_index(['date', 'Veículos de notícias']).reindex(idx, fill_value=0).reset_index()
185
-
186
- bar_fig_1 = px.bar(grouped_df, x='date', y='occurrences', color='Veículos de notícias',
187
- labels={'date': 'Período', 'occurrences': 'Número de notícias', 'Veículos de notícias': 'Portal'},
188
- title='Número de notícias por período de tempo')
189
- bar_fig_1.update_xaxes(tickformat="%b %d<br>%Y")
190
- # Bar Graph ends
191
-
192
- # filter dataframes based on updated data range
193
- mask_2 = ((df_pos["Topic"] == selected_topic) & (df_pos["domain_folder_name"] == selected_domain) & (df_pos['date'] >= start_date) & (df_pos['date'] <= end_date))
194
- mask_3 = ((df_neu["Topic"] == selected_topic) & (df_neu["domain_folder_name"] == selected_domain) & (df_neu['date'] >= start_date) & (df_neu['date'] <= end_date))
195
- mask_4 = ((df_neg["Topic"] == selected_topic) & (df_neg["domain_folder_name"] == selected_domain) & (df_neg['date'] >= start_date) & (df_neg['date'] <= end_date))
196
- df2_filtered = df_pos.loc[mask_2]
197
- df3_filtered = df_neu.loc[mask_3]
198
- df4_filtered = df_neg.loc[mask_4]
199
-
200
- #create line graphs based on filtered dataframes
201
- line_fig_2 = px.line(df2_filtered, x="date", y="rolling_mean_counts", line_group="FinBERT_label",
202
- title="Positive")
203
- line_fig_3 = px.line(df3_filtered, x="date", y="rolling_mean_counts", line_group="FinBERT_label",
204
- title="Neutral")
205
- line_fig_4 = px.line(df4_filtered, x="date", y="rolling_mean_counts", line_group="FinBERT_label",
206
- title="Negative")
207
-
208
- #set x-axis title and y-axis title in line graphs
209
- line_fig_2.update_layout(
210
- xaxis_title='Data',
211
- yaxis_title='Número de notícias com sentimento positivo')
212
- line_fig_3.update_layout(
213
- xaxis_title='Data',
214
- yaxis_title='Número de notícias com sentimento neutro')
215
- line_fig_4.update_layout(
216
- xaxis_title='Data',
217
- yaxis_title='Número de notícias com sentimento negativo')
218
-
219
- #set label format on y-axis in line graphs
220
- line_fig_2.update_xaxes(tickformat="%b %d<br>%Y")
221
- line_fig_3.update_xaxes(tickformat="%b %d<br>%Y")
222
- line_fig_4.update_xaxes(tickformat="%b %d<br>%Y")
223
-
224
- #set label format on y-axis in line graphs
225
- line_fig_2.update_traces(line_color='#1E88E5')
226
- line_fig_3.update_traces(line_color='#004D40')
227
- line_fig_4.update_traces(line_color='#D81B60')
228
-
229
- return line_fig_1, bar_fig_1, line_fig_2, line_fig_3, line_fig_4
230
 
231
  # return line_fig_1
232
 
 
152
  mask_1 = ((df["Topic"] == selected_topic) & (df['date'] >= start_date) & (df['date'] <= end_date))
153
  df_filtered = df.loc[mask_1]
154
  print(df_filtered.shape)
155
+ if len(df_filtered)>0:
156
+ #create line graphs based on filtered dataframes
157
+ line_fig_1 = px.line(df_filtered, x="date", y="normalised results",
158
+ color='Veículos de notícias', title="O gráfico mostra a evolução temporal de sentimento dos títulos de notícias. Numa escala de -1 (negativo) a 1 (positivo), sendo 0 (neutro).")
 
 
 
 
 
 
 
 
 
 
 
159
 
160
+ # Veículos de notícias
161
+ #set x-axis title and y-axis title in line graphs
162
+ line_fig_1.update_layout(
163
+ xaxis_title='Data',
164
+ yaxis_title='Classificação de Sentimento')
165
 
166
+ #set label format on y-axis in line graphs
167
+ line_fig_1.update_xaxes(tickformat="%b %d<br>%Y")
168
 
169
+ # Bar Graph start
170
+ grouped_df = df_filtered.groupby(['date', 'Veículos de notícias']).size().reset_index(name='occurrences')
171
+
172
+ # Sort DataFrame by 'period' column
173
+ grouped_df = grouped_df.sort_values(by='date')
174
+
175
+ # Create a list of all unique media
176
+ all_media = df_filtered['domain_folder_name'].unique()
177
+
178
+ # Create a date range from Jan/2000 to the last month in the dataset
179
+ date_range = pd.date_range(start=df_filtered['date'].min().date(), end=df_filtered['date'].max().date(), freq='MS')
180
+
181
+ # Create a MultiIndex with all combinations of date_range and all_media
182
+ idx = pd.MultiIndex.from_product([date_range, all_media], names=['date', 'Veículos de notícias'])
183
+
184
+ # Reindex the DataFrame to include all periods and media
185
+ grouped_df = grouped_df.set_index(['date', 'Veículos de notícias']).reindex(idx, fill_value=0).reset_index()
186
 
187
+ bar_fig_1 = px.bar(grouped_df, x='date', y='occurrences', color='Veículos de notícias',
188
+ labels={'date': 'Período', 'occurrences': 'Número de notícias', 'Veículos de notícias': 'Portal'},
189
+ title='Número de notícias por período de tempo')
190
+ bar_fig_1.update_xaxes(tickformat="%b %d<br>%Y")
191
+ # Bar Graph ends
192
+
193
+ # filter dataframes based on updated data range
194
+ mask_2 = ((df_pos["Topic"] == selected_topic) & (df_pos["domain_folder_name"] == selected_domain) & (df_pos['date'] >= start_date) & (df_pos['date'] <= end_date))
195
+ mask_3 = ((df_neu["Topic"] == selected_topic) & (df_neu["domain_folder_name"] == selected_domain) & (df_neu['date'] >= start_date) & (df_neu['date'] <= end_date))
196
+ mask_4 = ((df_neg["Topic"] == selected_topic) & (df_neg["domain_folder_name"] == selected_domain) & (df_neg['date'] >= start_date) & (df_neg['date'] <= end_date))
197
+ df2_filtered = df_pos.loc[mask_2]
198
+ df3_filtered = df_neu.loc[mask_3]
199
+ df4_filtered = df_neg.loc[mask_4]
200
 
201
+ #create line graphs based on filtered dataframes
202
+ line_fig_2 = px.line(df2_filtered, x="date", y="rolling_mean_counts", line_group="FinBERT_label",
203
+ title="Positive")
204
+ line_fig_3 = px.line(df3_filtered, x="date", y="rolling_mean_counts", line_group="FinBERT_label",
205
+ title="Neutral")
206
+ line_fig_4 = px.line(df4_filtered, x="date", y="rolling_mean_counts", line_group="FinBERT_label",
207
+ title="Negative")
208
+
209
+ #set x-axis title and y-axis title in line graphs
210
+ line_fig_2.update_layout(
211
+ xaxis_title='Data',
212
+ yaxis_title='Número de notícias com sentimento positivo')
213
+ line_fig_3.update_layout(
214
+ xaxis_title='Data',
215
+ yaxis_title='Número de notícias com sentimento neutro')
216
+ line_fig_4.update_layout(
217
+ xaxis_title='Data',
218
+ yaxis_title='Número de notícias com sentimento negativo')
219
+
220
+ #set label format on y-axis in line graphs
221
+ line_fig_2.update_xaxes(tickformat="%b %d<br>%Y")
222
+ line_fig_3.update_xaxes(tickformat="%b %d<br>%Y")
223
+ line_fig_4.update_xaxes(tickformat="%b %d<br>%Y")
224
+
225
+ #set label format on y-axis in line graphs
226
+ line_fig_2.update_traces(line_color='#1E88E5')
227
+ line_fig_3.update_traces(line_color='#004D40')
228
+ line_fig_4.update_traces(line_color='#D81B60')
229
+
230
+ return line_fig_1, bar_fig_1, line_fig_2, line_fig_3, line_fig_4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231
 
232
  # return line_fig_1
233