Beispiel #1
0
def example_3D():

    import pkg_resources

    DATA_PATH = pkg_resources.resource_filename('pynufft', './src/data/')

    image = numpy.load(DATA_PATH + 'phantom_3D_128_128_128.npz')['arr_0'][0::2,
                                                                          0::2,
                                                                          0::2]

    pyplot.imshow(numpy.abs(image[:, :, 32]),
                  label='original signal',
                  cmap=gray)
    pyplot.show()

    Nd = (64, 64, 64)  # time grid, tuple
    Kd = (64, 64, 64)  # frequency grid, tuple
    Jd = (1, 1, 1)  # interpolator
    #     om=       numpy.load(DATA_PATH+'om3D.npz')['arr_0']
    om = numpy.random.randn(15120, 3)
    print(om.shape)
    from pynufft import NUFFT_cpu, NUFFT_hsa
    NufftObj = NUFFT_cpu()

    NufftObj.plan(om, Nd, Kd, Jd)

    kspace = NufftObj.forward(image)

    restore_image = NufftObj.solve(kspace, 'cg', maxiter=200)

    #     restore_image1 = NufftObj.solve(kspace,'L1TVLAD', maxiter=200,rho=0.1)
    #
    restore_image2 = NufftObj.solve(kspace, 'L1TVOLS', maxiter=200, rho=0.1)
    pyplot.subplot(2, 2, 1)
    pyplot.imshow(numpy.abs(image[:, :, 32]),
                  label='original signal',
                  cmap=gray)
    pyplot.title('original')
    #     pyplot.subplot(2,2,2)
    #     pyplot.imshow(numpy.abs(restore_image1[:,:,32]), label='L1TVLAD',cmap=gray)
    #     pyplot.title('L1TVLAD')

    pyplot.subplot(2, 2, 3)
    pyplot.imshow(numpy.abs(restore_image2[:, :, 32]),
                  label='L1TVOLS',
                  cmap=gray)
    pyplot.title('L1TVOLS')

    pyplot.subplot(2, 2, 4)
    pyplot.imshow(numpy.abs(restore_image[:, :, 32]), label='CG', cmap=gray)
    pyplot.title('CG')
    #     pyplot.legend([im1, im im4])

    pyplot.show()
Beispiel #2
0
def example_1D():

    om = numpy.random.randn(1512, 1)
    # print(om)
    # print(om.shape)
    # pyplot.hist(om)
    # pyplot.show()

    Nd = (256, )  # time grid, tuple
    Kd = (512, )  # frequency grid, tuple
    Jd = (7, )  # interpolator
    from pynufft import NUFFT_cpu, NUFFT_hsa
    NufftObj = NUFFT_cpu()

    batch = 4

    NufftObj.plan(om, Nd, Kd, Jd, batch=batch)

    time_data = numpy.zeros((256, batch))
    time_data[64:192, :] = 1.0
    pyplot.plot(time_data)
    pyplot.ylim(-1, 2)
    pyplot.show()

    nufft_freq_data = NufftObj.forward(time_data)
    print('shape of y = ', nufft_freq_data.shape)

    x2 = NufftObj.adjoint(nufft_freq_data)
    restore_time = NufftObj.solve(nufft_freq_data, 'cg', maxiter=30)

    restore_time1 = NufftObj.solve(nufft_freq_data,
                                   'L1TVOLS',
                                   maxiter=30,
                                   rho=1)
    #
    #     restore_time2 = NufftObj.solve(nufft_freq_data,'L1TVOLS', maxiter=30,rho=1)
    #
    #     im1,=pyplot.plot(numpy.abs(time_data),'r',label='original signal')

    #     im3,=pyplot.plot(numpy.abs(restore_time2),'k--',label='L1TVOLS')
    #     im4,=pyplot.plot(numpy.abs(restore_time),'r:',label='conjugate_gradient_method')
    #     pyplot.legend([im1, im2, im3,im4])

    for slice in range(0, batch):
        pyplot.plot(numpy.abs(x2[:, slice]))
        pyplot.plot(numpy.abs(restore_time[:, slice]))
        pyplot.show()
def spiral_recon(data_path, ktraj, N, plot=0):

    ##
    # Load the raw data
    ##
    dat = sio.loadmat(data_path + 'rawdata_spiral')['dat']

    ##
    # Acq parameters
    ##
    Npoints = ktraj.shape[0]
    Nshots = ktraj.shape[1]
    Nchannels = dat.shape[-1]

    if len(dat.shape) < 4:
        Nslices = 1
        dat = dat.reshape(Npoints, Nshots, 1, Nchannels)
    else:
        Nslices = dat.shape[-2]

    if dat.shape[0] != ktraj.shape[0] or dat.shape[1] != ktraj.shape[1]:
        raise ValueError('Raw data and k-space trajectory do not match!')

    ##
    # Arrange data for pyNUFFT
    ##

    om = np.zeros((Npoints * Nshots, 2))
    om[:, 0] = np.real(ktraj).flatten()
    om[:, 1] = np.imag(ktraj).flatten()

    NufftObj = NUFFT_cpu()  # Create a pynufft object
    Nd = (N, N)  # image size
    Kd = (2 * N, 2 * N)  # k-space size
    Jd = (6, 6)  # interpolation size
    NufftObj.plan(om, Nd, Kd, Jd)

    ##
    # Recon
    ##
    im = np.zeros((N, N, Nslices, Nchannels), dtype=complex)
    for ch in range(Nchannels):
        for sl in range(Nslices):
            im[:, :, sl, ch] = NufftObj.solve(dat[:, :, sl, ch].flatten(),
                                              solver='cg',
                                              maxiter=50)

    sos = np.sum(np.abs(im), 2)
    sos = np.divide(sos, np.max(sos))

    if plot:
        plt.imshow(np.rot90(np.abs(sos[:, :, 0]), -1), cmap='gray')
        plt.axis('off')
        plt.title('Uncorrected Image')
        plt.show()
    return
Beispiel #4
0
def example_1D():

    om = numpy.random.randn(1512,1)
    # print(om)
    # print(om.shape)
    # pyplot.hist(om)
    # pyplot.show()
    
    Nd = (256,) # time grid, tuple
    Kd = (512,) # frequency grid, tuple
    Jd = (7,) # interpolator 
    from pynufft import NUFFT_cpu, NUFFT_hsa
    NufftObj = NUFFT_cpu()
    
    
    NufftObj.plan(om, Nd, Kd, Jd)
    
    
    time_data = numpy.zeros(256, )
    time_data[64:192] = 1.0
    pyplot.plot(time_data)
    pyplot.ylim(-1,2)
    pyplot.show()
    
    
    nufft_freq_data =NufftObj.forward(time_data)
    
    restore_time = NufftObj.solve(nufft_freq_data,'cg', maxiter=30)
    
    restore_time2 = NufftObj.solve(nufft_freq_data,'L1TVOLS', maxiter=30,rho=1)
    
    im1,=pyplot.plot(numpy.abs(time_data),'r',label='original signal')
 
#     im2,=pyplot.plot(numpy.abs(restore_time1),'b:',label='L1TVLAD')
    im3,=pyplot.plot(numpy.abs(restore_time2),'k--',label='L1TVOLS')
    im4,=pyplot.plot(numpy.abs(restore_time),'r:',label='conjugate_gradient_method')
    pyplot.legend([im1,  im3,im4])
    
    
    pyplot.show()
Beispiel #5
0
def non_uniform_fft(pos_stack,pos_wavefun,solver,interp_size):

    assert len(pos_wavefun.shape) == 2


    NufftObj = NUFFT_cpu()

    om = pos_stack
    Nd = (len(pos_stack[0]),len(pos_stack[1]))
    Kd = Nd
    Jd = (interp_size,interp_size)

    NufftObj.plan(om,Nd,Kd,Jd)

    y = NufftObj.forward(pos_wavefun)

    mom_wavefun_1 = NufftObj.solve(y,solver=solver )

    #mom_wavefun_2 = NufftObj.adjoint(y)

    return mom_wavefun_1 #, mom_wavefun_2
Beispiel #6
0
image = scipy.misc.ascent()
image = scipy.misc.imresize(image, (256, 256))
image = image * 1.0 / numpy.max(image[...])

print('loading image...')

matplotlib.pyplot.imshow(image.real, cmap=matplotlib.cm.gray)
matplotlib.pyplot.show()

y = NufftObj.forward(image)
print('setting non-uniform data')
print('y is an (M,) list', type(y), y.shape)

matplotlib.pyplot.subplot(2, 2, 1)
image0 = NufftObj.solve(y, solver='cg', maxiter=50)
matplotlib.pyplot.title('Restored image (cg)')
matplotlib.pyplot.imshow(image0.real,
                         cmap=matplotlib.cm.gray,
                         norm=matplotlib.colors.Normalize(vmin=0.0, vmax=1))

matplotlib.pyplot.subplot(2, 2, 2)
image2 = NufftObj.adjoint(y)
matplotlib.pyplot.imshow(image2.real,
                         cmap=matplotlib.cm.gray,
                         norm=matplotlib.colors.Normalize(vmin=0.0, vmax=5))
matplotlib.pyplot.title('Adjoint transform')

matplotlib.pyplot.subplot(2, 2, 3)
image3 = NufftObj.solve(y, solver='L1TVOLS', maxiter=50, rho=0.1)
matplotlib.pyplot.title('L1TV OLS')
Beispiel #7
0
def test_2D():
    import pkg_resources

    DATA_PATH = pkg_resources.resource_filename('pynufft', 'src/data/')
    #     PHANTOM_FILE = pkg_resources.resource_filename('pynufft', 'data/phantom_256_256.txt')
    import numpy
    import matplotlib.pyplot
    from pynufft import NUFFT_cpu
    # load example image
    #     image = numpy.loadtxt(DATA_PATH +'phantom_256_256.txt')
    image = scipy.misc.ascent()[::2, ::2]
    image = image.astype(numpy.float) / numpy.max(image[...])
    #numpy.save('phantom_256_256',image)
    matplotlib.pyplot.imshow(image, cmap=matplotlib.cm.gray)
    matplotlib.pyplot.show()
    print('loading image...')

    Nd = (256, 256)  # image size
    print('setting image dimension Nd...', Nd)
    Kd = (512, 512)  # k-space size
    print('setting spectrum dimension Kd...', Kd)
    Jd = (6, 6)  # interpolation size
    print('setting interpolation size Jd...', Jd)
    # load k-space points
    # om = numpy.loadtxt(DATA_PATH+'om.txt')
    om = numpy.load(DATA_PATH + 'om2D.npz')['arr_0']
    print('setting non-uniform coordinates...')
    matplotlib.pyplot.plot(om[::10, 0], om[::10, 1], 'o')
    matplotlib.pyplot.title('non-uniform coordinates')
    matplotlib.pyplot.xlabel('axis 0')
    matplotlib.pyplot.ylabel('axis 1')
    matplotlib.pyplot.show()

    NufftObj = NUFFT_cpu()
    NufftObj.plan(om, Nd, Kd, Jd)

    y = NufftObj.forward(image)
    print('setting non-uniform data')
    print('y is an (M,) list', type(y), y.shape)

    #     kspectrum = NufftObj.xx2k( NufftObj.solve(y,solver='bicgstab',maxiter = 100))
    image_restore = NufftObj.solve(y, solver='cg', maxiter=10)
    shifted_kspectrum = numpy.fft.fftshift(
        numpy.fft.fftn(numpy.fft.fftshift(image_restore)))
    print('getting the k-space spectrum, shape =', shifted_kspectrum.shape)
    print('Showing the shifted k-space spectrum')

    matplotlib.pyplot.imshow(shifted_kspectrum.real,
                             cmap=matplotlib.cm.gray,
                             norm=matplotlib.colors.Normalize(vmin=-100,
                                                              vmax=100))
    matplotlib.pyplot.title('shifted k-space spectrum')
    matplotlib.pyplot.show()

    image4 = NufftObj.solve(y, 'L1TVOLS', maxiter=100, rho=1)
    image2 = NufftObj.solve(y, 'dc', maxiter=25)
    image3 = NufftObj.solve(y, 'cg', maxiter=25)
    matplotlib.pyplot.subplot(1, 3, 1)
    matplotlib.pyplot.imshow(image2.real,
                             cmap=matplotlib.cm.gray,
                             norm=matplotlib.colors.Normalize(vmin=0.0,
                                                              vmax=1))
    matplotlib.pyplot.title('dc')
    matplotlib.pyplot.subplot(1, 3, 2)
    matplotlib.pyplot.imshow(image3.real,
                             cmap=matplotlib.cm.gray,
                             norm=matplotlib.colors.Normalize(vmin=0.0,
                                                              vmax=1))
    matplotlib.pyplot.title('cg')
    matplotlib.pyplot.subplot(1, 3, 3)
    matplotlib.pyplot.imshow(image4.real,
                             cmap=matplotlib.cm.gray,
                             norm=matplotlib.colors.Normalize(vmin=0.0,
                                                              vmax=1))
    matplotlib.pyplot.title('L1TVOLS')
    matplotlib.pyplot.show()

    #     matplotlib.pyplot.imshow(image2.real, cmap=matplotlib.cm.gray, norm=matplotlib.colors.Normalize(vmin=0.0, vmax=1))
    #     matplotlib.pyplot.show()
    maxiter = 25
    counter = 1
    for solver in ('dc', 'bicg', 'bicgstab', 'cg', 'gmres', 'lgmres', 'lsqr'):
        print(counter, solver)
        if 'lsqr' == solver:
            image2 = NufftObj.solve(y, solver, iter_lim=maxiter)
        else:
            image2 = NufftObj.solve(y, solver, maxiter=maxiter)


#     image2 = NufftObj.solve(y, solver='bicgstab',maxiter=30)
        matplotlib.pyplot.subplot(2, 4, counter)
        matplotlib.pyplot.imshow(image2.real,
                                 cmap=matplotlib.cm.gray,
                                 norm=matplotlib.colors.Normalize(vmin=0.0,
                                                                  vmax=1))
        matplotlib.pyplot.title(solver)
        #         print(counter, solver)
        counter += 1
    matplotlib.pyplot.show()
Beispiel #8
0
def test_cuda():

    import numpy
    import matplotlib.pyplot

    # load example image
    import pkg_resources

    ## Define the source of data
    DATA_PATH = pkg_resources.resource_filename('pynufft', 'src/data/')
    #     PHANTOM_FILE = pkg_resources.resource_filename('pynufft', 'data/phantom_256_256.txt')
    import scipy

    image = scipy.misc.ascent()
    image = scipy.misc.imresize(image, (256, 256))
    image = image.astype(numpy.float) / numpy.max(image[...])

    Nd = (256, 256)  # image space size
    Kd = (512, 512)  # k-space size
    Jd = (6, 6)  # interpolation size

    # load k-space points as M * 2 array
    om = numpy.load(DATA_PATH + 'om2D.npz')['arr_0']

    # Show the shape of om
    print('the shape of om = ', om.shape)

    # initiating NUFFT_cpu object
    nfft = NUFFT_cpu()  # CPU NUFFT class

    # Plan the nfft object
    nfft.plan(om, Nd, Kd, Jd)

    # initiating NUFFT_hsa object
    NufftObj = NUFFT_hsa('cuda', 0, 0)

    # Plan the NufftObj (similar to NUFFT_cpu)
    NufftObj.plan(om, Nd, Kd, Jd)

    import time
    t0 = time.time()
    for pp in range(0, 10):

        y = nfft.forward(image)

    t_cpu = (time.time() - t0) / 10.0

    ## Moving image to gpu
    ## gx is an gpu array, dtype = complex64
    gx = NufftObj.to_device(image)

    t0 = time.time()
    for pp in range(0, 100):
        gy = NufftObj.forward(gx)
    t_cu = (time.time() - t0) / 100

    print('t_cpu = ', t_cpu)
    print('t_cuda =, ', t_cu)

    print('gy close? = ',
          numpy.allclose(y, gy.get(), atol=numpy.linalg.norm(y) * 1e-3))
    print("acceleration=", t_cpu / t_cu)
    maxiter = 100
    import time
    t0 = time.time()
    x_cpu_cg = nfft.solve(y, 'cg', maxiter=maxiter)
    #     x2 =  nfft.solve(y2, 'L1TVLAD',maxiter=maxiter, rho = 2)
    t1 = time.time() - t0
    #     gy=NufftObj.thr.copy_array(NufftObj.thr.to_device(y2))

    t0 = time.time()
    x_cuda_cg = NufftObj.solve(gy, 'cg', maxiter=maxiter)
    #     x = NufftObj.solve(gy,'L1TVLAD', maxiter=maxiter, rho=2)

    t2 = time.time() - t0
    print(t1, t2)
    print('acceleration of cg=', t1 / t2)

    t0 = time.time()
    x_cpu_TV = nfft.solve(y, 'L1TVOLS', maxiter=maxiter, rho=2)
    t1 = time.time() - t0

    t0 = time.time()

    x_cuda_TV = NufftObj.solve(gy, 'L1TVOLS', maxiter=maxiter, rho=2)

    t2 = time.time() - t0
    print(t1, t2)
    print('acceleration of TV=', t1 / t2)

    matplotlib.pyplot.subplot(2, 2, 1)
    matplotlib.pyplot.imshow(x_cpu_cg.real, cmap=matplotlib.cm.gray)
    matplotlib.pyplot.title('CG_cpu')
    matplotlib.pyplot.subplot(2, 2, 2)
    matplotlib.pyplot.imshow(x_cuda_cg.get().real, cmap=matplotlib.cm.gray)
    matplotlib.pyplot.title('CG_cuda')
    matplotlib.pyplot.subplot(2, 2, 3)
    matplotlib.pyplot.imshow(x_cpu_TV.real, cmap=matplotlib.cm.gray)
    matplotlib.pyplot.title('TV_cpu')
    matplotlib.pyplot.subplot(2, 2, 4)
    matplotlib.pyplot.imshow(x_cuda_TV.get().real, cmap=matplotlib.cm.gray)
    matplotlib.pyplot.title('TV_cuda')
    matplotlib.pyplot.show()

    NufftObj.release()
    del NufftObj
Beispiel #9
0
Jd = (7, )  # interpolator

NufftObj = NUFFT_cpu()

NufftObj.plan(om, Nd, Kd, Jd)

time_data = numpy.zeros(256, )
time_data[64:192] = 1.0
pyplot.plot(time_data)
pyplot.ylim(-1, 2)
pyplot.show()

nufft_freq_data = NufftObj.forward(time_data)
pyplot.plot(om, nufft_freq_data.real, '.', label='real')
pyplot.plot(om, nufft_freq_data.imag, 'r.', label='imag')
pyplot.legend()
pyplot.show()

restore_time = NufftObj.solve(nufft_freq_data, 'cg', maxiter=30)
restore_time1 = NufftObj.solve(nufft_freq_data, 'L1TVLAD', maxiter=30, rho=1)
restore_time2 = NufftObj.solve(nufft_freq_data, 'L1TVOLS', maxiter=30, rho=1)

im1, = pyplot.plot(numpy.abs(time_data), 'r', label='original signal')
im2, = pyplot.plot(numpy.abs(restore_time1), 'b:', label='L1TVLAD')
im3, = pyplot.plot(numpy.abs(restore_time2), 'k--', label='L1TVOLS')
im4, = pyplot.plot(numpy.abs(restore_time),
                   'r:',
                   label='conjugate_gradient_method')
pyplot.legend([im1, im2, im3, im4])
pyplot.show()
    om[512 * index:512 * (index + 1), 1] = spoke_y

#plt.plot(om[:,0], om[:,1],'.')
#plt.title("Radial Kspace Trajectory")
#plt.show()

numProjections = kspace.shape[1]
numReadouts = kspace.shape[0]

print('Number of Projections = ', numProjections)
print('Number of Readout Values = ', numReadouts)

myNufft = NUFFT_cpu()
myNufft.plan(om=om, Nd=(256, 256), Kd=(numReadouts, numReadouts), Jd=(2, 2))

y = kspace.flatten(order='C')
image = myNufft.adjoint(y)
#y = myNufft.forward(image)

#ipdb.set_trace()

plt.subplot(2, 2, 1)
image0 = myNufft.solve(y, solver='cg', maxiter=50)

#img = image0.real/image0.real.max()
plt.title('Restored image (cg)')
plt.imshow(image0.real,
           cmap=matplotlib.cm.gray,
           norm=matplotlib.colors.Normalize(vmin=0.0, vmax=1))
plt.show()
Beispiel #11
0
pyplot.show()

Nd = (64, 64, 64)  # time grid, tuple
Kd = (64, 64, 64)  # frequency grid, tuple
Jd = (1, 1, 1)  # interpolator
#     om=       numpy.load(DATA_PATH+'om3D.npz')['arr_0']
om = numpy.random.randn(151200, 3) * 2
print(om.shape)
from pynufft import NUFFT_cpu, NUFFT_hsa
NufftObj = NUFFT_cpu()

NufftObj.plan(om, Nd, Kd, Jd)

kspace = NufftObj.forward(image)

restore_image = NufftObj.solve(kspace, 'cg', maxiter=500)

restore_image1 = NufftObj.solve(kspace, 'L1TVLAD', maxiter=500, rho=0.1)
#
restore_image2 = NufftObj.solve(kspace, 'L1TVOLS', maxiter=500, rho=0.1)
pyplot.subplot(2, 2, 1)
pyplot.imshow(numpy.real(image[:, :, 32]), label='original signal', cmap=gray)
pyplot.title('original')
pyplot.subplot(2, 2, 2)
pyplot.imshow(numpy.real(restore_image1[:, :, 32]), label='L1TVLAD', cmap=gray)
pyplot.title('L1TVLAD')

pyplot.subplot(2, 2, 3)
pyplot.imshow(numpy.real(restore_image2[:, :, 32]), label='L1TVOLS', cmap=gray)
pyplot.title('L1TVOLS')
Beispiel #12
0
Jd = (7, )  # interpolator

NufftObj = NUFFT_cpu()

NufftObj.plan(om, Nd, Kd, Jd)

time_data = numpy.zeros(256, )
time_data[64:192] = 1.0
pyplot.plot(time_data)
pyplot.ylim(-1, 2)
pyplot.show()

nufft_freq_data = NufftObj.forward(time_data)
pyplot.plot(om, nufft_freq_data.real, '.', label='real')
pyplot.plot(om, nufft_freq_data.imag, 'r.', label='imag')
pyplot.legend()
pyplot.show()

restore_time = NufftObj.solve(nufft_freq_data, 'cg', maxiter=30)
#restore_time1 = NufftObj.solve(nufft_freq_data, 'L1TVLAD', maxiter=30, rho=1)
#restore_time2 = NufftObj.solve(nufft_freq_data, 'L1TVOLS', maxiter=30, rho=1)

im1, = pyplot.plot(numpy.abs(time_data), 'r', label='original signal')
#im2, = pyplot.plot(numpy.abs(restore_time1), 'b:', label='L1TVLAD')
#im3, = pyplot.plot(numpy.abs(restore_time2), 'k--', label='L1TVOLS')
im4, = pyplot.plot(numpy.abs(restore_time),
                   'r:',
                   label='conjugate_gradient_method')
#pyplot.legend([im1, im2, im3, im4])
pyplot.show()
def test_opencl_multicoils():

    import numpy
    import matplotlib.pyplot

    # load example image
    import pkg_resources

    ## Define the source of data
    DATA_PATH = pkg_resources.resource_filename('pynufft', 'src/data/')
    #     PHANTOM_FILE = pkg_resources.resource_filename('pynufft', 'data/phantom_256_256.txt')
    import scipy

    image = scipy.misc.ascent()[::2, ::2]
    image = image.astype(numpy.float) / numpy.max(image[...])

    Nd = (256, 256)  # image space size
    Kd = (512, 512)  # k-space size
    Jd = (6, 6)  # interpolation size

    # load k-space points as M * 2 array
    om = numpy.load(DATA_PATH + 'om2D.npz')['arr_0']

    # Show the shape of om
    print('the shape of om = ', om.shape)

    batch = 8

    # initiating NUFFT_cpu object
    nfft = NUFFT_cpu()  # CPU NUFFT class

    # Plan the nfft object
    nfft.plan(om, Nd, Kd, Jd, batch=batch)

    # initiating NUFFT_hsa object
    try:
        NufftObj = NUFFT_hsa('cuda', 0, 0)
    except:
        try:
            NufftObj = NUFFT_hsa('ocl', 1, 0)
        except:
            NufftObj = NUFFT_hsa('ocl', 0, 0)

    # Plan the NufftObj (similar to NUFFT_cpu)
    NufftObj.plan(om, Nd, Kd, Jd, batch=batch, radix=2)
    coil_sense = numpy.ones(Nd + (batch, ), dtype=numpy.complex64)
    for cc in range(0, batch, 2):
        coil_sense[int(256 / batch) * cc:int(256 / batch) * (cc + 1), :,
                   cc].real *= 0.1
        coil_sense[:, int(256 / batch) * cc:int(256 / batch) * (cc + 1),
                   cc].imag *= -0.1

    NufftObj.set_sense(coil_sense)
    nfft.set_sense(coil_sense)
    y = nfft.forward_one2many(image)
    import time
    t0 = time.time()
    for pp in range(0, 2):

        xx = nfft.adjoint_many2one(y)

    t_cpu = (time.time() - t0) / 2

    ## Moving image to gpu
    ## gx is an gpu array, dtype = complex64
    gx = NufftObj.to_device(image)

    gy = NufftObj.forward_one2many(gx)

    t0 = time.time()
    for pp in range(0, 10):

        gxx = NufftObj.adjoint_many2one(gy)
    t_cu = (time.time() - t0) / 10
    print(y.shape, gy.get().shape)
    print('t_cpu = ', t_cpu)
    print('t_cuda =, ', t_cu)

    print('gy close? = ',
          numpy.allclose(y, gy.get(), atol=numpy.linalg.norm(y) * 1e-6))
    print('gy error = ',
          numpy.linalg.norm(y - gy.get()) / numpy.linalg.norm(y))
    print('gxx close? = ',
          numpy.allclose(xx, gxx.get(), atol=numpy.linalg.norm(xx) * 1e-6))
    print('gxx error = ',
          numpy.linalg.norm(xx - gxx.get()) / numpy.linalg.norm(xx))
    #     for bb in range(0, batch):
    matplotlib.pyplot.subplot(1, 2, 1)
    matplotlib.pyplot.imshow(xx[...].real, cmap=matplotlib.cm.gray)
    matplotlib.pyplot.title('Adjoint_cpu_coil')
    matplotlib.pyplot.subplot(1, 2, 2)
    matplotlib.pyplot.imshow(gxx.get()[...].real, cmap=matplotlib.cm.gray)
    matplotlib.pyplot.title('Adjoint_hsa_coil')
    #         matplotlib.pyplot.subplot(2, 2, 3)
    #         matplotlib.pyplot.imshow( x_cpu_TV.real, cmap= matplotlib.cm.gray)
    #         matplotlib.pyplot.title('TV_cpu')#     x_cuda_TV = NufftObj.solve(gy,'L1TVOLS', maxiter=maxiter, rho=2)
    #         matplotlib.pyplot.subplot(2, 2, 4)
    #         matplotlib.pyplot.imshow(x_cuda_TV.get().real, cmap= matplotlib.cm.gray)
    #         matplotlib.pyplot.title('TV_cuda')
    matplotlib.pyplot.show(block=False)
    matplotlib.pyplot.pause(1)
    matplotlib.pyplot.close()

    print("acceleration=", t_cpu / t_cu)
    maxiter = 100
    import time
    t0 = time.time()
    x_cpu_cg = nfft.solve(y, 'cg', maxiter=maxiter)
    #     x2 =  nfft.solve(y2, 'L1TVLAD',maxiter=maxiter, rho = 2)
    t1 = time.time() - t0
    #     gy=NufftObj.thr.copy_array(NufftObj.thr.to_device(y2))

    t0 = time.time()
    x_cuda_cg = NufftObj.solve(gy, 'cg', maxiter=maxiter)
    #     x = NufftObj.solve(gy,'L1TVLAD', maxiter=maxiter, rho=2)
    print('shape of cg = ', x_cuda_cg.get().shape, x_cpu_cg.shape)
    t2 = time.time() - t0
    print(t1, t2)
    print('acceleration of cg=', t1 / t2)

    t0 = time.time()
    #     x_cpu_TV =  nfft.solve(y, 'L1TVOLS',maxiter=maxiter, rho = 2)
    t1 = time.time() - t0

    t0 = time.time()

    #     x_cuda_TV = NufftObj.solve(gy,'L1TVOLS', maxiter=maxiter, rho=2)

    t2 = time.time() - t0
    print(t1, t2)
    #     print('acceleration of TV=', t1/t2 )

    #     try:
    for bb in range(0, batch):
        matplotlib.pyplot.subplot(2, batch, 1 + bb)
        matplotlib.pyplot.imshow(x_cpu_cg[..., bb].real,
                                 cmap=matplotlib.cm.gray)
        matplotlib.pyplot.title('CG_cpu_coil_' + str(bb))
        matplotlib.pyplot.subplot(2, batch, 1 + batch + bb)
        matplotlib.pyplot.imshow(x_cuda_cg.get()[..., bb].real,
                                 cmap=matplotlib.cm.gray)
        matplotlib.pyplot.title('CG_hsa_coil_' + str(bb))


#         matplotlib.pyplot.subplot(2, 2, 3)
#         matplotlib.pyplot.imshow( x_cpu_TV.real, cmap= matplotlib.cm.gray)
#         matplotlib.pyplot.title('TV_cpu')#     x_cuda_TV = NufftObj.solve(gy,'L1TVOLS', maxiter=maxiter, rho=2)
#         matplotlib.pyplot.subplot(2, 2, 4)
#         matplotlib.pyplot.imshow(x_cuda_TV.get().real, cmap= matplotlib.cm.gray)
#         matplotlib.pyplot.title('TV_cuda')
    matplotlib.pyplot.show()
    #     except:
    #         print('no matplotlib')

    NufftObj.release()
    del NufftObj
Beispiel #14
0
def test_init():
    
#     cm = matplotlib.cm.gray
    # load example image
    import pkg_resources
    
    DATA_PATH = pkg_resources.resource_filename('pynufft', 'src/data/')
#     PHANTOM_FILE = pkg_resources.resource_filename('pynufft', 'data/phantom_256_256.txt')
    import numpy
    
#     import matplotlib.pyplot
    
    import scipy

    image = scipy.misc.ascent()[::2,::2]
    image=image.astype(numpy.float)/numpy.max(image[...])

    Nd = (256, 256)  # image space size
    Kd = (512, 512)  # k-space size
    Jd = (6,6)  # interpolation size

    # load k-space points
    om = numpy.load(DATA_PATH+'om2D.npz')['arr_0']

    nfft = NUFFT_cpu()  # CPU
    
    nfft.plan(om, Nd, Kd, Jd)
    try:
        NufftObj = NUFFT_hsa('cuda',0,0)
    except:
        NufftObj = NUFFT_hsa('ocl',0,0)
#     NufftObj2 = NUFFT_hsa('cuda',0,0)
    NufftObj.debug = 1
    NufftObj.plan(om, Nd, Kd, Jd, radix=2)
#     NufftObj2.plan(om, Nd, Kd, Jd)
    
#     NufftObj.offload(API = 'cuda',   platform_number = 0, device_number = 0)
#     NufftObj2.offload(API = 'cuda',   platform_number = 0, device_number = 0)
#     NufftObj2.offload('cuda')
#     NufftObj.offload(API = 'cuda',   platform_number = 0, device_number = 0)
#     print('api=', NufftObj.thr.api_name())
#     NufftObj.offload(API = 'ocl',   platform_number = 0, device_number = 0)
    y = nfft.k2y(nfft.xx2k(nfft.x2xx(image)))
    
    NufftObj.x_Nd = NufftObj.thr.to_device( image.astype(dtype))
    
    gx = NufftObj.thr.copy_array(NufftObj.x_Nd)
    
    print('x close? = ', numpy.allclose(image, gx.get() , atol=1e-4))
    gxx = NufftObj.x2xx(gx)    

    print('xx close? = ', numpy.allclose(nfft.x2xx(image), gxx.get() , atol=1e-4))        

    gk = NufftObj.xx2k(gxx)    

    k = nfft.xx2k(nfft.x2xx(image))
    
    print('k close? = ', numpy.allclose(nfft.xx2k(nfft.x2xx(image)), gk.get(), atol=1e-3*numpy.linalg.norm(k)))   
    gy = NufftObj.k2y(gk)    
    k2 = NufftObj.y2k(gy)
    print('y close? = ', numpy.allclose(y, gy.get() ,  atol=1e-3*numpy.linalg.norm(y)), numpy.linalg.norm((y - gy.get())/numpy.linalg.norm(y)))
    y2 = y
    print('k2 close? = ', numpy.allclose(nfft.y2k(y2), k2.get(), atol=1e-3*numpy.linalg.norm(nfft.y2k(y2)) ), numpy.linalg.norm(( nfft.y2k(y2)- k2.get())/numpy.linalg.norm(nfft.y2k(y2))))   
    gxx2 = NufftObj.k2xx(k2)
#     print('xx close? = ', numpy.allclose(nfft.k2xx(nfft.y2k(y2)), NufftObj.xx_Nd.get(queue=NufftObj.queue, async=False) , atol=0.1))
    gx2 = NufftObj.xx2x(gxx2)
    print('x close? = ', numpy.allclose(nfft.adjoint(y2), gx2.get() , atol=1e-3*numpy.linalg.norm(nfft.adjoint(y2))))
    image3 = gx2.get() 
    import time
    t0 = time.time()
#     k = nfft.xx2k(nfft.x2xx(image))
    for pp in range(0,50):
#         y = nfft.k2y(nfft.xx2k(nfft.x2xx(image)))    
            y = nfft.forward(image)
#             y = nfft.k2y(k)
#                 k = nfft.y2k(y)
#             x = nfft.adjoint(y)
#             y = nfft.forward(image)
#     y2 = NufftObj.y.get(   NufftObj.queue, async=False)
    t_cpu = (time.time() - t0)/50.0 
    print(t_cpu)
    
#     del nfft
        
    gy2=NufftObj.forward(gx)
#     gk =     NufftObj.xx2k(NufftObj.x2xx(gx))
    t0= time.time()
    for pp in range(0,20):
#         pass
        gy2 = NufftObj.forward(gx)
#         gy2 = NufftObj.k2y(gk)
#             gx2 = NufftObj.adjoint(gy2)
#             gk2 = NufftObj.y2k(gy2)
#         del gy2
#     c = gx2.get()
#         gy=NufftObj.forward(gx)        
        
    NufftObj.thr.synchronize()
    t_cl = (time.time() - t0)/20
    print(t_cl)
    
    print('gy close? = ', numpy.allclose(y, gy.get(),  atol=numpy.linalg.norm(y)*1e-3))
    print("acceleration=", t_cpu/t_cl)
    maxiter =100
    import time
    t0= time.time()
#     x2 =  nfft.solve(y2, 'cg',maxiter=maxiter)
    x2 =  nfft.solve(y2, 'L1TVOLS',maxiter=maxiter, rho = 2)
    t1 = time.time()-t0 
#     gy=NufftObj.thr.copy_array(NufftObj.thr.to_device(y2))
    
    t0= time.time()

#     x = NufftObj.solve(gy,'cg', maxiter=maxiter)
    x = NufftObj.solve(gy,'L1TVOLS', maxiter=maxiter, rho=2)
    
    t2 = time.time() - t0
    print(t1, t2)
    print('acceleration=', t1/t2 )
#     k = x.get()
#     x = nfft.k2xx(k)/nfft.st['sn']
#     return
    try:
        import matplotlib.pyplot
        matplotlib.pyplot.subplot(1, 2, 1)
        matplotlib.pyplot.imshow( x.get().real, cmap= matplotlib.cm.gray, vmin = 0, vmax = 1)
        matplotlib.pyplot.title("HSA reconstruction")
        matplotlib.pyplot.subplot(1, 2,2)
        matplotlib.pyplot.imshow(x2.real, cmap= matplotlib.cm.gray)
        matplotlib.pyplot.title("CPU reconstruction")
        matplotlib.pyplot.show(block = False)
        matplotlib.pyplot.pause(3)
        matplotlib.pyplot.close()
#         del NufftObj.thr
#         del NufftObj
    except:
        print("no graphics")