Skip to content

Clamp prediction values to 39-400 mg/dL#562

Merged
marionbarker merged 2 commits intodevfrom
fix/clamp-prediction-values
Apr 9, 2026
Merged

Clamp prediction values to 39-400 mg/dL#562
marionbarker merged 2 commits intodevfrom
fix/clamp-prediction-values

Conversation

@bjorkert
Copy link
Copy Markdown
Contributor

Summary

  • Clamp prediction graph values to a range of 39–400 mg/dL to prevent extreme predictions (e.g. high COB) from blowing up the graph scale
  • Applies to both Loop and OpenAPS (ZT, IOB, COB, UAM) predictions
  • The min/max info display still shows unclamped values so the actual predicted range remains visible

Fixes #495

@bjorkert bjorkert linked an issue Mar 21, 2026 that may be closed by this pull request
@bjorkert bjorkert self-assigned this Mar 21, 2026
Copy link
Copy Markdown
Collaborator

@marionbarker marionbarker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would really like the clamped values to be configured as named values. For example.

CLAMP_MAX_PREDICTION = 400
CLAMP_MIN_PREDICTION = 39

Configure those in a constants file and then use them. Try to get away from having hard-coded values that need to be modified, sometimes in multiple places, if we ever decide to make a change.

This is not a requirement for this PR, but a suggestion moving forward.

@marionbarker
Copy link
Copy Markdown
Collaborator

Test

✅ Mostly does as expected.
Trio: I set up an outlier with a glucose value of 500 mg/dL so having the prediction flat-top at 400 doesn't look quite right.
Loop: there was not glucose outlier so prediction max and glucose tick-mark max were both visible at 400.

Do we want similar limits for outlier values for glucose. For example if someone fat fingers 1000 into Apple Health instead of 100?

Test Narrative

Uploaded a nasty pattern to Nightscout URL that includes 25 and 500.
Built this version and observe the scaling.

Started with glucose axis showing tic marks at 0, 50, 100, 150, 200, 250

The Glucose report is the actual value of 25 or 500, whereas Nightscout shows HIGH.

So this only clamps prediction values. The glucose value of 500 is displayed and glucose axis is now 0, 100, 200, 300, 400 and 500.

Trio Test

Go back to sine wave, change Trio CGM pattern to 160 +/- 100 with period of 1 hour, see if I can get some outlier predictions. Increase hours of prediction. Was 199/309 when 1 hour of prediction. Increase to 3 hours.

✅ For Trio, Min/Max shows: 166/431 for 3 hours, but glucose axis scales do not exceed the max glucose of 500.
❓ But also the plot flat tops at 400 when the glucose max range is 500 because that's the value

Loop Test

For Loop, don't need to play games with CGM. Can just add lots of carbs to get a high prediction.
Enter 100 g carbs, set a Manual Temp Basal to 0 U/hr for 2 hours.
Predictions is > 600 mg/dL
Increased hours of prediction to display to be 4 hours.

Initially, the glucose tic marks are 0, 50, 100, 150, 200, 250

dev version (without this PR): After loop where prediction shows min/max 117/681, the tick marks show 0 to 600 in steps of 100.

✅ build this version on the same phone: tick marks show 0 to 400 in steps of 100 and the prediction curve flat-tops at 400.

Copy link
Copy Markdown
Collaborator

@marionbarker marionbarker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I approve but posted a few questions to consider. Let me know if you want to make modifications or go ahead with this as is.

Replace hard-coded 39/400 mg/dL clamp bounds with
globalVariables.minDisplayGlucose and maxDisplayGlucose, shared with
the main-screen HIGH/LOW display logic so the range has a single
source of truth.
@bjorkert
Copy link
Copy Markdown
Contributor Author

bjorkert commented Apr 9, 2026

Good call — the constants now live in globalVariables.minDisplayGlucose / globalVariables.maxDisplayGlucose in Globals.swift, coordinated with #565 so both the main-screen HIGH/LOW labels and this prediction clamp share a single source of truth. If we ever want to adjust the range (there's a related discussion on #565 about Libre), it's now one place to change.

Copy link
Copy Markdown
Collaborator

@marionbarker marionbarker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve by code review and test.

@marionbarker marionbarker merged commit c7724d6 into dev Apr 9, 2026
@marionbarker marionbarker deleted the fix/clamp-prediction-values branch April 9, 2026 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Graph scale with high COB prediction

2 participants