Beispiel #1
0
def test():
    N = 512
    a = np.ones((N, N, N))
    b = 2 * 1j * np.ones((N, N, N))
    time = utc.timing()
    time.start()
    for _ in range(10):
        #b = blas.caxpy(a, b)
        b = a + b
    time.stop().display()
Beispiel #2
0
def test5():
    N = 512
    #x = np.asarray(np.random.rand(N,N,N), np.complex64)
    x = np.ones((N, N, N), np.complex64)
    timing = utc.timing()
    timing.start()
    xf = np.fft.fft2(x, s=None, axes=(0, 1, 2))
    timing.stop().display('numpy fft ').start()
    xf2 = fft2c2c_cuda(x, axes=(0, 1, 2))
    timing.stop().display('cuda fft ')
    print(np.allclose(np.absolute(xf), np.absolute(xf2), atol=1e-2))
Beispiel #3
0
def test():
    # read rf and tr arrays from mat file
    mat_contents = sio.loadmat(pathdat +
                               'mrf_t1t2b0pd_mrf_randphasecyc_traintest.mat')
    far = np.array(mat_contents["rf"].astype(np.complex128).squeeze())
    trr = np.array(mat_contents["trr"].astype(np.float64).squeeze())
    # input MRF time courses
    mat_contents2 = sio.loadmat(pathdat + 'datax1.mat')
    data_x = np.array(mat_contents2["datax1"]).astype(np.float64)
    # prepare for sequence simulation, y->x_hat
    Nk = far.shape[0]
    Nexample = data_x.shape[0]
    ti = 10  #ms
    M0 = np.array([0.0, 0.0, 1.0]).astype(np.float64)
    #image size
    nx = 217
    ny = 181
    # mask in ksp
    mask = ut.mask3d(nx, ny, Nk, [15, 15, 0], 0.4)
    #FTm           = opts.FFT2d_kmask(mask)
    FTm = cuopts.FFT2d_cuda_kmask(mask)

    #intial timing
    timing = utc.timing()
    # x real and imag parts should be combined
    data_x_c = np.zeros((Nexample, Nk), np.complex128)
    data_x_c = ssmrf.seqdata_realimag_2complex(data_x)
    #could do mask M here, data_x_c = M*data_x_c
    data_x_c = mask_ksp3d(nx, ny, Nk, FTm, data_x_c)
    #data_x0_c     = data_x_c
    data_x = ssmrf.seqdata_complex_2realimag(data_x_c)
    data_x0 = data_x
    # intial tmp data
    test_outy0 = np.zeros((Nexample, 4), dtype=np.float64)
    test_outy = np.zeros((Nexample, 4), dtype=np.float64)
    tmptest_outy = np.zeros((Nexample, 4), dtype=np.float64)
    acctest_outy = np.zeros((Nexample, 4), dtype=np.float64)
    # f'(y), cnn model
    timing.start()
    ssmrf.batch_apply_tf_cuda(Nexample, ny, sess.run, x, data_x, y_conv,
                              test_outy0, keep_prob)
    timing.stop().display('CNN model first estimation ')
    test_outy0 = constraints(test_outy0)
    acctest_outy = test_outy0
    # for loop start here
    for _ in range(40):
        #cuda simulate bloch for each pixel
        T1r, T2r, dfr, PDr = ssmrf.set_par((test_outy))
        timing.start()
        data_x_c = ssmrf.bloch_sim_batch_cuda(Nexample, 7 * ny, Nk, PDr, T1r,
                                              T2r, dfr, M0, trr, far, ti)
        timing.stop().display('Bloch sim in loop ')
        #0.5* d||M*f(p)-b||_2^2/dp = f'*(M*f(p)-b) = f'(M*f(p)) - f'(b)
        # could do mask, M, here, e.g. data_x_c=M*data_x_c
        data_x_c = mask_ksp3d(nx, ny, Nk, FTm, data_x_c)
        #seperate real/imag parts or abs/angle parts
        data_x = ssmrf.seqdata_complex_2realimag(data_x_c)
        # apply CNN model, f'(i)
        ssmrf.batch_apply_tf_cuda(Nexample, ny, sess.run, x, data_x, y_conv,
                                  tmptest_outy, keep_prob)
        tmptest_outy = constraints(tmptest_outy)
        # gradient = f'(M * f(p)) - f'(b)
        tmptest_outy = tmptest_outy - test_outy0  #+ 0.2*test_outy
        # gradient descent for test_outy
        acctest_outy = acctest_outy - tmptest_outy
        print('gradient 0.5* d||f(p)-b||_2^2/dp: %g' %
              np.linalg.norm(tmptest_outy))
        #print('test_outy: %g' % np.linalg.norm(test_outy))
        acctest_outy = constraints(acctest_outy)
        #could do soft thresholding on test_outy here, i.e. test_outy = threshold(test_outy)
        #test_outy     = l0_par(nx, ny, acctest_outy) #
        test_outy = tv_par(nx, ny, acctest_outy)

        sio.savemat(pathdat + 'cnn_cs_testouty.mat', {'test_outy': test_outy})
def test():
    # restore tensorflow model
    pathdat = '/working/larson/UTE_GRE_shuffling_recon/MRF/sim_ssfp_fa10_t1t2/IR_ssfp_t1t2b0pd5/'
    pathexe = '/home/pcao/git/mripy/'
    os.chdir(pathdat)
    execfile('load_cnn_t1t2b0_1dcov.py')  #restore model
    os.chdir(pathexe)

    # read rf and tr arrays from mat file
    mat_contents = sio.loadmat(pathdat +
                               'mrf_t1t2b0pd_mrf_randphasecyc_traintest.mat')
    far = np.array(mat_contents["rf"].astype(np.complex128).squeeze())
    trr = np.array(mat_contents["trr"].astype(np.int64).squeeze())

    # input MRF time courses
    mat_contents2 = sio.loadmat(pathdat + 'datax1.mat')
    data_x0 = mat_contents2["datax1"]

    # prepare for sequence simulation, y->x_hat
    Nk = far.shape[0]
    Nexample = data_x0.shape[0]
    ti = 10  #ms
    M0 = np.array([0.0, 0.0, 1.0]).astype(np.float32)

    nx = 217
    ny = 181

    timing = utc.timing()
    #times = []
    # x real and imag parts should be combined
    #data_x0_c = data_x0[:,0:Nexample] + 1j*data_x0[:,-Nexample:]
    data_x0_c = data_x0[:, 0:Nk] + 1j * data_x0[:, Nk:2 * Nk]
    # inital x
    data_x_acc = data_x0  #1j*np.zeros( (Nexample, 2*Nk) )
    data_x_acc_c = 1j * np.zeros((Nexample, Nk))  #data_x0_c

    mat_contents2 = sio.loadmat(pathdat + 'cnn_cs_testouty.mat')
    test_outy0 = mat_contents2["test_outy"].astype(np.float32)
    test_outy = np.zeros((Nexample, 4), dtype=np.float32)
    dtest_outy = np.zeros((Nexample, 4), dtype=np.float32)
    # for loop start here
    for _ in range(10):
        #cuda simulate bloch for each pixel
        T1r, T2r, dfr, PDr = set_par(test_outy)
        timing.start()
        test_outxhat = bloch_sim_batch_cuda(Nexample, 7 * ny, Nk, PDr, T1r,
                                            T2r, dfr, M0, trr, far, ti)
        timing.stop().display()
        print(
            np.linalg.norm(test_outxhat -
                           ssmrf.seqdata_realimag_2complex(data_x_acc)))
        print(np.linalg.norm(test_outxhat))

        #d||f(x)-y||_2^2/dx = 2*f'(x)*(f(x)-y)
        #f(x) - y
        data_x_acc_c = 2 * (test_outxhat - data_x0_c)

        #seperate real/imag parts or abs/angle parts
        data_x_acc[:, 0:Nk] = np.real(data_x_acc_c)
        data_x_acc[:, Nk:2 * Nk] = np.imag(data_x_acc_c)

        # apply CNN model, x->y, data_x_acc->CNN->test_outy
        # 2*f'(x)*(f(x)-y)
        batch_apply_tf_cuda(Nexample, ny, sess.run, x, data_x_acc, y_conv,
                            test_outy, keep_prob)

        # d||x-x0||_2^2/dx = 2* -x0 * (x-x0)
        dtest_outy = test_outy - 0.1 * test_outy0

        test_outy = test_outy - dtest_outy
        sio.savemat(pathdat + 'cnn_cs_testouty.mat', {'test_outy': test_outy})
Beispiel #5
0
def espirit_3d( xcrop, x_shape, nsingularv = 150, hkwin_shape = (16,16,16),\
    pad_before_espirit = 0, pad_fact = 1, sigv_th = 0.01, nsigv_th = 0.2 ):
    #ft = op.FFTnd((0,1,2))#3d fft operator
    ft = op.FFTWnd((0, 1, 2))  #3d fft operator
    timing = utc.timing()
    #multidimention tensor as the block hankel matrix
    #first 2 are x, y dims with rolling window size of hkwin_shape
    #last 1 is coil dimension, with stride of 1
    # output dims are : (3_hankel_dims + 1_coil_dim)_win_size + (3_hankel_dims + 1_coil_dim)_rolling_times
    timing.start()
    h = hk.hankelnd_r(xcrop,
                      (hkwin_shape[0], hkwin_shape[1], hkwin_shape[2], 1))
    timing.stop().display('Create Hankel ').start()
    dimh = h.shape
    #flatten the tensor to create a matrix= [flatten(fist4 dims), flatten(last4 dims)]
    #the second dim of hmtx contain coil information, i.e. dimh[3]=1, dimh[7]=N_coils
    hmtx = h.reshape(( dimh[0]* dimh[1]* dimh[2]* dimh[3], dimh[4], dimh[5], dimh[6], dimh[7])).\
              reshape((dimh[0]* dimh[1]* dimh[2]* dimh[3], dimh[4]* dimh[5]* dimh[6]* dimh[7]))
    timing.stop().display('Reshape Hankel ').start()
    #svd, could try other approaches
    # V has the coil information since the second dim of hmtx has coil data
    U, s, V = np.linalg.svd(hmtx, full_matrices=False)
    #U, s, V = randomized_svd(hmtx, n_components=nsingularv,n_iter=5,random_state=None)
    #U, s, V = scipy.sparse.linalg.svds(hmtx, nsingularv )
    timing.stop().display('SVD ').start()
    #S = np.diag(s)
    #ut.plotim1(np.absolute(V[:,0:150]).T)#plot V singular vectors
    ut.plot(s.T)  #plot singular values
    for k in range(len(s)):
        if s[k] > s[0] * nsigv_th:
            nsingularv = k
    print('extract %g out of %g singular vectors' % (nsingularv, len(s)))
    #invh = np.zeros(x.shape,complex)
    #print h.shape
    #hk.invhankelnd(h,invh,(2,3,1))

    #reshape vn to generate k-space vn tensor
    #first dim is singular vector, which is transposed to the second last dimension
    vn = V[0:nsingularv, :].reshape(
        (nsingularv, dimh[4], dimh[5], dimh[6], dimh[7])).transpose(
            (1, 2, 3, 0, 4))

    #zero pad vn, vn matrix of reshaped singular vectors,
    #dims of vn: nx,ny,nsingularv,ncoil
    #do pading before espirit, reduce the memory requirement
    if pad_before_espirit is 0:
        nx = min(pad_fact * xcrop.shape[0], x_shape[0])
        ny = min(pad_fact * xcrop.shape[1], x_shape[1])
        nz = min(pad_fact * xcrop.shape[2], x_shape[2])
    else:
        nx = x_shape[0]
        ny = x_shape[1]
        nz = x_shape[2]
    #coil dim
    nc = x_shape[3]
    #create hamming window
    hwin = hamming3d(vn.shape[0], vn.shape[1], vn.shape[2])
    # apply hamming window
    vn = np.multiply(vn, hwin[:, :, :, np.newaxis, np.newaxis])
    #zero pad
    vn = ut.pad3d(vn, nx, ny, nz)
    #plot first singular vecctor Vn[0]
    imvn = ft.backward(vn)
    #ut.plotim3(np.absolute(imvn[:,:,0,:].squeeze()))#spatial feature of V[:,1] singular vector
    sim = np.zeros((nx, ny, nz), dtype=vn.dtype)
    Vim = np.zeros((nx, ny, nz, nc), dtype=vn.dtype)
    #espirit loop, Vim eigen vector, Sim eigen value, this is a pixel wise PCA on vn
    for ix in range(nx):
        for iy in range(ny):
            for iz in range(nz):
                vpix = imvn[ix, iy, iz, :, :].squeeze()
                vpix = np.matrix(vpix).transpose()
                vvH = vpix.dot(vpix.getH())
                U, s, V = np.linalg.svd(vvH, full_matrices=False)
                #s, V = numpy.linalg.eig(vvH)
                #U, s, V = randomized_svd(vvH, n_components=2,n_iter=5,random_state=None)
                sim[ix, iy, iz] = s[0]
                Vim[ix, iy, iz, :] = V[0, :].squeeze()

    Vim = np.conj(Vim)
    timing.stop().display('ESPIRIT ')
    #pad the image after espirit
    if pad_before_espirit is 0:
        Vim = ft.backward(
            ut.pad3d(ft.forward(Vim), x_shape[0], x_shape[1], x_shape[2]))
        sim = ft.backward(
            ut.pad3d(ft.forward(sim), x_shape[0], x_shape[1], x_shape[2]))
    #plot first eigen vector, which is coil sensitvity map, and eigen value
    ut.plotim3(np.absolute(Vim[Vim.shape[0] // 2, :, :, :].squeeze()))
    ut.plotim1(np.absolute(sim[Vim.shape[0] // 2, :, :].squeeze()))
    #Vim_dims_name = ['x', 'y', 'z', 'coil']
    #sim_dims_name = ['x', 'y', 'z']
    Vimnorm = np.linalg.norm(Vim, axis=3)
    Vim = np.divide(Vim, 1e-6 + Vimnorm[:, :, :, np.newaxis])

    sim = sim / np.max(sim.flatten())
    for ix in range(x_shape[0]):
        for iy in range(x_shape[1]):
            for iz in range(x_shape[2]):
                if sim[ix, iy, iz] < sigv_th:
                    Vim[ix, iy, iz, :] = np.zeros(nc)
    return Vim, np.absolute(sim)  #, Vim_dims_name, sim_dims_name
Beispiel #6
0
trr = np.array(mat_contents["trr"].astype(np.int64).squeeze())

# input MRF time courses
mat_contents2 = sio.loadmat(pathdat + 'datax1.mat')
data_x0 = mat_contents2["datax1"]

# prepare for sequence simulation, y->x_hat
Nk = far.shape[0]
Nexample = data_x0.shape[0]
ti = 10  #ms
M0 = np.array([0.0, 0.0, 1.0]).astype(np.float32)

nx = 217
ny = 181

timing = utc.timing()
#times = []
# x real and imag parts should be combined
#data_x0_c = data_x0[:,0:Nexample] + 1j*data_x0[:,-Nexample:]
data_x0_c = data_x0[:, 0:Nk] + 1j * data_x0[:, Nk:2 * Nk]
# inital x
data_x_acc = data_x0  #1j*np.zeros( (Nexample, 2*Nk) )
data_x_acc_c = 1j * np.zeros((Nexample, Nk))  #data_x0_c

mat_contents2 = sio.loadmat(pathdat + 'cnn_cs_testouty.mat')
test_outy0 = mat_contents2["test_outy"].astype(np.float32)
test_outy = np.zeros((Nexample, 4), dtype=np.float32)
dtest_outy = np.zeros((Nexample, 4), dtype=np.float32)
# for loop start here
for _ in range(10):
    #cuda simulate bloch for each pixel
Beispiel #7
0
def test():
    # read rf and tr arrays from mat file
    mat_contents = sio.loadmat(pathdat +
                               'mrf_t1t2b0pd_mrf_randphasecyc_traintest.mat')
    far = np.array(mat_contents["rf"].astype(np.complex128).squeeze())
    trr = np.array(mat_contents["trr"].astype(np.float64).squeeze())
    # input MRF time courses
    mat_contents2 = sio.loadmat(pathdat + 'datax1.mat')
    data_x = np.array(mat_contents2["datax1"]).astype(np.float64)
    # prepare for sequence simulation, y->x_hat
    Nk = far.shape[0]
    Nexample = data_x.shape[0]
    ti = 10  #ms
    M0 = np.array([0.0, 0.0, 1.0]).astype(np.float64)
    #image size
    nx = 217
    ny = 181
    # mask in ksp
    mask = ut.mask3d(nx, ny, Nk, [15, 15, 0], 0.4)
    #FTm           = opts.FFT2d_kmask(mask)
    FTm = cuopts.FFT2d_cuda_kmask(mask)

    #intial timing
    timing = utc.timing()
    # x real and imag parts should be combined
    data_x_c = np.zeros((Nexample, Nk), np.complex128)
    data_x_c = ssmrf.seqdata_realimag_2complex(data_x)
    #could do mask M here, data_x_c = M*data_x_c
    data_x_c = mask_ksp3d(nx, ny, Nk, FTm, data_x_c)
    data_x0_c = data_x_c
    data_x_c_pre = data_x_c
    data_x_acc = ssmrf.seqdata_complex_2realimag(data_x_c)
    # intial tmp data
    test_outy0 = np.zeros((Nexample, 4), dtype=np.float64)
    test_outy = np.zeros((Nexample, 4), dtype=np.float64)
    step = 1.0
    # for loop start here
    for _ in range(40):
        for _ in range(10):
            ssmrf.batch_apply_tf_cuda(Nexample, ny, sess.run, x, data_x_acc,
                                      y_conv, test_outy, keep_prob)
            #cuda bloch simulation for each pixel, parameters-->x
            T1r, T2r, dfr, PDr = ssmrf.set_par(test_outy)
            #timing.start()
            data_x_c = ssmrf.bloch_sim_batch_cuda(Nexample, 7 * ny, Nk, PDr,
                                                  T1r, T2r, dfr, M0, trr, far,
                                                  ti)
            print(
                np.linalg.norm(data_x_c -
                               ssmrf.seqdata_realimag_2complex(data_x_acc)))
            print(np.linalg.norm(data_x_c))
            data_x_acc = ssmrf.seqdata_complex_2realimag(data_x_c)
        #timing.stop().display('Bloch sim in loop ')
        #0.5* d||M*FT(x)-b||_2^2/dx = 2 * FT^-1(M*(FT(x) - b))
        # lamda * d(x^2)/dx = 2*lambda*x
        data_x_c = mask_ksp3d(nx, ny, Nk, FTm,
                              data_x_c - data_x0_c)  #data_x_c-data_x0_c#
        # gradient descent/update
        data_x_acc = data_x_acc - step * ssmrf.seqdata_complex_2realimag(
            data_x_c)
        #data_x_c   = data_x_c - mask_ksp3d(nx, ny, Nk, FTm, data_x_c) + data_x0_c
        #data_x_acc = ssmrf.seqdata_complex_2realimag(data_x_c)
        #data_x_c      = (wavel1_data(nx, ny, ssmrf.seqdata_realimag_2complex(data_x_acc)))
        # apply CNN model, x-->parameters
        print('gradient 0.5* d||f(p)-b||_2^2/dp: %g' %
              np.linalg.norm(data_x0_c - data_x_c))
        #could do soft thresholding on test_outy here, i.e. test_outy = threshold(test_outy)
        #test_outy     = wavel1_par(nx, ny, test_outy) #
        #test_outy     = constraints( test_outy )
        #test_outy     = tv_par(nx, ny, test_outy)
        #test_outy  = constraints( test_outy )
        sio.savemat(pathdat + 'cnn_cs_testouty.mat', {'test_outy': test_outy})