From 0564c326b9a533349469211fdd22a6883be9a969 Mon Sep 17 00:00:00 2001 From: Wolkenfarmer Date: Sat, 14 Sep 2024 12:47:38 +0200 Subject: [PATCH] #330 backend: add continuousVariable model and data --- .../data/continuous_variables_data.py | 55 +++++++++++++++++++ backend/dps_training_k/template/admin.py | 1 + backend/dps_training_k/template/constants.py | 5 ++ .../commands/import_patient_information.py | 12 +++- .../migrations/0011_continuousvariable.py | 27 +++++++++ .../template/models/__init__.py | 5 +- .../template/models/continuous_variable.py | 21 +++++++ 7 files changed, 121 insertions(+), 5 deletions(-) create mode 100644 backend/dps_training_k/data/continuous_variables_data.py create mode 100644 backend/dps_training_k/template/migrations/0011_continuousvariable.py create mode 100644 backend/dps_training_k/template/models/continuous_variable.py diff --git a/backend/dps_training_k/data/continuous_variables_data.py b/backend/dps_training_k/data/continuous_variables_data.py new file mode 100644 index 00000000..121ac893 --- /dev/null +++ b/backend/dps_training_k/data/continuous_variables_data.py @@ -0,0 +1,55 @@ +from template.constants import ActionIDs, MaterialIDs, ContinuousVariableIDs +from template.models.continuous_variable import ContinuousVariable + + +def update_or_create_continuous_variables(): + """ + "name": ContinuousVariable.Variable.SPO2, // name as defined in ContinuousVariable + "function": ContinuousVariable.Function.LINEAR, // function as defined in ContinuousVariable + "exceptions": [{ + "actions": ["A1uuid", ["A2uuid", "A3uuid"]], // this means action1 OR (action3 AND action4); can be None + "materials": ["M1uuid", ["M2uuid", "M3uuid"]], // material1 OR material2 OR (material3 AND material4); can be None + "function": ContinuousVariable.Function.LINEAR, // function as defined in ContinuousVariable + }] // exceptions listed according to their priority. If the first exceptions applies, the rest won't be looked at + // actions and materials both need to be true in order for the exception to apply + """ + + ContinuousVariable.objects.update_or_create( + uuid=ContinuousVariableIDs.SPO2, + defaults={ + "name": ContinuousVariable.Variable.SPO2, + "function": ContinuousVariable.Function.LINEAR, + "exceptions": [ + { + "actions": [str(ActionIDs.BEATMUNGSGERAET_ANBRINGEN)], + "materials": [ + str(MaterialIDs.BEATMUNGSGERAET_STATIONAER), + str(MaterialIDs.BEATMUNGSGERAET_TRAGBAR), + ], + "function": ContinuousVariable.Function.LINEAR, + }, + { + "actions": [ + [str(ActionIDs.IV_ZUGANG), str(ActionIDs.DRUCKVERBAND)] + ], + "materials": [ + [str(MaterialIDs.EKG), str(MaterialIDs.BZ_MESSGERAET)] + ], + "function": ContinuousVariable.Function.DECREMENT, + }, + { + "actions": [str(ActionIDs.IV_ZUGANG)], + "materials": [str(MaterialIDs.EKG)], + "function": ContinuousVariable.Function.INCREMENT, + }, + ], + }, + ) + ContinuousVariable.objects.update_or_create( + uuid=ContinuousVariableIDs.HEART_RATE, + defaults={ + "name": ContinuousVariable.Variable.HEART_RATE, + "function": ContinuousVariable.Function.LINEAR, + "exceptions": [], + }, + ) diff --git a/backend/dps_training_k/template/admin.py b/backend/dps_training_k/template/admin.py index efb33f89..f3c1a264 100644 --- a/backend/dps_training_k/template/admin.py +++ b/backend/dps_training_k/template/admin.py @@ -8,3 +8,4 @@ admin.site.register(StateTransition) admin.site.register(PatientInformation) admin.site.register(Material) +admin.site.register(ContinuousVariable) diff --git a/backend/dps_training_k/template/constants.py b/backend/dps_training_k/template/constants.py index cdddf46c..6bceb53e 100644 --- a/backend/dps_training_k/template/constants.py +++ b/backend/dps_training_k/template/constants.py @@ -147,3 +147,8 @@ class ActionResultIDs: HB430 = "4a76bb8d-ccd2-4f27-9e08-3131ed3c0178" BZ920 = "3ddade44-701a-43b9-9670-a8e346a45048" BZ930 = "c30d670e-c072-4263-9f6a-895d00ef7368" + + +class ContinuousVariableIDs: + SPO2 = "cb39ff83-6f6a-4dba-9ae1-b2028dbddb35" + HEART_RATE = "862cca82-555e-4f05-bd38-9a6dc9f01f60" diff --git a/backend/dps_training_k/template/management/commands/import_patient_information.py b/backend/dps_training_k/template/management/commands/import_patient_information.py index d53a829c..cb50a264 100644 --- a/backend/dps_training_k/template/management/commands/import_patient_information.py +++ b/backend/dps_training_k/template/management/commands/import_patient_information.py @@ -3,6 +3,7 @@ from django.core.management.base import BaseCommand +from data.continuous_variables_data import update_or_create_continuous_variables from helpers.triage import Triage from template.constants import ActionIDs from template.models import PatientInformation @@ -75,7 +76,7 @@ def import_patients(file_path): "start_status": row["Start-Status"].strip(), "start_location": row["Start-Ort"].strip(), "op": row["OP / Interventions-Verlauf"].strip(), - } + }, ) pretreatments_list = [ pt.strip() for pt in patient_information.pretreatment.split(",") @@ -102,8 +103,13 @@ def import_patients(file_path): class Command(BaseCommand): - help = "Imports patient data from a CSV file" + help = "Imports patient data from a CSV file and update continuous variables data" def handle(self, *args, **options): import_patients("./data/patient_information.csv") # path to the csv file - self.stdout.write(self.style.SUCCESS("Successfully imported patient data")) + update_or_create_continuous_variables() + self.stdout.write( + self.style.SUCCESS( + "Successfully imported patient data and updated continuous variables data" + ) + ) diff --git a/backend/dps_training_k/template/migrations/0011_continuousvariable.py b/backend/dps_training_k/template/migrations/0011_continuousvariable.py new file mode 100644 index 00000000..dfa6342d --- /dev/null +++ b/backend/dps_training_k/template/migrations/0011_continuousvariable.py @@ -0,0 +1,27 @@ +# Generated by Django 5.0.1 on 2024-09-12 16:54 + +import uuid +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('template', '0010_merge_20240710_1548'), + ] + + operations = [ + migrations.CreateModel( + name='ContinuousVariable', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('uuid', models.UUIDField(default=uuid.uuid4, editable=False, unique=True)), + ('name', models.CharField(choices=[('SpO2', 'Spo2'), ('BPM', 'Heart Rate')], unique=True)), + ('function', models.CharField(choices=[('linear', 'Linear'), ('increment', 'Increment'), ('decrement', 'Decrement')])), + ('exceptions', models.JSONField()), + ], + options={ + 'abstract': False, + }, + ), + ] diff --git a/backend/dps_training_k/template/models/__init__.py b/backend/dps_training_k/template/models/__init__.py index 2675c5bc..b030ccef 100644 --- a/backend/dps_training_k/template/models/__init__.py +++ b/backend/dps_training_k/template/models/__init__.py @@ -1,7 +1,8 @@ from .action import Action +from .continuous_variable import ContinuousVariable +from .logic_node import LogicNode +from .material import Material from .patient_information import PatientInformation from .patient_state import PatientState from .state_transition import StateTransition from .subcondition import Subcondition -from .logic_node import LogicNode -from .material import Material diff --git a/backend/dps_training_k/template/models/continuous_variable.py b/backend/dps_training_k/template/models/continuous_variable.py new file mode 100644 index 00000000..57f12245 --- /dev/null +++ b/backend/dps_training_k/template/models/continuous_variable.py @@ -0,0 +1,21 @@ +from django.db import models + +from helpers.models import UUIDable + + +class ContinuousVariable(UUIDable, models.Model): + class Variable(models.TextChoices): + SPO2 = "SpO2" + HEART_RATE = "BPM" + + class Function(models.TextChoices): + LINEAR = "linear" + INCREMENT = "increment" + DECREMENT = "decrement" + + name = models.CharField(choices=Variable.choices, unique=True) + function = models.CharField(choices=Function.choices) + exceptions = models.JSONField() + + def __str__(self): + return f"Continuous variable called {self.name}"