Skip to content

Instantly share code, notes, and snippets.

@Jongmassey
Created January 12, 2022 13:03
Show Gist options
  • Save Jongmassey/34413a3af7563dbbf002792480d4f296 to your computer and use it in GitHub Desktop.
Save Jongmassey/34413a3af7563dbbf002792480d4f296 to your computer and use it in GitHub Desktop.
NHS SNOMED term browser api
import requests as rq
from furl import furl
import time
import sys, threading
import pickle
sys.setrecursionlimit(10 ** 7) # max depth of recursion
threading.stack_size(2 ** 27)
BASE_URL = "https://termbrowser.nhs.uk/sct-browser-api/snomed/uk-edition/v20211124/concepts/"
ANTIBACTERIAL_AGENT = 346325008
def get_children(id):
time.sleep(0.05)
f = furl(BASE_URL)
f.path.segments.append(id)
f.path.segments.append("children")
f.args["form"] = "inferred"
r = rq.get(f.url)
return r.json()
def get_active_ingredients(id):
time.sleep(0.05)
f = furl(BASE_URL)
f.path.segments.append(id)
r = rq.get(f.url)
data = r.json()
return [
(r["target"]["conceptId"], r["target"]["defaultTerm"])
for r in data["relationships"]
if r["type"]["defaultTerm"]
== "Has specific active ingredient (attribute)"
]
def walk(c):
children = get_children(c["conceptId"])
if len(children) == 0:
yield (c["conceptId"], get_active_ingredients(c["conceptId"]))
else:
for c in children:
yield from walk(c)
# if c["definitionStatus"] == "Primitive":
# yield (c["conceptId"], get_active_ingredients(c["conceptId"]))
# else:
# children = get_children(c["conceptId"])
# for c in children:
# yield from walk(c)
data = get_children(ANTIBACTERIAL_AGENT)
results = {}
for c in data:
results[(c["conceptId"], c["defaultTerm"])] = [a for a in walk(c)]
with open("results.pickle", "wb") as f:
pickle.dump(results, f)
with open("categories.csv", "w") as f:
for c in list(results.keys()):
f.write(f'"{c[0]}","{c[1]}"\n')
with open("ingredients.csv", "w") as f:
f.write("category_id,product_id,ingredient_id,ingredient\n")
for k, v in results.items():
category = k[0]
for p in v:
product = p[0]
for ingredient in p[1]:
f.write(
f'"{category}","{product}","{ingredient[0]}","{ingredient[1]}"\n'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment