Skip to content

Commit

Permalink
Adds chapter, root_block, root_category columns to `meta_icd11_…
Browse files Browse the repository at this point in the history
…diagnosis` DB table (#1448)

* add `root_label` column to `meta_icd11_diagnosis` db table

* adds `chapter`, `root_block`, `root_category`

* fix category being None
  • Loading branch information
rithviknishad authored Jul 13, 2023
1 parent e9d6be9 commit 0f4486a
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 3 deletions.
67 changes: 64 additions & 3 deletions care/facility/management/commands/load_meta_icd11_diagnosis.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,70 @@ class Command(BaseCommand):

help = "Loads ICD11 data to a table in to database."

data = []
roots_lookup = {}
"""
Eg:
```
{
"http://id.who.int/icd/entity/594985340": {
"chapter": "Certain infectious or parasitic diseases",
"block": "Intestinal infectious diseases",
"category": None,
},
}
```
"""

CLASS_KIND_DB_KEYS = {
"block": "root_block",
"category": "root_category",
}

def find_roots(self, item):
id = item["ID"]

if id in self.roots_lookup:
return self.roots_lookup[id]

if not item["parentId"]:
self.roots_lookup[id] = {item["classKind"]: item["label"]}
return self.roots_lookup[id]

if parent := self.roots_lookup.get(item["parentId"]):

def my(x):
return item["label"] if item["classKind"] == x else None

self.roots_lookup[id] = {
"chapter": parent.get("chapter") or my("chapter"),
"block": parent.get("block") or my("block"),
"category": parent.get("category") or my("category"),
}
return self.roots_lookup[id]

# The following code is never executed as the `icd11.json` file is
# pre-sorted and hence the parent is always present before the child.
print("Full-scan for", id, item["label"])
return self.find_roots(
[
icd11_object
for icd11_object in self.data
if icd11_object["ID"] == item["parentId"]
][0]
)

def handle(self, *args, **options):
print("Loading ICD11 data to database...")
print("Loading ICD11 data to DB Table (meta_icd11_diagnosis)...")
try:
icd11_objects = fetch_data()
self.data = fetch_data()

def roots(item):
return {
self.CLASS_KIND_DB_KEYS.get(k, k): v
for k, v in self.find_roots(item).items()
}

MetaICD11Diagnosis.objects.all().delete()
MetaICD11Diagnosis.objects.bulk_create(
[
Expand All @@ -30,8 +90,9 @@ def handle(self, *args, **options):
is_leaf=icd11_object["isLeaf"],
label=icd11_object["label"],
breadth_value=icd11_object["breadthValue"],
**roots(icd11_object),
)
for icd11_object in icd11_objects
for icd11_object in self.data
if icd11_object["ID"].split("/")[-1].isnumeric()
]
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 4.2.2 on 2023-07-12 13:38

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("facility", "0370_merge_20230705_1500"),
]

operations = [
migrations.AddField(
model_name="metaicd11diagnosis",
name="chapter",
field=models.CharField(default="", max_length=255),
preserve_default=False,
),
migrations.AddField(
model_name="metaicd11diagnosis",
name="root_block",
field=models.CharField(max_length=255, null=True),
),
migrations.AddField(
model_name="metaicd11diagnosis",
name="root_category",
field=models.CharField(max_length=255, null=True),
),
]
7 changes: 7 additions & 0 deletions care/facility/models/meta_icd11_diagnosis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@


class MetaICD11Diagnosis(models.Model):
"""
Not for production use. For Metabase purposes only. Do not build relations to this model.
"""

id = models.CharField(max_length=255, primary_key=True)
_id = models.IntegerField()
average_depth = models.IntegerField()
Expand All @@ -11,6 +15,9 @@ class MetaICD11Diagnosis(models.Model):
is_leaf = models.BooleanField()
label = models.CharField(max_length=255)
breadth_value = models.DecimalField(max_digits=24, decimal_places=22)
chapter = models.CharField(max_length=255)
root_block = models.CharField(max_length=255, null=True)
root_category = models.CharField(max_length=255, null=True)

class Meta:
db_table = "meta_icd11_diagnosis"
Expand Down

0 comments on commit 0f4486a

Please sign in to comment.