<commit_before>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Class 27 - Boolean Networks\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy\n",
    "\n",
    "nodes = ['Cell Size', \n",
    "         'Cln3', \n",
    "         'MBF', \n",
    "         'Clb5,6', \n",
    "         'Mcm1/SFF', \n",
    "         'Swi5', \n",
    "         'Sic1', \n",
    "         'Clb1,2', \n",
    "         'Cdc20&Cdc14', \n",
    "         'Cdh1', \n",
    "         'Cln1,2', \n",
    "         'SBF']\n",
    "\n",
    "N = len(nodes)\n",
    "\n",
    "# define the transition matrix \n",
    "a = numpy.zeros([N, N])\n",
    "a[0,1] = 1\n",
    "a[1,1] = -1\n",
    "a[1,2] = 1\n",
    "a[1,11] = 1\n",
    "a[2,3] = 1\n",
    "a[3,4] = 1\n",
    "a[3,6] = -1\n",
    "a[3,7] = 1\n",
    "a[3,9] = -1\n",
    "a[4,4] = -1\n",
    "a[4,5] = 1\n",
    "a[4,7] = 1\n",
    "a[4,8] = 1\n",
    "a[5,5] = -1\n",
    "a[5,6] = 1\n",
    "a[6,3] = -1\n",
    "a[6,7] = -1\n",
    "a[7,2] = -1\n",
    "a[7,4] = 1\n",
    "a[7,5] = -1\n",
    "a[7,6] = -1\n",
    "a[7,8] = 1\n",
    "a[7,9] = -1\n",
    "a[7,11] = -1\n",
    "a[8,3] = -1\n",
    "a[8,5] = 1\n",
    "a[8,6] = 1\n",
    "a[8,7] = -1\n",
    "a[8,8] = -1\n",
    "a[8,9] = 1\n",
    "a[9,7] = -1\n",
    "a[10,6] = -1\n",
    "a[10,9] = -1\n",
    "a[10,10] = -1\n",
    "a[11,10] = 1\n",
    "a = numpy.matrix(a)\n",
    "\n",
    "# define the matrix of states for the fixed points\n",
    "num_fp = 7\n",
    "fixed_points = numpy.zeros([num_fp, N])\n",
    "fixed_points[0, 6] = 1\n",
    "fixed_points[0, 9] = 1\n",
    "fixed_points[1, 10] = 1\n",
    "fixed_points[1, 11] = 1\n",
    "fixed_points[2, 2] = 1\n",
    "fixed_points[2, 6] = 1\n",
    "fixed_points[2, 9] = 1\n",
    "fixed_points[3, 6] = 1\n",
    "fixed_points[4, 2] = 1\n",
    "fixed_points[4, 6] = 1\n",
    "fixed_points[6, 9] = 1\n",
    "fixed_points = numpy.matrix(fixed_points)\n",
    "\n",
    "basin_counts = numpy.zeros(num_fp)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define a function `hamming.dist` that gives the hamming distance between two states of the Boolean network (as numpy arrays of ones and zeroes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def hamming_dist(x1, x2):\n",
    "    return np.sum(np.abs(x1-x2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define a function `evolve` that takes the network from one Boolean vector state to another Boolean vector state"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def evolve(state):\n",
    "    result = numpy.array(a.transpose().dot(state))\n",
    "    result = numpy.reshape(result, N)\n",
    "    result[result > 0] = 1\n",
    "    result[result == 0] = state[result == 0]\n",
    "    result[result < 0] = 0\n",
    "    return result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Write a function that runs 10,000 simulations of the network. In each simulation, the procedure is:\n",
    "- create a random binary vector of length 12, and call that vector `state`\n",
    "- iteratively call \"evolve\", passing the `state` to evolve and then updating `state` with the return value from `evolve`\n",
    "- check if `state` changes in the last call to `evolve`; if it does not, then you have reached a fixed point; stop iterating\n",
    "- compare the state to the rows of `fixed_points`; for the unique row `i` for which you find a match, increment the element in position `i` of `basin_counts`\n",
    "- print out `basin_counts`"
   ]
  },
  {
   "cell_type": "code",<commit_msg>added comments<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Class 27 - Boolean Networks\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy\n",
    "\n",
    "nodes = ['Cell Size', \n",
    "         'Cln3', \n",
    "         'MBF', \n",
    "         'Clb5,6', \n",
    "         'Mcm1/SFF', \n",
    "         'Swi5', \n",
    "         'Sic1', \n",
    "         'Clb1,2', \n",
    "         'Cdc20&Cdc14', \n",
    "         'Cdh1', \n",
    "         'Cln1,2', \n",
    "         'SBF']\n",
    "\n",
    "N = len(nodes)\n",
    "\n",
    "# define the transition matrix \n",
    "a = numpy.zeros([N, N])\n",
    "a[0,1] = 1\n",
    "a[1,1] = -1\n",
    "a[1,2] = 1\n",
    "a[1,11] = 1\n",
    "a[2,3] = 1\n",
    "a[3,4] = 1\n",
    "a[3,6] = -1\n",
    "a[3,7] = 1\n",
    "a[3,9] = -1\n",
    "a[4,4] = -1\n",
    "a[4,5] = 1\n",
    "a[4,7] = 1\n",
    "a[4,8] = 1\n",
    "a[5,5] = -1\n",
    "a[5,6] = 1\n",
    "a[6,3] = -1\n",
    "a[6,7] = -1\n",
    "a[7,2] = -1\n",
    "a[7,4] = 1\n",
    "a[7,5] = -1\n",
    "a[7,6] = -1\n",
    "a[7,8] = 1\n",
    "a[7,9] = -1\n",
    "a[7,11] = -1\n",
    "a[8,3] = -1\n",
    "a[8,5] = 1\n",
    "a[8,6] = 1\n",
    "a[8,7] = -1\n",
    "a[8,8] = -1\n",
    "a[8,9] = 1\n",
    "a[9,7] = -1\n",
    "a[10,6] = -1\n",
    "a[10,9] = -1\n",
    "a[10,10] = -1\n",
    "a[11,10] = 1\n",
    "a = numpy.matrix(a)\n",
    "\n",
    "# define the matrix of states for the fixed points\n",
    "num_fp = 7\n",
    "fixed_points = numpy.zeros([num_fp, N])\n",
    "fixed_points[0, 6] = 1\n",
    "fixed_points[0, 9] = 1\n",
    "fixed_points[1, 10] = 1\n",
    "fixed_points[1, 11] = 1\n",
    "fixed_points[2, 2] = 1\n",
    "fixed_points[2, 6] = 1\n",
    "fixed_points[2, 9] = 1\n",
    "fixed_points[3, 6] = 1\n",
    "fixed_points[4, 2] = 1\n",
    "fixed_points[4, 6] = 1\n",
    "fixed_points[6, 9] = 1\n",
    "fixed_points = numpy.matrix(fixed_points)\n",
    "\n",
    "basin_counts = numpy.zeros(num_fp)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define a function `hamming.dist` that gives the hamming distance between two states of the Boolean network (as numpy arrays of ones and zeroes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def hamming_dist(x1, x2):\n",
    "    return np.sum(np.abs(x1-x2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define a function `evolve` that takes the network from one Boolean vector state to another Boolean vector state"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def evolve(state):\n",
    "    result = numpy.array(a.transpose().dot(state))\n",
    "    result = numpy.reshape(result, N)\n",
    "    result[result > 0] = 1\n",
    "    result[result == 0] = state[result == 0]\n",
    "    result[result < 0] = 0\n",
    "    return result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Write a function that runs 10,000 simulations of the network. In each simulation, the procedure is:\n",
    "- create a random binary vector of length 12, and call that vector `state` (make sure the zeroth element is set to zero)\n",
    "- iteratively call \"evolve\", passing the `state` to evolve and then updating `state` with the return value from `evolve`\n",
    "- check if `state` changes in the last call to `evolve`; if it does not, then you have reached a fixed point; stop iterating\n",
    "- compare the state to the rows of `fixed_points`; for the unique row `i` for which you find a match, increment the element in position `i` of `basin_counts`\n",
    "- print out `basin_counts`"
   ]
  },
  {
   "cell_type": "code",
{'fraction_non_alphanumeric': 0.23093299937382591, 'fraction_numerical': 0.04733876017532874, 'mean_word_length': 2.128084606345476, 'pattern_counts': {'":': 64, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4023877', 'n_tokens_mistral': 3630, 'n_tokens_neox': 3465, 'n_words': 771}