Skip to content
Snippets Groups Projects
Commit 93751b2f authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

Update use of nibabel.Nifti1Image.get_data -> get_fdata. Fix bug in fsleyes

render  - it's --outfile / -of, not -outputFile
parent 0de3bf27
No related branches found
No related tags found
1 merge request!36Update use of nibabel.Nifti1Image.get_data -> get_fdata. Fix bug in fsleyes render - it's --outfile / -of, not -outputFile
%% Cell type:markdown id: tags:
# Object-oriented programming in Python
By now you might have realised that __everything__ in Python is an
object. Strings are objects, numbers are objects, functions are objects,
modules are objects - __everything__ is an object!
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
done. But some problems are just easier to solve, and to reason about, when
you use an object-oriented approach.
* [Objects versus classes](#objects-versus-classes)
* [Defining a class](#defining-a-class)
* [Object creation - the `__init__` method](#object-creation-the-init-method)
* [Our method is called `__init__`, but we didn't actually call the `__init__` method!](#our-method-is-called-init)
* [We didn't specify the `self` argument - what gives?!?](#we-didnt-specify-the-self-argument)
* [Attributes](#attributes)
* [Methods](#methods)
* [Method chaining](#method-chaining)
* [Protecting attribute access](#protecting-attribute-access)
* [A better way - properties](#a-better-way-properties])
* [Inheritance](#inheritance)
* [The basics](#the-basics)
* [Code re-use and problem decomposition](#code-re-use-and-problem-decomposition)
* [Polymorphism](#polymorphism)
* [Multiple inheritance](#multiple-inheritance)
* [Class attributes and methods](#class-attributes-and-methods)
* [Class attributes](#class-attributes)
* [Class methods](#class-methods)
* [Appendix: The `object` base-class](#appendix-the-object-base-class)
* [Appendix: `__init__` versus `__new__`](#appendix-init-versus-new)
* [Appendix: Monkey-patching](#appendix-monkey-patching)
* [Appendix: Method overloading](#appendix-method-overloading)
* [Useful references](#useful-references)
<a class="anchor" id="objects-versus-classes"></a>
## Objects versus classes
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
section.
If you have not done any object-oriented programming before, your first step
is to understand the difference between *objects* (also known as
*instances*) and *classes* (also known as *types*).
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
layout of a chunk of memory. For example, here is a typical struct definition:
> ```
> /**
> * Struct representing a stack.
> */
> typedef struct __stack {
> uint8_t capacity; /**< the maximum capacity of this stack */
> uint8_t size; /**< the current size of this stack */
> void **top; /**< pointer to the top of this stack */
> } stack_t;
> ```
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*
(functions associated with the object). You can pass objects around your code,
manipulate their attributes, and call their methods.
Returning to our C metaphor, you can think of an object as like an
instantiation of a struct:
> ```
> stack_t stack;
> stack.capacity = 10;
> ```
One of the major differences between a `struct` in C, and a `class` in Python
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
functions to your class definition, which will then be added as methods when
you create an object from that class.
Of course there are many more differences between C structs and classes (most
notably [inheritance](todo), [polymorphism](todo), and [access
protection](todo)). But if you can understand the difference between a
*definition* of a C struct, and an *instantiation* of that struct, then you
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
> object - even classes!
<a class="anchor" id="defining-a-class"></a>
## Defining a class
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.
%% Cell type:code id: tags:
```
class FSLMaths(object):
pass
```
%% Cell type:markdown id: tags:
In this statement, we defined a new class called `FSLMaths`, which inherits
from the built-in `object` base-class (see [below](inheritance) for more
details on inheritance).
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:
%% Cell type:code id: tags:
```
fm1 = FSLMaths()
fm2 = FSLMaths()
print(fm1)
print(fm2)
```
%% Cell type:markdown id: tags:
Although these objects are not of much use at this stage. Let's do some more
work.
<a class="anchor" id="object-creation-the-init-method"></a>
## Object creation - the `__init__` method
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:
%% Cell type:code id: tags:
```
class FSLMaths(object):
def __init__(self, inimg):
self.img = inimg
```
%% Cell type:markdown id: tags:
Here we have added a _method_ called `__init__` to our class (remember that a
_method_ is just a function which is defined in a class, and which can be
called on instances of that class). This method expects two arguments -
`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:
```
import nibabel as nib
import os.path as op
fpath = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')
inimg = nib.load(fpath)
fm = FSLMaths(inimg)
```
%% Cell type:markdown id: tags:
There are a couple of things to note here...
<a class="anchor" id="our-method-is-called-init"></a>
### Our method is called `__init__`, but we didn't actually call the `__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 instance of a class by
calling the class in the same way that we call a function.
There are a number of "special" methods that you can add to a class in Python
to customise various aspects of how instances of the class behave. One of the
first ones you may come across is the `__str__` method, which defines how an
object should be printed (more specifically, how an object gets converted into
a string). For example, we could add a `__str__` method to our `FSLMaths`
class like so:
%% Cell type:code id: tags:
```
class FSLMaths(object):
def __init__(self, inimg):
self.img = inimg
def __str__(self):
return f'FSLMaths({self.img.get_filename()})'
fpath = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')
inimg = nib.load(fpath)
fm = FSLMaths(inimg)
print(fm)
```
%% Cell type:markdown id: tags:
Refer to the [official
docs](https://docs.python.org/3/reference/datamodel.html#special-method-names)
for details on all of the special methods that can be defined in a class. And
take a look at the appendix for some more details on [how Python objects get
created](appendix-init-versus-new).
<a class="anchor" id="we-didnt-specify-the-self-argument"></a>
### 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, C# or similar, `self` in Python is equivalent to `this`
in those languages.
In a method, the `self` argument is a reference to the object that the method
was called on. So in this line of code:
%% Cell type:code id: tags:
```
fm = FSLMaths(inimg)
```
%% Cell type:markdown id: tags:
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
`__init__` method has finished).
But note that you __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 runtime will take care of passing the instance to its method, as the
first argument to the method.
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.
<a class="anchor" id="attributes"></a>
## Attributes
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
another object (which might be a string, a number, or a list, or some other
more complicated object).
Remember that we modified our `FSLMaths` class so that it is passed an input
image on creation:
%% Cell type:code id: tags:
```
class FSLMaths(object):
def __init__(self, inimg):
self.img = inimg
fpath = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')
fm = FSLMaths(nib.load(fpath))
```
%% Cell type:markdown id: tags:
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.img`. We have added an
_attribute_ to the `FSLMaths` instance, called `img`, and we can access that
attribute like so:
%% Cell type:code id: tags:
```
print('Input for our FSLMaths instance:', fm.img.get_filename())
```
%% Cell type:markdown id: tags:
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
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
all of the attributes and methods that are a part of a class. But Python is
much more flexible - you can simply add attributes to an object after it has
been created. In fact, you can even do this outside of the class
definition<sup>1</sup>:
%% Cell type:code id: tags:
```
fm = FSLMaths(inimg)
fm.another_attribute = 'Haha'
print(fm.another_attribute)
```
%% Cell type:markdown id: tags:
__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
of an object's attributes within the `__init__` method.
> <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
> extensions (Python modules that are written in C).
<a class="anchor" id="methods"></a>
## Methods
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
functionality:
%% Cell type:code id: tags:
```
class FSLMaths(object):
def __init__(self, inimg):
self.img = inimg
self.operations = []
def add(self, value):
self.operations.append(('add', value))
def mul(self, value):
self.operations.append(('mul', value))
def div(self, value):
self.operations.append(('div', value))
```
%% Cell type:markdown id: tags:
Woah woah, [slow down egg-head!](https://www.youtube.com/watch?v=yz-TemWooa4)
We've modified `__init__` so that a second attribute called `operations` is
added to our object - this `operations` attribute is simply a list.
Then, we added a handful of methods - `add`, `mul`, and `div` - which each
append a tuple to that `operations` list.
> 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
> method has been called on.
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
*stage* each operation, and then perform them all in one go at a later point
in time. So let's add another method, `run`, which actually does the work:
%% Cell type:code id: tags:
```
import numpy as np
import nibabel as nib
class FSLMaths(object):
def __init__(self, inimg):
self.img = inimg
self.operations = []
def add(self, value):
self.operations.append(('add', value))
def mul(self, value):
self.operations.append(('mul', value))
def div(self, value):
self.operations.append(('div', value))
def run(self, output=None):
data = np.array(self.img.get_data())
data = np.array(self.img.get_fdata())
for oper, value in self.operations:
# Value could be an image.
# If not, we assume that
# it is a scalar/numpy array.
if isinstance(value, nib.nifti1.Nifti1Image):
value = value.get_data()
value = value.get_fdata()
if oper == 'add':
data = data + value
elif oper == 'mul':
data = data * value
elif oper == 'div':
data = data / value
# turn final output into a nifti,
# and save it to disk if an
# 'output' has been specified.
outimg = nib.nifti1.Nifti1Image(data, inimg.affine)
if output is not None:
nib.save(outimg, output)
return outimg
```
%% Cell type:markdown id: tags:
We now have a useable (but not very useful) `FSLMaths` class!
%% Cell type:code id: tags:
```
fpath = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')
fmask = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm_brain_mask.nii.gz')
inimg = nib.load(fpath)
mask = nib.load(fmask)
fm = FSLMaths(inimg)
fm.mul(mask)
fm.add(-10)
outimg = fm.run()
norigvox = (inimg .get_data() > 0).sum()
nmaskvox = (outimg.get_data() > 0).sum()
norigvox = (inimg .get_fdata() > 0).sum()
nmaskvox = (outimg.get_fdata() > 0).sum()
print(f'Number of voxels >0 in original image: {norigvox}')
print(f'Number of voxels >0 in masked image: {nmaskvox}')
```
%% Cell type:markdown id: tags:
<a class="anchor" id="method-chaining"></a>
## Method chaining
A neat trick, which is used by all the cool kids these days, is to write
classes that allow *method chaining* - writing one line of code which
calls more than one method on an object, e.g.:
> ```
> fm = FSLMaths(img)
> result = fm.add(1).mul(10).run()
> ```
Adding this feature to our budding `FSLMaths` class is easy - all we have
to do is return `self` from each method:
%% Cell type:code id: tags:
```
import numpy as np
import nibabel as nib
class FSLMaths(object):
def __init__(self, inimg):
self.img = inimg
self.operations = []
def add(self, value):
self.operations.append(('add', value))
return self
def mul(self, value):
self.operations.append(('mul', value))
return self
def div(self, value):
self.operations.append(('div', value))
return self
def run(self, output=None):
data = np.array(self.img.get_data())
data = np.array(self.img.get_fdata())
for oper, value in self.operations:
# Value could be an image.
# If not, we assume that
# it is a scalar/numpy array.
if isinstance(value, nib.nifti1.Nifti1Image):
value = value.get_data()
value = value.get_fdata()
if oper == 'add':
data = data + value
elif oper == 'mul':
data = data * value
elif oper == 'div':
data = data / value
# turn final output into a nifti,
# and save it to disk if an
# 'output' has been specified.
outimg = nib.nifti1.Nifti1Image(data, inimg.affine)
if output is not None:
nib.save(outimg, output)
return outimg
```
%% Cell type:markdown id: tags:
Now we can chain all of our method calls, and even the creation of our
`FSLMaths` object, into a single line:
%% Cell type:code id: tags:
```
fpath = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')
fmask = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm_brain_mask.nii.gz')
inimg = nib.load(fpath)
mask = nib.load(fmask)
outimg = FSLMaths(inimg).mul(mask).add(-10).run()
norigvox = (inimg .get_data() > 0).sum()
nmaskvox = (outimg.get_data() > 0).sum()
norigvox = (inimg .get_fdata() > 0).sum()
nmaskvox = (outimg.get_fdata() > 0).sum()
print(f'Number of voxels >0 in original image: {norigvox}')
print(f'Number of voxels >0 in masked image: {nmaskvox}')
```
%% Cell type:markdown id: tags:
> In fact, this is precisely how the
> [`fsl.wrappers.fslmaths`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.wrappers.fslmaths.html)
> function works.
<a class="anchor" id="protecting-attribute-access"></a>
## Protecting attribute access
In our `FSLMaths` class, the input image was added as an attribute called
`img` 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
input image via `fm.img`.
But it is just as easy to write the attributes of an object. What's to stop
some sloppy research assistant from overwriting our `img` attribute?
%% Cell type:code id: tags:
```
inimg = nib.load(op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz'))
fm = FSLMaths(inimg)
fm.img = None
fm.run()
```
%% Cell type:markdown id: tags:
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
attributes at will. You can even replace the methods of an existing object if
you like:
%% Cell type:code id: tags:
```
fm = FSLMaths(inimg)
def myadd(value):
print('Oh no, I\'m not going to add {} to '
'your image. Go away!'.format(value))
fm.add = myadd
fm.add(123)
fm.mul = None
fm.mul(123)
```
%% Cell type:markdown id: tags:
But you really shouldn't get into the habit of doing devious things like
this. Think of the poor souls who inherit your code years after you have left
the lab - if you go around overwriting all of the methods and attributes of
your objects, they are not going to have a hope in hell of understanding what
your code is actually doing, and they are not going to like you very
much. Take a look at the appendix for a [brief discussion on this
topic](appendix-monkey-patching).
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
of an object. This is in contrast to languages like C++ and Java, where the
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 adhered to](https://docs.python.org/3/tutorial/classes.html#private-variables):
* Class-level attributes and methods, and module-level attributes, functions,
and classes, which begin with a single underscore (`_`), should be
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 enforced by the language in any way<sup>2</sup> - remember, we are
all responsible adults here!
* Class-level attributes and methods which begin with a double-underscore
(`__`) should be considered __private__. Python provides a weak form of
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
not accessible through its original name (it is still accessible via its
[mangled name](https://docs.python.org/3/tutorial/classes.html#private-variables)
though).
> <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
> `from [module] import *` technique.
So with all of this in mind, we can adjust our `FSLMaths` class to discourage
our sloppy research assistant from overwriting the `img` attribute:
%% Cell type:code id: tags:
```
# remainder of definition omitted for brevity
class FSLMaths(object):
def __init__(self, inimg):
self.__img = inimg
self.__operations = []
```
%% Cell type:markdown id: tags:
But now we have lost the ability to read our `__img` attribute:
%% Cell type:code id: tags:
```
inimg = nib.load(op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz'))
fm = FSLMaths(inimg)
print(fm.__img)
```
%% Cell type:markdown id: tags:
<a class="anchor" id="a-better-way-properties"></a>
### A better way - properties
Python has a feature called
[`properties`](https://docs.python.org/3/library/functions.html#property),
which is a nice way of controlling access to the attributes of an object. We
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
cover decorators in a later practical).
%% Cell type:code id: tags:
```
class FSLMaths(object):
def __init__(self, inimg):
self.__img = inimg
self.__operations = []
@property
def img(self):
return self.__img
```
%% Cell type:markdown id: tags:
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 public `img` property:
%% Cell type:code id: tags:
```
fpath = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')
inimg = nib.load(fpath)
fm = FSLMaths(inimg)
print(fm.img.get_filename())
```
%% Cell type:markdown id: tags:
Note that, even though we have defined `img` as a method, we can access it
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
to add the ability for a user of our `FSLMaths` class to change the input
image after creation.
%% Cell type:code id: tags:
```
class FSLMaths(object):
def __init__(self, inimg):
self.__img = None
self.__operations = []
self.img = inimg
@property
def img(self):
return self.__img
@img.setter
def img(self, value):
if not isinstance(value, nib.nifti1.Nifti1Image):
raise ValueError('value must be a NIFTI image!')
self.__img = value
```
%% Cell type:markdown id: tags:
> Note that we used the `img` setter method within `__init__` to validate the
> initial `inimg` that was passed in during creation.
Property setters are a nice way to add validation logic for when an attribute
is assigned a value. In this example, an error will be raised if the new input
is not a NIFTI image.
%% Cell type:code id: tags:
```
fpath = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')
inimg = nib.load(fpath)
fm = FSLMaths(inimg)
print('Input: ', fm.img.get_filename())
# let's change the input
# to a different image
fpath2 = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm_brain.nii.gz')
inimg2 = nib.load(fpath2)
fm.img = inimg2
print('New input: ', fm.img.get_filename())
print('This is going to explode')
fm.img = 'abcde'
```
%% Cell type:markdown id: tags:
<a class="anchor" id="inheritance"></a>
## Inheritance
One of the major advantages of an object-oriented programming approach is
_inheritance_ - the ability to define hierarchical relationships between
classes and instances.
<a class="anchor" id="the-basics"></a>
### The basics
My local veterinary surgery runs some Python code which looks like the
following, 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 must be '
'implemented by sub-classes')
class Dog(Animal):
def noiseMade(self):
return 'Woof'
class TalkingDog(Dog):
def noiseMade(self):
return 'Hi Homer, find your soulmate!'
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 represents a hierarchical relationship which exists in
the real world (and also represents 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`,
[`TalkingDog`](https://twitter.com/simpsonsqotd/status/427941665836630016?lang=en),
`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: ', d .noiseMade())
print('Noise made by labradors: ', l .noiseMade())
print('Noise made by cats: ', c .noiseMade())
print('Noise made by chihuahuas:', ch.noiseMade())
```
%% Cell type:markdown id: tags:
Note that calling the `noiseMade` method on a `Labrador` instance resulted in
the `Dog.noiseMade` implementation being called.
<a class="anchor" id="code-re-use-and-problem-decomposition"></a>
### 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 (and even more contrived)
example. Imagine that a former colleague had written a class called
`Operator`:
%% Cell type:code id: tags:
```
class Operator(object):
def __init__(self):
super().__init__() # this line will be explained later
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/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](multiple-inheritance)).
> The `super` function is one thing which changed between Python 2 and 3 -
> in Python 2, it was necessary to pass both the type and the instance
> to `super`. So it is common to see code that looks like this:
>
> ```
> def __init__(self):
> super(NumberOperator, self).__init__()
> ```
>
> Fortunately things are a lot cleaner in Python 3.
Let's move on to the next few lines in `__init__`:
> ```
> 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 `Operator.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 `Operator.run` method - for a `NumberOperator` instance, the
`NumberOperator.preprocess` method will get called<sup>3</sup>.
> <sup>3</sup> When a sub-class overrides a base-class method, it is still
> possible to access the base-class implementation [via the `super()`
> function](https://stackoverflow.com/a/4747427) (the preferred method), or by
> [explicitly calling the base-class
> implementation](https://stackoverflow.com/a/2421325).
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:
<a class="anchor" id="polymorphism"></a>
### Polymorphism
Inheritance also allows us to take advantage of *polymorphism*, which refers
to the 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 will work on an instance of any `Operator`
sub-classs. As an example, let's 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(' ', fname)
print(' Staged operations: ')
for i, (fname, vals) in enumerate(o.operations):
vals = ', '.join([str(v) for v in vals])
print(f' {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 specific type:
%% Cell type:code id: tags:
```
operatorSummary(no)
operatorSummary(so)
```
%% Cell type:markdown id: tags:
<a class="anchor" id="multiple-inheritance"></a>
### Multiple inheritance
Python allows you to define a class which has multiple base classes - this is
known as _multiple inheritance_. For example, we might want to build a
notification mechanisim into our `StringOperator` class, so that listeners can
be notified whenever the `capitalise` method gets called. It so happens that
our old colleague of `Operator` class fame also wrote a `Notifier` class which
allows listeners to register to be notified when an event occurs:
%% Cell type:code id: tags:
```
class Notifier(object):
def __init__(self):
super().__init__()
self.__listeners = {}
def register(self, name, func):
self.__listeners[name] = func
def notify(self, *args, **kwargs):
for func in self.__listeners.values():
func(*args, **kwargs)
```
%% Cell type:markdown id: tags:
Let's modify the `StringOperator` class to use the functionality of the
`Notifier ` class:
%% Cell type:code id: tags:
```
class StringOperator(Operator, Notifier):
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):
result = ' '.join([w[0].upper() + w[1:] for w in s.split()])
self.notify(result)
return result
def concat(self, s1, s2):
return s1 + s2
```
%% Cell type:markdown id: tags:
Now, anything which is interested in uses of the `capitalise` method can
register as a listener on a `StringOperator` instance:
%% Cell type:code id: tags:
```
so = StringOperator()
def capitaliseCalled(result):
print('Capitalise operation called:', result)
so.register('mylistener', capitaliseCalled)
so.do('capitalise')
so.do('concat', '?')
print(so.run('did you notice that functions are objects too'))
```
%% Cell type:markdown id: tags:
> Simple classes such as the `Notifier` are sometimes referred to as
> [_mixins_](https://en.wikipedia.org/wiki/Mixin).
If you wish to use multiple inheritance in your design, it is important to be
aware of the mechanism that Python uses to determine how base class methods
are called (and which base class method will be called, in the case of naming
conflicts). This is referred to as the Method Resolution Order (MRO) - further
details on the topic can be found
[here](https://www.python.org/download/releases/2.3/mro/), and a more concise
summary
[here](http://python-history.blogspot.co.uk/2010/06/method-resolution-order.html).
Note also that for base class `__init__` methods to be correctly called in a
design which uses multiple inheritance, _all_ classes in the hierarchy must
invoke `super().__init__()`. This can become complicated when some base
classes expect to be passed arguments to their `__init__` method. In scenarios
like this it may be prefereable to manually invoke the base class `__init__`
methods instead of using `super()`. For example:
> ```
> class StringOperator(Operator, Notifier):
> def __init__(self):
> Operator.__init__(self)
> Notifier.__init__(self)
> ```
This approach has the disadvantage that if the base classes change, you will
have to change these invocations. But the advantage is that you know exactly
how the class hierarchy will be initialised. In general though, doing
everything with `super()` will result in more maintainable code.
<a class="anchor" id="class-attributes-and-methods"></a>
## Class attributes and methods
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_
(`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
instance of the class - they are not associated with individual objects, but
rather with the class itself.
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
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
application to show evidence that more research is needed to optimise the
performance of the `add` operation.
<a class="anchor" id="class-attributes"></a>
### Class attributes
Let's add a `dict` called `opCounters` as a class attribute to the `FSLMaths`
class - whenever an operation is called on a `FSLMaths` instance, the counter
for that operation will be incremented:
%% Cell type:code id: tags:
```
import numpy as np
import nibabel as nib
class FSLMaths(object):
# It's this easy to add a class-level
# attribute. This dict is associated
# with the FSLMaths *class*, not with
# any individual FSLMaths instance.
opCounters = {}
def __init__(self, inimg):
self.img = inimg
self.operations = []
def add(self, value):
self.operations.append(('add', value))
return self
def mul(self, value):
self.operations.append(('mul', value))
return self
def div(self, value):
self.operations.append(('div', value))
return self
def run(self, output=None):
data = np.array(self.img.get_data())
data = np.array(self.img.get_fdata())
for oper, value in self.operations:
# Code omitted for brevity
# Increment the usage counter for this operation. We can
# access class attributes (and methods) through the class
# itself, as shown here.
FSLMaths.opCounters[oper] = FSLMaths.opCounters.get(oper, 0) + 1
# It is also possible to access class-level
# attributes via instances of the class, e.g.
# self.opCounters[oper] = self.opCounters.get(oper, 0) + 1
```
%% Cell type:markdown id: tags:
So let's see it in action:
%% Cell type:code id: tags:
```
fpath = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')
fmask = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm_brain_mask.nii.gz')
inimg = nib.load(fpath)
mask = nib.load(fmask)
FSLMaths(inimg).mul(mask).add(25).run()
FSLMaths(inimg).add(15).div(1.5).run()
print('FSLMaths usage statistics')
for oper in ('add', 'div', 'mul'):
print(' {} : {}'.format(oper, FSLMaths.opCounters.get(oper, 0)))
```
%% Cell type:markdown id: tags:
<a class="anchor" id="class-methods"></a>
### Class methods
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.
A class method is denoted by the
[`@classmethod`](https://docs.python.org/3.5/library/functions.html#classmethod)
decorator. Note that, where a regular method which is called on an instance
will be passed the instance as its first argument (`self`), a class method
will be passed the class itself as the first argument - the standard
convention is to call this argument `cls`:
%% Cell type:code id: tags:
```
class FSLMaths(object):
opCounters = {}
@classmethod
def usage(cls):
print('FSLMaths usage statistics')
for oper in ('add', 'div', 'mul'):
print(' {} : {}'.format(oper, FSLMaths.opCounters.get(oper, 0)))
def __init__(self, inimg):
self.img = inimg
self.operations = []
def add(self, value):
self.operations.append(('add', value))
return self
def mul(self, value):
self.operations.append(('mul', value))
return self
def div(self, value):
self.operations.append(('div', value))
return self
def run(self, output=None):
data = np.array(self.img.get_data())
data = np.array(self.img.get_fdata())
for oper, value in self.operations:
FSLMaths.opCounters[oper] = self.opCounters.get(oper, 0) + 1
```
%% Cell type:markdown id: tags:
> There is another decorator -
> [`@staticmethod`](https://docs.python.org/3.5/library/functions.html#staticmethod) -
> which can be used on methods defined within a class. The difference
> between a `@classmethod` and a `@staticmethod` is that the latter will *not*
> be passed the class (`cls`).
Calling a class method is the same as accessing a class attribute:
%% Cell type:code id: tags:
```
fpath = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm.nii.gz')
fmask = op.expandvars('$FSLDIR/data/standard/MNI152_T1_2mm_brain_mask.nii.gz')
inimg = nib.load(fpath)
mask = nib.load(fmask)
fm1 = FSLMaths(inimg).mul(mask).add(25)
fm2 = FSLMaths(inimg).add(15).div(1.5)
fm1.run()
fm2.run()
FSLMaths.usage()
```
%% Cell type:markdown id: tags:
Note that it is also possible to access class attributes and methods through
instances:
%% Cell type:code id: tags:
```
print(fm1.opCounters)
fm1.usage()
```
%% Cell type:markdown id: tags:
<a class="anchor" id="appendix-the-object-base-class"></a>
## Appendix: The `object` base-class
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,
then it should inherit from the built-in `object` class:
> ```
> class MyClass(object):
> ...
> ```
However, in older code bases, you might see class definitions that look like
this, without explicitly inheriting from the `object` base class:
> ```
> class MyClass:
> ...
> ```
This syntax is a [throwback to older versions of
Python](https://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes).
In Python 3 there is actually no difference in defining classes in the
"new-style" way we have used throughout this tutorial, or the "old-style" way
mentioned in this appendix.
But if you are writing code which needs to run on both Python 2 and 3, you
__must__ define your classes in the new-style convention, i.e. by explicitly
inheriting from the `object` base class. Therefore, the safest approach is to
always use the new-style format.
<a class="anchor" id="appendix-init-versus-new"></a>
## Appendix: `__init__` versus `__new__`
In Python, object creation is actually a two-stage process - *creation*, and
then *initialisation*. The `__init__` method gets called during the
*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
created.
You can also define a method called `__new__` if you need to control the
creation stage, although this is very rarely needed. One example of where you
might need to implement the `__new__` method is if you wish to create a
[subclass of a
`numpy.array`](https://docs.scipy.org/doc/numpy-1.14.0/user/basics.subclassing.html)
(although you might alternatively want to think about redefining your problem
so that this is not necessary).
A brief explanation on
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/),
and you may also wish to take a look at the [official Python
docs](https://docs.python.org/3/reference/datamodel.html#basic-customization).
<a class="anchor" id="appendix-monkey-patching"></a>
## Appendix: Monkey-patching
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,
whilst it is allowed by the Python programming language, it is generally
considered quite bad practice.
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
suitable. **But** if you want to write software that will be used, adopted,
maintained, and enjoyed by other people, you should be polite, write your code
in a clear, readable fashion, and avoid the use of devious tactics such as
monkey-patching.
**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,
(and it is horrific in many cases), it can be *extremely* useful in certain
circumstances. For instance, monkey-patching makes [unit testing a
breeze in Python](https://docs.python.org/3/library/unittest.mock.html).
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
library author to release a new version of the library, you can write your own
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)
<a class="anchor" id="appendix-method-overloading"></a>
## Appendix: Method overloading
Method overloading (defining multiple methods with the same name in a class,
but 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. In other words,
in Python only the name of a method is used to identify that method, unlike in
C++ and Java, where the full method signature (name, input types and return
types) is used.
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<sup>4</sup>. 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)
else:
raise AttributeError('No method available to accept {} '
'arguments'.format(len(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: ', a.add(1, 2))
print('Add three:', a.add(1, 2, 3))
print('Add four: ', a.add(1, 2, 3, 4))
```
%% Cell type:markdown id: tags:
> <sup>4</sup>Another option is the [`functools.singledispatch`
> decorator](https://docs.python.org/3/library/functools.html#functools.singledispatch),
> which is more complicated, but may allow you to write your dispatch logic in
> a more concise manner.
<a class="anchor" id="useful-references"></a>
## Useful references
The official Python documentation has a wealth of information on the internal
workings of classes and objects, so these pages are worth a read:
* https://docs.python.org/3/tutorial/classes.html
* https://docs.python.org/3/reference/datamodel.html
......
......@@ -378,7 +378,7 @@ class FSLMaths(object):
def run(self, output=None):
data = np.array(self.img.get_data())
data = np.array(self.img.get_fdata())
for oper, value in self.operations:
......@@ -386,7 +386,7 @@ class FSLMaths(object):
# If not, we assume that
# it is a scalar/numpy array.
if isinstance(value, nib.nifti1.Nifti1Image):
value = value.get_data()
value = value.get_fdata()
if oper == 'add':
data = data + value
......@@ -422,8 +422,8 @@ fm.add(-10)
outimg = fm.run()
norigvox = (inimg .get_data() > 0).sum()
nmaskvox = (outimg.get_data() > 0).sum()
norigvox = (inimg .get_fdata() > 0).sum()
nmaskvox = (outimg.get_fdata() > 0).sum()
print(f'Number of voxels >0 in original image: {norigvox}')
print(f'Number of voxels >0 in masked image: {nmaskvox}')
......@@ -470,7 +470,7 @@ class FSLMaths(object):
def run(self, output=None):
data = np.array(self.img.get_data())
data = np.array(self.img.get_fdata())
for oper, value in self.operations:
......@@ -478,7 +478,7 @@ class FSLMaths(object):
# If not, we assume that
# it is a scalar/numpy array.
if isinstance(value, nib.nifti1.Nifti1Image):
value = value.get_data()
value = value.get_fdata()
if oper == 'add':
data = data + value
......@@ -511,8 +511,8 @@ mask = nib.load(fmask)
outimg = FSLMaths(inimg).mul(mask).add(-10).run()
norigvox = (inimg .get_data() > 0).sum()
nmaskvox = (outimg.get_data() > 0).sum()
norigvox = (inimg .get_fdata() > 0).sum()
nmaskvox = (outimg.get_fdata() > 0).sum()
print(f'Number of voxels >0 in original image: {norigvox}')
print(f'Number of voxels >0 in masked image: {nmaskvox}')
......@@ -1213,7 +1213,7 @@ class FSLMaths(object):
def run(self, output=None):
data = np.array(self.img.get_data())
data = np.array(self.img.get_fdata())
for oper, value in self.operations:
......@@ -1294,7 +1294,7 @@ class FSLMaths(object):
def run(self, output=None):
data = np.array(self.img.get_data())
data = np.array(self.img.get_fdata())
for oper, value in self.operations:
FSLMaths.opCounters[oper] = self.opCounters.get(oper, 0) + 1
......
# Creating figures and movies with FSLeyes
You may be familiar with using FSLeyes for looking at your data. Perhaps you have taken screenshots and pasted those into a talk or a paper. Here we will learn how to script such things and ask FSLeyes to create complex figures directly from the command line.
You may be familiar with using FSLeyes for looking at your data. Perhaps you have taken screenshots and pasted those into a talk or a paper. Here we will learn how to script such things and ask FSLeyes to create complex figures directly from the command line.
This has several advantages. For example, you can re-create the same or similar figures simply by editing the script, instead of re-doing all the FSLeyes tinkering. It is also useful if you need to create complex figures for many datasets/subjects.
......@@ -15,9 +15,9 @@ This has several advantages. For example, you can re-create the same or similar
<a class="anchor" id="create-png"></a>
## Using fsleyes render
The first thing we will learn is how to generate a complex command line for FSLeyes.
The first thing we will learn is how to generate a complex command line for FSLeyes.
Everything you can set manually by clicking on the FSLeyes graphical interface can be reproduced through the command line. You can learn all about the command line arguments by reading the [documentation](https://open.win.ox.ac.uk/pages/fsl/fsleyes/fsleyes/userdoc/command_line.html) or looking at the FSLeyes full help (`fsleyes -fh`).
Everything you can set manually by clicking on the FSLeyes graphical interface can be reproduced through the command line. You can learn all about the command line arguments by reading the [documentation](https://open.win.ox.ac.uk/pages/fsl/fsleyes/fsleyes/userdoc/command_line.html) or looking at the FSLeyes full help (`fsleyes -fh`).
Here we will instead cheat and use the lazy approach. We will first use the graphical interface to produce a nice looking image. Then we will generate a full command line in FSLeyes and use that as the basis for creating more similar figures.
......@@ -29,7 +29,7 @@ fsleyes -std1mm &
```
Let's make this brain look funkier. Start by toggling off the sagittal and coronal views. We only want to see the axial view.
Let's make this brain look funkier. Start by toggling off the sagittal and coronal views. We only want to see the axial view.
Next, toggle off the location cursor. You should now see this:
......@@ -42,18 +42,18 @@ Now change the colormap to "Cool" (of course), and open the Overlay display pane
- Change the colourmap resolution from 256 to 7
- Change the Display range to min=1000 and max=8000
Close the display panel and open the View settings (spanner below cogwheel), then change the background color to be white.
Close the display panel and open the View settings (spanner below cogwheel), then change the background color to be white.
If you did the above you should now see:
<img src="data/snapshot2.png" alt="snapshot2" style="width:400px;"/>
Now wouldn't it be nice if one could generate the same thing from scratch without going through the above steps by hand? Here is how you do it:
Now wouldn't it be nice if one could generate the same thing from scratch without going through the above steps by hand? Here is how you do it:
**Settings -> Ortho View 1 -> Show command line for scene**
You can even click on copy to clipboard. Do that, then open a new text file (e.g. with emacs) and paste the result into the text file. The command you get looks very long so I am highlighting in <span style="color:blue">blue</span> the bit about the scene and in <span style="color:red">red</span> the bit about the overlay.
You can even click on copy to clipboard. Do that, then open a new text file (e.g. with emacs) and paste the result into the text file. The command you get looks very long so I am highlighting in <span style="color:blue">blue</span> the bit about the scene and in <span style="color:red">red</span> the bit about the overlay.
----
......@@ -68,7 +68,7 @@ If you run it all in a Terminal it will open FSLeyes and set it up to look like
Now as I said the command line is long and contains many things that are default behaviour anyway. So let's strip it down a little bit as it will make this document shorter. In the below, I am only keeping a subset of the options, but I did not add new ones:
```bash
fsleyes --scene ortho --hideLabels --layout horizontal --hidex --hidey --hideCursor --bgColour 1.0 1.0 1.0 --fgColour 0.0 0.0 0.0 /usr/local/fsl/data/standard/MNI152_T1_1mm.nii.gz --overlayType volume --cmap cool --displayRange 1000.0 8000.0 --cmapResolution 7 --interpolation spline
fsleyes --scene ortho --hideLabels --layout horizontal --hidex --hidey --hideCursor --bgColour 1.0 1.0 1.0 --fgColour 0.0 0.0 0.0 /usr/local/fsl/data/standard/MNI152_T1_1mm.nii.gz --overlayType volume --cmap cool --displayRange 1000.0 8000.0 --cmapResolution 7 --interpolation spline
```
......@@ -79,7 +79,7 @@ fsleyes --scene ortho --hideLabels --layout horizontal --hidex --hidey --hideCur
Instead of opening FSLeyes, we want to create an image (a PNG for example) to use in a presentation. This can be done very simply by using the above command and adding a render flag to fsleyes:
```
fsleyes render -outputfile my_image.png <rest of the command>
fsleyes render -of my_image.png <rest of the command>
```
Run the above code (make sure you replace `<rest of the command>` with the rest of the FSLeyes command we created earlier). This should now output a PNG file. Have a look at it!
......@@ -98,7 +98,7 @@ First, make sure that you have ImageMagick installed. To do that, go to your ter
We will re-use the previous render command. Start by copying that into a text editor. You can use the code below:
```bash
fsleyes render --outfile my_image.png --scene ortho --hideLabels --layout horizontal --hidex --hidey --hideCursor --bgColour 1.0 1.0 1.0 --fgColour 0.0 0.0 0.0 /usr/local/fsl/data/standard/MNI152_T1_1mm.nii.gz --overlayType volume --cmap cool --displayRange 1000.0 8000.0 --cmapResolution 7 --interpolation spline
fsleyes render --outfile my_image.png --scene ortho --hideLabels --layout horizontal --hidex --hidey --hideCursor --bgColour 1.0 1.0 1.0 --fgColour 0.0 0.0 0.0 /usr/local/fsl/data/standard/MNI152_T1_1mm.nii.gz --overlayType volume --cmap cool --displayRange 1000.0 8000.0 --cmapResolution 7 --interpolation spline
```
Now to change the z-location, we will use the `-voxelLoc` flag. We will also use a FOR loop to change the location, and store a different PNG at each step. Here is what the code will look like (copy it onto your text editor):
......@@ -111,7 +111,7 @@ mkdir -p my_folder
for ((z=0;z<=181;z+=4));do
zzz=`$FSLDIR/bin/zeropad $z 3`
echo "Slice $zzz"
out=my_folder/image_${zzz}.png
fsleyes render --outfile $out --voxelLoc 91 109 $z $rest_of_command
done
......@@ -119,7 +119,7 @@ done
Examine the above script line by line. We first create a variable called `rest_of_command` containing all the extra stuff for display that does not change in the FOR loop.
We then use a FOR loop, where we go through every fourth z-slice (can you see that?). We create a variable called `zzz` inside the loop. This is to use for naming the output PNG files so that they are listed in the same order as the z-slices (e.g. instead of image_1.png we have image_001.png).
We then use a FOR loop, where we go through every fourth z-slice (can you see that?). We create a variable called `zzz` inside the loop. This is to use for naming the output PNG files so that they are listed in the same order as the z-slices (e.g. instead of image_1.png we have image_001.png).
Run this script and you should see that many PNGs will get created, one per slice.
......@@ -129,7 +129,7 @@ Now to combine all these PNGs into a single GIF, run the below ImageMagick comma
convert -delay 5 my_folder/image_???.png -loop 0 my_movie.gif
```
Have a look at the GIF. On a mac you can simply use the space bar on your keyboard to preview the GIF.
Have a look at the GIF. On a mac you can simply use the space bar on your keyboard to preview the GIF.
It is still missing the changing text. We will also use ImageMagick for this. Below is the same script as before but with the addition of a call to `convert` that adds the text:
......@@ -141,10 +141,10 @@ mkdir -p my_folder
for ((z=0;z<=181;z+=4));do
zzz=`$FSLDIR/bin/zeropad $z 3`
echo "Slice $zzz"
out=my_folder/image_${zzz}.png
fsleyes render --outfile $out --voxelLoc 91 109 $z $rest_of_command
# Bit that adds annotation to the image
out_annot=my_folder/image_annot_${zzz}.png
convert $out \
......@@ -200,7 +200,7 @@ Ok I know that looks a little scary. Let's make the following changes. Open the
- Change the colourmap to Yellow
- Change the display range to min=1000, max=5000
- Turn the brain upside down
- Turn the brain upside down
- Add two clipping planes
Copy the command line that creates this scene as we have done before (Settings->3D View 1->Show command line for scence).
......@@ -215,7 +215,7 @@ for ((angle1=0,angle2=180;angle1<=16;angle1++,angle2--));do
echo $angle1 $angle2
fsleyes render --outfile $outputfolder/grot_`zeropad $angle1 3`.png [INSERT THE COMMAND HERE AND PUT IN $angle1 AND $angle2 WHERE YOU THINK THEY SHOULD GO ]
done
```
......@@ -236,7 +236,7 @@ You should be able to see this GIF:
By now hopefully you have seen how you can combine the power of intuitively interacting with the FSLeyes graphical interface and the power of bash scripting. We'll do one more example of creating a nice graphic and then rendering it in a bash script. This one will look very nice.
Run the code below. It will open FSLeyes, with a 2mm MNI brain, and will also load the XTRACT tracts atlas.
Run the code below. It will open FSLeyes, with a 2mm MNI brain, and will also load the XTRACT tracts atlas.
```bash
fsleyes -std $FSLDIR/data/atlases/XTRACT/xtract-tract-atlases-prob-1mm &
......@@ -258,7 +258,7 @@ Do the following:
[This](data/tracts.txt) is what I got after doing the above (feel free to use that).
Now use FSleyes render to create a nice looking figure. We will also control the DPI (digits per inch) of this figure, as some journal publishers insist that you have good quality images.
Now use FSleyes render to create a nice looking figure. We will also control the DPI (digits per inch) of this figure, as some journal publishers insist that you have good quality images.
```
fsleyes render --outfile my_tract.png --size 800 600 <COPY REST OF THE COMMAND HERE>
......@@ -271,12 +271,12 @@ If you want to compare what you produced to what I made, have a look at [this](d
----
Ok one last thing. Let's imagine that you want to check that your registration has worked properly and also wanted to make a PNG to show others that it does.
Ok one last thing. Let's imagine that you want to check that your registration has worked properly and also wanted to make a PNG to show others that it does.
For this, we will need two images that have been aligned with each other. These two are included with the practical material:
```
fsleyes data/example_func2highres.nii.gz data/highres.nii.gz &
fsleyes data/example_func2highres.nii.gz data/highres.nii.gz &
```
Then follow this recipe:
......
%% Cell type:markdown id:fa095385 tags:
# Matplotlib tutorial
The main plotting library in python is `matplotlib`.
It provides a simple interface to just explore the data,
while also having a lot of flexibility to create publication-worthy plots.
In fact, the vast majority of python-produced plots in papers will be either produced
directly using matplotlib or by one of the many plotting libraries built on top of
matplotlib (such as [seaborn](https://seaborn.pydata.org/) or [nilearn](https://nilearn.github.io/)).
Like everything in python, there is a lot of help available online (just google it or ask your local pythonista).
A particularly useful resource for matplotlib is the [gallery](https://matplotlib.org/gallery/index.html).
Here you can find a wide range of plots.
Just find one that looks like what you want to do and click on it to see (and copy) the code used to generate the plot.
## Contents
- [Basic plotting commands](#basic-plotting-commands)
- [Line plots](#line)
- [Scatter plots](#scatter)
- [Histograms and bar plots](#histograms)
- [Adding error bars](#error)
- [Shading regions](#shade)
- [Displaying images](#image)
- [Adding lines, arrows, text](#annotations)
- [Using the object-oriented interface](#OO)
- [Multiple plots (i.e., subplots)](#subplots)
- [Adjusting plot layouts](#layout)
- [Advanced grid configurations (GridSpec)](#grid-spec)
- [Styling your plot](#styling)
- [Setting title and labels](#labels)
- [Editing the x- and y-axis](#axis)
- [FAQ](#faq)
- [Why am I getting two images?](#double-image)
- [I produced a plot in my python script, but it does not show up](#show)
- [Changing where the image appears: backends](#backends)
<a class="anchor" id="basic-plotting-commands"></a>
## Basic plotting commands
Let's start with the basic imports:
%% Cell type:code id:41578cdc tags:
``` python
import matplotlib.pyplot as plt
import numpy as np
```
%% Cell type:markdown id:1a9a5f55 tags:
<a class="anchor" id="line"></a>
### Line plots
A basic lineplot can be made just by calling `plt.plot`:
%% Cell type:code id:2531bb20 tags:
``` python
plt.plot([1, 2, 3], [1.3, 4.2, 3.1])
```
%% Cell type:markdown id:9ef51d5c tags:
To adjust how the line is plotted, check the documentation:
%% Cell type:code id:9a768ab3 tags:
``` python
plt.plot?
```
%% Cell type:markdown id:d2e6a4d1 tags:
As you can see there are a lot of options.
The ones you will probably use most often are:
- `linestyle`: how the line is plotted (set to '' to omit the line)
- `marker`: how the points are plotted (these are not plotted by default)
- `color`: what color to use (defaults to cycling through a set of 7 colors)
%% Cell type:code id:85ed5f73 tags:
``` python
theta = np.linspace(0, 2 * np.pi, 101)
plt.plot(np.sin(theta), np.cos(theta))
plt.plot([-0.3, 0.3], [0.3, 0.3], marker='o', linestyle='', markersize=20)
plt.plot(0, -0.1, marker='s', color='black')
x = np.linspace(-0.5, 0.5, 5)
plt.plot(x, x ** 2 - 0.5, linestyle='--', marker='+', color='red')
```
%% Cell type:markdown id:a359e01a tags:
Because these keywords are so common, you can actually set one or more of them by passing in a string as the third argument.
%% Cell type:code id:0e69e842 tags:
``` python
x = np.linspace(0, 1, 11)
plt.plot(x, x)
plt.plot(x, x ** 2, '--') # sets the linestyle to dashed
plt.plot(x, x ** 3, 's') # sets the marker to square (and turns off the line)
plt.plot(x, x ** 4, '^y:') # sets the marker to triangles (i.e., '^'), linestyle to dotted (i.e., ':'), and the color to yellow (i.e., 'y')
```
%% Cell type:markdown id:891a9f9e tags:
<a class="anchor" id="scatter"></a>
### Scatter plots
The main extra feature of `plt.scatter` over `plt.plot` is that you can vary the color and size of the points based on some other variable array:
%% Cell type:code id:8cb13b7e tags:
``` python
x = np.random.rand(30)
y = np.random.rand(30)
plt.scatter(x, y, x * 30, y)
plt.colorbar() # adds a colorbar
```
%% Cell type:markdown id:df311f5c tags:
The third argument is the variable determining the size, while the fourth argument is the variable setting the color.
<a class="anchor" id="histograms"></a>
### Histograms and bar plots
For a simple histogram you can do this:
%% Cell type:code id:f9bb4e76 tags:
``` python
r = np.random.rand(1000)
n,bins,_ = plt.hist((r-0.5)**2, bins=30)
```
%% Cell type:markdown id:141bf7e8 tags:
where it also returns the number of elements in each bin, as `n`, and
the bin centres, as `bins`.
> The `_` in the third part on the left
> hand side is a shorthand for just throwing away the corresponding part
> of the return structure.
There is also a call for doing bar plots:
%% Cell type:code id:951bd53e tags:
``` python
samp1 = r[0:10]
samp2 = r[10:20]
bwidth = 0.3
xcoord = np.arange(10)
plt.bar(xcoord-bwidth, samp1, width=bwidth, color='red', label='Sample 1')
plt.bar(xcoord, samp2, width=bwidth, color='blue', label='Sample 2')
plt.legend(loc='upper left')
```
%% Cell type:markdown id:2ae38282 tags:
> If you want more advanced distribution plots beyond a simple histogram, have a look at the seaborn [gallery](https://seaborn.pydata.org/examples/index.html) for (too?) many options.
<a class="anchor" id="error"></a>
### Adding error bars
If your data is not completely perfect and has for some obscure reason some uncertainty associated with it,
you can plot these using `plt.error`:
%% Cell type:code id:3f440fd0 tags:
``` python
x = np.arange(5)
y1 = [0.3, 0.5, 0.7, 0.1, 0.3]
yerr = [0.12, 0.28, 0.1, 0.25, 0.6]
xerr = 0.3
plt.errorbar(x, y1, yerr, xerr, marker='s', linestyle='')
```
%% Cell type:markdown id:1405cf82 tags:
<a class="anchor" id="shade"></a>
### Shading regions
An area below a plot can be shaded using `plt.fill`
%% Cell type:code id:c0f12a0d tags:
``` python
x = np.linspace(0, 2, 100)
plt.fill(x, np.sin(x * np.pi))
```
%% Cell type:markdown id:71d7bc82 tags:
This can be nicely combined with a polar projection, to create 2D orientation distribution functions:
%% Cell type:code id:e337ced8 tags:
``` python
plt.subplot(projection='polar')
theta = np.linspace(0, 2 * np.pi, 100)
plt.fill(theta, np.exp(-2 * np.cos(theta) ** 2))
```
%% Cell type:markdown id:12c4eee6 tags:
The area between two lines can be shaded using `fill_between`:
%% Cell type:code id:54c6b838 tags:
``` python
x = np.linspace(0, 10, 1000)
y = 5 * np.sin(5 * x) + x - 0.1 * x ** 2
yl = x - 0.1 * x ** 2 - 5
yu = yl + 10
plt.plot(x, y, 'r')
plt.fill_between(x, yl, yu)
```
%% Cell type:markdown id:3a1d3815 tags:
<a class="anchor" id="image"></a>
### Displaying images
The main command for displaying images is `plt.imshow` (use `plt.pcolor` for cases where you do not have a regular grid)
%% Cell type:code id:ed051029 tags:
``` python
import nibabel as nib
import os.path as op
nim = nib.load(op.expandvars('${FSLDIR}/data/standard/MNI152_T1_1mm.nii.gz'), mmap=False)
imdat = nim.get_data().astype(float)
imdat = nim.get_fdata()
imslc = imdat[:,:,70]
plt.imshow(imslc, cmap=plt.cm.gray)
plt.colorbar()
plt.axis('off')
```
%% Cell type:markdown id:156b0628 tags:
Note that matplotlib will use the **voxel data orientation**, and that
configuring the plot orientation is **your responsibility**. To rotate a
slice, simply transpose the data (`.T`). To invert the data along along an
axis, you don't need to modify the data - simply swap the axis limits around:
%% Cell type:code id:a65cf0d6 tags:
``` python
plt.imshow(imslc.T, cmap=plt.cm.gray)
plt.xlim(reversed(plt.xlim()))
plt.ylim(reversed(plt.ylim()))
plt.colorbar()
plt.axis('off')
```
%% Cell type:markdown id:7c8a01a8 tags:
> It is easier to produce informative brain images using nilearn or fsleyes
<a class="anchor" id="annotations"></a>
### Adding lines, arrows, and text
Adding horizontal/vertical lines, arrows, and text:
%% Cell type:code id:3f9f4fad tags:
``` python
plt.axhline(-1) # horizontal line
plt.axvline(1) # vertical line
plt.arrow(0.2, -0.2, 0.2, -0.8, length_includes_head=True, width=0.01)
plt.text(0.5, 0.5, 'middle of the plot', transform=plt.gca().transAxes, ha='center', va='center')
plt.annotate("line crossing", (1, -1), (0.8, -0.8), arrowprops={}) # adds both text and arrow; need to set the arrowprops keyword for the arrow to be plotted
```
%% Cell type:markdown id:d2fb44b4 tags:
By default the locations of the arrows and text will be in data coordinates (i.e., whatever is on the axes),
however you can change that. For example to find the middle of the plot in the last example we use
axes coordinates, which are always (0, 0) in the lower left and (1, 1) in the upper right.
See the matplotlib [transformations tutorial](https://matplotlib.org/stable/tutorials/advanced/transforms_tutorial.html)
for more detail.
<a class="anchor" id="OO"></a>
## Using the object-oriented interface
In the examples above we simply added multiple lines/points/bars/images
(collectively called [artists](https://matplotlib.org/stable/tutorials/intermediate/artists.html) in matplotlib) to a single plot.
To prettify this plots, we first need to know what all the features are called:
![anatomy of a plot](https://matplotlib.org/stable/_images/anatomy.png)
Using the terms in this plot let's see what our first command of `plt.plot([1, 2, 3], [1.3, 4.2, 3.1])`
actually does:
1. First it creates a figure and makes this the active figure. Being the active figure means that any subsequent commands will affect figure. You can find the active figure at any point by calling `plt.gcf()`.
2. Then it creates an Axes or Subplot in the figure and makes this the active axes. Any subsequent commands will reuse this active axes. You can find the active axes at any point by calling `plt.gca()`.
3. Finally it creates a Line2D artist containing the x-coordinates `[1, 2, 3]` and `[1.3, 4.2, 3.1]` ands adds this to the active axes.
4. At some later time, when actually creating the plot, matplotlib will also automatically determine for you a default range for the x-axis and y-axis and where the ticks should be.
This concept of an "active" figure and "active" axes can be very helpful with a single plot, it can quickly get very confusing when you have multiple sub-plots within a figure or even multiple figures.
In that case we want to be more explicit about what sub-plot we want to add the artist to.
We can do this by switching from the "procedural" interface used above to the "object-oriented" interface.
The commands are very similar, we just have to do a little more setup.
For example, the equivalent of `plt.plot([1, 2, 3], [1.3, 4.2, 3.1])` is:
%% Cell type:code id:43229971 tags:
``` python
fig = plt.figure()
ax = fig.add_subplot()
ax.plot([1, 2, 3], [1.3, 4.2, 3.1])
```
%% Cell type:markdown id:8d4bee33 tags:
Note that here we explicitly create the figure and add a single sub-plot to the figure.
We then call the `plot` function explicitly on this figure.
The "Axes" object has all of the same plotting command as we used above,
although the commands to adjust the properties of things like the title, x-axis, and y-axis are slighly different.
`plt.getp` gives a helpful summary of the properties of a matplotlib object (and what you might change):
%% Cell type:code id:2cc5123a tags:
``` python
plt.getp(ax)
```
%% Cell type:markdown id:37251f4a tags:
When going through this list carefully you might have spotted that the plotted line is stored in the `lines` property.
Let's have a look at this line in more detail
%% Cell type:code id:db290a0a tags:
``` python
plt.getp(ax.lines[0])
```
%% Cell type:markdown id:ae053e0c tags:
This shows us all the properties stored about this line,
including its coordinates in many different formats
(`data`, `path`, `xdata`, `ydata`, or `xydata`),
the line style and width (`linestyle`, `linewidth`), `color`, etc.
<a class="anchor" id="subplots"></a>
## Multiple plots (i.e., subplots)
As stated one of the strengths of the object-oriented interface is that it is easier to work with multiple plots.
While we could do this in the procedural interface:
%% Cell type:code id:8bd710d5 tags:
``` python
plt.subplot(221)
plt.title("Upper left")
plt.subplot(222)
plt.title("Upper right")
plt.subplot(223)
plt.title("Lower left")
plt.subplot(224)
plt.title("Lower right")
```
%% Cell type:markdown id:28b82718 tags:
For such a simple example, this works fine. But for longer examples you would find yourself constantly looking back through the
code to figure out which of the subplots this specific `plt.title` command is affecting.
The recommended way to this instead is:
%% Cell type:code id:89a20086 tags:
``` python
fig, axes = plt.subplots(nrows=2, ncols=2)
axes[0, 0].set_title("Upper left")
axes[0, 1].set_title("Upper right")
axes[1, 0].set_title("Lower left")
axes[1, 1].set_title("Lower right")
```
%% Cell type:markdown id:852c2d46 tags:
Here we use `plt.subplots`, which creates both a new figure for us and a grid of sub-plots.
The returned `axes` object is in this case a 2x2 array of `Axes` objects, to which we set the title using the normal numpy indexing.
> Seaborn is great for creating grids of closely related plots. Before you spent a lot of time implementing your own have a look if seaborn already has what you want on their [gallery](https://seaborn.pydata.org/examples/index.html)
<a class="anchor" id="layout"></a>
### Adjusting plot layout
The default layout of sub-plots often leads to overlap between the labels/titles of the various subplots (as above) or to excessive amounts of whitespace in between. We can often fix this by just adding `fig.tight_layout` (or `plt.tight_layout`) after making the plot:
%% Cell type:code id:5c14ec50 tags:
``` python
fig, axes = plt.subplots(nrows=2, ncols=2)
axes[0, 0].set_title("Upper left")
axes[0, 1].set_title("Upper right")
axes[1, 0].set_title("Lower left")
axes[1, 1].set_title("Lower right")
fig.tight_layout()
```
%% Cell type:markdown id:338c7239 tags:
Uncomment `fig.tight_layout` and see how it adjusts the spacings between the plots automatically to reduce the whitespace.
If you want more explicit control, you can use `fig.subplots_adjust` (or `plt.subplots_adjust` to do this for the active figure).
For example, we can remove any whitespace between the plots using:
%% Cell type:code id:5df7361f tags:
``` python
np.random.seed(1)
fig, axes = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=True)
for ax in axes.flat:
offset = np.random.rand(2) * 5
ax.scatter(np.random.randn(10) + offset[0], np.random.randn(10) + offset[1])
fig.suptitle("group of plots, sharing x- and y-axes")
fig.subplots_adjust(wspace=0, hspace=0, top=0.9)
```
%% Cell type:markdown id:ff58c930 tags:
<a class="anchor" id="grid-spec"></a>
### Advanced grid configurations (GridSpec)
You can create more advanced grid layouts using [GridSpec](https://matplotlib.org/stable/tutorials/intermediate/gridspec.html).
An example taken from that website is:
%% Cell type:code id:c1651d0c tags:
``` python
fig = plt.figure(constrained_layout=True)
gs = fig.add_gridspec(3, 3)
f3_ax1 = fig.add_subplot(gs[0, :])
f3_ax1.set_title('gs[0, :]')
f3_ax2 = fig.add_subplot(gs[1, :-1])
f3_ax2.set_title('gs[1, :-1]')
f3_ax3 = fig.add_subplot(gs[1:, -1])
f3_ax3.set_title('gs[1:, -1]')
f3_ax4 = fig.add_subplot(gs[-1, 0])
f3_ax4.set_title('gs[-1, 0]')
f3_ax5 = fig.add_subplot(gs[-1, -2])
f3_ax5.set_title('gs[-1, -2]')
```
%% Cell type:markdown id:5676c42d tags:
<a class="anchor" id="styling"></a>
## Styling your plot
<a class="anchor" id="labels"></a>
### Setting title and labels
You can edit a large number of plot properties by using the `Axes.set_*` interface.
We have already seen several examples of this above, but here is one more:
%% Cell type:code id:b6841514 tags:
``` python
fig, axes = plt.subplots()
axes.plot([1, 2, 3], [2.3, 4.1, 0.8])
axes.set_xlabel('xlabel')
axes.set_ylabel('ylabel')
axes.set_title('title')
```
%% Cell type:markdown id:c27500eb tags:
You can also set any of these properties by calling `Axes.set` directly:
%% Cell type:code id:4aa8461b tags:
``` python
fig, axes = plt.subplots()
axes.plot([1, 2, 3], [2.3, 4.1, 0.8])
axes.set(
xlabel='xlabel',
ylabel='ylabel',
title='title',
)
```
%% Cell type:markdown id:e69e0f4b tags:
> To match the matlab API and save some typing the equivalent commands in the procedural interface do not have the `set_` preset. So, they are `plt.xlabel`, `plt.ylabel`, `plt.title`. This is also true for many of the `set_` commands we will see below.
You can edit the font of the text when setting the label or after the fact using the object-oriented interface:
%% Cell type:code id:d9958b2e tags:
``` python
fig, axes = plt.subplots()
axes.plot([1, 2, 3], [2.3, 4.1, 0.8])
axes.set_xlabel("xlabel", color='red')
axes.set_ylabel("ylabel")
axes.get_yaxis().get_label().set_fontsize('larger')
```
%% Cell type:markdown id:111da8e1 tags:
<a class="anchor" id="axis"></a>
### Editing the x- and y-axis
We can change many of the properties of the x- and y-axis by using `set_` commands.
- The range shown on an axis can be set using `ax.set_xlim` (or `plt.xlim`)
- You can switch to a logarithmic (or other) axis using `ax.set_xscale('log')`
- The location of the ticks can be set using `ax.set_xticks` (or `plt.xticks`)
- The text shown for the ticks can be set using `ax.set_xticklabels` (or as a second argument to `plt.xticks`)
- The style of the ticks can be adjusted by looping through the ticks (obtained through `ax.get_xticks` or calling `plt.xticks` without arguments).
For example:
%% Cell type:code id:4e402140 tags:
``` python
fig, axes = plt.subplots()
axes.errorbar([0, 1, 2], [0.8, 0.4, -0.2], 0.1, linestyle='-', marker='s')
axes.set_xticks((0, 1, 2))
axes.set_xticklabels(('start', 'middle', 'end'))
for tick in axes.get_xticklabels():
tick.set(
rotation=45,
size='larger'
)
axes.set_xlabel("Progression through practical")
axes.set_yticks((0, 0.5, 1))
axes.set_yticklabels(('0', '50%', '100%'))
fig.tight_layout()
```
%% Cell type:markdown id:9bd34f1c tags:
As illustrated earlier, we can get a more complete list of the things we could change about the x-axis by looking at its properties:
%% Cell type:code id:db2b0e6e tags:
``` python
plt.getp(axes.get_xaxis())
```
%% Cell type:markdown id:48b79b04 tags:
<a class="anchor" id="faq"></a>
## FAQ
<a class="anchor" id="double-image"></a>
### Why am I getting two images?
Any figure you produce in the notebook will be shown by default once a cell successfully finishes (i.e., without error).
If the code in a notebook cell crashes after creating the figure, this figure will still be in memory.
It will be shown after another cell successfully finishes.
You can remove this additional plot simply by rerunning the cell, after which you should only see the plot produced by the cell in question.
<a class="anchor" id="show"></a>
### I produced a plot in my python script, but it does not show up?
Add `plt.show()` to the end of your script (or save the figure to a file using `plt.savefig` or `fig.savefig`).
`plt.show` will show the image to you and will block the script to allow you to take in and adjust the figure before saving or discarding it.
<a class="anchor" id="backends"></a>
### Changing where the image appears: backends
Matplotlib works across a wide range of environments: Linux, Mac OS, Windows, in the browser, and more.
The exact detail of how to show you your plot will be different across all of these environments.
This procedure used to translate your `Figure`/`Axes` objects into an actual visualisation is called the backend.
In this notebook we were using the `inline` backend, which is the default when running in a notebook.
While very robust, this backend has the disadvantage that it only produces static plots.
We could have had interactive plots if only we had changed backends to `nbagg`.
You can change backends in the IPython terminal/notebook using:
%% Cell type:code id:e36ee821 tags:
``` python
%matplotlib nbagg
```
%% Cell type:markdown id:68b0aac8 tags:
> If you are using Jupyterlab (new version of the jupyter notebook) the `nbagg` backend will not work. Instead you will have to install `ipympl` and then use the `widgets` backend to get an interactive backend (this also works in the old notebooks).
In python scripts, this will give you a syntax error and you should instead use:
%% Cell type:code id:b81eb924 tags:
``` python
import matplotlib
matplotlib.use("MacOSX")
```
%% Cell type:markdown id:14663014 tags:
Usually, the default backend will be fine, so you will not have to set it.
Note that setting it explicitly will make your script less portable.
......
# Matplotlib tutorial
The main plotting library in python is `matplotlib`.
It provides a simple interface to just explore the data,
The main plotting library in python is `matplotlib`.
It provides a simple interface to just explore the data,
while also having a lot of flexibility to create publication-worthy plots.
In fact, the vast majority of python-produced plots in papers will be either produced
directly using matplotlib or by one of the many plotting libraries built on top of
......@@ -9,7 +9,7 @@ matplotlib (such as [seaborn](https://seaborn.pydata.org/) or [nilearn](https://
Like everything in python, there is a lot of help available online (just google it or ask your local pythonista).
A particularly useful resource for matplotlib is the [gallery](https://matplotlib.org/gallery/index.html).
Here you can find a wide range of plots.
Here you can find a wide range of plots.
Just find one that looks like what you want to do and click on it to see (and copy) the code used to generate the plot.
## Contents
......@@ -113,7 +113,7 @@ plt.legend(loc='upper left')
<a class="anchor" id="error"></a>
### Adding error bars
If your data is not completely perfect and has for some obscure reason some uncertainty associated with it,
If your data is not completely perfect and has for some obscure reason some uncertainty associated with it,
you can plot these using `plt.error`:
```
x = np.arange(5)
......@@ -155,7 +155,7 @@ The main command for displaying images is `plt.imshow` (use `plt.pcolor` for cas
import nibabel as nib
import os.path as op
nim = nib.load(op.expandvars('${FSLDIR}/data/standard/MNI152_T1_1mm.nii.gz'), mmap=False)
imdat = nim.get_data().astype(float)
imdat = nim.get_fdata()
imslc = imdat[:,:,70]
plt.imshow(imslc, cmap=plt.cm.gray)
plt.colorbar()
......@@ -195,7 +195,7 @@ for more detail.
<a class="anchor" id="OO"></a>
## Using the object-oriented interface
In the examples above we simply added multiple lines/points/bars/images
In the examples above we simply added multiple lines/points/bars/images
(collectively called [artists](https://matplotlib.org/stable/tutorials/intermediate/artists.html) in matplotlib) to a single plot.
To prettify this plots, we first need to know what all the features are called:
......@@ -234,13 +234,13 @@ Let's have a look at this line in more detail
plt.getp(ax.lines[0])
```
This shows us all the properties stored about this line,
including its coordinates in many different formats
including its coordinates in many different formats
(`data`, `path`, `xdata`, `ydata`, or `xydata`),
the line style and width (`linestyle`, `linewidth`), `color`, etc.
<a class="anchor" id="subplots"></a>
## Multiple plots (i.e., subplots)
As stated one of the strengths of the object-oriented interface is that it is easier to work with multiple plots.
As stated one of the strengths of the object-oriented interface is that it is easier to work with multiple plots.
While we could do this in the procedural interface:
```
plt.subplot(221)
......@@ -263,7 +263,7 @@ axes[0, 1].set_title("Upper right")
axes[1, 0].set_title("Lower left")
axes[1, 1].set_title("Lower right")
```
Here we use `plt.subplots`, which creates both a new figure for us and a grid of sub-plots.
Here we use `plt.subplots`, which creates both a new figure for us and a grid of sub-plots.
The returned `axes` object is in this case a 2x2 array of `Axes` objects, to which we set the title using the normal numpy indexing.
> Seaborn is great for creating grids of closely related plots. Before you spent a lot of time implementing your own have a look if seaborn already has what you want on their [gallery](https://seaborn.pydata.org/examples/index.html)
......@@ -394,7 +394,7 @@ Add `plt.show()` to the end of your script (or save the figure to a file using `
<a class="anchor" id="backends"></a>
### Changing where the image appears: backends
Matplotlib works across a wide range of environments: Linux, Mac OS, Windows, in the browser, and more.
Matplotlib works across a wide range of environments: Linux, Mac OS, Windows, in the browser, and more.
The exact detail of how to show you your plot will be different across all of these environments.
This procedure used to translate your `Figure`/`Axes` objects into an actual visualisation is called the backend.
......@@ -412,5 +412,5 @@ In python scripts, this will give you a syntax error and you should instead use:
import matplotlib
matplotlib.use("MacOSX")
```
Usually, the default backend will be fine, so you will not have to set it.
Note that setting it explicitly will make your script less portable.
\ No newline at end of file
Usually, the default backend will be fine, so you will not have to set it.
Note that setting it explicitly will make your script less portable.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment