Spaces:
Running
Running
Update analytics_plot_generator.py
Browse files- analytics_plot_generator.py +1 -62
analytics_plot_generator.py
CHANGED
@@ -111,67 +111,6 @@ def generate_posts_activity_plot(df, date_column='published_at'):
|
|
111 |
if fig: plt.close(fig)
|
112 |
return create_placeholder_plot(title="Posts Activity Error", message=str(e))
|
113 |
|
114 |
-
def generate_engagement_type_plot(df, likes_col='likeCount', comments_col='commentCount', shares_col='shareCount'):
|
115 |
-
"""Generates a bar plot for total engagement types (likes, comments, shares)."""
|
116 |
-
logging.info(f"Generating engagement type plot. Input df rows: {len(df) if df is not None else 'None'}")
|
117 |
-
|
118 |
-
required_cols = [likes_col, comments_col, shares_col]
|
119 |
-
if df is None or df.empty:
|
120 |
-
logging.warning("Engagement type: DataFrame is empty.")
|
121 |
-
return create_placeholder_plot(title="Post Engagement Types", message="No data available for the selected period.")
|
122 |
-
|
123 |
-
missing_cols = [col for col in required_cols if col not in df.columns]
|
124 |
-
if missing_cols:
|
125 |
-
msg = f"Engagement type: Columns missing: {missing_cols}. Available: {df.columns.tolist()}"
|
126 |
-
logging.warning(msg)
|
127 |
-
return create_placeholder_plot(title="Post Engagement Types", message=msg)
|
128 |
-
|
129 |
-
fig = None
|
130 |
-
try:
|
131 |
-
df_copy = df.copy()
|
132 |
-
for col in required_cols:
|
133 |
-
df_copy[col] = pd.to_numeric(df_copy[col], errors='coerce').fillna(0)
|
134 |
-
|
135 |
-
total_likes = df_copy[likes_col].sum()
|
136 |
-
total_comments = df_copy[comments_col].sum()
|
137 |
-
total_shares = df_copy[shares_col].sum()
|
138 |
-
|
139 |
-
if total_likes == 0 and total_comments == 0 and total_shares == 0:
|
140 |
-
logging.info("Engagement type: All engagement counts are zero.")
|
141 |
-
return create_placeholder_plot(title="Post Engagement Types", message="No engagement data (likes, comments, shares) in the selected period.")
|
142 |
-
|
143 |
-
engagement_data = {
|
144 |
-
'Likes': total_likes,
|
145 |
-
'Comments': total_comments,
|
146 |
-
'Shares': total_shares
|
147 |
-
}
|
148 |
-
|
149 |
-
categories = list(engagement_data.keys())
|
150 |
-
values = list(engagement_data.values())
|
151 |
-
|
152 |
-
# Define a list of distinct colors for the bars
|
153 |
-
bar_colors = plt.cm.get_cmap('Pastel1', len(categories))
|
154 |
-
|
155 |
-
fig, ax = plt.subplots(figsize=(8, 5))
|
156 |
-
_apply_rounded_corners_and_transparent_bg(fig, ax)
|
157 |
-
|
158 |
-
bars = ax.bar(categories, values, color=[bar_colors(i) for i in range(len(categories))], zorder=1)
|
159 |
-
ax.set_xlabel('Engagement Type')
|
160 |
-
ax.set_ylabel('Total Count')
|
161 |
-
ax.grid(axis='y', linestyle='--', alpha=0.6, zorder=0)
|
162 |
-
|
163 |
-
for bar in bars:
|
164 |
-
yval = bar.get_height()
|
165 |
-
ax.text(bar.get_x() + bar.get_width()/2.0, yval + (0.01 * max(values, default=10)), str(int(yval)), ha='center', va='bottom', zorder=2)
|
166 |
-
|
167 |
-
fig.tight_layout(pad=0.5)
|
168 |
-
fig.subplots_adjust(top=0.92, bottom=0.15, left=0.1, right=0.95) # Adjusted spacing
|
169 |
-
logging.info("Successfully generated engagement type plot.")
|
170 |
-
return fig
|
171 |
-
except Exception as e:
|
172 |
-
logging.error(f"Error generating engagement type plot: {e}", exc_info=True)
|
173 |
-
if fig: plt.close(fig)
|
174 |
-
return create_placeholder_plot(title="Engagement Type Error", message=str(e))
|
175 |
|
176 |
def generate_mentions_activity_plot(df, date_column='date'):
|
177 |
"""Generates a plot for mentions activity over time."""
|
@@ -849,7 +788,7 @@ def _parse_eb_label(label_data):
|
|
849 |
return []
|
850 |
return [str(label_data)] # Fallback for other types, ensuring it's a list
|
851 |
|
852 |
-
def generate_content_topic_breakdown_plot(df, topics_col='
|
853 |
title = f"Breakdown of Content by Topics (Top {top_n})"
|
854 |
logging.info(f"Generating {title}. Topics column: '{topics_col}'. Input df rows: {len(df) if df is not None else 'None'}")
|
855 |
|
|
|
111 |
if fig: plt.close(fig)
|
112 |
return create_placeholder_plot(title="Posts Activity Error", message=str(e))
|
113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
def generate_mentions_activity_plot(df, date_column='date'):
|
116 |
"""Generates a plot for mentions activity over time."""
|
|
|
788 |
return []
|
789 |
return [str(label_data)] # Fallback for other types, ensuring it's a list
|
790 |
|
791 |
+
def generate_content_topic_breakdown_plot(df, topics_col='li_eb_labels', top_n=15):
|
792 |
title = f"Breakdown of Content by Topics (Top {top_n})"
|
793 |
logging.info(f"Generating {title}. Topics column: '{topics_col}'. Input df rows: {len(df) if df is not None else 'None'}")
|
794 |
|