Created
October 25, 2012 20:58
-
-
Save dwf/3955348 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
| { | |
| "metadata": { | |
| "name": "Some decorator ideas for dealing with array-order-dependent code" | |
| }, | |
| "nbformat": 3, | |
| "nbformat_minor": 0, | |
| "worksheets": [ | |
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "By David Warde-Farley, Oct 25 2012. BSD License all 'round." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "import functools\n", | |
| "\n", | |
| "def ensure_fortran(*args, **kwargs):\n", | |
| " \"\"\"\n", | |
| " Modifies attributes of `self` named as positional arguments\n", | |
| " to be F-contiguous if they are not already.\n", | |
| " \"\"\"\n", | |
| " assert len(kwargs) == 0, \"keyword arguments not supported\"\n", | |
| " def wrap(method):\n", | |
| " @functools.wraps(method)\n", | |
| " def wrapped(self, *pargs, **kwargs):\n", | |
| " for name in args:\n", | |
| " attr = getattr(self, name)\n", | |
| " if not attr.flags.fortran:\n", | |
| " setattr(self, name, attr.copy('F'))\n", | |
| " return method(self, *pargs, **kwargs)\n", | |
| " return wrapped\n", | |
| " return wrap" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 1 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "import numpy as np\n", | |
| "\n", | |
| "class Foo(object):\n", | |
| " def __init__(self):\n", | |
| " self.x = np.empty((2, 2), order='C')\n", | |
| " self.y = np.empty((2, 2), order='C')\n", | |
| "\n", | |
| " @ensure_fortran('x', 'y')\n", | |
| " def bar(self):\n", | |
| " assert self.x.flags.fortran\n", | |
| " assert self.x.flags.f_contiguous\n", | |
| " assert self.y.flags.fortran\n", | |
| " assert self.y.flags.f_contiguous" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 2 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "Foo().bar()" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 3 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "import functools\n", | |
| "\n", | |
| "def force_array_order(*args, **deco_kwargs):\n", | |
| " \"\"\"A similar decorator capable of enforcing C or F contiguity.\"\"\"\n", | |
| " assert len(args) == 0, \"positional arguments not supported\"\n", | |
| " def wrap(method):\n", | |
| " @functools.wraps(method)\n", | |
| " def wrapped(self, *args, **kwargs):\n", | |
| " for name, contig in deco_kwargs.iteritems():\n", | |
| " attr = getattr(self, name)\n", | |
| " contig = contig.lower()\n", | |
| " if contig in ('c', 'f'):\n", | |
| " if not getattr(attr.flags, contig + '_contiguous'):\n", | |
| " setattr(self, name, attr.copy(contig.upper()))\n", | |
| " else:\n", | |
| " raise ValueError(\"unknown contiguity mode '%s'\" % str(contig))\n", | |
| " return method(self, *args, **kwargs)\n", | |
| " return wrapped\n", | |
| " return wrap" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 4 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "class Bar(object):\n", | |
| " def __init__(self):\n", | |
| " self.x = np.empty((2, 2), order='F')\n", | |
| " self.y = np.empty((2, 2), order='C')\n", | |
| " \n", | |
| " @force_array_order(x='c', y='f')\n", | |
| " def baz(self):\n", | |
| " assert self.x.flags.c_contiguous\n", | |
| " assert self.y.flags.f_contiguous" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 5 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "Bar().baz()" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 6 | |
| } | |
| ], | |
| "metadata": {} | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment