Skip to content

Instantly share code, notes, and snippets.

@gkthiruvathukal
Created July 20, 2021 18:19
Show Gist options
  • Save gkthiruvathukal/20a1bf1bf06f900e8d033526ccf4a9d3 to your computer and use it in GitHub Desktop.
Save gkthiruvathukal/20a1bf1bf06f900e8d033526ccf4a9d3 to your computer and use it in GitHub Desktop.
Interval Tree Python Example pairing with Nick Synovic
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "aa343163",
"metadata": {},
"outputs": [],
"source": [
"import intervaltree"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "cf4846c1",
"metadata": {},
"outputs": [],
"source": [
"from intervaltree import IntervalTree\n",
"tree = IntervalTree()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "2221fd77",
"metadata": {},
"outputs": [],
"source": [
"tree.addi(0, 4, \"Issue 1\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "14ba1710",
"metadata": {},
"outputs": [],
"source": [
"tree.addi(2, 7, \"Issue 2\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "2f082d95",
"metadata": {},
"outputs": [],
"source": [
"tree.addi(6, 8, \"Issue 3\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "fe70f39d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"IntervalTree([Interval(0, 4, 'Issue 1'), Interval(2, 7, 'Issue 2'), Interval(6, 8, 'Issue 3')])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "1cfc0f44",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{Interval(0, 4, 'Issue 1')}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree.overlap(0, 1)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "78e2dd01",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"day 0, overlap {Interval(0, 4, 'Issue 1')}\n",
"day 1, overlap {Interval(0, 4, 'Issue 1')}\n",
"day 2, overlap {Interval(0, 4, 'Issue 1'), Interval(2, 7, 'Issue 2')}\n",
"day 3, overlap {Interval(0, 4, 'Issue 1'), Interval(2, 7, 'Issue 2')}\n",
"day 4, overlap {Interval(2, 7, 'Issue 2')}\n",
"day 5, overlap {Interval(2, 7, 'Issue 2')}\n",
"day 6, overlap {Interval(6, 8, 'Issue 3'), Interval(2, 7, 'Issue 2')}\n",
"day 7, overlap {Interval(6, 8, 'Issue 3')}\n",
"day 8, overlap set()\n",
"day 9, overlap set()\n"
]
}
],
"source": [
"for i in range(0, 10):\n",
" print(\"day %d, overlap %s\" % (i, tree.overlap(i, i+1)))\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "0b433848",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{Interval(0, 4, 'Issue 1'), Interval(2, 7, 'Issue 2')}"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree.overlap(0, 4)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "3b4e4436",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{Interval(0, 4, 'Issue 1')}"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree.at(0)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "7163e5b9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{Interval(0, 4, 'Issue 1')}"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree.at(1)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "b2cba4fc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{Interval(0, 4, 'Issue 1'), Interval(2, 7, 'Issue 2')}"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree.at(2)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "1969eb91",
"metadata": {},
"outputs": [],
"source": [
"tree.addi(3, 9, {})"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "fa60b441",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{Interval(0, 4, 'Issue 1'), Interval(2, 7, 'Issue 2'), Interval(3, 9, {})}"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree.at(3)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "407685ad",
"metadata": {},
"outputs": [
{
"ename": "KeyError",
"evalue": "30",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipykernel_1844694/817043336.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mOut\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m30\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m: 30"
]
}
],
"source": [
"type(Out[30])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "def99d8a",
"metadata": {},
"outputs": [],
"source": [
"for entry in tree.at(2):\n",
" print(entry.begin, entry.end, entry.data)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "949b3b04",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"set()"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree.at(9)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0aa6b446",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment