Created
June 24, 2020 14:49
-
-
Save fepegar/89f2472f2d1f5bc0ec12068bbd030072 to your computer and use it in GitHub Desktop.
Functions for experimental TorchIO notebooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
def fourier_transform(array: np.ndarray): | |
transformed = np.fft.fftn(array) | |
fshift = np.fft.fftshift(transformed) | |
return fshift | |
def inv_fourier_transform(fshift: np.ndarray): | |
f_ishift = np.fft.ifftshift(fshift) | |
img_back = np.fft.ifftn(f_ishift) | |
return img_back | |
def get_1d_space(N, dt): | |
""" | |
Return a shifted linspace with N points and step dt. | |
The samples are shifted dt/2.""" | |
n = N/2 | |
t_min = -n * dt | |
t_max = (n-1) * dt | |
t = np.linspace(t_min, t_max, num=N) | |
return t, t_min, t_max | |
def plotx(x, f): | |
plt.plot(x, np.real(f)) | |
plt.plot(x, np.imag(f)) | |
plt.xlabel('$x~[mm]$') | |
def plotk(k, F): | |
plt.plot(k, np.real(F)) | |
plt.plot(k, np.imag(F)) | |
plt.xlabel('$k~[mm^{-1}]$') | |
def imshow(image, fourier=False, axis=None, extent=None, | |
labels=None, title=None, v=None): | |
if fourier: | |
np.seterr(divide='ignore') | |
log_image = np.log(np.abs(image)) | |
log_image[log_image == -np.inf] = 0 | |
image = log_image | |
if axis is None: | |
fig = plt.figure() | |
axis = fig.gca() | |
if labels is not None: | |
axis.set_xlabel(labels[0]) | |
axis.set_ylabel(labels[1]) | |
if title is not None: | |
axis.set_title(title) | |
return axis.imshow(image, cmap='gray', extent=extent, clim=v) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment