[f54d94]: / tutorials / 1_Ontology.ipynb

Download this file

157 lines (156 with data), 5.3 kB

{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# FEMR Ontology support\n",
    "\n",
    "FEMR provides support for querying ontologies using the OMOP Vocabulary. \n",
    "\n",
    "This enables easier definition of labeling functions as well as better feature generation."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Downloading the OMOP Vocabulary\n",
    "\n",
    "The OMOP Vocabulary can be downloaded for free from the [OHDSI ATHENA website.](https://athena.ohdsi.org/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Processing the OMOP Vocabulary\n",
    "\n",
    "femr.ontology.Ontology allows you to process, and then use the OMOP Vocabulary, optionally combining it with [code metadata from MEDS](https://github.com/Medical-Event-Data-Standard/meds/blob/e93f63a2f9642123c49a31ecffcdb84d877dc54a/src/meds/__init__.py#L94).\n",
    "\n",
    "```python \n",
    "ontology = femr.ontology.Ontology(path_to_athena, code_metadata)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Working with an Ontology object\n",
    "\n",
    "The following code samples illustrate the main ways to use a vocabulary object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/esteinberg/miniconda3/envs/debug_document_femr/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loaded ontology\n"
     ]
    }
   ],
   "source": [
    "import pickle\n",
    "\n",
    "# You can load / save ontology objects with pickle\n",
    "\n",
    "with open('input/meds/ontology.pkl', 'rb') as f:\n",
    "    ontology = pickle.load(f)\n",
    "\n",
    "print(\"Loaded ontology\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Generating train split: 200 examples [00:00, 34972.93 examples/s]\n",
      "Map: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 200/200 [00:00<00:00, 3282.29 examples/s]\n"
     ]
    }
   ],
   "source": [
    "# Ontology datasets downloaded by Athena tend to be very large as they contain many codes, including several that are no longer used.\n",
    "# We therefore provide a function to prune ontologies to a particular dataset of interest.\n",
    "# This makes it much cheaper to store and use an ontology object, both in terms of disk space and RAM\n",
    "\n",
    "import datasets\n",
    "dataset = datasets.Dataset.from_parquet(\"input/meds/data/*\")\n",
    "\n",
    "ontology.prune_to_dataset(dataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Description DRUGS FOR PEPTIC ULCER AND GASTRO-OESOPHAGEAL REFLUX DISEASE (GORD)\n",
      "Parents {'ATC/A02'}\n",
      "Children {'ATC/A02BX'}\n",
      "All children {'RxNorm/2344', 'ATC/A02BX', 'RxNorm/4501', 'ATC/A02BX71', 'ATC/A02B', 'RxNorm/7815', 'RxNorm/7019', 'ATC/A02BX77', 'RxNorm/2353', 'RxNorm/8705', 'RxNorm/38574', 'RxNorm/2620', 'RxNorm/2018', 'RxNorm/8704', 'RxNorm/8730', 'RxNorm/6852', 'RxNorm/2017', 'RxNorm/2403'}\n",
      "All parents {'ATC/A', 'ATC/A02', 'ATC/A02B'}\n"
     ]
    }
   ],
   "source": [
    "# First, we can query the description for a particular code\n",
    "print(\"Description\", ontology.get_description(\"ATC/A02B\"))\n",
    "\n",
    "# Second, we can search for the parents of a particular code\n",
    "print(\"Parents\", ontology.get_parents(\"ATC/A02B\"))\n",
    "\n",
    "# Finally, we can search for the children of a particular code\n",
    "print(\"Children\", ontology.get_children(\"ATC/A02B\"))\n",
    "\n",
    "# For the sake of convience, we also support the recursive versions of querying parents and children\n",
    "print(\"All children\", ontology.get_all_children(\"ATC/A02B\"))\n",
    "print(\"All parents\", ontology.get_all_parents(\"ATC/A02B\"))"
   ]
  }
 ],
 "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.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}