{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Reproducing `sirrice/icd9`\n", "`icdcodex` recapitulates the functionality of [`sirrice/icd9`](https://github.com/sirrice/icd9) which has similar functionality, which is somewhat dated and does not support ICD-10" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "import networkx as nx\n", "from icdcodex import hierarchy\n", "G, codes = hierarchy.icd9()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A simple demonstration\n", "\n", "From the read me\n", "\n", "> The library encodes ICD9 codes in their natural hierarchy. For example, \"Cholera due to vibrio cholerae\" has the ICD9 code 001.0, and is categorized as a type of Cholera, which in turn is a type of Intestinal Infectious Disease. Specifically, 001.0 has the following hierarchy: \"Cholera due to vibrio cholerae\" has the ICD9 code 001.0, and is categorized as a type of Cholera, which in turn is a type of Intestinal Infectious Disease.\n", "\n", "We can find this hierarchy by using the `shortest_path` method" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Infectious And Parasitic Diseases',\n", " 'Intestinal Infectious Diseases',\n", " 'Cholera',\n", " '0010']" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cholerae_icd_code = \"001.0\".replace(\".\", \"\")\n", "root_node, *natural_hierarchy = nx.shortest_path(G, source=\"root\", target=cholerae_icd_code)\n", "natural_hierarchy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using the library\n", "\n", "### Find top level codes\n", "To find the top level codes, we can do a one layer traversal starting at the root." ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "NodeView(('root', 'Infectious And Parasitic Diseases', 'Neoplasms', 'Endocrine, Nutritional And Metabolic Diseases, And Immunity Disorders', 'Diseases Of The Blood And Blood-Forming Organs', 'Mental Disorders', 'Diseases Of The Nervous System And Sense Organs', 'Diseases Of The Circulatory System', 'Diseases Of The Respiratory System', 'Diseases Of The Digestive System', 'Diseases Of The Genitourinary System', 'Complications Of Pregnancy, Childbirth, And The Puerperium', 'Diseases Of The Skin And Subcutaneous Tissue', 'Diseases Of The Musculoskeletal System And Connective Tissue', 'Congenital Anomalies', 'Certain Conditions Originating In The Perinatal Period', 'Symptoms, Signs, And Ill-Defined Conditions', 'Injury And Poisoning', 'Supplementary Classification Of External Causes Of Injury And Poisoning', 'Supplementary Classification Of Factors Influencing Health Status And Contact With Health Services'))" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from networkx.algorithms.traversal.breadth_first_search import bfs_tree\n", "top_level_nodes = bfs_tree(G, source=\"root\", depth_limit=1)\n", "top_level_nodes.nodes()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Any arbitrary sub-nodes are obtained in a similar fashion" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "NodeView(('Intestinal Infectious Diseases', 'Cholera', 'Typhoid and paratyphoid fevers', 'Other salmonella infections', 'Shigellosis', 'Other food poisoning (bacterial)', 'Amebiasis', 'Other protozoal intestinal diseases', 'Intestinal infections due to other organisms', 'Ill-defined intestinal infections', '0010', '0011', '0019', '0020', '0021', '0022', '0023', '0029', '0030', '0031', '00320', '00321', '00322', '00323', '00324', '00329', '0038', '0039', '0040', '0041', '0042', '0043', '0048', '0049', '0050', '0051', '0052', '0053', '0054', '00581', '00589', '0059', '0060', '0061', '0062', '0063', '0064', '0065', '0066', '0068', '0069', '0070', '0071', '0072', '0073', '0074', '0075', '0078', '0079', '00800', '00801', '00802', '00803', '00804', '00809', '0081', '0082', '0083', '00841', '00842', '00843', '00844', '00845', '00846', '00847', '00849', '0085', '00861', '00862', '00863', '00864', '00865', '00866', '00867', '00869', '0088', '0090', '0091', '0092', '0093'))" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "intestinal_infectious_disease_nodes = bfs_tree(G, source=\"Intestinal Infectious Diseases\").nodes()\n", "intestinal_infectious_disease_nodes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Find all nodes by a search criteria" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['0010', '0011', '0019']" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[n for n in G.nodes() if n.startswith(\"001\")]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Find all codes (i.e., leaf nodes) by a search criteria" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['0010', '0011', '0019']" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cholerae_nodes = bfs_tree(G, source=\"Cholera\").nodes()\n", "[n for n in cholerae_nodes if G.degree[n] == 1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ### Get the description of a code" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'description': 'Cholera due to vibrio cholerae'}" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "G.nodes()[\"0010\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Get a nodes parent and siblings" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "parent: Cholera, siblings: {'0010': {}, '0011': {}, '0019': {}}\n" ] } ], "source": [ "parent, = G.predecessors(\"0010\")\n", "print(f\"parent: {parent}, siblings: {G[parent]}\")" ] } ], "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.7.7" } }, "nbformat": 4, "nbformat_minor": 4 }