Skip to content

Commit

Permalink
#330: add full heart rate support as continuous variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolkenfarmer committed Sep 16, 2024
1 parent 1ec1e72 commit 267a9a2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
from template.models.continuous_variable import ContinuousVariable


def _extract_spo2(breathing_text):
"""Extracts the SpO2 value from a 'breathing' vital signs text."""
return re.search(r"SpO2:\s*(\d+)", breathing_text).group(1)
def _extract_spo2(vital_signs):
"""Extracts the SpO2 value from a vital signs text."""
return int(re.search(r"SpO2:\s*(\d+)", vital_signs["Breathing"]).group(1))


def _extract_bpm(vital_signs):
"""Extracts the heart rate value from a vital signs text."""
return int(re.search(r"Herzfreq:\s*(\d+)", vital_signs["Circulation"]).group(1))


def _check_subset(condition_items, completed_items):
Expand Down Expand Up @@ -65,28 +70,40 @@ def continuous_variables(self):
if not future_state:
return []

spo2_current = _extract_spo2(
self.patient_instance.patient_state.vital_signs["Breathing"]
)
spo2_target = _extract_spo2(future_state.vital_signs["Breathing"])

variables = ContinuousVariable.objects.all()

result = []
for variable in variables:
current, target = self._get_values(variable.name, future_state)
function, var_hash = self._get_applicable_function(variable)
var_hash = hash((var_hash, spo2_current, spo2_target))
var_hash = hash((var_hash, current, target))

result.append(
{
"name": variable.name,
"current": 100, # Placeholder value, adjust as needed
"target": 0, # Placeholder value, adjust as needed
"current": current,
"target": target,
"function": function,
"hash": var_hash,
}
)
return result

def _get_values(self, variable_name, future_state):
match variable_name:
case ContinuousVariable.Variable.SPO2:
fun = _extract_spo2
case ContinuousVariable.Variable.HEART_RATE:
fun = _extract_bpm
case _:
raise TypeError(
f"Did not find values for given continuous variable {variable_name}."
)

return fun(self.patient_instance.patient_state.vital_signs), fun(
future_state.vital_signs
)

def _get_applicable_function(self, variable):
completed_action_uuids = {
str(action.uuid)
Expand Down
10 changes: 8 additions & 2 deletions frontend/src/components/widgets/PatientStatus.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
import {useContinuousVariablesStore} from "@/stores/ContinuousVariables"
import {computed} from "vue"
import {strings} from "@/strings"
import {ContinuousVariableName} from "@/enums"
const patientStore = usePatientStore()
const continuousStateStore = useContinuousVariablesStore()
const breathing = computed(() => {
const spo2Val = continuousStateStore.getCurrentValueByName('SpO2')?.toFixed(0)
const spo2Val = continuousStateStore.getCurrentValueByName(ContinuousVariableName.SPO2)?.toFixed(0)
return patientStore.breathing.replace(/(SpO2: )\d+/, `$1${spo2Val}`)
})
const circulation = computed(() => {
const heartRateVal = continuousStateStore.getCurrentValueByName(ContinuousVariableName.HEART_RATE)?.toFixed(0)
return patientStore.circulation.replace(/(Herzfreq: )\d+/, `$1${heartRateVal}`)
})
</script>

<template>
Expand All @@ -38,7 +44,7 @@
<td>
<p class="key">
{{ strings.patientState.circulation }}
</p>{{ patientStore.circulation }}
</p>{{ circulation }}
</td>
<td>
<p class="key">
Expand Down
7 changes: 2 additions & 5 deletions frontend/src/stores/ContinuousVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export const useContinuousVariablesStore = defineStore('patientContinuous', {
continuousState.continuousVariables.forEach(variable => {
const existingVariable = this.continuousVariables.find(v => v.name === variable.name)
if (existingVariable) {
existingVariable.xCurrent = variable.current // ToDo: remove
existingVariable.xStart = existingVariable.xCurrent
existingVariable.xTarget = variable.target
existingVariable.tDelta = this.timeUntilPhaseChange
Expand Down Expand Up @@ -67,11 +66,9 @@ export const useContinuousVariablesStore = defineStore('patientContinuous', {
case ContinuousFunctionName.LINEAR:
return variable.xCurrent + linear(variable, this.timeUntilPhaseChange)
case ContinuousFunctionName.SIGMOID:
// console.log("Calculate sigmoid: " + (variable.xCurrent + sigmoid(variable, this.timeUntilPhaseChange)) + "; Time until phase" +
// " change: " + this.timeUntilPhaseChange)
return variable.xCurrent + sigmoid(variable, this.timeUntilPhaseChange)
case ContinuousFunctionName.SIGMOID_DELAYED:
return variable.xCurrent + sigmoid_delayed(variable, this.timeUntilPhaseChange)
return variable.xCurrent + sigmoidDelayed(variable, this.timeUntilPhaseChange)
case ContinuousFunctionName.INCREMENT:
return variable.xCurrent + 1
case ContinuousFunctionName.DECREMENT:
Expand Down Expand Up @@ -136,7 +133,7 @@ function sigmoid(variable: ContinuousVariableInternal, timeUntilPhaseChange: num
return atanDerivative * xStretcher / tStretchCorrector
}

function sigmoid_delayed(variable: ContinuousVariableInternal, timeUntilPhaseChange: number): number {
function sigmoidDelayed(variable: ContinuousVariableInternal, timeUntilPhaseChange: number): number {
// higher value = steeper; higher -> lower infinTargetCorrection
const steepness = 10
// magic value (try & error for steepness 10/20) needed for correction as the target height is only reached -> infin
Expand Down

0 comments on commit 267a9a2

Please sign in to comment.