Hide/Show the code
import numpy as np
= np.random.normal(0, 1, 10)
x x
array([-0.23788703, 0.634127 , -0.24433812, 0.25353522, 0.41089287,
0.89342388, 1.39188217, -0.7706414 , -0.39636496, 0.5776391 ])
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur posuere vestibulum facilisis. Aenean pretium orci augue, quis lobortis libero accumsan eu. Nam mollis lorem sit amet pellentesque ullamcorper. Curabitur lobortis libero eget malesuada vestibulum. Nam nec nibh massa. Pellentesque porttitor cursus tellus. Mauris urna erat, rhoncus sed faucibus sit amet, venenatis eu ipsum.
This document, accompanied by the customized GitHub repository, provides a template for writing contributions to Computo (Computo Team 2020). We show how Python
code can be included and how the repository can be set up for triggering GitHub actions for rendering the document, with dependencies handled by venv
and pip
.
You can start by clicking on the “use this template” button, on the top of the page of the github repository associated with this document. Of course, you can set your repository private during the preparation of your manuscript.
Quarto is a versatile formatting system for authoring documents integrating markdown, LaTeX and code blocks interpreted either via Jupyter or Knitr (thus supporting Python, R and Julia). It relies on the Pandoc document converter.
You need quarto installed on your system and the Computo extension to prepare your document. For the latter, once quarto is installed, run the following to install the extension in the current directory (it creates an _extension
directory which is ignored by git thanks to .gitignore
by default):
quarto add computorg/computo-quarto-extension
Quarto is expecting a .qmd
markdown file, but will also works with a standard Jupyter notebook file if you are used to it (it will just require to add the proper YAML metadata1).
Note: More advanced Jupyter-related functionality like Myst/Jupyter book are not supported in this Quarto setup. The markdown syntax inside the Jupyter notebook should follow the Quarto syntax (c.f. below). If you are more comfortable with using Myst/Jupyter book, we provide a specific template but it will requires more formatting work for Computo editorial team, thus highly encourage authors to use the Quarto templates.
This section covers basic formatting guidelines for quarto documents.
To render a document, run quarto render
. By default, both PDF and HTML documents are generated:
quarto render template-computo-python.qmd # renders both HTML and PDF
To check the syntax of the formatting below, you can use the </> source
button at the top right of this document.
Bold text or italic
But we can also do a numbered list
LaTeX code is natively supported2, which makes it possible to use mathematical formulae:
f(x_1, \dots, x_n; \mu, \sigma^2) = \frac{1}{\sigma \sqrt{2\pi}} \exp{\left(- \frac{1}{2\sigma^2}\sum_{i=1}^n(x_i - \mu)^2\right)}
It is also posible to cross-reference an equation, see Equation 1:
\begin{aligned} D_{x_N} & = \frac12 \left[\begin{array}{cc} x_L^\top & x_N^\top \end{array}\right] \, \left[\begin{array}{cc} L_L & B \\ B^\top & L_N \end{array}\right] \, \left[\begin{array}{c} x_L \\ x_N \end{array}\right] \\ & = \frac12 (x_L^\top L_L x_L + 2 x_N^\top B^\top x_L + x_N^\top L_N x_N), \end{aligned} \tag{1}
Quarto includes a nice support for theorems, with predefined prefix labels for theorems, lemmas, proposition, etc. see this page. Here is a simple example:
Theorem 1 (Strong law of large numbers) The sample average converges almost surely to the expected value:
\overline{X}_n\ \xrightarrow{\text{a.s.}}\ \mu \qquad\textrm{when}\ n \to \infty.
See Theorem 1.
Quarto uses either Jupyter or knitr to render code chunks. This can be triggered in the yaml header. In this tutorial, we use Jupyter
(Python
and Jupyter
must be installed on your computer).
---
title: "My Document"
author "Jane Doe"
jupyter: python3
---
python
code chunks may be embedded as follows:
import numpy as np
= np.random.normal(0, 1, 10)
x x
array([-0.23788703, 0.634127 , -0.24433812, 0.25353522, 0.41089287,
0.89342388, 1.39188217, -0.7706414 , -0.39636496, 0.5776391 ])
Plots can be generated as follows:
import matplotlib.pyplot as plt
import numpy as np
= np.linspace(0.1, 2 * np.pi, 41)
x = np.exp(np.sin(x))
y
plt.stem(x, y) plt.show()
It is also possible to create figures from static images:
Tables (with label: @tbl-mylabel
renders Table 1) can be generated with markdown as follows
Tables | Are | Cool |
---|---|---|
col 1 is | left-aligned | $1600 |
col 2 is | centered | $12 |
col 3 is | right-aligned | $1 |
References are displayed as footnotes using BibTeX, e.g. [@computo]
will be displayed as (Computo Team 2020), where computo
is the bibtex key for this specific entry. The bibliographic information is automatically retrieved from the .bib
file specified in the header of this document (here: references.bib
).
As already (partially) seen, Quarto includes a mechanism similar to the bibliographic references for sections, equations, theorems, figures, lists, etc. Have a look at this page.
Advanced formatting features are possible and documented (including interactive plots, pseudo-code, (Tikz) diagrams, Lua filters, mixing R + Python in the same document), but are beyond the scope of this simple introduction. We point several entries in this direction.
The Quarto web site for comprehensive documentation, including:
The template distributed with the Computo Quarto extension, which uses such advanced features.
Our mock version of the t-SNE paper, a full and advanced example using Python and the Jupyter kernel.
The previously published papers in Computo can be used as references.
Python
dependencies with venv
To make your work reproducible, you need to fix the packages and environment used to run your analysis. For Python
, venv
is one of the possible reliable method, supported by the community. You basically need a couple of commands to setup your environment on your local machine. First, to create a new virtual environment in the directory my_env
python3 -m venv my_env
and activate it
source my_env/bin/activate
Then installed the packages required to perform your analysis. Here,
python3 -m pip install jupyter matplotlib numpy
Once you are all set up, you need to save your working environment into a file so that anyone can reproduce your analysis on their side:
python3 -m pip freeze > requirements.txt
The corresponding requirements.txt
file found in this repository is then
requirements.txt
jupyter
matplotlib
numpy
requirements.txt
is the only file that needs to be versioned by git.
More details for using venv
and pip
can be found on the quarto page dedicated to environments.
conda
?For conda
users, it is also possible to follow the same path with your favorite version of conda
. There is a quarto page dedicated to the conda environments.
The repository associated with this template is pre-configured to trigger an action on push that performs the following:
ubuntu-latest
machinevenv
, using your requirements.txt
fileThe file .github/workflows/build_n_publish.yml is largely inspired from this file.
Once this is successful, you are ready to submit your manuscript to the Computo submission platform.
The first time, you possibly need to create the branch for the action to work. This can be done by running the following command from your computer, in your git repository:
quarto publish gh-pages
Then, set the branch gh-page
as the source of your github page, and trigger the action to check that everything works fine.
conda
?The build and deploy process of our Computo quarto extension shows how miniconda
can be set used in place of venv
. The main striking difference is the use of a environment.yml
file in place of requirements.txt
.
If your submission materials contain files larger than 50MB, especially data files, they won’t fit on a git repository as is. For this reason, we encourage you to put your data or any materials you deem necessary on an external “open data” centered repository hub such a Zenodo or OSF.
the same metadata as in the template-computo-python.qmd
file in the first cell, type “Raw”, of the notebook↩︎
@article{doe2024,
author = {Doe, Jane and Doe, John},
publisher = {Société Française de Statistique},
title = {Template for Contribution to {Computo}},
journal = {Computo},
date = {2024-10-16},
url = {https://computo.sfds.asso.fr/template-computo-quarto},
doi = {xxxx},
issn = {2824-7795},
langid = {en},
abstract = {Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Curabitur posuere vestibulum facilisis. Aenean pretium orci augue,
quis lobortis libero accumsan eu. Nam mollis lorem sit amet
pellentesque ullamcorper. Curabitur lobortis libero eget malesuada
vestibulum. Nam nec nibh massa. Pellentesque porttitor cursus
tellus. Mauris urna erat, rhoncus sed faucibus sit amet, venenatis
eu ipsum.}
}