Skip to content

Commit

Permalink
Merge pull request #2 from Philoul/AutoISF_Plugin_Prototype
Browse files Browse the repository at this point in the history
Create OapsProfileIsf
  • Loading branch information
ga-zelle authored May 8, 2024
2 parents 6fd5bc7 + baa2c5d commit 8b1d439
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 73 deletions.
31 changes: 15 additions & 16 deletions app/src/androidTest/kotlin/app/aaps/ReplayApsResultsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import app.aaps.core.interfaces.aps.CurrentTemp
import app.aaps.core.interfaces.aps.GlucoseStatus
import app.aaps.core.interfaces.aps.MealData
import app.aaps.core.interfaces.aps.OapsProfile
import app.aaps.core.interfaces.aps.OapsProfileAutoIsf
import app.aaps.core.interfaces.logging.AAPSLogger
import app.aaps.core.interfaces.logging.LTag
import app.aaps.core.interfaces.maintenance.FileListProvider
Expand Down Expand Up @@ -667,7 +668,7 @@ class ReplayApsResultsTest @Inject constructor() {
for (i in 0 until determineBasalResult.iobData!!.length())
iobData.add(determineBasalResult.iobData!!.getJSONObject(i).toIob())
val currentTime = determineBasalResult.currentTime
val profile = OapsProfile(
val profile = OapsProfileAutoIsf(
dia = 0.0,
min_5m_carbimpact = 0.0,
max_iob = determineBasalResult.profile.getDouble("max_iob"),
Expand Down Expand Up @@ -709,8 +710,6 @@ class ReplayApsResultsTest @Inject constructor() {
autosens_max = determineBasalResult.profile.getDouble("autosens_max"),
out_units = determineBasalResult.profile.optString("out_units"),
variable_sens = varSens, // TODO only available in result.variableSens? , not in determineBasalResult.profile.getDouble("variable_sens"),
insulinDivisor = 0,
TDD = 0.0,
autoISF_version = determineBasalResult.profile.optString("autoISF_version"),
enable_autoISF = determineBasalResult.profile.getBoolean("enable_autoISF"),
autoISF_max = determineBasalResult.profile.getDouble("autoISF_max"),
Expand Down Expand Up @@ -770,19 +769,19 @@ class ReplayApsResultsTest @Inject constructor() {
// aapsLogger.debug(LTag.APS, resultKt.reason.toString())
aapsLogger.debug(LTag.APS, "File: $filename")
// // assertThat(resultKt.reason.toString()).isEqualTo(result?.json?.getString("reason"))
assertThat(resultKt.tick ?: "").isEqualTo(result?.json()?.optString("tick"))
assertThat(resultKt.eventualBG ?: Double.NaN).isEqualTo(result?.json()?.optDouble("eventualBG"))
assertThat(resultKt.targetBG ?: Double.NaN).isEqualTo(result?.json()?.optDouble("targetBG"))
assertThat(resultKt.insulinReq ?: Double.NaN).isEqualTo(result?.json()?.optDouble("insulinReq"))
assertThat(resultKt.carbsReq ?: 0).isEqualTo(result?.json()?.optInt("carbsReq"))
assertThat(resultKt.carbsReqWithin ?: 0).isEqualTo(result?.json()?.optInt("carbsReqWithin"))
assertThat(resultKt.units ?: Double.NaN).isEqualTo(result?.json()?.optDouble("units"))
assertThat(resultKt.sensitivityRatio ?: Double.NaN).isEqualTo(result?.json()?.optDouble("sensitivityRatio"))
assertThat(resultKt.duration ?: 0).isEqualTo(result?.json()?.optInt("duration"))
assertThat(resultKt.rate ?: Double.NaN).isEqualTo(result?.json()?.optDouble("rate"))
assertThat(resultKt.COB ?: Double.NaN).isEqualTo(result?.json()?.optDouble("COB"))
assertThat(resultKt.IOB ?: Double.NaN).isEqualTo(result?.json()?.optDouble("IOB"))
assertThat(resultKt.variable_sens ?: Double.NaN).isEqualTo(result?.json()?.optDouble("variable_sens"))
assertThat(resultKt.tick ?: "").isEqualTo(result.json()?.optString("tick"))
assertThat(resultKt.eventualBG ?: 0.0).isWithin(1.0).of(result.json()?.optDouble("eventualBG") ?: 0.0)
assertThat(resultKt.targetBG ?: Double.NaN).isEqualTo(result.json()?.optDouble("targetBG"))
assertThat(resultKt.insulinReq ?: Double.NaN).isEqualTo(result.json()?.optDouble("insulinReq"))
assertThat(resultKt.carbsReq ?: 0).isEqualTo(result.json()?.optInt("carbsReq"))
assertThat(resultKt.carbsReqWithin ?: 0).isEqualTo(result.json()?.optInt("carbsReqWithin"))
assertThat(resultKt.units ?: Double.NaN).isEqualTo(result.json()?.optDouble("units"))
assertThat(resultKt.sensitivityRatio ?: Double.NaN).isEqualTo(result.json()?.optDouble("sensitivityRatio"))
assertThat(resultKt.duration ?: 0).isEqualTo(result.json()?.optInt("duration"))
assertThat(resultKt.rate ?: Double.NaN).isEqualTo(result.json()?.optDouble("rate"))
assertThat(resultKt.COB ?: Double.NaN).isEqualTo(result.json()?.optDouble("COB"))
assertThat(resultKt.IOB ?: Double.NaN).isEqualTo(result.json()?.optDouble("IOB"))
assertThat(resultKt.variable_sens ?: Double.NaN).isEqualTo(result.json()?.optDouble("variable_sens"))
}

enum class TestSource { ASSET, FILE }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import app.aaps.core.interfaces.aps.GlucoseStatus
import app.aaps.core.interfaces.aps.IobTotal
import app.aaps.core.interfaces.aps.MealData
import app.aaps.core.interfaces.aps.OapsProfile
import app.aaps.core.interfaces.aps.OapsProfileAutoIsf
import app.aaps.core.interfaces.aps.Predictions
import app.aaps.core.interfaces.constraints.Constraint
import app.aaps.core.interfaces.constraints.ConstraintsChecker
Expand Down Expand Up @@ -82,6 +83,7 @@ open class APSResultObject(protected val injector: HasAndroidInjector) : APSResu
override var glucoseStatus: GlucoseStatus? = null
override var currentTemp: CurrentTemp? = null
override var oapsProfile: OapsProfile? = null
override var oapsProfileAutoIsf: OapsProfileAutoIsf? = null
override var mealData: MealData? = null
override var autosensResult: AutosensResult? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ interface APSResult {
var glucoseStatus: GlucoseStatus?
var currentTemp: CurrentTemp?
var oapsProfile: OapsProfile?
var oapsProfileAutoIsf: OapsProfileAutoIsf?
var mealData: MealData?

val iob: IobTotal? get() = iobData?.get(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,5 @@ data class OapsProfile(
//DynISF only
var variable_sens: Double,
var insulinDivisor: Int,
var TDD: Double,
//AutoISF only
var autoISF_version: String = "",
var enable_autoISF: Boolean = true,
var autoISF_max: Double = 1.0,
var autoISF_min: Double = 1.0,
var bgAccel_ISF_weight: Double = 0.0,
var bgBrake_ISF_weight: Double = 0.0,
var enable_pp_ISF_always: Boolean = false,
var pp_ISF_hours: Int = 3,
var pp_ISF_weight: Double = 0.0,
var delta_ISFrange_weight: Double = 0.0,
var lower_ISFrange_weight: Double = 0.0,
var higher_ISFrange_weight: Double = 0.0,
var enable_dura_ISF_with_COB: Boolean = false,
var dura_ISF_weight: Double = 0.0,
var smb_delivery_ratio: Double = 0.5,
var smb_delivery_ratio_min: Double = 0.5,
var smb_delivery_ratio_max: Double = 0.5,
var smb_delivery_ratio_bg_range: Double = 0.0,
var smb_max_range_extension: Double = 1.0,
var enableSMB_EvenOn_OddOff: Boolean = false,
var enableSMB_EvenOn_OddOff_always: Boolean = false,
var iob_threshold_percent: Int = 100,
var profile_percentage: Int = 100
var TDD: Double
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package app.aaps.core.interfaces.aps

import kotlinx.serialization.Serializable

@Serializable
data class OapsProfileAutoIsf(
var dia: Double, // AMA only
var min_5m_carbimpact: Double, // AMA only
var max_iob: Double,
var max_daily_basal: Double,
var max_basal: Double,
var min_bg: Double,
var max_bg: Double,
var target_bg: Double,
var carb_ratio: Double,
var sens: Double,
var autosens_adjust_targets: Boolean, // AMA only
var max_daily_safety_multiplier: Double,
var current_basal_safety_multiplier: Double,
var high_temptarget_raises_sensitivity: Boolean,
var low_temptarget_lowers_sensitivity: Boolean,
var sensitivity_raises_target: Boolean,
var resistance_lowers_target: Boolean,
var adv_target_adjustments: Boolean,
var exercise_mode: Boolean,
var half_basal_exercise_target: Int,
var maxCOB: Int,
var skip_neutral_temps: Boolean,
var remainingCarbsCap: Int,
var enableUAM: Boolean,
var A52_risk_enable: Boolean,
var SMBInterval: Int,
var enableSMB_with_COB: Boolean,
var enableSMB_with_temptarget: Boolean,
var allowSMB_with_high_temptarget: Boolean,
var enableSMB_always: Boolean,
var enableSMB_after_carbs: Boolean,
var maxSMBBasalMinutes: Int,
var maxUAMSMBBasalMinutes: Int,
var bolus_increment: Double,
var carbsReqThreshold: Int,
var current_basal: Double,
var temptargetSet: Boolean,
var autosens_max: Double,
var out_units: String,
var lgsThreshold: Int?,
//AutoISF only
var variable_sens: Double,
var autoISF_version: String,
var enable_autoISF: Boolean,
var autoISF_max: Double,
var autoISF_min: Double,
var bgAccel_ISF_weight: Double,
var bgBrake_ISF_weight: Double,
var enable_pp_ISF_always: Boolean,
var pp_ISF_hours: Int,
var pp_ISF_weight: Double,
var delta_ISFrange_weight: Double,
var lower_ISFrange_weight: Double,
var higher_ISFrange_weight: Double,
var enable_dura_ISF_with_COB: Boolean,
var dura_ISF_weight: Double,
var smb_delivery_ratio: Double,
var smb_delivery_ratio_min: Double,
var smb_delivery_ratio_max: Double,
var smb_delivery_ratio_bg_range: Double,
var smb_max_range_extension: Double,
var enableSMB_EvenOn_OddOff: Boolean,
var enableSMB_EvenOn_OddOff_always: Boolean,
var iob_threshold_percent: Int,
var profile_percentage: Int
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import app.aaps.core.interfaces.aps.GlucoseStatus
import app.aaps.core.interfaces.aps.IobTotal
import app.aaps.core.interfaces.aps.MealData
import app.aaps.core.interfaces.aps.OapsProfile
import app.aaps.core.interfaces.aps.OapsProfileAutoIsf
import app.aaps.core.interfaces.aps.Predictions
import app.aaps.core.interfaces.aps.RT
import app.aaps.core.interfaces.constraints.Constraint
Expand Down Expand Up @@ -79,6 +80,7 @@ class DetermineBasalResult @Inject constructor(val injector: HasAndroidInjector)
override var glucoseStatus: GlucoseStatus? = null
override var currentTemp: CurrentTemp? = null
override var oapsProfile: OapsProfile? = null
override var oapsProfileAutoIsf: OapsProfileAutoIsf? = null
override var mealData: MealData? = null

lateinit var result: RT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import app.aaps.core.interfaces.aps.GlucoseStatus
import app.aaps.core.interfaces.aps.IobTotal
import app.aaps.core.interfaces.aps.MealData
import app.aaps.core.interfaces.aps.OapsProfile
import app.aaps.core.interfaces.aps.OapsProfileAutoIsf
import app.aaps.core.interfaces.aps.RT
import app.aaps.core.objects.aps.DetermineBasalResult
import dagger.android.HasAndroidInjector
Expand All @@ -17,8 +18,7 @@ import kotlinx.serialization.json.Json
fun app.aaps.database.entities.APSResult.fromDb(injector: HasAndroidInjector): APSResult =
when (algorithm) {
app.aaps.database.entities.APSResult.Algorithm.AMA,
app.aaps.database.entities.APSResult.Algorithm.SMB,
app.aaps.database.entities.APSResult.Algorithm.AUTO_ISF ->
app.aaps.database.entities.APSResult.Algorithm.SMB ->
DetermineBasalResult(injector, Json.decodeFromString(this.resultJson)).also { result ->
result.date = this.timestamp
result.glucoseStatus = this.glucoseStatusJson?.let { Json.decodeFromString(it) }
Expand All @@ -29,15 +29,25 @@ fun app.aaps.database.entities.APSResult.fromDb(injector: HasAndroidInjector): A
result.autosensResult = this.autosensDataJson?.let { Json.decodeFromString(it) }
}

else -> error("Unsupported")
app.aaps.database.entities.APSResult.Algorithm.AUTO_ISF ->
DetermineBasalResult(injector, Json.decodeFromString(this.resultJson)).also { result ->
result.date = this.timestamp
result.glucoseStatus = this.glucoseStatusJson?.let { Json.decodeFromString(it) }
result.currentTemp = this.currentTempJson?.let { Json.decodeFromString(it) }
result.iobData = this.iobDataJson?.let { Json.decodeFromString(it) }
result.oapsProfileAutoIsf = this.profileJson?.let { Json.decodeFromString(it) }
result.mealData = this.mealDataJson?.let { Json.decodeFromString(it) }
result.autosensResult = this.autosensDataJson?.let { Json.decodeFromString(it) }
}

else -> error("Unsupported")
}

@OptIn(ExperimentalSerializationApi::class)
fun APSResult.toDb(): app.aaps.database.entities.APSResult =
when (algorithm) {
APSResult.Algorithm.AMA,
APSResult.Algorithm.SMB,
APSResult.Algorithm.AUTO_ISF ->
APSResult.Algorithm.SMB ->
app.aaps.database.entities.APSResult(
timestamp = this.date,
algorithm = this.algorithm.toDb(),
Expand All @@ -50,7 +60,20 @@ fun APSResult.toDb(): app.aaps.database.entities.APSResult =
resultJson = Json.encodeToString(RT.serializer(), this.rawData() as RT)
)

else -> error("Unsupported")
APSResult.Algorithm.AUTO_ISF ->
app.aaps.database.entities.APSResult(
timestamp = this.date,
algorithm = this.algorithm.toDb(),
glucoseStatusJson = this.glucoseStatus?.let { Json.encodeToString(GlucoseStatus.serializer(), it) },
currentTempJson = this.currentTemp?.let { Json.encodeToString(CurrentTemp.serializer(), it) },
iobDataJson = this.iobData?.let { Json.encodeToString(ArraySerializer(IobTotal.serializer()), it) },
profileJson = this.oapsProfileAutoIsf?.let { Json.encodeToString(OapsProfileAutoIsf.serializer(), it) },
mealDataJson = this.mealData?.let { Json.encodeToString(MealData.serializer(), it) },
autosensDataJson = this.autosensResult?.let { Json.encodeToString(AutosensResult.serializer(), it) },
resultJson = Json.encodeToString(RT.serializer(), this.rawData() as RT)
)

else -> error("Unsupported")
}

fun app.aaps.database.entities.APSResult.Algorithm.fromDb(): APSResult.Algorithm =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class OpenAPSFragment : DaggerFragment(), MenuProvider {
binding.glucosestatus.text = lastAPSResult.glucoseStatus?.dataClassToHtml(listOf("glucose", "delta", "shortAvgDelta", "longAvgDelta"))
binding.currenttemp.text = lastAPSResult.currentTemp?.dataClassToHtml()
binding.iobdata.text = rh.gs(R.string.array_of_elements, lastAPSResult.iobData?.size) + "\n" + lastAPSResult.iob?.dataClassToHtml()
binding.profile.text = lastAPSResult.oapsProfile?.dataClassToHtml()
binding.profile.text = lastAPSResult.oapsProfile?.dataClassToHtml() ?: lastAPSResult.oapsProfileAutoIsf?.dataClassToHtml()
binding.mealdata.text = lastAPSResult.mealData?.dataClassToHtml()
binding.scriptdebugdata.text = lastAPSResult.scriptDebug?.joinToString("\n")
binding.constraints.text = lastAPSResult.inputConstraints?.getReasons()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import app.aaps.core.interfaces.aps.CurrentTemp
import app.aaps.core.interfaces.aps.GlucoseStatus
import app.aaps.core.interfaces.aps.IobTotal
import app.aaps.core.interfaces.aps.MealData
import app.aaps.core.interfaces.aps.OapsProfile
import app.aaps.core.interfaces.aps.OapsProfileAutoIsf
import app.aaps.core.interfaces.aps.Predictions
import app.aaps.core.interfaces.aps.RT
import app.aaps.core.interfaces.profile.ProfileUtil
import app.aaps.plugins.aps.openAPSAutoISF.OpenAPSAutoISFPlugin
import java.text.DecimalFormat
import java.time.Instant
import java.time.ZoneId
Expand Down Expand Up @@ -60,7 +59,7 @@ class DetermineBasalAutoISF @Inject constructor(
//if (profile.out_units === "mmol/L") round(value / 18, 1).toFixed(1);
//else Math.round(value);

fun enable_smb(profile: OapsProfile, microBolusAllowed: Boolean, meal_data: MealData, target_bg: Double): Boolean {
fun enable_smb(profile: OapsProfileAutoIsf, microBolusAllowed: Boolean, meal_data: MealData, target_bg: Double): Boolean {
// disable SMB when a high temptarget is set
if (!microBolusAllowed) {
consoleError.add("SMB disabled (!microBolusAllowed)")
Expand Down Expand Up @@ -105,10 +104,10 @@ class DetermineBasalAutoISF @Inject constructor(
consoleError.add(msg)
}

private fun getMaxSafeBasal(profile: OapsProfile): Double =
private fun getMaxSafeBasal(profile: OapsProfileAutoIsf): Double =
min(profile.max_basal, min(profile.max_daily_safety_multiplier * profile.max_daily_basal, profile.current_basal_safety_multiplier * profile.current_basal))

fun setTempBasal(_rate: Double, duration: Int, profile: OapsProfile, rT: RT, currenttemp: CurrentTemp): RT {
fun setTempBasal(_rate: Double, duration: Int, profile: OapsProfileAutoIsf, rT: RT, currenttemp: CurrentTemp): RT {
//var maxSafeBasal = Math.min(profile.max_basal, 3 * profile.max_daily_basal, 4 * profile.current_basal);

val maxSafeBasal = getMaxSafeBasal(profile)
Expand Down Expand Up @@ -148,7 +147,7 @@ class DetermineBasalAutoISF @Inject constructor(


fun determine_basal(
glucose_status: GlucoseStatus, currenttemp: CurrentTemp, iob_data_array: Array<IobTotal>, profile: OapsProfile, autosens_data: AutosensResult, meal_data: MealData,
glucose_status: GlucoseStatus, currenttemp: CurrentTemp, iob_data_array: Array<IobTotal>, profile: OapsProfileAutoIsf, autosens_data: AutosensResult, meal_data: MealData,
microBolusAllowed: Boolean, currentTime: Long, flatBGsDetected: Boolean, autoIsfMode: Boolean, loop_wanted_smb: String, profile_percentage: Int, smb_ratio: Double,
smb_max_range_extension: Double, iob_threshold_percent: Int, auto_isf_console: MutableList<String>
): RT {
Expand Down
Loading

0 comments on commit 8b1d439

Please sign in to comment.