{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Modeling and Simulation in Python\n", "\n", "Chapter 17\n", "\n", "Copyright 2017 Allen Downey\n", "\n", "License: [Creative Commons Attribution 4.0 International](https://creativecommons.org/licenses/by/4.0)\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Configure Jupyter so figures appear in the notebook\n", "%matplotlib inline\n", "\n", "# Configure Jupyter to display the assigned value after an assignment\n", "%config InteractiveShell.ast_node_interactivity='last_expr_or_assign'\n", "\n", "# import functions from the modsim.py module\n", "from modsim import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Data\n", "\n", "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.." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "data = pd.read_csv('data/glucose_insulin.csv', index_col='time')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's what the glucose time series looks like." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "plot(data.glucose, 'bo', label='glucose')\n", "decorate(xlabel='Time (min)',\n", " ylabel='Concentration (mg/dL)')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And the insulin time series." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "plot(data.insulin, 'go', label='insulin')\n", "decorate(xlabel='Time (min)',\n", " ylabel='Concentration ($\\mu$U/mL)')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For the book, I put them in a single figure, using `subplot`" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "subplot(2, 1, 1)\n", "plot(data.glucose, 'bo', label='glucose')\n", "decorate(ylabel='Concentration (mg/dL)')\n", "\n", "subplot(2, 1, 2)\n", "plot(data.insulin, 'go', label='insulin')\n", "decorate(xlabel='Time (min)',\n", " ylabel='Concentration ($\\mu$U/mL)')\n", "\n", "savefig('figs/chap17-fig01.pdf')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Interpolation\n", "\n", "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:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The return value from `interpolate` is a function." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "I = interpolate(data.insulin)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use the result, `I`, to estimate the insulin level at any point in time." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "scrolled": true }, "outputs": [], "source": [ "I(7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`I` can also take an array of time and return an array of estimates:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "t_0 = get_first_label(data)\n", "t_end = get_last_label(data)\n", "ts = linrange(t_0, t_end, endpoint=True)\n", "I(ts)\n", "type(ts)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's what the interpolated values look like." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "plot(data.insulin, 'go', label='insulin data')\n", "plot(ts, I(ts), color='green', label='interpolated')\n", "\n", "decorate(xlabel='Time (min)',\n", " ylabel='Concentration ($\\mu$U/mL)')\n", "\n", "savefig('figs/chap17-fig02.pdf')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** [Read the documentation](https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.interp1d.html) 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. " ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**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`." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Under the hood" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "source_code(interpolate)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }