Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
win-pytreat
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FSL
win-pytreat
Commits
def69741
Commit
def69741
authored
7 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
more oop updates
parent
8c1cc914
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
advanced_topics/object_oriented_programming.ipynb
+454
-31
454 additions, 31 deletions
advanced_topics/object_oriented_programming.ipynb
advanced_topics/object_oriented_programming.md
+380
-30
380 additions, 30 deletions
advanced_topics/object_oriented_programming.md
with
834 additions
and
61 deletions
advanced_topics/object_oriented_programming.ipynb
+
454
−
31
View file @
def69741
...
@@ -4,7 +4,7 @@
...
@@ -4,7 +4,7 @@
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
"source": [
"source": [
"# Object-oriented programming\n",
"# Object-oriented programming
in Python
\n",
"\n",
"\n",
"\n",
"\n",
"By now you might have realised that __everything__ in Python is an\n",
"By now you might have realised that __everything__ in Python is an\n",
...
@@ -63,7 +63,7 @@
...
@@ -63,7 +63,7 @@
"> ```\n",
"> ```\n",
"\n",
"\n",
"\n",
"\n",
"
The fundamental
difference between a `struct` in C, and a `class` in Python\n",
"
One of the major
difference
s
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",
"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",
"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",
"functions to your class definition, which will then be added as methods when\n",
...
@@ -71,10 +71,11 @@
...
@@ -71,10 +71,11 @@
"\n",
"\n",
"\n",
"\n",
"Of course there are many more differences between C structs and classes (most\n",
"Of course there are many more differences between C structs and classes (most\n",
"notably [inheritance](todo), and [protection](todo)). But if you can\n",
"notably [inheritance](todo), [polymorphism](todo), and [access\n",
"understand the difference between a _definition_ of a C struct, and an\n",
"protection](todo)). But if you can understand the difference between a\n",
"_instantiation_ of that struct, then you are most of the way towards\n",
"_definition_ of a C struct, and an _instantiation_ of that struct, then you\n",
"understanding the difference between a Python _class_, and a Python _object_.\n",
"are most of the way towards understanding the difference between a _class_,\n",
"and an _object_.\n",
"\n",
"\n",
"\n",
"\n",
"> But just to confuse you, remember that in Python, __everything__ is an\n",
"> But just to confuse you, remember that in Python, __everything__ is an\n",
...
@@ -154,9 +155,10 @@
...
@@ -154,9 +155,10 @@
"metadata": {},
"metadata": {},
"source": [
"source": [
"Here we have added a _method_ called `__init__` to our class (remember that a\n",
"Here we have added a _method_ called `__init__` to our class (remember that a\n",
"_method_ is just a function which is associated with a specific object). This\n",
"_method_ is just a function which is defined in a cliass, and which can be\n",
"method expects two arguments - `self`, and `inimg`. So now, when we create an\n",
"called on instances of that class). This method expects two arguments -\n",
"instance of the `FSLMaths` class, we will need to provide an input image:"
"`self`, and `inimg`. So now, when we create an instance of the `FSLMaths`\n",
"class, we will need to provide an input image:"
]
]
},
},
{
{
...
@@ -399,19 +401,13 @@
...
@@ -399,19 +401,13 @@
"\n",
"\n",
" for oper, value in self.operations:\n",
" for oper, value in self.operations:\n",
"\n",
"\n",
" # If value is a string, we assume\n",
" # Values could be an image that\n",
" # that it is a path to an image.\n",
" if isinstance(value, str):\n",
" image = nib.load(value)\n",
" value = image.get_data()\n",
"\n",
" # Or it could be an image that\n",
" # has already been loaded.\n",
" # has already been loaded.\n",
" elif isinstance(value, nib.nifti1.Nifti1Image):\n",
" elif isinstance(value, nib.nifti1.Nifti1Image):\n",
" value =
imag
e.get_data()\n",
" value =
valu
e.get_data()\n",
"\n",
"\n",
" # Otherwise we assume that\n",
" # Otherwise we assume that\n",
" #
it i
s a scalar
value
.\n",
" #
value
s a
re
scalar
s
.\n",
"\n",
"\n",
" if oper == 'add':\n",
" if oper == 'add':\n",
" data = data + value\n",
" data = data + value\n",
...
@@ -445,10 +441,12 @@
...
@@ -445,10 +441,12 @@
"outputs": [],
"outputs": [],
"source": [
"source": [
"input = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')\n",
"input = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')\n",
"inimg = nib.load(input)\n",
"mask = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm_brain_mask.nii.gz')\n",
"fm = FSLMaths(inimg)\n",
"input = nib.load(input)\n",
"mask = nib.load(mask)\n",
"fm = FSLMaths(input)\n",
"\n",
"\n",
"fm.mul(
op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm_brain_mask.nii.gz')
)\n",
"fm.mul(
mask
)\n",
"fm.add(-10)\n",
"fm.add(-10)\n",
"\n",
"\n",
"outimg = fm.run()\n",
"outimg = fm.run()\n",
...
@@ -495,7 +493,7 @@
...
@@ -495,7 +493,7 @@
"source": [
"source": [
"Well, the scary answer is ... there is __nothing__ stopping you from doing\n",
"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",
"whatever you want to a Python object. You can add, remove, and modify\n",
"attribute at will. You can even replace the methods of an existing object if\n",
"attribute
s
at will. You can even replace the methods of an existing object if\n",
"you like:"
"you like:"
]
]
},
},
...
@@ -529,7 +527,7 @@
...
@@ -529,7 +527,7 @@
"Python tends to assume that programmers are \"responsible adults\", and hence\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",
"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",
"of an object. This is in contrast to languages like C++ and Java, where the\n",
"notion of a private attribute or method is enforced by the language.\n",
"notion of a private attribute or method is
strictly
enforced by the language.\n",
"\n",
"\n",
"\n",
"\n",
"However, there are a couple of conventions in Python that are [universally\n",
"However, there are a couple of conventions in Python that are [universally\n",
...
@@ -685,9 +683,9 @@
...
@@ -685,9 +683,9 @@
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
"source": [
"source": [
"Property setters are a nice way to
restrict the values that a property may
\n",
"Property setters are a nice way to
add validation logic when an attribute is
\n",
"
take - note that we perform a sanity check in the `input` setter, to make
\n",
"
assigned a value. We are doing this in the above example, by making sure that
\n",
"
sure that
the new input is a NIFTI image:"
"the new input is a NIFTI image:"
]
]
},
},
{
{
...
@@ -722,6 +720,370 @@
...
@@ -722,6 +720,370 @@
"> validate the initial `inimg` that was passed in during creation.\n",
"> validate the initial `inimg` that was passed in during creation.\n",
"\n",
"\n",
"\n",
"\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",
"### The basics\n",
"\n",
"\n",
"For example, a veterinary surgery might be running some Python code which\n",
"looks like the following. Perhaps it is used to assist the nurses in\n",
"identifying an animal when it arrives at the surgery:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class Animal(object):\n",
" def noiseMade(self):\n",
" raise NotImplementedError('This method is implemented by sub-classes')\n",
"\n",
"class Dog(Animal):\n",
" def noiseMade(self):\n",
" return 'Woof'\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`, `Cat`, and `Chihuahua` classes (but not on the `Labrador` class). We\n",
"can call the `noiseMade` method on any `Animal` instance, but the specific\n",
"behaviour 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": [
"### 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 example. Imagine that a\n",
"former colleague had written a class called `Operator`:\n",
"\n",
"\n",
"> I know this is a little abstract (and quite contrived), but bear with me\n",
"> here."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class Operator(object):\n",
"\n",
" def __init__(self):\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. 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](todo)).\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 `Opoerator.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 `run` method - for a `NumberOperator` instance, the\n",
"`NumberOperator.preprocess` method will get called<sup>1</sup>.\n",
"\n",
"> <sup>1</sup> We can still [access overridden base-class methods](todo link)\n",
"> via the `super()` function, or by explicitly calling the base-class\n",
"> implementation.\n",
"\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": [
"### 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 should work on an instance of any `Operator`\n",
"sub-classs. For example, we can write a function which prints a summary\n",
"of an `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 type:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"operatorSummary(no)\n",
"operatorSummary(so)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Multiple inheritance\n",
"\n",
"\n",
"Mention the MRO\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"## Class attributes and methods\n",
"## Class attributes and methods\n",
"\n",
"\n",
"\n",
"\n",
...
@@ -892,7 +1254,7 @@
...
@@ -892,7 +1254,7 @@
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
"source": [
"source": [
"
C
alling a class method is the same as accessing a class attribute:"
"alling a class method is the same as accessing a class attribute:"
]
]
},
},
{
{
...
@@ -942,9 +1304,6 @@
...
@@ -942,9 +1304,6 @@
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
"source": [
"source": [
"## Inheritance\n",
"\n",
"\n",
"## Appendix: The `object` base-class\n",
"## Appendix: The `object` base-class\n",
"\n",
"\n",
"\n",
"\n",
...
@@ -1022,7 +1381,71 @@
...
@@ -1022,7 +1381,71 @@
"party library which has bugs in it. No problem - while you are waiting for the\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",
"library author to release a new version of the library, you can write your own\n",
"working implementation and [monkey-patch it\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)!"
"in](https://git.fmrib.ox.ac.uk/fsl/fsleyes/fsleyes/blob/0.21.0/fsleyes/views/viewpanel.py#L726)!\n",
"\n",
"\n",
"## Appendix: Method overloading\n",
"\n",
"\n",
"Method overloading (defining multiple methods on a class, each accepting\n",
"different arguments) is one of the only object-oriented features that is not\n",
"present in Python. Becuase Python does not perform any runtime checks on the\n",
"types of arguments that are passed to a method, or the compatibility of the\n",
"method to accept the arguments, it would not be possible to determine which\n",
"implementation of a method is to be called.\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. Here is YACE (Yet Another Contrived 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",
"\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": [
"## Useful references\n",
"\n",
"\n",
"https://docs.python.org/3.5/library/unittest.mock.html\n",
"https://docs.python.org/3.5/tutorial/classes.html\n",
"https://docs.python.org/3.5/library/functions.html\n",
"https://docs.python.org/2/reference/datamodel.html\n",
"https://www.reddit.com/r/learnpython/comments/2s3pms/what_is_the_difference_between_init_and_new/cnm186z/\n",
"https://docs.python.org/3.5/reference/datamodel.html\n",
"http://www.jesshamrick.com/2011/05/18/an-introduction-to-classes-and-inheritance-in-python/\n",
"https://www.digitalocean.com/community/tutorials/understanding-class-inheritance-in-python-3\n",
"\n",
"https://docs.python.org/3.5/library/functions.html#super"
]
]
}
}
],
],
...
...
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
# Object-oriented programming
# Object-oriented programming
in Python
By now you might have realised that __everything__ in Python is an
By now you might have realised that __everything__ in Python is an
object. Strings are objects, numbers are objects, functions are objects,
object. Strings are objects, numbers are objects, functions are objects,
modules are objects - __everything__ is an object!
modules are objects - __everything__ is an object!
But this does not mean that you have to use Python in an object-oriented
But this does not mean that you have to use Python in an object-oriented
fashion. You can stick with functions and statements, and get quite a lot
fashion. You can stick with functions and statements, and get quite a lot
done. But some problems are just easier to solve, and to reason about, when
done. But some problems are just easier to solve, and to reason about, when
you use an object-oriented approach.
you use an object-oriented approach.
## Objects versus classes
## Objects versus classes
If you are versed in C++, Java, C#, or some other object-oriented language,
If you are versed in C++, Java, C#, or some other object-oriented language,
then this should all hopefully sound familiar, and you can skip to the next
then this should all hopefully sound familiar, and you can skip to the next
section.
section.
If you have not done any object-oriented programming before, your first step
If you have not done any object-oriented programming before, your first step
is to understand the difference between _objects_ (also known as _instances_)
is to understand the difference between _objects_ (also known as _instances_)
and _classes_ (also known as _types_).
and _classes_ (also known as _types_).
If you have some experience in C, then you can start off by thinking of a
If you have some experience in C, then you can start off by thinking of a
class as like a
`struct`
definition - a
`struct`
is a specification for the
class as like a
`struct`
definition - a
`struct`
is a specification for the
layout of a chunk of memory. For example, here is a typical struct definition:
layout of a chunk of memory. For example, here is a typical struct definition:
> ```
> ```
> /**
> /**
> * Struct representing a stack.
> * Struct representing a stack.
> */
> */
> typedef struct __stack {
> typedef struct __stack {
> uint8_t capacity; /**< the maximum capacity of this stack */
> uint8_t capacity; /**< the maximum capacity of this stack */
> uint8_t size; /**< the current size of this stack */
> uint8_t size; /**< the current size of this stack */
> void **top; /**< pointer to the top of this stack */
> void **top; /**< pointer to the top of this stack */
> } stack_t;
> } stack_t;
> ```
> ```
Now, an _object_ is not a definition, but rather a thing which resides in
Now, an _object_ is not a definition, but rather a thing which resides in
memory. An object can have _attributes_ (pieces of information), and _methods_
memory. An object can have _attributes_ (pieces of information), and _methods_
(functions associated with the object). You can pass objects around your code,
(functions associated with the object). You can pass objects around your code,
manipulate their attributes, and call their methods.
manipulate their attributes, and call their methods.
Returning to our C metaphor, you can think of an object as like an
Returning to our C metaphor, you can think of an object as like an
instantiation of a struct:
instantiation of a struct:
> ```
> ```
> stack_t stack;
> stack_t stack;
> stack.capacity = 10;
> stack.capacity = 10;
> ```
> ```
The fundamental
difference between a
`struct`
in C, and a
`class`
in Python
One of the major
difference
s
between a
`struct`
in C, and a
`class`
in Python
and other object oriented languages, is that you can't (easily) add functions
and other object oriented languages, is that you can't (easily) add functions
to a
`struct`
- it is just a chunk of memory. Whereas in Python, you can add
to a
`struct`
- it is just a chunk of memory. Whereas in Python, you can add
functions to your class definition, which will then be added as methods when
functions to your class definition, which will then be added as methods when
you create an object from that class.
you create an object from that class.
Of course there are many more differences between C structs and classes (most
Of course there are many more differences between C structs and classes (most
notably
[
inheritance
](
todo
)
, and
[
protection
](
todo
)
). But if you can
notably
[
inheritance
](
todo
)
,
[
polymorphism
](
todo
)
, and
[
access
understand the difference between a _definition_ of a C struct, and an
protection
](
todo
)
). But if you can understand the difference between a
_instantiation_
of that struct, then you are most of the way towards
_definition_
of a C struct, and an _instantiation_ of that struct, then you
understanding the difference between a Python _class_, and a Python _object_.
are most of the way towards understanding the difference between a _class_,
and an _object_.
> But just to confuse you, remember that in Python, __everything__ is an
> But just to confuse you, remember that in Python, __everything__ is an
> object - even classes!
> object - even classes!
## Defining a class
## Defining a class
Defining a class in Python is simple. Let's take on a small project, by
Defining a class in Python is simple. Let's take on a small project, by
developing a class which can be used in place of the
`fslmaths`
shell command.
developing a class which can be used in place of the
`fslmaths`
shell command.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
class FSLMaths(object):
class FSLMaths(object):
pass
pass
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
In this statement, we defined a new class called
`FSLMaths`
, which inherits
In this statement, we defined a new class called
`FSLMaths`
, which inherits
from the built-in
`object`
base-class (see
[
below
](
todo
)
for more details on
from the built-in
`object`
base-class (see
[
below
](
todo
)
for more details on
inheritance).
inheritance).
Now that we have defined our class, we can create objects - instances of that
Now that we have defined our class, we can create objects - instances of that
class - by calling the class itself, as if it were a function:
class - by calling the class itself, as if it were a function:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
fm1 = FSLMaths()
fm1 = FSLMaths()
fm2 = FSLMaths()
fm2 = FSLMaths()
print(fm1)
print(fm1)
print(fm2)
print(fm2)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Although these objects are not of much use at this stage. Let's do some more
Although these objects are not of much use at this stage. Let's do some more
work.
work.
## Object creation - the `__init__` method
## Object creation - the `__init__` method
The first thing that our
`fslmaths`
replacement will need is an input image.
The first thing that our
`fslmaths`
replacement will need is an input image.
It makes sense to pass this in when we create an
`FSLMaths`
object:
It makes sense to pass this in when we create an
`FSLMaths`
object:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
class FSLMaths(object):
class FSLMaths(object):
def __init__(self, inimg):
def __init__(self, inimg):
self.input = inimg
self.input = inimg
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Here we have added a _method_ called
`__init__`
to our class (remember that a
Here we have added a _method_ called
`__init__`
to our class (remember that a
_method_
is just a function which is associated with a specific object). This
_method_
is just a function which is defined in a cliass, and which can be
method expects two arguments -
`self`
, and
`inimg`
. So now, when we create an
called on instances of that class). This method expects two arguments -
instance of the
`FSLMaths`
class, we will need to provide an input image:
`self`
, and
`inimg`
. So now, when we create an instance of the
`FSLMaths`
class, we will need to provide an input image:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
import nibabel as nib
import nibabel as nib
import os.path as op
import os.path as op
input = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')
input = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')
inimg = nib.load(input)
inimg = nib.load(input)
fm = FSLMaths(inimg)
fm = FSLMaths(inimg)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
There are a couple of things to note here:
There are a couple of things to note here:
__Our method is called__
`__init__`
__, but we didn't actually call the__
__Our method is called__
`__init__`
__, but we didn't actually call the__
`__init__`
__method!__
`__init__`
is a special method in Python - it is called
`__init__`
__method!__
`__init__`
is a special method in Python - it is called
when an instance of a class is created. And recall that we can create an
when an instance of a class is created. And recall that we can create an
instance of a class by calling the class in the same way that we call a
instance of a class by calling the class in the same way that we call a
function.
function.
__We didn't specify the `self` argument - what gives?!?__
The
`self`
argument
__We didn't specify the `self` argument - what gives?!?__
The
`self`
argument
is a special argument for methods in Python. If you are coming from C++, Java,
is a special argument for methods in Python. If you are coming from C++, Java,
C# or similar,
`self`
in Python is equivalent to
`this`
in those languages.
C# or similar,
`self`
in Python is equivalent to
`this`
in those languages.
### The `self` argument
### The `self` argument
In a method, the
`self`
argument is a reference to the object that the method
In a method, the
`self`
argument is a reference to the object that the method
was called on. So in this line of code:
was called on. So in this line of code:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
fm = FSLMaths(inimg)
fm = FSLMaths(inimg)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
the
`self`
argument in
`__init__`
will be a reference to the
`FSLMaths`
object
the
`self`
argument in
`__init__`
will be a reference to the
`FSLMaths`
object
that has been created (and is then assigned to the
`fm`
variable, after the
that has been created (and is then assigned to the
`fm`
variable, after the
`__init__`
method has finished).
`__init__`
method has finished).
But note that we do not need to explicitly provide the
`self`
argument - when
But note that we do not need to explicitly provide the
`self`
argument - when
you call a method on an object, or when you create a new object, the Python
you call a method on an object, or when you create a new object, the Python
runtime will take care of passing the instance as the
`self`
argument to the
runtime will take care of passing the instance as the
`self`
argument to the
method.
method.
But when you are writing a class, you _do_ need to explicitly list
`self`
as
But when you are writing a class, you _do_ need to explicitly list
`self`
as
the first argument to all of the methods of the class.
the first argument to all of the methods of the class.
## Attributes
## Attributes
In Python, the term _attribute_ is used to refer to a piece of information
In Python, the term _attribute_ is used to refer to a piece of information
that is associated with an object. An attribute is generally a reference to
that is associated with an object. An attribute is generally a reference to
another object (which might be a string, a number, or a list, or some other
another object (which might be a string, a number, or a list, or some other
more complicated object).
more complicated object).
Remember that we modified our
`FSLMaths`
class so that it is passed an input
Remember that we modified our
`FSLMaths`
class so that it is passed an input
image on creation:
image on creation:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
class FSLMaths(object):
class FSLMaths(object):
def __init__(self, inimg):
def __init__(self, inimg):
self.input = inimg
self.input = inimg
input = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')
input = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')
fm = FSLMaths(nib.load(input))
fm = FSLMaths(nib.load(input))
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Take a look at what is going on in the
`__init__`
method - we take the
`inimg`
Take a look at what is going on in the
`__init__`
method - we take the
`inimg`
argument, and create a reference to it called
`self.input`
. We have added an
argument, and create a reference to it called
`self.input`
. We have added an
_attribute_
to the
`FSLMaths`
instance, called
`input`
, and we can access that
_attribute_
to the
`FSLMaths`
instance, called
`input`
, and we can access that
attribute like so:
attribute like so:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
print('Input for our FSLMaths instance: {}'.format(fm.input.get_filename()))
print('Input for our FSLMaths instance: {}'.format(fm.input.get_filename()))
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
And that concludes the section on adding attributes to Python objects.
And that concludes the section on adding attributes to Python objects.
Just kidding. But it really is that simple. This is one aspect of Python which
Just kidding. But it really is that simple. This is one aspect of Python which
might be quite jarring to you if you are coming from a language with more
might be quite jarring to you if you are coming from a language with more
rigid semantics, such as C++ or Java. In those languages, you must pre-specify
rigid semantics, such as C++ or Java. In those languages, you must pre-specify
all of the attributes and methods that are a part of a class. But Python is
all of the attributes and methods that are a part of a class. But Python is
more flexible - you simply add attributes to an object affer it has been
more flexible - you simply add attributes to an object affer it has been
created. In fact, you can even do this outside of the class
created. In fact, you can even do this outside of the class
definition
<sup>
1
</sup>
:
definition
<sup>
1
</sup>
:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
fm = FSLMaths(inimg)
fm = FSLMaths(inimg)
fm.another_attribute = 'Haha'
fm.another_attribute = 'Haha'
print(fm.another_attribute)
print(fm.another_attribute)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
__But...__
while attributes can be added to a Python object at any time, it is
__But...__
while attributes can be added to a Python object at any time, it is
good practice (and makes for more readable and maintainable code) to add all
good practice (and makes for more readable and maintainable code) to add all
of an object's attributes within the
`__init__`
method.
of an object's attributes within the
`__init__`
method.
> <sup>1</sup>This not possible with many of the built-in types, such as
> <sup>1</sup>This not possible with many of the built-in types, such as
> `list` and `dict` objects, nor with types that are defined in Python
> `list` and `dict` objects, nor with types that are defined in Python
> extensions (Python modules that are written in C).
> extensions (Python modules that are written in C).
## Methods
## Methods
We've been dilly-dallying on this little
`FSLMaths`
project for a while now,
We've been dilly-dallying on this little
`FSLMaths`
project for a while now,
but our class still can't actually do anything. Let's start adding some
but our class still can't actually do anything. Let's start adding some
functionality:
functionality:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
class FSLMaths(object):
class FSLMaths(object):
def __init__(self, inimg):
def __init__(self, inimg):
self.input = inimg
self.input = inimg
self.operations = []
self.operations = []
def add(self, value):
def add(self, value):
self.operations.append(('add', value))
self.operations.append(('add', value))
def mul(self, value):
def mul(self, value):
self.operations.append(('mul', value))
self.operations.append(('mul', value))
def div(self, value):
def div(self, value):
self.operations.append(('div', value))
self.operations.append(('div', value))
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Woah woah,
[
slow down egg-head, you're going a mile a
Woah woah,
[
slow down egg-head, you're going a mile a
minute!
](
https://www.youtube.com/watch?v=yz-TemWooa4
)
We've modified
minute!
](
https://www.youtube.com/watch?v=yz-TemWooa4
)
We've modified
`__init__`
so that a second attribute called
`operations`
is added to our
`__init__`
so that a second attribute called
`operations`
is added to our
object - this
`operations`
attribute is simply a list.
object - this
`operations`
attribute is simply a list.
Then, we added a handful of methods -
`add`
,
`mul`
, and
`div`
- which each
Then, we added a handful of methods -
`add`
,
`mul`
, and
`div`
- which each
append a tuple to that
`operations`
list.
append a tuple to that
`operations`
list.
> Note that, just like in the `__init__` method, the first argument that will
> Note that, just like in the `__init__` method, the first argument that will
> be passed to these methods is `self` - a reference to the object that the
> be passed to these methods is `self` - a reference to the object that the
> method has been called on.
> method has been called on.
The idea behind this design is that our
`FSLMaths`
class will not actually do
The idea behind this design is that our
`FSLMaths`
class will not actually do
anything when we call the
`add`
,
`mul`
or
`div`
methods. Instead, it will
anything when we call the
`add`
,
`mul`
or
`div`
methods. Instead, it will
"stage" each operation, and then perform them all in one go. So let's add
"stage" each operation, and then perform them all in one go. So let's add
another method,
`run`
, which actually does the work:
another method,
`run`
, which actually does the work:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
import numpy as np
import numpy as np
import nibabel as nib
import nibabel as nib
class FSLMaths(object):
class FSLMaths(object):
def __init__(self, inimg):
def __init__(self, inimg):
self.input = inimg
self.input = inimg
self.operations = []
self.operations = []
def add(self, value):
def add(self, value):
self.operations.append(('add', value))
self.operations.append(('add', value))
def mul(self, value):
def mul(self, value):
self.operations.append(('mul', value))
self.operations.append(('mul', value))
def div(self, value):
def div(self, value):
self.operations.append(('div', value))
self.operations.append(('div', value))
def run(self, output=None):
def run(self, output=None):
data = np.array(self.input.get_data())
data = np.array(self.input.get_data())
for oper, value in self.operations:
for oper, value in self.operations:
# If value is a string, we assume
# Values could be an image that
# that it is a path to an image.
if isinstance(value, str):
image = nib.load(value)
value = image.get_data()
# Or it could be an image that
# has already been loaded.
# has already been loaded.
elif isinstance(value, nib.nifti1.Nifti1Image):
elif isinstance(value, nib.nifti1.Nifti1Image):
value =
imag
e.get_data()
value =
valu
e.get_data()
# Otherwise we assume that
# Otherwise we assume that
#
it i
s a scalar
value
.
#
value
s a
re
scalar
s
.
if oper == 'add':
if oper == 'add':
data = data + value
data = data + value
elif oper == 'mul':
elif oper == 'mul':
data = data * value
data = data * value
elif oper == 'div':
elif oper == 'div':
data = data / value
data = data / value
# turn final output into a nifti,
# turn final output into a nifti,
# and save it to disk if an
# and save it to disk if an
# 'output' has been specified.
# 'output' has been specified.
outimg = nib.nifti1.Nifti1Image(data, inimg.affine)
outimg = nib.nifti1.Nifti1Image(data, inimg.affine)
if output is not None:
if output is not None:
nib.save(outimg, output)
nib.save(outimg, output)
return outimg
return outimg
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
We now have a useable (but not very useful)
`FSLMaths`
class!
We now have a useable (but not very useful)
`FSLMaths`
class!
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
input = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')
input = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')
inimg = nib.load(input)
mask = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm_brain_mask.nii.gz')
fm = FSLMaths(inimg)
input = nib.load(input)
mask = nib.load(mask)
fm = FSLMaths(input)
fm.mul(
op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm_brain_mask.nii.gz')
)
fm.mul(
mask
)
fm.add(-10)
fm.add(-10)
outimg = fm.run()
outimg = fm.run()
norigvox = (inimg .get_data() > 0).sum()
norigvox = (inimg .get_data() > 0).sum()
nmaskvox = (outimg.get_data() > 0).sum()
nmaskvox = (outimg.get_data() > 0).sum()
print('Number of voxels >0 in original image: {}'.format(norigvox))
print('Number of voxels >0 in original image: {}'.format(norigvox))
print('Number of voxels >0 in masked image: {}'.format(nmaskvox))
print('Number of voxels >0 in masked image: {}'.format(nmaskvox))
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Protecting attribute access
## Protecting attribute access
In our
`FSLMaths`
class, the input image was added as an attribute called
In our
`FSLMaths`
class, the input image was added as an attribute called
`input`
to
`FSLMaths`
objects. We saw that it is easy to read the attributes
`input`
to
`FSLMaths`
objects. We saw that it is easy to read the attributes
of an object - if we have a
`FSLMaths`
instance called
`fm`
, we can read its
of an object - if we have a
`FSLMaths`
instance called
`fm`
, we can read its
input image via
`fm.input`
.
input image via
`fm.input`
.
But it is just as easy to write the attributes of an object. What's to stop
But it is just as easy to write the attributes of an object. What's to stop
some sloppy research assistant from overwriting our
`input`
attribute?
some sloppy research assistant from overwriting our
`input`
attribute?
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
inimg = nib.load(op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz'))
inimg = nib.load(op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz'))
fm = FSLMaths(inimg)
fm = FSLMaths(inimg)
fm.input = None
fm.input = None
fm.run()
fm.run()
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Well, the scary answer is ... there is __nothing__ stopping you from doing
Well, the scary answer is ... there is __nothing__ stopping you from doing
whatever you want to a Python object. You can add, remove, and modify
whatever you want to a Python object. You can add, remove, and modify
attribute at will. You can even replace the methods of an existing object if
attribute
s
at will. You can even replace the methods of an existing object if
you like:
you like:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
fm = FSLMaths(inimg)
fm = FSLMaths(inimg)
def myadd(value):
def myadd(value):
print('Oh no, I\'m not going to add {} to '
print('Oh no, I\'m not going to add {} to '
'your image. Go away!'.format(value))
'your image. Go away!'.format(value))
fm.add = myadd
fm.add = myadd
fm.add(123)
fm.add(123)
fm.mul = None
fm.mul = None
fm.mul(123)
fm.mul(123)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
But you really shouldn't get into the habit of doing devious things like
But you really shouldn't get into the habit of doing devious things like
this - take a look at the appendix for a
[
brief discussion on this topic
](
todo
)
.
this - take a look at the appendix for a
[
brief discussion on this topic
](
todo
)
.
Python tends to assume that programmers are "responsible adults", and hence
Python tends to assume that programmers are "responsible adults", and hence
doesn't do much in the way of restricting access to the attributes or methods
doesn't do much in the way of restricting access to the attributes or methods
of an object. This is in contrast to languages like C++ and Java, where the
of an object. This is in contrast to languages like C++ and Java, where the
notion of a private attribute or method is enforced by the language.
notion of a private attribute or method is
strictly
enforced by the language.
However, there are a couple of conventions in Python that are
[
universally
However, there are a couple of conventions in Python that are
[
universally
adhered
adhered
to
](
https://docs.python.org/3.5/tutorial/classes.html#private-variables
)
:
to
](
https://docs.python.org/3.5/tutorial/classes.html#private-variables
)
:
*
Class-level attributes and methods, and module-level attributes, functions,
*
Class-level attributes and methods, and module-level attributes, functions,
and classes, which begin with a single underscore (
`_`
), should be
and classes, which begin with a single underscore (
`_`
), should be
considered _protected_ - they are intended for internal use only, and should
considered _protected_ - they are intended for internal use only, and should
not be considered part of the public API of a class or module. This is not
not be considered part of the public API of a class or module. This is not
enforced by the language in any way
<sup>
2
</sup>
- remember, we are all
enforced by the language in any way
<sup>
2
</sup>
- remember, we are all
responsible adults here!
responsible adults here!
*
Class-level attributes and methods which begin with a double-underscore
*
Class-level attributes and methods which begin with a double-underscore
(
`__`
) should be considered _private_. Python provides a weak form of
(
`__`
) should be considered _private_. Python provides a weak form of
enforcement for this rule - any attribute or method with such a name will
enforcement for this rule - any attribute or method with such a name will
actually be _renamed_ (in a standardised manner) at runtime, so that it is
actually be _renamed_ (in a standardised manner) at runtime, so that it is
not accessible through its original name. It is still accessible via its
not accessible through its original name. It is still accessible via its
[
mangled
[
mangled
name
](
https://docs.python.org/3.5/tutorial/classes.html#private-variables
)
name
](
https://docs.python.org/3.5/tutorial/classes.html#private-variables
)
though.
though.
> <sup>2</sup> With the exception that module-level fields which begin with a
> <sup>2</sup> With the exception that module-level fields which begin with a
> single underscore will not be imported into the local scope via the
> single underscore will not be imported into the local scope via the
> `from [module] import *` techinque.
> `from [module] import *` techinque.
So with all of this in mind, we can adjust our
`FSLMaths`
class to discourage
So with all of this in mind, we can adjust our
`FSLMaths`
class to discourage
our sloppy research assistant from overwriting the
`input`
attribute:
our sloppy research assistant from overwriting the
`input`
attribute:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
# remainder of definition omitted for brevity
# remainder of definition omitted for brevity
class FSLMaths(object):
class FSLMaths(object):
def __init__(self, inimg):
def __init__(self, inimg):
self.__input = inimg
self.__input = inimg
self.__operations = []
self.__operations = []
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
But now we have lost the ability to read our
`__input`
attribute:
But now we have lost the ability to read our
`__input`
attribute:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
inimg = nib.load(op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz'))
inimg = nib.load(op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz'))
fm = FSLMaths(inimg)
fm = FSLMaths(inimg)
print(fm.__input)
print(fm.__input)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
### A better way - properties
### A better way - properties
Python has a feature called
Python has a feature called
[
`properties`
](
https://docs.python.org/3.5/library/functions.html#property
)
,
[
`properties`
](
https://docs.python.org/3.5/library/functions.html#property
)
,
which is a nice means of controlling access to the attributes of an object. We
which is a nice means of controlling access to the attributes of an object. We
can use properties by defining a "getter" method which can be used to access
can use properties by defining a "getter" method which can be used to access
our attributes, and "decorating" them with the
`@property`
decorator (we will
our attributes, and "decorating" them with the
`@property`
decorator (we will
cover decorators in a later practical).
cover decorators in a later practical).
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
class FSLMaths(object):
class FSLMaths(object):
def __init__(self, inimg):
def __init__(self, inimg):
self.__input = inimg
self.__input = inimg
self.__operations = []
self.__operations = []
@property
@property
def input(self):
def input(self):
return self.__input
return self.__input
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
So we are still storing our input image as a private attribute, but now
So we are still storing our input image as a private attribute, but now
we have made it available in a read-only manner via the
`input`
property:
we have made it available in a read-only manner via the
`input`
property:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
input = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')
input = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')
inimg = nib.load(input)
inimg = nib.load(input)
fm = FSLMaths(inimg)
fm = FSLMaths(inimg)
print(fm.input.get_filename())
print(fm.input.get_filename())
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Note that, even though we have defined
`input`
as a method, we can access it
Note that, even though we have defined
`input`
as a method, we can access it
like an attribute - this is due to the magic behind the
`@property`
decorator.
like an attribute - this is due to the magic behind the
`@property`
decorator.
We can also define "setter" methods for a property. For example, we might wish
We can also define "setter" methods for a property. For example, we might wish
to add the ability for a user of our
`FSLMaths`
class to change the input
to add the ability for a user of our
`FSLMaths`
class to change the input
image after creation.
image after creation.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
class FSLMaths(object):
class FSLMaths(object):
def __init__(self, inimg):
def __init__(self, inimg):
self.__input = None
self.__input = None
self.__operations = []
self.__operations = []
self.input = inimg
self.input = inimg
@property
@property
def input(self):
def input(self):
return self.__input
return self.__input
@input.setter
@input.setter
def input(self, value):
def input(self, value):
if not isinstance(value, nib.nifti1.Nifti1Image):
if not isinstance(value, nib.nifti1.Nifti1Image):
raise ValueError('value must be a NIFTI image!')
raise ValueError('value must be a NIFTI image!')
self.__input = value
self.__input = value
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Property setters are a nice way to
restrict the values that a property may
Property setters are a nice way to
add validation logic when an attribute is
take - note that we perform a sanity check in the
`input`
setter, to make
assigned a value. We are doing this in the above example, by making sure that
sure that
the new input is a NIFTI image:
the new input is a NIFTI image:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
input = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')
input = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')
inimg = nib.load(input)
inimg = nib.load(input)
fm = FSLMaths(inimg)
fm = FSLMaths(inimg)
print('Input: ', fm.input.get_filename())
print('Input: ', fm.input.get_filename())
# let's change the input
# let's change the input
# to a different image
# to a different image
input2 = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm_brain.nii.gz')
input2 = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm_brain.nii.gz')
inimg2 = nib.load(input2)
inimg2 = nib.load(input2)
fm.input = inimg2
fm.input = inimg2
print('New input: ', fm.input.get_filename())
print('New input: ', fm.input.get_filename())
# this is going to explode
# this is going to explode
fm.input = 'abcde'
fm.input = 'abcde'
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
> Note also that we used the `input` setter method within `__init__` to
> Note also that we used the `input` setter method within `__init__` to
> validate the initial `inimg` that was passed in during creation.
> validate the initial `inimg` that was passed in during creation.
## Inheritance
One of the major advantages of an object-oriented programming approach is
_inheritance_
- the ability to define hierarchical relationships between
classes and instances.
### The basics
For example, a veterinary surgery might be running some Python code which
looks like the following. Perhaps it is used to assist the nurses in
identifying an animal when it arrives at the surgery:
%% Cell type:code id: tags:
```
class Animal(object):
def noiseMade(self):
raise NotImplementedError('This method is implemented by sub-classes')
class Dog(Animal):
def noiseMade(self):
return 'Woof'
class Cat(Animal):
def noiseMade(self):
return 'Meow'
class Labrador(Dog):
pass
class Chihuahua(Dog):
def noiseMade(self):
return 'Yap yap yap'
```
%% Cell type:markdown id: tags:
Hopefully this example doesn't need much in the way of explanation - this
collection of classes captures a hierarchical relationship which exists in the
real world (and also captures the inherently annoying nature of
chihuahuas). For example, in the real world, all dogs are animals, but not all
animals are dogs. Therefore in our model, the
`Dog`
class has specified
`Animal`
as its base class. We say that the
`Dog`
class _extends_, _derives
from_, or _inherits from_, the
`Animal`
class, and that all
`Dog`
instances
are also
`Animal`
instances (but not vice-versa).
What does that
`noiseMade`
method do? There is a
`noiseMade`
method defined
on the
`Animal`
class, but it has been re-implemented, or _overridden_ in the
`Dog`
,
`Cat`
, and
`Chihuahua`
classes (but not on the
`Labrador`
class). We
can call the
`noiseMade`
method on any
`Animal`
instance, but the specific
behaviour that we get is dependent on the specific type of animal.
%% Cell type:code id: tags:
```
d = Dog()
l = Labrador()
c = Cat()
ch = Chihuahua()
print('Noise made by dogs: {}'.format(d .noiseMade()))
print('Noise made by labradors: {}'.format(l .noiseMade()))
print('Noise made by cats: {}'.format(c .noiseMade()))
print('Noise made by chihuahuas: {}'.format(ch.noiseMade()))
```
%% Cell type:markdown id: tags:
### Code re-use and problem decomposition
Inheritance allows us to split a problem into smaller problems, and to re-use
code. Let's demonstrate this with a more involved example. Imagine that a
former colleague had written a class called
`Operator`
:
> I know this is a little abstract (and quite contrived), but bear with me
> here.
%% Cell type:code id: tags:
```
class Operator(object):
def __init__(self):
self.__operations = []
self.__opFuncs = {}
@property
def operations(self):
return list(self.__operations)
@property
def functions(self):
return dict(self.__opFuncs)
def addFunction(self, name, func):
self.__opFuncs[name] = func
def do(self, name, *values):
self.__operations.append((name, values))
def preprocess(self, value):
return value
def run(self, input):
data = self.preprocess(input)
for oper, vals in self.__operations:
func = self.__opFuncs[oper]
vals = [self.preprocess(v) for v in vals]
data = func(data, *vals)
return data
```
%% Cell type:markdown id: tags:
This
`Operator`
class provides an interface and logic to execute a chain of
operations - an operation is some function which accepts one or more inputs,
and produce one output.
But it stops short of defining any operations. Instead, we can create another
class - a sub-class - which derives from the
`Operator`
class. This sub-class
will define the operations that will ultimately be executed by the
`Operator`
class. All that the
`Operator`
class does is:
-
Allow functions to be registered with the
`addFunction`
method - all
registered functions can be used via the
`do`
method.
-
Stage an operation (using a registered function) via the
`do`
method. Note
that
`do`
allows any number of values to be passed to it, as we used the
`*`
operator when specifying the
`values`
argument.
-
Run all staged operations via the
`run`
method - it passes an input through
all of the operations that have been staged, and then returns the final
result.
Let's define a sub-class:
%% Cell type:code id: tags:
```
class NumberOperator(Operator):
def __init__(self):
super().__init__()
self.addFunction('add', self.add)
self.addFunction('mul', self.mul)
self.addFunction('negate', self.negate)
def preprocess(self, value):
return float(value)
def add(self, a, b):
return a + b
def mul(self, a, b):
return a * b
def negate(self, a):
return -a
```
%% Cell type:markdown id: tags:
The
`NumberOperator`
is a sub-class of
`Operator`
, which we can use for basic
numerical calculations. It provides a handful of simple numerical methods, but
the most interesting stuff is inside
`__init__`
:
> ```
> super().__init__()
> ```
This line invokes
`Operator.__init__`
- the initialisation method for the
`Operator`
base-class. In Python, we can use the
[
built-in `super`
method
](
https://docs.python.org/3.5/library/functions.html#super
)
to take care
of correctly calling methods that are defined in an object's base-class (or
classes, in the case of
[
multiple inheritance
](
todo
)
).
> ```
> self.addFunction('add', self.add)
> self.addFunction('mul', self.mul)
> self.addFunction('negate', self.negate)
> ```
Here we are registering all of the functionality that is provided by the
`NumberOperator`
class, via the
`Opoerator.addFunction`
method.
The
`NumberOperator`
class has also overridden the
`preprocess`
method, to
ensure that all values handled by the
`Operator`
are numbers. This method gets
called within the
`run`
method - for a
`NumberOperator`
instance, the
`NumberOperator.preprocess`
method will get called
<sup>
1
</sup>
.
> <sup>1</sup> We can still [access overridden base-class methods](todo link)
> via the `super()` function, or by explicitly calling the base-class
> implementation.
Now let's see what our
`NumberOperator`
class does:
%% Cell type:code id: tags:
```
no = NumberOperator()
no.do('add', 10)
no.do('mul', 2)
no.do('negate')
print('Operations on {}: {}'.format(10, no.run(10)))
print('Operations on {}: {}'.format(2.5, no.run(5)))
```
%% Cell type:markdown id: tags:
It works! While this is a contrived example, hopefully you can see how
inheritance can be used to break a problem down into sub-problems:
-
The
`Operator`
class provides all of the logic needed to manage and execute
operations, without caring about what those operations are actually doing.
-
This leaves the
`NumberOperator`
class free to concentrate on implementing
the functions that are specific to its task, and not having to worry about
how they are executed.
We could also easily implement other
`Operator`
sub-classes to work on
different data types, such as arrays, images, or even non-numeric data such as
strings:
%% Cell type:code id: tags:
```
class StringOperator(Operator):
def __init__(self):
super().__init__()
self.addFunction('capitalise', self.capitalise)
self.addFunction('concat', self.concat)
def preprocess(self, value):
return str(value)
def capitalise(self, s):
return ' '.join([w[0].upper() + w[1:] for w in s.split()])
def concat(self, s1, s2):
return s1 + s2
so = StringOperator()
so.do('capitalise')
so.do('concat', '!')
print(so.run('python is an ok language'))
```
%% Cell type:markdown id: tags:
### Polymorphism
Inheritance also allows us to take advantage of _polymorphism_, which refers
to idea that, in an object-oriented language, we should be able to use an
object without having complete knowledge about the class, or type, of that
object. For example, we should be able to write a function which expects an
`Operator`
instance, but which should work on an instance of any
`Operator`
sub-classs. For example, we can write a function which prints a summary
of an
`Operator`
instance:
%% Cell type:code id: tags:
```
def operatorSummary(o):
print(type(o).__name__)
print(' All functions: ')
for fname in o.functions.keys():
print(' {}'.format(fname))
print(' Staged operations: ')
for i, (fname, vals) in enumerate(o.operations):
vals = ', '.join([str(v) for v in vals])
print(' {}: {}({})'.format(i + 1, fname, vals))
```
%% Cell type:markdown id: tags:
Because the
`operatorSummary`
function only uses methods that are defined
in the
`Operator`
base-class, we can use it on _any_
`Operator`
instance,
regardless of its type:
%% Cell type:code id: tags:
```
operatorSummary(no)
operatorSummary(so)
```
%% Cell type:markdown id: tags:
### Multiple inheritance
Mention the MRO
## Class attributes and methods
## Class attributes and methods
Up to this point we have been covering how to add attributes and methods to an
Up to this point we have been covering how to add attributes and methods to an
_object_
. But it is also possible to add methods and attributes to a _class_
_object_
. But it is also possible to add methods and attributes to a _class_
(
`static`
methods and fields, for those of you familiar with C++ or Java).
(
`static`
methods and fields, for those of you familiar with C++ or Java).
Class attributes and methods can be accessed without having to create an
Class attributes and methods can be accessed without having to create an
instance of the class - they are not associated with individual objects, but
instance of the class - they are not associated with individual objects, but
rather to the class itself.
rather to the class itself.
Class methods and attributes can be useful in several scenarios - as a
Class methods and attributes can be useful in several scenarios - as a
hypothetical, not very useful example, let's say that we want to gain usage
hypothetical, not very useful example, let's say that we want to gain usage
statistics for how many times each type of operation is used on instances of
statistics for how many times each type of operation is used on instances of
our
`FSLMaths`
class. We might, for example, use this information in a grant
our
`FSLMaths`
class. We might, for example, use this information in a grant
application to show evidence that more research is needed to optimise the
application to show evidence that more research is needed to optimise the
performance of the
`add`
operation.
performance of the
`add`
operation.
### Class attributes
### Class attributes
Let's add a
`dict`
as a class attribute to the
`FSLMaths`
class - this
`dict`
Let's add a
`dict`
as a class attribute to the
`FSLMaths`
class - this
`dict`
called on a
`FSLMaths`
object, that object will increment the class-level
called on a
`FSLMaths`
object, that object will increment the class-level
counters for each operation that is applied:
counters for each operation that is applied:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
import numpy as np
import numpy as np
import nibabel as nib
import nibabel as nib
class FSLMaths(object):
class FSLMaths(object):
# It's this easy to add a class-level
# It's this easy to add a class-level
# attribute. This dict is associated
# attribute. This dict is associated
# with the FSLMaths *class*, not with
# with the FSLMaths *class*, not with
# any individual FSLMaths instance.
# any individual FSLMaths instance.
opCounters = {}
opCounters = {}
def __init__(self, inimg):
def __init__(self, inimg):
self.input = inimg
self.input = inimg
self.operations = []
self.operations = []
def add(self, value):
def add(self, value):
self.operations.append(('add', value))
self.operations.append(('add', value))
def mul(self, value):
def mul(self, value):
self.operations.append(('mul', value))
self.operations.append(('mul', value))
def div(self, value):
def div(self, value):
self.operations.append(('div', value))
self.operations.append(('div', value))
def run(self, output=None):
def run(self, output=None):
data = np.array(self.input.get_data())
data = np.array(self.input.get_data())
for oper, value in self.operations:
for oper, value in self.operations:
# Code omitted for brevity
# Code omitted for brevity
# Increment the usage counter
# Increment the usage counter
# for this operation.
# for this operation.
FSLMaths.opCounters[oper] = self.opCounters.get(oper, 0) + 1
FSLMaths.opCounters[oper] = self.opCounters.get(oper, 0) + 1
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
So let's see it in action:
So let's see it in action:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
input = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')
input = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')
mask = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm_brain_mask.nii.gz')
mask = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm_brain_mask.nii.gz')
inimg = nib.load(input)
inimg = nib.load(input)
fm1 = FSLMaths(inimg)
fm1 = FSLMaths(inimg)
fm2 = FSLMaths(inimg)
fm2 = FSLMaths(inimg)
fm1.mul(mask)
fm1.mul(mask)
fm1.add(15)
fm1.add(15)
fm2.add(25)
fm2.add(25)
fm1.div(1.5)
fm1.div(1.5)
fm1.run()
fm1.run()
fm2.run()
fm2.run()
print('FSLMaths usage statistics')
print('FSLMaths usage statistics')
for oper in ('add', 'div', 'mul'):
for oper in ('add', 'div', 'mul'):
print(' {} : {}'.format(oper, FSLMaths.opCounters.get(oper, 0)))
print(' {} : {}'.format(oper, FSLMaths.opCounters.get(oper, 0)))
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
### Class methods
### Class methods
It is just as easy to add a method to a class - let's take our reporting code
It is just as easy to add a method to a class - let's take our reporting code
from above, and add it as a method to the
`FSLMaths`
class:
from above, and add it as a method to the
`FSLMaths`
class:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
class FSLMaths(object):
class FSLMaths(object):
opCounters = {}
opCounters = {}
# We use the @classmethod decorator to denote a class
# We use the @classmethod decorator to denote a class
# method. Also note that, where a regular method which
# method. Also note that, where a regular method which
# is called on an instance will be passed the instance
# is called on an instance will be passed the instance
# as its first argument ('self'), a class method will
# as its first argument ('self'), a class method will
# be passed the class itself as the first argument -
# be passed the class itself as the first argument -
# the standard convention is to call this argument 'cls'.
# the standard convention is to call this argument 'cls'.
@classmethod
@classmethod
def usage(cls):
def usage(cls):
print('FSLMaths usage statistics')
print('FSLMaths usage statistics')
for oper in ('add', 'div', 'mul'):
for oper in ('add', 'div', 'mul'):
print(' {} : {}'.format(oper, FSLMaths.opCounters.get(oper, 0)))
print(' {} : {}'.format(oper, FSLMaths.opCounters.get(oper, 0)))
def __init__(self, inimg):
def __init__(self, inimg):
self.input = inimg
self.input = inimg
self.operations = []
self.operations = []
def add(self, value):
def add(self, value):
self.operations.append(('add', value))
self.operations.append(('add', value))
def mul(self, value):
def mul(self, value):
self.operations.append(('mul', value))
self.operations.append(('mul', value))
def div(self, value):
def div(self, value):
self.operations.append(('div', value))
self.operations.append(('div', value))
def run(self, output=None):
def run(self, output=None):
data = np.array(self.input.get_data())
data = np.array(self.input.get_data())
for oper, value in self.operations:
for oper, value in self.operations:
# Code omitted for brevity
# Code omitted for brevity
# Increment the usage counter
# Increment the usage counter
# for this operation.
# for this operation.
FSLMaths.opCounters[oper] = self.opCounters.get(oper, 0) + 1
FSLMaths.opCounters[oper] = self.opCounters.get(oper, 0) + 1
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
C
alling a class method is the same as accessing a class attribute:
alling a class method is the same as accessing a class attribute:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
input = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')
input = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')
mask = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm_brain_mask.nii.gz')
mask = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm_brain_mask.nii.gz')
inimg = nib.load(input)
inimg = nib.load(input)
fm1 = FSLMaths(inimg)
fm1 = FSLMaths(inimg)
fm2 = FSLMaths(inimg)
fm2 = FSLMaths(inimg)
fm1.mul(mask)
fm1.mul(mask)
fm1.add(15)
fm1.add(15)
fm2.add(25)
fm2.add(25)
fm1.div(1.5)
fm1.div(1.5)
fm1.run()
fm1.run()
fm2.run()
fm2.run()
FSLMaths.usage()
FSLMaths.usage()
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Note that it is also possible to access class attributes and methods through
Note that it is also possible to access class attributes and methods through
instances:
instances:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
print(fm1.opCounters)
print(fm1.opCounters)
print(fm1.usage())
print(fm1.usage())
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Inheritance
## Appendix: The `object` base-class
## Appendix: The `object` base-class
When you are defining a class, you need to specify the base-class from which
When you are defining a class, you need to specify the base-class from which
your class inherits. If your class does not inherit from a particular class,
your class inherits. If your class does not inherit from a particular class,
then it should inherit from the built-in
`object`
class:
then it should inherit from the built-in
`object`
class:
> ```
> ```
> class MyClass(object):
> class MyClass(object):
> ...
> ...
> ```
> ```
However, in older code bases, you might see class definitions that look like
However, in older code bases, you might see class definitions that look like
this, without explicitly inheriting from the
`object`
base class:
this, without explicitly inheriting from the
`object`
base class:
> ```
> ```
> class MyClass:
> class MyClass:
> ...
> ...
> ```
> ```
This syntax is a
[
throwback to older versions of
This syntax is a
[
throwback to older versions of
Python
](
https://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes
)
.
Python
](
https://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes
)
.
In Python 3 there is actually no difference between in whether you define your
In Python 3 there is actually no difference between in whether you define your
class in the way we have shown in this tutorial, or the old-style way.
class in the way we have shown in this tutorial, or the old-style way.
But if you are writing code which needs to run on both Python 2 and 3, you
But if you are writing code which needs to run on both Python 2 and 3, you
_must_
define your classes to explicitly inherit from the
`object`
base class.
_must_
define your classes to explicitly inherit from the
`object`
base class.
## Appendix: `__init__` versus `__new__`
## Appendix: `__init__` versus `__new__`
In Python, object creation is actually a two-stage process - _creation_, and
In Python, object creation is actually a two-stage process - _creation_, and
then _initialisation_. The
`__init__`
method gets called during the
then _initialisation_. The
`__init__`
method gets called during the
_initialisation_
stage - its job is to initialise the state of the object. But
_initialisation_
stage - its job is to initialise the state of the object. But
note that, by the time
`__init__`
gets called, the object has already been
note that, by the time
`__init__`
gets called, the object has already been
created.
created.
You can also define a method called
`__new__`
if you need to control the
You can also define a method called
`__new__`
if you need to control the
creation stage, although this is very rarely needed. A brief explanation on
creation stage, although this is very rarely needed. A brief explanation on
the difference between
`__new__`
and
`__init__`
can be found
the difference between
`__new__`
and
`__init__`
can be found
[
here
](
https://www.reddit.com/r/learnpython/comments/2s3pms/what_is_the_difference_between_init_and_new/cnm186z/
)
,
[
here
](
https://www.reddit.com/r/learnpython/comments/2s3pms/what_is_the_difference_between_init_and_new/cnm186z/
)
,
and you may also wish to take a look at the
[
official Python
and you may also wish to take a look at the
[
official Python
docs
](
https://docs.python.org/3.5/reference/datamodel.html#basic-customization
)
.
docs
](
https://docs.python.org/3.5/reference/datamodel.html#basic-customization
)
.
## Appendix: Monkey-patching
## Appendix: Monkey-patching
The act of run-time modification of objects or class definitions is referred
The act of run-time modification of objects or class definitions is referred
to as
[
_monkey-patching_
](
https://en.wikipedia.org/wiki/Monkey_patch
)
and,
to as
[
_monkey-patching_
](
https://en.wikipedia.org/wiki/Monkey_patch
)
and,
while it is allowed by the Python programming language, it is generally
while it is allowed by the Python programming language, it is generally
considered quite rude practice.
considered quite rude practice.
Just because you _can_ do something doesn't mean that you _should_. Python
Just because you _can_ do something doesn't mean that you _should_. Python
gives you the flexibility to write your software in whatever manner you deem
gives you the flexibility to write your software in whatever manner you deem
suitable. __But__ if you want to write software that will be used, adopted,
suitable. __But__ if you want to write software that will be used, adopted,
and maintained by other people, you should be polite, write your code in a
and maintained by other people, you should be polite, write your code in a
clear, readable fashion, and avoid the use of devious tactics such as
clear, readable fashion, and avoid the use of devious tactics such as
monkey-patching.
monkey-patching.
__However__
, while monkey-patching may seem like a horrific programming
__However__
, while monkey-patching may seem like a horrific programming
practice to those of you coming from the realms of C++, Java, and the like,
practice to those of you coming from the realms of C++, Java, and the like,
(and it is horrific in many cases), it can be _extremely_ useful in certain
(and it is horrific in many cases), it can be _extremely_ useful in certain
circumstances. For instance, monkey-patching makes unit testing
[
a
circumstances. For instance, monkey-patching makes unit testing
[
a
breeze
](
https://docs.python.org/3.5/library/unittest.mock.html
)
in Python.
breeze
](
https://docs.python.org/3.5/library/unittest.mock.html
)
in Python.
As another example, consider the scenario where you are dependent on a third
As another example, consider the scenario where you are dependent on a third
party library which has bugs in it. No problem - while you are waiting for the
party library which has bugs in it. No problem - while you are waiting for the
library author to release a new version of the library, you can write your own
library author to release a new version of the library, you can write your own
working implementation and
[
monkey-patch it
working implementation and
[
monkey-patch it
in
](
https://git.fmrib.ox.ac.uk/fsl/fsleyes/fsleyes/blob/0.21.0/fsleyes/views/viewpanel.py#L726
)
!
in
](
https://git.fmrib.ox.ac.uk/fsl/fsleyes/fsleyes/blob/0.21.0/fsleyes/views/viewpanel.py#L726
)
!
## Appendix: Method overloading
Method overloading (defining multiple methods on a class, each accepting
different arguments) is one of the only object-oriented features that is not
present in Python. Becuase Python does not perform any runtime checks on the
types of arguments that are passed to a method, or the compatibility of the
method to accept the arguments, it would not be possible to determine which
implementation of a method is to be called.
However, because a Python method can be written to accept any number or type
of arguments, it is very easy to to build your own overloading logic by
writing a "dispatch" method. Here is YACE (Yet Another Contrived Example):
%% Cell type:code id: tags:
```
class Adder(object):
def add(self, *args):
if len(args) == 2: return self.__add2(*args)
elif len(args) == 3: return self.__add3(*args)
elif len(args) == 4: return self.__add4(*args)
def __add2(self, a, b):
return a + b
def __add3(self, a, b, c):
return a + b + c
def __add4(self, a, b, c, d):
return a + b + c + d
a = Adder()
print('Add two: {}'.format(a.add(1, 2)))
print('Add three: {}'.format(a.add(1, 2, 3)))
print('Add four: {}'.format(a.add(1, 2, 3, 4)))
```
%% Cell type:markdown id: tags:
## Useful references
https://docs.python.org/3.5/library/unittest.mock.html
https://docs.python.org/3.5/tutorial/classes.html
https://docs.python.org/3.5/library/functions.html
https://docs.python.org/2/reference/datamodel.html
https://www.reddit.com/r/learnpython/comments/2s3pms/what_is_the_difference_between_init_and_new/cnm186z/
https://docs.python.org/3.5/reference/datamodel.html
http://www.jesshamrick.com/2011/05/18/an-introduction-to-classes-and-inheritance-in-python/
https://www.digitalocean.com/community/tutorials/understanding-class-inheritance-in-python-3
https://docs.python.org/3.5/library/functions.html#super
...
...
This diff is collapsed.
Click to expand it.
advanced_topics/object_oriented_programming.md
+
380
−
30
View file @
def69741
# Object-oriented programming
# Object-oriented programming
in Python
By now you might have realised that __everything__ in Python is an
By now you might have realised that __everything__ in Python is an
...
@@ -57,7 +57,7 @@ instantiation of a struct:
...
@@ -57,7 +57,7 @@ instantiation of a struct:
> ```
> ```
The fundamental
difference between a
`struct`
in C, and a
`class`
in Python
One of the major
difference
s
between a
`struct`
in C, and a
`class`
in Python
and other object oriented languages, is that you can't (easily) add functions
and other object oriented languages, is that you can't (easily) add functions
to a
`struct`
- it is just a chunk of memory. Whereas in Python, you can add
to a
`struct`
- it is just a chunk of memory. Whereas in Python, you can add
functions to your class definition, which will then be added as methods when
functions to your class definition, which will then be added as methods when
...
@@ -65,10 +65,11 @@ you create an object from that class.
...
@@ -65,10 +65,11 @@ you create an object from that class.
Of course there are many more differences between C structs and classes (most
Of course there are many more differences between C structs and classes (most
notably
[
inheritance
](
todo
)
, and
[
protection
](
todo
)
). But if you can
notably
[
inheritance
](
todo
)
,
[
polymorphism
](
todo
)
, and
[
access
understand the difference between a _definition_ of a C struct, and an
protection
](
todo
)
). But if you can understand the difference between a
_instantiation_
of that struct, then you are most of the way towards
_definition_
of a C struct, and an _instantiation_ of that struct, then you
understanding the difference between a Python _class_, and a Python _object_.
are most of the way towards understanding the difference between a _class_,
and an _object_.
> But just to confuse you, remember that in Python, __everything__ is an
> But just to confuse you, remember that in Python, __everything__ is an
...
@@ -124,9 +125,10 @@ class FSLMaths(object):
...
@@ -124,9 +125,10 @@ class FSLMaths(object):
Here we have added a _method_ called
`__init__`
to our class (remember that a
Here we have added a _method_ called
`__init__`
to our class (remember that a
_method_
is just a function which is associated with a specific object). This
_method_
is just a function which is defined in a cliass, and which can be
method expects two arguments -
`self`
, and
`inimg`
. So now, when we create an
called on instances of that class). This method expects two arguments -
instance of the
`FSLMaths`
class, we will need to provide an input image:
`self`
, and
`inimg`
. So now, when we create an instance of the
`FSLMaths`
class, we will need to provide an input image:
```
```
...
@@ -316,19 +318,13 @@ class FSLMaths(object):
...
@@ -316,19 +318,13 @@ class FSLMaths(object):
for oper, value in self.operations:
for oper, value in self.operations:
# If value is a string, we assume
# Values could be an image that
# that it is a path to an image.
if isinstance(value, str):
image = nib.load(value)
value = image.get_data()
# Or it could be an image that
# has already been loaded.
# has already been loaded.
elif isinstance(value, nib.nifti1.Nifti1Image):
elif isinstance(value, nib.nifti1.Nifti1Image):
value =
imag
e.get_data()
value =
valu
e.get_data()
# Otherwise we assume that
# Otherwise we assume that
#
it i
s a scalar
value
.
#
value
s a
re
scalar
s
.
if oper == 'add':
if oper == 'add':
data = data + value
data = data + value
...
@@ -354,10 +350,12 @@ We now have a useable (but not very useful) `FSLMaths` class!
...
@@ -354,10 +350,12 @@ We now have a useable (but not very useful) `FSLMaths` class!
```
```
input = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')
input = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')
inimg = nib.load(input)
mask = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm_brain_mask.nii.gz')
fm = FSLMaths(inimg)
input = nib.load(input)
mask = nib.load(mask)
fm = FSLMaths(input)
fm.mul(
op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm_brain_mask.nii.gz')
)
fm.mul(
mask
)
fm.add(-10)
fm.add(-10)
outimg = fm.run()
outimg = fm.run()
...
@@ -393,7 +391,7 @@ fm.run()
...
@@ -393,7 +391,7 @@ fm.run()
Well, the scary answer is ... there is __nothing__ stopping you from doing
Well, the scary answer is ... there is __nothing__ stopping you from doing
whatever you want to a Python object. You can add, remove, and modify
whatever you want to a Python object. You can add, remove, and modify
attribute at will. You can even replace the methods of an existing object if
attribute
s
at will. You can even replace the methods of an existing object if
you like:
you like:
...
@@ -419,7 +417,7 @@ this - take a look at the appendix for a [brief discussion on this topic](todo).
...
@@ -419,7 +417,7 @@ this - take a look at the appendix for a [brief discussion on this topic](todo).
Python tends to assume that programmers are "responsible adults", and hence
Python tends to assume that programmers are "responsible adults", and hence
doesn't do much in the way of restricting access to the attributes or methods
doesn't do much in the way of restricting access to the attributes or methods
of an object. This is in contrast to languages like C++ and Java, where the
of an object. This is in contrast to languages like C++ and Java, where the
notion of a private attribute or method is enforced by the language.
notion of a private attribute or method is
strictly
enforced by the language.
However, there are a couple of conventions in Python that are
[
universally
However, there are a couple of conventions in Python that are
[
universally
...
@@ -534,9 +532,9 @@ class FSLMaths(object):
...
@@ -534,9 +532,9 @@ class FSLMaths(object):
```
```
Property setters are a nice way to
restrict the values that a property may
Property setters are a nice way to
add validation logic when an attribute is
take - note that we perform a sanity check in the
`input`
setter, to make
assigned a value. We are doing this in the above example, by making sure that
sure that
the new input is a NIFTI image:
the new input is a NIFTI image:
```
```
...
@@ -562,6 +560,306 @@ fm.input = 'abcde'
...
@@ -562,6 +560,306 @@ fm.input = 'abcde'
> validate the initial `inimg` that was passed in during creation.
> validate the initial `inimg` that was passed in during creation.
## Inheritance
One of the major advantages of an object-oriented programming approach is
_inheritance_
- the ability to define hierarchical relationships between
classes and instances.
### The basics
For example, a veterinary surgery might be running some Python code which
looks like the following. Perhaps it is used to assist the nurses in
identifying an animal when it arrives at the surgery:
```
class Animal(object):
def noiseMade(self):
raise NotImplementedError('This method is implemented by sub-classes')
class Dog(Animal):
def noiseMade(self):
return 'Woof'
class Cat(Animal):
def noiseMade(self):
return 'Meow'
class Labrador(Dog):
pass
class Chihuahua(Dog):
def noiseMade(self):
return 'Yap yap yap'
```
Hopefully this example doesn't need much in the way of explanation - this
collection of classes captures a hierarchical relationship which exists in the
real world (and also captures the inherently annoying nature of
chihuahuas). For example, in the real world, all dogs are animals, but not all
animals are dogs. Therefore in our model, the
`Dog`
class has specified
`Animal`
as its base class. We say that the
`Dog`
class _extends_, _derives
from_, or _inherits from_, the
`Animal`
class, and that all
`Dog`
instances
are also
`Animal`
instances (but not vice-versa).
What does that
`noiseMade`
method do? There is a
`noiseMade`
method defined
on the
`Animal`
class, but it has been re-implemented, or _overridden_ in the
`Dog`
,
`Cat`
, and
`Chihuahua`
classes (but not on the
`Labrador`
class). We
can call the
`noiseMade`
method on any
`Animal`
instance, but the specific
behaviour that we get is dependent on the specific type of animal.
```
d = Dog()
l = Labrador()
c = Cat()
ch = Chihuahua()
print('Noise made by dogs: {}'.format(d .noiseMade()))
print('Noise made by labradors: {}'.format(l .noiseMade()))
print('Noise made by cats: {}'.format(c .noiseMade()))
print('Noise made by chihuahuas: {}'.format(ch.noiseMade()))
```
### Code re-use and problem decomposition
Inheritance allows us to split a problem into smaller problems, and to re-use
code. Let's demonstrate this with a more involved example. Imagine that a
former colleague had written a class called
`Operator`
:
> I know this is a little abstract (and quite contrived), but bear with me
> here.
```
class Operator(object):
def __init__(self):
self.__operations = []
self.__opFuncs = {}
@property
def operations(self):
return list(self.__operations)
@property
def functions(self):
return dict(self.__opFuncs)
def addFunction(self, name, func):
self.__opFuncs[name] = func
def do(self, name, *values):
self.__operations.append((name, values))
def preprocess(self, value):
return value
def run(self, input):
data = self.preprocess(input)
for oper, vals in self.__operations:
func = self.__opFuncs[oper]
vals = [self.preprocess(v) for v in vals]
data = func(data, *vals)
return data
```
This
`Operator`
class provides an interface and logic to execute a chain of
operations - an operation is some function which accepts one or more inputs,
and produce one output.
But it stops short of defining any operations. Instead, we can create another
class - a sub-class - which derives from the
`Operator`
class. This sub-class
will define the operations that will ultimately be executed by the
`Operator`
class. All that the
`Operator`
class does is:
-
Allow functions to be registered with the
`addFunction`
method - all
registered functions can be used via the
`do`
method.
-
Stage an operation (using a registered function) via the
`do`
method. Note
that
`do`
allows any number of values to be passed to it, as we used the
`*`
operator when specifying the
`values`
argument.
-
Run all staged operations via the
`run`
method - it passes an input through
all of the operations that have been staged, and then returns the final
result.
Let's define a sub-class:
```
class NumberOperator(Operator):
def __init__(self):
super().__init__()
self.addFunction('add', self.add)
self.addFunction('mul', self.mul)
self.addFunction('negate', self.negate)
def preprocess(self, value):
return float(value)
def add(self, a, b):
return a + b
def mul(self, a, b):
return a * b
def negate(self, a):
return -a
```
The
`NumberOperator`
is a sub-class of
`Operator`
, which we can use for basic
numerical calculations. It provides a handful of simple numerical methods, but
the most interesting stuff is inside
`__init__`
:
> ```
> super().__init__()
> ```
This line invokes
`Operator.__init__`
- the initialisation method for the
`Operator`
base-class. In Python, we can use the
[
built-in `super`
method
](
https://docs.python.org/3.5/library/functions.html#super
)
to take care
of correctly calling methods that are defined in an object's base-class (or
classes, in the case of
[
multiple inheritance
](
todo
)
).
> ```
> self.addFunction('add', self.add)
> self.addFunction('mul', self.mul)
> self.addFunction('negate', self.negate)
> ```
Here we are registering all of the functionality that is provided by the
`NumberOperator`
class, via the
`Opoerator.addFunction`
method.
The
`NumberOperator`
class has also overridden the
`preprocess`
method, to
ensure that all values handled by the
`Operator`
are numbers. This method gets
called within the
`run`
method - for a
`NumberOperator`
instance, the
`NumberOperator.preprocess`
method will get called
<sup>
1
</sup>
.
> <sup>1</sup> We can still [access overridden base-class methods](todo link)
> via the `super()` function, or by explicitly calling the base-class
> implementation.
Now let's see what our
`NumberOperator`
class does:
```
no = NumberOperator()
no.do('add', 10)
no.do('mul', 2)
no.do('negate')
print('Operations on {}: {}'.format(10, no.run(10)))
print('Operations on {}: {}'.format(2.5, no.run(5)))
```
It works! While this is a contrived example, hopefully you can see how
inheritance can be used to break a problem down into sub-problems:
-
The
`Operator`
class provides all of the logic needed to manage and execute
operations, without caring about what those operations are actually doing.
-
This leaves the
`NumberOperator`
class free to concentrate on implementing
the functions that are specific to its task, and not having to worry about
how they are executed.
We could also easily implement other
`Operator`
sub-classes to work on
different data types, such as arrays, images, or even non-numeric data such as
strings:
```
class StringOperator(Operator):
def __init__(self):
super().__init__()
self.addFunction('capitalise', self.capitalise)
self.addFunction('concat', self.concat)
def preprocess(self, value):
return str(value)
def capitalise(self, s):
return ' '.join([w[0].upper() + w[1:] for w in s.split()])
def concat(self, s1, s2):
return s1 + s2
so = StringOperator()
so.do('capitalise')
so.do('concat', '!')
print(so.run('python is an ok language'))
```
### Polymorphism
Inheritance also allows us to take advantage of _polymorphism_, which refers
to idea that, in an object-oriented language, we should be able to use an
object without having complete knowledge about the class, or type, of that
object. For example, we should be able to write a function which expects an
`Operator`
instance, but which should work on an instance of any
`Operator`
sub-classs. For example, we can write a function which prints a summary
of an
`Operator`
instance:
```
def operatorSummary(o):
print(type(o).__name__)
print(' All functions: ')
for fname in o.functions.keys():
print(' {}'.format(fname))
print(' Staged operations: ')
for i, (fname, vals) in enumerate(o.operations):
vals = ', '.join([str(v) for v in vals])
print(' {}: {}({})'.format(i + 1, fname, vals))
```
Because the
`operatorSummary`
function only uses methods that are defined
in the
`Operator`
base-class, we can use it on _any_
`Operator`
instance,
regardless of its type:
```
operatorSummary(no)
operatorSummary(so)
```
### Multiple inheritance
Mention the MRO
## Class attributes and methods
## Class attributes and methods
...
@@ -708,7 +1006,7 @@ class FSLMaths(object):
...
@@ -708,7 +1006,7 @@ class FSLMaths(object):
```
```
C
alling a class method is the same as accessing a class attribute:
alling a class method is the same as accessing a class attribute:
```
```
...
@@ -741,9 +1039,6 @@ print(fm1.usage())
...
@@ -741,9 +1039,6 @@ print(fm1.usage())
```
```
## Inheritance
## Appendix: The `object` base-class
## Appendix: The `object` base-class
...
@@ -822,3 +1117,58 @@ party library which has bugs in it. No problem - while you are waiting for the
...
@@ -822,3 +1117,58 @@ party library which has bugs in it. No problem - while you are waiting for the
library author to release a new version of the library, you can write your own
library author to release a new version of the library, you can write your own
working implementation and
[
monkey-patch it
working implementation and
[
monkey-patch it
in
](
https://git.fmrib.ox.ac.uk/fsl/fsleyes/fsleyes/blob/0.21.0/fsleyes/views/viewpanel.py#L726
)
!
in
](
https://git.fmrib.ox.ac.uk/fsl/fsleyes/fsleyes/blob/0.21.0/fsleyes/views/viewpanel.py#L726
)
!
## Appendix: Method overloading
Method overloading (defining multiple methods on a class, each accepting
different arguments) is one of the only object-oriented features that is not
present in Python. Becuase Python does not perform any runtime checks on the
types of arguments that are passed to a method, or the compatibility of the
method to accept the arguments, it would not be possible to determine which
implementation of a method is to be called.
However, because a Python method can be written to accept any number or type
of arguments, it is very easy to to build your own overloading logic by
writing a "dispatch" method. Here is YACE (Yet Another Contrived Example):
```
class Adder(object):
def add(self, *args):
if len(args) == 2: return self.__add2(*args)
elif len(args) == 3: return self.__add3(*args)
elif len(args) == 4: return self.__add4(*args)
def __add2(self, a, b):
return a + b
def __add3(self, a, b, c):
return a + b + c
def __add4(self, a, b, c, d):
return a + b + c + d
a = Adder()
print('Add two: {}'.format(a.add(1, 2)))
print('Add three: {}'.format(a.add(1, 2, 3)))
print('Add four: {}'.format(a.add(1, 2, 3, 4)))
```
## Useful references
https://docs.python.org/3.5/library/unittest.mock.html
https://docs.python.org/3.5/tutorial/classes.html
https://docs.python.org/3.5/library/functions.html
https://docs.python.org/2/reference/datamodel.html
https://www.reddit.com/r/learnpython/comments/2s3pms/what_is_the_difference_between_init_and_new/cnm186z/
https://docs.python.org/3.5/reference/datamodel.html
http://www.jesshamrick.com/2011/05/18/an-introduction-to-classes-and-inheritance-in-python/
https://www.digitalocean.com/community/tutorials/understanding-class-inheritance-in-python-3
https://docs.python.org/3.5/library/functions.html#super
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment