def test_complex_abs(shape): shape = shape + [2] x = create_input(shape) out_torch = fastmri.complex_abs(x).numpy() input_numpy = transforms.tensor_to_complex_np(x) out_numpy = np.abs(input_numpy) assert np.allclose(out_torch, out_numpy)
def test_ifft2(shape): shape = shape + [2] x = create_input(shape) out_torch = fastmri.ifft2c(x).numpy() out_torch = out_torch[..., 0] + 1j * out_torch[..., 1] input_numpy = transforms.tensor_to_complex_np(x) input_numpy = np.fft.ifftshift(input_numpy, (-2, -1)) out_numpy = np.fft.ifft2(input_numpy, norm="ortho") out_numpy = np.fft.fftshift(out_numpy, (-2, -1)) assert np.allclose(out_torch, out_numpy)
volume_kspace = orig['kspace'][()] kspace_list = [] reconstruction_list = [] total_slices = volume_kspace.shape[0] for i in range(total_slices // 2 - num_middle_slices // 2, total_slices // 2 + num_middle_slices // 2 + 1): slice_kspace = volume_kspace[i] slice_kspace2 = to_tensor( slice_kspace) # Convert from numpy array to pytorch tensor kspace_crop = complex_center_crop(slice_kspace2, (size, size)) ift = fastmri.ifft2c( kspace_crop ) # Apply Inverse Fourier Transform to get the complex image reconstruction = fastmri.complex_abs(ift) kspace_list.append(tensor_to_complex_np(kspace_crop)) reconstruction_list.append(reconstruction) stacked_kspace = np.stack(kspace_list) stacked_reconstruction_esc = np.stack(reconstruction_list) dest['kspace'] = stacked_kspace dest['reconstruction_esc'] = stacked_reconstruction_esc dest['ismrmrd_header'] = orig['ismrmrd_header'][()] dest.attrs['norm'] = np.linalg.norm(stacked_reconstruction_esc) dest.attrs['max'] = np.max(stacked_reconstruction_esc) dest.close() orig.close()