|
| 1 | +// |
| 2 | +// LoopAlgorithmTests.swift |
| 3 | +// LoopTests |
| 4 | +// |
| 5 | +// Created by Pete Schwamb on 8/17/23. |
| 6 | +// Copyright © 2023 LoopKit Authors. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import XCTest |
| 10 | +import LoopKit |
| 11 | +import LoopCore |
| 12 | +import HealthKit |
| 13 | + |
| 14 | +final class LoopAlgorithmTests: XCTestCase { |
| 15 | + |
| 16 | + override func setUpWithError() throws { |
| 17 | + // Put setup code here. This method is called before the invocation of each test method in the class. |
| 18 | + } |
| 19 | + |
| 20 | + override func tearDownWithError() throws { |
| 21 | + // Put teardown code here. This method is called after the invocation of each test method in the class. |
| 22 | + } |
| 23 | + |
| 24 | + public var bundle: Bundle { |
| 25 | + return Bundle(for: type(of: self)) |
| 26 | + } |
| 27 | + |
| 28 | + public func loadFixture<T>(_ resourceName: String) -> T { |
| 29 | + let path = bundle.path(forResource: resourceName, ofType: "json")! |
| 30 | + return try! JSONSerialization.jsonObject(with: Data(contentsOf: URL(fileURLWithPath: path)), options: []) as! T |
| 31 | + } |
| 32 | + |
| 33 | + func loadBasalRateScheduleFixture(_ resourceName: String) -> BasalRateSchedule { |
| 34 | + let fixture: [JSONDictionary] = loadFixture(resourceName) |
| 35 | + |
| 36 | + let items = fixture.map { |
| 37 | + return RepeatingScheduleValue(startTime: TimeInterval(minutes: $0["minutes"] as! Double), value: $0["rate"] as! Double) |
| 38 | + } |
| 39 | + |
| 40 | + return BasalRateSchedule(dailyItems: items, timeZone: .utcTimeZone)! |
| 41 | + } |
| 42 | + |
| 43 | + func loadPredictedGlucoseFixture(_ name: String) -> [PredictedGlucoseValue] { |
| 44 | + let decoder = JSONDecoder() |
| 45 | + decoder.dateDecodingStrategy = .iso8601 |
| 46 | + |
| 47 | + let url = bundle.url(forResource: name, withExtension: "json")! |
| 48 | + return try! decoder.decode([PredictedGlucoseValue].self, from: try! Data(contentsOf: url)) |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + func testLiveCaptureWithFunctionalAlgorithm() throws { |
| 53 | + // This matches the "testForecastFromLiveCaptureInputData" test of LoopDataManagerDosingTests, |
| 54 | + // Using the same input data, but generating the forecast using LoopPrediction |
| 55 | + |
| 56 | + let mockGlucoseStore = MockGlucoseStore(for: .liveCapture) |
| 57 | + let historicGlucose = mockGlucoseStore.storedGlucose! |
| 58 | + |
| 59 | + let mockDoseStore = MockDoseStore(for: .liveCapture) |
| 60 | + let doses = mockDoseStore.doseHistory! |
| 61 | + |
| 62 | + let mockCarbStore = MockCarbStore(for: .liveCapture) |
| 63 | + let carbEntries = mockCarbStore.carbHistory! |
| 64 | + |
| 65 | + let baseTime = historicGlucose.last!.startDate |
| 66 | + let treatmentInterval = LoopAlgorithm.treatmentHistoryDateInterval(for: baseTime) |
| 67 | + |
| 68 | + |
| 69 | + let isfStart = min(treatmentInterval.start, doses.map { $0.startDate }.min() ?? .distantFuture) |
| 70 | + let isfEnd = baseTime.addingTimeInterval(InsulinMath.defaultInsulinActivityDuration).dateCeiledToTimeInterval(GlucoseMath.defaultDelta) |
| 71 | + |
| 72 | + let basalRateSchedule = loadBasalRateScheduleFixture("basal_profile") |
| 73 | + |
| 74 | + let insulinSensitivitySchedule = InsulinSensitivitySchedule( |
| 75 | + unit: .milligramsPerDeciliter, |
| 76 | + dailyItems: [ |
| 77 | + RepeatingScheduleValue(startTime: 0, value: 45), |
| 78 | + RepeatingScheduleValue(startTime: 32400, value: 55) |
| 79 | + ], |
| 80 | + timeZone: .utcTimeZone |
| 81 | + )! |
| 82 | + let carbRatioSchedule = CarbRatioSchedule( |
| 83 | + unit: .gram(), |
| 84 | + dailyItems: [ |
| 85 | + RepeatingScheduleValue(startTime: 0.0, value: 10.0), |
| 86 | + ], |
| 87 | + timeZone: .utcTimeZone |
| 88 | + )! |
| 89 | + |
| 90 | + let glucoseTargetRangeSchedule = GlucoseRangeSchedule( |
| 91 | + unit: HKUnit.milligramsPerDeciliter, |
| 92 | + dailyItems: [ |
| 93 | + RepeatingScheduleValue(startTime: TimeInterval(0), value: DoubleRange(minValue: 100, maxValue: 110)), |
| 94 | + RepeatingScheduleValue(startTime: TimeInterval(28800), value: DoubleRange(minValue: 90, maxValue: 100)), |
| 95 | + RepeatingScheduleValue(startTime: TimeInterval(75600), value: DoubleRange(minValue: 100, maxValue: 110)) |
| 96 | + ], |
| 97 | + timeZone: .utcTimeZone)! |
| 98 | + |
| 99 | + let settings = LoopAlgorithmSettings( |
| 100 | + basal: basalRateSchedule.between(start: treatmentInterval.start, end: treatmentInterval.end), |
| 101 | + sensitivity: insulinSensitivitySchedule.quantitiesBetween(start: isfStart, end: isfEnd), |
| 102 | + carbRatio: carbRatioSchedule.between(start: treatmentInterval.start, end: treatmentInterval.end), |
| 103 | + target: glucoseTargetRangeSchedule.quantityBetween(start: baseTime, end: isfEnd), |
| 104 | + maximumBasalRatePerHour: 5.0, |
| 105 | + maximumBolus: 10, |
| 106 | + suspendThreshold: GlucoseThreshold(unit: HKUnit.milligramsPerDeciliter, value: 75)) |
| 107 | + |
| 108 | + let input = LoopPredictionInput( |
| 109 | + glucoseHistory: historicGlucose, |
| 110 | + doses: doses, |
| 111 | + carbEntries: carbEntries, |
| 112 | + settings: settings) |
| 113 | + |
| 114 | + let prediction = try LoopAlgorithm.generatePrediction(input: input) |
| 115 | + |
| 116 | + let expectedPredictedGlucose = loadPredictedGlucoseFixture("live_capture_predicted_glucose") |
| 117 | + |
| 118 | + XCTAssertEqual(expectedPredictedGlucose.count, prediction.glucose.count) |
| 119 | + |
| 120 | + let defaultAccuracy = 1.0 / 40.0 |
| 121 | + |
| 122 | + for (expected, calculated) in zip(expectedPredictedGlucose, prediction.glucose) { |
| 123 | + XCTAssertEqual(expected.startDate, calculated.startDate) |
| 124 | + XCTAssertEqual(expected.quantity.doubleValue(for: .milligramsPerDeciliter), calculated.quantity.doubleValue(for: .milligramsPerDeciliter), accuracy: defaultAccuracy) |
| 125 | + } |
| 126 | + |
| 127 | + //XCTAssertEqual(1.99, recommendedBasal!.unitsPerHour, accuracy: defaultAccuracy) |
| 128 | + } |
| 129 | + |
| 130 | + func testPerformanceExample() throws { |
| 131 | + // This is an example of a performance test case. |
| 132 | + self.measure { |
| 133 | + // Put the code you want to measure the time of here. |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments