NOTE: If you use ChatGPT or Github Copilot, please make sure that you can answer any questions about the code.
This is an interactive Jupyter Notebook. The notebook consists of markdown cells (like this) and code cells. To edit a code cell, you just click in it and start typing. To execute code you can either click the Run button, or use the keyboard shortcut Shift + Enter.
If you're running the lab using Google Colab, there is a button above to save a copy of the notebook to your Google Drive.
This lab is making use of the NumPy (tutorial) and PyPlot (tutorial) packages.
A few tips for numpy:
my_nparray[0]
is the first element.[start:stop:step]
notation, e.g. my_nparray[::2]
for every second element or my_nparray[0:20]
for the 20 first elements.my_kspace = kspace_res512_FOV24.copy()
, otherwise you might end up with unwanted results.np.zeros
, you need to specify that it's a complex datatype using dtype=np.complex64
np.log
may give undesired results for arrays containing zeros. You can use np.log(x+1)
for those instances.ij
or 0+ij
can be used to represent complex numbers. np.pi
can be used for pi.A few tips for matplotlib:
plt.imshow
to display images, and use the option cmap='gray'
to display them in black and white.plt.figure()
to create multiple plots or use fig, axes = plt.subplots(2,2)
to create a 2 by 2 grid.figsize=(20,20)
to make the plotting area larger, e.g. plt.figure(figsize=(20,20))
or fig, axes = plt.subplots(2,2, figsize=(20,20))
Finally, if you need to reset your workspace, run the first cell again!
!wget -nc https://raw.githubusercontent.com/fyrdahl/kspace-lab/master/data.h5
import numpy as np
import matplotlib.pyplot as plt
import h5py
from scipy.signal import convolve2d
with h5py.File('data.h5', 'r') as F:
kspace_res512_FOV24 = np.array(F['kspace_res512_FOV24'])
kspace_res512_FOV48 = np.array(F['kspace_res512_FOV48'])
fftc = lambda f: np.fft.fftshift(np.fft.fft(np.fft.ifftshift(f)))
ifftc = lambda F: np.fft.fftshift(np.fft.ifft(np.fft.ifftshift(F)))
fft2c = lambda f: np.fft.fftshift(np.fft.fft2(np.fft.ifftshift(f)))
ifft2c = lambda F: np.fft.fftshift(np.fft.ifft2(np.fft.ifftshift(F)))
Use kspace_res512_FOV24
np.log
command.fig, axes = plt.subplots(2,2)
axes[0][0].imshow(np.abs(np.log(kspace_res512_FOV24)), cmap='gray')
axes[0][0].set_title('Magnitude')
axes[0][0].set_axis_off()
# Plot phase here
axes[0][1].imshow(..., cmap='gray')
axes[0][1].set_title('Phase')
axes[0][1].set_axis_off()
# Plot real part here
axes[1][0].imshow(..., cmap='gray')
axes[1][0].set_title('Real')
axes[1][0].set_axis_off()
# Plot imaginary part here
axes[1][1].imshow(..., cmap='gray')
axes[1][1].set_title('Imaginary')
axes[1][1].set_axis_off()
plt.show()
Use kspace_res512_FOV48
Use kspace_res512_FOV24
should have a 512x256, 256x256, 64x64, 512x64 and 64x512 resolution. Display the magnitude of k-space and the magnitude image in the spatial domain. Remember that you should use np.log(x+1)
when taking the "log" a matrix containing zeros.
Use kspace_res512_FOV24
[kx,ky]
to 200000 for these elements: [100,100]
, [200,200]
, [250,250]
, [255,255]
, [257,257]
. Display the image for each k-space adjustment. Describe the artifact.Remember that numpy arrays are mutable. If you want to create an image for each k-space location, you need to use .copy()
to create copies of the original array.
Use kspace_res512_FOV24
Change the k-space phase without altering the magnitude. Remember that complex
numbers can be written as z = mag * np.exp(1j*phase)
Use kspace_res512_FOV24
To define a boxcar filter, you can use filter = np.ones((n,n))
, then use convolve2d(image,filter)
to perform a 2D convolution.