Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insulin activity #1

Open
scottleibrand opened this issue Oct 25, 2016 · 53 comments
Open

Insulin activity #1

scottleibrand opened this issue Oct 25, 2016 · 53 comments
Labels

Comments

@scottleibrand
Copy link

How hard would it be to also return the level of insulin activity (first derivative of IOB)? That is the other thing we need to be able to incorporate this into oref0 or similar.

@beached
Copy link

beached commented Oct 25, 2016

All the curves I have seen use body mass as a factor. But a quick hack might be to take the total insulin/DIA and get a U/min or use a normal distribution.

@scottleibrand
Copy link
Author

I think it should be literally as "simple" as also returning the derivative of the curves already in this code. (I'd have to get help from WolframAlpha to figure out the derivatives, though. I don't remember any of that from HS calc.) :-)

@bustavo
Copy link
Member

bustavo commented Oct 28, 2016

If my math is not that bad ( and please don't trust me...test test test ), the derivative should be:
(constant_a * 4) * time_from_bolus^3 + (constant_b * 3) * time_from_bolus^2 + (constant_c * 2) * time_from_bolus + constant_d

Make sure to maintain each constant symbol ( plus or minus )...

@beached
Copy link

beached commented Oct 29, 2016

I am not sure that is the correct way(formula above is fine a_x^b becomes ab_x^(b-1) for each term).

I may be wrong too, but that will give your the instantanious rate of change for iob left at time t in percentage. But don't we really want the amount changed between time t and the last measurement.

@bustavo
Copy link
Member

bustavo commented Oct 29, 2016

If you need to get the difference of IOB between t and ( t-1 ), then you can do that with the actual formula as it is just manually getting the two values for each time and then subtracting the amounts... is that what you need @scottleibrand?

@kenstack
Copy link

as @bustavo says its really asy to do it by simply taking a backward difference or a forward difference
since the step times change I dont use the old value - I simply do

(iob (t+1)-iob(t))/1.0 (you can skip the divide by 1 of course :)) that will give you the local slope of iob, and if you multiple that by your sensitivity at that moment it gives you the iob velocity

@cjo20
Copy link

cjo20 commented Nov 29, 2016

I've been looking at ways of changing the maths behind iob/activity in oref0 to something easier to maintain. As part of the comparison, I looked at the 3 hour formula from here.

I calculated the derivative (everything /100, derivative sign flipped to make the comparison easier) and compared to the existing oref0 results.

poly = -3.203e-9 * pow(i,4) + 1.354e-6 * pow(i,3) - 1.759e-4 * pow(i,2) + 9.255e-4 * i + 0.99951
polyDeriv = 4*3.203e-9 * pow(i,3) - 3*1.354e-6 * pow(i,2) + 2*1.759e-4 * i - 9.255e-4

image
This was the result - the tail end of the derivative doesn't fit very well with what would be expected. Is this potentially a problem with the polynomials currently being used?

@bustavo bustavo reopened this Nov 29, 2016
@bustavo
Copy link
Member

bustavo commented Nov 29, 2016

I think I could explain this with two interesting things I've seen with my own personal data:

  1. It could make sense that the curve goes back up in the end if you consider that sensitivity goes up as your bg goes down. If you keep your sensitivity fixed ( ie always 22 points per insulin Unit ), this curve shape could make sense... if you keep your sensitivity variable it doesn't ( ie 15 at 150 / 20 at 120 / 30 at 100 / 40 at 80 / etc ) because the sensitivity variability will already be accounting for the increment you are trying to show with the curve tail going up again.

  2. It could make sense that the curve goes back up in the end if you consider that maybe your IOB curve is a little bit longer... @kenstack & I observed that after 180 minutes we had about other 30 minutes of activity ( bg still lowering ) and the only reason we found for this was a longer IOB curve, hence the new IOB 3.5 hour curve which now fits perfectly with the observed data.

I'd like to read @kenstack others opinion on this curve for discussion...

@cjo20
Copy link

cjo20 commented Nov 29, 2016

Just to be absolutely clear, the grey line above is purely from taking the derivative of this polynomial: https://github.com/Perceptus/iob/blob/master/insulin_on_board_pct.js#L71

The orange line is what is currently used by oref0.

I'm not sure where exactly the polynomial equations came from, but if it was just from trying to fit a curve to published ones then I could imagine it would be very easy to get something that looks right but with weird behaviours in the derivatives.

Trying to model things like changing insulin resistance as part of this equation doesn't feel right, mainly because there isn't actually a guarantee that BG will be lower 150 minutes after any bolus.

@bustavo
Copy link
Member

bustavo commented Nov 29, 2016

Yes there is absolutely no need at all to integrate insulin resistance into this curve as this curve just measures the effect of the insulin.

The referenced polynomial is a fit from Walsh's published insulin activity curves which come from a clamp study where a person got insulin put into its body and then glucose was used to try to level the bg according to the insulin activity... the resulting curve ( glucose used to maintain the bg level with the used insulin ) is this IOB curve.

I don't really still understand why there is a need to get a derivative from IOB curves... could you please explain?

@cjo20
Copy link

cjo20 commented Nov 29, 2016

The IOB curve tells you how much insulin you have that effectively hasn't done anything yet. The derivative tells you how much insulin you have acting on you at that time to lower your BG. This is useful in calculating expected BG change when determining what insulin doses you might need to end up in range.

You can get the same information without explicitly differentiating the polynomial, by just doing (f(x+1)-f(x))/x, but that will have the same kick in the tail as the derivative polynomial. Calculating the derivative is just easier for creating a graph.

@bustavo
Copy link
Member

bustavo commented Nov 29, 2016

Ah yes! I do the difference of the values between two points in time which is the same as the derivative...

So the question is really... is the derivative right? I think it is for myself I've been using it for a long time to calculate what I call "insulin consumption" so I can calculate the insulin sensitivity at that point in time.

@cjo20
Copy link

cjo20 commented Nov 29, 2016

It's currently unclear if the curve is correct or not (it probably isn't correct). If it is wrong, it is also unclear how much impact it has; I suspect "not much", but it would be nice for things to be more correct where possible. It certainly doesn't look right at first glance. It feels like the derivative should be 0 at 180 minutes.

@bustavo
Copy link
Member

bustavo commented Nov 29, 2016

There are a couple of IFs on the equation that help do that:

if( time_from_bolus <= 0.0 ) {
percentage = 100.0
} else if ( time_from_bolus >= insulin_duration * 60.0 ) {
percentage = 0.0
}

@cjo20
Copy link

cjo20 commented Nov 29, 2016

Ok, I'll be a bit more pedantic :P It feels like the derivative should only have one turning point between 0 and DIA, at the peak insulin action. There should be no step changes - it should be almost 0 just before the end of DIA.

@kenstack
Copy link

@cjo20 yes that is correct here is the function plus the derivative plotted together
image

those polynomials were fit without any constraints on the derivatives - but I dont think its an issue here - remeber that the IOB curves are MASSIVE approximations to start with - all that simple linear bolus wizard type math is not that accurate anyway, so I wouldnt be too worried about this. If you are concerned and want a different velocity curve, you can certainly fit one. If you are looking for a local derivative for "iob velocity", then either do what you did or simply take the difference between (iob(t+1 min)-iob(t)) and divide by one. If you are looking for the effect on BG over say 20 min then id take the difference between iob(t+20)-iob(t) and use the actual difference in BG not the velocity imho.

@bustavo
Copy link
Member

bustavo commented Nov 29, 2016

No worries @cjo20 :)

The MDT devices jump from 5% to 0% so @kenstack worked on "smoothing" that tail part of the curve... it never hits 0 so we have to enforce the 0 at the end of the IOB curve length.

@kenstack
Copy link

@cjo20 if you find an old MDT pump manual it has the curves they use in it - take a look - IOB never goes to zero even at 180 min for a 3 hour curve, derivatives arent zero... they are just approximations - if you want to smooth the derivative youll need to change the curve itself to be smooth to zero at the end - if you look at the iob curve you can see it hits zero at a slope so hence the non zero velocity

@cjo20
Copy link

cjo20 commented Nov 29, 2016

@kenstack It looks like your graph shows less of a kick than mine, which is odd.

AFAIK, The old MDT curves weren't really designed to work with an APS system - the problem being that 2.5 hours after basal correction starts, oref0 is going to think the correction is having more of an impact than it did at 2 hours. I need to look in to how much of a difference it'll actually make.

@kenstack
Copy link

kenstack commented Nov 29, 2016

@cjo20 the MDT curves like most IOB curves come from a glucose clamp test where the subject's bg is "clamped" at 100 via intravenous feed of glucose. then they are given a 12-15U bolus of insulin. The curve is derived from the amount of glucose that needs to be given over the next say 5-6 hours to keep BG flat. Incrementally you are right - but I dont think its going to matter much - at least I havent noticed it in my loop. But looking at that curve you are talking about (eyeballing here the difference from 0.4 to that bump) max of about 0.05-0.075 versus a peak of -0.8. Not huge, but yes you could certainly smooth it - let me know if you see any real difference.

@cjo20
Copy link

cjo20 commented Nov 29, 2016

@kenstack Yeah, it looks a lot worse on my graph - going from under 0.004 to almost 0.006. It looks much less of an issue in your graph. Now I'm curious about why our graphs are so different.

@kenstack
Copy link

kenstack commented Nov 29, 2016

one more set of curves - here is the 3.5 hour curve that @bustavo and I use in our loops

image

I think its going to be tough to get rid of the bump at the end - physically I agree its likely not real, but the reality is that "DIA" is really 5-6 hours however the observable effect we see in most boluses is 3-4 hours. so these curves, to drive IOB to zero at DIA need to really get agressive at the end - or youll need to really modify the observable drop earlier (ie have it be more agressive) so you net out at zero. This is likely why MDT chose to instead always leave about 5% of insulin "on board" even at DIA.

that initial positive velocity obviously is not real thats just poor curve fitting

@kenstack
Copy link

kenstack commented Nov 29, 2016

@cjo20 huh yes not sure... I am going to try to modify the curves a bit later today and try to smooth velocity at the end as well as hit zero iob at the DIA. I dont think - without changing the initial shape drastically - Ill be able to get velocity to zero at the end of the curve but Ill see what I can do. again Im not sure its going to make a ton of difference but lets try it. Make sense?

@cjo20
Copy link

cjo20 commented Nov 29, 2016

It's theoretically possible to write a program that will generate the curves for you, based on setting a few constraints. I may give it a go at the weekend.

@beached
Copy link

beached commented Nov 29, 2016

That 5-6 can be observable. I have had it become significant when exercising.

Is that curve at that point going to be significant and change decisions? There is very little active insulin to do anything at this point. This is the speed of the decay going from slower/faster at the tail end right?

@kenstack
Copy link

kenstack commented Nov 29, 2016

@cjo20 yes - but that wont match the experimental values (glucose clamp) for the effect of insulin as they measured it. We can certainly create some smooth curves for speed and IOB level - is that what you are looking for? It would just be an optimization loop around an equation form - could set iob to 100% at t=0, 0% at t=dia, derivatives equal to zero at t=0,DIA - youll either end up with a really complicated polynomial (order 6 plus Inwould guess) or something that doesnt fit in the middle but hits the boundary conditions. Thats the tradeoff. Also as you crank up the order of the polynomial youll get really weird derivative behavior in the middle.

@kenstack
Copy link

@beached I dont think it is for an IOB calc but maybe for velocity calcs (I use velocity for example to calc deviations, but I havent noticed anything too weird) - but we can play with the smoothing - @cjo20 is right the velocity shouldnt decrease again at the end, but its likely as we try to fix it the fit gets worse everywhere else. the issue is that these simple models have limits. We are really pushing it at the moment using this stuff as a controller I think. But, until we can simplify the more complicated models, this is what we have.

@cjo20
Copy link

cjo20 commented Nov 29, 2016

@kenstack You can try and get as close though. Basically, if you set up a system of equations:

f(x) = a*x^4 + b*x^3 + c*x^2 + d*x + e
f(0) = 1
f(180) = 0
f'(0) = 0
f'(180) = 0
f''(peak) = 0

And then solve them as simultaneous equations. Some of the values will be unbound, which means you get to "pick" what they are and they'll still fit those points, and it should be possible to constrain it so there aren't any additional turning points in the area we care about. At that point, it should be possible to start adding more constraints to try and make it match the published curves. I don't know how close we can get, but it's probably worth me spending some time looking at it. If I can write a program to generate possible solutions then it should be easier.

@kenstack
Copy link

@cjo20 right - but I think youve over constrained it - you have 5 conditions for 4 variables, and on top of that you havent included the ability to use those same constants to minimize the fit error to the data, right? right now those constants are chosen with no regard to those derivatives etc - just to fit the experimental data. We can definitely create a curve that fits the BCs, but it would like be of higher order and likely wouldnt fit the data. If we would rather have a curve that simply is smooth betwee t=0 and t=DIA I totally agree.

@kenstack
Copy link

5 variables sorry :) the over constraint is the fitting

@beached
Copy link

beached commented Nov 29, 2016

My stuff keeps it at 5min granularity so I would only have 1-2 blip in the velocity anyway.

One thing I wonder though, and excuse my ignorance, but this is all a fuzzy estimation to fit collected data. The original data was probably collected at some interval(5min?) and whole blood. I think that too has a measurement error of over 5%. How tight can the curve really be fitted to that and still be representative?

I wonder if carrying the error through all the calculations might give some insight into how to more safely make decisions.

@kenstack
Copy link

@beached yes its just a very loose estimate. The collection of the data - I think they collect at a larger interval since the test is so long (5-6 hours) - the shape is then "shrunk" in time to fit DIA

@cjo20 actually Im not sure you can constrain a 4th order poly as much as that - I think youll get no solution as Im thinking about this.... d=0 and e=1 by inspection based on the BC's at zero, youll end up with 3 equations for 3 unknowns but I not sure you can force the poly thru that. As I said befor eyou may need to icnrease the order, which leads to more variables which is fine but its moving the wring way re getting more complicated

@kenstack
Copy link

kenstack commented Nov 29, 2016

@cjo20 actually since your 2nd derivative constraint isnt at 180 you can indeed solve it - here is the result - looks fine - as suspected a bit more aggresive at early times in terms of velocity and less at the end. Derivatives now match your BC's. I dont think you'll see a huge difference in practice but definitely let us know! I can generate the curves for other DIAs if interested. @bustavo and I usually use the 3.5 hr curve.

image

@cjo20
Copy link

cjo20 commented Nov 29, 2016

If it's not too much trouble, are you able to plot the IOB curve on the same graph as your formula?

@kenstack
Copy link

kenstack commented Nov 29, 2016

image

blue is the new one

@beached
Copy link

beached commented Nov 30, 2016

Related, here is a screenshot I found that even shows the active insulin increasing near the end of DAI. It's from https://www.ncbi.nlm.nih.gov/pubmed/22882249
image

@bustavo
Copy link
Member

bustavo commented Nov 30, 2016

@beached I think that is insulin concentration through time... its different than IOB...

IOB measures the effect on glucose from insulin.
This curve measures the concentration of insulin through time in mg/kg/min.

I will publish that repository with those equations soon under Perceptus.

@beached
Copy link

beached commented Nov 30, 2016

It's the infusion rate of glucose needed to match the insulin I thought. so same as action of insulin.

Maybe I am confused

@bustavo
Copy link
Member

bustavo commented Nov 30, 2016

No idea... I can't access the document only view the summary... the times from dosing considered on that curve ( up to 12 hours ) makes sense to me from what I've seen on insulin concentration curves but not on IOB curves...

@bustavo
Copy link
Member

bustavo commented Nov 30, 2016

@beached you're right, mg/kg/min is the glucose infusion rate...
@kenstack, could those be the clamp studies results?

@beached
Copy link

beached commented Nov 30, 2016

I wonder how the relationship between dose to body mass holds up as a method of determining the proper dia. Looks like all the charts I see have a longer dia as the ratio goes up. Might explain a lot of the anecdotal variability with people basing it on types of meals and something to play with.

@kenstack
Copy link

kenstack commented Nov 30, 2016

@beached yes those are clamp studies - GIR is glucose infusion rate - they derive the IOB curves from those. And yes they go very long 5+ hours - reality is insulin action is much longer then 3 hours - but its hidden by changes in EGP etc - again these simple models make tons of assumptions - the body mass is handled by them giving insulin injections that are proportional to body mass

@beached
Copy link

beached commented Nov 30, 2016

@kenstack I have yet to see an AP that uses body mass in the predicting the GIR and DIA. I am ignorant of the research out there, but it seems like it is a significant factor from the data I see in these clamp studies

@kenstack
Copy link

@beached sorry not sure I understood your point there. The IOB curves in this case are derived from the area under those curves - the body mass is taken into account (sort of) by them generally using an insulin dose proportional body mass (though studies can vary) - so if you are a 75 kg person they use about 14-16 U to give you an idea. If less or more they change it proportionally - so in their minds the IOB curve takes into account the body mass

@beached
Copy link

beached commented Nov 30, 2016

The charts seems to show that as the U dose/kg mass increases, so does the DIA. So if one has a large meal, say 100g/CHO, takes 15U of insulin, and has a mass of 100kg they would have a ratio of 0.15U/kg. So, probably, a shorter duration than had they eaten a 150g meal and taken 22.5U with a ratio of 0.23U/kg.

Adjust the choice of IOB curve based on the dose/body mass ratio is what I mean. Not a fixed value

This also implies that basal doses, would probably have the shortest duration. I think this has to do with ratio's of insulin volume to surface area as it is highest when the dose is small and absorbs faster.

@cjo20
Copy link

cjo20 commented Nov 30, 2016

Increased body mass tends to be associated with increased insulin resistance.
People tend to have worked out an insulin:carb ratio that works for them. By adjusting this ratio so that it keeps your blood sugar in range after food, insulin resistance is taken in to account, which means that the I:C ratio will tend to take in to account body weight, leading to most people having a similar u/kg.

@beached
Copy link

beached commented Nov 30, 2016

@cjo20 What I read from the data is that the more you take the longer it takes to complete. For example, a 1U bolus would take 4hrs, but a 10U bolus could take 5hrs. The U/kg would be 10x. I made up those times, but the trend is what I am emphasising. Large carb meals stretch the iob curve.

@bustavo
Copy link
Member

bustavo commented Nov 30, 2016

@beached Interesting! I'd like to read more about that... do you have the reference to the article?

@beached
Copy link

beached commented Nov 30, 2016

Maybe I read into too much, but the data on all charts shows a longer DIA for larger doses(0.1/0.2/0.4U)/kg . The link above shows it as does the graph above. Plus this http://www.diapedia.org/management/81040851208/pharmacokinetics-and-dynamics-of-insulin-absorption

@bustavo
Copy link
Member

bustavo commented Nov 30, 2016

I am now live testing the new 3.5 hr smoothed out curve... will let you know how it goes

active_percentage = ( ((-0.0000000560934) * (elapsed_time4)) + ((0.0000451551) * (elapsed_time3)) - ((0.00927644) * (elapsed_time**2)) + ((0.0) * (elapsed_time)) + (100.0) )

@kenstack
Copy link

@beached no doubt you are rihgt - but its a bit more complicated. The real absorbtion of insulin, and diffusion of glucose is driven by gradients (like any diffusion process). so a dose of 1 U with no insulin in tissue or blood is going to have a far different profile then one with insulin already there at a high concentration. So lets say you bolused 5 U, then 1U an our later. Its not really dose anymore - its concentration. We are working on simplifying that stuff but its not easy.. at least for me :)

@kenstack
Copy link

@cjo20 Ive been running a 3.5 hr curve since last night based on the smoother BC's you had asked for - I havent noticed a difference yet but Ill keep ypu updated let me know your results as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants