{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Object-oriented programming in Python\n",
    "\n",
    "\n",
    "By now you might have realised that __everything__ in Python is an\n",
    "object. Strings are objects, numbers are objects, functions are objects,\n",
    "modules are objects - __everything__ is an object!\n",
    "\n",
    "\n",
    "But this does not mean that you have to use Python in an object-oriented\n",
    "fashion. You can stick with functions and statements, and get quite a lot\n",
    "done. But some problems are just easier to solve, and to reason about, when\n",
    "you use an object-oriented approach.\n",
    "\n",
    "\n",
    "* [Objects versus classes](#objects-versus-classes)\n",
    "* [Defining a class](#defining-a-class)\n",
    "* [Object creation - the `__init__` method](#object-creation-the-init-method)\n",
    " * [Our method is called `__init__`, but we didn't actually call the `__init__` method!](#our-method-is-called-init)\n",
    " * [We didn't specify the `self` argument - what gives?!?](#we-didnt-specify-the-self-argument)\n",
    "* [Attributes](#attributes)\n",
    "* [Methods](#methods)\n",
    "* [Protecting attribute access](#protecting-attribute-access)\n",
    " * [A better way - properties](#a-better-way-properties])\n",
    "* [Inheritance](#inheritance)\n",
    " * [The basics](#the-basics)\n",
    " * [Code re-use and problem decomposition](#code-re-use-and-problem-decomposition)\n",
    " * [Polymorphism](#polymorphism)\n",
    " * [Multiple inheritance](#multiple-inheritance)\n",
    "* [Class attributes and methods](#class-attributes-and-methods)\n",
    " * [Class attributes](#class-attributes)\n",
    " * [Class methods](#class-methods)\n",
    "* [Appendix: The `object` base-class](#appendix-the-object-base-class)\n",
    "* [Appendix: `__init__` versus `__new__`](#appendix-init-versus-new)\n",
    "* [Appendix: Monkey-patching](#appendix-monkey-patching)\n",
    "* [Appendix: Method overloading](#appendix-method-overloading)\n",
    "* [Useful references](#useful-references)\n",
    "\n",
    "\n",
    "<a class=\"anchor\" id=\"objects-versus-classes\"></a>\n",
    "## Objects versus classes\n",
    "\n",
    "\n",
    "If you are versed in C++, Java, C#, or some other object-oriented language,\n",
    "then this should all hopefully sound familiar, and you can skip to the next\n",
    "section.\n",
    "\n",
    "\n",
    "If you have not done any object-oriented programming before, your first step\n",
    "is to understand the difference between _objects_ (also known as\n",
    "_instances_) and _classes_ (also known as _types_).\n",
    "\n",
    "\n",
    "If you have some experience in C, then you can start off by thinking of a\n",
    "class as like a `struct` definition - a `struct` is a specification for the\n",
    "layout of a chunk of memory. For example, here is a typical struct definition:\n",
    "\n",
    "> ```\n",
    "> /**\n",
    ">  * Struct representing a stack.\n",
    ">  */\n",
    "> typedef struct __stack {\n",
    ">   uint8_t capacity; /**< the maximum capacity of this stack */\n",
    ">   uint8_t size;     /**< the current size of this stack     */\n",
    ">   void  **top;      /**< pointer to the top of this stack   */\n",
    "> } stack_t;\n",
    "> ```\n",
    "\n",
    "\n",
    "Now, an _object_ is not a definition, but rather a thing which resides in\n",
    "memory. An object can have _attributes_ (pieces of information), and _methods_\n",
    "(functions associated with the object). You can pass objects around your code,\n",
    "manipulate their attributes, and call their methods.\n",
    "\n",
    "\n",
    "Returning to our C metaphor, you can think of an object as like an\n",
    "instantiation of a struct:\n",
    "\n",
    "\n",
    "> ```\n",
    "> stack_t stack;\n",
    "> stack.capacity = 10;\n",
    "> ```\n",
    "\n",
    "\n",
    "One of the major differences between a `struct` in C, and a `class` in Python\n",
    "and other object oriented languages, is that you can't (easily) add functions\n",
    "to a `struct` - it is just a chunk of memory. Whereas in Python, you can add\n",
    "functions to your class definition, which will then be added as methods when\n",
    "you create an object from that class.\n",
    "\n",
    "\n",
    "Of course there are many more differences between C structs and classes (most\n",
    "notably [inheritance](todo), [polymorphism](todo), and [access\n",
    "protection](todo)). But if you can understand the difference between a\n",
    "_definition_ of a C struct, and an _instantiation_ of that struct, then you\n",
    "are most of the way towards understanding the difference between a _class_,\n",
    "and an _object_.\n",
    "\n",
    "\n",
    "> But just to confuse you, remember that in Python, __everything__ is an\n",
    "> object - even classes!\n",
    "\n",
    "\n",
    "<a class=\"anchor\" id=\"defining-a-class\"></a>\n",
    "## Defining a class\n",
    "\n",
    "\n",
    "Defining a class in Python is simple. Let's take on a small project, by\n",
    "developing a class which can be used in place of the `fslmaths` shell command."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class FSLMaths(object):\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this statement, we defined a new class called `FSLMaths`, which inherits\n",
    "from the built-in `object` base-class (see [below](inheritance) for more\n",
    "details on inheritance).\n",
    "\n",
    "\n",
    "Now that we have defined our class, we can create objects - instances of that\n",
    "class - by calling the class itself, as if it were a function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fm1 = FSLMaths()\n",
    "fm2 = FSLMaths()\n",
    "print(fm1)\n",
    "print(fm2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Although these objects are not of much use at this stage. Let's do some more\n",
    "work.\n",
    "\n",
    "\n",
    "<a class=\"anchor\" id=\"object-creation-the-init-method\"></a>\n",
    "## Object creation - the `__init__` method\n",
    "\n",
    "\n",
    "The first thing that our `fslmaths` replacement will need is an input image.\n",
    "It makes sense to pass this in when we create an `FSLMaths` object:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class FSLMaths(object):\n",
    "    def __init__(self, inimg):\n",
    "        self.img = inimg"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here we have added a _method_ called `__init__` to our class (remember that a\n",
    "_method_ is just a function which is defined in a class, and which can be\n",
    "called on instances of that class).  This method expects two arguments -\n",
    "`self`, and `inimg`. So now, when we create an instance of the `FSLMaths`\n",
    "class, we will need to provide an input image:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import nibabel as nib\n",
    "import os.path as op\n",
    "\n",
    "fpath = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')\n",
    "inimg = nib.load(fpath)\n",
    "fm    = FSLMaths(inimg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are a couple of things to note here...\n",
    "\n",
    "\n",
    "<a class=\"anchor\" id=\"our-method-is-called-init\"></a>\n",
    "### Our method is called `__init__`, but we didn't actually call the `__init__` method!\n",
    "\n",
    "\n",
    "`__init__` is a special method in Python - it is called when an instance of a\n",
    "class is created. And recall that we can create an instance of a class by\n",
    "calling the class in the same way that we call a function.\n",
    "\n",
    "\n",
    "There are a number of \"special\" methods that you can add to a class in Python\n",
    "to customise various aspects of how instances of the class behave.  One of the\n",
    "first ones you may come across is the `__str__` method, which defines how an\n",
    "object should be printed (more specifically, how an object gets converted into\n",
    "a string). For example, we could add a `__str__` method to our `FSLMaths`\n",
    "class like so:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class FSLMaths(object):\n",
    "\n",
    "    def __init__(self, inimg):\n",
    "        self.img = inimg\n",
    "\n",
    "    def __str__(self):\n",
    "        return 'FSLMaths({})'.format(self.img.get_filename())\n",
    "\n",
    "fpath = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')\n",
    "inimg = nib.load(fpath)\n",
    "fm    = FSLMaths(inimg)\n",
    "\n",
    "print(fm)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Refer to the [official\n",
    "docs](https://docs.python.org/3.5/reference/datamodel.html#special-method-names)\n",
    "for details on all of the special methods that can be defined in a class. And\n",
    "take a look at the appendix for some more details on [how Python objects get\n",
    "created](appendix-init-versus-new).\n",
    "\n",
    "\n",
    "<a class=\"anchor\" id=\"we-didnt-specify-the-self-argument\"></a>\n",
    "### We didn't specify the `self` argument - what gives?!?\n",
    "\n",
    "\n",
    "The `self` argument is a special argument for methods in Python. If you are\n",
    "coming from C++, Java, C# or similar, `self` in Python is equivalent to `this`\n",
    "in those languages.\n",
    "\n",
    "\n",
    "In a method, the `self` argument is a reference to the object that the method\n",
    "was called on. So in this line of code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fm = FSLMaths(inimg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "the `self` argument in `__init__` will be a reference to the `FSLMaths` object\n",
    "that has been created (and is then assigned to the `fm` variable, after the\n",
    "`__init__` method has finished).\n",
    "\n",
    "\n",
    "But note that you __do not__ need to explicitly provide the `self` argument\n",
    "when you call a method on an object, or when you create a new object. The\n",
    "Python runtime will take care of passing the instance to its method, as the\n",
    "first argument to the method.\n",
    "\n",
    "\n",
    "But when you are writing a class, you __do__ need to explicitly list `self` as\n",
    "the first argument to all of the methods of the class.\n",
    "\n",
    "\n",
    "<a class=\"anchor\" id=\"attributes\"></a>\n",
    "## Attributes\n",
    "\n",
    "\n",
    "In Python, the term __attribute__ is used to refer to a piece of information\n",
    "that is associated with an object. An attribute is generally a reference to\n",
    "another object (which might be a string, a number, or a list, or some other\n",
    "more complicated object).\n",
    "\n",
    "\n",
    "Remember that we modified our `FSLMaths` class so that it is passed an input\n",
    "image on creation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class FSLMaths(object):\n",
    "    def __init__(self, inimg):\n",
    "        self.img = inimg\n",
    "\n",
    "fpath = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')\n",
    "fm    = FSLMaths(nib.load(fpath))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Take a look at what is going on in the `__init__` method - we take the `inimg`\n",
    "argument, and create a reference to it called `self.img`. We have added an\n",
    "_attribute_ to the `FSLMaths` instance, called `img`, and we can access that\n",
    "attribute like so:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('Input for our FSLMaths instance: {}'.format(fm.img.get_filename()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And that concludes the section on adding attributes to Python objects.\n",
    "\n",
    "\n",
    "Just kidding. But it really is that simple. This is one aspect of Python which\n",
    "might be quite jarring to you if you are coming from a language with more\n",
    "rigid semantics, such as C++ or Java. In those languages, you must pre-specify\n",
    "all of the attributes and methods that are a part of a class. But Python is\n",
    "much more flexible - you can simply add attributes to an object after it has\n",
    "been created.  In fact, you can even do this outside of the class\n",
    "definition<sup>1</sup>:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fm = FSLMaths(inimg)\n",
    "fm.another_attribute = 'Haha'\n",
    "print(fm.another_attribute)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "__But ...__ while attributes can be added to a Python object at any time, it is\n",
    "good practice (and makes for more readable and maintainable code) to add all\n",
    "of an object's attributes within the `__init__` method.\n",
    "\n",
    "\n",
    "> <sup>1</sup>This not possible with many of the built-in types, such as\n",
    "> `list` and `dict` objects, nor with types that are defined in Python\n",
    "> extensions (Python modules that are written in C).\n",
    "\n",
    "\n",
    "<a class=\"anchor\" id=\"methods\"></a>\n",
    "## Methods\n",
    "\n",
    "\n",
    "We've been dilly-dallying on this little `FSLMaths` project for a while now,\n",
    "but our class still can't actually do anything. Let's start adding some\n",
    "functionality:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class FSLMaths(object):\n",
    "\n",
    "    def __init__(self, inimg):\n",
    "        self.img        = inimg\n",
    "        self.operations = []\n",
    "\n",
    "    def add(self, value):\n",
    "        self.operations.append(('add', value))\n",
    "\n",
    "    def mul(self, value):\n",
    "        self.operations.append(('mul', value))\n",
    "\n",
    "    def div(self, value):\n",
    "        self.operations.append(('div', value))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Woah woah, [slow down egg-head!](https://www.youtube.com/watch?v=yz-TemWooa4)\n",
    "We've modified `__init__` so that a second attribute called `operations` is\n",
    "added to our object - this `operations` attribute is simply a list.\n",
    "\n",
    "\n",
    "Then, we added a handful of methods - `add`, `mul`, and `div` - which each\n",
    "append a tuple to that `operations` list.\n",
    "\n",
    "\n",
    "> Note that, just like in the `__init__` method, the first argument that will\n",
    "> be passed to these methods is `self` - a reference to the object that the\n",
    "> method has been called on.\n",
    "\n",
    "\n",
    "The idea behind this design is that our `FSLMaths` class will not actually do\n",
    "anything when we call the `add`, `mul` or `div` methods. Instead, it will\n",
    "\"stage\" each operation, and then perform them all in one go. So let's add\n",
    "another method, `run`, which actually does the work:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy   as np\n",
    "import nibabel as nib\n",
    "\n",
    "class FSLMaths(object):\n",
    "\n",
    "    def __init__(self, inimg):\n",
    "        self.img        = inimg\n",
    "        self.operations = []\n",
    "\n",
    "    def add(self, value):\n",
    "        self.operations.append(('add', value))\n",
    "\n",
    "    def mul(self, value):\n",
    "        self.operations.append(('mul', value))\n",
    "\n",
    "    def div(self, value):\n",
    "        self.operations.append(('div', value))\n",
    "\n",
    "    def run(self, output=None):\n",
    "\n",
    "        data = np.array(self.img.get_data())\n",
    "\n",
    "        for oper, value in self.operations:\n",
    "\n",
    "            # Value could be an image.\n",
    "            # If not, we assume that\n",
    "            # it is a scalar/numpy array.\n",
    "            if isinstance(value, nib.nifti1.Nifti1Image):\n",
    "                value = value.get_data()\n",
    "\n",
    "\n",
    "            if oper == 'add':\n",
    "                data = data + value\n",
    "            elif oper == 'mul':\n",
    "                data = data * value\n",
    "            elif oper == 'div':\n",
    "                data = data / value\n",
    "\n",
    "        # turn final output into a nifti,\n",
    "        # and save it to disk if an\n",
    "        # 'output' has been specified.\n",
    "        outimg = nib.nifti1.Nifti1Image(data, inimg.affine)\n",
    "\n",
    "        if output is not None:\n",
    "            nib.save(outimg, output)\n",
    "\n",
    "        return outimg"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We now have a useable (but not very useful) `FSLMaths` class!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fpath = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')\n",
    "fmask = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm_brain_mask.nii.gz')\n",
    "inimg = nib.load(fpath)\n",
    "mask  = nib.load(fmask)\n",
    "fm    = FSLMaths(inimg)\n",
    "\n",
    "fm.mul(mask)\n",
    "fm.add(-10)\n",
    "\n",
    "outimg = fm.run()\n",
    "\n",
    "norigvox = (inimg .get_data() > 0).sum()\n",
    "nmaskvox = (outimg.get_data() > 0).sum()\n",
    "\n",
    "print('Number of voxels >0 in original image: {}'.format(norigvox))\n",
    "print('Number of voxels >0 in masked image:   {}'.format(nmaskvox))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a class=\"anchor\" id=\"protecting-attribute-access\"></a>\n",
    "## Protecting attribute access\n",
    "\n",
    "\n",
    "In our `FSLMaths` class, the input image was added as an attribute called\n",
    "`img` to `FSLMaths` objects. We saw that it is easy to read the attributes\n",
    "of an object - if we have a `FSLMaths` instance called `fm`, we can read its\n",
    "input image via `fm.img`.\n",
    "\n",
    "\n",
    "But it is just as easy to write the attributes of an object. What's to stop\n",
    "some sloppy research assistant from overwriting our `img` attribute?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "inimg = nib.load(op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz'))\n",
    "fm = FSLMaths(inimg)\n",
    "fm.img = None\n",
    "fm.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Well, the scary answer is ... there is __nothing__ stopping you from doing\n",
    "whatever you want to a Python object. You can add, remove, and modify\n",
    "attributes at will. You can even replace the methods of an existing object if\n",
    "you like:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fm = FSLMaths(inimg)\n",
    "\n",
    "def myadd(value):\n",
    "    print('Oh no, I\\'m not going to add {} to '\n",
    "          'your image. Go away!'.format(value))\n",
    "\n",
    "fm.add = myadd\n",
    "fm.add(123)\n",
    "\n",
    "fm.mul = None\n",
    "fm.mul(123)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "But you really shouldn't get into the habit of doing devious things like\n",
    "this. Think of the poor souls who inherit your code years after you have left\n",
    "the lab - if you go around overwriting all of the methods and attributes of\n",
    "your objects, they are not going to have a hope in hell of understanding what\n",
    "your code is actually doing, and they are not going to like you very\n",
    "much. Take a look at the appendix for a [brief discussion on this\n",
    "topic](appendix-monkey-patching).\n",
    "\n",
    "\n",
    "Python tends to assume that programmers are \"responsible adults\", and hence\n",
    "doesn't do much in the way of restricting access to the attributes or methods\n",
    "of an object. This is in contrast to languages like C++ and Java, where the\n",
    "notion of a private attribute or method is strictly enforced by the language.\n",
    "\n",
    "\n",
    "However, there are a couple of conventions in Python that are [universally\n",
    "adhered\n",
    "to](https://docs.python.org/3.5/tutorial/classes.html#private-variables):\n",
    "\n",
    "* Class-level attributes and methods, and module-level attributes, functions,\n",
    "  and classes, which begin with a single underscore (`_`), should be\n",
    "  considered __protected__ - they are intended for internal use only, and\n",
    "  should not be considered part of the public API of a class or module.  This\n",
    "  is not enforced by the language in any way<sup>2</sup> - remember, we are\n",
    "  all responsible adults here!\n",
    "\n",
    "* Class-level attributes and methods which begin with a double-underscore\n",
    "  (`__`) should be considered __private__. Python provides a weak form of\n",
    "  enforcement for this rule - any attribute or method with such a name will\n",
    "  actually be _renamed_ (in a standardised manner) at runtime, so that it is\n",
    "  not accessible through its original name (it is still accessible via its\n",
    "  [mangled\n",
    "  name](https://docs.python.org/3.5/tutorial/classes.html#private-variables)\n",
    "  though).\n",
    "\n",
    "\n",
    "> <sup>2</sup> With the exception that module-level fields which begin with a\n",
    "> single underscore will not be imported into the local scope via the\n",
    "> `from [module] import *` techinque.\n",
    "\n",
    "\n",
    "So with all of this in mind, we can adjust our `FSLMaths` class to discourage\n",
    "our sloppy research assistant from overwriting the `img` attribute:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# remainder of definition omitted for brevity\n",
    "class FSLMaths(object):\n",
    "    def __init__(self, inimg):\n",
    "        self.__img        = inimg\n",
    "        self.__operations = []"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "But now we have lost the ability to read our `__img` attribute:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "inimg = nib.load(op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz'))\n",
    "fm = FSLMaths(inimg)\n",
    "print(fm.__img)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a class=\"anchor\" id=\"a-better-way-properties\"></a>\n",
    "### A better way - properties\n",
    "\n",
    "\n",
    "Python has a feature called\n",
    "[`properties`](https://docs.python.org/3.5/library/functions.html#property),\n",
    "which is a nice way of controlling access to the attributes of an object. We\n",
    "can use properties by defining a \"getter\" method which can be used to access\n",
    "our attributes, and \"decorating\" them with the `@property` decorator (we will\n",
    "cover decorators in a later practical)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class FSLMaths(object):\n",
    "    def __init__(self, inimg):\n",
    "        self.__img        = inimg\n",
    "        self.__operations = []\n",
    "\n",
    "    @property\n",
    "    def img(self):\n",
    "        return self.__img"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So we are still storing our input image as a private attribute, but now we\n",
    "have made it available in a read-only manner via the public `img` property:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fpath = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')\n",
    "inimg = nib.load(fpath)\n",
    "fm    = FSLMaths(inimg)\n",
    "\n",
    "print(fm.img.get_filename())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that, even though we have defined `img` as a method, we can access it\n",
    "like an attribute - this is due to the magic behind the `@property` decorator.\n",
    "\n",
    "\n",
    "We can also define \"setter\" methods for a property. For example, we might wish\n",
    "to add the ability for a user of our `FSLMaths` class to change the input\n",
    "image after creation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class FSLMaths(object):\n",
    "    def __init__(self, inimg):\n",
    "        self.__img        = None\n",
    "        self.__operations = []\n",
    "        self.img          = inimg\n",
    "\n",
    "    @property\n",
    "    def img(self):\n",
    "        return self.__img\n",
    "\n",
    "    @img.setter\n",
    "    def img(self, value):\n",
    "        if not isinstance(value, nib.nifti1.Nifti1Image):\n",
    "            raise ValueError('value must be a NIFTI image!')\n",
    "        self.__img = value"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Note that we used the `img` setter method within `__init__` to validate the\n",
    "> initial `inimg` that was passed in during creation.\n",
    "\n",
    "\n",
    "Property setters are a nice way to add validation logic for when an attribute\n",
    "is assigned a value. In this example, an error will be raised if the new input\n",
    "is not a NIFTI image."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fpath = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')\n",
    "inimg = nib.load(fpath)\n",
    "fm    = FSLMaths(inimg)\n",
    "\n",
    "print('Input:     ', fm.img.get_filename())\n",
    "\n",
    "# let's change the input\n",
    "# to a different image\n",
    "fpath2 = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm_brain.nii.gz')\n",
    "inimg2 = nib.load(fpath2)\n",
    "fm.img = inimg2\n",
    "\n",
    "print('New input: ', fm.img.get_filename())\n",
    "\n",
    "print('This is going to explode')\n",
    "fm.img = 'abcde'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a class=\"anchor\" id=\"inheritance\"></a>\n",
    "## Inheritance\n",
    "\n",
    "\n",
    "One of the major advantages of an object-oriented programming approach is\n",
    "_inheritance_ - the ability to define hierarchical relationships between\n",
    "classes and instances.\n",
    "\n",
    "\n",
    "<a class=\"anchor\" id=\"the-basics\"></a>\n",
    "### The basics\n",
    "\n",
    "\n",
    "My local veterinary surgery runs some Python code which looks like the\n",
    "following, to assist the nurses in identifying an animal when it arrives at\n",
    "the surgery:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Animal(object):\n",
    "    def noiseMade(self):\n",
    "        raise NotImplementedError('This method must be '\n",
    "                                  'implemented by sub-classes')\n",
    "\n",
    "class Dog(Animal):\n",
    "    def noiseMade(self):\n",
    "        return 'Woof'\n",
    "\n",
    "class TalkingDog(Dog):\n",
    "    def noiseMade(self):\n",
    "        return 'Hi Homer, find your soulmate!'\n",
    "\n",
    "class Cat(Animal):\n",
    "    def noiseMade(self):\n",
    "        return 'Meow'\n",
    "\n",
    "class Labrador(Dog):\n",
    "    pass\n",
    "\n",
    "class Chihuahua(Dog):\n",
    "    def noiseMade(self):\n",
    "        return 'Yap yap yap'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Hopefully this example doesn't need much in the way of explanation - this\n",
    "collection of classes captures a hierarchical relationship which exists in the\n",
    "real world (and also captures the inherently annoying nature of\n",
    "chihuahuas). For example, in the real world, all dogs are animals, but not all\n",
    "animals are dogs.  Therefore in our model, the `Dog` class has specified\n",
    "`Animal` as its base class. We say that the `Dog` class _extends_, _derives\n",
    "from_, or _inherits from_, the `Animal` class, and that all `Dog` instances\n",
    "are also `Animal` instances (but not vice-versa).\n",
    "\n",
    "\n",
    "What does that `noiseMade` method do?  There is a `noiseMade` method defined\n",
    "on the `Animal` class, but it has been re-implemented, or _overridden_ in the\n",
    "`Dog`,\n",
    "[`TalkingDog`](https://twitter.com/simpsonsqotd/status/427941665836630016?lang=en),\n",
    "`Cat`, and `Chihuahua` classes (but not on the `Labrador` class).  We can call\n",
    "the `noiseMade` method on any `Animal` instance, but the specific behaviour\n",
    "that we get is dependent on the specific type of animal."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d  = Dog()\n",
    "l  = Labrador()\n",
    "c  = Cat()\n",
    "ch = Chihuahua()\n",
    "\n",
    "print('Noise made by dogs:       {}'.format(d .noiseMade()))\n",
    "print('Noise made by labradors:  {}'.format(l .noiseMade()))\n",
    "print('Noise made by cats:       {}'.format(c .noiseMade()))\n",
    "print('Noise made by chihuahuas: {}'.format(ch.noiseMade()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that calling the `noiseMade` method on a `Labrador` instance resulted in\n",
    "the `Dog.noiseMade` implementation being called.\n",
    "\n",
    "\n",
    "<a class=\"anchor\" id=\"code-re-use-and-problem-decomposition\"></a>\n",
    "### Code re-use and problem decomposition\n",
    "\n",
    "\n",
    "Inheritance allows us to split a problem into smaller problems, and to re-use\n",
    "code.  Let's demonstrate this with a more involved (and even more contrived)\n",
    "example.  Imagine that a former colleague had written a class called\n",
    "`Operator`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Operator(object):\n",
    "\n",
    "    def __init__(self):\n",
    "        super().__init__() # this line will be explained later\n",
    "        self.__operations = []\n",
    "        self.__opFuncs    = {}\n",
    "\n",
    "    @property\n",
    "    def operations(self):\n",
    "        return list(self.__operations)\n",
    "\n",
    "    @property\n",
    "    def functions(self):\n",
    "        return dict(self.__opFuncs)\n",
    "\n",
    "    def addFunction(self, name, func):\n",
    "        self.__opFuncs[name] = func\n",
    "\n",
    "    def do(self, name, *values):\n",
    "        self.__operations.append((name, values))\n",
    "\n",
    "    def preprocess(self, value):\n",
    "        return value\n",
    "\n",
    "    def run(self, input):\n",
    "        data = self.preprocess(input)\n",
    "        for oper, vals in self.__operations:\n",
    "            func = self.__opFuncs[oper]\n",
    "            vals = [self.preprocess(v) for v in vals]\n",
    "            data = func(data, *vals)\n",
    "        return data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This `Operator` class provides an interface and logic to execute a chain of\n",
    "operations - an operation is some function which accepts one or more inputs,\n",
    "and produce one output.\n",
    "\n",
    "\n",
    "But it stops short of defining any operations. Instead, we can create another\n",
    "class - a sub-class - which derives from the `Operator` class. This sub-class\n",
    "will define the operations that will ultimately be executed by the `Operator`\n",
    "class. All that the `Operator` class does is:\n",
    "\n",
    "- Allow functions to be registered with the `addFunction` method - all\n",
    "  registered functions can be used via the `do` method.\n",
    "\n",
    "- Stage an operation (using a registered function) via the `do` method. Note\n",
    "  that `do` allows any number of values to be passed to it, as we used the `*`\n",
    "  operator when specifying the `values` argument.\n",
    "\n",
    "- Run all staged operations via the `run` method - it passes an input through\n",
    "  all of the operations that have been staged, and then returns the final\n",
    "  result.\n",
    "\n",
    "\n",
    "Let's define a sub-class:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class NumberOperator(Operator):\n",
    "\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.addFunction('add',    self.add)\n",
    "        self.addFunction('mul',    self.mul)\n",
    "        self.addFunction('negate', self.negate)\n",
    "\n",
    "    def preprocess(self, value):\n",
    "        return float(value)\n",
    "\n",
    "    def add(self, a, b):\n",
    "        return a + b\n",
    "\n",
    "    def mul(self, a, b):\n",
    "        return a * b\n",
    "\n",
    "    def negate(self, a):\n",
    "        return -a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `NumberOperator` is a sub-class of `Operator`, which we can use for basic\n",
    "numerical calculations. It provides a handful of simple numerical methods, but\n",
    "the most interesting stuff is inside `__init__`.\n",
    "\n",
    "\n",
    "> ```\n",
    "> super().__init__()\n",
    "> ```\n",
    "\n",
    "\n",
    "This line invokes `Operator.__init__` - the initialisation method for the\n",
    "`Operator` base-class.\n",
    "\n",
    "\n",
    "In Python, we can use the [built-in `super`\n",
    "method](https://docs.python.org/3.5/library/functions.html#super) to take care\n",
    "of correctly calling methods that are defined in an object's base-class (or\n",
    "classes, in the case of [multiple inheritance](multiple-inheritance)).\n",
    "\n",
    "\n",
    "> The `super` function is one thing which changed between Python 2 and 3 -\n",
    "> in Python 2, it was necessary to pass both the type and the instance\n",
    "> to `super`. So it is common to see code that looks like this:\n",
    ">\n",
    "> ```\n",
    "> def __init__(self):\n",
    ">     super(NumberOperator, self).__init__()\n",
    "> ```\n",
    ">\n",
    "> Fortunately things are a lot cleaner in Python 3.\n",
    "\n",
    "\n",
    "Let's move on to the next few lines in `__init__`:\n",
    "\n",
    "\n",
    "> ```\n",
    "> self.addFunction('add',    self.add)\n",
    "> self.addFunction('mul',    self.mul)\n",
    "> self.addFunction('negate', self.negate)\n",
    "> ```\n",
    "\n",
    "\n",
    "Here we are registering all of the functionality that is provided by the\n",
    "`NumberOperator` class, via the `Operator.addFunction` method.\n",
    "\n",
    "\n",
    "The `NumberOperator` class has also overridden the `preprocess` method, to\n",
    "ensure that all values handled by the `Operator` are numbers. This method gets\n",
    "called within the `Operator.run` method - for a `NumberOperator` instance, the\n",
    "`NumberOperator.preprocess` method will get called<sup>3</sup>.\n",
    "\n",
    "\n",
    "> <sup>3</sup> When a sub-class overrides a base-class method, it is still\n",
    "> possible to access the base-class implementation [via the `super()`\n",
    "> function](https://stackoverflow.com/a/4747427) (the preferred method), or by\n",
    "> [explicitly calling the base-class\n",
    "> implementation](https://stackoverflow.com/a/2421325).\n",
    "\n",
    "\n",
    "Now let's see what our `NumberOperator` class does:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "no = NumberOperator()\n",
    "no.do('add', 10)\n",
    "no.do('mul', 2)\n",
    "no.do('negate')\n",
    "\n",
    "print('Operations on {}: {}'.format(10,  no.run(10)))\n",
    "print('Operations on {}: {}'.format(2.5, no.run(5)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It works! While this is a contrived example, hopefully you can see how\n",
    "inheritance can be used to break a problem down into sub-problems:\n",
    "\n",
    "- The `Operator` class provides all of the logic needed to manage and execute\n",
    "  operations, without caring about what those operations are actually doing.\n",
    "\n",
    "- This leaves the `NumberOperator` class free to concentrate on implementing\n",
    "  the functions that are specific to its task, and not having to worry about\n",
    "  how they are executed.\n",
    "\n",
    "\n",
    "We could also easily implement other `Operator` sub-classes to work on\n",
    "different data types, such as arrays, images, or even non-numeric data such as\n",
    "strings:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class StringOperator(Operator):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.addFunction('capitalise', self.capitalise)\n",
    "        self.addFunction('concat',     self.concat)\n",
    "\n",
    "    def preprocess(self, value):\n",
    "        return str(value)\n",
    "\n",
    "    def capitalise(self, s):\n",
    "        return ' '.join([w[0].upper() + w[1:] for w in s.split()])\n",
    "\n",
    "    def concat(self, s1, s2):\n",
    "        return s1 + s2\n",
    "\n",
    "so = StringOperator()\n",
    "so.do('capitalise')\n",
    "so.do('concat', '!')\n",
    "\n",
    "print(so.run('python is an ok language'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a class=\"anchor\" id=\"polymorphism\"></a>\n",
    "### Polymorphism\n",
    "\n",
    "\n",
    "Inheritance also allows us to take advantage of _polymorphism_, which refers\n",
    "to idea that, in an object-oriented language, we should be able to use an\n",
    "object without having complete knowledge about the class, or type, of that\n",
    "object. For example, we should be able to write a function which expects an\n",
    "`Operator` instance, but which will work on an instance of any `Operator`\n",
    "sub-classs. As an example, let's write a function which prints a summary of an\n",
    "`Operator` instance:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def operatorSummary(o):\n",
    "    print(type(o).__name__)\n",
    "    print('  All functions: ')\n",
    "    for fname in o.functions.keys():\n",
    "        print('    {}'.format(fname))\n",
    "    print('  Staged operations: ')\n",
    "    for i, (fname, vals) in enumerate(o.operations):\n",
    "        vals = ', '.join([str(v) for v in vals])\n",
    "        print('    {}: {}({})'.format(i + 1, fname, vals))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Because the `operatorSummary` function only uses methods that are defined\n",
    "in the `Operator` base-class, we can use it on _any_ `Operator` instance,\n",
    "regardless of its specific type:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "operatorSummary(no)\n",
    "operatorSummary(so)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a class=\"anchor\" id=\"multiple-inheritance\"></a>\n",
    "### Multiple inheritance\n",
    "\n",
    "\n",
    "Python allows you to define a class which has multiple base classes - this is\n",
    "known as _multiple inheritance_. For example, we might want to build a\n",
    "notification mechanisim into our `StringOperator` class, so that listeners can\n",
    "be notified whenever the `capitalise` method gets called. It so happens that\n",
    "our old colleague of `Operator` class fame also wrote a `Notifier` class which\n",
    "allows listeners to register to be notified when an event occurs:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Notifier(object):\n",
    "\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.__listeners = {}\n",
    "\n",
    "    def register(self, name, func):\n",
    "        self.__listeners[name] = func\n",
    "\n",
    "    def notify(self, *args, **kwargs):\n",
    "        for func in self.__listeners.values():\n",
    "            func(*args, **kwargs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's modify the `StringOperator` class to use the functionality of the\n",
    "`Notifier ` class:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class StringOperator(Operator, Notifier):\n",
    "\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.addFunction('capitalise', self.capitalise)\n",
    "        self.addFunction('concat',     self.concat)\n",
    "\n",
    "    def preprocess(self, value):\n",
    "        return str(value)\n",
    "\n",
    "    def capitalise(self, s):\n",
    "        result = ' '.join([w[0].upper() + w[1:] for w in s.split()])\n",
    "        self.notify(result)\n",
    "        return result\n",
    "\n",
    "    def concat(self, s1, s2):\n",
    "        return s1 + s2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, anything which is interested in uses of the `capitalise` method can\n",
    "register as a listener on a `StringOperator` instance:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "so = StringOperator()\n",
    "\n",
    "def capitaliseCalled(result):\n",
    "    print('Capitalise operation called: {}'.format(result))\n",
    "\n",
    "so.register('mylistener', capitaliseCalled)\n",
    "\n",
    "so.do('capitalise')\n",
    "so.do('concat', '?')\n",
    "\n",
    "print(so.run('did you notice that functions are objects too'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Simple classes such as the `Notifier` are sometimes referred to as\n",
    "  [_mixins_](https://en.wikipedia.org/wiki/Mixin).\n",
    "\n",
    "\n",
    "If you wish to use multiple inheritance in your design, it is important to be\n",
    "aware of the mechanism that Python uses to determine how base class methods\n",
    "are called (and which base class method will be called, in the case of naming\n",
    "conflicts). This is referred to as the Method Resolution Order (MRO) - further\n",
    "details on the topic can be found\n",
    "[here](https://www.python.org/download/releases/2.3/mro/), and a more concise\n",
    "summary\n",
    "[here](http://python-history.blogspot.co.uk/2010/06/method-resolution-order.html).\n",
    "\n",
    "\n",
    "Note also that for base class `__init__` methods to be correctly called in a\n",
    "design which uses multiple inheritance, _all_ classes in the hierarchy must\n",
    "invoke `super().__init__()`. This can become complicated when some base\n",
    "classes expect to be passed arguments to their `__init__` method. In scenarios\n",
    "like this it may be prefereable to manually invoke the base class `__init__`\n",
    "methods instead of using `super()`. For example:\n",
    "\n",
    "\n",
    "> ```\n",
    "> class StringOperator(Operator, Notifier):\n",
    ">     def __init__(self):\n",
    ">         Operator.__init__(self)\n",
    ">         Notifier.__init__(self)\n",
    "> ```\n",
    "\n",
    "\n",
    "This approach has the disadvantage that if the base classes change, you will\n",
    "have to change these invocations. But the advantage is that you know exactly\n",
    "how the class hierarchy will be initialised. In general though, doing\n",
    "everything with `super()` will result in more maintainable code.\n",
    "\n",
    "\n",
    "<a class=\"anchor\" id=\"class-attributes-and-methods\"></a>\n",
    "## Class attributes and methods\n",
    "\n",
    "\n",
    "Up to this point we have been covering how to add attributes and methods to an\n",
    "_object_. But it is also possible to add methods and attributes to a _class_\n",
    "(`static` methods and fields, for those of you familiar with C++ or Java).\n",
    "\n",
    "\n",
    "Class attributes and methods can be accessed without having to create an\n",
    "instance of the class - they are not associated with individual objects, but\n",
    "rather with the class itself.\n",
    "\n",
    "\n",
    "Class methods and attributes can be useful in several scenarios - as a\n",
    "hypothetical, not very useful example, let's say that we want to gain usage\n",
    "statistics for how many times each type of operation is used on instances of\n",
    "our `FSLMaths` class. We might, for example, use this information in a grant\n",
    "application to show evidence that more research is needed to optimise the\n",
    "performance of the `add` operation.\n",
    "\n",
    "\n",
    "<a class=\"anchor\" id=\"class-attributes\"></a>\n",
    "### Class attributes\n",
    "\n",
    "\n",
    "Let's add a `dict` called `opCounters` as a class attribute to the `FSLMaths`\n",
    "class - whenever an operation is called on a `FSLMaths` instance, the counter\n",
    "for that operation will be incremented:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy   as np\n",
    "import nibabel as nib\n",
    "\n",
    "class FSLMaths(object):\n",
    "\n",
    "    # It's this easy to add a class-level\n",
    "    # attribute. This dict is associated\n",
    "    # with the FSLMaths *class*, not with\n",
    "    # any individual FSLMaths instance.\n",
    "    opCounters = {}\n",
    "\n",
    "    def __init__(self, inimg):\n",
    "        self.img        = inimg\n",
    "        self.operations = []\n",
    "\n",
    "    def add(self, value):\n",
    "        self.operations.append(('add', value))\n",
    "\n",
    "    def mul(self, value):\n",
    "        self.operations.append(('mul', value))\n",
    "\n",
    "    def div(self, value):\n",
    "        self.operations.append(('div', value))\n",
    "\n",
    "    def run(self, output=None):\n",
    "\n",
    "        data = np.array(self.img.get_data())\n",
    "\n",
    "        for oper, value in self.operations:\n",
    "\n",
    "            # Code omitted for brevity\n",
    "\n",
    "            # Increment the usage counter\n",
    "            # for this operation. We can\n",
    "            # access class attributes (and\n",
    "            # methods) through the class\n",
    "            # itself.\n",
    "            FSLMaths.opCounters[oper] = self.opCounters.get(oper, 0) + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So let's see it in action:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fpath = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')\n",
    "fmask = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm_brain_mask.nii.gz')\n",
    "inimg = nib.load(fpath)\n",
    "mask  = nib.load(fmask)\n",
    "\n",
    "fm1 = FSLMaths(inimg)\n",
    "fm2 = FSLMaths(inimg)\n",
    "\n",
    "fm1.mul(mask)\n",
    "fm1.add(15)\n",
    "\n",
    "fm2.add(25)\n",
    "fm1.div(1.5)\n",
    "\n",
    "fm1.run()\n",
    "fm2.run()\n",
    "\n",
    "print('FSLMaths usage statistics')\n",
    "for oper in ('add', 'div', 'mul'):\n",
    "    print('  {} : {}'.format(oper, FSLMaths.opCounters.get(oper, 0)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a class=\"anchor\" id=\"class-methods\"></a>\n",
    "### Class methods\n",
    "\n",
    "\n",
    "It is just as easy to add a method to a class - let's take our reporting code\n",
    "from above, and add it as a method to the `FSLMaths` class.\n",
    "\n",
    "\n",
    "A class method is denoted by the\n",
    "[`@classmethod`](https://docs.python.org/3.5/library/functions.html#classmethod)\n",
    "decorator. Note that, where a regular method which is called on an instance\n",
    "will be passed the instance as its first argument (`self`), a class method\n",
    "will be passed the class itself as the first argument - the standard\n",
    "convention is to call this argument `cls`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class FSLMaths(object):\n",
    "\n",
    "    opCounters = {}\n",
    "\n",
    "    @classmethod\n",
    "    def usage(cls):\n",
    "        print('FSLMaths usage statistics')\n",
    "        for oper in ('add', 'div', 'mul'):\n",
    "            print('  {} : {}'.format(oper, FSLMaths.opCounters.get(oper, 0)))\n",
    "\n",
    "    def __init__(self, inimg):\n",
    "        self.img        = inimg\n",
    "        self.operations = []\n",
    "\n",
    "    def add(self, value):\n",
    "        self.operations.append(('add', value))\n",
    "\n",
    "    def mul(self, value):\n",
    "        self.operations.append(('mul', value))\n",
    "\n",
    "    def div(self, value):\n",
    "        self.operations.append(('div', value))\n",
    "\n",
    "    def run(self, output=None):\n",
    "\n",
    "        data = np.array(self.img.get_data())\n",
    "\n",
    "        for oper, value in self.operations:\n",
    "            FSLMaths.opCounters[oper] = self.opCounters.get(oper, 0) + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> There is another decorator -\n",
    "> [`@staticmethod`](https://docs.python.org/3.5/library/functions.html#staticmethod) -\n",
    "> which can be used on methods defined within a class. The difference\n",
    "> between a `@classmethod` and a `@staticmethod` is that the latter will _not_\n",
    "> be passed the class (`cls`).\n",
    "\n",
    "\n",
    "calling a class method is the same as accessing a class attribute:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fpath = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')\n",
    "fmask = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm_brain_mask.nii.gz')\n",
    "inimg = nib.load(fpath)\n",
    "mask  = nib.load(fmask)\n",
    "\n",
    "fm1 = FSLMaths(inimg)\n",
    "fm2 = FSLMaths(inimg)\n",
    "\n",
    "fm1.mul(mask)\n",
    "fm1.add(15)\n",
    "\n",
    "fm2.add(25)\n",
    "fm1.div(1.5)\n",
    "\n",
    "fm1.run()\n",
    "fm2.run()\n",
    "\n",
    "FSLMaths.usage()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that it is also possible to access class attributes and methods through\n",
    "instances:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(fm1.opCounters)\n",
    "fm1.usage()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a class=\"anchor\" id=\"appendix-the-object-base-class\"></a>\n",
    "## Appendix: The `object` base-class\n",
    "\n",
    "\n",
    "When you are defining a class, you need to specify the base-class from which\n",
    "your class inherits. If your class does not inherit from a particular class,\n",
    "then it should inherit from the built-in `object` class:\n",
    "\n",
    "\n",
    "> ```\n",
    "> class MyClass(object):\n",
    ">     ...\n",
    "> ```\n",
    "\n",
    "\n",
    "However, in older code bases, you might see class definitions that look like\n",
    "this, without explicitly inheriting from the `object` base class:\n",
    "\n",
    "\n",
    "> ```\n",
    "> class MyClass:\n",
    ">     ...\n",
    "> ```\n",
    "\n",
    "\n",
    "This syntax is a [throwback to older versions of\n",
    "Python](https://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes).\n",
    "In Python 3 there is actually no difference in defining classes in the\n",
    "\"new-style\" way we have used throughout this tutorial, or the \"old-style\" way\n",
    "mentioned in this appendix.\n",
    "\n",
    "\n",
    "But if you are writing code which needs to run on both Python 2 and 3, you\n",
    "__must__ define your classes in the new-style convention, i.e. by explicitly\n",
    "inheriting from the `object` base class. Therefore, the safest approach is to\n",
    "always use the new-style format.\n",
    "\n",
    "\n",
    "<a class=\"anchor\" id=\"appendix-init-versus-new\"></a>\n",
    "## Appendix: `__init__` versus `__new__`\n",
    "\n",
    "\n",
    "In Python, object creation is actually a two-stage process - _creation_, and\n",
    "then _initialisation_. The `__init__` method gets called during the\n",
    "_initialisation_ stage - its job is to initialise the state of the object. But\n",
    "note that, by the time `__init__` gets called, the object has already been\n",
    "created.\n",
    "\n",
    "\n",
    "You can also define a method called `__new__` if you need to control the\n",
    "creation stage, although this is very rarely needed. One example of where you\n",
    "might need to implement the `__new__` method is if you wish to create a\n",
    "[subclass of a\n",
    "`numpy.array`](https://docs.scipy.org/doc/numpy-1.14.0/user/basics.subclassing.html)\n",
    "(although you might alternatively want to think about redefining your problem\n",
    "so that this is not necessary).\n",
    "\n",
    "\n",
    "A brief explanation on\n",
    "the difference between `__new__` and `__init__` can be found\n",
    "[here](https://www.reddit.com/r/learnpython/comments/2s3pms/what_is_the_difference_between_init_and_new/cnm186z/),\n",
    "and you may also wish to take a look at the [official Python\n",
    "docs](https://docs.python.org/3.5/reference/datamodel.html#basic-customization).\n",
    "\n",
    "\n",
    "<a class=\"anchor\" id=\"appendix-monkey-patching\"></a>\n",
    "## Appendix: Monkey-patching\n",
    "\n",
    "\n",
    "The act of run-time modification of objects or class definitions is referred\n",
    "to as [_monkey-patching_](https://en.wikipedia.org/wiki/Monkey_patch) and,\n",
    "whilst it is allowed by the Python programming language, it is generally\n",
    "considered quite bad practice.\n",
    "\n",
    "\n",
    "Just because you _can_ do something doesn't mean that you _should_. Python\n",
    "gives you the flexibility to write your software in whatever manner you deem\n",
    "suitable.  __But__ if you want to write software that will be used, adopted,\n",
    "maintained, and enjoyed by other people, you should be polite, write your code\n",
    "in a clear, readable fashion, and avoid the use of devious tactics such as\n",
    "monkey-patching.\n",
    "\n",
    "\n",
    "__However__, while monkey-patching may seem like a horrific programming\n",
    "practice to those of you coming from the realms of C++, Java, and the like,\n",
    "(and it is horrific in many cases), it can be _extremely_ useful in certain\n",
    "circumstances.  For instance, monkey-patching makes [unit testing a\n",
    "breeze in Python](https://docs.python.org/3.5/library/unittest.mock.html).\n",
    "\n",
    "\n",
    "As another example, consider the scenario where you are dependent on a third\n",
    "party library which has bugs in it. No problem - while you are waiting for the\n",
    "library author to release a new version of the library, you can write your own\n",
    "working implementation and [monkey-patch it\n",
    "in!](https://git.fmrib.ox.ac.uk/fsl/fsleyes/fsleyes/blob/0.21.0/fsleyes/views/viewpanel.py#L726)\n",
    "\n",
    "\n",
    "<a class=\"anchor\" id=\"appendix-method-overloading\"></a>\n",
    "## Appendix: Method overloading\n",
    "\n",
    "\n",
    "Method overloading (defining multiple methods with the same name in a class,\n",
    "but each accepting different arguments) is one of the only object-oriented\n",
    "features that is not present in Python. Becuase Python does not perform any\n",
    "runtime checks on the types of arguments that are passed to a method, or the\n",
    "compatibility of the method to accept the arguments, it would not be possible\n",
    "to determine which implementation of a method is to be called. In other words,\n",
    "in Python only the name of a method is used to identify that method, unlike in\n",
    "C++ and Java, where the full method signature (name, input types and return\n",
    "types) is used.\n",
    "\n",
    "\n",
    "However, because a Python method can be written to accept any number or type\n",
    "of arguments, it is very easy to to build your own overloading logic by\n",
    "writing a \"dispatch\" method<sup>4</sup>. Here is YACE (Yet Another Contrived\n",
    "Example):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Adder(object):\n",
    "\n",
    "    def add(self, *args):\n",
    "        if   len(args) == 2: return self.__add2(*args)\n",
    "        elif len(args) == 3: return self.__add3(*args)\n",
    "        elif len(args) == 4: return self.__add4(*args)\n",
    "        else:\n",
    "            raise AttributeError('No method available to accept {} '\n",
    "                                 'arguments'.format(len(args)))\n",
    "\n",
    "    def __add2(self, a, b):\n",
    "        return a + b\n",
    "\n",
    "    def __add3(self, a, b, c):\n",
    "        return a + b + c\n",
    "\n",
    "    def __add4(self, a, b, c, d):\n",
    "        return a + b + c + d\n",
    "\n",
    "a = Adder()\n",
    "\n",
    "print('Add two:   {}'.format(a.add(1, 2)))\n",
    "print('Add three: {}'.format(a.add(1, 2, 3)))\n",
    "print('Add four:  {}'.format(a.add(1, 2, 3, 4)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> <sup>4</sup>Another option is the [`functools.singledispatch`\n",
    "> decorator](https://docs.python.org/3.5/library/functools.html#functools.singledispatch),\n",
    "> which is more complicated, but may allow you to write your dispatch logic in\n",
    "> a more concise manner.\n",
    "\n",
    "\n",
    "<a class=\"anchor\" id=\"useful-references\"></a>\n",
    "## Useful references\n",
    "\n",
    "\n",
    "The official Python documentation has a wealth of information on the internal\n",
    "workings of classes and objects, so these pages are worth a read:\n",
    "\n",
    "\n",
    "* https://docs.python.org/3.5/tutorial/classes.html\n",
    "* https://docs.python.org/3.5/reference/datamodel.html"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}