Skip to content

Commit

Permalink
conversion list add default units
Browse files Browse the repository at this point in the history
  • Loading branch information
marceloarocha committed Jan 22, 2025
1 parent 9ed065b commit 2513441
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 57 deletions.
7 changes: 7 additions & 0 deletions models/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,10 @@ class RegulationAction(Enum):
class SegmentTypeEnum(Enum):
ADULT = 1
PEDIATRIC = 2


class DefaultMeasureUnitEnum(Enum):
MG = "mg"
ML = "ml"
MCG = "mcg"
UI = "UI"
3 changes: 1 addition & 2 deletions routes/admin/admin_unit_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ def get_unit_conversion_list():
request_data = request.get_json()

return admin_unit_conversion_service.get_conversion_list(
id_segment=request_data.get("idSegment"),
show_prediction=request_data.get("showPrediction", False),
id_segment=request_data.get("idSegment")
)


Expand Down
33 changes: 0 additions & 33 deletions services/admin/admin_ai_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,39 +86,6 @@ def get_substance_by_drug_name(drug_names: list[str]):
return drugs_dict


def get_factors(conversions):
if len(conversions) == 0:
return conversions

model_factor = _get_model("models/noharm-ml-unit.gz")
token_med = _get_model("models/noharm-tkm-unit.gz")
token_unit = _get_model("models/noharm-tku-unit.gz")

for c in conversions:
if c["factor"] == None:
name = re.sub(r"\w{6,}", "", c["name"]).lower()
measure_unit = c["idMeasureUnit"].lower()

vector_med = token_med.transform([name])
vector_unit = token_unit.transform([measure_unit])
vector_factor = numpy.concatenate(
(vector_med.toarray(), vector_unit.toarray()), axis=1
)

prediction = model_factor.predict(vector_factor)[0]
factor_idx = numpy.where(model_factor.classes_ == prediction)[0][0]
factor_prob = model_factor.predict_proba(vector_factor).round(2)[0][
factor_idx
]

c["prediction"] = prediction
c["accuracy"] = factor_prob
c["predictionName"] = name
c["predictionUnit"] = measure_unit

return conversions


def _get_client():
return boto3.client("s3")

Expand Down
2 changes: 1 addition & 1 deletion services/admin/admin_integration_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def _set_new_config(old_config: dict, new_config: dict):
config["getname"]["secret"] = new_config["getname"]["secret"]
elif "proxy" == config["getname"]["type"]:
config["getname"]["url"] = new_config["getname"]["url"]
config["getname"]["urlDev"] = new_config["getname"]["urlDev"]
config["getname"]["urlDev"] = new_config["getname"].get("urlDev", None)
config["getname"]["params"] = new_config["getname"]["params"]
config["getname"]["token"] = {
"url": new_config["getname"]["token"]["url"],
Expand Down
77 changes: 56 additions & 21 deletions services/admin/admin_unit_conversion_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
Substance,
)
from models.segment import Segment
from models.enums import IntegrationStatusEnum, SegmentTypeEnum
from models.enums import SegmentTypeEnum, DefaultMeasureUnitEnum
from services import drug_service as main_drug_service
from services.admin import (
admin_ai_service,
admin_drug_service,
admin_integration_status_service,
)
from decorators.has_permission_decorator import has_permission, Permission
from exception.validation_error import ValidationError
Expand All @@ -27,7 +25,36 @@


@has_permission(Permission.ADMIN_UNIT_CONVERSION)
def get_conversion_list(id_segment, show_prediction=False):
def get_conversion_list(id_segment):
nh_default_units = (
db.session.query(MeasureUnit)
.filter(
MeasureUnit.measureunit_nh.in_(
[
DefaultMeasureUnitEnum.MCG.value,
DefaultMeasureUnitEnum.MG.value,
DefaultMeasureUnitEnum.ML.value,
DefaultMeasureUnitEnum.UI.value,
]
)
)
.all()
)

if not nh_default_units:
raise ValidationError(
"Pendência de configuração das unidades de medida",
"errors.invalidParams",
status.HTTP_400_BAD_REQUEST,
)

default_units = {}
for du in nh_default_units:
default_units[du.measureunit_nh] = {
"idMeasureUnit": du.id,
"description": du.description,
}

active_drugs = db.session.query(distinct(Outlier.idDrug).label("idDrug")).cte(
"active_drugs"
)
Expand Down Expand Up @@ -62,21 +89,7 @@ def get_conversion_list(id_segment, show_prediction=False):
.group_by(DrugAttributes.idDrug, DrugAttributes.idMeasureUnitPrice)
)

substance_units = (
db.session.query(
DrugAttributes.idDrug.label("idDrug"),
func.min(MeasureUnit.id).label("idMeasureUnit"),
)
.join(Drug, Drug.id == DrugAttributes.idDrug)
.join(Substance, Substance.id == Drug.sctid)
.join(MeasureUnit, Substance.default_measureunit == MeasureUnit.measureunit_nh)
.filter(Substance.default_measureunit != None)
.group_by(DrugAttributes.idDrug)
)

units = prescribed_units.union(price_units, current_units, substance_units).cte(
"units"
)
units = prescribed_units.union(price_units, current_units).cte("units")

conversion_list = (
db.session.query(
Expand All @@ -87,6 +100,8 @@ def get_conversion_list(id_segment, show_prediction=False):
MeasureUnitConvert.factor,
MeasureUnit.description,
Drug.sctid,
Substance.default_measureunit,
MeasureUnit.measureunit_nh,
)
.join(active_drugs, Drug.id == active_drugs.c.idDrug)
.join(units, Drug.id == units.c.idDrug)
Expand All @@ -105,7 +120,11 @@ def get_conversion_list(id_segment, show_prediction=False):
)

result = []
drug_defaultunit = set()
for i in conversion_list:
if i.default_measureunit == i.measureunit_nh:
drug_defaultunit.add(i.id)

result.append(
{
"idDrug": i[1],
Expand All @@ -118,8 +137,24 @@ def get_conversion_list(id_segment, show_prediction=False):
}
)

if show_prediction:
return admin_ai_service.get_factors(result)
for i in conversion_list:
if i.id not in drug_defaultunit and i.default_measureunit:
drug_defaultunit.add(i.id)

d_unit = default_units.get(i.default_measureunit, None)

if d_unit:
result.append(
{
"idDrug": i.id,
"name": i.name,
"idMeasureUnit": d_unit.get("idMeasureUnit"),
"factor": None,
"idSegment": escape_html(id_segment),
"measureUnit": d_unit.get("description"),
"sctid": i.sctid,
}
)

return result

Expand Down

0 comments on commit 2513441

Please sign in to comment.