diff --git a/talks/matlab_vs_python/bloch/bloch.ipynb b/talks/matlab_vs_python/bloch/bloch.ipynb index 37d4b8cae68fadcdb3dd933e0555965660fbd74f..56cda45c327b0a55ba602c9d3c57c34158402076 100644 --- a/talks/matlab_vs_python/bloch/bloch.ipynb +++ b/talks/matlab_vs_python/bloch/bloch.ipynb @@ -2,9 +2,7 @@ "cells": [ { "cell_type": "markdown", - "execution_count": null, "metadata": {}, - "outputs": [], "source": [ "Imports" ] @@ -16,15 +14,13 @@ "outputs": [], "source": [ "import numpy as np\n", - "from scipy.integrate import ode\n", + "from scipy.integrate import ode, solve_ivp\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", - "execution_count": null, "metadata": {}, - "outputs": [], "source": [ "# Define the Bloch equation\n", "\n", @@ -42,7 +38,7 @@ "outputs": [], "source": [ "# define bloch equation\n", - "def bloch_ode(t, M, T1, T2):\n", + "def bloch(t, M, T1, T2):\n", " # get effective B-field at time t\n", " B = B_eff(t)\n", " # cross product of M and B, add T1 and T2 relaxation terms\n", @@ -55,9 +51,7 @@ }, { "cell_type": "markdown", - "execution_count": null, "metadata": {}, - "outputs": [], "source": [ "# Define the pulse sequence \n", "\n", @@ -102,9 +96,7 @@ }, { "cell_type": "markdown", - "execution_count": null, "metadata": {}, - "outputs": [], "source": [ "# Plot the pulse sequence\n", "\n", @@ -139,18 +131,15 @@ }, { "cell_type": "markdown", - "execution_count": null, "metadata": {}, - "outputs": [], "source": [ "# Integrate ODE \n", "\n", - "This uses a Runge-Kutta variant called the \"Dormand-Prince method\"\n", + "This uses a Runge-Kutta variant called the \"Dormand-Prince method\" by default. Other ode integration methods are available.\n", "\n", "In this section:\n", - "- list of arrays\n", "- ode solvers\n", - "- list appending" + "- lambdas (anonymous functions)" ] }, { @@ -160,42 +149,30 @@ "outputs": [], "source": [ "# Set the initial conditions\n", - "# time (t) = 0\n", "# equilibrium magnetization (M) = (0, 0, 1)\n", - "t = [0]\n", - "M = [np.array([0, 0, 1])]\n", - "\n", - "# Set integrator time-step\n", - "dt= 0.005\n", + "M = [0, 0, 1]\n", "\n", - "# Set up ODE integrator object\n", - "r = ode(bloch_ode)\n", + "# Set time interval for integration\n", + "t = [0, 5]\n", "\n", - "# Choose the integrator method\n", - "r.set_integrator('dopri5')\n", - "\n", - "# Pass in initial values\n", - "r.set_initial_value(M[0], t[0])\n", + "# Set max step size\n", + "dt = 0.005\n", "\n", "# Set T1 and T2\n", "T1, T2 = 1500, 50\n", - "r.set_f_params(T1, T2)\n", "\n", - "# Integrate by looping over time, moving dt by step size each iteration\n", - "# Append new time point and Magnetisation vector at every step to t and M\n", - "while r.successful() and r.t < 5:\n", - " t.append(r.t + dt)\n", - " M.append(r.integrate(t[-1]))\n", + "# Integrate ODE\n", + "# In Scipy 1.2.0, the first argument to solve_ivp must be a function that takes exactly 2 arguments\n", + "sol = solve_ivp(lambda t, M : bloch(t, M, T1, T2), t, M, max_step=dt)\n", "\n", - "# Convert M to 2-D numpy array from list of arrays\n", - "M = np.array(M)" + "# Grab output\n", + "t = sol.t\n", + "M = sol.y" ] }, { "cell_type": "markdown", - "execution_count": null, "metadata": {}, - "outputs": [], "source": [ "# Plot Results\n", "\n", @@ -213,9 +190,9 @@ "_, ax = plt.subplots(figsize=(12,12))\n", "\n", "# Plot x, y and z components of Magnetisation\n", - "ax.plot(t, M[:,0], label='Mx')\n", - "ax.plot(t, M[:,1], label='My')\n", - "ax.plot(t, M[:,2], label='Mz')\n", + "ax.plot(t, M[0,:], label='Mx')\n", + "ax.plot(t, M[1,:], label='My')\n", + "ax.plot(t, M[2,:], label='Mz')\n", "\n", "# Add legend and grid\n", "ax.legend()\n", @@ -239,9 +216,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3-final" + "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 4 -} \ No newline at end of file +} diff --git a/talks/matlab_vs_python/partial_fourier/partial_fourier.ipynb b/talks/matlab_vs_python/partial_fourier/partial_fourier.ipynb index 0a9189994cc053df6b50da17a975b394d3d94fc9..87f826f3d7d200f19fb6ea5241a6a349230f1361 100644 --- a/talks/matlab_vs_python/partial_fourier/partial_fourier.ipynb +++ b/talks/matlab_vs_python/partial_fourier/partial_fourier.ipynb @@ -2,9 +2,7 @@ "cells": [ { "cell_type": "markdown", - "execution_count": null, "metadata": {}, - "outputs": [], "source": [ "Imports" ] @@ -22,9 +20,7 @@ }, { "cell_type": "markdown", - "execution_count": null, "metadata": {}, - "outputs": [], "source": [ "# Load data\n", "\n", @@ -57,9 +53,7 @@ }, { "cell_type": "markdown", - "execution_count": null, "metadata": {}, - "outputs": [], "source": [ "# 6/8 Partial Fourier sampling\n", "\n", @@ -91,9 +85,7 @@ }, { "cell_type": "markdown", - "execution_count": null, "metadata": {}, - "outputs": [], "source": [ "# Estimate phase\n", "\n", @@ -130,9 +122,7 @@ }, { "cell_type": "markdown", - "execution_count": null, "metadata": {}, - "outputs": [], "source": [ "# POCS reconstruction\n", "\n", @@ -192,9 +182,7 @@ }, { "cell_type": "markdown", - "execution_count": null, "metadata": {}, - "outputs": [], "source": [ "# Display error and plot reconstruction\n", "\n", @@ -252,9 +240,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3-final" + "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 4 -} \ No newline at end of file +}