Skip to content

Instantly share code, notes, and snippets.

@jpolchlo
Created January 28, 2020 22:11
Show Gist options
  • Select an option

  • Save jpolchlo/e3f0c61d9369919101d535c325789db5 to your computer and use it in GitHub Desktop.

Select an option

Save jpolchlo/e3f0c61d9369919101d535c325789db5 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"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": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"+--------------------+\n",
"| tens|\n",
"+--------------------+\n",
"|BufferedTensor wi...|\n",
"+--------------------+\n",
"\n",
"deserializing Row(arrow_tensor=Numpy Tensor of shape (3, 14, 14), extent=Row(xmin=0.0, ymin=0.0, xmax=0.0, ymax=0.0), x_buffer=3, y_buffer=2)\n"
]
},
{
"data": {
"text/plain": [
"Row(tens=BufferedTensor of shape (3, 14, 14) with extent {'xmin': 0.0, 'ymin': 0.0, 'xmax': 0.0, 'ymax': 0.0} and buffer of size 2×3 pixels)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"R2 = Row('tens')\n",
"df = spark.createDataFrame([R2(bt)])\n",
"df.show()\n",
"df.first()"
]
},
{
"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",
"|ArrowTensor(org.a...|\n",
"+--------------------+\n",
"\n"
]
},
{
"data": {
"text/plain": [
"Row(tens=Numpy Tensor of shape (1, 10, 10))"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tensor = ArrowTensor(np.ones([1,10,10]))\n",
"\n",
"R = Row('tens')\n",
"df = spark.createDataFrame([R(tensor)])\n",
"df.show()\n",
"df.first()"
]
},
{
"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",
"| 1|\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",
"| [10, 10]|\n",
"+-------------------+\n",
"\n"
]
}
],
"source": [
"df.select(rf_dimensions(col('tens'))).show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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