{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Part 1: NumPy Basics\n", "\n", "### Explore NumPy array\n", "\n", "- [NumPy ndarray](https://numpy.org/devdocs/reference/arrays.ndarray.html)\n", "- [Array indexing](https://numpy.org/devdocs/reference/arrays.indexing.html)\n", "- [Argwhere](https://numpy.org/doc/stable/reference/generated/numpy.argwhere.html)\n", "- [arange](https://numpy.org/doc/stable/reference/generated/numpy.arange.html)\n", "- [zeros](https://numpy.org/doc/stable/reference/generated/numpy.zeros.html)\n", "\n", "#### Using the above links and NumPy documentation to complete the following tasks:\n", "\n", "- use np.arange to make an array of time steps beginning at 0, ending at 10, with a step of 0.1\n", "- print out the values of those time steps\n", "- print out the shape of the time step.\n", "- use np.argwhere to find the first time step > 0.52, and print out the value of that time step.\n", "- use np.zeros to make fake data as a 2D ndarray. Think of it as the concentrations of 3 proteins over those timesteps. Initialize a ndaray of zeros should be 3 x time.size large.\n", "- then loop over the rows and coumns, thus\n", "\n", "for i in range(fakeY.shape[0]): # loop over rows\n", " for j in range(fakeY.shape[1]): # loop over columns\n", " fakeY[i,j] = i + 0.001*j\n", "\n", "- and print out its size and contents" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "# Write code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### More practice with ndarrays\n", "\n", "- Print the elements of the time array that are at indices 11, 12, 13, ..., 20\n", "- Print the result of subtracting 1 from fakeY\n", "- Replace all elements in the first column of fakeY with the number 2 and print the result.\n", "- Print a tuple with just one element" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Write code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Part 2: Scipy Basics\n", "\n", "In CS341, we will use scipy.linalg to find the distance between two vectors, scipy.signal to find the peaks in a time-series, and scipy.integrate to solve ordinary differential equations (i.e. simulate a model).\n", "\n", "If you have a recent enough version of scipy, then it will have [scipy.integrate.solve_ivp](https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.solve_ivp.html), which is short for \"solve initial value problem\". It takes as input the model, the initial value (the concentration of all the proteins at time = 0), the time steps over which you want to simulate the model, and parameter values. It returns the concentration of proteins at those time steps.\n", "\n", "If you have an older version of scipy, then you will need to use [scipy.integrate.odeint](https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.odeint.html#scipy.integrate.odeint), which takes the model in a different form (the order of arguments is different).\n", "\n", "We are going to begin with the simplest model typically used. In this model, we have one protein $P$, whose concentration we will model over time. The process we will model is called degradation - the process by which the protein is destroyed. At any given moment in time, a given proportion of that protein will be destroyed. If we start with a lot of protein, then early in the simulation we will be losing lots of it (it will be degrading quickly). Once we have just a little protein, we lose less at any given moment, so the process is slower. We capture that with the ordinary differential equation\n", "\n", "$\\frac{dP(t)}{dt} = -k P(t)$\n", "\n", "where k is the constant that determines the proportion of protein $P$ that is degraded. I read this as \"the rate of change of P depends is proportional to the amount of P\" and, if we assume that k>=0 (which is usually the case in these models), then we see that the rate of change of P is negative, and will be more negative (bigger) when P is bigger\".\n", "\n", "Below, use scipy.integrate.solve_vip to simulate the degradation model when $k=2$. $P(0)$ should be 3, and use timesteps 0 to 10 with a step of 0.1." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import scipy.integrate\n", "\n", "# write code here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For the purposes of parameter-fitting, we want to avoid hard-coding the coefficients. I will refer to these as the model parameters or just \"parameters\". We should re-write the model to use parameters. And then supply them to solve_ivp in the \"args\" argument." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# Write code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Part 3: MatPlotLib Basics\n", "\n", "- [MatPlotLib](https://matplotlib.org/)\n", "- [matplotlib.pyplot introduction](https://matplotlib.org/tutorials/introductory/pyplot.html)\n", "\n", "#### Using the above links and MatPlotLib documentation to complete the following tasks:\n", "\n", "Visualize the ODE output with Matplotlib. All plots should include a title, axis labels and a legend when applicable.\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "# Write code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## For older versions of Scipy\n", "\n", "If you don't have solve_ivp, then we need to make a couple of adjustments. First swap the order of t and y in the model function. Then, update the call to odeint. the function is called \"func\" instead of \"fun\". It takes the entire t array in just one parameter. Also, it returns just the y values." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# Write code here" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# Write code here" ] } ], "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.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }