<commit_before>{
 "metadata": {
  "name": "",
  "signature": "sha256:a811e0a3b222dc6eaf65e01428901222fd6c04d79283bd19013355123137625e"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Exercise Numpy/Scipy"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### 1. Basics\n",
      "1. generate a 1-vector of size 10\n",
      "2. generate a zero-matrix of size 4x3\n",
      "3. generate a 3x3 identity matrix\n",
      "4. contruct a vector of length 100 with alternating ones and zeros ( hint: use slice indexing )\n",
      "5. construct a 8x8 matrix with a checkboard pattern of zeros and ones\n",
      "6. create a random vector of size 100 and sort it\n",
      "7. compute the inverse, eigenvectors and eigenvalues of the following matrix:\n",
      "$$\\mathbf{A} = \\left[\\begin{array}\n",
      "{rrr}\n",
      "1 & 2 & 3 \\\\\n",
      "4 & 5 & 6 \\\\\n",
      "7 & 8 & 9\n",
      "\\end{array}\\right]\n",
      "$$\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "import scipy as sp"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 0
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### 2. Solving ordinary differential equations (ODEs) using scipy\n",
      "\n",
      "Consider the following hypothetical reaction system which was first proposed by <NAME> (Lotka-Voltera System):\n",
      "$$ \\begin{align} \n",
      "r1:& \\;\\; X \\rightarrow 2 X \\\\ \n",
      "r2:& \\;\\; X + Y \\rightarrow 2 Y \\\\\n",
      "r3:& \\;\\; Y \\rightarrow \\varnothing \n",
      "\\end{align} $$\n",
      "\n",
      "1. set up the stoichiometric matrix of the system (this can be done without Python)\n",
      "2. the rate of change of the species participating in a reaction system can be written as\n",
      "$$ \\frac {dS} {dt} = N \\cdot v(S,p) $$\n",
      "with the vector of species concentrations $S$, the stoichiometric matrix N and the vector of reaction velocities $v(S,p)$ ($p$ denotes a vector of parmameters)\n",
      "define a function `dSdt( S, t )` which computes the rate of change for each species, given the vector of current species concentrations $S$ and the current time $t$. Assume that all reactions follow irreversible mass-action kinetics with parameters $k_1 = 1.5$, $k_2 = 0.8 $ and $k_3 = 0.9$, each equal to \n",
      "3. Use the scipy ODE integrator scipy.integrate.odeint to solve the reaction system numerically on the time interval $t \\in [0, 30]$ using the following initial conditions: $X(0)= Y(0) = 1$"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import scipy.integrate\n",
      "from matplotlib import pyplot as plt\n",
      "plt.style.use('ggplot')\n",
      "%matplotlib inline\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### 3. Parameter fitting\n",
      "\n",
      "You have been provided with an experimenal time-course measurement of the X and Y species (pickled object `experimental_data.p`). The data has the form of a numpy array with the first column representing the experimental time (in seconds), column 2 the measurement of X (in mM) and column 3 Y.\n",
      "\n",
      "* modify the `dSdt` function from above, such that a parameter vector `p`$= [k1, k2, k3]$ can be passed in addition to `S` and `t`\n",
      "\n",
      "* define an objective function `obj_fct( p, S_exp, t_exp )` which computes the squared distance between the experimental data and a model simulation for the parameters `p`$= [k1, k2, k3]$ for the time point vector `t_exp` (assume that the initial conditions are known and as defined above), (hint: additional arguments can be passed to the function integrated by `scipy.optimize.odeint` using the `args = (p,)` argument. Dont forget the comma in the tuple!\n",
      "\n",
      "* try to identify the parameters of the reaction system using this objective function and the `sicipy.optimize.fmin` minimization algorithm. Fit only parameters k2 and k3 and keep k1 at 1.5. Use the parameters given above as starting values.\n",
      "\n",
      "* plot a simulation of t"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import pickle\n",
      "import scipy.optimize"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    }
   ],
   "metadata": {}
  }
 ]
}<commit_msg>added numpy<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercise Numpy/Scipy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. Basics\n",
    "1. generate a 1-vector of size 10\n",
    "2. generate a zero-matrix of size 4x3\n",
    "3. generate a 3x3 identity matrix\n",
    "4. contruct a vector of length 100 with alternating ones and zeros ( hint: use slice indexing )\n",
    "5. construct a 8x8 matrix with a checkboard pattern of zeros and ones\n",
    "6. create a random vector of size 100 and sort it\n",
    "7. compute the inverse, eigenvectors and eigenvalues of the following matrix:\n",
    "$$\\mathbf{A} = \\left[\\begin{array}\n",
    "{rrr}\n",
    "1 & 2 & 3 \\\\\n",
    "4 & 5 & 6 \\\\\n",
    "7 & 8 & 9\n",
    "\\end{array}\\right]\n",
    "$$\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import scipy as sp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.,  0.,  1.,  0.,  1.,  0.,  1.,  0.],\n",
       "       [ 0.,  1.,  0.,  1.,  0.,  1.,  0.,  1.],\n",
       "       [ 1.,  0.,  1.,  0.,  1.,  0.,  1.,  0.],\n",
       "       [ 0.,  1.,  0.,  1.,  0.,  1.,  0.,  1.],\n",
       "       [ 1.,  0.,  1.,  0.,  1.,  0.,  1.,  0.],\n",
       "       [ 0.,  1.,  0.,  1.,  0.,  1.,  0.,  1.],\n",
       "       [ 1.,  0.,  1.,  0.,  1.,  0.,  1.,  0.],\n",
       "       [ 0.,  1.,  0.,  1.,  0.,  1.,  0.,  1.]])"
      ]
     },
     "execution_count": 160,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array(range(10)) #1\n",
    "np.zeros((4,3))     #2\n",
    "np.identity(3)      #3\n",
    "a=np.ones(100)      #4\n",
    "a[::2]=0\n",
    "a\n",
    "b=np.ones((64))      #4\n",
    "c=b.reshape(8,8)\n",
    "c[:,1][::2]=0\n",
    "c[1,:][::2]=0\n",
    "c[:,3][::2]=0\n",
    "c[3,:][::2]=0\n",
    "c[:,5][::2]=0\n",
    "c[5,:][::2]=0\n",
    "c[:,7][::2]=0\n",
    "c[7,:][::2]=0\n",
    "#c[1::2,][1::2]=0\n",
    "\n",
    "\n",
    "np.sort(np.random.rand(100))\n",
    "\n",
    "A = np.array ([[1,2,3],[4,5,6],[7,8,9]])\n",
    "\n",
    "B = np.linalg.inv(A)\n",
    "\n",
    "eigen = np.linalg.eig(A)\n",
    "eigenwert=eigen[0] \n",
    "eigenvec=eigen[1]\n",
    "c\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. Solving ordinary differential equations (ODEs) using scipy\n",
    "\n",
    "Consider the following hypothetical reaction system which was first proposed by <NAME> (Lotka-Voltera System):\n",
    "$$ \\begin{align} \n",
    "r1:& \\;\\; X \\rightarrow 2 X \\\\ \n",
    "r2:& \\;\\; X + Y \\rightarrow 2 Y \\\\\n",
    "r3:& \\;\\; Y \\rightarrow \\varnothing \n",
    "\\end{align} $$\n",
    "\n",
    "1. set up the stoichiometric matrix of the system (this can be done without Python)\n",
    "2. the rate of change of the species participating in a reaction system can be written as\n",
    "$$ \\frac {dS} {dt} = N \\cdot v(S,p) $$\n",
    "with the vector of species concentrations $S$, the stoichiometric matrix N and the vector of reaction velocities $v(S,p)$ ($p$ denotes a vector of parmameters)\n",
    "define a function `dSdt( S, t )` which computes the rate of change for each species, given the vector of current species concentrations $S$ and the current time $t$. Assume that all reactions follow irreversible mass-action kinetics with parameters $k_1 = 1.5$, $k_2 = 0.8 $ and $k_3 = 0.9$, each equal to \n",
    "3. Use the scipy ODE integrator scipy.integrate.odeint to solve the reaction system numerically on the time interval $t \\in [0, 30]$ using the following initial conditions: $X(0)= Y(0) = 1$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.7 -0.1]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7fa497bdba50>,\n",
       " <matplotlib.lines.Line2D at 0x7fa497bdbcd0>]"
      ]
     },
     "execution_count": 176,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "<KEY>",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fa49c703910>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from scipy.integrate import *\n",
    "from matplotlib import pyplot as plt\n",
    "plt.style.use('ggplot')\n",
    "%matplotlib inline\n",
    "\n",
    "N=np.array([[1,-1,0],[0,1,-1]])\n",
    "S0=[1.,1.]\n",
    "\n",
    "def dSdt(S,t):\n",
    "    p=np.array([1.5,0.8,0.9])\n",
    "    v=p*np.array([S[0],S[0]*S[1],S[1]])\n",
    "    #print v\n",
    "    return np.dot(N,v)\n",
    "print dSdt(S0,0)\n",
    "\n",
    "time=np.arange(0,30,0.1)\n",
    "y=odeint(dSdt, S0, time)\n",
    "plt.plot(time,y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. Parameter fitting\n",
    "\n",
    "You have been provided with an experimenal time-course measurement of the X and Y species (pickled object `experimental_data.p`). The data has the form of a numpy array with the first column representing the experimental time (in seconds), column 2 the measurement of X (in mM) and column 3 Y.\n",
    "\n",
    "* modify the `dSdt` function from above, such that a parameter vector `p`$= [k1, k2, k3]$ can be passed in addition to `S` and `t`\n",
    "\n",
    "* define an objective function `obj_fct( p, S_exp, t_exp )` which computes the squared distance between the experimental data and a model simulation for the parameters `p`$= [k1, k2, k3]$ for the time point vector `t_exp` (assume that the initial conditions are known and as defined above), (hint: additional arguments can be passed to the function integrated by `scipy.optimize.odeint` using the `args = (p,)` argument. Dont forget the comma in the tuple!\n",
    "\n",
    "* try to identify the parameters of the reaction system using this objective function and the `sicipy.optimize.fmin` minimization algorithm. Fit only parameters k2 and k3 and keep k1 at 1.5. Use the parameters given above as starting values.\n",
    "\n",
    "* plot a simulation of t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-0.9 -0.3]\n"
     ]
    }
   ],
   "source": [
    "import pickle\n",
    "import scipy.optimize\n",
    "N=np.array([[1,-1,0],[0,1,-1]])\n",
    "S0=[1.,3.]\n",
    "p=np.array([1.5,0.8,0.9])\n",
    "\n",
    "def dSdt(S,t,p):\n",
    "    \n",
    "    v=p*np.array([S[0],S[0]*S[1],S[1]])\n",
    "    #print v\n",
    "    return np.dot(N,v)\n",
    "print dSdt(S0,0,p)\n",
    "\n",
    "def obj_fct(p,S_exp,t_exp)\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
{'fraction_non_alphanumeric': 0.19520601597994675, 'fraction_numerical': 0.03360488798370672, 'mean_word_length': 2.346526867627785, 'pattern_counts': {'":': 130, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7536317', 'n_tokens_mistral': 4945, 'n_tokens_neox': 4695, 'n_words': 1347}