Skip to content

Commit

Permalink
Merge pull request #113 from silx-kit/numpy-updates
Browse files Browse the repository at this point in the history
Update the NumPy section
  • Loading branch information
payno authored Jun 29, 2023
2 parents e5474f0 + 927326a commit f6eec40
Show file tree
Hide file tree
Showing 3 changed files with 573 additions and 966 deletions.
90 changes: 44 additions & 46 deletions python/numpy/Exercise-HPLC.ipynb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "cb1a9b1f-5495-417a-a21b-10f1f6132e56",
"metadata": {},
Expand All @@ -11,6 +12,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "36b6b654-fbc3-4124-ba7c-3340225a3356",
"metadata": {},
Expand All @@ -19,6 +21,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "77205b12-3f0b-4e8a-90b5-c2964d4cb34c",
"metadata": {},
Expand All @@ -29,6 +32,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "be649cc3-56f7-4ab9-b2a8-1fb6235e6493",
"metadata": {},
Expand All @@ -39,6 +43,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "93503d83-1b11-4c91-8155-3f51a28ab6a7",
"metadata": {},
Expand All @@ -54,23 +59,18 @@
"Those **I** values are stored as a 2D dataset of shape (400, 1000) in the `intensities.npy` file.\n",
"The **q** values are stored in the `q.txt` file.\n",
"\n",
"At first, only the buffer is passing through the capillary, then sample+buffer and finally buffer again."
]
},
{
"cell_type": "markdown",
"id": "530ef2d2-5910-447f-831a-b6029e4a1beb",
"metadata": {},
"source": [
"At first, only the buffer is passing through the capillary, then sample + buffer and finally buffer again.\n",
"\n",
"The goal is to extract the intensity contributed by the sample.\n",
"The steps are:\n",
"\n",
"0. Separate integrated intensities corresponding to buffer+sample from those corresponding to buffer only\n",
"0. Estimate the buffer and the sample+buffer intensities by averaging the selected integrated intensities\n",
"0. Remove the buffer background from sample+buffer"
"1. Separate integrated intensities corresponding to buffer+sample from those corresponding to buffer only\n",
"2. Estimate the buffer and the sample + buffer intensities by averaging the selected integrated intensities\n",
"3. Remove the buffer background from sample + buffer"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "98afc9af-cdb1-4bf7-8c93-c0d1a7fb2a8e",
"metadata": {},
Expand All @@ -85,10 +85,11 @@
"metadata": {},
"outputs": [],
"source": [
"import numpy"
"import numpy as np"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "d2c55c1c-c588-40e5-ae01-c476a1c2616e",
"metadata": {},
Expand All @@ -110,6 +111,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "c70d852d-470d-45d1-adc7-a6a23feb5854",
"metadata": {},
Expand Down Expand Up @@ -138,13 +140,13 @@
"metadata": {},
"outputs": [],
"source": [
"# Plot intensities\n",
"# Plot the intensities\n",
"import matplotlib.colors as colors\n",
"\n",
"fig = plt.figure()\n",
"plt.imshow(intensities, norm=colors.LogNorm(), aspect=\"auto\")\n",
"\n",
"# Note: with latest version of matplotlib:\n",
" \n",
"# Note: with latest version of matplotlib: \n",
"# plt.imshow(intensities, norm=\"log\", aspect=\"auto\")"
]
},
Expand All @@ -162,6 +164,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "de96e9c6-5421-4d74-8b4d-9da68716cac2",
"metadata": {},
Expand All @@ -170,6 +173,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "4961fad5-fe0b-4d6e-9663-228c630fb60c",
"metadata": {
Expand Down Expand Up @@ -207,14 +211,16 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "9c2026c6-a04a-4ad5-92a4-5e473ce3dd3b",
"metadata": {},
"source": [
"Note: This is not meaningful, the buffer and sample+buffer cases should be separated."
"Note: This is not meaningful, the buffer and sample + buffer cases should be separated."
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "3adff22d-4835-4f3c-935f-a06f71372e01",
"metadata": {},
Expand Down Expand Up @@ -242,12 +248,13 @@
"outputs": [],
"source": [
"fig = plt.figure()\n",
"plt.plot(frames_intensities)\n",
"plt.plot(intensities_per_frame)\n",
"plt.xlabel(\"Frame ID\")\n",
"plt.ylabel(\"I\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "37949d27-0a36-488b-b230-df7765a6f286",
"metadata": {},
Expand All @@ -256,15 +263,16 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "de3ad40e-97b1-4ef9-aae7-168117aad0a0",
"metadata": {
"tags": []
},
"source": [
"### Separate sample+buffer from buffer only\n",
"### Separate sample + buffer from buffer only\n",
"\n",
"Select buffer and sample+buffer intensities by using a threshold over `intensities_summed`"
"Select buffer and sample + buffer intensities by using a threshold over `intensities_summed`."
]
},
{
Expand Down Expand Up @@ -298,11 +306,12 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "51fa7126-33b6-4fb2-8fa9-0c5b427325ba",
"metadata": {},
"source": [
"### Average sample+buffer and buffer intensities\n",
"### Average sample + buffer and buffer intensities\n",
"\n",
"Compute the average of azimuthal integrations of `buffer` for each `q`."
]
Expand All @@ -318,6 +327,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "93b16a5e-466a-4a7f-8da5-6b591bf6c3ce",
"metadata": {},
Expand All @@ -344,13 +354,14 @@
"source": [
"fig = plt.figure()\n",
"plt.plot(q, buffer_mean, 'black', q, sample_buffer_mean, 'red')\n",
"plt.title(\"buffer and sample+buffer average\")\n",
"plt.title(\"buffer and sample + buffer average\")\n",
"plt.xlabel(\"q\")\n",
"plt.ylabel(\"I\")\n",
"plt.yscale(\"log\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "13476124-7e14-4a6a-a49f-1f3b02dd1a6f",
"metadata": {},
Expand Down Expand Up @@ -383,6 +394,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "dbc81e5b-19b4-4aac-8648-7e8b67f3c9e0",
"metadata": {},
Expand All @@ -395,35 +407,35 @@
"```python\n",
"# Part I\n",
"\n",
"import numpy\n",
"import numpy as np\n",
"\n",
"# Load data\n",
"intensities = numpy.load(\"intensities.npy\")\n",
"q = numpy.loadtxt(\"q.txt\")\n",
"intensities = np.load(\"intensities.npy\")\n",
"q = np.loadtxt(\"q.txt\")\n",
"\n",
"# Part II\n",
"\n",
"# Average of all azimuthal integrations\n",
"intensities_mean = numpy.mean(intensities, axis=0)\n",
"intensities_mean = np.mean(intensities, axis=0)\n",
"\n",
"# Summed intensity of each azimuthal integration\n",
"frames_intensities = numpy.sum(intensities, axis=1)\n",
"intensities_per_frame = np.sum(intensities, axis=1)\n",
"\n",
"# Part III\n",
"\n",
"# Separate sample+buffer from buffer only\n",
"# Separate sample + buffer from buffer only\n",
"# 1. with thresholds\n",
"buffer_mask = frames_intensities < 32500\n",
"buffer_mask = intensities_per_frame < 32500\n",
"buffer = intensities[buffer_mask]\n",
"sample_buffer_mask = frames_intensities > 33000\n",
"sample_buffer = intensities[sample_buffer_mask]\n",
"# 2. With slicing\n",
"buffer = intensities[:200]\n",
"sample_buffer = intensities[270:340]\n",
"\n",
"# Average sample+buffer and buffer intensities\n",
"buffer_mean = numpy.mean(buffer, axis=0)\n",
"sample_buffer_mean = numpy.mean(sample_buffer, axis=0)\n",
"# Average sample + buffer and buffer intensities\n",
"buffer_mean = np.mean(buffer, axis=0)\n",
"sample_buffer_mean = np.mean(sample_buffer, axis=0)\n",
"\n",
"# Remove buffer background\n",
"sample = sample_buffer_mean - buffer_mean\n",
Expand All @@ -434,22 +446,8 @@
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
"name": "python"
}
},
"nbformat": 4,
Expand Down
4 changes: 2 additions & 2 deletions python/numpy/exercicesolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def ex4_2_alt():

def ex5_inefficient_fill(height=1000, width=1000):
"""Inefficient fill using 2 for loops"""
data = numpy.zeros((height, width), dtype=numpy.float)
data = numpy.zeros((height, width), dtype=float)
for row in range(int(height)):
for col in range(int(width)):
data[row, col] = numpy.cos(row) * numpy.sin(col)
Expand All @@ -82,7 +82,7 @@ def ex5_naive_fill(height=1000, width=1000):
"""Fill using 2 for loops but pre-computing sin and cos"""
width_sin = numpy.sin(numpy.arange(width))
height_cos = numpy.cos(numpy.arange(height))
data = numpy.zeros((height, width), numpy.float)
data = numpy.zeros((height, width), dtype=numpy.float64)
for row in range(int(height)):
for col in range(int(width)):
data[row, col] = height_cos[row] * width_sin[col]
Expand Down
Loading

0 comments on commit f6eec40

Please sign in to comment.