Back Matter
Capture the book back matter inside a <book-back>
element.
The book-back element must contain a single <book-part>
element
with a @book-part-type
attribute with the value back
.
The <book-part>
element may contain the same content as a chapter
<book-part>
see Chapters for further
details.
Example
<book-back> <book-part book-part-type="back" id="bk978-0-7503-3071-8ch11"> <book-part-meta> <book-part-id book-part-id-type="doi">10.1088/978-0-7503-3071-8ch11</book-part-id> <title-group> <title>Back matter</title> </title-group> <contrib-group> <contrib contrib-type="author" xlink:type="simple"> <name name-style="western"> <surname>Windows-Yule</surname> <given-names>Kit</given-names> </name> <xref ref-type="aff" rid="bk978-0-7503-3071-8ch11aff1"/> </contrib> <contrib contrib-type="author" xlink:type="simple"> <name name-style="western"> <surname>Nicuşan</surname> <given-names>Leonard</given-names> </name> <xref ref-type="aff" rid="bk978-0-7503-3071-8ch11aff2"/> </contrib> <contrib contrib-type="author" xlink:type="simple"> <name name-style="western"> <surname>Herald</surname> <given-names>Matthew T</given-names> </name> <xref ref-type="aff" rid="bk978-0-7503-3071-8ch11aff3"/> </contrib> <contrib contrib-type="author" xlink:type="simple"> <name name-style="western"> <surname>Manger</surname> <given-names>Samuel</given-names> </name> <xref ref-type="aff" rid="bk978-0-7503-3071-8ch11aff4"/> </contrib> <contrib contrib-type="author" xlink:type="simple"> <name name-style="western"> <surname>Parker</surname> <given-names>David</given-names> </name> <xref ref-type="aff" rid="bk978-0-7503-3071-8ch11aff5"/> </contrib> <aff id="bk978-0-7503-3071-8ch11aff1">School of Chemical Engineering, <institution xlink:type="simple">University of Birmingham</institution>, Edgbaston, Birmingham, <country>UK</country> </aff> <aff id="bk978-0-7503-3071-8ch11aff2">School of Chemical Engineering, <institution xlink:type="simple">University of Birmingham</institution>, Edgbaston, Birmingham, <country>UK</country> </aff> <aff id="bk978-0-7503-3071-8ch11aff3">School of Chemical Engineering, <institution xlink:type="simple">University of Birmingham</institution>, Edgbaston, Birmingham, <country>UK</country> </aff> <aff id="bk978-0-7503-3071-8ch11aff4">School of Chemical Engineering, <institution xlink:type="simple">University of Birmingham</institution>, Edgbaston, Birmingham, <country>UK</country> </aff> <aff id="bk978-0-7503-3071-8ch11aff5">Positron Imaging Centre, School of Physics and Astronomy, <institution xlink:type="simple">University of Birmingham</institution>, Edgbaston, Birmingham, <country>UK</country> </aff> </contrib-group> <fpage>11-1</fpage> <lpage>11-25</lpage> <self-uri content-type="pdf" xlink:href="bk978-0-7503-3071-8ch11.pdf"/> <self-uri content-type="epub" xlink:href="bk978-0-7503-3071-8ch11.epub"/> </book-part-meta> <back> <app-group> <app id="bk978-0-7503-3071-8AppA"> <label>Appendix A</label> <title>Python tutorial</title> <sec id="bk978-0-7503-3071-8AppAs1"> <title>Python tutorial</title> <sec id="bk978-0-7503-3071-8AppAs2"> <title>For engineers, scientists and other people who just want a computer to do the work.</title> <p>The aim of this tutorial is to get you up to speed with Python programming. Besides the basics, it should teach you how to use it effectively for usual programming tasks, like creating dynamic arrays, iterating them, saving them as CSV, etc.</p> <p>It assumes a very basic understanding of programming languages—e.g. knowing what a variables and conditionals are. If you’ve written more than 100 lines of code in, say, MATLAB, you should be fine.</p> </sec> </sec> <sec id="bk978-0-7503-3071-8AppAsA-1"> <label>A.1</label> <title>The Python programming language</title> <p>Python is a general-purpose, interpreted programming language that aims to be as simple and clear as possible. Its main design philosophy dictates: <italic toggle="yes">></italic>There should be one—and preferably only one—obvious way to do it.</p> <p>Indeed, the Python community and developers put a high premium on simplicity, sometimes even at the expense of computational efficiency. However, for this exact reason Python code is perhaps the easiest to read and understand, even for people without any specific knowledge of the language. This ease of expressing ideas as succintly as possible is also responsible for Python’s adoption as the <italic toggle="yes">de facto</italic> programming language for data science. There is a very active—and welcoming!—community developing an ecosystem of <italic toggle="yes">libraries</italic> that can help with almost any task at hand.</p> <p>Even in the field of scientific computing, which traditionally prefers more explicit lower-level languages such as Fortran and C++, almost all numerical code developed nowadays also includes at least a Python interface. As such, Python also found use as a powerful <italic toggle="yes">orchestrating</italic> language, gluing together external high-performance subroutines into cohesive scripts that are easy to develop, understand and extend. For example, the ubiquitous library for matrices and <italic toggle="yes">N</italic>-dimensional arrays, NumPy (short for <bold>Num</bold>erical <bold>Py</bold>thon), contains subroutines written in heavily-optimised, low-level C code, making array computing in a dynamic language like Python oftentimes faster than straightforward implementations in much more complex languages.</p> <p>Because it is an interpreted programming language, Python code can exist and run in a variety of environments:<list id="bk978-0-7503-3071-8AppAl1" list-type="bullet"> <list-item id="bk978-0-7503-3071-8AppAl1.1"> <label>•</label> <p>As a script.</p> </list-item> <list-item id="bk978-0-7503-3071-8AppAl1.2"> <label>•</label> <p>As functions or classes grouped into packages.</p> </list-item> <list-item id="bk978-0-7503-3071-8AppAl1.3"> <label>•</label> <p>As a command-line (or REPL—Read-Eval-Print-Loop).</p> </list-item> <list-item id="bk978-0-7503-3071-8AppAl1.4"> <label>•</label> <p>As mixed text and code cells in a Jupyter Notebook (such as this one!).</p> </list-item> </list> </p> <sec id="bk978-0-7503-3071-8AppAsA-1-1"> <label>A.1.1</label> <title>Some terminology</title> <p>Perhaps it would be useful to clear up some of the terms that are usually passed around when talking about programming in Python, and programming languages in general:<list id="bk978-0-7503-3071-8AppAl2" list-type="bullet"> <list-item id="bk978-0-7503-3071-8AppAl2.1"> <label>•</label> <p> <bold>Python</bold> is the programming <italic toggle="yes">language</italic> itself, containing a set of syntactic rules for expressing code (much like the grammar of a human language!)</p> </list-item> <list-item id="bk978-0-7503-3071-8AppAl2.2"> <label>•</label> <p> <bold>CPython</bold> is the most popular Python <italic toggle="yes">interpreter</italic>, the actual program that takes code written following Python’s syntax rules, understands it, and executes whatever commands the program outlines. Indeed, you can have different interpreters for the same language!</p> </list-item> <list-item id="bk978-0-7503-3071-8AppAl2.3"> <label>•</label> <p> <bold>IDE</bold>, or <bold>I</bold>ntegrated <bold>D</bold>evelopment <bold>E</bold>nvironment is, like the name suggests, a programming environment; at the minimum, it includes a text editor with programming syntax highlighting and the ability to execute the code written in it. <italic toggle="yes">Spyder</italic> is an example IDE specially tailored to Python programming.</p> </list-item> <list-item id="bk978-0-7503-3071-8AppAl2.4"> <label>•</label> <p> <bold>Library</bold> in the context of programming, is a collection of pre-written code that often automates or simplifies a task. <italic toggle="yes">NumPy</italic> is such a library allowing simple expression of array operations without worrying about low-level computer details (e.g. adding two matrices without needing to think about requesting memory from the operating system, loading each value from the matrices, telling the computer processor to add them, then saving them in the requested memory—sounds pretty tedious put this way, doesn’t it?)</p> </list-item> <list-item id="bk978-0-7503-3071-8AppAl2.5"> <label>•</label> <p> <bold>Jupyter Notebook</bold>—essentially what you are seeing here!—is a programming environment containing <italic toggle="yes">text cells</italic> and interactive <italic toggle="yes">code cells</italic> that can be executed. If a <italic toggle="yes">Jupyter Notebook</italic> is the file itself (normally ending in <italic toggle="yes">“.ipynb”</italic>), <italic toggle="yes">JupyterLab</italic> is the environment that opens and displays notebooks such as this one.</p> </list-item> <list-item id="bk978-0-7503-3071-8AppAl2.6"> <label>•</label> <p> <bold>Anaconda</bold> is a ‘batteries included’ programming environment bringing together CPython, the most important libraries for data science, Spyder, an IDE for Python development and JupyterLab, which can host mixed text and interactive code cells. If you would like to code in Python, downloading Anaconda is the easiest way to get started!</p> </list-item> </list> </p> <p>By the way, <italic toggle="yes">Python</italic> comes from the famous British comedy troupe Monty Python, not the snake!</p> </sec> <sec id="bk978-0-7503-3071-8AppAsA-1-2"> <label>A.1.2</label> <title>The ‘pip’ package manager</title> <p>It is a very good idea to encapsulate code into packages, modules, and the like. Thankfully, python has a pretty smart package manager called ‘pip’ that takes care of installing and updating different packages.</p> <p>Let’s install the Numerical Python (NumPy) package. It provides classic MATLAB-like array functionality to Python, while being a bit more extensible. It is simply the most important library in Python for numerical computing.</p> <p>Run the next cell. You will see the output right under it.</p> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf5_pr.gif" xlink:type="simple"/> </disp-formula> </sec> <sec id="bk978-0-7503-3071-8AppAsA-1-3"> <label>A.1.3</label> <title>Using modules</title> <p>Once you have a module installed, you can import it inside your Python code (be it script, function, sandwich toaster, etc.):<disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf6_pr.gif" xlink:type="simple"/> </disp-formula> </p> </sec> </sec> <sec id="bk978-0-7503-3071-8AppAsA-2"> <label>A.2</label> <title>Basic constructs</title> <p>Now we’ll start actually programming in Python!</p> <sec id="bk978-0-7503-3071-8AppAsA-2-1"> <label>A.2.1</label> <title>Basic data structures</title> <p>The simplest (i.e. most usual) data types that you will use are:<list id="bk978-0-7503-3071-8AppAl3" list-type="bullet"> <list-item id="bk978-0-7503-3071-8AppAl3.1"> <label>•</label> <p> <monospace>int</monospace> : normal integers. Python integers have a variable size: they are as big as your memory permits!</p> </list-item> <list-item id="bk978-0-7503-3071-8AppAl3.2"> <label>•</label> <p> <monospace>float</monospace> : real numbers. For the C++ nerds: it is equivalent to a double-precision floating-point number.</p> </list-item> <list-item id="bk978-0-7503-3071-8AppAl3.3"> <label>•</label> <p> <monospace>str</monospace> : character strings.</p> </list-item> </list> </p> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf7_pr.gif" xlink:type="simple"/> </disp-formula> <sec id="bk978-0-7503-3071-8AppAsA-2-1-1"> <label>A.2.1.1</label> <title>Lists</title> <p>The <monospace>list</monospace> is a built-in data structure. Unlike a MATLAB array, it is a variable-length container. This makes it very handy when we are dynamically allocating/reading in data:</p> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf8_pr.gif" xlink:type="simple"/> </disp-formula> </sec> </sec> <sec id="bk978-0-7503-3071-8AppAs11"> <title>Conditionals and loops:</title> <p>In Python we have the usual <monospace>if</monospace> <monospace>/</monospace> <monospace>elif</monospace> <monospace>/</monospace> <monospace>else</monospace> statements and the <monospace>for</monospace> <monospace>/</monospace> <monospace>while</monospace> loops.</p> <p>Python is a whitespace-delimited language. This means that blocks of code are delimited by the indentation level, rather than curly braces or start/end statements.<disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf9_pr.gif" xlink:type="simple"/> </disp-formula>x is a float!<disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf10_pr.gif" xlink:type="simple"/> </disp-formula> </p> </sec> <sec id="bk978-0-7503-3071-8AppAsA-2-2"> <label>A.2.2</label> <title>Functions</title> <p>Functions in Python have a few extra features: - Positional arguments. - Keyword arguments. - Default values.</p> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf11_pr.gif" xlink:type="simple"/> </disp-formula> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf12_pr.gif" xlink:type="simple"/> </disp-formula> </sec> <sec id="bk978-0-7503-3071-8AppAsA-2-3"> <label>A.2.3</label> <title>Classes</title> <p>A <monospace>class</monospace> is a powerful way to bundle data (called ‘properties’ and/or ‘attributes’) and functions that operate on it (called ‘methods’):</p> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf13_pr.gif" xlink:type="simple"/> </disp-formula> <p>It’s important to note that there is nothing that classes can do that couldn’t also be achieved with functions! Don’t worry if you do not <italic toggle="yes">deeply</italic> grasp what a <monospace>class</monospace> is—it is a more advanced concept that only becomes useful once your project grows in size. The most important things to understand are: - A class is a container for properties and methods. - You ‘instantiate’ a class and it will remember the properties you set. - You access the properties and methods of instances of classes using a dot.</p> <p>So a class helps you ‘bundle’ together related properties and behaviours, which you don’t really need to do for smaller projects (say < 1000 lines of code). Using classes and objects that encapsulate data and functions is commonly called <italic toggle="yes">Object-Oriented Programming</italic>, or <italic toggle="yes">OOP</italic>. Teaching OOP would take an entire tutorial on its own, and its usefulness would only become apparent for large codebases.</p> <p>If you do, however, find yourself writing more than 1000 lines of code for a single project, then you’re probably ready to learn and understand OOP. In that case we recommend the following tutorial—it is quite comprehensive but doesn’t bore you with all the theoretical aspects of OOP: <ext-link ext-link-type="uri" xlink:href="https://realpython.com/python3-object-oriented-programming/" xlink:type="simple">https://realpython.com/python3-object-oriented-programming/</ext-link>.</p> </sec> </sec> <sec id="bk978-0-7503-3071-8AppAsA-3"> <label>A.3</label> <title>NumPy basics</title> <p>Now let’s extend our Python arsenal with some NumPy-fuelled mathematical power! The easiest way to explain it is by example:</p> <sec id="bk978-0-7503-3071-8AppAsA-3-1"> <label>A.3.1</label> <title>NumPy array creation and basic properties</title> <p>Starting from lists:</p> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf14_pr.gif" xlink:type="simple"/> </disp-formula> <p>Using NumPy functions:</p> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf15_pr.gif" xlink:type="simple"/> </disp-formula> </sec> <sec id="bk978-0-7503-3071-8AppAsA-3-2"> <label>A.3.2</label> <title>NumPy array indexing</title> <p>This part is very similar to MATLAB’s arrays.</p> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf16_pr.gif" xlink:type="simple"/> </disp-formula> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf17_pr.gif" xlink:type="simple"/> </disp-formula> </sec> <sec id="bk978-0-7503-3071-8AppAsA-3-3"> <label>A.3.3</label> <title>Operations on NumPy arrays</title> <p>Inspired by MATLAB, extended by NumPy (TM). Here are some basic operations:</p> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf18_pr.gif" xlink:type="simple"/> </disp-formula> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf19_pr.gif" xlink:type="simple"/> </disp-formula> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf20_pr.gif" xlink:type="simple"/> </disp-formula> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf21_pr.gif" xlink:type="simple"/> </disp-formula> </sec> </sec> <sec id="bk978-0-7503-3071-8AppAsA-4"> <label>A.4</label> <title>Matplotlib basics</title> <p>Now let’s create some simple plots using Matplotlib. The quickest way is using MATLAB-style ‘stateful’ plotting (stateful in the sense that Matplotlib saves a global figure for all plotting commands you run):</p> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf22_pr.gif" xlink:type="simple"/> </disp-formula> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf1_pr.gif" xlink:type="simple"/> </disp-formula> <p>A more programmatic way to plot data would be to use individual figures and axes in the object-oriented style:</p> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf23_pr.gif" xlink:type="simple"/> </disp-formula> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf2_pr.gif" xlink:type="simple"/> </disp-formula> <p>When using subplots, the object-oriented style becomes more useful:</p> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf24_pr.gif" xlink:type="simple"/> </disp-formula> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf3_pr.gif" xlink:type="simple"/> </disp-formula> </sec> <sec id="bk978-0-7503-3071-8AppAsA-5"> <label>A.5</label> <title>Plotly basics</title> <p>Plotly is JavaScript-based plotting engine that creates beautiful figures. They can be exported as interactive webpages and can be more efficient than Matplotlib in handling big datasets.</p> <p>Plotly is not a ‘core’ package like NumPy or Matplotlib. Let’s install it using <monospace>pip</monospace>:</p> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf25_pr.gif" xlink:type="simple"/> </disp-formula> <p>Plotly is a high-level, declarative charting library. This means that every setting of the plot can be accessed and set as a property of the instance.</p> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf26_pr.gif" xlink:type="simple"/> </disp-formula> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf4_pr.gif" xlink:type="simple"/> </disp-formula> </sec> <sec id="bk978-0-7503-3071-8AppAsA-6"> <label>A.6</label> <title>Extra</title> <p>This section is entirely optional. It shows some more advanced, but oftentimes useful features of Python. We recommend you skim through it, such that you are at least familiar with the concepts. If a need for them arises, you’ll know where to find them:</p> <sec id="bk978-0-7503-3071-8AppAsA-6-1"> <label>A.6.1</label> <title>Other data structures</title> <p>Some other data structures are: - <monospace>tuple</monospace> : an ordered, immutable collection. - <monospace>dict</monospace> : a dictionary, mapping keys to values. - <monospace>set</monospace> : a set, like the mathematical definition—i.e. no repeating values.</p> <p>Also, <monospace>None</monospace> is a special value that signifies an undefined or unset variable. It can be used as a default function parameter.</p> <p>Each of them defines some special operations:</p> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf27_pr.gif" xlink:type="simple"/> </disp-formula> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf28_pr.gif" xlink:type="simple"/> </disp-formula> </sec> <sec id="bk978-0-7503-3071-8AppAsA-6-2"> <label>A.6.2</label> <title>Documentation</title> <p>The documentation of a function/class/module can be accessed in a couple of ways:</p> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf29a_pr.gif" xlink:type="simple"/> </disp-formula> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf29b_pr.gif" xlink:type="simple"/> </disp-formula> <p>You can easily document your own code, and Python will automatically generate the documentation strings:</p> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf30_pr.gif" xlink:type="simple"/> </disp-formula> </sec> <sec id="bk978-0-7503-3071-8AppAsA-6-3"> <label>A.6.3</label> <title>Python virtual environments</title> <p>Once you start building larger applications, it is important to have a reliable programming environment. What happens if: - Your app only works on one version of the Python interpreter. - Your app only works with older versions of some modules.</p> <p>The set of Python and modules’ versions forms a ‘Virtual Environment’. You can ‘freeze’ the state of your Virtual Environment and know that what you are developing surely works on those versions.</p> <p>This is the main idea. There are quite a few ways of managing multiple virtual environments (but <monospace>conda</monospace> is probably the nicest <italic toggle="yes">virtualenv</italic> manager). I will let you read about them once the need for them arises.</p> </sec> </sec> <sec id="bk978-0-7503-3071-8AppAsA-7"> <label>A.7</label> <title>Common <italic toggle="yes">how-to</italic>s</title> <p>This is the final chapter of this tutorial, congratulations!</p> <p>We will show you the ‘easy’ (or ‘right’, following Python’s philosophy) way of doing a few usual tasks. This should save you a few trips to <italic toggle="yes">StackOverflow</italic> and NumPy’s documentation.</p> <sec id="bk978-0-7503-3071-8AppAsA-7-1"> <label>A.7.1</label> <title>Formatting strings</title> <p>We frequently need to output data in a pretty format. Thankfully, Python has some nice ways of printing stuff:</p> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf31_pr.gif" xlink:type="simple"/> </disp-formula> </sec> <sec id="bk978-0-7503-3071-8AppAsA-7-2"> <label>A.7.2</label> <title>Iteration + list comprehensions</title> <p>If you start using C++-style for/while loops, stop and think if there isn’t a more concise (i.e. better) way of doing what you want. Python has some nice features for different types of iteration:</p> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf32_pr.gif" xlink:type="simple"/> </disp-formula> <p>List comprehensions provide a concise and powerful way of generating or accessing iterable data.</p> <p>The main syntax is:</p> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf33_pr.gif" xlink:type="simple"/> </disp-formula> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf34_pr.gif" xlink:type="simple"/> </disp-formula> </sec> <sec id="bk978-0-7503-3071-8AppAsA-7-3"> <label>A.7.3</label> <title>Dynamic array creation</title> <p>We frequently need to create arrays dynamically, without knowing how much data there will be (e.g. reading lines from a file). Python <monospace>list</monospace>s have variable size, but are not great for maths, while NumPy <monospace>ndarray</monospace>s are very powerful but are not resizable. How can we achieve the best of both worlds? Combining them, of course:</p> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf35_pr.gif" xlink:type="simple"/> </disp-formula> </sec> <sec id="bk978-0-7503-3071-8AppAsA-7-4"> <label>A.7.4</label> <title>Saving and loading data</title> <p>We frequently need to load data from a file, do something to it, then save it somewhere else. In engineering, we most often handle big piles of numerical data that is usually saved as in a CSV (comma-separated-values) format. Again, Python has a few tricks up its sleeve.</p> <p>Reading or writing files line by line is laborious and oftentimes slow. NumPy has some optimised functions for this:</p> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf36_pr.gif" xlink:type="simple"/> </disp-formula> </sec> <sec id="bk978-0-7503-3071-8AppAsA-7-5"> <label>A.7.5</label> <title>Sorting</title> <p>Sorting data is terribly common. NumPy has one of the best sorting functions available, called TimSort (read about it, it’s great). For example, we often want to sort the rows of an array based on one of the columns (e.g. time from a simulation):</p> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf37a_pr.gif" xlink:type="simple"/> </disp-formula> <disp-formula> <graphic orientation="portrait" position="float" xlink:href="bk978-0-7503-3071-8app1uf37b_pr.gif" xlink:type="simple"/> </disp-formula> </sec> </sec> <sec id="bk978-0-7503-3071-8AppAsA-8"> <label>A.8</label> <title>The end</title> <p>If you reached this point, congratulations! You’re officially a Python programmer, ready to take on the beautiful, vast world of data science. We covered a lot of ground here, from introducing the <monospace>Python</monospace> interpreter, the <monospace>pip</monospace> package manager and basic programming constructs, all the way to using NumPy arrays and list comprehensions to slice and dice our data, be it nicely-formatted or not. Because that’s what data science is all about, right?</p> <p>Don’t forget that it’s perfectly fine to not remember everything in this tutorial by heart—all that matters is knowing where to get the information you need, using <monospace>help</monospace> or <italic toggle="yes">StackOverflow</italic> or your neighbour. From here on, the best way to learn is by practice: start coding a few projects you might be interested in (there are lots of examples on Google) and continue to develop your programming skills; the hardest, but most rewarding aspect isn’t knowing a programming language, but <bold>making the computer do the work</bold> using the right tools at your disposal—be they programming languages or libraries.</p> </sec> </app> <app id="bk978-0-7503-3071-8AppB"> <title>Glossary</title> <def-list list-content="abbreviations"> <def-item> <term>Activity:</term> <def> <p>The level of radioactivity possessed by a tracer. In the specific context of PEPT, this typically refers to the number of positrons and thus gamma photon <italic toggle="yes">pairs</italic>, as opposed to individual gamma photons, emitted per second</p> </def> </def-item> <def-item> <term>ASIC:</term> <def> <p>Application-specific integrated circuit—i.e. a chip that was specially designed for a given purpose, such as identifying single events in PET / PEPT systems.</p> </def> </def-item> <def-item> <term>Attenuation:</term> <def> <p>The decrease in the intensity of (i.e. number of photons in) a beam of electromagnetic radiation (e.g. x-rays or gamma-rays) as it passes through a given medium.</p> </def> </def-item> <def-item> <term>Becquerel (Bq):</term> <def> <p>The SI derived unit of measure characterising the number of nuclei undergoing radioactive decay in a quantity of radioactive material. For example, a PEPT tracer particle with an activity of 20 MBq undergoes 20 million radioactive disintegrations per second. Indeed, Becquerels represent frequency (being equivalent to s<sup>–1</sup> or Hz), but this special name was introduced to avoid potentially dangerous measurement mistakes.</p> </def> </def-item> <def-item> <term>Bidispersity:</term> <def> <p>A bidisperse or ‘binary’ granular system is one containing exactly two distinct ‘species’ of particle (see also Polydispersity).</p> </def> </def-item> <def-item> <term>Binary system:</term> <def> <p>See ‘Bidispersity’</p> </def> </def-item> <def-item> <term>Body force:</term> <def> <p>A force, such as that occurring due to a gravitational or electromagnetic field, that acts throughout the volume of a system (or a body, if you will).</p> </def> </def-item> <def-item> <term>Coincidence event:</term> <def> <p>The recording, by a gamma-camera system, of two photons within a time window short enough that said photons are likely to have been produced simultaneously, i.e. due to a positron annihilation.</p> </def> </def-item> <def-item> <term>Coincidence rate:</term> <def> <p>The number of coincidence events (see above) registered by a gamma camera per unit time.</p> </def> </def-item> <def-item> <term>Collimation:</term> <def> <p>The use of a collimator (see below) to ensure that a detector receives only photons travelling in or close to a specific direction (typically orthogonal to the detector face).</p> </def> </def-item> <def-item> <term>Collimator:</term> <def> <p>A device which filters a beam of x-rays or gamma rays such that all photon trajectories are approximately parallel, travelling in a single chosen direction (in the case of nuclear imaging, normally perpendicular to the face of a detector).</p> </def> </def-item> <def-item> <term>Cyclone:</term> <def> <p>A device which uses the centrifugal forces induced by cyclonic fluid flows for the separation of particulate solids from fluids.</p> </def> </def-item> <def-item> <term>Detector sensitivity:</term> <def> <p>The number of events detected per unit time per unit of activity (i.e. the fraction of photons emitted by a source that are successfully detected by a given camera).</p> </def> </def-item> <def-item> <term>Direct activation:</term> <def> <p>The introduction of radioactivity into a material via the direct conversion of stable nuclei in said material into radioactive nuclei.</p> </def> </def-item> <def-item> <term>Eddy (current):</term> <def> <p>In the context of fluid mechanics, an eddy current—often referred to simply as an eddy—is a current whose flow direction diverges from that of the general (i.e. ‘bulk’ or ‘net’) flow within a system.</p> </def> </def-item> <def-item> <term>Electrical Permittivity:</term> <def> <p>See ‘Permittivity’.</p> </def> </def-item> <def-item> <term>Ergodicity:</term> <def> <p>An ergodic system is one whose time-averaged statistical behaviour is equivalent to its ensemble-averaged behaviour. In our present context, it is more clear to explain it in terms of a system for which the average behaviour of a single particle over a suitably long time is representative of the behaviour of an entire system of identical particles.</p> </def> </def-item> <def-item> <term>False coincidence:</term> <def> <p>A coincidence event (see definition above) which does not correspond to a positron-electron annihilation pair, or otherwise does not yield a line of response passing through the point of annihilation of such a pair.</p> </def> </def-item> <def-item> <term>FoV:</term> <def> <p>Field of view, the experimental volume which is actively being imaged by a detector system.</p> </def> </def-item> <def-item> <term>FPGA:</term> <def> <p>Field-programmable gate array, a circuit that can be configured by end-users ‘in the field’.</p> </def> </def-item> <def-item> <term>FWHM:</term> <def> <p>Full-width at half-maximum, the width of a bell-like distribution at half the maximum probability.</p> </def> </def-item> <def-item> <term>Half-life:</term> <def> <p>The time taken for a sample of material to reduce to half of its initial quantity. In nuclear science, it represents the time needed for half of the radioactive nuclei in a sample to undergo radioactive decay.</p> </def> </def-item> <def-item> <term>Hysteretic:</term> <def> <p>A hysteretic material—granular media being prime examples—is one whose behaviour depends upon its history.</p> </def> </def-item> <def-item> <term>Indirect activation:</term> <def> <p>The application of radioactivity to a particle through the absorption (or otherwise application) of an already radioactively-labelled material.</p> </def> </def-item> <def-item> <term>Granular material:</term> <def> <p>A system composed of multiple, discrete, macroscopic solids.</p> </def> </def-item> <def-item> <term>Inverse square law:</term> <def> <p>The amount of x-ray or gamma radiation received from a point-like radiation source (e.g. a tracer particle) decreases as the square of the distance from the source to the receiving object.</p> </def> </def-item> <def-item> <term>Labelling:</term> <def> <p>Marking (either literally or figuratively) a particle or fluid such that its motion may be recorded. In the context of PEPT, this is usually achieved via the attachment of a radioactive substance (see also ‘Radiotracer’).</p> </def> </def-item> <def-item> <term>Line of Response (LoR):</term> <def> <p>The line, or vector, representing the path taken by a pair of back-to-back gamma photons emitted from a positron-electron annihilation event.</p> </def> </def-item> <def-item> <term>Mass absorption coefficient:</term> <def> <p>The rate at which a given form of radiation with a given initial energy is absorbed by a given material.</p> </def> </def-item> <def-item> <term>Monodispersity:</term> <def> <p>A monodisperse granular system is one which contains only one single ‘species’ of particle—i.e. a system of particles all of whose sizes, densities, geometries and other physical properties can be considered effectively identical.</p> </def> </def-item> <def-item> <term>Non-invasive:</term> <def> <p>In the context of an experimental measurement, a non-invasive reading is one which acquires data from a system without disrupting its usual behaviour. As an example, inserting a physical sensor into a flowstream is an example of an invasive measurement, while performing PEPT using a tracer that is physically identical to others in the system would be non-invasive.</p> </def> </def-item> <def-item> <term>Occlusion:</term> <def> <p>The ‘loss’ of one or more tracer particles during PEPT (or indeed other) imaging, for example when the count rate registered by a particle is too low for it to be successfully detected.</p> </def> </def-item> <def-item> <term>Permittivity:</term> <def> <p>The (electrical) permittivity of a material is a measure of how strongly said material is polarised by an applied electric field. It can, in essence, be thought of as a measure of how much energy can be stored by a material in an electric field.</p> </def> </def-item> <def-item> <term>Polydispersity:</term> <def> <p>Any granular system which is not monodisperse, i.e. which contains more than one distinct ‘species’ of particle, can be considered polydisperse. However, the term is typically only used for systems comprising more than two or three distinct species, the terms binary/bidisperse or ternary/tridisperse being more commonly used to describe systems with lower orders of polydispersity.</p> </def> </def-item> <def-item> <term>Positron camera:</term> <def> <p>A detector system designed to detect the interactions of gamma rays emitted from positron-electron annihilation events, and thus reconstruct their trajectories as required for PEPT (and indeed PET) imaging.</p> </def> </def-item> <def-item> <term>Powder characterisation:</term> <def> <p>The use of experimental measurements to extract information regarding the physical properties—typically <italic toggle="yes">bulk</italic> properties—of a powder or granulate. Examples of powder characterisation devices are provided in section <xref ref-type="sec" rid="bk978-0-7503-3071-8ch4s4-4">4.4</xref>.</p> </def> </def-item> <def-item> <term>Quasi-two-dimensional:</term> <def> <p>A system whose constituents themselves are three-dimensional (e.g. spheres as opposed to discs) but whose dimensions are constrained such that all, or at least a majority, of motion occurs in only two of three spatial dimensions.</p> </def> </def-item> <def-item> <term>Radioisotope:</term> <def> <p>A form (isotope) of a given element which undergoes radioactive decay.</p> </def> </def-item> <def-item> <term>Radiolabelling:</term> <def> <p>Attaching radioactivity to a particle or fluid such that it may be tracked using a suitable imaging technique. See also ‘labelling’.</p> </def> </def-item> <def-item> <term>Radiotracer:</term> <def> <p>A particle (or, in the case of PET, fluid) ‘labelled’ with a radioactive material such that its trajectory may be followed using a suitable imaging technique.</p> </def> </def-item> <def-item> <term>Secondary radiation:</term> <def> <p>Radiation emitted by a tracer or a system being imaged which does not arise from <inline-formula> <tex-math> <?CDATA ${\beta }^{+}$?> </tex-math> <mml:math overflow="scroll"> <mml:msup> <mml:mrow> <mml:mi>β</mml:mi> </mml:mrow> <mml:mrow> <mml:mo form="prefix">+</mml:mo> </mml:mrow> </mml:msup> </mml:math> <inline-graphic xlink:href="bk978-0-7503-3071-8AppBieqn1.gif" xlink:type="simple"/> </inline-formula> annihilation, and thus cannot be used for the location of tracers. Note that this term can also be defined more broadly—the specific definition provided here is applicable only to positron imaging techniques such as PEPT.</p> </def> </def-item> <def-item> <term>Segregation:</term> <def> <p>The spontaneous separation of the individual components of a dynamic binary or polydisperse system based on differences in particles’ sizes, shapes, densities and/or other physical properties.</p> </def> </def-item> <def-item> <term>Sensitivity:</term> <def> <p>See <italic toggle="yes">detector sensitivity</italic>.</p> </def> </def-item> <def-item> <term>Slurry:</term> <def> <p>A semi-solid mixture of particles immersed/suspended in a fluid.</p> </def> </def-item> <def-item> <term>Steady state:</term> <def> <p>A state in which a system’s behaviour does not vary significantly with time—for example a stream of fluid circulating at a constant rate and in a consistent pattern within a stirred container.</p> </def> </def-item> <def-item> <term>Ternary system:</term> <def> <p>A ternary granular system is one containing exactly three distinct ‘species’ of particle.</p> </def> </def-item> <def-item> <term>Tomography:</term> <def> <p>From the Greek <italic toggle="yes">tomos</italic> meaning slice or section; the process of reconstructing a full three-dimensional image of an object from a series of two-dimensional projections of said object.</p> </def> </def-item> <def-item> <term>Tracer:</term> <def> <p>See ‘Radiotracer’.</p> </def> </def-item> <def-item> <term>Tracer activity:</term> <def> <p>In the context of PEPT, the level of <inline-formula> <tex-math> <?CDATA ${\beta }^{+}$?> </tex-math> <mml:math overflow="scroll"> <mml:msup> <mml:mrow> <mml:mi>β</mml:mi> </mml:mrow> <mml:mrow> <mml:mo form="prefix">+</mml:mo> </mml:mrow> </mml:msup> </mml:math> <inline-graphic xlink:href="bk978-0-7503-3071-8AppBieqn2.gif" xlink:type="simple"/> </inline-formula> radioactivity possessed by a tracer particle—i.e. the number of positrons emitted per unit time.</p> </def> </def-item> <def-item> <term> <italic toggle="yes">Z</italic> number:</term> <def> <p>The number of protons in the nucleus of an atom.</p> </def> </def-item> </def-list> </app> </app-group> </back> </book-part> </book-back>