{
"metadata": {
"name": "",
"signature": "sha256:84b0bda1fe6340899a6fc34c1a266a1635f127131874f61a9ea797226c17041b"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"%run NBCONFIG.ipynb"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Populating the interactive namespace from numpy and matplotlib\n"
]
},
{
"html": [
""
],
"metadata": {},
"output_type": "display_data",
"text": [
""
]
}
],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"pyparty Grids"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`pyparty` provides some basic Grid classes, the goals of which are to provide a gridding/tiling system for creating particles and background. As with other aspects of pyparty, grids can be created from barebones:\n",
"\n",
"http://stackoverflow.com/questions/19586828/drawing-grid-pattern-in-matplotlib\n",
"\n",
"`pyparty` Grids wrap numpy.meshgrid() and provide a basic API for human-natural interaction. For example, grids retain information on their spacing, tiles, centers, corners etc... Before looking at the Grid classes in general, let's look at the builtin **CartesianGrid** of the canvas class. First, let's conjur up some random circles. Canvas has a special constructor for this:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from pyparty import Canvas, splot, showim\n",
"c=Canvas.random_circles(n=15, background='honeydew', pcolor='random')"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Canvas' grid can be accessed as an attribute: "
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"c.show(grid=True)\n",
"c.grid"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": [
"CartesianGrid (512 X 512) at 0xb80fd7c:\n",
" X --> 15 divisions (34.1 pixels / div)\n",
" Y --> 15 divisions (34.1 pixels / div)"
]
},
{
"metadata": {},
"output_type": "display_data",
"svg": [
"\n",
"\n",
"\n",
"\n"
],
"text": [
""
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A variety of *show()* keywords can be used to change grid properties. Any of these keywords **will automatically set grid=True**. These are:\n",
"\n",
" - gcolor : Change grid color\n",
" - gunder : Grid appears *under* particles\n",
" - gstyle : change linestyle of grid\n",
" \n",
"**gstyle is only available for patchshow()**"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"ax1, ax2, ax3, ax4 = splot(2,2, figsize=(10,10))\n",
"\n",
"c.show(ax1, gcolor='blue', title='standard')\n",
"c.show(ax2, gcolor='blue', gunder=True, title='grid under')\n",
"c.patchshow(ax3, gcolor='orange', gunder=True, title='patches grid under')\n",
"c.patchshow(ax4, gcolor='orange', gstyle='--', title='patches gstyle = --');"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"svg": [
"\n",
"\n",
"\n",
"\n"
],
"text": [
""
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can change the tiling via the **spacing** or **points** parameters, for both the x and y dimensions. *Spacing* sets the width of each grid tile; *points* let the users fix the grid to a preset number of spaces. At any time, the **reset_grid()** method will restore the default resolution and spacing. "
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from pyparty import splot\n",
"\n",
"ax1, ax2 = splot(1,2, figsize=(10,10))\n",
"\n",
"c.grid.xdiv=5\n",
"c.show(ax1, gcolor='black', title='5 \"X\" divisions')\n",
"c.reset_grid()\n",
"\n",
"c.grid.yspacing=20\n",
"c.patchshow(ax2, gcolor=(1,0,0), title='20 pixels per \"Y\" division')\n",
"c.reset_grid();"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"svg": [
"\n",
"\n",
"\n",
"\n"
],
"text": [
""
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The \"grid\" keyword to *show()* takes a variety of arguments:\n",
" \n",
" - True or \"grid\" : Full grid\n",
" - \"centers\" : tile center dots\n",
" - \"corners\" : tile bottom right corner dots\n",
" - \"hlines\" : horizonal grid lines\n",
" - \"vlines\" : vertical"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"axs = splot(2,2)\n",
"\n",
"c.show(axs[0], grid='centers', title='centers')\n",
"c.show(axs[1], grid='corners', title='corners')\n",
"c.show(axs[2], grid='hlines', title='hlines')\n",
"c.show(axs[3], grid='vlines', title='vlines')\n",
"\n",
"# Zoom in to for clarity\n",
"for ax in axs:\n",
" ax.set_xlim(-2,37)\n",
" ax.set_ylim(37,-2)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"svg": [
"\n",
"\n",
"\n",
"\n"
],
"text": [
""
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There's not much magic going on here, becuase *centers*, *corners* etc.. are all accessible indicies in the underlying grid. We could access them directly **and use them as masks** on any ndarray."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Make a blank 512x512 array (ones=white)\n",
"blank = np.ones((512,512,3))\n",
"\n",
"#Get the indicies of corners, centers\n",
"blank[c.grid.centers] = (1,0,0) #r\n",
"blank[c.grid.hlines] = (0,1,0) #g\n",
"blank[c.grid.corners] = (0,0,1) #b\n",
"imshow(blank)\n",
"plt.xlim(-1,104)\n",
"plt.ylim(104,-1);"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"svg": [
"\n",
"\n",
"\n",
"\n"
],
"text": [
""
]
}
],
"prompt_number": 7
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Mapping Particles to the Grid"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A major motivator for the canvas grid is to map particles to the grid points. Canvas selectively promotes a few very common grid attributes for convienence. These are:\n",
"\n",
"- c.gcenters --> c.grid.centers\n",
"- c.gcorners --> c.grid.corners\n",
"- c.ghlines --> c.grid.hlines\n",
"- c.gvlines --> c.grid.vlines\n",
"- c.gpairs() --> c.grid.pairs()\n",
" \n",
"All other grid attributes must be accessed directly on the grid. Important ones include:\n",
"\n",
"- c.grid.xdiv / ydiv --> number of xy divisions\n",
"- c.grid.zz --> underlying 2d mesh ndarray\n",
"- c.grid.xspacing / yspacing --> xy spacing per grid line\n",
"- **EXPERIMENTAL** (see end of notebook):\n",
" - c.grid.tiles\n",
" - c.grid.as_tiles()\n",
" - c.grid.dlines\n",
" - c.grid.dlines_neg (negative diagonal lines)\n",
"\n",
"As shown above, grid feature attribute (centers/edges etc...) are returned as masks. For particle **mapping** it is advantageous to return the coordinates of these points instead. For example, a set of (x,y) pairs corresponding to the grid centers. This is done through the **c.gpairs()** method:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from skimage.data import moon, lena\n",
"\n",
"cnew=Canvas(background=moon())\n",
"cnew.grid.xdiv=10\n",
"cnew.grid.ydiv=20\n",
"\n",
"for (cx, cy) in cnew.gpairs('centers'):\n",
" cnew.add('circle', radius=10, center=(cx,cy), color='orange')\n",
" \n",
"for idx, (cx, cy) in enumerate( cnew.gpairs('corners') ):\n",
" cnew.add('ellipse', xradius=10, yradius=30, center=(cx,cy), color='white', phi=45*idx)\n",
"\n",
"cnew.patchshow(gcolor='yellow', gstyle='--');"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stderr",
"text": [
"No handlers could be found for logger \"pyparty.utils\"\n"
]
},
{
"metadata": {},
"output_type": "display_data",
"svg": [
"\n",
"\n",
"\n",
"