Last active
October 29, 2024 22:50
-
-
Save thibaultmol/42f602168410cde2798d695b0cb7f3dc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "361219a3", | |
"metadata": { | |
"id": "361219a3" | |
}, | |
"source": [ | |
"## Cumulative Posts Over Time\n", | |
"\n", | |
"https://colab.research.google.com/drive/1Bq4PFTgVfq8nZfGCc-IC9OlfcebNTbH_?usp=sharing \n", | |
"\n", | |
"This notebook allows you to visualize the cumulative number of posts over time.\n", | |
"Follow the instructions below to input your data and generate the plot.\n", | |
"\n", | |
"THIS CODE IS CREATIVE COMMONS ZERO, DO WITH IT AS YOU PLEASE,.\n", | |
"\n", | |
"FIRST: You need to copy this colab file. Go to 'File' and copy it to your google drive. Then open that file and follow the next instruction on the copied file:\n", | |
"\n", | |
"HOW TO RUN: click at the top 'Runtime' and then choose 'run all'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "9fe6bb84", | |
"metadata": { | |
"id": "9fe6bb84" | |
}, | |
"outputs": [], | |
"source": [ | |
"import matplotlib.pyplot as plt\n", | |
"import pandas as pd\n", | |
"import ipywidgets as widgets\n", | |
"from IPython.display import display, Markdown\n", | |
"\n", | |
"# Display instructions for the user\n", | |
"display(Markdown(\"### Instructions:\"))\n", | |
"display(Markdown(\"\"\"\n", | |
"1. In the next cell, enter your dates in the format: `2024-06-03, 2024-06-03, ...`\n", | |
"2. Run all cells to visualize the cumulative post counts.\n", | |
"\"\"\"))\n", | |
"\n", | |
"# Create input widgets\n", | |
"date_input = widgets.Text(\n", | |
" value='',\n", | |
" placeholder='Enter dates (YYYY-MM-DD, YYYY-MM-DD, ...)',\n", | |
" description='Dates:',\n", | |
" disabled=False\n", | |
")\n", | |
"button = widgets.Button(description=\"Generate Plot\")\n", | |
"output = widgets.Output()\n", | |
"\n", | |
"# Define button click event handler\n", | |
"def on_button_clicked(b):\n", | |
" with output:\n", | |
" output.clear_output() # Clear previous output\n", | |
" user_input = date_input.value\n", | |
" if user_input:\n", | |
" dates = [date.strip() for date in user_input.split(',')]\n", | |
" df = pd.DataFrame(dates, columns=['date'])\n", | |
" df['date'] = pd.to_datetime(df['date'])\n", | |
" daily_post_counts = df['date'].value_counts().sort_index()\n", | |
" cumulative_posts = daily_post_counts.cumsum()\n", | |
"\n", | |
" plt.figure(figsize=(10, 5))\n", | |
" plt.plot(cumulative_posts.index, cumulative_posts.values, marker='o', linestyle='-', linewidth=2)\n", | |
" plt.xlabel('Date')\n", | |
" plt.ylabel('Cumulative Number of Posts')\n", | |
" plt.title('Cumulative Posts Over Time')\n", | |
" plt.xticks(rotation=45)\n", | |
" plt.tight_layout()\n", | |
" plt.show()\n", | |
" else:\n", | |
" display(Markdown(\"**Please enter dates in the input cell above.**\"))\n", | |
"\n", | |
"# Attach event handler to button\n", | |
"button.on_click(on_button_clicked)\n", | |
"\n", | |
"# Display widgets\n", | |
"display(date_input, button, output)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "2c418cad", | |
"metadata": { | |
"id": "2c418cad" | |
}, | |
"outputs": [], | |
"source": [ | |
"\n", | |
"# Provide date input as comma-separated values in the format: \"YYYY-MM-DD, YYYY-MM-DD, ...\"\n", | |
"user_input = date_input.value # PASTE THE COMMA SEPERATED DATE LIST HERE\n", | |
"\n", | |
"# Process the input if provided\n", | |
"if user_input:\n", | |
" dates = [date.strip() for date in user_input.split(',')]\n", | |
"else:\n", | |
" dates = [] # Empty list if user hasn't entered any dates yet\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "15271cb1", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 66 | |
}, | |
"id": "15271cb1", | |
"outputId": "0b0e678c-2e5b-4ffe-f141-5b533da3803d" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": [ | |
"<IPython.core.display.Markdown object>" | |
], | |
"text/markdown": "**Please enter dates in the input cell above.**" | |
}, | |
"metadata": {} | |
} | |
], | |
"source": [ | |
"\n", | |
"if dates:\n", | |
" # Create a DataFrame from the dates\n", | |
" df = pd.DataFrame(dates, columns=['date'])\n", | |
" df['date'] = pd.to_datetime(df['date'])\n", | |
"\n", | |
" # Count occurrences of each date and calculate cumulative posts\n", | |
" daily_post_counts = df['date'].value_counts().sort_index() # Sorting by date\n", | |
" cumulative_posts = daily_post_counts.cumsum()\n", | |
"\n", | |
" # Plotting\n", | |
" plt.figure(figsize=(10, 5))\n", | |
" plt.plot(cumulative_posts.index, cumulative_posts.values, marker='o', linestyle='-', linewidth=2)\n", | |
" plt.xlabel('Date')\n", | |
" plt.ylabel('Cumulative Number of Posts')\n", | |
" plt.title('Cumulative Posts Over Time')\n", | |
" plt.xticks(rotation=45)\n", | |
" plt.tight_layout()\n", | |
" plt.show()\n", | |
"else:\n", | |
" display(Markdown(\"**Please enter dates in the input cell above.**\"))\n" | |
] | |
} | |
], | |
"metadata": { | |
"colab": { | |
"provenance": [] | |
}, | |
"language_info": { | |
"name": "python" | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment