瀏覽代碼

notebooks splitted and renamed

Gert-Ludwig Ingold 5 年之前
父節點
當前提交
d433e99f2d
共有 4 個文件被更改,包括 475 次插入312 次删除
  1. 89 0
      notebooks/0_intro_scipy.ipynb
  2. 377 0
      notebooks/1_tgv_stats.ipynb
  3. 7 309
      notebooks/scipy-intro.ipynb
  4. 2 3
      notebooks/chain.ipynb

+ 89 - 0
notebooks/0_intro_scipy.ipynb

@@ -0,0 +1,89 @@
+{
+ "cells": [
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "# Introduction to SciPy\n",
+    "Tutorial at EuroSciPy 2019, Bilbao"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "## Getting started"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "Let us see whether we have everything we need:"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import numpy as np\n",
+    "np.__version__"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import scipy\n",
+    "scipy.__version__"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import matplotlib\n",
+    "matplotlib.__version__"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "## Table of contents\n",
+    "\n",
+    "1. [Statistical analysis – Gyroscope data taken in a TGV](1_tgv_stats.ipynb)\n",
+    "1. [Signal analysis – Rocking motion of a TGV](2_tgv_signal.ipynb)\n",
+    "1. [Optimization – The hanging chain      \n",
+    "    Solving a set of ordinary differential equations – The falling chain](3_chain.ipynb)"
+   ]
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Python 3",
+   "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.7.4"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}

+ 377 - 0
notebooks/1_tgv_stats.ipynb

@@ -0,0 +1,377 @@
+{
+ "cells": [
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "# Introduction to SciPy\n",
+    "Tutorial at EuroSciPy 2019, Bilbao\n",
+    "## 1. Statistical analysis – Gyroscope data taken in a TGV"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import numpy as np\n",
+    "from scipy import optimize, stats\n",
+    "%matplotlib notebook\n",
+    "import matplotlib.pyplot as plt"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "### Importing the data"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "Our data are stored in a compressed file `TGV_data.csv.bz2`. Each row of the uncompressed file contains entries separated by commas and the first row contains labels explaining the content of the respective column.\n",
+    "\n",
+    "NumPy provides rather universal tools to import data from files into NumPy arrays. We will use `genfromtxt` which allows to deal with `bz2` compressed data files and also handles the labels in the first row of the data."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "data = np.genfromtxt('data/TGV_data.csv.bz2', delimiter=',', names=True)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "There are five columns identified by names:"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "data.dtype.names"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "time = data['Time_s']\n",
+    "omega_x = data['Gyroscope_x_rads']\n",
+    "omega_y = data['Gyroscope_y_rads']"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "Let us first get an idea of the data."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "plt.plot(time, omega_x)\n",
+    "plt.plot(time, omega_y)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "### Statistical analysis"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "We use the data for $\\omega_x$ to demonstrate some statistical analysis. Let us first take a look at a histogram of the data."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "n, bins = np.histogram(omega_x, bins=100, density=True)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "Because we have set `density=True`, the data can be considered as a normalized probability distribution."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "np.sum(n)*(bins[1]-bins[0])"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "The array `bins` contains the edges of the bins. To plot the histogram, we need their centers."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "bincenters = 0.5*(bins[1:]+bins[:-1])"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "plt.plot(bincenters, n)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "With SciPy we can easily determine some statistical characteristics from the original data:"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "description = stats.describe(omega_x, ddof=False)\n",
+    "description"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "We can compare our histogram with a Gaussian for the mean and variance just obtained."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "loc = description.mean\n",
+    "scale = np.sqrt(description.variance)\n",
+    "loc, scale"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "x = np.linspace(-0.1, 0.1, 200)\n",
+    "plt.plot(x, stats.norm.pdf(x, loc, scale))"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "A maximum likelihood fit with a Gaussian will yield the same result."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "stats.norm.fit(omega_x)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "How can we fit the histogram to a Gaussian? Use nonlinear least-squares curve fitting."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "def gaussian(x, loc, scale):\n",
+    "    return stats.norm.pdf(x, loc, scale)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "popt, pcov = optimize.curve_fit(gaussian, bincenters, n)\n",
+    "popt"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "plt.plot(x, gaussian(x, *popt))"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "How likely is it that our data follow a normal distribution?"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "stats.normaltest(omega_x)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "Normally distributed data"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "normdata = stats.norm.rvs(1, 0.2, 5000)\n",
+    "plt.plot(normdata)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "n, bins = np.histogram(normdata, bins=100, density=True)\n",
+    "bincenters = 0.5*(bins[1:]+bins[:-1])\n",
+    "plt.plot(bincenters, n)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "x = np.linspace(0, 1.6, 200)\n",
+    "plt.plot(x, stats.norm.pdf(x, *stats.norm.fit(normdata)))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "popt, pcov = optimize.curve_fit(gaussian, bincenters, n)\n",
+    "popt"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "plt.plot(x, gaussian(x, *popt))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "stats.normaltest(normdata)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "Distribution with skewness"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "for a in range(-4, 5, 2):\n",
+    "    x = np.linspace(stats.skewnorm.ppf(0.001, a),\n",
+    "                    stats.skewnorm.ppf(0.999, a), 100)\n",
+    "    plt.plot(x, stats.skewnorm.pdf(x, a))"
+   ]
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Python 3",
+   "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.7.4"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}

+ 7 - 309
notebooks/scipy-intro.ipynb

@@ -5,60 +5,22 @@
    "metadata": {},
    "source": [
     "# Introduction to SciPy\n",
-    "Tutorial at EuroSciPy 2019, Bilbao"
-   ]
-  },
-  {
-   "cell_type": "markdown",
-   "metadata": {},
-   "source": [
-    "## Getting started"
-   ]
-  },
-  {
-   "cell_type": "markdown",
-   "metadata": {},
-   "source": [
-    "We first import a number of packages."
+    "Tutorial at EuroSciPy 2019, Bilbao\n",
+    "## 2. Signal analysis – Rocking motion of a TGV"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": null,
+   "execution_count": 1,
    "metadata": {},
    "outputs": [],
    "source": [
     "import numpy as np\n",
-    "np.__version__"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "import scipy\n",
-    "scipy.__version__"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "from scipy import fftpack, optimize, signal, stats"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
+    "from scipy import fftpack, signal\n",
     "%matplotlib notebook\n",
-    "import matplotlib.pyplot as plt"
+    "import matplotlib.pyplot as plt\n",
+    "from matplotlib import cm\n",
+    "from mpl_toolkits.mplot3d import Axes3D"
    ]
   },
   {
@@ -143,268 +105,6 @@
    "cell_type": "markdown",
    "metadata": {},
    "source": [
-    "### Statistical analysis"
-   ]
-  },
-  {
-   "cell_type": "markdown",
-   "metadata": {},
-   "source": [
-    "We use the data for $\\omega_x$ to demonstrate some statistical analysis. Let us first take a look at a histogram of the data."
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "n, bins = np.histogram(omega_x, bins=100, density=True)"
-   ]
-  },
-  {
-   "cell_type": "markdown",
-   "metadata": {},
-   "source": [
-    "Because we have set `density=True`, the data can be considered as a normalized probability distribution."
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "np.sum(n)*(bins[1]-bins[0])"
-   ]
-  },
-  {
-   "cell_type": "markdown",
-   "metadata": {},
-   "source": [
-    "The array `bins` contains the edges of the bins. To plot the histogram, we need their centers."
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "bincenters = 0.5*(bins[1:]+bins[:-1])"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "plt.plot(bincenters, n)"
-   ]
-  },
-  {
-   "cell_type": "markdown",
-   "metadata": {},
-   "source": [
-    "With SciPy we can easily determine some statistical characteristics from the original data:"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "description = stats.describe(omega_x, ddof=False)\n",
-    "description"
-   ]
-  },
-  {
-   "cell_type": "markdown",
-   "metadata": {},
-   "source": [
-    "We can compare our histogram with a Gaussian for the mean and variance just obtained."
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "loc = description.mean\n",
-    "scale = np.sqrt(description.variance)\n",
-    "loc, scale"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "x = np.linspace(-0.1, 0.1, 200)\n",
-    "plt.plot(x, stats.norm.pdf(x, loc, scale))"
-   ]
-  },
-  {
-   "cell_type": "markdown",
-   "metadata": {},
-   "source": [
-    "A maximum likelihood fit with a Gaussian will yield the same result."
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "stats.norm.fit(omega_x)"
-   ]
-  },
-  {
-   "cell_type": "markdown",
-   "metadata": {},
-   "source": [
-    "How can we fit the histogram to a Gaussian? Use nonlinear least-squares curve fitting."
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "def gaussian(x, loc, scale):\n",
-    "    return stats.norm.pdf(x, loc, scale)"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "popt, pcov = optimize.curve_fit(gaussian, bincenters, n)\n",
-    "popt"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "plt.plot(x, gaussian(x, *popt))"
-   ]
-  },
-  {
-   "cell_type": "markdown",
-   "metadata": {},
-   "source": [
-    "How likely is it that our data follow a normal distribution?"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "stats.normaltest(omega_x)"
-   ]
-  },
-  {
-   "cell_type": "markdown",
-   "metadata": {},
-   "source": [
-    "Normally distributed data"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "normdata = stats.norm.rvs(1, 0.2, 5000)\n",
-    "plt.plot(normdata)"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "n, bins = np.histogram(normdata, bins=100, density=True)\n",
-    "bincenters = 0.5*(bins[1:]+bins[:-1])\n",
-    "plt.plot(bincenters, n)"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "x = np.linspace(0, 1.6, 200)\n",
-    "plt.plot(x, stats.norm.pdf(x, *stats.norm.fit(normdata)))"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "popt, pcov = optimize.curve_fit(gaussian, bincenters, n)\n",
-    "popt"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "plt.plot(x, gaussian(x, *popt))"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "stats.normaltest(normdata)"
-   ]
-  },
-  {
-   "cell_type": "markdown",
-   "metadata": {},
-   "source": [
-    "Distribution with skewness"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "for a in range(-4, 5, 2):\n",
-    "    x = np.linspace(stats.skewnorm.ppf(0.001, a),\n",
-    "                    stats.skewnorm.ppf(0.999, a), 100)\n",
-    "    plt.plot(x, stats.skewnorm.pdf(x, a))"
-   ]
-  },
-  {
-   "cell_type": "markdown",
-   "metadata": {},
-   "source": [
     "### Spectral properties"
    ]
   },
@@ -687,8 +387,6 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "from matplotlib import cm\n",
-    "from mpl_toolkits.mplot3d import Axes3D\n",
     "fig = plt.figure()\n",
     "ax = Axes3D(fig)\n",
     "ax.plot_surface(freq[:, np.newaxis], time, Sxx, cmap=cm.viridis)"

+ 2 - 3
notebooks/chain.ipynb

@@ -6,8 +6,7 @@
    "source": [
     "# Introduction to SciPy\n",
     "Tutorial at EuroSciPy 2019, Bilbao\n",
-    "\n",
-    "## Optimization – The hanging chain\n",
+    "## 3.1. Optimization – The hanging chain\n",
     "![chain link](images/chainlink.png)"
    ]
   },
@@ -99,7 +98,7 @@
    "cell_type": "markdown",
    "metadata": {},
    "source": [
-    "## Solving a set of ordinary differential equations – The falling chain"
+    "## 3.2. Solving a set of ordinary differential equations – The falling chain"
    ]
   },
   {