{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Resampling methods\n\nAn example to compare different resampling methods for age models. Usually, age\nmodels are not sampled on regular depth intervals. It can often be useful to\nhave age models that are regularly sampled. There are several methods in paleos\nfor achieving this including:\n\n- linear resample\n- cubic resample\n- spline resample\n- LOESS resample (local least squares)\n- LOWESS resample (locally weighted least squares)\n\nEach of these will give a different result. Expert discretion should be used\nwhen choosing the best method to use. Each method will have pros and cons.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport pandas as pd\nimport plotly\nimport plotly.graph_objects as go\nimport paleos.common as pcm\nimport paleos.agemodel as pam"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Read in the original age model data\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "age_model = pd.read_csv(\"data/NathanLeckie_1146_GTS12.csv\", header=None)\nage_model.columns=[\"Depth\", \"Age\"]\nage_model = age_model.set_index(\"Depth\").squeeze(\"columns\")\nprint(age_model)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "There are some duplicated depths, let's average out the ages for those depths\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "age_model = age_model.sort_index().dropna()\nage_model = pcm.average_duplicated(age_model)\nprint(age_model)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Define the new depths to interpolate/resample on to\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "new_depths = np.arange(300.0, 650.0, step=10.0)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Do the resampling with a selection of methods\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "linear = pam.resample_linear(age_model, new_depths)\ncubic = pam.resample_cubic(age_model, new_depths)\nspline = pam.resample_spline(age_model, new_depths)\nloess = pam.resample_loess(age_model, new_depths, smoothing_factor=0.4)\nlowess = pam.resample_lowess(age_model, new_depths, smoothing_factor=0.4)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Compare the original age model with the resampled data\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig = go.Figure()\nfig.add_trace(\n    go.Scatter(\n        x=age_model.values, y=age_model.index, mode=\"lines+markers\", name=\"original\"\n    )\n)\nfig.add_trace(\n    go.Scatter(\n        x=linear.values, y=linear.index, mode=\"lines+markers\", name=\"Linear resample\"\n    )\n)\nfig.add_trace(\n    go.Scatter(\n        x=cubic.values, y=cubic.index, mode=\"lines+markers\", name=\"Cubic resample\"\n    )\n)\nfig.add_trace(\n    go.Scatter(\n        x=spline.values, y=spline.index, mode=\"lines+markers\", name=\"Spline resample\"\n    )\n)\nfig.add_trace(\n    go.Scatter(\n        x=loess.values, y=loess.index, mode=\"lines+markers\", name=\"LOESS resample\"\n    )\n)\nfig.add_trace(\n    go.Scatter(\n        x=lowess.values, y=lowess.index, mode=\"lines+markers\", name=\"LOWESS resample\"\n    )\n)\nfig.update_yaxes(autorange=\"reversed\")\nfig.update_layout(width=700, height=1000, margin=go.layout.Margin(l=0, r=0, t=40, b=20))\nfig.update_layout(\n    title=\"Age model resampling\", xaxis_title=\"Age (Ma)\", yaxis_title=\"Depth (m)\"\n)\nplotly.io.show(fig)"
      ]
    }
  ],
  "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.9.13"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}