{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Creating and Using Panel Data Using ArcGIS Defined Location Cubes" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os as OS\n", "import arcpy as ARCPY\n", "import SSDataObject as SSDO\n", "import SSPanelObject as SSPO\n", "import SSPanel as PANEL\n", "ARCPY.overwriteOutput = True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example: Per Capita Incomes Relative to National Average in California (1969 - 2010)\n", "\n", "- Create Defined Locations Cube from Repeating Shapes Feature Class\n", "- Run Mann-Kendall Trend Statistic\n", "- Save to NetCDF File" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "inputFC = r'../data/CA_Counties_Panel.shp'\n", "outputCube = r'../data/CA_Panel.nc'\n", "fullFC = OS.path.abspath(inputFC)\n", "outputCube = OS.path.abspath(outputCube)\n", "fullPath, fcName = OS.path.split(fullFC)\n", "ssdo = SSDO.SSDataObject(inputFC)\n", "uniqueIDField = \"MYID\"\n", "timeField = \"YEAR\"\n", "analysisField = \"PCR\"\n", "panelObj = SSPO.SSPanelObject(inputFC)\n", "requireGeometry = panelObj.ssdo.shapeType.upper() == \"POLYGON\"\n", "panelObj.obtainData(uniqueIDField, \"YEAR\", \"1 Years\", fields = [analysisField],\n", " requireGeometry = requireGeometry)\n", "panelCube = PANEL.SSPanel(outputCube, panelObj = panelObj)\n", "varName = panelObj.fieldNames[0]\n", "panelCube.mannKendall(varName)\n", "panelCube.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Open Panel Cube From NetCDF File for Analysis " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "panel = PANEL.SSPanel(outputCube)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Number of Locations and Time Periods" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "# locations = 58, # time periods = 42\n" ] } ], "source": [ "print(\"# locations = {0}, # time periods = {1}\".format(panel.numLocations, panel.numTime))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## List Variables" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['projection' 'time' 'x' 'y' 'lat' 'lon' 'poly_breaks' 'poly_coords'\n", " 'time_step_ID' 'location_ID' 'MYID' 'PCR' 'PCR_TREND_ZSCORE'\n", " 'PCR_TREND_PVALUE' 'PCR_TREND_BIN']\n" ] } ], "source": [ "print(panel.obtainVariableList())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## View Mann-Kendall Trend Results in PANDAS Data Frame" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " PCR_TREND_PVALUE PCR_TREND_ZSCORE\n", "158 1.552410e-03 3.164704\n", "159 6.646548e-01 -0.433496\n", "160 1.202954e-06 -4.855150\n", "161 1.539366e-08 -5.657117\n", "162 1.441587e-03 -3.186192\n" ] } ], "source": [ "import pandas as PANDAS\n", "locations = panel.locationLabel[0]\n", "z = panel.obtainValues('PCR_TREND_ZSCORE')\n", "pv = panel.obtainValues('PCR_TREND_PVALUE')\n", "d = {'PCR_TREND_ZSCORE':z, 'PCR_TREND_PVALUE':pv}\n", "df = PANDAS.DataFrame(d, index = locations)\n", "print(df.head())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get 3D Analysis Variable " ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(42, 58)\n" ] } ], "source": [ "data = panel.obtainValues(analysisField)\n", "print(data.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Use PySAL to Analyze LISA Markov Transitions" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4]\n" ] } ], "source": [ "import pysal as PYSAL\n", "w = PYSAL.open(r\"../data/queen.gal\").read()\n", "lm = PYSAL.LISA_Markov(data.T, w)\n", "print(lm.classes)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## View Transistion Probabilities" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.91103789 0.02800659 0.00988468 0.05107084]\n", " [0.07037037 0.84074074 0.08518519 0.0037037 ]\n", " [0.00246711 0.01726974 0.95805921 0.02220395]\n", " [0.09122807 0.00701754 0.09122807 0.81052632]]\n" ] } ], "source": [ "print(lm.p)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "panel.close()" ] }, { "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }