Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ketoacidosis protection #107

Closed
wants to merge 13 commits into from

Conversation

swissalpine
Copy link
Contributor

@swissalpine swissalpine commented Dec 9, 2020

This PR enables a ketoacedosis protection. There are two different strategies for this:

  1. A small basal rate when IOB is in negative range as if no basal insulin has been delivered for one hour (zero temp is possible and the small temporary basal rate is activated very rarely).
  2. Every Zero Temp is replaced with a small TBR.

Both concepts have been tested by two different people over the last two years We have not noticed any negative effects with regard to hypo avoidance.

@Tornado-Tim
Copy link
Member

Tornado-Tim commented Dec 9, 2020

This is a veryhacky approach which I am not in favor of. Has this been approached/submitted to the OpenAPS repo first?

Changes should be done in the core oref algorithm (and if they are, they should be submitted to the OpenAPS repo first) not the abstraction layer.

@ArthurusDent
Copy link

Have there been adverse events recorded that would make a change necessary?

@guydavies
Copy link

guydavies commented Dec 10, 2020 via email

@swissalpine
Copy link
Contributor Author

@ArthurusDent Thanks for your question. And yes, I wrote this for a person (a well known diabetic professional) who couldn't loop any other way because she has been shown to start a ketoacidotic episode after an hour of Zero Temp. She uses the 20% minimum strategy.

@guydavies I myself had big problems with exercise, which also led to hours of Zero Temp, which led to massive drops in performance for me after about 2,5 hours. I use the variable strategy, so Zero Temp is possible, but only up to a certain limit of negative IOB. And: With a f. e. 30% temporary basal rate which kicks in at f. e. -0,7 U IOB I never seen any influence of my bg - but the good feeling not to have ketoacidosis problems. And: It is disabled by default.

@Tornado-Tim Maybe you are right with your criticism and we must find another way to integrate this.
However, these strategies do not interfere with the algorithm, but change the result at a time when the algorithm has already done its work. And that, as written, only as an option, only for those who want it.
And wouldn't it be a pity if AndroidAPS only discusses layouts and adds pretty graphics (I am exaggerating). Why not send a good idea the other way, give something back from AndroidAPS.

BTW this is the first time that the concept behind was not convincing. Whenever I've explained it so far, it's been more of a "I want that too!", hence the PR. Interesting. Probably this is a problem of my english?

@guydavies
Copy link

guydavies commented Dec 10, 2020 via email

@jotomo
Copy link
Contributor

jotomo commented Dec 10, 2020

@swissalpine It does influence the algo in that the algo calculates with being able to withhold insulin, which is then overruled by AAPS. This might practically be negligible, but appending modifications that what OpenAPS has calculated isn't as simple as it seems. It's simple code-wise, but the overall complexity grows as OpenAPS now potentially works against what AAPS does and uses wrong assumptions.
Not sure where, but this was discussed with @scottleibrand , who didn't like the idea (in this form) on the grounds that the algo shouldn't deliver insulin when its intend is to reduce a declining BG.
When an extended negative IOB situation occurs, the user should therefore be triggered to consume carbs and add some insulin to remedy the situation.
From that perspective, this would be a 'carbs required' situation which could be handled similarly by showing an alarm and asking the user to consume carbs and bolus. That would have the chance of being integrated coherently with the core OpenAPS algo, I think.
(Rather than checking if iOB currently is negative more than a hour worth of basal, I'd consider checking if IOB was more than 60m in total below that threshold within the last 2h or so. Being negative for short periods shouldn't be a problem if it isn't during an high intensity exercise situation. Though I haven't fully though that through).
The tricky bit (as always) is to find a solution that extends beyond very specific use-cases, to be applicable (and configurable if needed) for most users.
Would adding a 'carbs (and bolus) required' alarm be a good solution, or would that trigger frequent alarms and thus alarm fatigue?
(I'm not opposed to the idea, if there is a real-world problem, it would be nice if it can be solved, but delivering insulin when it's the opposite of what the algo would do without notifying the is unnerving)

@ArthurusDent
Copy link

@swissalpine Ok, now I'm wondering to how many other people this applies to. I think it's important to know how much of an edge case this is. How did she determine that she was ketoacidotic? Is this just clinically detectable or does she get actual symptoms?

@scottleibrand
Copy link
Member

I agree that this is probably not the safest approach for most people. Usually, when someone is zero temped for an hour or two, it's after a meal when they have lots of residual insulin activity, their BG is dropping, and the algorithm is attempting to counteract that as aggressively as it can to avoid hypoglycemia. Any restriction on the duration of zero temping or the minimum temp basal rate would require a considerable number of other changes in maximum safe insulin dosing given the inability to zero-temp as aggressively to counteract any overshoot. I don't think most AndroidAPS users would benefit from such changes, as it'd result in more and longer highs.

I also agree that avoiding both ketoacidosis and hypoglycemia is something that can only reliably be done by alerting the user that carbs are required. If you wanted to expand the carbsReq feature to alert on scenarios where insulin activity (not IOB) is too negative and BG is still flat/dropping, that might be useful to a broader population.

@swissalpine
Copy link
Contributor Author

swissalpine commented Dec 10, 2020

Thank you for the controversial discussion of the approach. I very much appreciate your approach to this idea!
And it's a pity that unfortunately it's not so easy to try out, because maybe it would change or substantiate the view on the matter ...
Instead may I expand a bit to explain the approach, because it seems to me that its usefulness is not yet clear?

The problem with our loop algorithm is that it can only handle activity to a limited extent (and by activity I don't just mean sports). It works fantastically in bed as long as you don't move too much while doing it ..., and requires user intervention when activity goes beyond what is covered in the basal rate and factors. We help ourselves here with TT, with carbs or/and BR changes, some stop the loop.

But there are also the things that the Loop can actually do without intervention:
For example, when I am conducting an orchestra rehearsal, I am physically active (I wouldn't call it sports).To avoid hypoglycemia, the Loop runs Zero Temp to stabilize my blood sugar. Perfect! I love it!
But now my BG may be consistently at 100 mg/dl during the three hour trial, while my target is 110 mg/dl. And AAPS consistently induces Zero Temps over this long period of time, causing my IOB to gradually drop. If I have not eaten immediately before and no bolus is active, my IOB drops to -2.1 U during these three hours (I am assuming here myself and a basal rate of 0.7 U on average). Three hours without insulin, that does not have to, but can already lead to a beginning ketotic derailment with resistance etc.. And I don't think that this case is so rare.
The variable protection would now after a certain time, but only at a clearly negative IOB (for me normally at -0.7) e.g. deliver 20% insulin. This is an incredible feeling of security for me, a minimum of insulin arrives in the body, and the IOB still continues to fall!
And no, during this rehearsal I don't want an alarm and I don't want to have to eat because it is unproductive and unnecessary, distracting.
Instead of orchestra rehearsal, one could also speak of a seminar, of an exam, of shopping, of a hike or the like, just so as not to speak of competitive sports (which, by the way, I haven't done for at least three years).

With the variable strategy zero temps are also possible over a longer period of time (with a bolus of 6 IU theoretically in my case over 8-9 hours zero temps), the concern that after a meal the 20% kick in and prevent the insulin saving is unfounded, at least with the variable strategy this cannot happen.

And that this small basal amount has a massive effect with such a low IOB (as written with me of -0.7 U and lower), I have not noticed, because the IOB continues to fall and the BG should actually rise, which it just does not because of the activity. There is a little bit of insulin, which our body simply needs, nothing more ... In pre-loop times I reduced my basal rate, but never set it to zero during three hours.

By the way, it is interesting that in the last weeks not even this protection was activated: because I was with cold symptoms exclusively at home and worked at the desk, hardly any movement, no sports.Normally, I have the phases in which the TBR kicks in about once a week.
But this also shows that we are not talking about an everyday manipulation of the algorithm, but a single, rarely occurring special case, which can, however, be intercepted.

Based on my more than two years of self-experimentation (n=1), I simply cannot understand the concerns expressed and remain convinced that this means additional security (!) (for those who want it). There was no hypo associated with it.

I am also surprised by the risk discussion. In my eyes, the interventions that Automation allows in AAPS are much more aggressively conceivable (e.g. profil increase by 30% with a delta of +1) and possibly more dangerous in combination with SMB. Automation interferes with the algorithm far more massively than the possibility discussed here.

Perhaps the reservations are also directed primarily against the other strategy: The 20% always as keto protection are not my favored solution (although I also tried it on myself), but encounter a very special and certainly absolutely rare special case. But even this strategy did not cause any hypo problems, even if the 20% is called all the time, since every Zero Temp is replaced here ...

Thanks for reading.

@scottleibrand
Copy link
Member

You might be able to accomplish what you want by making the decision based on insulin activity instead of IOB. When you have a post-meal long zero temp, IOB can go very negative even as BGI from insulin activity is still positive or weakly negative. In your scenario of 3h of moderate activity without much insulin beforehand, BGI from insulin activity would go much more negative.

You could also have the action resulting from going below the BGI threshold be a low temp target instead of a full override of the dosing decision. That way, if you’re at risk of a hypo despite the very negative BGI, it can keep zero temping, but resume insulin delivery sooner when it’s safe to do so.

@AdrianLxM
Copy link
Contributor

Apart from all the said above, the code would also need some cleanup:

sp.getBoolean(R.string.keto_protect, false) && !sp.getBoolean("keto_protect_strategy", true)

  • Naming makes it hard to read (I want keto-protect but I don't want keto-protect?)
  • One setting is taken via a string literal, the other setting via a string resource.
  • The naming convention in AAPS for key string resources is key_…

@swissalpine
Copy link
Contributor Author

@AdrianLxM Thanks for your review which is reasonable. I have corrected it and hope I have not made a mistake.

@scottleibrand This "hack" is based on the total IOB (bolus and basal). Therefore, I do not quite understand why a "positive or weakly negative" BGI should be possible with a clearly negative total IOB. Or have I possibly translated the objection incorrectly?

To show that zero temping is possible, here is a typical day when, as usual, the protection does not start:
grafik

And here's a family hiking day from vacation (in August 2020) with some BG outliers; the arrow marks the 20% hypo-protection:
grafik

Perhaps this will show the safety and impact of this approach (by the way, this was the last day I know of that protection was active).

@scottleibrand
Copy link
Member

Insulin activity and BGI peak at about an hour after insulin is given or withheld, but IOB reflects that immediately. So IOB always goes negative before insulin activity goes negative and BGI goes positive, and vice versa. If someone’s BG is still being driven down by insulin activity, you wouldn’t want to prevent the loop from zero temping to counteract that. However, if their insulin activity is negative and BGI is positive (their BG should be rising sharply), you’re more likely to be in an insufficient-insulin situation (even if BG hasn’t started rising as fast as expected yet). In that situation it’s probably safe to allow some additional insulin to be given any time BG stops falling, which is easiest to implement with a low temp target.

@swissalpine
Copy link
Contributor Author

@scottleibrand Thanks for the explanations!
Would it be a possibility to combine IOB and BGI to be sure that blood glucose should trending up?

(bolusIob.iob + basalIob.basaliob) < (0 - baseBasalRate) && -(bolusIob.activity + basalIob.activity) > 0

A temporary target is not safe enough for me, since it is not possible to predict how much insulin will really be delivered. Because in this case, 20-30% insulin may simply be necessary to avoid ketoacidosis. I assume this value, based on a guess (but no complete certainty) of my diabetologist Dr. Kröger. There are probably no studies on the question, with how much insulin a ketoacidosis can be safely avoided.

@scottleibrand
Copy link
Member

Yes, combining both BGI and IOB seems like a good approach.

If BG is < 60 mg/dL and flat/dropping, it's not safe to give even 20-30% insulin until BG starts to rise: mitigating any risk of hypoglycemia is more urgent than preventing euglycemic DKA. A temp target approach ensures that insulin is only given when hypoglycemia risk is mitigated, but there are other ways to do so as well.

@Tornado-Tim
Copy link
Member

@swissalpine could you PR against oref repo, then update this PR if oref get updated with the changes?

@ulrikethurm
Copy link

Dear All,
First I want to thank all of you developers from my deep hart. You made the best present to so many people with typ 1 diabetes, who want to take care of their health. This can not be rewarded enough what you all did for our community!!! THANK YOU!
I am using the so called Ketoacidosis protection very sucessfullyfor more than 3 years. Without this function I personally would NOT be looping. When I started my DIY Loop in 2017 I experienced a ketoacidisis with a blood glucose of 140 mg/dl on a long hike in cold weather. I set the glucose target to 150 mg/dl and started with a glucose of 138 mg/dl. So without the ketoacidosis protection the loop put me to zero insulin. We hiked about 4 hours, the glucose staid at that level and no insulin was given. I started to feel not good, had cramps, startd to vomit and had strong blood ketones showing a ketoacidosis still with a good glucose level below 150 mg/dl. I react very sensitive towards insulin, I am a marathon runner myself, working as a diabetes specialist nurse for more than 30 years and I am on an insulin pump since 1986! I know, that I need a little insulin so my cells are able to get the glucose into the cells etc. and without this ketoacidosis protection I do not feel safe with my DIY Loop. I know, this might not be usefull for everybody, but for those, who need this function, it could be life saving. Therfor I would be very happy, if you at least could think about this as an option for those, being very sensitive for insulin and running a risk of ketoacidosis very easily. It must not be added to every loop, but DIY looping stands for the option, to individualize the loop to the individual needs. This protection is a need for me personally.
All the best and thank you so much for all the work you did, merry christmas and stay healthy, take care, Ulrike

@scottleibrand
Copy link
Member

@ulrikethurm when doing an identical hike with a minimum level of insulin, what happens to your BG? I would expect it to drop considerably, rather than stay in range. In that case, you would've had to eat carbs to bring blood sugar back up. Have you tried eating (and entering) the carbs first, rather than giving the insulin first and risking a low? Would it be useful to receive a carbs required notification when you've been receiving too little insulin, so you can eat and enter the carbs and get insulin dosing started?

@ulrikethurm
Copy link

ulrikethurm commented Dec 21, 2020 via email

@scottleibrand
Copy link
Member

Thanks, that is helpful context.

It sounds like someone should open a PR against oref0 that adds a few off-by-default (probably initially hidden) preferences that allows people to set a minimum IOB and insulin activity threshold, and a corresponding minimum % basal rate, if they want to take over more personal responsibility for avoiding lows in exchange for less worry about avoiding ketosis. We should also include reasonable maximum allowable values for all three thresholds.

Once that is code reviewed and merged into oref0 for testing, someone can do the same on AndroidAPS.

@twain47
Copy link

twain47 commented Dec 23, 2020

I am more concerned about getting ketoacidosis, due to my extrem insulin sensitivity I once got a ketoacidosis by just putting
of the pump for going into a sauna for only 60 minutes. O.k., due to the heat the insulin resorption is faster, but it was just
these 60 minutes without insulin, which got me into ketocis, symptomatic and detected with blood ketone measuring.

@ulrikethurm If you don't mind me asking...

When you start experiencing ketoacidosis symptoms what sort of level of blood ketones are you seeing?

@ulrikethurm
Copy link

ulrikethurm commented Dec 28, 2020 via email

@swissalpine
Copy link
Contributor Author

@Tornado-Tim @scottleibrand
Thank you so much for pointing me to the openAPS repo and asking me to create a PR there. I would like to do that!
However, as a non-IT person trying to learn Java by reading the AndroidAPS code, it is a big challenge for me to do this in a repo that is foreign to me, especially without being able to test my suggestions. I will look into it, but I don't know if I can do it.
Also, the "detour" still doesn't necessarily make sense to me: AndroidAPS is based to a large extent on openAPS, that's true, but it also goes beyond that in some of its functionality and lags behind in others. Why can't this feature be integrated into AAPS first?
But as I said, I will try ...

One more note on Ulrike's comments and Scott's response:
Ulrike uses the strategy that always delivers 20% insulin instead of 0%. I have also tried this for weeks without any problems, but since I don't need it, I have developed for myself the variable strategy. However, this 20% always is not included in Scott's suggestion:
It sounds like someone should open a PR against oref0 that adds a few off-by-default (probably initially hidden) preferences that allows people to set a minimum IOB and insulin activity threshold, and a corresponding minimum % basal rate.
Does "Ulrike's" strategy have any chance of being included as an alternative strategy?

@scottleibrand
Copy link
Member

The vast majority of people on any sort of APS (probably >99%) experience occasional hypoglycemia but never euglycemic DKA. The hard 20% minimum is more dangerous for them, so I don't want to include it if we can accomplish the same goal by ensuring minimum insulin delivery only when insulin activity and IOB are already low/negative.

I can help with porting to oref0 if someone wants to hit me up on Slack and actively work on it with me.

@swissalpine
Copy link
Contributor Author

@scottleibrand I'm very busy the next two weeks but I will have a lool at openAPS repo and meet you on slack if I have an idea what to do ... Thanks for your support!

@AdrianLxM
Copy link
Contributor

I think the consensus is, that this topic needs further discussion.
I'll convert this to a draft-pr for the time being.

@AdrianLxM AdrianLxM marked this pull request as draft January 22, 2021 21:43
@MilosKozak MilosKozak closed this Oct 4, 2021
@swissalpine swissalpine deleted the ketoacidosis-protection branch March 8, 2022 05:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants