| 1 |
Representing Brian objects |
| 2 |
============================= |
| 3 |
|
| 4 |
``__repr__`` and ``__str__`` |
| 5 |
---------------------------- |
| 6 |
|
| 7 |
Every class should specify or inherit useful ``__repr__`` and ``__str__`` methods. The ``__repr__`` |
| 8 |
method should give the "official" representation of the object; if possible, this should be a valid |
| 9 |
Python expression, ideally allowing for ``eval(repr(x)) == x``. The ``__str__`` method on the other |
| 10 |
hand, gives an "informal" representation of the object. This can be anything that is helpful but |
| 11 |
does not have to be Python code. For example: |
| 12 |
|
| 13 |
.. doctest:: |
| 14 |
|
| 15 |
>>> import numpy as np |
| 16 |
>>> ar = np.array([1, 2, 3]) * mV |
| 17 |
>>> print(ar) # uses __str__ |
| 18 |
[ 1. 2. 3.] mV |
| 19 |
>>> ar # uses __repr__ |
| 20 |
array([ 1., 2., 3.]) * mvolt |
| 21 |
|
| 22 |
If the representation returned by ``__repr__`` is not Python code, it should be enclosed in |
| 23 |
``<...>``, e.g. a `Synapses` representation might be ``<Synapses object with 64 synapses>``. |
| 24 |
|
| 25 |
If you don't want to make the distinction between ``__repr__`` and ``__str__``, simply define only |
| 26 |
a ``__repr__`` function, it will be used instead of ``__str__`` automatically (no need to write |
| 27 |
``__str__ = __repr__``). Finally, if you include the class name in the representation (which you |
| 28 |
should in most cases), use ``self.__class__.__name__`` instead of spelling out the name explicitly |
| 29 |
-- this way it will automatically work correctly for subclasses. It will also prevent you from |
| 30 |
forgetting to update the class name in the representation if you decide to rename the class. |
| 31 |
|
| 32 |
LaTeX representations with sympy |
| 33 |
-------------------------------- |
| 34 |
Brian objects dealing with mathematical expressions and equations often internally use sympy. |
| 35 |
Sympy's `~sympy.printing.latex.latex` function does a nice job of converting expressions into |
| 36 |
LaTeX code, using fractions, root symbols, etc. as well as converting greek variable names into |
| 37 |
corresponding symbols and handling sub- and superscripts. For the conversion of variable names |
| 38 |
to work, they should use an underscore for subscripts and two underscores for superscripts:: |
| 39 |
|
| 40 |
>>> from sympy import latex, Symbol |
| 41 |
>>> tau_1__e = Symbol('tau_1__e') |
| 42 |
>>> print(latex(tau_1__e)) |
| 43 |
\tau^{e}_{1} |
| 44 |
|
| 45 |
Sympy's printer supports formatting arbitrary objects, all they have to do is to implement a |
| 46 |
``_latex`` method (no trailing underscore). For most Brian objects, this is unnecessary as they will |
| 47 |
never be formatted with sympy's LaTeX printer. For some core objects, in particular the units, |
| 48 |
is is useful, however, as it can be reused in LaTeX representations for ipython (see below). |
| 49 |
Note that the ``_latex`` method should not return ``$`` or ``\begin{equation}`` (sympy's method |
| 50 |
includes a ``mode`` argument that wraps the output automatically). |
| 51 |
|
| 52 |
Representations for ipython |
| 53 |
--------------------------------- |
| 54 |
|
| 55 |
"Old" ipython console |
| 56 |
~~~~~~~~~~~~~~~~~~~~~ |
| 57 |
|
| 58 |
In particular for representations involing arrays or lists, it can be useful to break up the |
| 59 |
representation into chunks, or indent parts of the representation. This is supported by the |
| 60 |
ipython console's "pretty printer". To make this work for a class, add a |
| 61 |
``_repr_pretty_(self, p, cycle)`` (note the *single* underscores) method. You can find more |
| 62 |
information in the `ipython documentation <http://ipython.org/ipython-doc/dev/api/generated/IPython.lib.pretty.html#extending>`__ . |
| 63 |
|
| 64 |
"New" ipython console (qtconsole and notebook) |
| 65 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 66 |
|
| 67 |
The new ipython consoles, the qtconsole and the ipython notebook support a much richer set of |
| 68 |
representations for objects. As Brian deals a lot with mathematical objects, in particular the |
| 69 |
LaTeX and to a lesser extent the HTML formatting capabilities of the ipython notebook are |
| 70 |
interesting. To support LaTeX representation, implement a `_repr_latex_` method returning the |
| 71 |
LaTeX code (*including* ``$``, ``\begin{equation}`` or similar). If the object already has a |
| 72 |
``_latex`` method (see `LaTeX representations with sympy`_ above), this can be as simple as:: |
| 73 |
|
| 74 |
def _repr_latex_(self): |
| 75 |
return sympy.latex(self, mode='inline') # wraps the expression in $ .. $ |
| 76 |
|
| 77 |
The LaTeX rendering only supports a single mathematical block. For complex objects, e.g. |
| 78 |
`NeuronGroup` it might be useful to have a richer representation. This can be achieved by returning |
| 79 |
HTML code from ``_repr_html_`` -- this HTML code is processed by MathJax so it can include literal |
| 80 |
LaTeX code that will be transformed before it is rendered as HTML. An object containing two |
| 81 |
equations could therefore be represented with a method like this:: |
| 82 |
|
| 83 |
def _repr_html_(self): |
| 84 |
return ''' |
| 85 |
<h3> Equation 1 </h3> |
| 86 |
{eq_1} |
| 87 |
<h3> Equation 2 </h3> |
| 88 |
{eq_2}'''.format(eq_1=sympy.latex(self.eq_1, mode='equation'), |
| 89 |
eq_2=sympy.latex(self.eq_2, mode='equation')) |