Skip to content

Commit 08fcaf9

Browse files
authored
Begin incorporating myst features into notebooks (#279)
* Use note directive. * Use tip directive. * Use note directive. * Use note directive. * Use raises-exception tag. * Use note directive. * Suppress imshow warning.
1 parent 40e2850 commit 08fcaf9

File tree

1 file changed

+47
-8
lines changed

1 file changed

+47
-8
lines changed

content/tutorial-svd.md

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,18 @@ from scipy.datasets import face
4343
img = face()
4444
```
4545

46-
**Note**: If you prefer, you can use your own image as you work through this tutorial. In order to transform your image into a NumPy array that can be manipulated, you can use the `imread` function from the [matplotlib.pyplot](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.html#module-matplotlib.pyplot) submodule. Alternatively, you can use the [imageio.imread](https://imageio.readthedocs.io/en/stable/_autosummary/imageio.v3.imread.html) function from the `imageio` library. Be aware that if you use your own image, you'll likely need to adapt the steps below. For more information on how images are treated when converted to NumPy arrays, see [A crash course on NumPy for images](https://scikit-image.org/docs/stable/user_guide/numpy_images.html) from the `scikit-image` documentation.
46+
```{note}
47+
If you prefer, you can use your own image as you work through this tutorial.
48+
In order to transform your image into a NumPy array that can be manipulated, you
49+
can use the `imread` function from the
50+
[matplotlib.pyplot](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.html#module-matplotlib.pyplot) submodule.
51+
Alternatively, you can use the
52+
[imageio.imread](https://imageio.readthedocs.io/en/stable/_autosummary/imageio.v3.imread.html)
53+
function from the `imageio` library.
54+
Be aware that if you use your own image, you'll likely need to adapt the steps below.
55+
For more information on how images are treated when converted to NumPy arrays,
56+
see [A crash course on NumPy for images](https://scikit-image.org/docs/stable/user_guide/numpy_images.html) from the `scikit-image` documentation.
57+
```
4758

4859
+++
4960

@@ -105,7 +116,13 @@ Since we are going to perform linear algebra operations on this data, it might b
105116
img_array = img / 255
106117
```
107118

108-
This operation, dividing an array by a scalar, works because of NumPy's [broadcasting rules](https://numpy.org/devdocs/user/theory.broadcasting.html#array-broadcasting-in-numpy). (Note that in real-world applications, it would be better to use, for example, the [img_as_float](https://scikit-image.org/docs/stable/api/skimage.html#skimage.img_as_float) utility function from `scikit-image`).
119+
This operation, dividing an array by a scalar, works because of NumPy's [broadcasting rules](https://numpy.org/devdocs/user/theory.broadcasting.html#array-broadcasting-in-numpy).
120+
121+
```{tip}
122+
In real-world applications, it may be better to use, for example, the
123+
[img_as_float](https://scikit-image.org/docs/stable/api/skimage.html#skimage.img_as_float)
124+
utility function from `scikit-image`.
125+
```
109126

110127
You can check that the above works by doing some tests; for example, inquiring
111128
about maximum and minimum values for this array:
@@ -134,7 +151,19 @@ It is possible to use methods from linear algebra to approximate an existing set
134151

135152
+++
136153

137-
**Note**: We will use NumPy's linear algebra module, [numpy.linalg](https://numpy.org/devdocs/reference/routines.linalg.html#module-numpy.linalg), to perform the operations in this tutorial. Most of the linear algebra functions in this module can also be found in [scipy.linalg](https://docs.scipy.org/doc/scipy/reference/linalg.html#module-scipy.linalg), and users are encouraged to use the [scipy](https://docs.scipy.org/doc/scipy/reference/index.html#module-scipy) module for real-world applications. However, some functions in the [scipy.linalg](https://docs.scipy.org/doc/scipy/reference/linalg.html#module-scipy.linalg) module, such as the SVD function, only support 2D arrays. For more information on this, check the [scipy.linalg page](https://docs.scipy.org/doc/scipy/tutorial/linalg.html).
154+
```{note}
155+
We will use NumPy's linear algebra module,
156+
[numpy.linalg](https://numpy.org/devdocs/reference/routines.linalg.html#module-numpy.linalg),
157+
to perform the operations in this tutorial.
158+
Most of the linear algebra functions in this module can also be found in
159+
[scipy.linalg](https://docs.scipy.org/doc/scipy/reference/linalg.html#module-scipy.linalg),
160+
and users are encouraged to use the [scipy](https://docs.scipy.org/doc/scipy/reference/index.html#module-scipy)
161+
module for real-world applications.
162+
However, some functions in the
163+
[scipy.linalg](https://docs.scipy.org/doc/scipy/reference/linalg.html#module-scipy.linalg)
164+
module, such as the SVD function, only support 2D arrays.
165+
For more information on this, check the [scipy.linalg page](https://docs.scipy.org/doc/scipy/tutorial/linalg.html).
166+
```
138167

139168
+++
140169

@@ -177,7 +206,11 @@ import numpy as np
177206
U, s, Vt = np.linalg.svd(img_gray)
178207
```
179208

180-
**Note** If you are using your own image, this command might take a while to run, depending on the size of your image and your hardware. Don't worry, this is normal! The SVD can be a pretty intensive computation.
209+
```{note}
210+
If you are using your own image, this command might take a while to run,
211+
depending on the size of your image and your hardware.
212+
Don't worry, this is normal! The SVD can be a pretty intensive computation.
213+
```
181214

182215
+++
183216

@@ -188,9 +221,10 @@ U.shape, s.shape, Vt.shape
188221
```
189222

190223
Note that `s` has a particular shape: it has only one dimension. This means that some linear algebra functions that expect 2d arrays might not work. For example, from the theory, one might expect `s` and `Vt` to be
191-
compatible for multiplication. However, this is not true as `s` does not have a second axis. Executing
224+
compatible for multiplication. However, this is not true as `s` does not have a second axis:
192225

193-
```python
226+
```{code-cell}
227+
:tags: [raises-exception]
194228
s @ Vt
195229
```
196230

@@ -331,7 +365,12 @@ plt.imshow(np.transpose(reconstructed, (1, 2, 0)))
331365
plt.show()
332366
```
333367

334-
In fact, `imshow` peforms this clipping under-the-hood, so if you skip the first line in the previous code cell, you might see a warning message saying `"Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers)."`
368+
```{note}
369+
In fact, `imshow` peforms this clipping under-the-hood, so if you skip the first
370+
line in the previous code cell, you might see a warning message saying
371+
`"Clipping input data to the valid range for imshow with RGB data ([0..1] for
372+
floats or [0..255] for integers)."`
373+
```
335374

336375
Now, to do the approximation, we must choose only the first `k` singular values for each color channel. This can be done using the following syntax:
337376

@@ -351,7 +390,7 @@ approx_img.shape
351390
which is not the right shape for showing the image. Finally, reordering the axes back to our original shape of `(768, 1024, 3)`, we can see our approximation:
352391

353392
```{code-cell}
354-
plt.imshow(np.transpose(approx_img, (1, 2, 0)))
393+
plt.imshow(np.transpose(np.clip(approx_img, 0, 1), (1, 2, 0)))
355394
plt.show()
356395
```
357396

0 commit comments

Comments
 (0)