{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "-pyAjwc4XVz-"
   },
   "source": [
    "# This Jupyter Notebook page describes how to use the trained machine learning bedload model described in the paper \"Development of a Machine Learning Model for River Bedload\" by Hosseiny et al. (in review). \n",
    "\n",
    "This code was produced to predict bedload transport rates using the trained ANN model described in Hosseiny et al. (in review) and may need to be modified for your purposes. It has been adapted from a Google Colab environment. You are responsible for inspecting the code and making the necessary adjustments for your application. You will need to install TensorFlow to use the model. \n",
    "\n",
    "You will need to change the relevant paths to the downloaded model and desired data input files for your own use.\n",
    "\n",
    "Please report any bugs or errors to Claire Masteller at cmasteller ....at.... gmail dot com.\n",
    "\n",
    "The code was created by Hossein Hosseiny on Sept. 24, 2022 and modified for distribution by Claire Masteller in January 2023\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "rDi_VLO5zmSo"
   },
   "source": [
    "# Import required packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "hrQgCt8ZXGYC"
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import tensorflow as tf\n",
    "from tensorflow import keras"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "Zuh6iMJRdDNr",
    "outputId": "d04dbfda-0dd7-4944-b2d4-8ce697d26d80"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mounted at /content/drive\n"
     ]
    }
   ],
   "source": [
    "# Uncomment if Connecting a Google Colab to Google Drive\n",
    "#from google.colab import drive\n",
    "#drive.mount('/content/drive')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "ju20jaLOZkHM"
   },
   "source": [
    "# Prepare the Input Data\n",
    "\n",
    "### Define or Load Input Data\n",
    "\n",
    "The data can be imported by Pandas library. For instance, in case of an Excel file:\n",
    "\n",
    "data = pd.read_excel(data.xlsx)\n",
    "\n",
    "It should be noted that to be compatible with the pretrained model, the order of the variables must be in the following order and have the specified units:\n",
    "\n",
    "[ Width (m),\tBed Slope (m/m), Discharge (m$^3$/s), D16 (m), D50 (m), D84 (m), D90 (m) ]\n",
    "\n",
    "\n",
    "### IMPORTANT: All variables of the data being used for prediction must be within the range of the data used for training the model which is presented in the original study.\n",
    "\n",
    "What follows is an example script which runs the model on a data batch of n=3, representing three measurement sets from different rivers.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "XjO6nFFxZo2J"
   },
   "outputs": [],
   "source": [
    "# Compiles the input data into a single input Data Frame \n",
    "data = pd.DataFrame([[6.68,\t0.0026,\t1.649\t, 0.00268,\t0.01,\t0.021, 0.0273], \n",
    "                     [5.55, 0.0095, 0.58, 0.00042, 0.0024,0.02138, 0.03235],\n",
    "                     [12.76, 0.0091, 9.6, 0.02503, 0.116, 0.247, 0.34]], \n",
    "                    columns=['W', 'S', 'Q', 'D16','D50','D84', 'D90'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "syfWYvaM0ZMJ"
   },
   "source": [
    "Each row in data shows an instance which consists of 7 variables (width, slope, discharge, D16, D50, D84, D90) used as input to the model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 143
    },
    "id": "9261djV4hUhk",
    "outputId": "7cf3b19c-da02-4d79-8920-d0d841cd4193"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "  <div id=\"df-4026e15d-a161-45e0-b7ca-9fe7ae0dfc01\">\n",
       "    <div class=\"colab-df-container\">\n",
       "      <div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>W</th>\n",
       "      <th>S</th>\n",
       "      <th>Q</th>\n",
       "      <th>D16</th>\n",
       "      <th>D50</th>\n",
       "      <th>D84</th>\n",
       "      <th>D90</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>6.68</td>\n",
       "      <td>0.0026</td>\n",
       "      <td>1.649</td>\n",
       "      <td>0.00268</td>\n",
       "      <td>0.0100</td>\n",
       "      <td>0.02100</td>\n",
       "      <td>0.02730</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5.55</td>\n",
       "      <td>0.0095</td>\n",
       "      <td>0.580</td>\n",
       "      <td>0.00042</td>\n",
       "      <td>0.0024</td>\n",
       "      <td>0.02138</td>\n",
       "      <td>0.03235</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>12.76</td>\n",
       "      <td>0.0091</td>\n",
       "      <td>9.600</td>\n",
       "      <td>0.02503</td>\n",
       "      <td>0.1160</td>\n",
       "      <td>0.24700</td>\n",
       "      <td>0.34000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>\n",
       "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-4026e15d-a161-45e0-b7ca-9fe7ae0dfc01')\"\n",
       "              title=\"Convert this dataframe to an interactive table.\"\n",
       "              style=\"display:none;\">\n",
       "        \n",
       "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
       "       width=\"24px\">\n",
       "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
       "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
       "  </svg>\n",
       "      </button>\n",
       "      \n",
       "  <style>\n",
       "    .colab-df-container {\n",
       "      display:flex;\n",
       "      flex-wrap:wrap;\n",
       "      gap: 12px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert {\n",
       "      background-color: #E8F0FE;\n",
       "      border: none;\n",
       "      border-radius: 50%;\n",
       "      cursor: pointer;\n",
       "      display: none;\n",
       "      fill: #1967D2;\n",
       "      height: 32px;\n",
       "      padding: 0 0 0 0;\n",
       "      width: 32px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert:hover {\n",
       "      background-color: #E2EBFA;\n",
       "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
       "      fill: #174EA6;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert {\n",
       "      background-color: #3B4455;\n",
       "      fill: #D2E3FC;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert:hover {\n",
       "      background-color: #434B5C;\n",
       "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
       "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
       "      fill: #FFFFFF;\n",
       "    }\n",
       "  </style>\n",
       "\n",
       "      <script>\n",
       "        const buttonEl =\n",
       "          document.querySelector('#df-4026e15d-a161-45e0-b7ca-9fe7ae0dfc01 button.colab-df-convert');\n",
       "        buttonEl.style.display =\n",
       "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
       "\n",
       "        async function convertToInteractive(key) {\n",
       "          const element = document.querySelector('#df-4026e15d-a161-45e0-b7ca-9fe7ae0dfc01');\n",
       "          const dataTable =\n",
       "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
       "                                                     [key], {});\n",
       "          if (!dataTable) return;\n",
       "\n",
       "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
       "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
       "            + ' to learn more about interactive tables.';\n",
       "          element.innerHTML = '';\n",
       "          dataTable['output_type'] = 'display_data';\n",
       "          await google.colab.output.renderOutput(dataTable, element);\n",
       "          const docLink = document.createElement('div');\n",
       "          docLink.innerHTML = docLinkHtml;\n",
       "          element.appendChild(docLink);\n",
       "        }\n",
       "      </script>\n",
       "    </div>\n",
       "  </div>\n",
       "  "
      ],
      "text/plain": [
       "       W       S      Q      D16     D50      D84      D90\n",
       "0   6.68  0.0026  1.649  0.00268  0.0100  0.02100  0.02730\n",
       "1   5.55  0.0095  0.580  0.00042  0.0024  0.02138  0.03235\n",
       "2  12.76  0.0091  9.600  0.02503  0.1160  0.24700  0.34000"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Prints compiled data frame for inspection\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "-RTqHZwmdXLE"
   },
   "source": [
    "### Normalize and Scale the Input Data\n",
    "\n",
    "The following steps normalize and scale (sc) data to [0-1] by min-max method after logarithmic transformation (base 10) is applied on data. \n",
    "\n",
    "The min-max values for all variables are obtained from the the original datasets. \n",
    "\n",
    "The base10 logarithm (L) of the **min** and **max** values of the data in the original study are estimated for all variables. For instance, normalized, scaled values for river width are obtained by:\n",
    "\n",
    "    $w_{maxL} = log(max(Width))$\n",
    "\n",
    "    $w_{minL} = log(min(Width))$\n",
    "\n",
    "Then the width data can be scaled as:\n",
    "\n",
    "    $W_{sc} = (log(W) -  w_{minL})/(w_{maxL} - w_{minL})$\n",
    "\n",
    "### IMPORTANT: Data must fall within the range of the training data for the model to run reliably. Please see the supplementary material of Hosseiny et al. (in review) and the original dataset as included in this data repository."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "DJClrP6MdQPJ"
   },
   "outputs": [],
   "source": [
    "# Constant values here are associated with the logarithm of the minimum and maximum \n",
    "# of each variable used in the original study\n",
    "\n",
    "data['W_sc'] = (np.log10(data['W']) -  -0.5228787452803376)/(3.008600171761918)\n",
    "data['S_sc'] = (np.log10(data['S']) -  -4.045757490560675)/(3.179296398930892)\n",
    "data['Q_sc'] = (np.log10(data['Q']) -  -4.301029995663981)/(6.932047378606766)\n",
    "data['D16_sc'] = (np.log10(data['D16']) -  -3.8728952016351923)/(2.864121277327687)\n",
    "data['D50_sc'] = (np.log10(data['D50']) -  -3.5228787452803374)/(2.862330186586778)\n",
    "data['D84_sc'] = (np.log10(data['D84']) -  -3.3979400086720375)/(3.1327398382608846)\n",
    "data['D90_sc'] = (np.log10(data['D90']) -  -2.9244530386074694)/(2.8144938134579296)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 143
    },
    "id": "joI2W51KmW-z",
    "outputId": "dee1ede1-0e54-4f3f-be4f-8837d27a5e5f"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "  <div id=\"df-e49fad0a-31e7-45f1-bfcf-8b490b8050d6\">\n",
       "    <div class=\"colab-df-container\">\n",
       "      <div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>W</th>\n",
       "      <th>S</th>\n",
       "      <th>Q</th>\n",
       "      <th>D16</th>\n",
       "      <th>D50</th>\n",
       "      <th>D84</th>\n",
       "      <th>D90</th>\n",
       "      <th>W_sc</th>\n",
       "      <th>S_sc</th>\n",
       "      <th>Q_sc</th>\n",
       "      <th>D16_sc</th>\n",
       "      <th>D50_sc</th>\n",
       "      <th>D84_sc</th>\n",
       "      <th>D90_sc</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>6.68</td>\n",
       "      <td>0.0026</td>\n",
       "      <td>1.649</td>\n",
       "      <td>0.00268</td>\n",
       "      <td>0.0100</td>\n",
       "      <td>0.02100</td>\n",
       "      <td>0.02730</td>\n",
       "      <td>0.447934</td>\n",
       "      <td>0.459451</td>\n",
       "      <td>0.651792</td>\n",
       "      <td>0.454251</td>\n",
       "      <td>0.532042</td>\n",
       "      <td>0.549091</td>\n",
       "      <td>0.483432</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5.55</td>\n",
       "      <td>0.0095</td>\n",
       "      <td>0.580</td>\n",
       "      <td>0.00042</td>\n",
       "      <td>0.0024</td>\n",
       "      <td>0.02138</td>\n",
       "      <td>0.03235</td>\n",
       "      <td>0.421183</td>\n",
       "      <td>0.636456</td>\n",
       "      <td>0.586329</td>\n",
       "      <td>0.173227</td>\n",
       "      <td>0.315509</td>\n",
       "      <td>0.551577</td>\n",
       "      <td>0.509622</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>12.76</td>\n",
       "      <td>0.0091</td>\n",
       "      <td>9.600</td>\n",
       "      <td>0.02503</td>\n",
       "      <td>0.1160</td>\n",
       "      <td>0.24700</td>\n",
       "      <td>0.34000</td>\n",
       "      <td>0.541358</td>\n",
       "      <td>0.630579</td>\n",
       "      <td>0.762156</td>\n",
       "      <td>0.793038</td>\n",
       "      <td>0.903927</td>\n",
       "      <td>0.890798</td>\n",
       "      <td>0.872602</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>\n",
       "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-e49fad0a-31e7-45f1-bfcf-8b490b8050d6')\"\n",
       "              title=\"Convert this dataframe to an interactive table.\"\n",
       "              style=\"display:none;\">\n",
       "        \n",
       "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
       "       width=\"24px\">\n",
       "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
       "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
       "  </svg>\n",
       "      </button>\n",
       "      \n",
       "  <style>\n",
       "    .colab-df-container {\n",
       "      display:flex;\n",
       "      flex-wrap:wrap;\n",
       "      gap: 12px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert {\n",
       "      background-color: #E8F0FE;\n",
       "      border: none;\n",
       "      border-radius: 50%;\n",
       "      cursor: pointer;\n",
       "      display: none;\n",
       "      fill: #1967D2;\n",
       "      height: 32px;\n",
       "      padding: 0 0 0 0;\n",
       "      width: 32px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert:hover {\n",
       "      background-color: #E2EBFA;\n",
       "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
       "      fill: #174EA6;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert {\n",
       "      background-color: #3B4455;\n",
       "      fill: #D2E3FC;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert:hover {\n",
       "      background-color: #434B5C;\n",
       "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
       "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
       "      fill: #FFFFFF;\n",
       "    }\n",
       "  </style>\n",
       "\n",
       "      <script>\n",
       "        const buttonEl =\n",
       "          document.querySelector('#df-e49fad0a-31e7-45f1-bfcf-8b490b8050d6 button.colab-df-convert');\n",
       "        buttonEl.style.display =\n",
       "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
       "\n",
       "        async function convertToInteractive(key) {\n",
       "          const element = document.querySelector('#df-e49fad0a-31e7-45f1-bfcf-8b490b8050d6');\n",
       "          const dataTable =\n",
       "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
       "                                                     [key], {});\n",
       "          if (!dataTable) return;\n",
       "\n",
       "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
       "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
       "            + ' to learn more about interactive tables.';\n",
       "          element.innerHTML = '';\n",
       "          dataTable['output_type'] = 'display_data';\n",
       "          await google.colab.output.renderOutput(dataTable, element);\n",
       "          const docLink = document.createElement('div');\n",
       "          docLink.innerHTML = docLinkHtml;\n",
       "          element.appendChild(docLink);\n",
       "        }\n",
       "      </script>\n",
       "    </div>\n",
       "  </div>\n",
       "  "
      ],
      "text/plain": [
       "       W       S      Q      D16     D50      D84      D90      W_sc  \\\n",
       "0   6.68  0.0026  1.649  0.00268  0.0100  0.02100  0.02730  0.447934   \n",
       "1   5.55  0.0095  0.580  0.00042  0.0024  0.02138  0.03235  0.421183   \n",
       "2  12.76  0.0091  9.600  0.02503  0.1160  0.24700  0.34000  0.541358   \n",
       "\n",
       "       S_sc      Q_sc    D16_sc    D50_sc    D84_sc    D90_sc  \n",
       "0  0.459451  0.651792  0.454251  0.532042  0.549091  0.483432  \n",
       "1  0.636456  0.586329  0.173227  0.315509  0.551577  0.509622  \n",
       "2  0.630579  0.762156  0.793038  0.903927  0.890798  0.872602  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#prints the original and scaled values of the input data\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "7hTB6bMHnDLx"
   },
   "source": [
    "# Bedload Prediction\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "nQIoDRMLOo8O"
   },
   "source": [
    "### Load pre-trained ANN model\n",
    "\n",
    "This step requires the user to specify the local path to the model folder.  The model folder contains four items, a saved_model.pb file, a keras_metadata.pb file, and two folders containing the variable weights and other assets.  The keras.models.load_model compiles the model using the path to the parent folder containing these items."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "T3abFDhFOxFp"
   },
   "outputs": [],
   "source": [
    "model = keras.models.load_model('/content/drive/MyDrive/Colab Notebooks/Sed_ML/Model_Data/BEDLOAD_MODEL_FINAL') "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Run the model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "xoLumK9dnHiS"
   },
   "outputs": [],
   "source": [
    "# model prediction using the scaled input parameters\n",
    "\n",
    "qs_sc = model.predict(data[['W_sc','S_sc','Q_sc', 'D16_sc',\t'D50_sc',\t'D84_sc',\t'D90_sc']])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "dczKwnpcnm8x",
    "outputId": "9a27ab17-ad69-443d-9059-c46c46908c78"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Scaled & Log-transformed Bedloads \n",
      " [[0.68983376]\n",
      " [0.65461224]\n",
      " [0.29142514]]\n"
     ]
    }
   ],
   "source": [
    "# print scaled, log-transformed bedload values predicte by the trained ANN model\n",
    "\n",
    "print(\" Scaled & Log-transformed Bedloads \\n\", qs_sc )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "rDJhg8WloeYf"
   },
   "source": [
    "### Convert scaled predictions back to g/s/m\n",
    "\n",
    "The direct output of the model -similar to the inputs- is log-transformed and scaled to vary between [0-1]. \n",
    "\n",
    "To get the final prediction in g/s/m, it is required to scale back the output of the model using the minimum and maximum measured values for qs that were used to train the ANN in the original study, as shown below:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "cgg1IIT0oVbN",
    "outputId": "4e7313df-0c06-428a-8fd3-0c8215706d0f"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Predicted Log-transformed Bedloads \n",
      " [[1.4959375 ]\n",
      " [1.3702065 ]\n",
      " [0.07373011]]\n"
     ]
    }
   ],
   "source": [
    "# Constant values here are associated with the logarithm of the minimum and maximum \n",
    "# of bedlod data used in the original study \n",
    "\n",
    "log10_qs = qs_sc * (3.5697206171332327) + -0.9665762445130504\n",
    "print(\"Predicted Log-transformed Bedloads \\n\", log10_qs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "brRO5fe_pTN6",
    "outputId": "7ac9e87d-741f-4c9f-fbfb-72c894fc9020"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "qs prediction(g/s/m)\n",
      " [[31.328346 ]\n",
      " [23.453436 ]\n",
      " [ 1.1850321]]\n"
     ]
    }
   ],
   "source": [
    "qs = 10 ** log10_qs\n",
    "print('qs prediction(g/s/m)\\n',qs )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "k2bkWgrvqMrQ"
   },
   "outputs": [],
   "source": [
    "data['qs-g/s/m'] = qs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 143
    },
    "id": "6bh7Zb7vsYER",
    "outputId": "c5086a5c-a31e-459f-b020-756b0ab45d49"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "  <div id=\"df-dff337e1-173d-4c12-8173-ec56043cfe52\">\n",
       "    <div class=\"colab-df-container\">\n",
       "      <div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>W</th>\n",
       "      <th>S</th>\n",
       "      <th>Q</th>\n",
       "      <th>D16</th>\n",
       "      <th>D50</th>\n",
       "      <th>D84</th>\n",
       "      <th>D90</th>\n",
       "      <th>W_sc</th>\n",
       "      <th>S_sc</th>\n",
       "      <th>Q_sc</th>\n",
       "      <th>D16_sc</th>\n",
       "      <th>D50_sc</th>\n",
       "      <th>D84_sc</th>\n",
       "      <th>D90_sc</th>\n",
       "      <th>qs-g/s/m</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>6.68</td>\n",
       "      <td>0.0026</td>\n",
       "      <td>1.649</td>\n",
       "      <td>0.00268</td>\n",
       "      <td>0.0100</td>\n",
       "      <td>0.02100</td>\n",
       "      <td>0.02730</td>\n",
       "      <td>0.447934</td>\n",
       "      <td>0.459451</td>\n",
       "      <td>0.651792</td>\n",
       "      <td>0.454251</td>\n",
       "      <td>0.532042</td>\n",
       "      <td>0.549091</td>\n",
       "      <td>0.483432</td>\n",
       "      <td>31.328346</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5.55</td>\n",
       "      <td>0.0095</td>\n",
       "      <td>0.580</td>\n",
       "      <td>0.00042</td>\n",
       "      <td>0.0024</td>\n",
       "      <td>0.02138</td>\n",
       "      <td>0.03235</td>\n",
       "      <td>0.421183</td>\n",
       "      <td>0.636456</td>\n",
       "      <td>0.586329</td>\n",
       "      <td>0.173227</td>\n",
       "      <td>0.315509</td>\n",
       "      <td>0.551577</td>\n",
       "      <td>0.509622</td>\n",
       "      <td>23.453436</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>12.76</td>\n",
       "      <td>0.0091</td>\n",
       "      <td>9.600</td>\n",
       "      <td>0.02503</td>\n",
       "      <td>0.1160</td>\n",
       "      <td>0.24700</td>\n",
       "      <td>0.34000</td>\n",
       "      <td>0.541358</td>\n",
       "      <td>0.630579</td>\n",
       "      <td>0.762156</td>\n",
       "      <td>0.793038</td>\n",
       "      <td>0.903927</td>\n",
       "      <td>0.890798</td>\n",
       "      <td>0.872602</td>\n",
       "      <td>1.185032</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>\n",
       "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-dff337e1-173d-4c12-8173-ec56043cfe52')\"\n",
       "              title=\"Convert this dataframe to an interactive table.\"\n",
       "              style=\"display:none;\">\n",
       "        \n",
       "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
       "       width=\"24px\">\n",
       "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
       "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
       "  </svg>\n",
       "      </button>\n",
       "      \n",
       "  <style>\n",
       "    .colab-df-container {\n",
       "      display:flex;\n",
       "      flex-wrap:wrap;\n",
       "      gap: 12px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert {\n",
       "      background-color: #E8F0FE;\n",
       "      border: none;\n",
       "      border-radius: 50%;\n",
       "      cursor: pointer;\n",
       "      display: none;\n",
       "      fill: #1967D2;\n",
       "      height: 32px;\n",
       "      padding: 0 0 0 0;\n",
       "      width: 32px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert:hover {\n",
       "      background-color: #E2EBFA;\n",
       "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
       "      fill: #174EA6;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert {\n",
       "      background-color: #3B4455;\n",
       "      fill: #D2E3FC;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert:hover {\n",
       "      background-color: #434B5C;\n",
       "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
       "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
       "      fill: #FFFFFF;\n",
       "    }\n",
       "  </style>\n",
       "\n",
       "      <script>\n",
       "        const buttonEl =\n",
       "          document.querySelector('#df-dff337e1-173d-4c12-8173-ec56043cfe52 button.colab-df-convert');\n",
       "        buttonEl.style.display =\n",
       "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
       "\n",
       "        async function convertToInteractive(key) {\n",
       "          const element = document.querySelector('#df-dff337e1-173d-4c12-8173-ec56043cfe52');\n",
       "          const dataTable =\n",
       "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
       "                                                     [key], {});\n",
       "          if (!dataTable) return;\n",
       "\n",
       "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
       "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
       "            + ' to learn more about interactive tables.';\n",
       "          element.innerHTML = '';\n",
       "          dataTable['output_type'] = 'display_data';\n",
       "          await google.colab.output.renderOutput(dataTable, element);\n",
       "          const docLink = document.createElement('div');\n",
       "          docLink.innerHTML = docLinkHtml;\n",
       "          element.appendChild(docLink);\n",
       "        }\n",
       "      </script>\n",
       "    </div>\n",
       "  </div>\n",
       "  "
      ],
      "text/plain": [
       "       W       S      Q      D16     D50      D84      D90      W_sc  \\\n",
       "0   6.68  0.0026  1.649  0.00268  0.0100  0.02100  0.02730  0.447934   \n",
       "1   5.55  0.0095  0.580  0.00042  0.0024  0.02138  0.03235  0.421183   \n",
       "2  12.76  0.0091  9.600  0.02503  0.1160  0.24700  0.34000  0.541358   \n",
       "\n",
       "       S_sc      Q_sc    D16_sc    D50_sc    D84_sc    D90_sc   qs-g/s/m  \n",
       "0  0.459451  0.651792  0.454251  0.532042  0.549091  0.483432  31.328346  \n",
       "1  0.636456  0.586329  0.173227  0.315509  0.551577  0.509622  23.453436  \n",
       "2  0.630579  0.762156  0.793038  0.903927  0.890798  0.872602   1.185032  "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# print final data frame with original input values, scaled values, and bedload transport prediction, qs-g/s/m\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "eWEGThxQsrp6"
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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": 1
}
