{
  "metadata": {
    "language_info": {
      "codemirror_mode": {
        "name": "python",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8"
    },
    "kernelspec": {
      "name": "python",
      "display_name": "Python (Pyodide)",
      "language": "python"
    }
  },
  "nbformat_minor": 4,
  "nbformat": 4,
  "cells": [
    {
      "cell_type": "markdown",
      "source": "# Dealing with missing data using pandas and scipy",
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": "import numpy as np\nimport piplite\npiplite.install(\"itkwidgets===1.0a8\")\npiplite.install(\"pandas\")\npiplite.install(\"scipy\")\nimport pandas as pd\nimport scipy\nimport random",
      "metadata": {
        "trusted": true
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": "from itkwidgets import view",
      "metadata": {
        "trusted": true
      },
      "execution_count": null,
      "outputs": [
        {
          "ename": "<class 'ModuleNotFoundError'>",
          "evalue": "No module named 'itkwidgets'",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
            "Input \u001b[0;32mIn [6]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mitkwidgets\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m view\n",
            "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'itkwidgets'"
          ],
          "output_type": "error"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": "### Create manifold",
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": "def F(x, y):\n    return np.sin(np.sqrt(x ** 2 + y ** 2))\n\nx = np.linspace(-6, 6, 50)\ny = np.linspace(-6, 6, 50)\nz = F(x, y)\n\nN = np.size(x)\nM = np.zeros((N*N,3))\nnp.shape(M)",
      "metadata": {
        "trusted": true
      },
      "execution_count": 4,
      "outputs": [
        {
          "execution_count": 4,
          "output_type": "execute_result",
          "data": {
            "text/plain": "(2500, 3)"
          },
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "code",
      "source": "for i in range(0,np.size(x)):\n    for j in range(0,np.size(y)):\n        M[j*N+i,:]=[np.floor(100*x[i]), np.floor(100*y[j]), np.floor(100*np.sin(np.sqrt(x[i] ** 2 + y[j] ** 2)))]",
      "metadata": {
        "trusted": true
      },
      "execution_count": 5,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": "Produce missing data artificially",
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": "random.seed(8)\nshapeM = np.shape(M)\nrandomlist = []\nfor i in range(0,100):\n    n = random.randint(0,shapeM[0]-3)\n    M[n:n+2,2]='NaN'\n    randomlist.append(n)\n\nprint(randomlist)",
      "metadata": {
        "trusted": true
      },
      "execution_count": 6,
      "outputs": [
        {
          "name": "stdout",
          "text": "[928, 1517, 1537, 517, 791, 179, 348, 560, 1013, 2074, 857, 1641, 124, 1880, 1996, 1856, 1599, 2027, 2347, 787, 1649, 366, 1986, 959, 81, 1092, 2130, 1669, 1942, 1552, 465, 1058, 397, 258, 1582, 1545, 441, 237, 1386, 960, 352, 2038, 2116, 851, 2381, 582, 2485, 263, 2210, 153, 2001, 794, 607, 2371, 1868, 2393, 1823, 1170, 2282, 1461, 1745, 552, 648, 2462, 399, 2178, 1285, 1462, 2026, 2062, 809, 1264, 615, 1457, 2165, 1164, 2088, 292, 2078, 2232, 900, 1409, 998, 48, 1211, 1405, 900, 1092, 137, 1734, 1043, 1543, 1222, 1707, 717, 1623, 476, 680, 104, 826]\n",
          "output_type": "stream"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": "### Point cloud with missing data",
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": "view(point_sets=M, rotate=True)",
      "metadata": {
        "trusted": true
      },
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": "<IPython.core.display.Javascript object>",
            "application/javascript": "window.connectPlugin && window.connectPlugin(\"309359bf-7520-472f-bdf4-023dcc68f3bb\")"
          },
          "metadata": {}
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": "<IPython.core.display.HTML object>",
            "text/html": "<div id=\"6392699c-1520-445f-bc5f-1a1c1a216096\"></div>"
          },
          "metadata": {}
        },
        {
          "execution_count": 7,
          "output_type": "execute_result",
          "data": {
            "text/plain": "<itkwidgets.viewer.Viewer at 0x41117d0>"
          },
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "code",
      "source": "df = pd.DataFrame(M)",
      "metadata": {
        "trusted": true
      },
      "execution_count": 61,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": "z_new = df[2].interpolate(method='polynomial', order=2)\nz_array = z_new.to_numpy()\nM_recovered = M\nM_recovered[:,2] = z_array",
      "metadata": {
        "trusted": true
      },
      "execution_count": 62,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": "### Point cloud with \"recovered\" data",
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": "view(point_sets=M_recovered, rotate=True)",
      "metadata": {
        "trusted": true
      },
      "execution_count": 63,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": "<IPython.core.display.Javascript object>",
            "application/javascript": "window.connectPlugin && window.connectPlugin(\"036da92d-b993-47b2-851e-572da51aa4fd\")"
          },
          "metadata": {}
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": "<IPython.core.display.HTML object>",
            "text/html": "<div id=\"5552be50-b24a-48d4-86d4-c0ebc48e64bf\"></div>"
          },
          "metadata": {}
        },
        {
          "execution_count": 63,
          "output_type": "execute_result",
          "data": {
            "text/plain": "<itkwidgets.viewer.Viewer at 0x79574f0>"
          },
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "code",
      "source": "",
      "metadata": {},
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": "",
      "metadata": {},
      "execution_count": null,
      "outputs": []
    }
  ]
}