Skip to content

Commit

Permalink
conciliation substance inference
Browse files Browse the repository at this point in the history
  • Loading branch information
marceloarocha committed Nov 27, 2024
1 parent cc2277c commit fad03b7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
32 changes: 32 additions & 0 deletions services/admin/admin_ai_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import tempfile
import numpy
import re
import gc
from typing import List

from config import Config
Expand Down Expand Up @@ -48,6 +49,37 @@ def get_substance(drugs: List[Drug]):
return drugs_array


def get_substance_by_drug_name(drug_names: list[str]):
drugs_dict = {}
drug_names_words = []

if not drug_names:
return drugs_dict

for n in drug_names:
drug_names_words.append(" ".join(re.findall("\w{4,}", n)))

model_subst = _get_model("models/noharm-ml-subst.gz")
token_subst = _get_model("models/noharm-tk-subst.gz")

vector = token_subst.transform(drug_names_words)
prediction = model_subst.predict(vector)
probabilities = model_subst.predict_proba(vector).round(2)
MIN_PROB = 0.5

for idx, p in enumerate(prediction):
subst_idx = numpy.where(model_subst.classes_ == p)[0][0]
subst_prob = probabilities[idx][subst_idx]

if subst_prob >= MIN_PROB:
drugs_dict[drug_names[idx]] = p

del model_subst, token_subst, vector, prediction, probabilities
gc.collect()

return drugs_dict


def get_factors(conversions):
if len(conversions) == 0:
return conversions
Expand Down
5 changes: 5 additions & 0 deletions services/prescription_view_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def _internal_get_prescription(
alerts_data=alerts_data,
exams_data=exam_data,
config_data=config_data,
is_complete=is_complete,
)

review_data = _get_review_data(prescription=prescription, is_complete=is_complete)
Expand Down Expand Up @@ -522,6 +523,7 @@ def _get_drug_data(
alerts_data: dict,
exams_data: dict,
config_data: dict,
is_complete: bool,
):
drug_list = DrugList(
drugList=drugs,
Expand Down Expand Up @@ -578,6 +580,9 @@ def _get_drug_data(
)
concilia_list = drug_list.conciliaList(concilia_drugs, [])

if is_complete and concilia_list:
p_drugs = drug_list.infer_substance(pDrugs=p_drugs)

return {
"drug_list": drug_list,
"headers": headers,
Expand Down
23 changes: 23 additions & 0 deletions utils/drug_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from utils.dateutils import to_iso
from services import drug_service
from services.admin import admin_ai_service
from utils import stringutils, numberutils, prescriptionutils


Expand Down Expand Up @@ -398,6 +399,27 @@ def changeDrugName(pDrugs):

return result

@staticmethod
def infer_substance(pDrugs):
names = []
for p in pDrugs:
if p["idDrug"] == 0:
names.append(p["drug"])

substances = admin_ai_service.get_substance_by_drug_name(drug_names=names)

result = []
for p in pDrugs:
if p["idDrug"] == 0:
if p["drug"] in substances:
p["sctid_infer"] = substances[p["drug"]]

result.append(p)
else:
result.append(p)

return result

@staticmethod
def conciliaList(pDrugs, result=[]):
for pd in pDrugs:
Expand Down Expand Up @@ -434,6 +456,7 @@ def conciliaList(pDrugs, result=[]):
),
"time": prescriptionutils.timeValue(pd[0].interval),
"recommendation": pd[0].notes,
"sctid": str(pd.Substance.id) if pd.Substance else None,
}
)

Expand Down

0 comments on commit fad03b7

Please sign in to comment.