forked from LoopKit/Loop
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LOOP-4781 Loop to use LoopAlgorithm swift package (#617)
* Adding testRunWithOngoingTempBasal * Building with LoopAlgorithm swift package * fix merge * Tests moved to LoopAlgorithm package * Fix warning * Adding cancel helper for TempBasalRecommendation * Add TempBasalRecommendationTests for extensions * Add SimpleInsulinDose so we can specify which fast acting model to LoopAlgorithm * Remove unused imports * Update for LoopAlgorithm using doses with insulin models * Updates from PR review * Fix signature of method call, and cleanup unused method * Unused var * Remove unused parts of method for clarity
- Loading branch information
Showing
76 changed files
with
1,368 additions
and
1,195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// BasalRelativeDose.swift | ||
// Loop | ||
// | ||
// Created by Pete Schwamb on 2/12/24. | ||
// Copyright © 2024 LoopKit Authors. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import LoopAlgorithm | ||
|
||
public extension Array where Element == BasalRelativeDose { | ||
func trimmed(from start: Date? = nil, to end: Date? = nil) -> [BasalRelativeDose] { | ||
return self.compactMap { (dose) -> BasalRelativeDose? in | ||
if let start, dose.endDate < start { | ||
return nil | ||
} | ||
if let end, dose.startDate > end { | ||
return nil | ||
} | ||
if dose.type == .bolus { | ||
// Do not split boluses | ||
return dose | ||
} | ||
return dose.trimmed(from: start, to: end) | ||
} | ||
} | ||
} | ||
|
||
extension BasalRelativeDose { | ||
public func trimmed(from start: Date? = nil, to end: Date? = nil, syncIdentifier: String? = nil) -> BasalRelativeDose { | ||
|
||
let originalDuration = endDate.timeIntervalSince(startDate) | ||
|
||
let startDate = max(start ?? .distantPast, self.startDate) | ||
let endDate = max(startDate, min(end ?? .distantFuture, self.endDate)) | ||
|
||
var trimmedVolume: Double = volume | ||
|
||
if originalDuration > .ulpOfOne && (startDate > self.startDate || endDate < self.endDate) { | ||
trimmedVolume = volume * (endDate.timeIntervalSince(startDate) / originalDuration) | ||
} | ||
|
||
return BasalRelativeDose( | ||
type: self.type, | ||
startDate: startDate, | ||
endDate: endDate, | ||
volume: trimmedVolume | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
import Foundation | ||
import HealthKit | ||
import LoopKit | ||
import LoopAlgorithm | ||
|
||
// MARK: - Simulated Core Data | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// | ||
// TempBasalRecommendation.swift | ||
// Loop | ||
// | ||
// Created by Pete Schwamb on 2/9/24. | ||
// Copyright © 2024 LoopKit Authors. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import LoopKit | ||
import LoopAlgorithm | ||
|
||
extension TempBasalRecommendation { | ||
/// Equates the recommended rate with another rate | ||
/// | ||
/// - Parameter unitsPerHour: The rate to compare | ||
/// - Returns: Whether the rates are equal within Double precision | ||
private func matchesRate(_ unitsPerHour: Double) -> Bool { | ||
return abs(self.unitsPerHour - unitsPerHour) < .ulpOfOne | ||
} | ||
|
||
/// Adjusts a recommendation based on the current state of pump delivery. If the current temp basal matches | ||
/// the recommendation, and enough time is remaining, then recommend no action. If we are running a temp basal | ||
/// and the new rate matches the scheduled rate, then cancel the currently running temp basal. If the current scheduled | ||
/// rate matches the recommended rate, then recommend no action. Otherwise, set a new temp basal of the | ||
/// recommended rate. | ||
/// | ||
/// - Parameters: | ||
/// - date: The date the recommendation would be delivered | ||
/// - neutralBasalRate: The scheduled basal rate at `date` | ||
/// - lastTempBasal: The previously set temp basal | ||
/// - continuationInterval: The duration of time before an ongoing temp basal should be continued with a new command | ||
/// - neutralBasalRateMatchesPump: A flag describing whether `neutralBasalRate` matches the scheduled basal rate of the pump. | ||
/// If `false` and the recommendation matches `neutralBasalRate`, the temp will be recommended | ||
/// at the scheduled basal rate rather than recommending no temp. | ||
/// - Returns: A temp basal recommendation | ||
func adjustForCurrentDelivery( | ||
at date: Date, | ||
neutralBasalRate: Double, | ||
currentTempBasal: DoseEntry?, | ||
continuationInterval: TimeInterval, | ||
neutralBasalRateMatchesPump: Bool | ||
) -> TempBasalRecommendation? { | ||
// Adjust behavior for the currently active temp basal | ||
if let currentTempBasal, currentTempBasal.type == .tempBasal, currentTempBasal.endDate > date | ||
{ | ||
/// If the last temp basal has the same rate, and has more than `continuationInterval` of time remaining, don't set a new temp | ||
if matchesRate(currentTempBasal.unitsPerHour), | ||
currentTempBasal.endDate.timeIntervalSince(date) > continuationInterval { | ||
return nil | ||
} else if matchesRate(neutralBasalRate), neutralBasalRateMatchesPump { | ||
// If our new temp matches the scheduled rate of the pump, cancel the current temp | ||
return .cancel | ||
} | ||
} else if matchesRate(neutralBasalRate), neutralBasalRateMatchesPump { | ||
// If we recommend the in-progress scheduled basal rate of the pump, do nothing | ||
return nil | ||
} | ||
|
||
return self | ||
} | ||
|
||
public static var cancel: TempBasalRecommendation { | ||
return self.init(unitsPerHour: 0, duration: 0) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
import Foundation | ||
import LoopKit | ||
import LoopAlgorithm | ||
|
||
|
||
extension UserDefaults { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
|
||
import Foundation | ||
import LoopKit | ||
import LoopAlgorithm | ||
|
||
class DoseEnactor { | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.