-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtemplate-computo-python.qmd
325 lines (225 loc) · 12.7 KB
/
template-computo-python.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
---
title: "Template for contribution to Computo"
subtitle: "Example dedicated to `Python` users"
author:
- name: Jane Doe
corresponding: true
email: janedoe@nowhere.moon
url: https://janedoe.someplace.themoon.org
orcid: 0000-0000-0000-0000
affiliations:
- name: Name of Affiliation one
department: Statistics
url: https://someplace.themoon.org
- name: John Doe
email: johndoe@nowhere.moon
url: https://johndoe.someplace.themoon.org
orcid: 0000-0000-0000-0000
affiliations:
- name: Name of Afficiliation two
department: Computer Science
url: https://someplace.themoon.org
date: last-modified
date-modified: last-modified
description: |
This document provides a template based on the [`quarto`](https://quarto.org/) system for contributions to Computo, using the [`quarto journal extension`](https://github.com/computorg/computo-quarto-extension), the Jupyter kernel (Python user) and `venv+pip` to set-up the dependencies.
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.
keywords: [key1, key2, key3]
citation:
type: article-journal
container-title: "Computo"
doi: "xxxx"
url: https://computo.sfds.asso.fr/template-computo-quarto
publisher: "Société Française de Statistique"
issn: "2824-7795"
bibliography: references.bib
github-user: computorg
repo: "template-computo-python"
draft: true # set to false once the build is running
published: false # will be set to true once accepted
format:
computo-html: default
computo-pdf: default
jupyter: python3
---
# Introduction
## About this document
This document, accompanied by the [customized GitHub repository](https://github.com/computorg/template-computo-python/), provides a template for writing contributions to **Computo** [@computo]. 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`.
## Setup a GitHub repository for preparing your submission
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](https://github.com/computorg/template-computo-python/). Of course, you can set your repository private during the preparation of your manuscript.
## Quarto
[Quarto](https://quarto.org/) 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](https://pandoc.org/MANUAL.html) document converter.
## Requirements
You need [quarto](https://quarto.org/docs/get-started/) installed on your system and the [Computo extension](https://github.com/computorg/computo-quarto-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):
```.bash
quarto add computorg/computo-quarto-extension
```
[`Python`](https://www.python.org/) and [`Jupyter`](https://jupyter.org/install) must be installed on your computer.
## Link with your usual tools
Quarto is expecting a `.qmd` markdown file, but will also works with a standard [Jupyter notebook](https://quarto.org/docs/get-started/hello/jupyter.html) file if you are used to it (it will just require to add the proper YAML metadata^[the same metadata as in the [`template-computo-python.qmd` file](https://github.com/computorg/template-computo-python/blob/main/template-computo-python.qmd) in the first cell, type "Raw", of the notebook]).
**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](#formatting)). If you are more comfortable with using Myst/Jupyter book, we provide a [specific template](https://github.com/computorg/template-computo-myst) but it will requires more formatting work for Computo editorial team, thus highly encourage authors to use the Quarto templates._
# Formatting
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:
```.bash
quarto render template-computo-python.qmd # renders both HTML and PDF
```
::: {.callout-tip}
## Note
To check the syntax of the formatting below, you can use the `</> source` button at the top right of this document.
:::
## Basic markdown formatting
**Bold text** or _italic_
- This is a list
- With more elements
- It isn't numbered.
But we can also do a numbered list
1. This is my first item
2. This is my second item
3. This is my third item
## Mathematics
### Mathematical formulae
[LaTeX](https://www.latex-project.org/) code is natively supported[^lualatex], which makes it possible to use mathematical formulae:
[^lualatex]: We use [lualatex](https://lualatex.org/) for this purpose.
$$
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 @eq-mylabel:
$$
\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}
$$ {#eq-mylabel}
### Theorems and other amsthem-like environments
Quarto includes a nice support for theorems, with predefined prefix labels for theorems, lemmas, proposition, etc. see [this page](https://quarto.org/docs/authoring/cross-references.html#theorems-and-proofs). Here is a simple example:
::: {#thm-slln}
## 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 @thm-slln.
## Python Code
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).
``` yaml
---
title: "My Document"
author "Jane Doe"
jupyter: python3
---
```
`python` code chunks may be embedded as follows:
```{python python-code}
import numpy as np
x = np.random.normal(0, 1, 10)
x
```
## Figures
Plots can be generated as follows:
```{python stem-plot}
#| label: fig-stem-plot
#| fig-cap: "A basic Stem plot"
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.1, 2 * np.pi, 41)
y = np.exp(np.sin(x))
plt.stem(x, y)
plt.show()
```
It is also possible to create figures from static images:
:::{#fig-logo}
![](https://computo.sfds.asso.fr/assets/img/logo_text_vertical.png){height=200}
Computo logo (label)
:::
## Tables
Tables (with label: `@tbl-mylabel` renders @tbl-mylabel) 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 |
: my table caption {#tbl-mylabel}
## Handling references {#sec-references}
### Bibliographic references
References are displayed as footnotes using
[BibTeX](http://www.bibtex.org/), e.g. `[@computo]` will be displayed
as [@computo], 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`).
### Other cross-references
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](https://quarto.org/docs/authoring/cross-references.html).
## Advanced formatting
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.
::: {.callout-warning}
## More information
- [The Quarto web site](https://quarto.org) for comprehensive documentation, including:
+ [Tutorial](https://quarto.org/docs/get-started/)
+ [User guide](https://quarto.org/docs/guide/)
+ [Options reference](https://quarto.org/docs/reference/)
- [The template distributed with the Computo Quarto extension](https://computo.sfds.asso.fr/computo-quarto-extension/), which uses such advanced features.
- [Our mock version of the t-SNE paper](https://computo.sfds.asso.fr/published-paper-tsne/), a full and advanced example using Python and the Jupyter kernel.
- [The previously published papers in Computo](https://computo.sfds.asso.fr/publications/) can be used as references.
:::
# Finalize your submission
## Handle `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`
``` .bash
python3 -m venv my_env
```
and activate it
``` .bash
source my_env/bin/activate
```
Then installed the packages required to perform your analysis. Here,
``` .bash
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:
``` .bash
python3 -m pip freeze > requirements.txt
```
The corresponding `requirements.txt` file [found in this repository](https://github.com/computorg/template-computo-python/blob/main/requirements.txt) is then
```{.bash filename="requirements.txt"}
jupyter
matplotlib
numpy
```
::: {.callout-important}
`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](https://quarto.org/docs/projects/virtual-environments.html#using-venv).
### What about `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](https://quarto.org/docs/projects/virtual-environments.html#using-conda).
## Continuous integration
The repository associated with this template is pre-configured to trigger an action on push that performs the following:
1. Check out the repository on an `ubuntu-latest` machine
2. Install quarto and dependencies, including the Computo extension
3. Install Python (3.10) and dependencies with `venv`, using your `requirements.txt` file
4. Render your .qmd file and Publish the results on a gh-page (both HTML and PDF)
The file [.github/workflows/build_n_publish.yml](https://github.com/computorg/template-computo-python/blob/main/.github/workflows/build_n_publish.yml) is largely inspired from [this file](https://quarto.org/docs/publishing/github-pages.html#example-jupyter-with-venv).
Once this is successful, you are ready to submit your manuscript to the [Computo submission platform](https://computo.scholasticahq.com/).
::: {.callout-warning}
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:
```.bash
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.
:::
### What about CI and `conda`?
[The build and deploy process of our Computo quarto extension](https://github.com/computorg/computo-quarto-extension/blob/main/.github/workflows/build.yml) 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`.
## Data and large files
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](https://zenodo.org/) or [OSF](https://osf.io/).
# References {.unnumbered}
::: {#refs}
:::