-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Integral retrospective correction #726
Conversation
@ps2 I've tried to address your comments in the latest update, which includes the following
In the latest update, instead of setting |
If both sliders on, would that functionally be same as having IRC on? I am a little confused how the toggle isn't more of a "If you want an RC feature enabled, choose between RC and IRC" |
@Kdisimone The switches are meant to convey the notion that IRC is an extension of RC, not an alternative to RC. It is possible to enable RC but not IRC, but it is not possible to enable IRC unless RC is enabled. That logic is wired into the two switches: if RC is disabled, this automatically disables both RC and IRC. If IRC is enabled, this automatically enables both RC and IRC. |
Thanks @dm61 that helps! I'll keep that in mind for docs and fb questions when that comes. |
@@ -112,7 +112,8 @@ extension UserDefaults { | |||
maximumBasalRatePerHour: maximumBasalRatePerHour, | |||
maximumBolus: maximumBolus, | |||
suspendThreshold: suspendThreshold, | |||
retrospectiveCorrectionEnabled: bool(forKey: "com.loudnate.Loop.RetrospectiveCorrectionEnabled") | |||
retrospectiveCorrectionEnabled: bool(forKey: "com.loudnate.Loop.RetrospectiveCorrectionEnabled"), | |||
integralRetrospectiveCorrectionEnabled: bool(forKey: "com.loopkit.Loop.IntegralRetrospectiveCorrectionEnabled") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it matter that one setting is "com.loopkit" and the other is "com.loudnate?"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, any unique key would do. The keys are following Nate and Pete's naming convention.
FWIW I'm running this patch now as well, sitting on top of 1.5.7dev. |
@francescaneri Pete has added necessary resources to LoopKit and has provided everything needed to update and improve the IRC implementation, which is what I am working on now. |
@dm61 thanks |
fix minor errors
5888513
to
34c390a
Compare
Integral retrospective branch has been completely reworked. All credits and thanks go to @ps2 who developed LoopKit resources that enable more robust, safer and accurate implementation of IRC algorithm:
Taking advantage of the new implementation approach, I have also made some tweaks in the IRC algorithm to reduce likelihood of glucose oscillations that may occur if ISF value is on a low side, and to reduce lag time in response to increases in bg after carb treatments of lows. These are relatively small improvements, please do not expect that these issues have been completely resolved. If ISF value is too low, you will likely observe some glucose oscillations with or without RC or IRC. I've been testing the new IRC for about a week before making it available for testing by others. But, since this is a major rework of IRC code, some careful testing and feedback would be very welcome and much appreciated. |
@dm61 thanks for the work! After some family stuff early this week, I'd be happy to test! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IRC algorithm state should be moved into its own class, and should have unit tests written against it. Also, IRC algorithm factors should be included in an issue report.
Loop/Managers/AnalyticsManager.swift
Outdated
@@ -118,6 +118,10 @@ final class AnalyticsManager: IdentifiableClass { | |||
if newValue.retrospectiveCorrectionEnabled != oldValue.retrospectiveCorrectionEnabled { | |||
logEvent("Retrospective correction enabled change") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RC should be enabled for everyone, and allow IRC to be turned on/off.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you prefer to have just the RC slider switch removed (while still keeping retrospectiveCorrectionEnabled
so that one could disable RC by code modification), or to have retrospectiveCorrectionEnabled
completely removed from Loop?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
retrospectiveCorrectionEnabled
can be removed. It's still easy to remove from enabledEffects
if someone wanted to in the code.
Loop/Managers/LoopDataManager.swift
Outdated
@@ -40,6 +40,19 @@ final class LoopDataManager { | |||
NotificationCenter.default.removeObserver(observer) | |||
} | |||
} | |||
|
|||
// Integral restrospective correction parameters | |||
private let currentDiscrepancyGain = 1.0 // Standard retrospective correction gain |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IRC algorithm state should be moved into its own class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done that. Would be great if you could take a look and let me know if anything else needs be done. Then, I'll look into unit tests and issue report, will likely have questions about how to proceed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good. The class should be moved into its own file, and that will allow room for some documentation about how it works. Being an important core algorithm, it would be good to spend some time documenting the general functionality of IRC, the functions in the class, their parameters, and any static parameters. https://nshipster.com/swift-documentation/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IRC class is now in a separate file, including more detailed comments. I'll add IRC state to issue report next. Regarding unit tests, I've looked at DoseMathTests and searched around a bit, but I am still not sure how to put the unit tests together. Looks like need to add another target? Can you recommend a good reference (for beginners)?
Loop/Managers/LoopDataManager.swift
Outdated
// Integral restrospective correction parameters | ||
private let currentDiscrepancyGain = 1.0 // Standard retrospective correction gain | ||
private let persistentDiscrepancyGain = 5.0 // Retrospective correction gain for persistent long-term errors, must be >= currentDiscrepancyGain | ||
private let correctionTimeConstant = 120.0 // Retrospective correction filter time constant in minutes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
time constants should use TimeInterval
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
IRC algorithm moved into its own class.
IRC class in a separate file, with comments
add IRC state to issue report
This is looking good, but could be cleaned up a bit more: There should be a generic interface that returns glucose effect, and two implementations: one being your new IRC, and one being a legacy RC. That same interface can store its own “total retrospective correction” value or even generate its own user-facing description. The length of integration should be included in the issue report, as things like sign changes cause the integration to exit early. |
@ps2, I've refactored RC based on the last comment, but I'm not really sure this is what you were looking for. Maybe the RetrospectiveCorrection protocol should be organized a bit differently? |
@dm61 much more stable 👍 good job |
@francescaneri thanks for the feedback. Just fyi, I have just a few more things to do to complete IRC implementation, but I've been really swamped at work and other things, which is why this is taking some time. |
Updates:
At this point, I think I've completed all I've planned to do based on the review comments. Of course, would not mind making any further improvements if need be. |
@dm61 I'm testing. I can tell you over the weekend! |
improved: the basal and the algorithm work much better. even the predilection is more realistic. thanks for the job @dm61 |
@ps2 @Kdisimone it is possible to merge it? |
@ps2 any comments regarding what's needed to get this into Loop proper? |
6ce3673
to
1a96620
Compare
@ps2 I'd like to request closing this PR. As far as I can tell, anecdotal feedback from users who have tried branches with IRC has generally been favorable. However, I do not see how to completely eliminate risks associated with more aggressive corrections, especially in cases when ISF setting is too low. I would therefore recommend that we remove IRC from further considerations for the mainline Loop. To the extent I am able to do so, I am planning to keep the IRC branch up to date with Loop dev, so this feature will remain available for interested users. I'd like to thank you for providing LoopKit resources in support of IRC. I have no doubts these resources will prove beneficial in future Loop algorithm developments. Finally, I can't thank you enough for keeping high bar for Loop in terms of safety, effectiveness, user experience, and code quality. |
LMK if you or anyone else want to try an approach along the lines of autosens: IRC appears to be solving much the same problem, of sensitivity running higher or lower than normal for an extended period. As best I can tell autosens doesn’t have the same safety concerns that you mention here with regard to exacerbating poor ISF settings. |
with overrides-mode the integration would be simpler 👍 |
I think we might still want to include some of the core computations of this work; and also do more analysis that helps us understand what conditions it works in and when it doesn't. But I'll close it for now. |
PR for #695.