Created
January 16, 2020 19:39
-
-
Save jpolchlo/0f99107253d8e1090e3f5323f0711126 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "from pyrasterframes import *\n", | |
| "from pyrasterframes.rasterfunctions import *\n", | |
| "from pyrasterframes.utils import create_rf_spark_session" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "spark = create_rf_spark_session()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import numpy as np\n", | |
| "from pyrasterframes.rf_types import *\n", | |
| "from pyspark import Row\n", | |
| "from pyspark.sql.functions import udf, col" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "from pyrasterframes.rf_types import BufferedTensor, BufferedTensorUDT" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "btudt = BufferedTensorUDT()\n", | |
| "bt = BufferedTensor(np.ones([3,14,14]), 2, 3, extent={'xmin': 0.0, 'ymin': 0.0, 'xmax': 2.0, 'ymax': 2.0})" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "BufferedTensor of shape (3, 14, 14) with extent {'xmin': 0.0, 'ymin': 0.0, 'xmax': 2.0, 'ymax': 2.0}and buffer of size 2×3 pixels" | |
| ] | |
| }, | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "bt" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "BufferedTensor of shape (3, 12, 12) with extent {'xmin': 0.0, 'ymin': 0.0, 'xmax': 2.0, 'ymax': 2.0}and buffer of size 1.0×2.0 pixels" | |
| ] | |
| }, | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "summed = bt.convolve(np.ones([3,3]))\n", | |
| "summed" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "array([[[9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.]],\n", | |
| "\n", | |
| " [[9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.]],\n", | |
| "\n", | |
| " [[9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.],\n", | |
| " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.]]])" | |
| ] | |
| }, | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "summed.ndarray" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 9, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "+--------------------+\n", | |
| "| tens|\n", | |
| "+--------------------+\n", | |
| "|BufferedTensor(Ar...|\n", | |
| "+--------------------+\n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "R = Row('tens')\n", | |
| "df = spark.createDataFrame([R(bt)])\n", | |
| "df.show()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 10, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "@udf\n", | |
| "def get_bands(t):\n", | |
| " return np.shape(t.ndarray)[0]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 11, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "+---------------+\n", | |
| "|get_bands(tens)|\n", | |
| "+---------------+\n", | |
| "| 3|\n", | |
| "+---------------+\n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "df.select(get_bands(col('tens'))).show()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 12, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "+-------------------+\n", | |
| "|rf_dimensions(tens)|\n", | |
| "+-------------------+\n", | |
| "| [8, 10]|\n", | |
| "+-------------------+\n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "df.select(rf_dimensions(col('tens'))).show()" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "RasterFrames", | |
| "language": "python", | |
| "name": "pyspark" | |
| }, | |
| "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 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment