Last active
October 17, 2018 18:45
-
-
Save phobson/ee6384cfada1136d5bbb44bc4c992f1b to your computer and use it in GitHub Desktop.
Focus Demo
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": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Setup the environment" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from functools import wraps\n", | |
"\n", | |
"import numpy\n", | |
"from matplotlib import pyplot\n", | |
"\n", | |
"import ipywidgets\n", | |
"\n", | |
"from pygridgen import grid\n", | |
"from pygridtools import viz, iotools\n", | |
"%matplotlib inline" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def make_grid(focus):\n", | |
" return grid.Gridgen([10, 65, 65, 10], [5, 5, 40, 40], [1, 1, 1, 1], (35, 55),\n", | |
" ul_idx=3, focus=focus)\n", | |
"\n", | |
"\n", | |
"def make_fake_bathy(shape):\n", | |
" j_cells, i_cells = shape\n", | |
" y, x = numpy.mgrid[:j_cells, :i_cells]\n", | |
" z = (y - (j_cells // 2))** 2 - x\n", | |
" return z\n", | |
"\n", | |
"\n", | |
"def plot_grid(x, y, ax=None):\n", | |
" shape = x[1:, 1:].shape \n", | |
" bathy = make_fake_bathy(shape)\n", | |
" if not ax:\n", | |
" fig, ax = pyplot.subplots(figsize=(8, 8))\n", | |
" ax.set_aspect('equal')\n", | |
" \n", | |
" return viz.plot_cells(x, y, ax=ax, cmap='Blues', colors=bathy, lw=0.5, ec='0.3')\n", | |
"\n", | |
"\n", | |
"def compare_focus(focus):\n", | |
" g1 = make_grid(None)\n", | |
" g2 = make_grid(focus)\n", | |
" fig, (ax1, ax2) = pyplot.subplots(nrows=2, figsize=(8, 13), sharex=True)\n", | |
" _, a1 = plot_grid(g1.x, g1.y, ax=ax1)\n", | |
" _, a2 = plot_grid(g2.x, g2.y, ax=ax2)\n", | |
" fig.subplots_adjust(hspace=-0.2)\n", | |
" return fig, a1, a2\n", | |
"\n", | |
"\n", | |
"def _normer(x):\n", | |
" return (x - x.min()) / (x.max() - x.min())\n", | |
"\n", | |
"\n", | |
"def normalize(f):\n", | |
" @wraps(f)\n", | |
" def wrapper(*args, **kwargs):\n", | |
" x, y = f(*args, **kwargs)\n", | |
" return _normer(x), _normer(y)\n", | |
" return wrapper" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Demo the shape widget" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"d = numpy.array([\n", | |
" (13, 16, 1.00),\n", | |
" (18, 13, 1.00),\n", | |
" (12, 7, 0.50),\n", | |
" (10, 10, -0.25),\n", | |
" ( 5, 10, -0.25),\n", | |
" ( 5, 0, 1.00),\n", | |
" ( 0, 0, 1.00),\n", | |
" ( 0, 15, 0.50),\n", | |
" ( 8, 15, -0.25),\n", | |
" (11, 13, -0.25),\n", | |
"])\n", | |
"g = grid.Gridgen(d[:, 0], d[:, 1], d[:, 2], (75, 75), ul_idx=1, focus=None)\n", | |
"\n", | |
"new_grid, widget = iotools.interactive_grid_shape(g, plotfxn=plot_grid)\n", | |
"widget" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"f = grid.Focus(\n", | |
" grid._FocusPoint(pos=0.33, axis='x', factor=3, extent=0.3),\n", | |
" grid._FocusPoint(pos=0.80, axis='y', factor=4, extent=0.2),\n", | |
")\n", | |
"compare_focus(f)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"@normalize\n", | |
"def f(x, y, xo=0.6, yo=0.5):\n", | |
" xf = numpy.tan((x - xo)*2.0)\n", | |
" yf = numpy.tan((y - yo)*2.0)\n", | |
" return xf, yf\n", | |
"\n", | |
"compare_focus(f)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Fun, pointless, direct manipulation of nodes" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"scrolled": false | |
}, | |
"outputs": [], | |
"source": [ | |
"_y, _x = numpy.mgrid[5:40, 10:65]\n", | |
"y = _y + 4 * numpy.cos(_x / 5)\n", | |
"x = _x + 2 * numpy.sin(_y / 3)\n", | |
"plot_grid(x=x, y=y)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"scrolled": false | |
}, | |
"outputs": [], | |
"source": [ | |
"y = _y + 2 * numpy.cos(_x / 2.5)\n", | |
"x = _x + 4 * numpy.sin(_y / 3)\n", | |
"plot_grid(x=x, y=y)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"g.x" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python [conda env:grid]", | |
"language": "python", | |
"name": "conda-env-grid-py" | |
}, | |
"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.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment