Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • c0c798a
  • /
  • batch_multicoil_NUFFT.py
Raw File Download
Permalinks

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge Iframe embedding
swh:1:cnt:25428e0f1f892fd96d698d052bc31cf13715115c
directory badge Iframe embedding
swh:1:dir:c0c798aab17971bc32cd88fa2d8891f80ee77cd2
Citations

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
batch_multicoil_NUFFT.py
# Generating trajectories for Cartesian k-space
import numpy
import matplotlib.pyplot
matplotlib.pyplot.gray()
def create_fake_coils(N, n_coil):
    dtype = numpy.complex64
    xx,yy = numpy.meshgrid(numpy.arange(0,N),numpy.arange(0,N))
    
#     ZZ= numpy.exp(-((xx-128)/32)**2-((yy-128)/32)**2)     
    coil_sensitivity = []
    
    image_sense = ()
    
    r = N
    phase_factor =  0
    sense_array = numpy.empty( (N, N, n_coil), dtype = dtype)
    for nn in range(0,n_coil):
        
        tmp_angle = nn*2*numpy.pi/n_coil
        shift_r = int(N)
        shift_x= (numpy.cos(tmp_angle)*shift_r).astype(dtype)
        shift_y= (numpy.sin(tmp_angle)*shift_r).astype(dtype)
#         ZZ= numpy.exp(-((xx-N/2-shift_x)/r)**2-((yy-N/2-shift_y)/r)**2).astype(numpy.complex64)
        ZZ= numpy.exp(1.0j * numpy.random.randn()*0)* numpy.exp(-phase_factor *1.0j*((xx-N/2-shift_x)/N + (yy-N/2-shift_y)/N))* numpy.exp(-((xx-N/2-shift_x)/r)**2-((yy-N/2-shift_y)/r)**2).astype(numpy.complex64)  
#         coil_sensitivity +=(numpy.roll(numpy.roll(ZZ,shift_x,axis=0),shift_y,axis=1),)
        coil_sensitivity +=[ZZ,]
        sense_array[:,:, nn] = ZZ
#     if n_coil > 1:
#         coil_sensitivity[0] = coil_sensitivity[0] + coil_sensitivity[1]
    
    
    
    return sense_array
def fake_Cartesian(Nd):
    dim = len(Nd) # dimension
    M = numpy.prod(Nd)
    om = numpy.zeros((M, dim), dtype = numpy.float)
    grid = numpy.indices(Nd)
    for dimid in range(0, dim):
        om[:, dimid] = (grid[dimid].ravel() *2/ Nd[dimid] - 1.0)*numpy.pi
    return om    


# Begin of test_batch_NUFFT()
def test_batch_NUFFT():
    
    import scipy.misc
    from pynufft import NUFFT
    
    Nd = (256,256)
    Kd = (512,512)
    Jd = (6,6)
    
    image = scipy.misc.ascent()[::2, ::2]
#     image = scipy.misc.imresize(image, Nd).astype(numpy.complex64)
    om = fake_Cartesian(Nd)
    
    batch = 3
    print('Number of samples (M) = ', om.shape[0])
    print('Dimension = ', om.shape[1])
    print('Nd = ', Nd)
    print('Kd = ', Kd)
    print('Jd = ', Jd)
     
#     NufftObj = NUFFT_cpu()
    NufftObj = NUFFT()
    NufftObj.plan(om, Nd, Kd, Jd, ft_axes = (0,1),  batch=batch)
    
    # Now transform 1 image to multiple channels using forward_one2many() method
#     y = NufftObj.forward_one2many(image) # for NUFFT_cpu()
    multi_image = numpy.broadcast_to(numpy.reshape(image, Nd + (1,)), Nd + (batch,))
    y0 = NufftObj.forward(multi_image)
    y = y0

#     y = NufftObj.forward_one2many(image).get() # for NUFFT_hsa()
    
    # Now reshape the data for IFFT
#     y2 = y.reshape(Nd + (batch, ), order='C') 
    
    # Perform IFFT to check the correctness of the k-space
#     x2 = numpy.fft.ifftshift(
#                 numpy.fft.ifftn(
#                     numpy.fft.ifftshift(y2, axes = (0,1)
#                     ), axes = (0,1)
#                 ), axes = (0,1)
#             )
    
    x2 = NufftObj.adjoint(y0)
    
    
    # display the result
    for pp in range(0, batch):
        matplotlib.pyplot.subplot(batch,    3, 1 + pp*3)
        matplotlib.pyplot.imshow(image.real, )#vmin = 0, vmax = 255)
        matplotlib.pyplot.title('Original image')
        matplotlib.pyplot.subplot(batch,3,2 + pp*3)
        matplotlib.pyplot.imshow(x2[:,:,pp].real, )#vmin = 0, vmax = 255)
        matplotlib.pyplot.title('Restored image  of coil '+str(pp + 1))
#         matplotlib.pyplot.subplot(batch,3,3 + pp*3)
#         matplotlib.pyplot.imshow(abs(image - x2[:,:,pp]), vmin = 0, vmax = 255)
#         matplotlib.pyplot.title('Difference map of coil '+str(pp + 1))
    matplotlib.pyplot.show()
    # end of test_batch_NUFFT()


# Begin of test_multicoil_NUFFT()
def test_senselike_NUFFT():
    
    import scipy.misc
    from pynufft import NUFFT, helper
    
    Nd = (256,256)
    Kd = (512,512)
    Jd = (6,6)
    
    image = scipy.misc.ascent()[::2,::2]
#     image = scipy.misc.imresize(image, Nd).astype(numpy.complex64)
    om = fake_Cartesian(Nd)
    
    batch = 3
    print('Number of samples (M) = ', om.shape[0])
    print('Dimension = ', om.shape[1])
    print('Nd = ', Nd)
    print('Kd = ', Kd)
    print('Jd = ', Jd)
     
#     NufftObj = NUFFT_cpu()
    device_list = helper.device_list()
    NufftObj = NUFFT(device_list[0], legacy=True)
    NufftObj.plan(om, Nd, Kd, Jd, ft_axes = (0,1),batch=batch)
    
    # Now create fake coil sensitivity profiles
    sense_array = create_fake_coils(256, batch)
    
    # set_sense() method apply the sensitivities to the object
    NufftObj.set_sense(sense_array)
    
#     NufftObj.reset_sense() # reset the coil sensitivity profile
    
    # Now transform 1 image to multiple channels using forward_one2many() method
#     y = NufftObj.forward_one2many(image) # for NUFFT_cpu()

#     multi_image = numpy.broadcast_to(numpy.reshape(image, Nd + (1,)), Nd + (batch,))
#     y = NufftObj.forward(multi_image).get()

    y0 = NufftObj.forward_one2many(image)# for NUFFT_hsa()
    y = y0
    # Now reshape the data for IFFT
#     y2 = y.reshape(Nd +(batch, ) , order='C') 
#     
#     # Perform IFFT to check the correctness of the k-space
#     x2 = numpy.fft.ifftshift(
#                 numpy.fft.ifftn(
#                     numpy.fft.ifftshift(y2, axes = (0,1)
#                     ), axes = (0,1)
#                 ), axes = (0,1)
#             )
    
    x2 = NufftObj.adjoint(y0)
    
    # display the result
    for pp in range(0, batch):
        matplotlib.pyplot.subplot(batch,    3, 1 + pp*3)
        matplotlib.pyplot.imshow(image.real, vmin = 0, vmax = 255)
        matplotlib.pyplot.title('Original image')
        
        matplotlib.pyplot.subplot(batch,3,2 + pp*3)
        matplotlib.pyplot.imshow(sense_array[:,:,pp].real)
        matplotlib.pyplot.title('Simulated coil sensitivity '+str(pp + 1))
        
        matplotlib.pyplot.subplot(batch,3,3 + pp*3)
        matplotlib.pyplot.imshow(x2[:,:,pp].real)
        matplotlib.pyplot.title('Restored image of coil '+str(pp + 2))

    matplotlib.pyplot.show()
    # end of test_multicoil_NUFFT()
    
if __name__ == '__main__':
    test_batch_NUFFT()  # no sense, multiply sense outside the forward() method
    test_senselike_NUFFT()    # multiplication of sense happens inside the forward_one2many() method

Software Heritage — Copyright (C) 2015–2025, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Contact— JavaScript license information— Web API

back to top