Skip to content

Commit e3e67bb

Browse files
authored
Apply black formatting to all notebooks. (#95)
1 parent 022313d commit e3e67bb

10 files changed

+558
-482
lines changed

content/mooreslaw-tutorial.md

+64-64
Large diffs are not rendered by default.

content/pairing.md

+15-13
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ jupytext:
44
text_representation:
55
extension: .md
66
format_name: myst
7-
format_version: 0.12
8-
jupytext_version: 1.6.0
7+
format_version: 0.13
8+
jupytext_version: 1.11.1
99
kernelspec:
1010
display_name: Python 3
1111
language: python
@@ -16,7 +16,7 @@ kernelspec:
1616

1717
## What you'll do
1818
This guide will keep a Jupyter notebook synced _or paired_ between
19-
`.ipynb` and `.md`.
19+
`.ipynb` and `.md`.
2020

2121
## What you'll learn
2222
- The difference between Jupyter's json format and MyST-NB's markdown
@@ -44,15 +44,15 @@ format.
4444
Jupyter notebooks are stored on your disk in a
4545
[json format](https://nbformat.readthedocs.io/en/latest/format_description.html). The json format is
4646
very powerful and allows you to store almost any input and output that
47-
Python libraries can create. The drawback is that it is hard to see and compare changes made in the notebook file when reviewing pull requests, because this means the reviewers are looking only at the raw json files.
47+
Python libraries can create. The drawback is that it is hard to see and compare changes made in the notebook file when reviewing pull requests, because this means the reviewers are looking only at the raw json files.
4848

49-
MyST-NB notebooks are stored on your disk in a
49+
MyST-NB notebooks are stored on your disk in a
5050
[markdown](https://en.wikipedia.org/wiki/Markdown) format. The markdown
5151
format is a lightweight markup language. Its key design goal is
5252
[_readability_](https://daringfireball.net/projects/markdown/syntax#philosophy).
5353
The drawback is that markdown can only store the inputs of your code.
5454
Each time you open the notebook, you must execute the inputs to see the
55-
output.
55+
output.
5656

5757
> __Note:__ You should use [common mark](https://commonmark.org)
5858
> markdown cells. Jupyter only renders common mark markdown, but MyST-NB
@@ -64,7 +64,7 @@ output.
6464
Consider these two versions of the same __Simple notebook example__. You
6565
have three things in the notebooks:
6666

67-
1. A markdown cell that explains the code
67+
1. A markdown cell that explains the code
6868
```This code calculates 2+2 and prints the output.```
6969
2. A code cell that shows the code
7070
```python
@@ -78,10 +78,12 @@ have three things in the notebooks:
7878
---
7979
__<center> Simple notebook example </center>__
8080
This code calculates 2+2 and prints the output.
81-
```{code-cell} ipython3
81+
82+
```{code-cell}
8283
x = 2 + 2
83-
print('x = ', x)
84+
print("x = ", x)
8485
```
86+
8587
---
8688

8789
Here are the two Simple notebook example raw inputs side-by-side:
@@ -186,7 +188,7 @@ When you submit a Jupyter notebook to NumPy tutorials, we (the reviewers) will c
186188
it to a MyST-NB format. You can also submit the MyST-NB `.md` in your
187189
pull request.
188190
To keep the `.ipynb` and `.md` in sync--_or paired_--you need
189-
[Jupytext](https://jupytext.readthedocs.io/en/latest/index.html).
191+
[Jupytext](https://jupytext.readthedocs.io/en/latest/index.html).
190192

191193
Install `jupytext` using:
192194

@@ -200,7 +202,7 @@ conda install jupytext -c conda-forge
200202

201203
Once installed, start your `jupyter lab` or `jupyter notebook`
202204
session in the browser. When launching `jupyter lab` it will ask you to rebuild
203-
to include the Jupytext extension.
205+
to include the Jupytext extension.
204206

205207
You can pair the two formats in the classic Jupyter, Jupyter Lab,
206208
or the command line:
@@ -238,8 +240,8 @@ jupytext --sync notebook.ipynb
238240
239241
## Wrapping up
240242

241-
In this tutorial, you saw the json `.ipynb` and MyST-NB `.md` raw code
243+
In this tutorial, you saw the json `.ipynb` and MyST-NB `.md` raw code
242244
to create Jupyter notebooks. You can use both formats to create
243245
tutorials. Now you can work in either a simple text editor like VIM
244246
or emacs or continue building notebooks in your browser. Jupytext can
245-
handle pairing to keep your work in sync.
247+
handle pairing to keep your work in sync.

content/save-load-arrays.md

+31-33
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ jupytext:
33
text_representation:
44
extension: .md
55
format_name: myst
6-
format_version: 0.12
7-
jupytext_version: 1.6.0
6+
format_version: 0.13
7+
jupytext_version: 1.11.1
88
kernelspec:
99
display_name: Python 3
1010
language: python
@@ -22,14 +22,14 @@ file types back into NumPy workspaces.
2222
## What you'll do
2323

2424
You'll learn two ways of saving and reading files--as compressed and as
25-
text files--that will serve most of your storage needs in NumPy.
25+
text files--that will serve most of your storage needs in NumPy.
2626

27-
* You'll create two 1D arrays and one 2D array
27+
* You'll create two 1D arrays and one 2D array
2828
* You'll save these arrays to files
2929
* You'll remove variables from your workspace
3030
* You'll load the variables from your saved file
3131
* You'll compare zipped binary files to human-readable delimited files
32-
* You'll finish with the skills of saving, loading, and sharing NumPy arrays
32+
* You'll finish with the skills of saving, loading, and sharing NumPy arrays
3333

3434

3535
## What you'll need
@@ -39,7 +39,7 @@ text files--that will serve most of your storage needs in NumPy.
3939

4040
Load the necessary functions using the following command.
4141

42-
```{code-cell} ipython3
42+
```{code-cell}
4343
import numpy as np
4444
```
4545

@@ -63,11 +63,11 @@ In this tutorial, you will use the following Python, IPython magic, and NumPy fu
6363

6464
Now that you have imported the NumPy library, you can make a couple of
6565
arrays; let's start with two 1D arrays, `x` and `y`, where `y =
66-
x**2`.You
66+
x**2`.You
6767
will assign `x` to the integers from 0 to 9 using
6868
[`np.arange`](https://numpy.org/doc/stable/reference/generated/numpy.arange.html).
6969

70-
```{code-cell} ipython3
70+
```{code-cell}
7171
x = np.arange(0, 10, 1)
7272
y = x ** 2
7373
print(x)
@@ -76,7 +76,7 @@ print(y)
7676

7777
## Save your arrays with NumPy's [`savez`](https://numpy.org/doc/stable/reference/generated/numpy.savez.html?highlight=savez#numpy.savez)
7878

79-
Now you have two arrays in your workspace,
79+
Now you have two arrays in your workspace,
8080

8181
`x: [0 1 2 3 4 5 6 7 8 9]`
8282

@@ -85,12 +85,12 @@ Now you have two arrays in your workspace,
8585
The first thing you will do is save them to a file as zipped arrays
8686
using
8787
[`savez`](https://numpy.org/doc/stable/reference/generated/numpy.savez.html?highlight=savez#numpy.savez).
88-
You will use two options to label the arrays in the file,
88+
You will use two options to label the arrays in the file,
8989

9090
1. `x_axis = x`: this option is assigning the name `x_axis` to the variable `x`
9191
2. `y_axis = y`: this option is assigning the name `y_axis` to the variable `y`
9292

93-
```{code-cell} ipython3
93+
```{code-cell}
9494
np.savez("x_y-squared.npz", x_axis=x, y_axis=y)
9595
```
9696

@@ -103,7 +103,7 @@ name `x_y-squared.npz`. This file is a zipped binary of the two arrays,
103103
format](https://numpy.org/doc/stable/reference/generated/numpy.lib.format.html#module-numpy.lib.format)
104104
files. The NPY format is a [native binary
105105
format](https://en.wikipedia.org/wiki/Binary_file). You cannot read
106-
the numbers in a standard text editor or spreadsheet.
106+
the numbers in a standard text editor or spreadsheet.
107107

108108
1. remove `x` and `y` from the workspaec with [`del`](https://docs.python.org/3/reference/simple_stmts.html#del)
109109
2. load the arrays into the workspace in a dictionary with [`np.load`](https://numpy.org/doc/stable/reference/generated/numpy.load.html#numpy.load)
@@ -112,31 +112,31 @@ To see what variables are in the workspace, use the Jupyter/IPython
112112
"magic" command
113113
[`whos`](https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-whos).
114114

115-
```{code-cell} ipython3
115+
```{code-cell}
116116
del x, y
117117
```
118118

119-
```{code-cell} ipython3
119+
```{code-cell}
120120
%whos
121121
```
122122

123-
```{code-cell} ipython3
123+
```{code-cell}
124124
load_xy = np.load("x_y-squared.npz")
125125
126126
print(load_xy.files)
127127
```
128128

129-
```{code-cell} ipython3
129+
```{code-cell}
130130
whos
131131
```
132132

133133
## Reassign the NpzFile arrays to `x` and `y`
134134

135135
You've now created the dictionary with an `NpzFile`-type. The
136136
included files are `x_axis` and `y_axis` that you defined in your
137-
`savez` command. You can reassign `x` and `y` to the `load_xy` files.
137+
`savez` command. You can reassign `x` and `y` to the `load_xy` files.
138138

139-
```{code-cell} ipython3
139+
```{code-cell}
140140
x = load_xy["x_axis"]
141141
y = load_xy["y_axis"]
142142
print(x)
@@ -153,7 +153,7 @@ that is easier to share. Next, you use the
153153
[`savetxt`](https://numpy.org/doc/stable/reference/generated/numpy.savetxt.html#numpy.savetxt)
154154
to save `x` and `y` in a comma separated value file, `x_y-squared.csv`.
155155
The resulting csv is composed of ASCII characters. You can load the file
156-
back into NumPy or read it with other programs.
156+
back into NumPy or read it with other programs.
157157

158158
## Rearrange the data into a single 2D array
159159
First, you have to create a single 2D array from your two 1D arrays. The
@@ -167,7 +167,7 @@ two NumPy functions to format the data:
167167

168168
2. [`np.newaxis`](https://numpy.org/doc/stable/reference/constants.html?highlight=newaxis#numpy.newaxis): this function forces the 1D array into a 2D column vector with 10 rows and 1 column.
169169

170-
```{code-cell} ipython3
170+
```{code-cell}
171171
array_out = np.block([x[:, np.newaxis], y[:, np.newaxis]])
172172
print("the output array has shape ", array_out.shape, " with values:")
173173
print(array_out)
@@ -181,7 +181,7 @@ You use `savetxt` with a three options to make your file easier to read:
181181
* `header = 'x, y'`: this option writes a header before any data that labels the columns of the csv
182182
* `delimiter = ','`: this option tells `savetxt` to place a comma between each column in the file
183183

184-
```{code-cell} ipython3
184+
```{code-cell}
185185
np.savetxt("x_y-squared.csv", X=array_out, header="x, y", delimiter=",")
186186
```
187187

@@ -208,31 +208,29 @@ There are two features that you shoud notice here:
208208
1. NumPy uses `#` to ignore headings when using `loadtxt`. If you're using
209209
[`loadtxt`](https://numpy.org/doc/stable/reference/generated/numpy.loadtxt.html)
210210
with other csv files, you can skip header rows with `skiprows =
211-
<number_of_header_lines>`.
211+
<number_of_header_lines>`.
212212
2. The integers were written in scientific notation. _You can_ specify
213213
the format of the text using the `savetxt` option, [`fmt =
214214
`](https://docs.python.org/3/library/string.html#formatstrings), but it
215215
will still be written with ASCII characters. In general, you cannot
216-
preserve the type of ASCII numbers as `float` or `int`.
216+
preserve the type of ASCII numbers as `float` or `int`.
217217

218218

219-
Now, delete `x` and `y` again and assign them to your columns in `x-y_squared.csv`.
219+
Now, delete `x` and `y` again and assign them to your columns in `x-y_squared.csv`.
220220

221-
```{code-cell} ipython3
221+
```{code-cell}
222222
del x, y
223223
```
224224

225-
```{code-cell} ipython3
226-
load_xy = np.loadtxt(
227-
"x_y-squared.csv",
228-
delimiter=",")
225+
```{code-cell}
226+
load_xy = np.loadtxt("x_y-squared.csv", delimiter=",")
229227
```
230228

231-
```{code-cell} ipython3
229+
```{code-cell}
232230
load_xy.shape
233231
```
234232

235-
```{code-cell} ipython3
233+
```{code-cell}
236234
x = load_xy[:, 0]
237235
y = load_xy[:, 1]
238236
print(x)
@@ -242,12 +240,12 @@ print(y)
242240
## Success, but remember your types
243241

244242
When you saved the arrays to the csv file, you did not preserve the
245-
`int` type. When loading the arrays back into your workspace the default process will be to load the csv file as a 2D floating point array e.g. `load_xy.dtype == 'float64'` and `load_xy.shape == (10, 2)`.
243+
`int` type. When loading the arrays back into your workspace the default process will be to load the csv file as a 2D floating point array e.g. `load_xy.dtype == 'float64'` and `load_xy.shape == (10, 2)`.
246244

247245
+++
248246

249247
## Wrapping up
250248

251-
In conclusion, you can create, save, and load arrays in NumPy. Saving arrays makes sharing your work and collaboration much easier. There are other ways Python can save data to files, such as [pickle](https://docs.python.org/3/library/pickle.html), but `savez` and `savetxt` will serve most of your storage needs for future NumPy work and sharing with other people, respectively.
249+
In conclusion, you can create, save, and load arrays in NumPy. Saving arrays makes sharing your work and collaboration much easier. There are other ways Python can save data to files, such as [pickle](https://docs.python.org/3/library/pickle.html), but `savez` and `savetxt` will serve most of your storage needs for future NumPy work and sharing with other people, respectively.
252250

253251
__Next steps__: you can import data with missing values from [Importing with genfromtext](https://numpy.org/devdocs/user/basics.io.genfromtxt.html) or learn more about general NumPy IO with [Reading and Writing Files](https://numpy.org/devdocs/user/how-to-io.html).

0 commit comments

Comments
 (0)