{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "<p align=\"center\">\n",
    "    <img src=\"https://github.com/GeostatsGuy/GeostatsPy/blob/master/TCG_color_logo.png?raw=true\" width=\"220\" height=\"240\" />\n",
    "\n",
    "</p>\n",
    "\n",
    "## Interactive PreDrill Prediction Demonstration\n",
    "\n",
    "\n",
    "### Michael Pyrcz, Associate Professor, University of Texas at Austin \n",
    "\n",
    "##### [Twitter](https://twitter.com/geostatsguy) | [GitHub](https://github.com/GeostatsGuy) | [Website](http://michaelpyrcz.com) | [GoogleScholar](https://scholar.google.com/citations?user=QVZ20eQAAAAJ&hl=en&oi=ao) | [Book](https://www.amazon.com/Geostatistical-Reservoir-Modeling-Michael-Pyrcz/dp/0199731446) | [YouTube](https://www.youtube.com/channel/UCLqEr-xV-ceHdXXXrTId5ig)  | [LinkedIn](https://www.linkedin.com/in/michael-pyrcz-61a648a1)\n",
    "\n",
    "\n",
    "### The Interactive Workflow\n",
    "\n",
    "Here's a simple workflow for calculating a predrill (prior to sampling) spatial prediction with simple kriging.\n",
    "\n",
    "#### PreDrill Estimation\n",
    "\n",
    "It is common to make spatial predictions with uncertainty at an unsampled location.  This is done for the following purposes:\n",
    "\n",
    "* predrill assessments to cross validate the current spatial model\n",
    "\n",
    "* identification of the next locations sampling for information and subsurface resource recovery\n",
    "\n",
    "This demonstration will help reinforce the practical implications of the choice of variogram model parameters.\n",
    "\n",
    "#### Spatial Estimation\n",
    "\n",
    "Consider the case of making an estimate at some unsampled location, $𝑧(\\bf{u}_0)$, where $z$ is the property of interest (e.g. porosity etc.) and $𝐮_0$ is a location vector describing the unsampled location.\n",
    "\n",
    "How would you do this given data, $𝑧(\\bf{𝐮}_1)$, $𝑧(\\bf{𝐮}_2)$, and $𝑧(\\bf{𝐮}_3)$?\n",
    "\n",
    "It would be natural to use a set of linear weights to formulate the estimator given the available data.\n",
    "\n",
    "\\begin{equation}\n",
    "z^{*}(\\bf{u}) = \\sum^{n}_{\\alpha = 1} \\lambda_{\\alpha} z(\\bf{u}_{\\alpha})\n",
    "\\end{equation}\n",
    "\n",
    "We could add an unbiasedness constraint to impose the sum of the weights equal to one.  What we will do is assign the remainder of the weight (one minus the sum of weights) to the global average; therefore, if we have no informative data we will estimate with the global average of the property of interest.\n",
    "\n",
    "\\begin{equation}\n",
    "z^{*}(\\bf{u}) = \\sum^{n}_{\\alpha = 1} \\lambda_{\\alpha} z(\\bf{u}_{\\alpha}) + \\left(1-\\sum^{n}_{\\alpha = 1} \\lambda_{\\alpha} \\right) \\overline{z}\n",
    "\\end{equation}\n",
    "\n",
    "We will make a stationarity assumption, so let's assume that we are working with residuals, $y$. \n",
    "\n",
    "\\begin{equation}\n",
    "y^{*}(\\bf{u}) = z^{*}(\\bf{u}) - \\overline{z}(\\bf{u})\n",
    "\\end{equation}\n",
    "\n",
    "If we substitute this form into our estimator the estimator simplifies, since the mean of the residual is zero.\n",
    "\n",
    "\\begin{equation}\n",
    "y^{*}(\\bf{u}) = \\sum^{n}_{\\alpha = 1} \\lambda_{\\alpha} y(\\bf{u}_{\\alpha})\n",
    "\\end{equation}\n",
    "\n",
    "while satisfying the unbaisedness constraint.  \n",
    "\n",
    "#### Kriging\n",
    "\n",
    "Now the next question is what weights should we use?  \n",
    "\n",
    "We could use equal weighting, $\\lambda = \\frac{1}{n}$, and the estimator would be the average of the local data applied for the spatial estimate. This would not be very informative.\n",
    "\n",
    "We could assign weights considering the spatial context of the data and the estimate:\n",
    "\n",
    "* **spatial continuity** as quantified by the variogram (and covariance function)\n",
    "* **redundancy** the degree of spatial continuity between all of the available data with themselves \n",
    "* **closeness** the degree of spatial continuity between the avaiable data and the estimation location\n",
    "\n",
    "The kriging approach accomplishes this, calculating the best linear unbiased weights for the local data to estimate at the unknown location.  The derivation of the kriging system and the resulting linear set of equations is available in the lecture notes.  Furthermore kriging provides a measure of the accuracy of the estimate!  This is the kriging estimation variance (sometimes just called the kriging variance).\n",
    "\n",
    "\\begin{equation}\n",
    "\\sigma^{2}_{E}(\\bf{u}) = C(0) - \\sum^{n}_{\\alpha = 1} \\lambda_{\\alpha} C(\\bf{u}_0 - \\bf{u}_{\\alpha})\n",
    "\\end{equation}\n",
    "\n",
    "What is 'best' about this estimate? Kriging estimates are best in that they minimize the above estimation variance. \n",
    "\n",
    "#### Properties of Kriging\n",
    "\n",
    "Here are some important properties of kriging:\n",
    "\n",
    "* **Exact interpolator** - kriging estimates with the data values at the data locations\n",
    "* **Kriging variance** can be calculated before getting the sample information, as the kriging estimation variance is not dependent on the values of the data nor the kriging estimate, i.e. the kriging estimator is homoscedastic. \n",
    "* **Spatial context** - kriging takes into account, furthermore to the statements on spatial continuity, closeness and redundancy we can state that kriging accounts for the configuration of the data and structural continuity of the variable being estimated.\n",
    "* **Scale** - kriging may be generalized to account for the support volume of the data and estimate. We will cover this later.\n",
    "* **Multivariate** - kriging may be generalized to account for multiple secondary data in the spatial estimate with the cokriging system. We will cover this later.\n",
    "* **Smoothing effect** of kriging can be forecast. We will use this to build stochastic simulations later.\n",
    "\n",
    "#### Spatial Continuity \n",
    "\n",
    "**Spatial Continuity** is the correlation between values over distance.\n",
    "\n",
    "* No spatial continuity – no correlation between values over distance, random values at each location in space regardless of separation distance.\n",
    "\n",
    "* Homogenous phenomenon have perfect spatial continuity, since all values as the same (or very similar) they are correlated. \n",
    "\n",
    "We need a statistic to quantify spatial continuity! A convenient method is the Semivariogram.\n",
    "\n",
    "#### The Semivariogram\n",
    "\n",
    "Function of difference over distance.\n",
    "\n",
    "* The expected (average) squared difference between values separated by a lag distance vector (distance and direction), $h$:\n",
    "\n",
    "\\begin{equation}\n",
    "\\gamma(\\bf{h}) = \\frac{1}{2 N(\\bf{h})} \\sum^{N(\\bf{h})}_{\\alpha=1} (z(\\bf{u}_\\alpha) - z(\\bf{u}_\\alpha + \\bf{h}))^2  \n",
    "\\end{equation}\n",
    "\n",
    "where $z(\\bf{u}_\\alpha)$ and $z(\\bf{u}_\\alpha + \\bf{h})$ are the spatial sample values at tail and head locations of the lag vector respectively.\n",
    "\n",
    "* Calculated over a suite of lag distances to obtain a continuous function.\n",
    "\n",
    "* the $\\frac{1}{2}$ term converts a variogram into a semivariogram, but in practice the term variogram is used instead of semivariogram.\n",
    "* We prefer the semivariogram because it relates directly to the covariance function, $C_x(\\bf{h})$ and univariate variance, $\\sigma^2_x$:\n",
    "\n",
    "\\begin{equation}\n",
    "C_x(\\bf{h}) = \\sigma^2_x - \\gamma(\\bf{h})\n",
    "\\end{equation}\n",
    "\n",
    "Note the correlogram is related to the covariance function as:\n",
    "\n",
    "\\begin{equation}\n",
    "\\rho_x(\\bf{h}) = \\frac{C_x(\\bf{h})}{\\sigma^2_x}\n",
    "\\end{equation}\n",
    "\n",
    "The correlogram provides of function of the $\\bf{h}-\\bf{h}$ scatter plot correlation vs. lag offset $\\bf{h}$.  \n",
    "\n",
    "\\begin{equation}\n",
    "-1.0 \\le \\rho_x(\\bf{h}) \\le 1.0\n",
    "\\end{equation}\n",
    "\n",
    "#### Objective \n",
    "\n",
    "In the PGE 383: Stochastic Subsurface Modeling class I want to provide hands-on experience with building subsurface modeling workflows. Python provides an excellent vehicle to accomplish this. I have coded a package called GeostatsPy with GSLIB: Geostatistical Library (Deutsch and Journel, 1998) functionality that provides basic building blocks for building subsurface modeling workflows. \n",
    "\n",
    "The objective is to remove the hurdles of subsurface modeling workflow construction by providing building blocks and sufficient examples. This is not a coding class per se, but we need the ability to 'script' workflows working with numerical methods.    \n",
    "\n",
    "#### Getting Started\n",
    "\n",
    "Here's the steps to get setup in Python with the GeostatsPy package:\n",
    "\n",
    "1. Install Anaconda 3 on your machine (https://www.anaconda.com/download/). \n",
    "2. From Anaconda Navigator (within Anaconda3 group), go to the environment tab, click on base (root) green arrow and open a terminal. \n",
    "3. In the terminal type: pip install geostatspy. \n",
    "4. Open Jupyter and in the top block get started by copy and pasting the code block below from this Jupyter Notebook to start using the geostatspy functionality. \n",
    "\n",
    "You will need to copy the data file to your working directory.  They are available here:\n",
    "\n",
    "* Tabular data - sample_data.csv at https://git.io/fh4gm.\n",
    "\n",
    "There are exampled below with these functions. You can go here to see a list of the available functions, https://git.io/fh4eX, other example workflows and source code. \n",
    "\n",
    "#### Load the required libraries\n",
    "\n",
    "The following code loads the required libraries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import geostatspy.GSLIB as GSLIB                       # GSLIB utilies, visualization and wrapper\n",
    "import geostatspy.geostats as geostats                 # GSLIB methods convert to Python    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will also need some standard packages. These should have been installed with Anaconda 3."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import os                                               # to set current working directory \n",
    "import sys                                              # supress output to screen for interactive variogram modeling\n",
    "import io\n",
    "import numpy as np                                      # arrays and matrix math\n",
    "import pandas as pd                                     # DataFrames\n",
    "import matplotlib.pyplot as plt                         # plotting\n",
    "from matplotlib.pyplot import cm                        # color maps\n",
    "from matplotlib.patches import Ellipse                  # plot an ellipse\n",
    "import math                                             # sqrt operator\n",
    "from scipy.stats import norm\n",
    "from ipywidgets import interactive                      # widgets and interactivity\n",
    "from ipywidgets import widgets                            \n",
    "from ipywidgets import Layout\n",
    "from ipywidgets import Label\n",
    "from ipywidgets import VBox, HBox"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you get a package import error, you may have to first install some of these packages. This can usually be accomplished by opening up a command window on Windows and then typing 'python -m pip install [package-name]'. More assistance is available with the respective package docs.  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Set the working directory\n",
    "\n",
    "I always like to do this so I don't lose files and to simplify subsequent read and writes (avoid including the full address each time).  Also, in this case make sure to place the required (see above) GSLIB executables in this directory or a location identified in the environmental variable *Path*."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#os.chdir(\"d:/PGE383\")                                   # set the working directory"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Loading Tabular Data\n",
    "\n",
    "Here's the command to load our comma delimited data file in to a Pandas' DataFrame object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>X</th>\n",
       "      <th>Y</th>\n",
       "      <th>Facies</th>\n",
       "      <th>Porosity</th>\n",
       "      <th>Perm</th>\n",
       "      <th>AI</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>138</th>\n",
       "      <td>280.0</td>\n",
       "      <td>409.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.136716</td>\n",
       "      <td>17.126888</td>\n",
       "      <td>4728.067903</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>132</th>\n",
       "      <td>230.0</td>\n",
       "      <td>749.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.204587</td>\n",
       "      <td>1665.384124</td>\n",
       "      <td>3994.709069</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>300.0</td>\n",
       "      <td>500.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.159891</td>\n",
       "      <td>105.797436</td>\n",
       "      <td>3658.811458</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>198</th>\n",
       "      <td>620.0</td>\n",
       "      <td>389.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.100296</td>\n",
       "      <td>7.578784</td>\n",
       "      <td>6397.384419</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>207</th>\n",
       "      <td>370.0</td>\n",
       "      <td>689.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.142434</td>\n",
       "      <td>218.419949</td>\n",
       "      <td>5072.154879</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         X      Y  Facies  Porosity         Perm           AI\n",
       "138  280.0  409.0     1.0  0.136716    17.126888  4728.067903\n",
       "132  230.0  749.0     1.0  0.204587  1665.384124  3994.709069\n",
       "14   300.0  500.0     1.0  0.159891   105.797436  3658.811458\n",
       "198  620.0  389.0     0.0  0.100296     7.578784  6397.384419\n",
       "207  370.0  689.0     1.0  0.142434   218.419949  5072.154879"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#df = pd.read_csv(\"sample_data_MV_biased.csv\")           # read a .csv file in as a DataFrame\n",
    "df = pd.read_csv(\"https://raw.githubusercontent.com/GeostatsGuy/GeoDataSets/master/sample_data_MV_biased.csv\") # read the data from Dr. Pyrcz's GitHub repository\n",
    "df = df.iloc[:,1:]\n",
    "df = df.sample(frac = 0.2,random_state=73073)\n",
    "#print(df.iloc[0:5,:])                                  # display first 4 samples in the table as a preview\n",
    "df.head()                                               # we could also use this command for a table preview "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will work with all facies pooled together. I wanted to simplify this workflow and focus more on spatial continuity direction detection. Finally, by not using facies we do have more samples to support our statistical inference. Most often facies are essential in the subsurface model. Don't worry we will check if this is reasonable in a bit.   \n",
    "\n",
    "You are welcome to repeat this workflow on a by-facies basis.  The following code could be used to build DataFrames ('df_sand' and 'df_shale') for each facies.\n",
    "\n",
    "```p\n",
    "df_sand = pd.DataFrame.copy(df[df['Facies'] == 1]).reset_index()  # copy only 'Facies' = sand records\n",
    "df_shale = pd.DataFrame.copy(df[df['Facies'] == 0]).reset_index() # copy only 'Facies' = shale records\n",
    "```\n",
    "\n",
    "Let's look at summary statistics for all facies combined:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>X</th>\n",
       "      <td>74.0</td>\n",
       "      <td>471.891892</td>\n",
       "      <td>282.603724</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>232.500000</td>\n",
       "      <td>405.000000</td>\n",
       "      <td>765.000000</td>\n",
       "      <td>990.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Y</th>\n",
       "      <td>74.0</td>\n",
       "      <td>489.932432</td>\n",
       "      <td>291.512933</td>\n",
       "      <td>9.000000</td>\n",
       "      <td>224.000000</td>\n",
       "      <td>484.500000</td>\n",
       "      <td>729.000000</td>\n",
       "      <td>999.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Facies</th>\n",
       "      <td>74.0</td>\n",
       "      <td>0.594595</td>\n",
       "      <td>0.494322</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Porosity</th>\n",
       "      <td>74.0</td>\n",
       "      <td>0.125834</td>\n",
       "      <td>0.031093</td>\n",
       "      <td>0.062169</td>\n",
       "      <td>0.102895</td>\n",
       "      <td>0.126264</td>\n",
       "      <td>0.148840</td>\n",
       "      <td>0.204587</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Perm</th>\n",
       "      <td>74.0</td>\n",
       "      <td>73.040451</td>\n",
       "      <td>214.164084</td>\n",
       "      <td>0.300430</td>\n",
       "      <td>2.081215</td>\n",
       "      <td>10.385010</td>\n",
       "      <td>50.841029</td>\n",
       "      <td>1665.384124</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AI</th>\n",
       "      <td>74.0</td>\n",
       "      <td>4853.532434</td>\n",
       "      <td>1100.612966</td>\n",
       "      <td>1981.177309</td>\n",
       "      <td>4074.882537</td>\n",
       "      <td>4722.574276</td>\n",
       "      <td>5542.402947</td>\n",
       "      <td>7561.250336</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          count         mean          std          min          25%  \\\n",
       "X          74.0   471.891892   282.603724     0.000000   232.500000   \n",
       "Y          74.0   489.932432   291.512933     9.000000   224.000000   \n",
       "Facies     74.0     0.594595     0.494322     0.000000     0.000000   \n",
       "Porosity   74.0     0.125834     0.031093     0.062169     0.102895   \n",
       "Perm       74.0    73.040451   214.164084     0.300430     2.081215   \n",
       "AI         74.0  4853.532434  1100.612966  1981.177309  4074.882537   \n",
       "\n",
       "                  50%          75%          max  \n",
       "X          405.000000   765.000000   990.000000  \n",
       "Y          484.500000   729.000000   999.000000  \n",
       "Facies       1.000000     1.000000     1.000000  \n",
       "Porosity     0.126264     0.148840     0.204587  \n",
       "Perm        10.385010    50.841029  1665.384124  \n",
       "AI        4722.574276  5542.402947  7561.250336  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.describe().transpose()                               # summary table of sand only DataFrame statistics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's pick some min and max values for the color bars on our plots."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "pormin = 0.06; pormax = 0.18\n",
    "permmin = 0.0; permmax = 1500.0\n",
    "poravg = np.average(df['Porosity'].values)\n",
    "porvar = np.var(df['Porosity'].values)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Inspection of Posted Data\n",
    "\n",
    "Data visualization is very useful to detect patterns. Our brains are very good at pattern detection. I promote quantitative methods and recognize issues with cognitive bias, but it is important to recognize the value is expert intepretation based on data visualization.\n",
    "\n",
    "* This data visualization will also be important to assist with parameter selection for the quantitative methods later.\n",
    "\n",
    "Let's plot the location maps of normal score transforms of porosity and permeability for all facies. We will also include a cross plot of the nscore permeability vs. porosity colored by facies to aid with comparison in spatial features between the porosity and permeability data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 76132 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 5 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "cmap = plt.cm.plasma                                    # set the color map\n",
    "plt.subplot(131)                                        # location map of normal score transform of porosity\n",
    "GSLIB.locmap_st(df,'X','Y','Porosity',0,1000,0,1000,pormin,pormax,'Porosity - All Facies','X (m)','Y (m)','Nscore Porosity',cmap)\n",
    "\n",
    "plt.subplot(132)                                        # location map of normal score transform of permeability\n",
    "GSLIB.locmap_st(df,'X','Y','Perm',0,1000,0,1000,permmin,permmax,'Permeability - All Facies','X (m)','Y (m)','Nscore Permeability',cmap)\n",
    "\n",
    "plt.subplot(133)\n",
    "facies = df['Facies'].values +0.01                      # normal score porosity / permeability scatter plot color coded by facies\n",
    "plt.scatter(df['Porosity'],df['Perm'],c = facies,edgecolor = 'black',cmap = plt.cm.inferno)\n",
    "#plt.plot([-3,3],[-3,3],color = 'black')\n",
    "plt.xlabel(r'Porosity (%)')\n",
    "plt.ylabel(r'Permeability (mD)')\n",
    "plt.title('Permeability vs. Porosity')\n",
    "plt.xlim([pormin,pormax])\n",
    "plt.ylim([permmin,permmax])\n",
    "plt.grid(True)\n",
    "\n",
    "plt.subplots_adjust(left=0.0, bottom=0.0, right=2.0, top=0.8, wspace=0.5, hspace=0.3)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Simple, Simple Kriging Function\n",
    "\n",
    "Let's write a fast Python function to take data points and unknown location and provide the:\n",
    "\n",
    "* **simple kriging estimate**\n",
    "\n",
    "* **simple kriging variance / estimation variance**\n",
    "\n",
    "* **simple kriging weights**\n",
    "\n",
    "This provides a fast method for small datasets, with less parameters (no search parameters) and the ability to see the simple kriging weights "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def simple_simple_krige(df,xcol,ycol,vcol,dfl,xlcol,ylcol,vario,skmean):\n",
    "# load the variogram\n",
    "    nst = vario['nst']; pmx = 9999.9\n",
    "    cc = np.zeros(nst); aa = np.zeros(nst); it = np.zeros(nst)\n",
    "    ang = np.zeros(nst); anis = np.zeros(nst)\n",
    "    nug = vario['nug']; sill = nug \n",
    "    cc[0] = vario['cc1']; sill = sill + cc[0]\n",
    "    it[0] = vario['it1']; ang[0] = vario['azi1']; \n",
    "    aa[0] = vario['hmaj1']; anis[0] = vario['hmin1']/vario['hmaj1'];\n",
    "    if nst == 2:\n",
    "        cc[1] = vario['cc2']; sill = sill + cc[1]\n",
    "        it[1] = vario['it2']; ang[1] = vario['azi2']; \n",
    "        aa[1] = vario['hmaj2']; anis[1] = vario['hmin2']/vario['hmaj2'];    \n",
    "\n",
    "# set up the required matrices\n",
    "    rotmat, maxcov = geostats.setup_rotmat(nug,nst,it,cc,ang,pmx)    \n",
    "    ndata = len(df); a = np.zeros([ndata,ndata]); r = np.zeros(ndata); s = np.zeros(ndata); rr = np.zeros(ndata)\n",
    "    nest = len(dfl)\n",
    "\n",
    "    est = np.zeros(nest); var = np.full(nest,sill); weights = np.zeros([nest,ndata])\n",
    "\n",
    "# Make and solve the kriging matrix, calculate the kriging estimate and variance \n",
    "    for iest in range(0,nest):\n",
    "        for idata in range(0,ndata):\n",
    "            for jdata in range(0,ndata):\n",
    "                a[idata,jdata] = geostats.cova2(df[xcol].values[idata],df[ycol].values[idata],df[xcol].values[jdata],df[ycol].values[jdata],\n",
    "                                        nst,nug,pmx,cc,aa,it,ang,anis,rotmat,maxcov)\n",
    "            r[idata] = geostats.cova2(df[xcol].values[idata],df[ycol].values[idata],dfl[xlcol].values[iest],dfl[ylcol].values[iest],\n",
    "                                        nst,nug,pmx,cc,aa,it,ang,anis,rotmat,maxcov)\n",
    "            rr[idata] = r[idata]\n",
    "        \n",
    "        s = geostats.ksol_numpy(ndata,a,r)    \n",
    "        sumw = 0.0\n",
    "        for idata in range(0,ndata):                          \n",
    "            sumw = sumw + s[idata]\n",
    "            weights[iest,idata] = s[idata]\n",
    "            est[iest] = est[iest] + s[idata]*df[vcol].values[idata]\n",
    "            var[iest] = var[iest] - s[idata]*rr[idata]\n",
    "        est[iest] = est[iest] + (1.0-sumw)*skmean\n",
    "    return est,var,weights "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Interactive Simple Kriging Method\n",
    "\n",
    "The following code includes:\n",
    "\n",
    "* dashboard with variogram model data locations \n",
    "\n",
    "* plots of variogram model, data locations with point scaled by weights and uncertainty distribution at the unknown location"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings; warnings.simplefilter('ignore')\n",
    "\n",
    "# interactive calculation of the sample set (control of source parametric distribution and number of samples)\n",
    "style = {'description_width': 'initial'}\n",
    "l = widgets.Text(value='                                     Simple Kriging-based PreDrill Estimation, Michael Pyrcz, Associate Professor, The University of Texas at Austin',layout=Layout(width='950px', height='30px'))\n",
    "nug = widgets.FloatSlider(continuous_update=False,min = 0, max = 1.0, value = 0.0, step = 0.1, description = 'nug',orientation='vertical',\n",
    "                          layout=Layout(width='25px', height='200px'))\n",
    "nug.style.handle_color = 'gray'\n",
    "it1 = widgets.Dropdown(options=['Spherical', 'Exponential', 'Gaussian'],value='Spherical',\n",
    "    description='Type1:',disabled=False,layout=Layout(width='180px', height='30px'), style=style)\n",
    "\n",
    "azi = widgets.FloatSlider(continuous_update=False,min=0, max = 360, value = 0, step = 22.5, description = 'azi',\n",
    "                        orientation='vertical',layout=Layout(width='40px', height='200px'))\n",
    "azi.style.handle_color = 'gray'\n",
    "hmaj1 = widgets.FloatSlider(continuous_update=False,min=0.01, max = 2000.0, value = 400.0, step = 25.0, description = 'hmaj1',\n",
    "                        orientation='vertical',layout=Layout(width='40px', height='200px'))\n",
    "hmaj1.style.handle_color = 'gray'\n",
    "hmin1 = widgets.FloatSlider(continuous_update=False,min = 0.01, max = 2000.0, value = 200.0, step = 25.0, description = 'hmin1',\n",
    "                        orientation='vertical',layout=Layout(width='40px', height='200px'))\n",
    "hmin1.style.handle_color = 'gray'\n",
    "uikvar = widgets.HBox([nug,it1,azi,hmaj1,hmin1],)                   # basic widget formatting   \n",
    "\n",
    "x1 = widgets.FloatSlider(continuous_update=False,min=0.0, max = 1000.0, value = 500.0, step = 1.0, description = 'x1',orientation='horizontal',\n",
    "                         layout=Layout(width='180px', height='30px'),readout_format = '.0f',style=style)\n",
    "x1.style.handle_color = 'blue'\n",
    "y1 = widgets.FloatSlider(continuous_update=False,min=0.0, max = 1000.0, value = 500.0, step = 1.0, description = 'y1',orientation='vertical',\n",
    "                         layout=Layout(width='90px', height='180px'),readout_format = '.0f',style=style)\n",
    "y1.style.handle_color = 'blue'\n",
    "uik1 = widgets.VBox([x1,y1],)\n",
    "\n",
    "uipars = widgets.HBox([uikvar,uik1],) \n",
    "uik = widgets.VBox([l,uipars],)\n",
    "\n",
    "def convert_type(it):\n",
    "    if it == 'Spherical': \n",
    "        return 1\n",
    "    elif it == 'Exponential':\n",
    "        return 2\n",
    "    else: \n",
    "        return 3\n",
    "\n",
    "def f_make_krige(nug,it1,azi,hmaj1,hmin1,x1,y1):                       # function to take parameters, make sample and plot\n",
    "    text_trap = io.StringIO()\n",
    "    sys.stdout = text_trap\n",
    "    it1 = convert_type(it1)\n",
    "    nst = 1; xlag = 10; nlag = int(hmaj1/xlag); c1 = 1.0-nug\n",
    "    vario = GSLIB.make_variogram(nug,nst,it1,c1,azi,hmaj1,hmin1) # make model object\n",
    "    index_maj,h_maj,gam_maj,cov_maj,ro_maj = geostats.vmodel(nlag,xlag,azi,vario)   # project the model in the major azimuth                                                  # project the model in the 135 azimuth\n",
    "    index_min,h_min,gam_min,cov_min,ro_min = geostats.vmodel(nlag,xlag,azi+90.0,vario) # project the model in the minor azimuth\n",
    "      \n",
    "    xl = [x1,-999,-999]; yl = [y1,-999,-999]; value1 = [0,0,0]\n",
    "    dfl = pd.DataFrame({'X':xl,'Y':yl, 'Value':value1}) # method does not work for estimating 1 point only, 2 dummy points added   \n",
    "        \n",
    "    sk_est, sk_var, sk_weights =  simple_simple_krige(df,'X','Y','Porosity',dfl,'X','Y',vario,skmean=poravg)\n",
    "    print('Est = ' + str(sk_est[0]) + ', at ' + str(dfl['X'].values[0]) + ',' + str(dfl['Y'].values[0]))\n",
    "    #print('sk_est');print(sk_est)\n",
    "    if sk_var[0] <= 0.0: \n",
    "        sk_std = 0.0\n",
    "    else:\n",
    "        sk_std = math.sqrt(sk_var[0]*porvar)\n",
    "    \n",
    "    xlag = 10.0; nlag = int(hmaj1/xlag)\n",
    "    \n",
    "    plt.subplot(1,3,1)\n",
    "    plt.plot([0,hmaj1*1.5],[1.0,1.0],color = 'black')\n",
    "    plt.plot(h_maj,gam_maj,color = 'black',label = 'Major ' + str(azi))    \n",
    "    plt.plot(h_min,gam_min,color = 'black',label = 'Minor ' + str(azi+90.0))\n",
    "    deltas = [22.5, 45, 67.5]; \n",
    "    ndelta = len(deltas); hd = np.zeros(ndelta); gamd = np.zeros(ndelta);\n",
    "    color=iter(cm.plasma(np.linspace(0,1,ndelta)))\n",
    "    for delta in deltas:\n",
    "        index,hd,gamd,cov,ro = geostats.vmodel(nlag,xlag,azi+delta,vario);\n",
    "        c=next(color)\n",
    "        plt.plot(hd,gamd,color = c,label = 'Azimuth ' + str(azi+delta))\n",
    "    plt.xlabel(r'Lag Distance $\\bf(h)$, (m)')\n",
    "    plt.ylabel(r'$\\gamma \\bf(h)$')\n",
    "    plt.title('Interpolated Porosity Variogram Models')\n",
    "    plt.xlim([0,hmaj1*1.5])\n",
    "    plt.ylim([0,1.4])\n",
    "    plt.legend(loc='upper left')\n",
    "    \n",
    "    plt.subplot(1,3,2)\n",
    "    plt.scatter(df['X'],df['Y'],c = df['Porosity'], edgecolors = 'black', vmin = pormin, vmax= pormax)\n",
    "    plt.scatter(dfl['X'],dfl['Y'],c = sk_est,marker = 's',edgecolors = 'black',s = (1-sk_var[0])*200+40, vmin = pormin, vmax= pormax)\n",
    "    print('x'); print((dfl[\"X\"]))\n",
    "    print('y'); print((dfl[\"Y\"]))\n",
    "    print('est'); print(sk_est)\n",
    "    ax = plt.gca()\n",
    "    plt.xlabel('X(m)'); plt.ylabel('Y(m)')\n",
    "    plt.title('Data and PreDrill Locations')\n",
    "    plt.xlim([0,1000])\n",
    "    plt.ylim([0,1000])\n",
    "    #for i, txt in enumerate([1,2,3]):\n",
    "    #    plt.annotate(txt, (x[i]-40, y[i]-40))\n",
    "    plt.annotate('Mean Weight = ' + str(np.round(1.0 - np.sum(sk_weights[0]),2)), (20, 20))\n",
    "    plt.annotate(round(sk_est[0],2), (x1, y1 + 50))\n",
    "    plt.colorbar()\n",
    "    \n",
    "    ellipse = Ellipse((x1, y1),width=hmin1*2.0,height=hmaj1*2.0,angle = 360-azi,facecolor='none',edgecolor='black',alpha = 1.0,linestyle='--')\n",
    "    ax = plt.gca()\n",
    "    ax.add_patch(ellipse)\n",
    "    \n",
    "    samples = norm.rvs(sk_est[0],sk_std,1000,random_state=73073)    \n",
    "    plt.subplot(1,3,3)\n",
    "    plt.hist(samples,bins = np.linspace(pormin,pormax,20),alpha=0.2,color=\"red\",edgecolor=\"black\",density = True,label='Local')\n",
    "    plt.hist(df['Porosity'].values,bins = np.linspace(pormin,pormax,20),alpha=0.2,color=\"black\",edgecolor=\"black\",density = True,label ='Global')\n",
    "    plt.xlim([pormin,pormax]); plt.ylim([0,50]); plt.legend()\n",
    "    plt.title('PreDrill Model at the Unknown Location')\n",
    "    plt.xlabel('Value'); plt.ylabel('Frequency')\n",
    "    \n",
    "    ax = plt.gca()\n",
    "    ax.annotate('Simple Kriging Estimate = ' + str(np.round(sk_est[0],2)), (0.065, 45.5))\n",
    "    ax.annotate('Simple Kriging Variance = ' + str(np.round(sk_var[0],2)), (0.065, 42.5))\n",
    "    plt.subplots_adjust(left=0.0, bottom=0.0, right=2.2, top=0.9, wspace=0.3, hspace=0.3)\n",
    "    plt.show()\n",
    "    \n",
    "# connect the function to make the samples and plot to the widgets    \n",
    "interactive_plot = widgets.interactive_output(f_make_krige, {'nug':nug, 'it1':it1, 'azi':azi, 'hmaj1':hmaj1, 'hmin1':hmin1, \n",
    "                                                      'x1':x1, 'y1':y1,})\n",
    "interactive_plot.clear_output(wait = True)               # reduce flickering by delaying plot updating"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Interactive PreDrill, Spatial Sample Prediction Demostration\n",
    "\n",
    "* select the variogram model and the unsampled location and observe the variogram-based, spatial uncertainty model\n",
    "\n",
    "#### Michael Pyrcz, Associate Professor, University of Texas at Austin, Cofounder & Cheif Science Officer of daytum \n",
    "\n",
    "##### [Twitter](https://twitter.com/geostatsguy) | [GitHub](https://github.com/GeostatsGuy) | [Website](http://michaelpyrcz.com) | [GoogleScholar](https://scholar.google.com/citations?user=QVZ20eQAAAAJ&hl=en&oi=ao) | [Book](https://www.amazon.com/Geostatistical-Reservoir-Modeling-Michael-Pyrcz/dp/0199731446) | [YouTube](https://www.youtube.com/channel/UCLqEr-xV-ceHdXXXrTId5ig)  | [LinkedIn](https://www.linkedin.com/in/michael-pyrcz-61a648a1) | [GeostatsPy](https://github.com/GeostatsGuy/GeostatsPy)\n",
    "\n",
    "### The Inputs\n",
    "\n",
    "Select the variogram model and the data locations:\n",
    "\n",
    "* **nug**: nugget effect\n",
    "\n",
    "* **c1 **: contributions of the sill\n",
    "\n",
    "* **hmaj1 / hmin1 **: range in the major and minor direction\n",
    "\n",
    "* **(x1, y1),...(x3,y3) **: spatial data locations  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c008d2d9844749bf91c9c88dbe912cc1",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(Text(value='                                     Simple Kriging-based PreDrill Estimation, Mich…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "893b07abb6cc4547b9de2c0e4480b264",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output()"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(uik, interactive_plot)                            # display the interactive plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Comments\n",
    "\n",
    "This was an interactive demonstration of simple kriging-based predrill estimation for spatial data analytics. Much more could be done, I have other demonstrations on the basics of working with DataFrames, ndarrays, univariate statistics, plotting data, declustering, data transformations and many other workflows available at https://github.com/GeostatsGuy/PythonNumericalDemos and https://github.com/GeostatsGuy/GeostatsPy. \n",
    "  \n",
    "#### The Author:\n",
    "\n",
    "### Michael Pyrcz, Associate Professor, University of Texas at Austin \n",
    "*Novel Data Analytics, Geostatistics and Machine Learning Subsurface Solutions*\n",
    "\n",
    "With over 17 years of experience in subsurface consulting, research and development, Michael has returned to academia driven by his passion for teaching and enthusiasm for enhancing engineers' and geoscientists' impact in subsurface resource development. \n",
    "\n",
    "For more about Michael check out these links:\n",
    "\n",
    "#### [Twitter](https://twitter.com/geostatsguy) | [GitHub](https://github.com/GeostatsGuy) | [Website](http://michaelpyrcz.com) | [GoogleScholar](https://scholar.google.com/citations?user=QVZ20eQAAAAJ&hl=en&oi=ao) | [Book](https://www.amazon.com/Geostatistical-Reservoir-Modeling-Michael-Pyrcz/dp/0199731446) | [YouTube](https://www.youtube.com/channel/UCLqEr-xV-ceHdXXXrTId5ig)  | [LinkedIn](https://www.linkedin.com/in/michael-pyrcz-61a648a1)\n",
    "\n",
    "#### Want to Work Together?\n",
    "\n",
    "I hope this content is helpful to those that want to learn more about subsurface modeling, data analytics and machine learning. Students and working professionals are welcome to participate.\n",
    "\n",
    "* Want to invite me to visit your company for training, mentoring, project review, workflow design and / or consulting? I'd be happy to drop by and work with you! \n",
    "\n",
    "* Interested in partnering, supporting my graduate student research or my Subsurface Data Analytics and Machine Learning consortium (co-PIs including Profs. Foster, Torres-Verdin and van Oort)? My research combines data analytics, stochastic modeling and machine learning theory with practice to develop novel methods and workflows to add value. We are solving challenging subsurface problems!\n",
    "\n",
    "* I can be reached at mpyrcz@austin.utexas.edu.\n",
    "\n",
    "I'm always happy to discuss,\n",
    "\n",
    "*Michael*\n",
    "\n",
    "Michael Pyrcz, Ph.D., P.Eng. Associate Professor The Hildebrand Department of Petroleum and Geosystems Engineering, Bureau of Economic Geology, The Jackson School of Geosciences, The University of Texas at Austin\n",
    "\n",
    "#### More Resources Available at: [Twitter](https://twitter.com/geostatsguy) | [GitHub](https://github.com/GeostatsGuy) | [Website](http://michaelpyrcz.com) | [GoogleScholar](https://scholar.google.com/citations?user=QVZ20eQAAAAJ&hl=en&oi=ao) | [Book](https://www.amazon.com/Geostatistical-Reservoir-Modeling-Michael-Pyrcz/dp/0199731446) | [YouTube](https://www.youtube.com/channel/UCLqEr-xV-ceHdXXXrTId5ig)  | [LinkedIn](https://www.linkedin.com/in/michael-pyrcz-61a648a1)  \n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "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.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
