Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pytreat-practicals-2020
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FSL
pytreat-practicals-2020
Commits
6e5f1b9a
Commit
6e5f1b9a
authored
5 years ago
by
Mark Chiew
Browse files
Options
Downloads
Patches
Plain Diff
Fixed weird bug w.r.t. ipynbs created in VS Code, switched ode to odeint
parent
d74a8c2c
No related branches found
No related tags found
1 merge request
!26
Fixed weird bug w.r.t. ipynbs created in VS Code, switched ode to odeint
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
talks/matlab_vs_python/bloch/bloch.ipynb
+20
-43
20 additions, 43 deletions
talks/matlab_vs_python/bloch/bloch.ipynb
talks/matlab_vs_python/partial_fourier/partial_fourier.ipynb
+2
-14
2 additions, 14 deletions
talks/matlab_vs_python/partial_fourier/partial_fourier.ipynb
with
22 additions
and
57 deletions
talks/matlab_vs_python/bloch/bloch.ipynb
+
20
−
43
View file @
6e5f1b9a
...
@@ -2,9 +2,7 @@
...
@@ -2,9 +2,7 @@
"cells": [
"cells": [
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [],
"source": [
"source": [
"Imports"
"Imports"
]
]
...
@@ -16,15 +14,13 @@
...
@@ -16,15 +14,13 @@
"outputs": [],
"outputs": [],
"source": [
"source": [
"import numpy as np\n",
"import numpy as np\n",
"from scipy.integrate import ode\n",
"from scipy.integrate import ode
, solve_ivp
\n",
"import matplotlib.pyplot as plt"
"import matplotlib.pyplot as plt"
]
]
},
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [],
"source": [
"source": [
"# Define the Bloch equation\n",
"# Define the Bloch equation\n",
"\n",
"\n",
...
@@ -42,7 +38,7 @@
...
@@ -42,7 +38,7 @@
"outputs": [],
"outputs": [],
"source": [
"source": [
"# define bloch equation\n",
"# 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",
" # get effective B-field at time t\n",
" B = B_eff(t)\n",
" B = B_eff(t)\n",
" # cross product of M and B, add T1 and T2 relaxation terms\n",
" # cross product of M and B, add T1 and T2 relaxation terms\n",
...
@@ -55,9 +51,7 @@
...
@@ -55,9 +51,7 @@
},
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [],
"source": [
"source": [
"# Define the pulse sequence \n",
"# Define the pulse sequence \n",
"\n",
"\n",
...
@@ -102,9 +96,7 @@
...
@@ -102,9 +96,7 @@
},
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [],
"source": [
"source": [
"# Plot the pulse sequence\n",
"# Plot the pulse sequence\n",
"\n",
"\n",
...
@@ -139,18 +131,15 @@
...
@@ -139,18 +131,15 @@
},
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [],
"source": [
"source": [
"# Integrate ODE \n",
"# Integrate ODE \n",
"\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",
"\n",
"In this section:\n",
"In this section:\n",
"- list of arrays\n",
"- ode solvers\n",
"- ode solvers\n",
"- l
ist appending
"
"- l
ambdas (anonymous functions)
"
]
]
},
},
{
{
...
@@ -160,42 +149,30 @@
...
@@ -160,42 +149,30 @@
"outputs": [],
"outputs": [],
"source": [
"source": [
"# Set the initial conditions\n",
"# Set the initial conditions\n",
"# time (t) = 0\n",
"# equilibrium magnetization (M) = (0, 0, 1)\n",
"# equilibrium magnetization (M) = (0, 0, 1)\n",
"t = [0]\n",
"M = [0, 0, 1]\n",
"M = [np.array([0, 0, 1])]\n",
"\n",
"# Set integrator time-step\n",
"dt= 0.005\n",
"\n",
"\n",
"# Set
up ODE integrator object
\n",
"# Set
time interval for integration
\n",
"
r
=
ode(bloch_ode)
\n",
"
t
=
[0, 5]
\n",
"\n",
"\n",
"# Choose the integrator method\n",
"# Set max step size\n",
"r.set_integrator('dopri5')\n",
"dt = 0.005\n",
"\n",
"# Pass in initial values\n",
"r.set_initial_value(M[0], t[0])\n",
"\n",
"\n",
"# Set T1 and T2\n",
"# Set T1 and T2\n",
"T1, T2 = 1500, 50\n",
"T1, T2 = 1500, 50\n",
"r.set_f_params(T1, T2)\n",
"\n",
"\n",
"# Integrate by looping over time, moving dt by step size each iteration\n",
"# Integrate ODE\n",
"# Append new time point and Magnetisation vector at every step to t and M\n",
"# In Scipy 1.2.0, the first argument to solve_ivp must be a function that takes exactly 2 arguments\n",
"while r.successful() and r.t < 5:\n",
"sol = solve_ivp(lambda t, M : bloch(t, M, T1, T2), t, M, max_step=dt)\n",
" t.append(r.t + dt)\n",
" M.append(r.integrate(t[-1]))\n",
"\n",
"\n",
"# Convert M to 2-D numpy array from list of arrays\n",
"# Grab output\n",
"M = np.array(M)"
"t = sol.t\n",
"M = sol.y"
]
]
},
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [],
"source": [
"source": [
"# Plot Results\n",
"# Plot Results\n",
"\n",
"\n",
...
@@ -213,9 +190,9 @@
...
@@ -213,9 +190,9 @@
"_, ax = plt.subplots(figsize=(12,12))\n",
"_, ax = plt.subplots(figsize=(12,12))\n",
"\n",
"\n",
"# Plot x, y and z components of Magnetisation\n",
"# Plot x, y and z components of Magnetisation\n",
"ax.plot(t, M[
:,0
], label='Mx')\n",
"ax.plot(t, M[
0,:
], label='Mx')\n",
"ax.plot(t, M[
:,1
], label='My')\n",
"ax.plot(t, M[
1,:
], label='My')\n",
"ax.plot(t, M[
:,2
], label='Mz')\n",
"ax.plot(t, M[
2,:
], label='Mz')\n",
"\n",
"\n",
"# Add legend and grid\n",
"# Add legend and grid\n",
"ax.legend()\n",
"ax.legend()\n",
...
@@ -239,9 +216,9 @@
...
@@ -239,9 +216,9 @@
"name": "python",
"name": "python",
"nbconvert_exporter": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"pygments_lexer": "ipython3",
"version": "3.7.3
-final
"
"version": "3.7.3"
}
}
},
},
"nbformat": 4,
"nbformat": 4,
"nbformat_minor": 4
"nbformat_minor": 4
}
}
\ No newline at end of file
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Imports
Imports
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
import
numpy
as
np
import
numpy
as
np
from
scipy.integrate
import
ode
from
scipy.integrate
import
ode
,
solve_ivp
import
matplotlib.pyplot
as
plt
import
matplotlib.pyplot
as
plt
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
# Define the Bloch equation
# Define the Bloch equation
$$
\f
rac{d
\v
ec{M}}{dt} =
\v
ec{M}
\t
imes
\v
ec{B} -
\f
rac{M_x + M_y}{T2} -
\f
rac{M_z - M_0}{T1}$$
$$
\f
rac{d
\v
ec{M}}{dt} =
\v
ec{M}
\t
imes
\v
ec{B} -
\f
rac{M_x + M_y}{T2} -
\f
rac{M_z - M_0}{T1}$$
In this section:
In this section:
-
define a function
-
define a function
-
numpy functions like np.cross
-
numpy functions like np.cross
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
# define bloch equation
# define bloch equation
def
bloch
_ode
(
t
,
M
,
T1
,
T2
):
def
bloch
(
t
,
M
,
T1
,
T2
):
# get effective B-field at time t
# get effective B-field at time t
B
=
B_eff
(
t
)
B
=
B_eff
(
t
)
# cross product of M and B, add T1 and T2 relaxation terms
# cross product of M and B, add T1 and T2 relaxation terms
return
np
.
array
([
M
[
1
]
*
B
[
2
]
-
M
[
2
]
*
B
[
1
]
-
M
[
0
]
/
T2
,
return
np
.
array
([
M
[
1
]
*
B
[
2
]
-
M
[
2
]
*
B
[
1
]
-
M
[
0
]
/
T2
,
M
[
2
]
*
B
[
0
]
-
M
[
0
]
*
B
[
2
]
-
M
[
1
]
/
T2
,
M
[
2
]
*
B
[
0
]
-
M
[
0
]
*
B
[
2
]
-
M
[
1
]
/
T2
,
M
[
0
]
*
B
[
1
]
-
M
[
1
]
*
B
[
0
]
-
(
M
[
2
]
-
1
)
/
T1
])
M
[
0
]
*
B
[
1
]
-
M
[
1
]
*
B
[
0
]
-
(
M
[
2
]
-
1
)
/
T1
])
# alternatively
# alternatively
#return np.cross(M,B) - np.array([M[0]/T2, M[1]/T2, (M[2]-1)/T1])
#return np.cross(M,B) - np.array([M[0]/T2, M[1]/T2, (M[2]-1)/T1])
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
# Define the pulse sequence
# Define the pulse sequence
We work in the rotating frame, so we only need the amplitude envelope of the RF pulse
We work in the rotating frame, so we only need the amplitude envelope of the RF pulse
Typically, B1 excitation fields point in the x- and/or y-directions
Typically, B1 excitation fields point in the x- and/or y-directions
Gradient fields point in the z-direction
Gradient fields point in the z-direction
In this simple example, a simple sinc-pulse excitation pulse is applied for 1 ms along the x-axis
In this simple example, a simple sinc-pulse excitation pulse is applied for 1 ms along the x-axis
then a gradient is turned on for 1.5 ms after that.
then a gradient is turned on for 1.5 ms after that.
In this section:
In this section:
-
constants such as np.pi
-
constants such as np.pi
-
functions like np.sinc
-
functions like np.sinc
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
# define effective B-field
# define effective B-field
def
B_eff
(
t
):
def
B_eff
(
t
):
# Do nothing for 0.25 ms
# Do nothing for 0.25 ms
if
t
<
0.25
:
if
t
<
0.25
:
return
np
.
array
([
0
,
0
,
0
])
return
np
.
array
([
0
,
0
,
0
])
# Sinc RF along x-axis and slice-select gradient on for 1.00 ms
# Sinc RF along x-axis and slice-select gradient on for 1.00 ms
elif
t
<
1.25
:
elif
t
<
1.25
:
return
np
.
array
([
np
.
pi
*
np
.
sinc
((
t
-
0.75
)
*
4
),
0
,
np
.
pi
])
return
np
.
array
([
np
.
pi
*
np
.
sinc
((
t
-
0.75
)
*
4
),
0
,
np
.
pi
])
# Do nothing for 0.25 ms
# Do nothing for 0.25 ms
elif
t
<
1.50
:
elif
t
<
1.50
:
return
np
.
array
([
0
,
0
,
0
])
return
np
.
array
([
0
,
0
,
0
])
# Slice refocusing gradient on for 1.50 ms
# Slice refocusing gradient on for 1.50 ms
# Half the area of the slice-select gradient lobe
# Half the area of the slice-select gradient lobe
elif
t
<
3.00
:
elif
t
<
3.00
:
return
np
.
array
([
0
,
0
,
-
(
1
/
3
)
*
np
.
pi
])
return
np
.
array
([
0
,
0
,
-
(
1
/
3
)
*
np
.
pi
])
# Pulse sequence finished
# Pulse sequence finished
else
:
else
:
return
np
.
array
([
0
,
0
,
0
])
return
np
.
array
([
0
,
0
,
0
])
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
# Plot the pulse sequence
# Plot the pulse sequence
In this section:
In this section:
-
unpacking return values
-
unpacking return values
-
unwanted return values
-
unwanted return values
-
list comprehension
-
list comprehension
-
basic plotting
-
basic plotting
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
# Create 2 vertical subplots
# Create 2 vertical subplots
_
,
ax
=
plt
.
subplots
(
2
,
1
,
figsize
=
(
12
,
12
))
_
,
ax
=
plt
.
subplots
(
2
,
1
,
figsize
=
(
12
,
12
))
# Get pulse sequence B-fields from 0 - 5 ms
# Get pulse sequence B-fields from 0 - 5 ms
pulseq
=
[
B_eff
(
t
)
for
t
in
np
.
linspace
(
0
,
5
,
1000
)]
pulseq
=
[
B_eff
(
t
)
for
t
in
np
.
linspace
(
0
,
5
,
1000
)]
pulseq
=
np
.
array
(
pulseq
)
pulseq
=
np
.
array
(
pulseq
)
# Plot RF
# Plot RF
ax
[
0
].
plot
(
pulseq
[:,
0
])
ax
[
0
].
plot
(
pulseq
[:,
0
])
ax
[
0
].
set_ylabel
(
'
B1
'
)
ax
[
0
].
set_ylabel
(
'
B1
'
)
# Plot gradient
# Plot gradient
ax
[
1
].
plot
(
pulseq
[:,
2
])
ax
[
1
].
plot
(
pulseq
[:,
2
])
ax
[
1
].
set_ylabel
(
'
Gradient
'
)
ax
[
1
].
set_ylabel
(
'
Gradient
'
)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
# Integrate ODE
# Integrate ODE
This uses a Runge-Kutta variant called the "Dormand-Prince method"
This uses a Runge-Kutta variant called the "Dormand-Prince method"
by default. Other ode integration methods are available.
In this section:
In this section:
-
list of arrays
-
ode solvers
-
ode solvers
-
l
ist appending
-
l
ambdas (anonymous functions)
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
# Set the initial conditions
# Set the initial conditions
# time (t) = 0
# equilibrium magnetization (M) = (0, 0, 1)
# equilibrium magnetization (M) = (0, 0, 1)
t
=
[
0
]
M
=
[
0
,
0
,
1
]
M
=
[
np
.
array
([
0
,
0
,
1
])]
# Set
integrator time-step
# Set
time interval for integration
dt
=
0.005
t
=
[
0
,
5
]
# Set up ODE integrator object
# Set max step size
r
=
ode
(
bloch_ode
)
dt
=
0.005
# Choose the integrator method
r
.
set_integrator
(
'
dopri5
'
)
# Pass in initial values
r
.
set_initial_value
(
M
[
0
],
t
[
0
])
# Set T1 and T2
# Set T1 and T2
T1
,
T2
=
1500
,
50
T1
,
T2
=
1500
,
50
r
.
set_f_params
(
T1
,
T2
)
# Integrate by looping over time, moving dt by step size each iteration
# Integrate ODE
# Append new time point and Magnetisation vector at every step to t and M
# In Scipy 1.2.0, the first argument to solve_ivp must be a function that takes exactly 2 arguments
while
r
.
successful
()
and
r
.
t
<
5
:
sol
=
solve_ivp
(
lambda
t
,
M
:
bloch
(
t
,
M
,
T1
,
T2
),
t
,
M
,
max_step
=
dt
)
t
.
append
(
r
.
t
+
dt
)
M
.
append
(
r
.
integrate
(
t
[
-
1
]))
# Convert M to 2-D numpy array from list of arrays
# Grab output
M
=
np
.
array
(
M
)
t
=
sol
.
t
M
=
sol
.
y
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
# Plot Results
# Plot Results
In this section:
In this section:
-
more plotting
-
more plotting
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
# Create single axis
# Create single axis
_
,
ax
=
plt
.
subplots
(
figsize
=
(
12
,
12
))
_
,
ax
=
plt
.
subplots
(
figsize
=
(
12
,
12
))
# Plot x, y and z components of Magnetisation
# Plot x, y and z components of Magnetisation
ax
.
plot
(
t
,
M
[
:,
0
],
label
=
'
Mx
'
)
ax
.
plot
(
t
,
M
[
0
,:
],
label
=
'
Mx
'
)
ax
.
plot
(
t
,
M
[
:,
1
],
label
=
'
My
'
)
ax
.
plot
(
t
,
M
[
1
,:
],
label
=
'
My
'
)
ax
.
plot
(
t
,
M
[
:,
2
],
label
=
'
Mz
'
)
ax
.
plot
(
t
,
M
[
2
,:
],
label
=
'
Mz
'
)
# Add legend and grid
# Add legend and grid
ax
.
legend
()
ax
.
legend
()
ax
.
grid
()
ax
.
grid
()
```
```
...
...
This diff is collapsed.
Click to expand it.
talks/matlab_vs_python/partial_fourier/partial_fourier.ipynb
+
2
−
14
View file @
6e5f1b9a
...
@@ -2,9 +2,7 @@
...
@@ -2,9 +2,7 @@
"cells": [
"cells": [
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [],
"source": [
"source": [
"Imports"
"Imports"
]
]
...
@@ -22,9 +20,7 @@
...
@@ -22,9 +20,7 @@
},
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [],
"source": [
"source": [
"# Load data\n",
"# Load data\n",
"\n",
"\n",
...
@@ -57,9 +53,7 @@
...
@@ -57,9 +53,7 @@
},
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [],
"source": [
"source": [
"# 6/8 Partial Fourier sampling\n",
"# 6/8 Partial Fourier sampling\n",
"\n",
"\n",
...
@@ -91,9 +85,7 @@
...
@@ -91,9 +85,7 @@
},
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [],
"source": [
"source": [
"# Estimate phase\n",
"# Estimate phase\n",
"\n",
"\n",
...
@@ -130,9 +122,7 @@
...
@@ -130,9 +122,7 @@
},
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [],
"source": [
"source": [
"# POCS reconstruction\n",
"# POCS reconstruction\n",
"\n",
"\n",
...
@@ -192,9 +182,7 @@
...
@@ -192,9 +182,7 @@
},
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [],
"source": [
"source": [
"# Display error and plot reconstruction\n",
"# Display error and plot reconstruction\n",
"\n",
"\n",
...
@@ -252,9 +240,9 @@
...
@@ -252,9 +240,9 @@
"name": "python",
"name": "python",
"nbconvert_exporter": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"pygments_lexer": "ipython3",
"version": "3.7.3
-final
"
"version": "3.7.3"
}
}
},
},
"nbformat": 4,
"nbformat": 4,
"nbformat_minor": 4
"nbformat_minor": 4
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment