CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
AllenDowney

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: AllenDowney/ModSimPy
Path: blob/master/notebooks/chap17.ipynb
Views: 531
Kernel: Python 3

Modeling and Simulation in Python

Chapter 17

Copyright 2017 Allen Downey

License: Creative Commons Attribution 4.0 International

# Configure Jupyter so figures appear in the notebook %matplotlib inline # Configure Jupyter to display the assigned value after an assignment %config InteractiveShell.ast_node_interactivity='last_expr_or_assign' # import functions from the modsim.py module from modsim import *

Data

We have data from Pacini and Bergman (1986), "MINMOD: a computer program to calculate insulin sensitivity and pancreatic responsivity from the frequently sampled intravenous glucose tolerance test", Computer Methods and Programs in Biomedicine, 23: 113-122..

data = pd.read_csv('data/glucose_insulin.csv', index_col='time')

Here's what the glucose time series looks like.

plot(data.glucose, 'bo', label='glucose') decorate(xlabel='Time (min)', ylabel='Concentration (mg/dL)')

And the insulin time series.

plot(data.insulin, 'go', label='insulin') decorate(xlabel='Time (min)', ylabel='Concentration ($\mu$U/mL)')

For the book, I put them in a single figure, using subplot

subplot(2, 1, 1) plot(data.glucose, 'bo', label='glucose') decorate(ylabel='Concentration (mg/dL)') subplot(2, 1, 2) plot(data.insulin, 'go', label='insulin') decorate(xlabel='Time (min)', ylabel='Concentration ($\mu$U/mL)') savefig('figs/chap17-fig01.pdf')

Interpolation

We have measurements of insulin concentration at discrete points in time, but we need to estimate it at intervening points. We'll use interpolate, which takes a Series and returns a function:

The return value from interpolate is a function.

I = interpolate(data.insulin)

We can use the result, I, to estimate the insulin level at any point in time.

I(7)

I can also take an array of time and return an array of estimates:

t_0 = get_first_label(data) t_end = get_last_label(data) ts = linrange(t_0, t_end, endpoint=True) I(ts) type(ts)

Here's what the interpolated values look like.

plot(data.insulin, 'go', label='insulin data') plot(ts, I(ts), color='green', label='interpolated') decorate(xlabel='Time (min)', ylabel='Concentration ($\mu$U/mL)') savefig('figs/chap17-fig02.pdf')

Exercise: Read the documentation of scipy.interpolate.interp1d. Pass a keyword argument to interpolate to specify one of the other kinds of interpolation, and run the code again to see what it looks like.

# Solution goes here

Exercise: Interpolate the glucose data and generate a plot, similar to the previous one, that shows the data points and the interpolated curve evaluated at the time values in ts.

# Solution goes here

Under the hood

source_code(interpolate)