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

Merge pull request #615 from nightscout/oref-swift-fix-broken-iob-optimizations

Fix broken iob optimizations
Sam King 4 месяцев назад
Родитель
Сommit
9555d0ca59

+ 1 - 38
Trio/Sources/APS/OpenAPSSwift/Iob/IobGenerator.swift

@@ -7,44 +7,7 @@ struct IobGenerator {
         clock: Date,
         autosens: Autosens?
     ) throws -> [IobResult] {
-        // As a performance optimization, filter out any pump events
-        // that occurred before the DIA would use it
-        let durationOfInsulinActionAgo = Double(profile.dia ?? 10) * 60 * 60
-        // add an extra two hours to the DIA to ensure we get all temp basals
-        let lastDurationOfInsulinAction = clock - durationOfInsulinActionAgo - 2.hoursToSeconds
-
-        // we have to keep all of our suspend/resume events due to a hardcoded
-        // DIA value in dealing with suspended pumps in JS
-        var pumpHistory = history.filter({ $0.timestamp >= lastDurationOfInsulinAction || $0.isSuspendOrResume() })
-            .map({ $0.computedEvent() })
-
-        // To make sure that lastTemp and lastBolusTime are filled in
-        // correctly, we need to check if there aren't any tempBasal or bolus
-        // events in the DIA-filtered list. If not, find the most recent one
-        // from the full history and add it.
-        if pumpHistory.filter({ $0.type == .tempBasal }).isEmpty {
-            // Find the most recent TempBasal event from before the DIA cutoff
-            let olderTempBasals = history.filter({ $0.type == .tempBasal && $0.timestamp < lastDurationOfInsulinAction })
-            if let lastTempBasal = olderTempBasals.max(by: { $0.timestamp < $1.timestamp }) {
-                // Find its matching TempBasalDuration (same timestamp)
-                if let matchingDuration = history
-                    .first(where: { $0.type == .tempBasalDuration && $0.timestamp == lastTempBasal.timestamp })
-                {
-                    pumpHistory.append(lastTempBasal.computedEvent())
-                    pumpHistory.append(matchingDuration.computedEvent())
-                }
-            }
-        }
-
-        // we need to check for amount != 0 to match the lastBolusTime logic
-        if pumpHistory.filter({ $0.type == .bolus && $0.amount != 0 }).isEmpty {
-            // Find the most recent Bolus event from before the DIA cutoff
-            let olderBoluses = history
-                .filter({ $0.type == .bolus && $0.amount != 0 && $0.timestamp < lastDurationOfInsulinAction })
-            if let lastBolus = olderBoluses.max(by: { $0.timestamp < $1.timestamp }) {
-                pumpHistory.append(lastBolus.computedEvent())
-            }
-        }
+        let pumpHistory = history.map { $0.computedEvent() }
 
         let treatments = try IobHistory.calcTempTreatments(
             history: pumpHistory,

+ 7 - 10
Trio/Sources/APS/OpenAPSSwift/Iob/IobHistory.swift

@@ -27,6 +27,9 @@ import Foundation
 /// These seem like small issues, and they are, but I have seen both in my data over a few days of running.
 
 struct IobHistory {
+    /// Used for calculating the beginning of a 0 temp when the pump history begins suspended
+    static let MAX_PUMP_HISTORY_HOURS: Double = 36
+
     struct PumpSuspended {
         let timestamp: Date
         let durationInMinutes: Decimal
@@ -116,7 +119,6 @@ struct IobHistory {
     /// periods of suspension.
     private static func getSuspends(
         pumpHistory: [ComputedPumpHistoryEvent],
-        profile: Profile,
         clock: Date
     ) throws -> [PumpSuspended] {
         let pumpSuspendResumeFull = pumpHistory.filter { $0.type == .pumpSuspend || $0.type == .pumpResume }
@@ -146,14 +148,9 @@ struct IobHistory {
         // If our first suspend/resume event is a resume, the pump is suspended
         // when our history begins
 
-        var profileDia = profile.dia ?? 10
-        if profileDia < 5 {
-            profileDia = 5
-        }
-
-        let maxDiaAgo = clock - profileDia.hoursToSeconds
-        if let first = pumpSuspendResume.first, first.type == .pumpResume, maxDiaAgo < first.timestamp {
-            let start = maxDiaAgo
+        let maxPumpHistoryAgo = clock - TimeInterval(hours: MAX_PUMP_HISTORY_HOURS)
+        if let first = pumpSuspendResume.first, first.type == .pumpResume, maxPumpHistoryAgo < first.timestamp {
+            let start = maxPumpHistoryAgo
             let duration = first.timestamp.timeIntervalSince(start).secondsToMinutes
             suspends.append(PumpSuspended(timestamp: start, durationInMinutes: duration, isSuspendedPrior: true))
         }
@@ -465,7 +462,7 @@ struct IobHistory {
         // ignore any records in the future and sort them
         let pumpHistory = history.filter({ $0.timestamp <= clock }).sorted { $0.timestamp < $1.timestamp }
         let tempBasals = try getTempBasals(pumpHistory: pumpHistory, clock: clock, zeroTempDuration: zeroTempDuration)
-        let suspends = try getSuspends(pumpHistory: pumpHistory, profile: profile, clock: clock)
+        let suspends = try getSuspends(pumpHistory: pumpHistory, clock: clock)
         let boluses = pumpHistory.filter({ $0.type == .bolus }).map { $0.copyWith(insulin: $0.amount) }
 
         var tempHistory: [ComputedPumpHistoryEvent]

+ 1 - 1
Trio/Sources/APS/OpenAPSSwift/Logging/OrefFunction.swift

@@ -41,7 +41,7 @@ enum OrefFunction: String, Codable {
             // can ignore them in our comparison
             return Set(["maxDeviation", "minDeviation", "allDeviations", "bwCarbs", "bwFound", "journalCarbs", "nsCarbs"])
         case .autosens:
-            return Set(["deviationsUnsorted"])
+            return Set(["deviationsUnsorted", "debugInfo"])
         case .determineBasal:
             // FIXME: Adjust as we go
             return Set([

+ 2 - 1
TrioTests/OpenAPSSwiftTests/javascript/bundle/autosens-prepare.js

@@ -13,7 +13,8 @@ function generate(glucose_data, pumphistory_data, basalprofile, profile_data, ca
     
     var iob_inputs = {
         history: pumphistory_data,
-        profile: profile_data
+        profile: profile_data,
+        clock: now
     };
 
     var detection_inputs = {

Разница между файлами не показана из-за своего большого размера
+ 1 - 1
TrioTests/OpenAPSSwiftTests/javascript/bundle/autosens.js


Разница между файлами не показана из-за своего большого размера
+ 1 - 1
TrioTests/OpenAPSSwiftTests/javascript/bundle/autotune-prep.js


Разница между файлами не показана из-за своего большого размера
+ 1 - 1
TrioTests/OpenAPSSwiftTests/javascript/bundle/iob-history.js


Разница между файлами не показана из-за своего большого размера
+ 1 - 1
TrioTests/OpenAPSSwiftTests/javascript/bundle/iob.js


Разница между файлами не показана из-за своего большого размера
+ 1 - 1
TrioTests/OpenAPSSwiftTests/javascript/bundle/meal.js