oref0
oref0 copied to clipboard
halfBasalExerciseTarget and lowTT settings can lead to negative ISF and Basal
When we set halfBasalExerciseTarget to (for example) 110 and lowTempTarget of 80, the sensitivityRatio is negative halfBasal of 100 leads to sensitivity of 0 the code with the calculations is in determine-basal.js: https://github.com/openaps/oref0/blob/master/lib/determine-basal/determine-basal.js#L209
formula is (halfBasalTarget - 100) / (halfBasalTarget - 100 + target - 100) in the situation mentioned above we get (110 - 100) / (110 - 100 + 80 - 100) = 10 / (-10) = -1 that leads to negative basal and ISF and calculations are all wrong
setting halfBasal to 100 leads to ratio = 0.0 and thus when we calculate ISF, we divide the original ISF by the ratio, so divide by zero
this can happen only on low TT because the numerator is almost guaranteed to be above 0 and with the denominator of halfBasalTarget + target - 200 we get negative results only with low targets and low TT usually require low sensitivity, so we may set it as a max available value in preferences
I suggest we add the check after the sensitivityRatio = Math.min(sensitivityRatio, profile.autosens_max);
as follows:
if (sensitivityRatio <= 0.0) sensitivityRatio = profile.autosens_max;
To Reproduce Steps to reproduce the behavior:
- set halfBasalExerciseTarget to 110
- set low temptarget lowers sensitivity to true
- activate TT of 80
- see the negative ratio, ISF and Basal needed
the second issue:
- set halfBasalExerciseTarget to 100
- set low tt lowers sensitivity / high tt raises sensitivity
- activate low / high tt
- see the determine-basal fail
Expected behavior With low TT the ratio needs to be high
Smartphone (please complete the following information):
Setup Information (please complete the following information):
- All pump, CGMs and rigs are affected
- the issue exists in the current master branch
There are several possibilities to arrive at denominator of 0 in marked formula. eg: set halfBasalExerciseTarget to 120 set low tt lowers sensitivity activate low tt of 80 .. and so on, always symmetric around 100.
I confirm these bugs. My screenshots.
Negative InsulinReq, negative senstitivity ratio, negative ISF with target 81 and half_basal_exercise_target 108:
Deviding to zero with half_basal_exercise_target 100:
My preferences.json with half_exercise_basal_target: 100:
Following the comment of OpenAPS not allowing TTs lower than 80, updated the example numbers
I've confirmed the behavior described. The half_basal_exercise_target was never intended to be set that low, nor tested at such low values. I would prefer that we set a hard-coded minimum value for half_basal_exercise_target. Since the minimum temp target is hard-coded to 80 mg/dL, and the normalTarget is hard-coded to 100 mg/dL, a minimum value of >120 mg/dL for half_basal_exercise_target is required. If it's set lower than that, should we automatically bump it up to 130 or 125 mg/dL?
@scottleibrand I'm using halfBasal of 110 and TT of 144 to get sensitivity ratio down to ~20% if I were to use 130, I would have to set TT to 220 for the same effect which is too high
alternatively we can do
if (c * (c + target_bg-normalTarget) <= 0.0) sensitivityRatio = profile.autosens_max;
which will guarantee that we don't devide by zero or anything like that
Feel free to submit an alternative PR if you have specific code tweaks you think would work better.
I've added my PR as I'm using a low halfBasal to be able to get to even lower ratio than 50% without using a really high TT when workout
@scottleibrand, hello! I just tested that bug with negative ISF and denominator of 0.
result of halfBasalTarget 100 with normalTarget 100 in a browser console:
result of halfBasalTarget 110 with normalTarget 100 in a browser console:
We can see, that sensitivityRatio is 1.2.
My prefrences.json on openaps:
The result with target_bg 80, halfBasalTarget 100 on openaps log:
I can not see dividing by zero. Openaps continues to work. You can visit my repository: https://github.com/SeregaYakovlev/oref0/tree/dev
Is #1403 your preferred solution, or is there another change you'd rather make?