<jupyter_start><jupyter_text>Reproducing `sirrice/icd9``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<jupyter_code>import networkx as nx
from icdcodex import hierarchy
G, codes = hierarchy.icd9()<jupyter_output><empty_output><jupyter_text>A simple demonstrationFrom the read me> 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.We can find this hierarchy by using the `shortest_path` method<jupyter_code>cholerae_icd_code = "001.0".replace(".", "")
root_node, *natural_hierarchy = nx.shortest_path(G, source="root", target=cholerae_icd_code)
natural_hierarchy<jupyter_output><empty_output><jupyter_text>Using the library Find top level codesTo find the top level codes, we can do a one layer traversal starting at the root.<jupyter_code>from networkx.algorithms.traversal.breadth_first_search import bfs_tree
top_level_nodes = bfs_tree(G, source="root", depth_limit=1)
top_level_nodes.nodes()<jupyter_output><empty_output><jupyter_text>Any arbitrary sub-nodes are obtained in a similar fashion<jupyter_code>intestinal_infectious_disease_nodes = bfs_tree(G, source="Intestinal Infectious Diseases").nodes()
intestinal_infectious_disease_nodes<jupyter_output><empty_output><jupyter_text>Find all nodes by a search criteria<jupyter_code>[n for n in G.nodes() if n.startswith("001")]<jupyter_output><empty_output><jupyter_text>Find all codes (i.e., leaf nodes) by a search criteria<jupyter_code>cholerae_nodes = bfs_tree(G, source="Cholera").nodes()
[n for n in cholerae_nodes if G.degree[n] == 1]<jupyter_output><empty_output><jupyter_text>Get the description of a code<jupyter_code>G.nodes()["0010"]<jupyter_output><empty_output><jupyter_text>Get a nodes parent and siblings<jupyter_code>parent, = G.predecessors("0010")
print(f"parent: {parent}, siblings: {G[parent]}")<jupyter_output>parent: Cholera, siblings: {'0010': {}, '0011': {}, '0019': {}}
{'fraction_non_alphanumeric': 0.10270498732037193, 'fraction_numerical': 0.021132713440405747, 'mean_word_length': 7.965909090909091, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 33, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '321062', 'license': 'MIT', 'n_tokens_mistral': 851, 'n_tokens_neox': 791, 'n_words': 218, 'path': 'docs/sirrice.ipynb', 'repo_name': 'jeremyadamsfisher/icd-codex', 'size': '2366'}