Skip to content

Instantly share code, notes, and snippets.

@smitkadvani
Created March 26, 2024 23:37
Show Gist options
  • Save smitkadvani/c58360166082d4c72b89a0d76c370611 to your computer and use it in GitHub Desktop.
Save smitkadvani/c58360166082d4c72b89a0d76c370611 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import bioframe as bf\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"chr1 11925630\n",
"chr2 177637758\n",
"chr3 62259946\n",
"chr4 202237397\n",
"chr5 200931227\n",
"dtype: int64\n"
]
}
],
"source": [
"unique_chrom_names = [f'chr{i}' for i in range(1, 10001)]\n",
"unique_chrom_lengths = np.random.randint(1_000_000, 250_000_000, size=10000) # Random lengths between 1 million and 250 million\n",
"unique_chrom_series = pd.Series(data=unique_chrom_lengths, index=unique_chrom_names)\n",
"print(unique_chrom_series.head())\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"chromosomes = bf.make_viewframe(unique_chrom_series)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def generate_plot(lp):\n",
" function_info = next(iter(lp.get_stats().timings.keys()))\n",
" filename = function_info[0]\n",
" start_line = function_info[1]\n",
" with open(filename, 'r') as f:\n",
" source_lines = f.readlines()\n",
" function_timings = next(iter(lp.get_stats().timings.values()))\n",
" line_numbers = [line_no for line_no, _, _ in function_timings]\n",
" times_ms = [time_ns / 1_000_000 for _, _, time_ns in function_timings] # Convert nanoseconds to milliseconds\n",
" line_descriptions = [source_lines[line_no - start_line].strip() for line_no, _, _ in function_timings]\n",
" combined = list(zip(line_numbers, times_ms, line_descriptions))\n",
" top_3_lines = sorted(combined, key=lambda x: x[1], reverse=True)[:3]\n",
" plt.figure(figsize=(12, 8))\n",
" bars = plt.bar(line_numbers, times_ms, color='skyblue')\n",
" plt.xticks(line_numbers, [f'Line {ln}' for ln in line_numbers], rotation='vertical')\n",
" plt.ylabel('Time (milliseconds)')\n",
" plt.xlabel('Source Code Line Number')\n",
" plt.title('Profiling Results: Time Spent per Line')\n",
" plt.tight_layout() # Adjust layout to make room for the rotated x-axis labels\n",
" plt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "bioframe-setup",
"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.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment