Skip to content

Commit

Permalink
feat: add violins for nodejs recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
zieka committed Aug 5, 2024
1 parent f2f5b53 commit 22e88ba
Show file tree
Hide file tree
Showing 22 changed files with 6,401 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from code_data_science import data_table as dt\n",
"\n",
"df = dt.read_csv(\"../samples/dependency_usage_violin_nodejs.csv\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df = df[[\"name\", \"requestedVersion\"]]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from code_data_science.versions import index as index_versions\n",
"import re\n",
"\n",
"# make sure version is a string\n",
"df[\"requestedVersion\"] = df[\"requestedVersion\"].astype(str)\n",
"df[\"version\"] = list(\n",
" map(\n",
" lambda v: v.removeprefix(\"^\")\n",
" .removeprefix(\"~\")\n",
" .removeprefix(\">\")\n",
" .removeprefix(\"=\"),\n",
" df.requestedVersion,\n",
" )\n",
")\n",
"\n",
"vmap = index_versions(df.version)\n",
"df[\"nVersion\"] = list(map(lambda v: vmap[v], df.version))\n",
"\n",
"def index_name(names):\n",
" sorted_names = sorted(list(set(names)))\n",
" return {name: sorted_names.index(name) for name in sorted_names}\n",
"\n",
"\n",
"nmap = index_name(df.name)\n",
"df[\"nName\"] = list(map(lambda g: nmap[g], df.name))\n",
"\n",
"df = df.sort_values(by=[\"nVersion\", \"nName\"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import plotly.graph_objects as go\n",
"import code_data_science.palette as palette\n",
"\n",
"colors = palette.colors_by_weight(500)\n",
"\n",
"fig = go.Figure()\n",
"\n",
"# Add a trace to the plot for each category\n",
"for i, category in enumerate(df[\"nName\"].unique()):\n",
" category_data = df[df[\"nName\"] == category]\n",
"\n",
" # Calculate counts for each dependency and version combination\n",
" counts = (\n",
" category_data.groupby(\"nVersion\")[\"nName\"].count().reset_index(name=\"count\")\n",
" )\n",
"\n",
" category_data_with_counts = category_data.merge(counts, on=\"nVersion\")\n",
"\n",
" # Generate hover text including the count information\n",
" hover_text = category_data_with_counts.apply(\n",
" lambda row: f'<b>Package</b>: {row[\"name\"]}<br><b>Version</b>: {row[\"version\"]}<br><b>Count</b>: {row[\"count\"]}',\n",
" axis=1,\n",
" )\n",
"\n",
" fig.add_trace(\n",
" go.Scatter(\n",
" x=category_data[\"nName\"],\n",
" y=category_data[\"nVersion\"],\n",
" mode=\"markers\",\n",
" marker=dict(color=colors[i % len(colors)], size=8),\n",
" showlegend=False,\n",
" name=\"\",\n",
" text=hover_text,\n",
" hoverinfo=\"text\",\n",
" hoverlabel=dict(font=dict(size=18)),\n",
" )\n",
" )\n",
"\n",
" fig.add_trace(\n",
" go.Violin(\n",
" x=category_data[\"nName\"],\n",
" y=category_data[\"nVersion\"],\n",
" fillcolor=\"black\",\n",
" opacity=0.15,\n",
" line_color=\"black\",\n",
" showlegend=False,\n",
" width=0.7,\n",
" bandwidth=0.4,\n",
" hoverinfo=\"none\",\n",
" hoveron=\"points\",\n",
" )\n",
" )\n",
"\n",
"num_versions = df[\"nVersion\"].nunique()\n",
"height_per_version = 32\n",
"width_per_dependency = 80\n",
"fig_height = max(num_versions * height_per_version, 900)\n",
"fig_width = max(len(list(nmap.values())) * width_per_dependency, 900)\n",
"tick_font_size = 13\n",
"# Customizing the layout\n",
"fig.update_layout(\n",
" title=\"Package versions in use\",\n",
" xaxis_title=\"Packages\",\n",
" yaxis_title=\"Versions\",\n",
" height=fig_height,\n",
" width=fig_width,\n",
" xaxis=dict(\n",
" tickfont=dict(size=tick_font_size),\n",
" tickmode=\"array\",\n",
" tickvals=list(nmap.values()),\n",
" ticktext=list(nmap.keys()),\n",
" ),\n",
" yaxis=dict(\n",
" tickfont=dict(size=tick_font_size),\n",
" tickmode=\"array\",\n",
" tickvals=list(vmap.values()),\n",
" ticktext=list(vmap.keys()),\n",
" ),\n",
")\n",
"\n",
"fig.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
169 changes: 169 additions & 0 deletions moderne_visualizations_misc/dependency_usage_violin_nodejs_db.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from code_data_science import data_table as dt\n",
"\n",
"df = dt.read_csv(\"../samples/dependency_usage_violin_nodejs.csv\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df = df[[\"name\", \"requestedVersion\"]]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from code_data_science.versions import index as index_versions\n",
"import re\n",
"\n",
"# make sure version is a string\n",
"df[\"requestedVersion\"] = df[\"requestedVersion\"].astype(str)\n",
"df[\"version\"] = list(\n",
" map(\n",
" lambda v: v.removeprefix(\"^\")\n",
" .removeprefix(\"~\")\n",
" .removeprefix(\">\")\n",
" .removeprefix(\"=\"),\n",
" df.requestedVersion,\n",
" )\n",
")\n",
"\n",
"vmap = index_versions(df.version)\n",
"df[\"nVersion\"] = list(map(lambda v: vmap[v], df.version))\n",
"\n",
"def index_name(names):\n",
" sorted_names = sorted(list(set(names)))\n",
" return {name: sorted_names.index(name) for name in sorted_names}\n",
"\n",
"\n",
"nmap = index_name(df.name)\n",
"df[\"nName\"] = list(map(lambda g: nmap[g], df.name))\n",
"\n",
"df = df.sort_values(by=[\"nVersion\", \"nName\"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import plotly.graph_objects as go\n",
"import code_data_science.palette as palette\n",
"\n",
"colors = palette.colors_by_weight(500)\n",
"\n",
"fig = go.Figure()\n",
"\n",
"# Add a trace to the plot for each category\n",
"for i, category in enumerate(df[\"nName\"].unique()):\n",
" category_data = df[df[\"nName\"] == category]\n",
"\n",
" # Calculate counts for each dependency and version combination\n",
" counts = (\n",
" category_data.groupby(\"nVersion\")[\"nName\"].count().reset_index(name=\"count\")\n",
" )\n",
"\n",
" category_data_with_counts = category_data.merge(counts, on=\"nVersion\")\n",
"\n",
" # Generate hover text including the count information\n",
" hover_text = category_data_with_counts.apply(\n",
" lambda row: f'<b>Package</b>: {row[\"name\"]}<br><b>Version</b>: {row[\"version\"]}<br><b>Count</b>: {row[\"count\"]}',\n",
" axis=1,\n",
" )\n",
"\n",
" fig.add_trace(\n",
" go.Scatter(\n",
" x=category_data[\"nName\"],\n",
" y=category_data[\"nVersion\"],\n",
" mode=\"markers\",\n",
" marker=dict(color=colors[i % len(colors)], size=8),\n",
" showlegend=False,\n",
" name=\"\",\n",
" text=hover_text,\n",
" hoverinfo=\"text\",\n",
" hoverlabel=dict(font=dict(size=18)),\n",
" )\n",
" )\n",
"\n",
" fig.add_trace(\n",
" go.Violin(\n",
" x=category_data[\"nName\"],\n",
" y=category_data[\"nVersion\"],\n",
" fillcolor=\"black\",\n",
" opacity=0.15,\n",
" line_color=\"black\",\n",
" showlegend=False,\n",
" width=0.7,\n",
" bandwidth=0.4,\n",
" hoverinfo=\"none\",\n",
" hoveron=\"points\",\n",
" )\n",
" )\n",
"\n",
"num_versions = df[\"nVersion\"].nunique()\n",
"height_per_version = 32\n",
"width_per_dependency = 80\n",
"fig_height = max(num_versions * height_per_version, 900)\n",
"fig_width = max(len(list(nmap.values())) * width_per_dependency, 900)\n",
"tick_font_size = 13\n",
"# Customizing the layout\n",
"fig.update_layout(\n",
" title=\"Package versions in use\",\n",
" xaxis_title=\"Packages\",\n",
" yaxis_title=\"Versions\",\n",
" height=fig_height,\n",
" width=fig_width,\n",
" xaxis=dict(\n",
" tickfont=dict(size=tick_font_size),\n",
" tickmode=\"array\",\n",
" tickvals=list(nmap.values()),\n",
" ticktext=list(nmap.keys()),\n",
" ),\n",
" yaxis=dict(\n",
" tickfont=dict(size=tick_font_size),\n",
" tickmode=\"array\",\n",
" tickvals=list(vmap.values()),\n",
" ticktext=list(vmap.keys()),\n",
" ),\n",
")\n",
"\n",
"fig.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 22e88ba

Please sign in to comment.