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

Added sensitivityRatio adjustment for low temp target set

Sam King 9 месяцев назад
Родитель
Сommit
26fca4b0c2

+ 23 - 9
Trio/Sources/APS/OpenAPSSwift/DetermineBasal/DetermineBasal+Helpers.swift

@@ -231,14 +231,17 @@ extension DeterminationGenerator {
     }
 
     static func calculateSensitivityRatio(
+        currentGlucose: Decimal,
         profile: Profile,
         autosens: Autosens?,
         targetGlucose: Decimal,
-        temptargetSet: Bool
-    ) -> Decimal {
+        temptargetSet: Bool,
+        dynamicIsfResult: DynamicISFResult?
+    ) -> (Decimal, Bool) {
         let normalTarget: Decimal = 100
         let halfBasalTarget = profile.halfBasalExerciseTarget
         var ratio: Decimal = 1
+        var updateAutosensRatio = false
 
         // High temp target raises sensitivity or low temp lowers it
         if (profile.highTemptargetRaisesSensitivity && temptargetSet && targetGlucose > normalTarget) ||
@@ -250,16 +253,27 @@ extension DeterminationGenerator {
             } else {
                 ratio = c / (c + targetGlucose - normalTarget)
             }
-            ratio = min(ratio, profile.autosensMax)
-            // You can round here if needed: ratio = ratio.rounded(2)
-            return ratio
+            ratio = min(ratio, profile.autosensMax).jsRounded(scale: 2)
+        } else if let autosens = autosens {
+            // Use autosens if present
+            ratio = autosens.ratio
         }
-        // Use autosens if present
+
         if let autosens = autosens {
-            return autosens.ratio
+            // Increase the dynamic ratio when using a low temp target
+            if profile.temptargetSet == true, targetGlucose < normalTarget, let dynamicIsfResult = dynamicIsfResult,
+               currentGlucose >= targetGlucose
+            {
+                if ratio < dynamicIsfResult.ratio {
+                    ratio = dynamicIsfResult.ratio * (normalTarget / targetGlucose)
+                    // Use autosesns.max limit
+                    ratio = min(ratio, profile.autosensMax).jsRounded(scale: 2)
+                    updateAutosensRatio = true
+                }
+            }
         }
-        // Otherwise default to 1.0 (no adjustment)
-        return 1.0
+
+        return (ratio, updateAutosensRatio)
     }
 
     static func computeAdjustedBasal(currentBasalRate: Decimal, sensitivityRatio: Decimal) -> Decimal {

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

@@ -63,12 +63,22 @@ enum DeterminationGenerator {
                 timestamp: autosensData.timestamp
             )
         }
-        let sensitivityRatio = calculateSensitivityRatio(
+        let (sensitivityRatio, updateAutosensRatio) = calculateSensitivityRatio(
+            currentGlucose: currentGlucose,
             profile: profile,
             autosens: autosensData,
             targetGlucose: profile.profileTarget(trioCustomOrefVariables: trioCustomOrefVariables) ?? 120,
-            temptargetSet: profile.temptargetSet ?? false
+            temptargetSet: profile.temptargetSet ?? false,
+            dynamicIsfResult: dynamicIsfResult
         )
+        if updateAutosensRatio {
+            autosensData = Autosens(
+                ratio: sensitivityRatio,
+                newisf: autosensData.newisf,
+                deviationsUnsorted: autosensData.deviationsUnsorted,
+                timestamp: autosensData.timestamp
+            )
+        }
 
         let basal: Decimal
         if let dynamicIsfResult = dynamicIsfResult, profile.tddAdjBasal {

+ 2 - 2
TrioTests/OpenAPSSwiftTests/DetermineBasalJsonTests.swift

@@ -97,13 +97,13 @@ import Testing
         #expect(comparison.resultType == .matching)
     }
 
-    @Test("Format determineBasal inputs for running in JS", .enabled(if: false)) func formatInputs() async throws {
+    @Test("Format determineBasal inputs for running in JS", .enabled(if: true)) func formatInputs() async throws {
         let openAps = OpenAPSFixed()
 
         // 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/f1d04efa-c39b-4f0a-9955-65ab663ff9fb.0.json")
+        let algorithmComparison = try await HttpFiles.downloadFile(at: "/files/f5e63e4f-b5bc-421a-b5d8-87d3326a688a.0.json")
         let determineBasalInput = algorithmComparison.determineBasalInput!
 
         let encoder = JSONCoding.encoder