Skip to content

Instantly share code, notes, and snippets.

@sergiobuj
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save sergiobuj/e6f4bd0657d5ee2fa8d9 to your computer and use it in GitHub Desktop.

Select an option

Save sergiobuj/e6f4bd0657d5ee2fa8d9 to your computer and use it in GitHub Desktop.
What if we could use [the amazing] Clusterpy on top of Pysal?
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:0333f9a9480cfb8b2972406747e3000ab4ff142852f2ee25a6cfd404e95953e3"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np\n",
"import pysal as ps"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"(ImportError('cannot import name minimize_scalar',), 'Maximum Likelihood in PySAL requires SciPy version 0.11 or newer.')\n",
"(ImportError('cannot import name minimize_scalar',), 'Maximum Likelihood in PySAL requires SciPy version 0.11 or newer.')\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"class Utils(object):\n",
" \"\"\"\n",
" \"\"\"\n",
" @staticmethod\n",
" def numeric_dbf(dbf, attributes = None):\n",
" \"\"\"\n",
" \"\"\"\n",
" wanted_attr = None\n",
" if attributes:\n",
" wanted_attr = np.in1d(dbf.header, attributes)\n",
" else:\n",
" wanted_attr = ~np.in1d(dbf.header, ['ID', 'Id', 'id' ])\n",
" \n",
" num_types = np.in1d([ i[0] for i in dbf.field_spec], ['N', 'F'])\n",
" attr_indices = np.nonzero(num_types & wanted_attr)\n",
"\n",
" num_fields = len(attr_indices[0])\n",
" mat = np.empty((dbf.n_records, num_fields))\n",
"\n",
" for row in range(dbf.n_records):\n",
" row_attrs = np.array(dbf.by_row(row))\n",
" mat[row,:] = row_attrs[attr_indices].astype(np.float)\n",
" return mat\n",
"\n",
"class cluster_execution(object):\n",
" \"\"\"\n",
" \"\"\"\n",
" def __init__(self, w, dbf):\n",
" \"\"\"\n",
" \"\"\"\n",
" self.w = w\n",
" self.dbf = dbf\n",
" self.attr_matrix = Utils.numeric_dbf(dbf)\n",
" self.regions = np.array([-1] * dbf.n_records, dtype=numpy.short)\n",
" \n",
" def region_centroid(self, region):\n",
" \"\"\"\n",
" \"\"\"\n",
" areas = np.nonzero(self.regions == region)\n",
" region = np.array(self.attr_matrix[areas])\n",
" return region.sum(axis=0) / len(areas)\n",
" \n",
" def closest_to_region(self, areas, region):\n",
" \"\"\"\n",
" \"\"\"\n",
" distance = np.vectorize(self.distance_area_to_region, otypes=[np.ndarray])\n",
" # convert to type object to be able to send an array as parameter to a vectorized function\n",
" centroid_obj = np.array((1,), dtype=object)\n",
" centroid_obj[0] = self.region_centroid(region)\n",
" closest = distance(areas, region, centroid_obj).argmin()\n",
" return areas[closest]\n",
"\n",
" def distance_area_to_region(self, area, region, centroid = None):\n",
" \"\"\"\n",
" Difference squared\n",
" \"\"\"\n",
" if centroid == None:\n",
" areas = np.nonzero(self.regions == region) #area in region?\n",
" centroid = self.region_centroid(region)\n",
" diff = self.attr_matrix[area] - centroid\n",
" return np.sum(diff ** 2)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"data = 'china'\n",
"data = 'n100'\n",
"shpf = data + '.shp'\n",
"dbff = data + '.dbf'"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dbf = ps.open(dbff)\n",
"shp = ps.open(shpf)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create a cluster execution with a W and DBF"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"cl = cluster_execution(ps.rook_from_shapefile(shpf), dbf)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"cl.regions[:2] = 0\n",
"cl.regions"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 6,
"text": [
"array([ 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,\n",
" -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,\n",
" -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,\n",
" -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,\n",
" -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,\n",
" -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], dtype=int16)"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"cl.region_centroid(0)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 7,
"text": [
"array([ -1.6274063, 26.2722547])"
]
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"cl.distance_area_to_region(13, 0)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"text": [
"155.96353719184179"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"cl.closest_to_region([20, 21, 22, 23], 0)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 9,
"text": [
"22"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 9
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment