{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nLearn algorithm and customize your input raster without writing it on disk\n=============================================================================\n\nThis example shows how to customize your raster (ndvi, smooth signal...) in the \nlearning process to avoi generate a new raster.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Import librairies\n-------------------------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from museotoolbox.ai import SuperLearner\nfrom museotoolbox.processing import extract_ROI\nfrom museotoolbox import datasets\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn import metrics"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Load HistoricalMap dataset\n-------------------------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "raster,vector = datasets.load_historical_data(low_res=True)\nfield = 'Class'"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Initialize Random-Forest and metrics\n--------------------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "classifier = RandomForestClassifier(random_state=12,n_jobs=1)\n\nkappa = metrics.make_scorer(metrics.cohen_kappa_score)\nf1_mean = metrics.make_scorer(metrics.f1_score,average='micro')\nscoring = dict(kappa=kappa,f1_mean=f1_mean,accuracy='accuracy')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Start learning\n---------------------------\nsklearn will compute different metrics, but will keep best results from kappa (refit='kappa')\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "SL = SuperLearner(classifier=classifier,param_grid=dict(n_estimators=[10]),n_jobs=1,verbose=1)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create or use custom function\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "def reduceBands(X,bandToKeep=[0,2]):\n    # this function get the first and the last band\n    X=X[:,bandToKeep].reshape(-1,len(bandToKeep))\n    return X\n\n# add this function to learnAndPredict class\nSL.customize_array(reduceBands)\n\n# if you learn from vector, refit according to the f1_mean\nX,y = extract_ROI(raster,vector,field)\nSL.fit(X,y,cv=2,scoring=scoring,refit='f1_mean')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Read the model\n-------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(SL.model)\nprint(SL.model.cv_results_)\nprint(SL.model.best_score_)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Get F1 for every class from best params\n-----------------------------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "for stats in SL.get_stats_from_cv(confusion_matrix=False,F1=True):\n    print(stats['F1'])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Get each confusion matrix from folds\n-----------------------------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "for stats in SL.get_stats_from_cv(confusion_matrix=True):\n    print(stats['confusion_matrix'])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Save each confusion matrix from folds\n-----------------------------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "SL.save_cm_from_cv('/tmp/testMTB/',prefix='RS50_')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Predict map\n---------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "SL.predict_image(raster,'/tmp/classification.tif',\n                  higher_confidence='/tmp/confidence.tif',\n                  confidence_per_class='/tmp/confidencePerClass.tif')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot example\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from matplotlib import pyplot as plt\nfrom osgeo import gdal\nsrc=gdal.Open('/tmp/classification.tif')\nplt.imshow(src.GetRasterBand(1).ReadAsArray(),cmap=plt.get_cmap('tab20'))\nplt.axis('off')\nplt.show()"
      ]
    }
  ],
  "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.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}