{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nGet phenology metrics\n================================================================================\nThis example shows how to smooth a time series using linear, cubic interpolation or savitzski_golay.\nThen how to get phenology metrics (start of season, end of season, length of season and amplitude)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "import library\n---------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nfrom museopheno import time_series"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create example values\n----------------------------------------------------\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Initially these values came from the LCHloC spectral index\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "x = np.asarray([3.4825737, 4.27786  , 5.0373, 4.7196426, 4.1233397, 4.0338645,2.7735472])    \n\ndates = [20180429, 20180513, 20180708, 20180815, 20180915, 20181015, 20181115]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Resample to every 5 days\n----------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "dates_5days = time_series.generate_temporal_sampling(dates[0],dates[-1],5)\n    \nts = time_series.SmoothSignal(dates=dates,output_dates = dates_5days)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "savitzski golay\n----------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Savitzski golay can use several interpolation type before smoothing the trend"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Savitzski golay from linear interpolation\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "x_savgol_linear = ts.savitzski_golay(x,window_length=9,polyorder=1,interpolation_params=dict(kind='linear'))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Savitzski golay from cubic interpolation\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "x_savgol_cubic = ts.savitzski_golay(x,window_length=9,polyorder=5,interpolation_params=dict(kind='cubic'))\n\n\nsos_cub,eos_cub=time_series.get_phenology_metrics(x_savgol_cubic,sos=0.5,eos=0.5)[0]\nsos_lin,eos_lin=time_series.get_phenology_metrics(x_savgol_linear,sos=0.5,eos=0.5)[0]\n#x_doublelogistic = ts.doubleLogistic(x,t01=10,t02=120)\n#sos_dl,eos_dl=time_series.getPhenologyMetrics(x_doublelogistic,sos=0.3,eos=0.8)\n#################\n# Plot results\nfrom matplotlib import pyplot as plt\nplt.plot_date(ts.init_datetime,x,'x',color='C0',markersize=8,label='Original data')\n\nplt.plot_date(ts.output_datetime,x_savgol_linear.flatten(),'--',linewidth=3,color='C3',label='savitzski golay from linear interpolation',alpha=.5)    \nplt.plot_date(ts.output_datetime,x_savgol_cubic.flatten(),'--',linewidth=3,color='black',label='savitzski golay from cubic interpolation',alpha=.5)\n#plt.plot_date(ts.output_datetime,x_doublelogistic.flatten(),'-',linewidth=3,color='C1',label='double logistic',alpha=.5) \n\nplt.plot_date(ts.output_datetime[sos_lin],x_savgol_linear[:,sos_lin],'>',markersize=12,color='C3',alpha=.8)\nplt.plot_date(ts.output_datetime[eos_lin],x_savgol_linear[:,eos_lin],'<',markersize=12,color='C3',alpha=.8)\n\nplt.plot_date(ts.output_datetime[sos_cub],x_savgol_cubic[:,sos_cub],'>',markersize=12,color='black',alpha=.8)\nplt.plot_date(ts.output_datetime[eos_cub],x_savgol_cubic[:,eos_cub],'<',markersize=12,color='black',alpha=.8)\n\n#plt.plot_date(ts.output_datetime[sos_dl],x_doublelogistic[sos_dl],'>',markersize=12,color='C1',alpha=.8,label='start of season')\n#plt.plot_date(ts.output_datetime[eos_dl],x_doublelogistic[eos_dl],'<',markersize=12,color='C1',alpha=.8,label='end of season')\n\n\nplt.legend(loc='best')"
      ]
    }
  ],
  "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
}