Example #1
0
def numsim_epi():
    ##
    # Original image: Shep-Logan Phantom
    ##
    N = 128
    FOV = 256e-3
    ph = resize(shepp_logan_phantom(), (N, N))  #.astype(complex)
    plt.imshow(np.abs(ph), cmap='gray')
    plt.title('Original phantom')
    plt.axis('off')
    plt.colorbar()
    plt.show()

    brain_mask = mask_by_threshold(ph)

    # Floodfill from point (0, 0)
    ph_holes = ~(flood_fill(brain_mask, (0, 0), 1).astype(int)) + 2
    mask = brain_mask + ph_holes

    ##
    # EPI k-space trajectory
    ##
    dt = 4e-6
    ktraj = ssEPI_2d(N, FOV)  # k-space trajectory

    plt.plot(ktraj[:, 0], ktraj[:, 1])
    plt.title('EPI trajectory')
    plt.show()

    Ta = ktraj.shape[0] * dt
    T = (np.arange(ktraj.shape[0]) * dt).reshape(ktraj.shape[0], 1)
    seq_params = {
        'N': N,
        'Npoints': ktraj.shape[0],
        'Nshots': 1,
        't_readout': Ta,
        't_vector': T
    }

    ##
    # Simulated field map
    ##
    # fmax_v = [50, 75, 100]  # Hz
    fmax_v = [100, 150, 200]

    i = 0
    or_corrupted = np.zeros((N, N, len(fmax_v)), dtype='complex')
    or_corrected_CPR = np.zeros((N, N, len(fmax_v)), dtype='complex')
    or_corrected_fsCPR = np.zeros((N, N, len(fmax_v)), dtype='complex')
    or_corrected_MFI = np.zeros((N, N, len(fmax_v)), dtype='complex')
    for fmax in fmax_v:
        # field_map = fieldmap_sim.realistic(np.abs(ph), mask, fmax)

        SL_smooth = gaussian(ph, sigma=3)
        field_map = cv2.normalize(SL_smooth, None, -fmax, fmax,
                                  cv2.NORM_MINMAX)
        field_map = np.round(field_map * mask)
        field_map[np.where(field_map == -0.0)] = 0

        plt.imshow(field_map, cmap='gray')
        plt.title('Field Map +/-' + str(fmax) + ' Hz')
        plt.colorbar()
        plt.axis('off')
        plt.show()

        ##
        # Corrupted images
        ##
        or_corrupted[:, :,
                     i], EPI_ksp = ORC.add_or_CPR(ph, ktraj, field_map, 'EPI',
                                                  seq_params)
        corrupt = (np.abs(or_corrupted[:, :, i]) -
                   np.abs(or_corrupted[..., i]).min()) / (
                       np.abs(or_corrupted[:, :, i]).max() -
                       np.abs(or_corrupted[..., i]).min())
        # plt.imshow(np.abs(or_corrupted[:,:,i]),cmap='gray')
        plt.imshow(corrupt, cmap='gray')
        plt.colorbar()
        plt.title('Corrupted Image')
        plt.axis('off')
        plt.show()

        ###
        # Corrected images
        ###
        or_corrected_CPR[:, :,
                         i] = ORC.correct_from_kdat('CPR', EPI_ksp, ktraj,
                                                    field_map, seq_params,
                                                    'EPI')
        or_corrected_fsCPR[:, :,
                           i] = ORC.correct_from_kdat('fsCPR', EPI_ksp, ktraj,
                                                      field_map, seq_params,
                                                      'EPI')
        or_corrected_MFI[:, :,
                         i] = ORC.correct_from_kdat('MFI', EPI_ksp, ktraj,
                                                    field_map, seq_params,
                                                    'EPI')

        # or_corrected_CPR[:, :, i] = ORC.CPR(or_corrupted[:, :, i], 'im', ktraj, field_map, 'EPI', seq_params)
        #
        # or_corrected_fsCPR[:, :, i] = ORC.fs_CPR(or_corrupted[:, :, i], 'im', ktraj, field_map, 2, 'EPI', seq_params)
        #
        # or_corrected_MFI[:, :, i] = ORC.MFI(or_corrupted[:, :, i], 'im', ktraj, field_map, 2, 'EPI', seq_params)

        i += 1

    ##
    # Plot
    ##
    im_stack = np.stack(
        (np.squeeze(or_corrupted), np.squeeze(or_corrected_CPR),
         np.squeeze(or_corrected_fsCPR), np.squeeze(or_corrected_MFI)))
    # np.save('im_stack.npy',im_stack)
    cols = ('Corrupted Image', 'CPR Correction', 'fs-CPR Correction',
            'MFI Correction')
    row_names = ('-/+ 100 Hz', '-/+ 150 Hz', '-/+ 200 Hz')
    plot_correction_results(im_stack, cols, row_names)
def numsim_cartesian():

    N = 192
    ph = resize(shepp_logan_phantom(), (N, N))

    ph = ph.astype(complex)
    plt.imshow(np.abs(ph), cmap='gray')
    plt.title('Original phantom')
    plt.axis('off')
    plt.colorbar()
    plt.show()

    brain_mask = mask_by_threshold(ph)

    # Floodfill from point (0, 0)
    ph_holes = ~(flood_fill(brain_mask, (0, 0), 1).astype(int)) + 2
    mask = brain_mask + ph_holes

    ##
    # Cartesian k-space trajectory
    ##
    dt = 10e-6  # grad raster time
    ktraj_cart = np.arange(0, N * dt, dt).reshape(1, N)
    ktraj_cart = np.tile(ktraj_cart, (N, 1))
    plt.imshow(ktraj_cart, cmap='gray')
    plt.title('Cartesian trajectory')
    plt.show()

    ##
    # Simulated field map
    ##
    fmax_v = [1600, 3200, 4800]  # Hz correspontig to 25, 50 and 75 ppm at 3T
    i = 0

    or_corrupted = np.zeros((N, N, len(fmax_v)), dtype='complex')
    or_corrected_CPR = np.zeros((N, N, len(fmax_v)), dtype='complex')
    or_corrected_fsCPR = np.zeros((N, N, len(fmax_v)), dtype='complex')
    or_corrected_MFI = np.zeros((N, N, len(fmax_v)), dtype='complex')
    for fmax in fmax_v:
        field_map = fieldmap_sim.realistic(np.abs(ph), mask, fmax)

        plt.imshow(field_map, cmap='gray')
        plt.title('Field Map')
        plt.colorbar()
        plt.axis('off')
        plt.show()

        ##
        # Corrupted images
        ##
        or_corrupted[:, :, i], _ = ORC.add_or_CPR(ph, ktraj_cart, field_map)
        corrupt = (np.abs(or_corrupted[:, :, i]) -
                   np.abs(or_corrupted[..., i]).min()) / (
                       np.abs(or_corrupted[:, :, i]).max() -
                       np.abs(or_corrupted[..., i]).min())
        #plt.imshow(np.abs(or_corrupted[:,:,i]),cmap='gray')
        plt.imshow(corrupt, cmap='gray')
        plt.colorbar()
        plt.title('Corrupted Image')
        plt.axis('off')
        plt.show()

        ###
        # Corrected images
        ###
        or_corrected_CPR[:, :, i] = ORC.CPR(or_corrupted[:, :, i], 'im',
                                            ktraj_cart, field_map)
        or_corrected_fsCPR[:, :, i] = ORC.fs_CPR(or_corrupted[:, :, i], 'im',
                                                 ktraj_cart, field_map, 2)
        or_corrected_MFI[:, :, i] = ORC.MFI(or_corrupted[:, :, i], 'im',
                                            ktraj_cart, field_map, 2)
        i += 1


##
# Plot
##
    im_stack = np.stack(
        (np.squeeze(or_corrupted), np.squeeze(or_corrected_CPR),
         np.squeeze(or_corrected_fsCPR), np.squeeze(or_corrected_MFI)))
    cols = ('Corrupted Image', 'CPR Correction', 'fs-CPR Correction',
            'MFI Correction')
    row_names = ('-/+ 1600 Hz', '-/+ 3200 Hz', '-/+ 4800 Hz')
    plot_correction_results(im_stack, cols, row_names)
Example #3
0
def numsim_spiral():
    N = 128  # ph.shape[0]
    ph = resize(shepp_logan_phantom(), (N, N))  #.astype(complex)
    plt.imshow(np.abs(ph), cmap='gray')
    plt.title('Original phantom')
    plt.axis('off')
    plt.colorbar()
    plt.show()

    brain_mask = mask_by_threshold(ph)

    # Floodfill from point (0, 0)
    ph_holes = ~(flood_fill(brain_mask, (0, 0), 1).astype(int)) + 2
    mask = brain_mask + ph_holes

    ##
    # Spiral k-space trajectory
    ##
    dt = 4e-6
    ktraj = np.load('sample_data/SS_sprial_ktraj.npy')  # k-space trajectory

    plt.plot(ktraj.real, ktraj.imag)
    plt.title('Spiral trajectory')
    plt.show()

    #ktraj_dcf = np.load('test_data/ktraj_noncart_dcf.npy').flatten() # density compensation factor
    t_ro = ktraj.shape[0] * dt
    T = (np.arange(ktraj.shape[0]) * dt).reshape(ktraj.shape[0], 1)

    seq_params = {
        'N': ph.shape[0],
        'Npoints': ktraj.shape[0],
        'Nshots': ktraj.shape[1],
        't_readout': t_ro,
        't_vector': T
    }  #, 'dcf': ktraj_dcf}
    ##
    # Simulated field map
    ##
    fmax_v = [100, 150, 200]  # Hz

    i = 0
    or_corrupted = np.zeros((N, N, len(fmax_v)), dtype='complex')
    or_corrected_CPR = np.zeros((N, N, len(fmax_v)), dtype='complex')
    or_corrected_fsCPR = np.zeros((N, N, len(fmax_v)), dtype='complex')
    or_corrected_MFI = np.zeros((N, N, len(fmax_v)), dtype='complex')
    for fmax in fmax_v:

        SL_smooth = gaussian(ph, sigma=3)
        field_map = cv2.normalize(SL_smooth, None, -fmax, fmax,
                                  cv2.NORM_MINMAX)
        field_map = np.round(field_map * mask)
        field_map[np.where(field_map == -0.0)] = 0
        ###
        plt.imshow(field_map, cmap='gray')
        plt.title('Field Map +/-' + str(fmax) + ' Hz')
        plt.colorbar()
        plt.axis('off')
        plt.show()

        ##
        # Corrupted images
        ##
        or_corrupted[:, :, i], ksp_corrupted = ORC.add_or_CPR(
            ph, ktraj, field_map, 1, seq_params)
        corrupt = (np.abs(or_corrupted[:, :, i]) -
                   np.abs(or_corrupted[..., i]).min()) / (
                       np.abs(or_corrupted[:, :, i]).max() -
                       np.abs(or_corrupted[..., i]).min())
        #plt.imshow(np.abs(or_corrupted[:,:,i]),cmap='gray')
        plt.imshow(corrupt, cmap='gray')
        plt.colorbar()
        plt.title('Corrupted Image')
        plt.axis('off')
        plt.show()

        ###
        # Corrected images
        ###
        or_corrected_CPR[:, :,
                         i] = ORC.correct_from_kdat('CPR', ksp_corrupted,
                                                    ktraj, field_map,
                                                    seq_params, 1)
        or_corrected_fsCPR[:, :,
                           i] = ORC.correct_from_kdat('fsCPR', ksp_corrupted,
                                                      ktraj, field_map,
                                                      seq_params, 1)
        or_corrected_MFI[:, :,
                         i] = ORC.correct_from_kdat('MFI', ksp_corrupted,
                                                    ktraj, field_map,
                                                    seq_params, 1)
        # or_corrected_CPR[:, :, i] = ORC.CPR(or_corrupted[:, :, i], 'im', ktraj, field_map, 1, seq_params)
        #
        # or_corrected_fsCPR[:, :, i] = ORC.fs_CPR(or_corrupted[:, :, i], 'im', ktraj, field_map, 2, 1, seq_params)
        #
        # or_corrected_MFI[:,:,i] = ORC.MFI(or_corrupted[:,:,i], 'im', ktraj, field_map, 2, 1, seq_params)

        i += 1

    ##
    # Plot
    ##
    im_stack = np.stack(
        (np.squeeze(or_corrupted), np.squeeze(or_corrected_CPR),
         np.squeeze(or_corrected_fsCPR), np.squeeze(or_corrected_MFI)))
    #np.save('im_stack.npy',im_stack)
    cols = ('Corrupted Image', 'CPR Correction', 'fs-CPR Correction',
            'MFI Correction')
    row_names = ('-/+ 100 Hz', '-/+ 150 Hz', '-/+ 200 Hz')
    plot_correction_results(im_stack, cols, row_names)