Spaces:
Restarting
Restarting
Update analytics_plot_generator.py
Browse files- analytics_plot_generator.py +0 -112
analytics_plot_generator.py
CHANGED
@@ -839,115 +839,3 @@ def generate_content_topic_breakdown_plot(df, topics_col='li_eb_labels', top_n=1
|
|
839 |
logging.error(f"Error generating {title}: {e}", exc_info=True)
|
840 |
if fig: plt.close(fig)
|
841 |
return create_placeholder_plot(title=f"{title} Error", message=str(e))
|
842 |
-
|
843 |
-
|
844 |
-
if __name__ == '__main__':
|
845 |
-
# Create dummy data for testing
|
846 |
-
posts_data = {
|
847 |
-
'id': [f'post{i}' for i in range(1, 8)],
|
848 |
-
'published_at': pd.to_datetime(['2023-01-01', '2023-01-01', '2023-01-02', '2023-01-03', '2023-01-03', '2023-01-03', '2023-01-04']),
|
849 |
-
'likeCount': [10, 5, 12, 8, 15, 3, 20],
|
850 |
-
'commentCount': [2, 1, 3, 1, 4, 0, 5],
|
851 |
-
'shareCount': [1, 0, 1, 1, 2, 0, 1],
|
852 |
-
'clickCount': [20, 15, 30, 22, 40, 10, 50],
|
853 |
-
'impressionCount': [200, 150, 300, 220, 400, 100, 500],
|
854 |
-
'engagement': [0.05, 0.04, 0.06, 0.055, 0.07, 0.03, 0.08],
|
855 |
-
'media_type': ['TEXT', 'IMAGE', 'TEXT', 'VIDEO', 'IMAGE', 'TEXT', 'IMAGE'],
|
856 |
-
'eb_labels': [
|
857 |
-
"['AI', 'Tech']", # String representation of a list
|
858 |
-
['Innovation'], # Actual list
|
859 |
-
'General', # Single string
|
860 |
-
None,
|
861 |
-
['Tech', 'Future'],
|
862 |
-
"['AI', 'Development']",
|
863 |
-
['Tech']
|
864 |
-
],
|
865 |
-
# Corrected 'comment_sentiment' to match a potential fallback name 'sentiment' if the primary is missing.
|
866 |
-
# For testing, let's assume 'comment_sentiment' is the primary and 'sentiment' is a fallback.
|
867 |
-
# To test the fallback, you could rename 'comment_sentiment' to something else in one run.
|
868 |
-
'comment_sentiment': ['Positive', 'Neutral', 'Positive', 'Negative', 'Positive', 'Neutral', 'Positive']
|
869 |
-
}
|
870 |
-
sample_merged_posts_df = pd.DataFrame(posts_data)
|
871 |
-
# Example: To test fallback for comment sentiment, you could do:
|
872 |
-
# sample_merged_posts_df_test_fallback = sample_merged_posts_df.rename(columns={'comment_sentiment': 'actual_comment_sentiment'})
|
873 |
-
# sample_merged_posts_df_test_fallback['sentiment'] = sample_merged_posts_df_test_fallback['actual_comment_sentiment']
|
874 |
-
|
875 |
-
|
876 |
-
follower_data = {
|
877 |
-
'follower_count_type': [
|
878 |
-
'follower_gains_monthly', 'follower_gains_monthly', 'follower_gains_monthly',
|
879 |
-
'follower_geo', 'follower_geo', 'follower_geo', 'follower_geo', 'follower_geo', 'follower_geo', 'follower_geo', 'follower_geo', 'follower_geo', 'follower_geo',
|
880 |
-
'follower_function', 'follower_function',
|
881 |
-
'follower_industry', 'follower_industry',
|
882 |
-
'follower_seniority', 'follower_seniority'
|
883 |
-
],
|
884 |
-
'category_name': [
|
885 |
-
'2024-01-01', '2024-02-01', '2024-03-01',
|
886 |
-
'Italy', 'United Kingdom', 'Spain', 'Germany', 'Switzerland', 'France', 'United States', 'Netherlands', 'Brazil', 'Belgium',
|
887 |
-
'Engineering', 'Sales',
|
888 |
-
'Tech', 'Finance',
|
889 |
-
'Senior', 'Junior'
|
890 |
-
],
|
891 |
-
'follower_count_organic': [
|
892 |
-
100, 110, 125,
|
893 |
-
4500, 187, 106, 83, 68, 63, 55, 41, 22, 22,
|
894 |
-
400, 200,
|
895 |
-
250, 180,
|
896 |
-
300, 220
|
897 |
-
],
|
898 |
-
'follower_count_paid': [
|
899 |
-
20, 30, 25,
|
900 |
-
200, 10, 5, 10, 5, 8, 2, 5, 3, 1,
|
901 |
-
30, 20,
|
902 |
-
45, 35,
|
903 |
-
60, 40
|
904 |
-
]
|
905 |
-
}
|
906 |
-
sample_follower_stats_df = pd.DataFrame(follower_data)
|
907 |
-
|
908 |
-
mentions_data = { # Added for mentions plot testing
|
909 |
-
'date': pd.to_datetime(['2023-01-01', '2023-01-02', '2023-01-02', '2023-01-03']),
|
910 |
-
'sentiment_label': ['Positive', 'Negative', 'Positive', 'Neutral']
|
911 |
-
}
|
912 |
-
sample_mentions_df = pd.DataFrame(mentions_data)
|
913 |
-
|
914 |
-
|
915 |
-
# --- Test individual plots ---
|
916 |
-
# (Using a dictionary for easier management and selective testing)
|
917 |
-
plot_functions_to_test = {
|
918 |
-
"Posts Activity": (generate_posts_activity_plot, [sample_merged_posts_df.copy()], {}),
|
919 |
-
"Engagement Type": (generate_engagement_type_plot, [sample_merged_posts_df.copy()], {}),
|
920 |
-
"Mentions Activity": (generate_mentions_activity_plot, [sample_mentions_df.copy()], {}), # Added sample_mentions_df
|
921 |
-
"Mention Sentiment": (generate_mention_sentiment_plot, [sample_mentions_df.copy()], {}), # Added sample_mentions_df
|
922 |
-
"Followers Count": (generate_followers_count_over_time_plot, [sample_follower_stats_df.copy()], {'type_value':'follower_gains_monthly'}),
|
923 |
-
"Followers Growth": (generate_followers_growth_rate_plot, [sample_follower_stats_df.copy()], {'type_value':'follower_gains_monthly'}),
|
924 |
-
"Followers Geo": (generate_followers_by_demographics_plot, [sample_follower_stats_df.copy()], {'type_value':'follower_geo', 'plot_title':"Followers by Location"}),
|
925 |
-
"Engagement Rate": (generate_engagement_rate_over_time_plot, [sample_merged_posts_df.copy()], {}),
|
926 |
-
"Reach (Clicks)": (generate_reach_over_time_plot, [sample_merged_posts_df.copy()], {}),
|
927 |
-
"Impressions": (generate_impressions_over_time_plot, [sample_merged_posts_df.copy()], {}),
|
928 |
-
"Likes Over Time": (generate_likes_over_time_plot, [sample_merged_posts_df.copy()], {}),
|
929 |
-
"Shares Over Time": (generate_shares_over_time_plot, [sample_merged_posts_df.copy()], {}),
|
930 |
-
"Comments Over Time": (generate_comments_over_time_plot, [sample_merged_posts_df.copy()], {}),
|
931 |
-
"Comments Sentiment": (generate_comments_sentiment_breakdown_plot, [sample_merged_posts_df.copy()], {'sentiment_column':'comment_sentiment'}),
|
932 |
-
"Post Frequency Daily": (generate_post_frequency_plot, [sample_merged_posts_df.copy()], {'resample_period':'D'}),
|
933 |
-
"Post Frequency Weekly": (generate_post_frequency_plot, [sample_merged_posts_df.copy()], {'resample_period':'W'}),
|
934 |
-
"Content Format": (generate_content_format_breakdown_plot, [sample_merged_posts_df.copy()], {}),
|
935 |
-
"Content Topics": (generate_content_topic_breakdown_plot, [sample_merged_posts_df.copy()], {'top_n':5}),
|
936 |
-
}
|
937 |
-
|
938 |
-
# Create a directory to save plots if it doesn't exist
|
939 |
-
# import os
|
940 |
-
# output_dir = "test_plots_updated" # Changed dir name
|
941 |
-
# os.makedirs(output_dir, exist_ok=True)
|
942 |
-
|
943 |
-
for name, (func, args, kwargs_dict) in plot_functions_to_test.items(): # Renamed kwargs to kwargs_dict
|
944 |
-
logging.info(f"--- Testing: {name} ---")
|
945 |
-
fig = func(*args, **kwargs_dict) # Use kwargs_dict
|
946 |
-
if fig:
|
947 |
-
logging.info(f"{name} plot generated.")
|
948 |
-
# fig.savefig(os.path.join(output_dir, f"{name.lower().replace(' ', '_')}_test.png"))
|
949 |
-
# plt.close(fig) # Close the figure after saving to free memory
|
950 |
-
else:
|
951 |
-
logging.warning(f"{name} plot generation failed or returned None.")
|
952 |
-
|
953 |
-
logging.info("Test script finished. Review plots if saved locally.")
|
|
|
839 |
logging.error(f"Error generating {title}: {e}", exc_info=True)
|
840 |
if fig: plt.close(fig)
|
841 |
return create_placeholder_plot(title=f"{title} Error", message=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|