Exemple #1
0
def add_rician_noise_to_file(filename, stds, shuffle=False):
    #stds = numpy.arange(0.01,0.1,0.01)
    flt_filename = filename + '.flt'
    dsize, data = flt_utils.read_flt_file(flt_filename)
    inds = numpy.arange(data.shape[1])
    for std in stds:
        s_noise = add_rician_noise(data, std)
        k, n = s_noise.shape
        if shuffle:
            numpy.random.shuffle(inds)
        output_filename = '%s_sd%03d.flt' % (filename, std * 100)
        txt_filename = '%s_sd%03d.txt' % (filename, std * 100)
        flt_utils.write_flt_file(output_filename, s_noise[:, inds], dsize)
        f = open(txt_filename, 'w')
        f.write('\n'.join([str(ind) for ind in inds]))
        f.close()
def add_rician_noise_to_file(filename, stds, shuffle = False):
    #stds = numpy.arange(0.01,0.1,0.01)
    flt_filename = filename + '.flt'
    dsize, data = flt_utils.read_flt_file(flt_filename)
    inds = numpy.arange(data.shape[1])
    for std in stds:
        s_noise = add_rician_noise(data, std)
        k,n = s_noise.shape
        if shuffle:
            numpy.random.shuffle(inds)
        output_filename = '%s_sd%03d.flt'%(filename,std*100)
        txt_filename = '%s_sd%03d.txt'%(filename,std*100)
        flt_utils.write_flt_file(output_filename,s_noise[:,inds],dsize)
        f = open(txt_filename,'w')
        f.write('\n'.join([str(ind) for ind in inds]))
        f.close()
def test(gradient_file, signal_file, knots_file, _lambda = 0):
    """
    Fit the spherical thin-plate spline model to descrete signal data.
    Reference:
    Ferreira et al. Directional Log-Spline Distributions,
    Bayesian Analysis 3(2) 2008, pp. 297-316 [Eq.(3)]

    In [331]: c = test('81vectors.txt','3fib.mhd')
    2.92138710735e-013

    In [332]: c = test('81vectors.txt','2fib.mhd')
    2.37209920248e-013

    In [333]: c = test('81vectors.txt','1fib.mhd')
    3.90984974495e-013

    """
    import mhd_utils
    import flt_utils
    g = loadtxt(gradient_file) #diffusion-mri/Python/data/81vectors.txt
    v = loadtxt(knots_file)
    #vv = r_[v,-v]
    #gg = r_[g,-g]
    _R = assemble_kernel_matrix(g, v)
    #_one = ones([81,1])
    #_eye = eye(81)
    #_R = assemble_kernel_matrix(gg, vv)
    _one_column = ones([len(g),1])
    _one_row = ones([1,len(v)])
    #_eye = eye(81*2)
    #A = r_[c_[_one,_R + _lambda * _eye], c_[0, _one.T]]
    A = r_[c_[_one_column,_R], c_[0, _one_row]]
    basename = os.path.splitext(signal_file)[1]
    if basename == '.flt':
        [dsize,s] = flt_utils.read_flt_file(signal_file)
    elif basename == '.mhd':
        [s,dsize] = mhd_utils.load_raw_data_with_mhd(signal_file)
    else:
        return

    _zero_row = zeros([1,s.shape[1]])
    c = dot(linalg.pinv(A),r_[s,_zero_row])
    #return A,s,c,_R
    #c = dot(linalg.pinv(A),r_[s,s,[[0]]])
    print max(abs(c[0] + dot(_R,c[1::]) - s))
    return A,c,s,g,_R
def test(gradient_file, signal_file, knots_file, _lambda=0):
    """
    Fit the spherical thin-plate spline model to descrete signal data.
    Reference:
    Ferreira et al. Directional Log-Spline Distributions,
    Bayesian Analysis 3(2) 2008, pp. 297-316 [Eq.(3)]

    In [331]: c = test('81vectors.txt','3fib.mhd')
    2.92138710735e-013

    In [332]: c = test('81vectors.txt','2fib.mhd')
    2.37209920248e-013

    In [333]: c = test('81vectors.txt','1fib.mhd')
    3.90984974495e-013

    """
    import mhd_utils
    import flt_utils
    g = loadtxt(gradient_file)  #diffusion-mri/Python/data/81vectors.txt
    v = loadtxt(knots_file)
    #vv = r_[v,-v]
    #gg = r_[g,-g]
    _R = assemble_kernel_matrix(g, v)
    #_one = ones([81,1])
    #_eye = eye(81)
    #_R = assemble_kernel_matrix(gg, vv)
    _one_column = ones([len(g), 1])
    _one_row = ones([1, len(v)])
    #_eye = eye(81*2)
    #A = r_[c_[_one,_R + _lambda * _eye], c_[0, _one.T]]
    A = r_[c_[_one_column, _R], c_[0, _one_row]]
    basename = os.path.splitext(signal_file)[1]
    if basename == '.flt':
        [dsize, s] = flt_utils.read_flt_file(signal_file)
    elif basename == '.mhd':
        [s, dsize] = mhd_utils.load_raw_data_with_mhd(signal_file)
    else:
        return

    _zero_row = zeros([1, s.shape[1]])
    c = dot(linalg.pinv(A), r_[s, _zero_row])
    #return A,s,c,_R
    #c = dot(linalg.pinv(A),r_[s,s,[[0]]])
    print max(abs(c[0] + dot(_R, c[1::]) - s))
    return A, c, s, g, _R