Skip to content

Commit 81d7e3c

Browse files
committed
format a bit
1 parent 30ddeda commit 81d7e3c

File tree

1 file changed

+88
-29
lines changed

1 file changed

+88
-29
lines changed

ch01python/00pythons.ipynb.py

Lines changed: 88 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
#
2424
# ### Why Python?
2525
#
26-
# * Python has a readable [syntax](https://en.wikipedia.org/wiki/Syntax_(programming_languages)) that makes it relatively quick to pick up.
26+
# * Python has a readable [syntax](https://en.wikipedia.org/wiki/Syntax_(programming_languages))
27+
# that makes it relatively quick to pick up.
2728
# * Python is popular in research, and has lots of libraries for science.
2829
# * Python interfaces well with faster languages.
2930
# * Python is free, so you'll never have a problem getting hold of it, wherever you go.
@@ -38,18 +39,30 @@
3839
#
3940
# Programs are a rigorous way of describing data analysis for other researchers, as well as for computers.
4041
#
41-
# Computational research suffers from people assuming each other's data manipulation is correct. By sharing _readable_, _reproducible_ and _well-tested_ code, which makes all of the data processing steps used in an analysis explicit and checks that each of those steps behaves as expected, we enable other researchers to understand and assesss the validity of those analysis steps for themselves. In a research code context the problem is generally not so much _garbage in, garbage out_, but _sensible input, reasonable output_: 'black-box' analysis pipelines that given sensible looking data inputs produce reasonable appearing but incorrect analyses as outputs.
42+
# Computational research suffers from people assuming each other's data manipulation is correct.
43+
# By sharing _readable_, _reproducible_ and _well-tested_ code, which makes all of the data processing
44+
# steps used in an analysis explicit and checks that each of those steps behaves as expected, we enable
45+
# other researchers to understand and assesss the validity of those analysis steps for themselves.
46+
# In a research code context the problem is generally not so much _garbage in, garbage out_, but _sensible
47+
# input, reasonable output_: 'black-box' analysis pipelines that given sensible looking data inputs produce
48+
# reasonable appearing but incorrect analyses as outputs.
4249
#
4350
# ## Many kinds of Python
4451
#
4552
# ### Python notebooks
4653
#
47-
# A particularly easy way to get started using Python, and one particularly suited to the sort of exploratory work common in a research context, is using [Jupyter](https://jupyter.org/https://jupyter.org/) notebooks.
54+
# A particularly easy way to get started using Python, and one particularly suited to the sort of
55+
# exploratory work common in a research context, is using [Jupyter](https://jupyter.org/https://jupyter.org/)
56+
# notebooks.
4857
#
49-
# In a notebook, you can easily mix code with discussion and commentary, and display the results outputted by code alongside the code itself, including graphs and other data visualisations.
58+
# In a notebook, you can easily mix code with discussion and commentary, and display the results
59+
# outputted by code alongside the code itself, including graphs and other data visualisations.
5060
#
51-
# For example if we wish to plot a figure-eight curve ([lemniscate](https://en.wikipedia.org/wiki/Lemniscate_of_Gerono)), we can include the parameteric equations
52-
# $x = \sin(2\theta) / 2, y = \cos(\theta), \theta \in [0, 2\pi)$ which mathematically define the curve as well as corresponding Python code to plot the curve and the output of that code all within the same notebook:
61+
# For example if we wish to plot a figure-eight curve
62+
# ([lemniscate](https://en.wikipedia.org/wiki/Lemniscate_of_Gerono)), we can include the parameteric
63+
# equations $x = \sin(2\theta) / 2, y = \cos(\theta), \theta \in [0, 2\pi)$ which mathematically define
64+
# the curve as well as corresponding Python code to plot the curve and the output of that code all within
65+
# the same notebook:
5366

5467
# %%
5568
# Plot lemniscate curve
@@ -63,33 +76,41 @@
6376
lines = ax.plot(x, y)
6477

6578
# %% [markdown]
66-
# We will be mainly mainly working with Jupyter notebooks in this course and will be using [Jupyter Lab](https://jupyterlab.readthedocs.io/) to view, edit and run the notebooks. To install Jupyter Lab, follow the setup instructions shown [on the course website](../index.html#what-you-need-for-the-course), or use the installation in [Desktop@UCL](https://my.desktop.ucl.ac.uk/).
6779
#
6880
# #### Notebook cells
6981
#
7082
# Jupyter notebooks consist of sequence of _cells_. Cells can be of two main types:
7183
#
72-
# * _Markdown cells_: Cells containing descriptive text and discussion with rich-text formatting via the [Markdown](https://en.wikipedia.org/wiki/Markdown) text markup language.
73-
# * _Code cells_: Cells containing Python code, which is displayed with syntax highlighting. The results returned by the computation performed when running the cell are displayed below the cell as the cell _output_, with Jupyter having a _rich display_ system allowing embedding a range of different outputs including for example static images, videos and interactive widgets.
84+
# * _Markdown cells_: Cells containing descriptive text and discussion with rich-text formatting
85+
# via the [Markdown](https://en.wikipedia.org/wiki/Markdown) text markup language.
86+
# * _Code cells_: Cells containing Python code, which is displayed with syntax highlighting.
87+
# The results returned by the computation performed when running the cell are displayed below the cell as the cell _output_, with Jupyter having a _rich display_ system allowing embedding a range of different outputs including for example static images, videos and interactive widgets.
7488
#
75-
# The document you are currently reading is a Jupyter notebook, and this text you are reading is a Markdown cell in the notebook. Below we see an example of a code cell.
89+
# The document you are currently reading is a Jupyter notebook, and this text you are reading is
90+
# Markdown cell in the notebook. Below we see an example of a code cell.
7691

7792
# %%
7893
print("This cell is a code cell")
7994

8095
# %% [markdown] jp-MarkdownHeadingCollapsed=true
81-
# Code cell inputs are numbered, with the cell output shown immediately below the input. Here the output is the text that we instruct the cell to print to the standard output stream. Cells will also display a representation of the value outputted by the last line in the cell, if any. For example
96+
# Code cell inputs are numbered, with the cell output shown immediately below the input. Here the output
97+
# is the text that we instruct the cell to print to the standard output stream. Cells will also display
98+
# a representation of the value outputted by the last line in the cell, if any. For example
8299

83100
# %%
84101
print("This text will be displayed\n")
85102
"This is text will also be displayed\n"
86103

87104
# %% [markdown]
88-
# There is a small difference in the formatting of the output here, with the `print` function displaying the text without quotation mark delimiters and with any _escaped_ special characters (such as the `"\n"` newline character here) processed.
105+
# There is a small difference in the formatting of the output here, with the `print` function displaying
106+
# the text without quotation mark delimiters and with any _escaped_ special characters (such as the
107+
# `"\n"` newline character here) processed.
89108
#
90109
# #### Markdown formatting
91110
#
92-
# The Markdown language used in Markdown cells provides a simple way to add basic text formatting to the rendered output while aiming to be retain the readability of the original Markdown source. For example to achieve the following rendered output text
111+
# The Markdown language used in Markdown cells provides a simple way to add basic text formatting to the
112+
# rendered output while aiming to be retain the readability of the original Markdown source. For example
113+
# to achieve the following rendered output text
93114
#
94115
# **bold**, *italic*, ~~striketrough~~, `monospace`
95116
#
@@ -111,11 +132,13 @@
111132
# [Link to search](https://duckduckgo.com/)
112133
# ```
113134
#
114-
# For more information [see this tutorial notebook in the official Jupyter documentation](https://nbviewer.org/github/jupyter/notebook/blob/master/docs/source/examples/Notebook/Working%20With%20Markdown%20Cells.ipynb).
135+
# For more information
136+
# [see this tutorial notebook in the official Jupyter documentation](https://nbviewer.org/github/jupyter/notebook/blob/master/docs/source/examples/Notebook/Working%20With%20Markdown%20Cells.ipynb).
115137
#
116138
# #### Editing and running cells in the notebook
117139
#
118-
# When working with the notebook, you can either be editing the content of a cell (termed _edit mode_), or outside the cells, navigating around the notebook (termed _command mode_).
140+
# When working with the notebook, you can either be editing the content of a cell (termed _edit mode_),
141+
# or outside the cells, navigating around the notebook (termed _command mode_).
119142
#
120143
# * When in _edit mode_ in a cell, press <kbd>esc</kbd> to leave it and change to _command mode_.
121144
# * When navigating between cells in _command mode_, press <kbd>enter</kbd> to change in to _edit mode_ in the selected cell.
@@ -135,22 +158,48 @@
135158
# * Press <kbd>tab</kbd> to suggest completions of variable names and object attribute. (Try it!)
136159
# * Press <kbd>shift</kbd>+<kbd>tab</kbd> when in the argument list of a function to display a pop-up showing documentation for the function.
137160
#
138-
# *Supplementary material*: Learn more about [Jupyter notebooks](https://jupyter-notebook.readthedocs.io/en/stable/notebook.html/).
161+
# *Supplementary material*: Learn more about
162+
# [Jupyter notebooks](https://jupyter-notebook.readthedocs.io/en/stable/notebook.html/).
139163
#
140164
# ### Python interpreters
141165
#
142-
# An alternative to running Python code via a notebook interface is to run commands in a Python _interpreter_ (also known as an _interactive shell_ or _read-eval-print-loop (REPL)_). This is similar in concept to interacting with your operating system via a command-line interface such as the `bash` or `zsh` shells in Linux and MacOS or `Command Prompt` in Windows. A Python interpreter provides a _prompt_ into which we can type Python code corresponding to commands we wish to execute; we then execute this code by hitting <kbd>enter</kbd> with any output from the computation being displayed before returning to the prompt again.
166+
# An alternative to running Python code via a notebook interface is to run commands in a
167+
# Python _interpreter_ (also known as an _interactive shell_ or _read-eval-print-loop (REPL)_).
168+
# This is similar in concept to interacting with your operating system via a command-line interface
169+
# such as the `bash` or `zsh` shells in Linux and MacOS or `Command Prompt` in Windows. A Python
170+
# interpreter provides a _prompt_ into which we can type Python code corresponding to commands we
171+
# wish to execute; we then execute this code by hitting <kbd>enter</kbd> with any output from the
172+
# computation being displayed before returning to the prompt again.
143173
#
144-
# We will not further explore using Python via an interpreter in this course but if you wish to learn more about such command-line interfaces we recommend you attend one of the [Software Carpentry](https://software-carpentry.org/lessons/https://software-carpentry.org/lessons/) workshops (sessions are regularly organised by [our group](http://rits.github-pages.ucl.ac.uk/software-carpentry/)), which covers this and other skills for computationally based research.
174+
# We will not further explore using Python via an interpreter in this course but if you wish to
175+
# learn more about such command-line interfaces we recommend you attend one of the
176+
# [Software Carpentry](https://software-carpentry.org/lessons/https://software-carpentry.org/lessons/)
177+
# workshops (sessions are regularly organised by [our group](http://rits.github-pages.ucl.ac.uk/software-carpentry/)),
178+
# which covers this and other skills for computationally based research.
145179

146180
# %% [markdown]
147181
# ### Python libraries
148182
#
149-
# A very common requirement in research (and all other!) programming is needing to reuse code in multiple different files. While it may seem that copying-and-pasting is an adequate solution to this problem, this should generally be avoided wherever possible and code which we wish to reuse _factored out_ in to _libraries_ which we we can _import_ in to other files to access the functionality of this code.
150-
#
151-
# Compared to copying and pasting code, writing and using libraries has the major advantage of meaning if we spot a bug in the code we only need to fix it once in the underlying library, and we straight away have the fixed code available everywhere the library is used rather than having to separately implement the fix in each file it is used. This similarly applies to for example adding new features to a piece of code. By creating libraries we also make it easier for other researchers to use our code.
152-
#
153-
# While it is simple to use libraries within a notebook (and we have already seen examples of this when we imported the Python libraries NumPy and Matplotlib in the figure-eight plot example above), it is non-trivial to use code from one notebook in another without copying-and-pasting. To create Python libraries we therefore generally write the code in to text files with a `.py` extension which in Python terminology are called _modules_ . The code can in these file can then be used in notebooks (or other modules) using the Python `import` statement. For example the cell below creates a file `draw_eight.py` in the same directory as this notebook containing Python code defining a _function_ (we will cover how to define and call functions later in the course) which creates a figure-eight plot and return the figure object.
183+
# A very common requirement in research (and all other!) programming is needing to reuse code in
184+
# multiple different files. While it may seem that copying-and-pasting is an adequate solution to this
185+
# problem, this should generally be avoided wherever possible and code which we wish to reuse
186+
# _factored out_ in to _libraries_ which we we can _import_ in to other files to access the functionality
187+
# of this code.
188+
#
189+
# Compared to copying and pasting code, writing and using libraries has the major advantage of meaning
190+
# if we spot a bug in the code we only need to fix it once in the underlying library, and we straight away
191+
# have the fixed code available everywhere the library is used rather than having to separately implement
192+
# the fix in each file it is used. This similarly applies to for example adding new features to a piece of
193+
# code. By creating libraries we also make it easier for other researchers to use our code.
194+
#
195+
# While it is simple to use libraries within a notebook (and we have already seen examples of this when we
196+
# imported the Python libraries NumPy and Matplotlib in the figure-eight plot example above), it is non-trivial
197+
# to use code from one notebook in another without copying-and-pasting. To create Python libraries we therefore
198+
# generally write the code in to text files with a `.py` extension which in Python terminology are called _modules_.
199+
# The code can in these file can then be used in notebooks (or other modules) using the Python `import` statement.
200+
# For example the cell below creates a file `draw_eight.py` in the same directory as this notebook containing Python
201+
# code defining a _function_ (we will cover how to define and call functions later in the course) which creates a
202+
# figure-eight plot and return the figure object.
154203

155204
# %%
156205
# %%writefile draw_eight.py
@@ -167,23 +216,33 @@ def make_figure():
167216

168217

169218
# %% [markdown]
170-
# We can use this code in the notebook by _importing_ the `draw_eight` module and then _calling_ the `make_figure` function defined in the module.
219+
# We can use this code in the notebook by _importing_ the `draw_eight` module and then _calling_ the
220+
# `make_figure` function defined in the module.
171221

172222
# %%
173223
import draw_eight # Load the library
174224
fig = draw_eight.make_figure()
175225

176226
# %% [markdown]
177-
# We will cover how to import and use functionality from libraries, how to install third-party libraries and how to write your own libraries that can be shared and used by other in this course.
227+
# We will cover how to import and use functionality from libraries, how to install third-party libraries
228+
# and how to write your own libraries that can be shared and used by other in this course.
178229
#
179230
# ### Python scripts
180231
#
181-
# While Jupyter notebooks are a great medium for learning how to use Python and for exploratory work, there are some drawbacks:
232+
# While Jupyter notebooks are a great medium for learning how to use Python and for exploratory work,
233+
# there are some drawbacks:
182234
#
183235
# * The require Jupyter Lab (or a similar application) to be installed to run the notebook.
184236
# * It can be difficult to run notebooks non-interactively, for example when scheduling a job on a cluster [such as those offered by UCL Research Computing](https://www.rc.ucl.ac.uk/docs/Background/Cluster_Computing).
185237
# * The flexibility of being able to run the code in cells in any order can also make it difficult to reason how outputs were produced and can lead to non-reproducible analyses.
186238
#
187-
# In some settings it can therefore be preferrable to write Python _scripts_ - that is files (typically with a `.py` extension) which contain Python code which completely describes a computational task to perform and that can be run by passing the name of the script file to the `python` program in a command-line environment. Optionally scripts may also allow passing in arguments from the command-line to control the execution of the script. As scripts are generally run from text-based terminals, non-text outputs such as images will generally be saved to files on disk.
188-
#
189-
# Python scripts are well suited to for example for describing computationally demanding simulations or analyses to run as long jobs on a remote server or cluster, or tasks where the input and output is mainly at the file level - for instance batch processing a series of data files. We will not cover how to write Python scripts in this course, however you can learn more about this topics in our [MPHY001: _Research software engineering with Python_ course](http://github-pages.ucl.ac.uk/rsd-engineeringcourse/).
239+
# In some settings it can therefore be preferrable to write Python _scripts_ - that is files (typically with
240+
# a `.py` extension) which contain Python code which completely describes a computational task to perform
241+
# and that can be run by passing the name of the script file to the `python` program in a command-line
242+
# environment. Optionally scripts may also allow passing in arguments from the command-line to control
243+
# the execution of the script. As scripts are generally run from text-based terminals, non-text outputs such
244+
# as images will generally be saved to files on disk.
245+
#
246+
# Python scripts are well suited to for example for describing computationally demanding simulations or analyses
247+
# to run as long jobs on a remote server or cluster, or tasks where the input and output is mainly at the file level
248+
# - for instance batch processing a series of data files.

0 commit comments

Comments
 (0)