{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import mahotas as mh\n",
    "import numpy as np\n",
    "from sklearn.datasets import load_digits\n",
    "import matplotlib.pyplot as plt\n",
    "from tpot import TPOT\n",
    "from sklearn.cross_validation import train_test_split\n",
    "import dautil as dl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "context = dl.nb.Context('extracting_texture')\n",
    "lr = dl.nb.LatexRenderer(chapter=11, start=5, context=context)\n",
    "lr.render(r' C_{\\Delta x, \\Delta y}(i,j)=\\sum_{p=1}^n\\sum_{q=1}^m\\begin{cases} 1, &amp; \\text{if }I(p,q)=i\\text{ and }I(p+\\Delta x,q+\\Delta y)=j \\\\ 0, &amp; \\text{otherwise}\\end{cases}')\n",
    "lr.render(r'''\\begin{align}\n",
    " Angular \\text{ } 2nd \\text{ } Moment &amp;= \\sum_{i} \\sum_{j} p[i,j]^{2}\\\\\n",
    " Contrast &amp;= \\sum_{n=0}^{Ng-1} n^{2} \\left \\{ \\sum_{i=1}^{Ng} \\sum_{j=1}^{Ng} p[i,j] \\right \\} \\text{, where } |i-j|=n\\\\\n",
    " Correlation &amp;= \\frac{\\sum_{i=1}^{Ng} \\sum_{j=1}^{Ng}(ij)p[i,j] - \\mu_x \\mu_y}{\\sigma_x \\sigma_y} \\\\\n",
    " Entropy &amp;= -\\sum_{i}\\sum_{j} p[i,j] log(p[i,j])\\\\\n",
    "\\end{align}''')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "digits = load_digits()\n",
    "X = digits.data.copy()\n",
    "\n",
    "for i, img in enumerate(digits.images):\n",
    "    np.append(X[i], mh.features.haralick(\n",
    "        img.astype(np.uint8)).ravel())\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(\n",
    "    X, digits.target, train_size=0.75)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "tpot = TPOT(generations=6, population_size=101,\n",
    "            random_state=46, verbosity=2)\n",
    "tpot.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "context = dl.nb.Context('extracting_texture')\n",
    "dl.nb.RcWidget(context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print('Score {:.2f}'.format(tpot.score(X_train, y_train, X_test, y_test)))\n",
    "dl.plotting.img_show(plt.gca(), digits.images[0])\n",
    "plt.title('Original Image')\n",
    "\n",
    "plt.figure()\n",
    "dl.plotting.img_show(plt.gca(), digits.data[0].reshape((8, 8)))\n",
    "plt.title('Core Features')\n",
    "plt.figure()\n",
    "dl.plotting.img_show(plt.gca(), mh.features.haralick(\n",
    "    digits.images[0].astype(np.uint8)))\n",
    "plt.title('Haralick Features')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "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.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
