From 67910d50258b8546e871397760c624e496cfa016 Mon Sep 17 00:00:00 2001 From: Wolkenfarmer Date: Mon, 16 Sep 2024 22:45:32 +0200 Subject: [PATCH] #330 frontend: improved sigmoid and delayed sigmoid functions to be more precise --- .../data/continuous_variables_data.py | 2 +- frontend/src/stores/ContinuousVariables.ts | 28 +++++-------------- 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/backend/dps_training_k/data/continuous_variables_data.py b/backend/dps_training_k/data/continuous_variables_data.py index 7449744b..9648699e 100644 --- a/backend/dps_training_k/data/continuous_variables_data.py +++ b/backend/dps_training_k/data/continuous_variables_data.py @@ -54,7 +54,7 @@ def update_or_create_continuous_variables(): uuid=ContinuousVariableIDs.HEART_RATE, defaults={ "name": ContinuousVariable.Variable.HEART_RATE, - "function": ContinuousVariable.Function.LINEAR, + "function": ContinuousVariable.Function.SIGMOID_DELAYED, "exceptions": [], }, ) diff --git a/frontend/src/stores/ContinuousVariables.ts b/frontend/src/stores/ContinuousVariables.ts index 2a3797c6..bbc4e463 100644 --- a/frontend/src/stores/ContinuousVariables.ts +++ b/frontend/src/stores/ContinuousVariables.ts @@ -111,50 +111,36 @@ function linear(variable: ContinuousVariableInternal, timeUntilPhaseChange: numb } function sigmoid(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 - const infinTargetCorrection = 3.5 + const steepness = 8 // = the horizontal range that is taken from the original atan function (higher values = steeper) const t = variable.tDelta - timeUntilPhaseChange const tMid = variable.tDelta / 2 - const tStretchFactor = 1 / ((Math.pow(variable.tDelta, 2)) / Math.pow(steepness, 2)) - const tStretchCorrectorNormalizer = Math.atan(Math.sqrt(1) * variable.tDelta) / Math.sqrt(1) - const tStretchCorrector = (Math.atan(Math.sqrt(tStretchFactor) * variable.tDelta) / Math.sqrt(tStretchFactor)) / tStretchCorrectorNormalizer + const tStretchCorrector = variable.tDelta / steepness - // original height of atanDerivative fun, - infinTargetCorrection scaled with t_delta - const pi = Math.PI - (infinTargetCorrection / variable.tDelta) const xDelta = variable.xTarget - variable.xStart - const xStretcher = xDelta / pi + const xStretcher = xDelta / (2 * Math.atan(steepness / 2)) const atanDerivative = (1 / (Math.pow(t - tMid, 2) * tStretchFactor + 1)) - return atanDerivative * xStretcher / tStretchCorrector + return (atanDerivative * xStretcher / tStretchCorrector) } 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 - const infinTargetCorrection = 3.5 + const steepness = 8 // = the horizontal range that is taken from the original atan function (higher values = steeper) const t = variable.tDelta - timeUntilPhaseChange const tMid = variable.tDelta / 2 - const tShiftFrac = 3 const t0Shifted = variable.tDelta / tShiftFrac // Time when sigmoid starts (shifted by one tShiftFrac) const tDeltaShifted = variable.tDelta - t0Shifted const tShiftedMid = tMid + (variable.tDelta - tMid) / tShiftFrac // Shifted midpoint const tStretchFactor = 1 / ((Math.pow(tDeltaShifted, 2)) / Math.pow(steepness, 2)) - const tStretchCorrectorNormalizer = Math.atan(Math.sqrt(1) * variable.tDelta) / Math.sqrt(1) - const tStretchCorrector = (Math.atan(Math.sqrt(tStretchFactor) * variable.tDelta) / Math.sqrt(tStretchFactor)) / tStretchCorrectorNormalizer + const tStretchCorrector = tDeltaShifted / steepness - // original height of atanDerivative fun, - infinTargetCorrection scaled with t_delta - const pi = Math.PI - (infinTargetCorrection / tDeltaShifted) const xDelta = variable.xTarget - variable.xStart - const xStretcher = xDelta / pi + const xStretcher = xDelta / (2 * Math.atan(steepness / 2)) const atanDerivative = (1 / (Math.pow(t - tShiftedMid, 2) * tStretchFactor + 1))