Просмотр исходного кода

Fix bug in basal rate calculation

Sam King 5 месяцев назад
Родитель
Сommit
61c0d060a8

+ 8 - 3
Trio/Sources/APS/OpenAPSSwift/DetermineBasal/DetermineBasal+Helpers.swift

@@ -164,9 +164,14 @@ extension DeterminationGenerator {
         return (ratio, updateAutosensRatio)
     }
 
-    static func computeAdjustedBasal(currentBasalRate: Decimal, sensitivityRatio: Decimal) -> Decimal {
-        // FIXME: Ideally, we round this here to allowed pump basal increments
-        currentBasalRate * sensitivityRatio
+    static func computeAdjustedBasal(
+        profile: Profile,
+        currentBasalRate: Decimal,
+        sensitivityRatio: Decimal,
+        overrideFactor: Decimal
+    ) -> Decimal {
+        let adjustedBasal = currentBasalRate * sensitivityRatio * overrideFactor
+        return TempBasalFunctions.roundBasal(profile: profile, basalRate: adjustedBasal)
     }
 
     static func computeAdjustedSensitivity(

+ 12 - 5
Trio/Sources/APS/OpenAPSSwift/DetermineBasal/DetermineBasalGenerator.swift

@@ -75,7 +75,8 @@ enum DeterminationGenerator {
             glucoseStatus: glucoseStatus,
             profile: profile,
             currentTemp: currentTemp,
-            currentTime: currentTime
+            currentTime: currentTime,
+            trioCustomOrefVariables: trioCustomOrefVariables
         ) {
             return errorDetermination
         }
@@ -157,13 +158,17 @@ enum DeterminationGenerator {
         let basal: Decimal
         if let dynamicIsfResult = dynamicIsfResult, profile.tddAdjBasal {
             basal = computeAdjustedBasal(
+                profile: profile,
                 currentBasalRate: profile.currentBasal ?? profile.basalFor(time: currentTime),
-                sensitivityRatio: dynamicIsfResult.tddRatio
+                sensitivityRatio: dynamicIsfResult.tddRatio,
+                overrideFactor: trioCustomOrefVariables.overrideFactor()
             )
         } else {
             basal = computeAdjustedBasal(
+                profile: profile,
                 currentBasalRate: profile.currentBasal ?? profile.basalFor(time: currentTime),
-                sensitivityRatio: sensitivityRatio
+                sensitivityRatio: sensitivityRatio,
+                overrideFactor: trioCustomOrefVariables.overrideFactor()
             )
         }
 
@@ -532,7 +537,8 @@ enum DeterminationGenerator {
         glucoseStatus: GlucoseStatus,
         profile: Profile,
         currentTemp: TempBasal?,
-        currentTime: Date
+        currentTime: Date,
+        trioCustomOrefVariables: TrioCustomOrefVariables
     ) throws -> Determination? {
         let glucose = glucoseStatus.glucose
         let noise = glucoseStatus.noise
@@ -544,9 +550,10 @@ enum DeterminationGenerator {
         let device = glucoseStatus.device
 
         // Always use profile-supplied basal
-        guard let basal = profile.currentBasal else {
+        guard let profileBasal = profile.currentBasal else {
             throw DeterminationError.missingCurrentBasal
         }
+        let basal = profileBasal * trioCustomOrefVariables.overrideFactor()
 
         // Compose tick for log
         let tick: String = (delta > -0.5) ? "+\(delta.rounded(toPlaces: 0))" : "\(delta.rounded(toPlaces: 0))"

+ 3 - 2
Trio/Sources/APS/OpenAPSSwift/Logging/OrefFunction.swift

@@ -45,12 +45,13 @@ enum OrefFunction: String, Codable {
         case .determineBasal:
             // FIXME: Adjust as we go
             return Set([
-                // Not calculating yet
-                "id",
+                // Final dosing calculation
                 // "units",
                 // "insulinReq",
                 // "rate",
                 // "duration",
+                // Not calculating yet
+                "id",
                 "deliverAt",
                 "temp",
                 "reservoir",

+ 1 - 1
TrioTests/OpenAPSSwiftTests/DetermineBasalJsonTests.swift

@@ -103,7 +103,7 @@ import Testing
         // this test is meant for one-off analysis so it's ok to hard code
         // a file, just make sure to _not_ check in updates to this to
         // avoid polluting our change logs
-        let algorithmComparison = try await HttpFiles.downloadFile(at: "/files/cfd2e5b0-bfca-4f11-a232-845b3ea114d6.0.json")
+        let algorithmComparison = try await HttpFiles.downloadFile(at: "/files/cb0dac5c-4620-42ae-a66d-1fa54d89daaa.0.json")
         let determineBasalInput = algorithmComparison.determineBasalInput!
 
         let encoder = JSONCoding.encoder