Skip to content

Commit f6eec40

Browse files
authored
Merge pull request #113 from silx-kit/numpy-updates
Update the NumPy section
2 parents e5474f0 + 927326a commit f6eec40

3 files changed

Lines changed: 573 additions & 966 deletions

File tree

python/numpy/Exercise-HPLC.ipynb

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"cells": [
33
{
4+
"attachments": {},
45
"cell_type": "markdown",
56
"id": "cb1a9b1f-5495-417a-a21b-10f1f6132e56",
67
"metadata": {},
@@ -11,6 +12,7 @@
1112
]
1213
},
1314
{
15+
"attachments": {},
1416
"cell_type": "markdown",
1517
"id": "36b6b654-fbc3-4124-ba7c-3340225a3356",
1618
"metadata": {},
@@ -19,6 +21,7 @@
1921
]
2022
},
2123
{
24+
"attachments": {},
2225
"cell_type": "markdown",
2326
"id": "77205b12-3f0b-4e8a-90b5-c2964d4cb34c",
2427
"metadata": {},
@@ -29,6 +32,7 @@
2932
]
3033
},
3134
{
35+
"attachments": {},
3236
"cell_type": "markdown",
3337
"id": "be649cc3-56f7-4ab9-b2a8-1fb6235e6493",
3438
"metadata": {},
@@ -39,6 +43,7 @@
3943
]
4044
},
4145
{
46+
"attachments": {},
4247
"cell_type": "markdown",
4348
"id": "93503d83-1b11-4c91-8155-3f51a28ab6a7",
4449
"metadata": {},
@@ -54,23 +59,18 @@
5459
"Those **I** values are stored as a 2D dataset of shape (400, 1000) in the `intensities.npy` file.\n",
5560
"The **q** values are stored in the `q.txt` file.\n",
5661
"\n",
57-
"At first, only the buffer is passing through the capillary, then sample+buffer and finally buffer again."
58-
]
59-
},
60-
{
61-
"cell_type": "markdown",
62-
"id": "530ef2d2-5910-447f-831a-b6029e4a1beb",
63-
"metadata": {},
64-
"source": [
62+
"At first, only the buffer is passing through the capillary, then sample + buffer and finally buffer again.\n",
63+
"\n",
6564
"The goal is to extract the intensity contributed by the sample.\n",
6665
"The steps are:\n",
6766
"\n",
68-
"0. Separate integrated intensities corresponding to buffer+sample from those corresponding to buffer only\n",
69-
"0. Estimate the buffer and the sample+buffer intensities by averaging the selected integrated intensities\n",
70-
"0. Remove the buffer background from sample+buffer"
67+
"1. Separate integrated intensities corresponding to buffer+sample from those corresponding to buffer only\n",
68+
"2. Estimate the buffer and the sample + buffer intensities by averaging the selected integrated intensities\n",
69+
"3. Remove the buffer background from sample + buffer"
7170
]
7271
},
7372
{
73+
"attachments": {},
7474
"cell_type": "markdown",
7575
"id": "98afc9af-cdb1-4bf7-8c93-c0d1a7fb2a8e",
7676
"metadata": {},
@@ -85,10 +85,11 @@
8585
"metadata": {},
8686
"outputs": [],
8787
"source": [
88-
"import numpy"
88+
"import numpy as np"
8989
]
9090
},
9191
{
92+
"attachments": {},
9293
"cell_type": "markdown",
9394
"id": "d2c55c1c-c588-40e5-ae01-c476a1c2616e",
9495
"metadata": {},
@@ -110,6 +111,7 @@
110111
]
111112
},
112113
{
114+
"attachments": {},
113115
"cell_type": "markdown",
114116
"id": "c70d852d-470d-45d1-adc7-a6a23feb5854",
115117
"metadata": {},
@@ -138,13 +140,13 @@
138140
"metadata": {},
139141
"outputs": [],
140142
"source": [
141-
"# Plot intensities\n",
143+
"# Plot the intensities\n",
142144
"import matplotlib.colors as colors\n",
143145
"\n",
144146
"fig = plt.figure()\n",
145147
"plt.imshow(intensities, norm=colors.LogNorm(), aspect=\"auto\")\n",
146-
"\n",
147-
"# Note: with latest version of matplotlib:\n",
148+
" \n",
149+
"# Note: with latest version of matplotlib: \n",
148150
"# plt.imshow(intensities, norm=\"log\", aspect=\"auto\")"
149151
]
150152
},
@@ -162,6 +164,7 @@
162164
]
163165
},
164166
{
167+
"attachments": {},
165168
"cell_type": "markdown",
166169
"id": "de96e9c6-5421-4d74-8b4d-9da68716cac2",
167170
"metadata": {},
@@ -170,6 +173,7 @@
170173
]
171174
},
172175
{
176+
"attachments": {},
173177
"cell_type": "markdown",
174178
"id": "4961fad5-fe0b-4d6e-9663-228c630fb60c",
175179
"metadata": {
@@ -207,14 +211,16 @@
207211
]
208212
},
209213
{
214+
"attachments": {},
210215
"cell_type": "markdown",
211216
"id": "9c2026c6-a04a-4ad5-92a4-5e473ce3dd3b",
212217
"metadata": {},
213218
"source": [
214-
"Note: This is not meaningful, the buffer and sample+buffer cases should be separated."
219+
"Note: This is not meaningful, the buffer and sample + buffer cases should be separated."
215220
]
216221
},
217222
{
223+
"attachments": {},
218224
"cell_type": "markdown",
219225
"id": "3adff22d-4835-4f3c-935f-a06f71372e01",
220226
"metadata": {},
@@ -242,12 +248,13 @@
242248
"outputs": [],
243249
"source": [
244250
"fig = plt.figure()\n",
245-
"plt.plot(frames_intensities)\n",
251+
"plt.plot(intensities_per_frame)\n",
246252
"plt.xlabel(\"Frame ID\")\n",
247253
"plt.ylabel(\"I\")"
248254
]
249255
},
250256
{
257+
"attachments": {},
251258
"cell_type": "markdown",
252259
"id": "37949d27-0a36-488b-b230-df7765a6f286",
253260
"metadata": {},
@@ -256,15 +263,16 @@
256263
]
257264
},
258265
{
266+
"attachments": {},
259267
"cell_type": "markdown",
260268
"id": "de3ad40e-97b1-4ef9-aae7-168117aad0a0",
261269
"metadata": {
262270
"tags": []
263271
},
264272
"source": [
265-
"### Separate sample+buffer from buffer only\n",
273+
"### Separate sample + buffer from buffer only\n",
266274
"\n",
267-
"Select buffer and sample+buffer intensities by using a threshold over `intensities_summed`"
275+
"Select buffer and sample + buffer intensities by using a threshold over `intensities_summed`."
268276
]
269277
},
270278
{
@@ -298,11 +306,12 @@
298306
]
299307
},
300308
{
309+
"attachments": {},
301310
"cell_type": "markdown",
302311
"id": "51fa7126-33b6-4fb2-8fa9-0c5b427325ba",
303312
"metadata": {},
304313
"source": [
305-
"### Average sample+buffer and buffer intensities\n",
314+
"### Average sample + buffer and buffer intensities\n",
306315
"\n",
307316
"Compute the average of azimuthal integrations of `buffer` for each `q`."
308317
]
@@ -318,6 +327,7 @@
318327
]
319328
},
320329
{
330+
"attachments": {},
321331
"cell_type": "markdown",
322332
"id": "93b16a5e-466a-4a7f-8da5-6b591bf6c3ce",
323333
"metadata": {},
@@ -344,13 +354,14 @@
344354
"source": [
345355
"fig = plt.figure()\n",
346356
"plt.plot(q, buffer_mean, 'black', q, sample_buffer_mean, 'red')\n",
347-
"plt.title(\"buffer and sample+buffer average\")\n",
357+
"plt.title(\"buffer and sample + buffer average\")\n",
348358
"plt.xlabel(\"q\")\n",
349359
"plt.ylabel(\"I\")\n",
350360
"plt.yscale(\"log\")"
351361
]
352362
},
353363
{
364+
"attachments": {},
354365
"cell_type": "markdown",
355366
"id": "13476124-7e14-4a6a-a49f-1f3b02dd1a6f",
356367
"metadata": {},
@@ -383,6 +394,7 @@
383394
]
384395
},
385396
{
397+
"attachments": {},
386398
"cell_type": "markdown",
387399
"id": "dbc81e5b-19b4-4aac-8648-7e8b67f3c9e0",
388400
"metadata": {},
@@ -395,35 +407,35 @@
395407
"```python\n",
396408
"# Part I\n",
397409
"\n",
398-
"import numpy\n",
410+
"import numpy as np\n",
399411
"\n",
400412
"# Load data\n",
401-
"intensities = numpy.load(\"intensities.npy\")\n",
402-
"q = numpy.loadtxt(\"q.txt\")\n",
413+
"intensities = np.load(\"intensities.npy\")\n",
414+
"q = np.loadtxt(\"q.txt\")\n",
403415
"\n",
404416
"# Part II\n",
405417
"\n",
406418
"# Average of all azimuthal integrations\n",
407-
"intensities_mean = numpy.mean(intensities, axis=0)\n",
419+
"intensities_mean = np.mean(intensities, axis=0)\n",
408420
"\n",
409421
"# Summed intensity of each azimuthal integration\n",
410-
"frames_intensities = numpy.sum(intensities, axis=1)\n",
422+
"intensities_per_frame = np.sum(intensities, axis=1)\n",
411423
"\n",
412424
"# Part III\n",
413425
"\n",
414-
"# Separate sample+buffer from buffer only\n",
426+
"# Separate sample + buffer from buffer only\n",
415427
"# 1. with thresholds\n",
416-
"buffer_mask = frames_intensities < 32500\n",
428+
"buffer_mask = intensities_per_frame < 32500\n",
417429
"buffer = intensities[buffer_mask]\n",
418430
"sample_buffer_mask = frames_intensities > 33000\n",
419431
"sample_buffer = intensities[sample_buffer_mask]\n",
420432
"# 2. With slicing\n",
421433
"buffer = intensities[:200]\n",
422434
"sample_buffer = intensities[270:340]\n",
423435
"\n",
424-
"# Average sample+buffer and buffer intensities\n",
425-
"buffer_mean = numpy.mean(buffer, axis=0)\n",
426-
"sample_buffer_mean = numpy.mean(sample_buffer, axis=0)\n",
436+
"# Average sample + buffer and buffer intensities\n",
437+
"buffer_mean = np.mean(buffer, axis=0)\n",
438+
"sample_buffer_mean = np.mean(sample_buffer, axis=0)\n",
427439
"\n",
428440
"# Remove buffer background\n",
429441
"sample = sample_buffer_mean - buffer_mean\n",
@@ -434,22 +446,8 @@
434446
}
435447
],
436448
"metadata": {
437-
"kernelspec": {
438-
"display_name": "Python 3 (ipykernel)",
439-
"language": "python",
440-
"name": "python3"
441-
},
442449
"language_info": {
443-
"codemirror_mode": {
444-
"name": "ipython",
445-
"version": 3
446-
},
447-
"file_extension": ".py",
448-
"mimetype": "text/x-python",
449-
"name": "python",
450-
"nbconvert_exporter": "python",
451-
"pygments_lexer": "ipython3",
452-
"version": "3.9.12"
450+
"name": "python"
453451
}
454452
},
455453
"nbformat": 4,

python/numpy/exercicesolution.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def ex4_2_alt():
7171

7272
def ex5_inefficient_fill(height=1000, width=1000):
7373
"""Inefficient fill using 2 for loops"""
74-
data = numpy.zeros((height, width), dtype=numpy.float)
74+
data = numpy.zeros((height, width), dtype=float)
7575
for row in range(int(height)):
7676
for col in range(int(width)):
7777
data[row, col] = numpy.cos(row) * numpy.sin(col)
@@ -82,7 +82,7 @@ def ex5_naive_fill(height=1000, width=1000):
8282
"""Fill using 2 for loops but pre-computing sin and cos"""
8383
width_sin = numpy.sin(numpy.arange(width))
8484
height_cos = numpy.cos(numpy.arange(height))
85-
data = numpy.zeros((height, width), numpy.float)
85+
data = numpy.zeros((height, width), dtype=numpy.float64)
8686
for row in range(int(height)):
8787
for col in range(int(width)):
8888
data[row, col] = height_cos[row] * width_sin[col]

0 commit comments

Comments
 (0)