Ejemplo n.º 1
0
def test_peaksFromModelParallel():
    SNR = 100
    S0 = 100

    _, fbvals, fbvecs = get_data('small_64D')

    bvals = np.load(fbvals)
    bvecs = np.load(fbvecs)

    gtab = gradient_table(bvals, bvecs)
    mevals = np.array(([0.0015, 0.0003, 0.0003],
                       [0.0015, 0.0003, 0.0003]))

    data, _ = multi_tensor(gtab, mevals, S0, angles=[(0, 0), (60, 0)],
                           fractions=[50, 50], snr=SNR)

    # test equality with/without multiprocessing
    model = SimpleOdfModel()
    pam_multi = peaks_from_model(model, data, _sphere, .5, 45,
                                 normalize_peaks=True, return_odf=True,
                                 return_sh=True, parallel=True)

    pam_single = peaks_from_model(model, data, _sphere, .5, 45,
                                  normalize_peaks=True, return_odf=True,
                                  return_sh=True, parallel=False)

    assert_array_almost_equal(pam_multi.gfa, pam_single.gfa)
    assert_array_almost_equal(pam_multi.qa, pam_single.qa)
    assert_array_almost_equal(pam_multi.peak_values, pam_single.peak_values)
    assert_array_equal(pam_multi.peak_indices, pam_single.peak_indices)
    assert_array_almost_equal(pam_multi.peak_dirs, pam_single.peak_dirs)
    assert_array_almost_equal(pam_multi.shm_coeff, pam_single.shm_coeff)
    assert_array_almost_equal(pam_multi.odf, pam_single.odf)
Ejemplo n.º 2
0
def test_peaksFromModel():
    data = np.zeros((10, 2))

    # Test basic case
    model = SimpleOdfModel()
    odf_argmax = _odf.argmax()
    pam = peaks_from_model(model, data, _sphere, .5, 45, normalize_peaks=True)

    assert_array_equal(pam.gfa, gfa(_odf))
    assert_array_equal(pam.peak_values[:, 0], 1.)
    assert_array_equal(pam.peak_values[:, 1:], 0.)
    mn, mx = _odf.min(), _odf.max()
    assert_array_equal(pam.qa[:, 0], (mx - mn) / mx)
    assert_array_equal(pam.qa[:, 1:], 0.)
    assert_array_equal(pam.peak_indices[:, 0], odf_argmax)
    assert_array_equal(pam.peak_indices[:, 1:], -1)

    # Test that odf array matches and is right shape
    pam = peaks_from_model(model, data, _sphere, .5, 45, return_odf=True)
    expected_shape = (len(data), len(_odf))
    assert_equal(pam.odf.shape, expected_shape)
    assert_true((_odf == pam.odf).all())
    assert_array_equal(pam.peak_values[:, 0], _odf.max())

    # Test mask
    mask = (np.arange(10) % 2) == 1

    pam = peaks_from_model(model,
                           data,
                           _sphere,
                           .5,
                           45,
                           mask=mask,
                           normalize_peaks=True)
    assert_array_equal(pam.gfa[~mask], 0)
    assert_array_equal(pam.qa[~mask], 0)
    assert_array_equal(pam.peak_values[~mask], 0)
    assert_array_equal(pam.peak_indices[~mask], -1)

    assert_array_equal(pam.gfa[mask], gfa(_odf))
    assert_array_equal(pam.peak_values[mask, 0], 1.)
    assert_array_equal(pam.peak_values[mask, 1:], 0.)
    mn, mx = _odf.min(), _odf.max()
    assert_array_equal(pam.qa[mask, 0], (mx - mn) / mx)
    assert_array_equal(pam.qa[mask, 1:], 0.)
    assert_array_equal(pam.peak_indices[mask, 0], odf_argmax)
    assert_array_equal(pam.peak_indices[mask, 1:], -1)
Ejemplo n.º 3
0
def test_peaks_shm_coeff():

    SNR = 100
    S0 = 100

    _, fbvals, fbvecs = get_data('small_64D')

    from dipy.data import get_sphere

    sphere = get_sphere('symmetric724')

    bvals = np.load(fbvals)
    bvecs = np.load(fbvecs)

    gtab = gradient_table(bvals, bvecs)
    mevals = np.array(([0.0015, 0.0003, 0.0003],
                       [0.0015, 0.0003, 0.0003]))

    data, _ = multi_tensor(gtab, mevals, S0, angles=[(0, 0), (60, 0)],
                             fractions=[50, 50], snr=SNR)

    from dipy.reconst.shm import CsaOdfModel

    model = CsaOdfModel(gtab, 4)

    pam = peaks_from_model(model, data[None,:], sphere, .5, 45,
                           return_odf=True, return_sh=True)
    # Test that spherical harmonic coefficients return back correctly
    B = np.linalg.pinv(pam.invB)
    odf2 = np.dot(pam.shm_coeff, B)
    assert_array_almost_equal(pam.odf, odf2)
    assert_equal(pam.shm_coeff.shape[-1], 45)

    pam = peaks_from_model(model, data[None,:], sphere, .5, 45,
                           return_odf=True, return_sh=False)
    assert_equal(pam.shm_coeff, None)

    pam = peaks_from_model(model, data[None, :], sphere, .5, 45,
                           return_odf=True, return_sh=True,
                           sh_basis_type='mrtrix')

    B = np.linalg.pinv(pam.invB)
    odf2 = np.dot(pam.shm_coeff, B)
    assert_array_almost_equal(pam.odf, odf2)
Ejemplo n.º 4
0
def test_peaksFromModel():
    data = np.zeros((10, 2))

    # Test basic case
    model = SimpleOdfModel()
    odf_argmax = _odf.argmax()
    pam = peaks_from_model(model, data, _sphere, .5, 45, normalize_peaks=True)

    assert_array_equal(pam.gfa, gfa(_odf))
    assert_array_equal(pam.peak_values[:, 0], 1.)
    assert_array_equal(pam.peak_values[:, 1:], 0.)
    mn, mx = _odf.min(), _odf.max()
    assert_array_equal(pam.qa[:, 0], (mx - mn) / mx)
    assert_array_equal(pam.qa[:, 1:], 0.)
    assert_array_equal(pam.peak_indices[:, 0], odf_argmax)
    assert_array_equal(pam.peak_indices[:, 1:], -1)

    # Test that odf array matches and is right shape
    pam = peaks_from_model(model, data, _sphere, .5, 45, return_odf=True)
    expected_shape = (len(data), len(_odf))
    assert_equal(pam.odf.shape, expected_shape)
    assert_true((_odf == pam.odf).all())
    assert_array_equal(pam.peak_values[:, 0], _odf.max())

    # Test mask
    mask = (np.arange(10) % 2) == 1

    pam = peaks_from_model(model, data, _sphere, .5, 45, mask=mask,
                           normalize_peaks=True)
    assert_array_equal(pam.gfa[~mask], 0)
    assert_array_equal(pam.qa[~mask], 0)
    assert_array_equal(pam.peak_values[~mask], 0)
    assert_array_equal(pam.peak_indices[~mask], -1)

    assert_array_equal(pam.gfa[mask], gfa(_odf))
    assert_array_equal(pam.peak_values[mask, 0], 1.)
    assert_array_equal(pam.peak_values[mask, 1:], 0.)
    mn, mx = _odf.min(), _odf.max()
    assert_array_equal(pam.qa[mask, 0], (mx - mn) / mx)
    assert_array_equal(pam.qa[mask, 1:], 0.)
    assert_array_equal(pam.peak_indices[mask, 0], odf_argmax)
    assert_array_equal(pam.peak_indices[mask, 1:], -1)
Ejemplo n.º 5
0
def pfm(model, data, mask, sphere, parallel=False, min_angle=25.0,
        relative_peak_th=0.1, sh_order=8):

    print 'Peak extraction with sh_order : ', sh_order, ' min_angle: ', min_angle, 'deg and relative peak threshold of : ', relative_peak_th

    peaks = peaks_from_model(model=model,
                             data=data,
                             mask=mask,
                             sphere=sphere,
                             relative_peak_threshold=relative_peak_th,
                             min_separation_angle=min_angle,
                             return_odf=False,
                             return_sh=True,
                             normalize_peaks=False,
                             sh_order=sh_order,
                             sh_basis_type='mrtrix',
                             npeaks=5,
                             parallel=parallel,
                             nbr_process=6)
    return peaks
        
        response_ar = np.array([mean_evals[0], mean_evals[1], mean_evals[1], S0])
        if i < 10 :
            np.savetxt(dname + 'response_0' + str(i) + '.txt', response_ar)
        else :
            np.savetxt(dname +  'response_' + str(i) + '.txt', response_ar)

        csd_model = ConstrainedSphericalDeconvModel(gtab, (evals, S0))
        from dipy.reconst.odf import peaks_from_model
        peaks = peaks_from_model(model=csd_model,
                                 data=data,
                                 sphere=sphere,
                                 relative_peak_threshold=0.25,
                                 min_separation_angle=45,
                                 mask=wm_mask,
                                 return_odf=False, 
                                 return_sh=True, 
                                 normalize_peaks=False,
                                 sh_order=8,
                                 sh_basis_type='mrtrix',
                                 npeaks=5, 
                                 parallel=True, nbr_process=8)

    

        shm_coeff = peaks.shm_coeff
        
        
        myPeaksDirs = peaks.peak_dirs
        test = np.reshape(myPeaksDirs, [myPeaksDirs.shape[0], 
                                        myPeaksDirs.shape[1], 
Ejemplo n.º 7
0
					# Create diffusion MR gradients
					gtab = gradient_table(bvals, bvecs)

					# Resample diffusion data and mask 
					print('... resample data')
					data, affine = resample(data, affine, zooms, new_zooms)
					mask, affine = resample(mask, affine, zooms, new_zooms, order=0)

					# Deconvolution
					t = time.time()
					print('... perform deconvolution')
					dsmodel = DiffusionSpectrumDeconvModel(gtab)
					dsipeaks = peaks_from_model(model=dsmodel,
					                            data=data,
					                            sphere=sphere,
					                            relative_peak_threshold=.5,
					                            min_separation_angle=25,
					                            mask=mask,
					                            return_odf=True,
					                            normalize_peaks=True)
					#save_pickle(os.path.join(main_dir,tp,'CMP','scalars','dsideconv.pkl'), dsipeaks)

					# Set headers for output images
					hdr_gfa = nib.load(fimg).get_header()
					hdr_gfa.set_data_shape(mask.shape)
					hdr_gfa.set_zooms(new_zooms)
					hdr_peaks = nib.load(fimg).get_header()
					hdr_peaks.set_data_shape((mask.shape[0],mask.shape[1],mask.shape[2],5))
					hdr_peaks.set_zooms((new_zooms[0],new_zooms[1],new_zooms[2],1.))
					hdr_odf = nib.load(fimg).get_header()
					hdr_odf.set_data_shape((mask.shape[0],mask.shape[1],mask.shape[2],sphere.x.shape[0]))
					hdr_odf.set_zooms((new_zooms[0],new_zooms[1],new_zooms[2],1.))			
Ejemplo n.º 8
0
Distribution Function) and return for
example the peaks and their indices, or GFA which is similar to FA but for ODF
based models. This function mainly needs a reconstruction model, the data and a
sphere as input. The sphere is an object that represents the spherical discrete
grid where the ODF values will be evaluated.
"""

sphere = get_sphere('symmetric724')

start_time = time.time()
csapeaks_parallel = peaks_from_model(model=csamodel,
                                     data=data,
                                     sphere=sphere,
                                     relative_peak_threshold=.8,
                                     min_separation_angle=45,
                                     mask=None,
                                     return_odf=False,
                                     normalize_peaks=True,
                                     ravel_peaks=False,
                                     npeaks=5,
                                     parallel=True,
                                     nbr_process=2)  # default multiprocessing.cpu_count()

time_parallel = time.time() - start_time
print("peaks_from_model using 2 process ran in : " +
      str(time_parallel) + " seconds")
"""
peaks_from_model using 2 process ran in  : 114.333221912 seconds, using 2 process
"""

start_time = time.time()
csapeaks = peaks_from_model(model=csamodel,
Ejemplo n.º 9
0
"""
`Peaks_from_model` is used to calculate properties of the ODFs (Orientation
Distribution Function) and return for
example the peaks and their indices, or GFA which is similar to FA but for ODF
based models. This function mainly needs a reconstruction model, the data and a
sphere as input. The sphere is an object that represents the spherical discrete
grid where the ODF values will be evaluated.
"""

sphere = get_sphere('symmetric724')

csapeaks = peaks_from_model(model=csamodel,
                            data=maskdata,
                            sphere=sphere,
                            relative_peak_threshold=.8,
                            min_separation_angle=45,
                            mask=mask,
                            return_odf=False,
                            normalize_peaks=True)

GFA = csapeaks.gfa

print('GFA.shape (%d, %d, %d)' % GFA.shape)

"""
GFA.shape ``(81, 106, 76)``

Apart from GFA, csapeaks also has the attributes peak_values, peak_indices and
ODF. peak_values shows the maxima values of the ODF and peak_indices gives us
their position on the discrete sphere that was used to do the reconstruction of
the ODF. In order to obtain the full ODF, return_odf should be True. Before
Ejemplo n.º 10
0
ODF = gqfit.odf(sphere)

print('ODF.shape (%d, %d, %d)' % ODF.shape)

"""
ODF.shape ``(96, 96, 724)``

Using peaks_from_model we can find the main peaks of the ODFs and other
properties.
"""

gqpeaks = peaks_from_model(model=gqmodel,
                           data=dataslice,
                           sphere=sphere,
                           relative_peak_threshold=.8,
                           min_separation_angle=45,
                           mask=mask,
                           return_odf=False,
                           normalize_peaks=True)

gqpeak_values = gqpeaks.peak_values

"""
gqpeak_indices show which sphere points have the maximum values.
"""

gqpeak_indices = gqpeaks.peak_indices

"""
It is also possible to calculate GFA.
"""
Ejemplo n.º 11
0
def test_peaks_shm_coeff():

    SNR = 100
    S0 = 100

    _, fbvals, fbvecs = get_data('small_64D')

    from dipy.data import get_sphere

    sphere = get_sphere('symmetric724')

    bvals = np.load(fbvals)
    bvecs = np.load(fbvecs)

    gtab = gradient_table(bvals, bvecs)
    mevals = np.array(([0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003]))

    data, _ = multi_tensor(gtab,
                           mevals,
                           S0,
                           angles=[(0, 0), (60, 0)],
                           fractions=[50, 50],
                           snr=SNR)

    from dipy.reconst.shm import CsaOdfModel

    model = CsaOdfModel(gtab, 4)

    pam = peaks_from_model(model,
                           data[None, :],
                           sphere,
                           .5,
                           45,
                           return_odf=True,
                           return_sh=True)
    # Test that spherical harmonic coefficients return back correctly
    B = np.linalg.pinv(pam.invB)
    odf2 = np.dot(pam.shm_coeff, B)
    assert_array_almost_equal(pam.odf, odf2)
    assert_equal(pam.shm_coeff.shape[-1], 45)

    pam = peaks_from_model(model,
                           data[None, :],
                           sphere,
                           .5,
                           45,
                           return_odf=True,
                           return_sh=False)
    assert_equal(pam.shm_coeff, None)

    pam = peaks_from_model(model,
                           data[None, :],
                           sphere,
                           .5,
                           45,
                           return_odf=True,
                           return_sh=True,
                           sh_basis_type='mrtrix')

    B = np.linalg.pinv(pam.invB)
    odf2 = np.dot(pam.shm_coeff, B)
    assert_array_almost_equal(pam.odf, odf2)
Ejemplo n.º 12
0
"""

ODF = gqfit.odf(sphere)

print('ODF.shape (%d, %d, %d)' % ODF.shape)
"""
ODF.shape ``(96, 96, 724)``

Using peaks_from_model we can find the main peaks of the ODFs and other
properties.
"""

gqpeaks = peaks_from_model(model=gqmodel,
                           data=dataslice,
                           sphere=sphere,
                           relative_peak_threshold=.8,
                           min_separation_angle=45,
                           mask=mask,
                           return_odf=False,
                           normalize_peaks=True)

gqpeak_values = gqpeaks.peak_values
"""
gqpeak_indices show which sphere points have the maximum values.
"""

gqpeak_indices = gqpeaks.peak_indices
"""
It is also possible to calculate GFA.
"""

GFA = gqpeaks.gfa
Ejemplo n.º 13
0
"""

csamodel = CsaOdfModel(gtab, 4)

"""
Peaks from model is used to calculate properties of the ODFs and return for
example the peaks and their indices, or GFA which is similar to FA but for ODF
based models.
"""

from dipy.reconst.odf import peaks_from_model

peaks = peaks_from_model(model=csamodel, 
                         data=data2, 
                         sphere=sphere, 
                         relative_peak_threshold=.8, 
                         min_separation_angle=45, 
                         mask=mask, 
                         normalize_peaks=True)

"""
This time we will not use FA as input to EuDX but we will use directly
the maximum peaks of the ODF. The a_low threshold is the 
"""

eu = EuDX(peaks.peak_values, 
          peaks.peak_indices, 
          odf_vertices = sphere.vertices, 
          a_low=0.2)

csa_streamlines = [streamline for streamline in eu]
    from dipy.data import get_sphere
    sphere = get_sphere('symmetric724')

    print('>>> Find peaks...')
    from time import time
    t0 = time()

    from dipy.reconst.odf import peaks_from_model
    peaks = peaks_from_model(model=shore_model,
                         data=data,
                         mask=mask,
                         sphere=sphere,
                         relative_peak_threshold=0.3,
                         min_separation_angle=25,
                         return_odf=False,
                         return_sh=True,
                         normalize_peaks=False,
                         sh_order=8,
                         npeaks=5,
                         parallel=False,
                         nbr_process=6)

    t1 = time()
    np.savetxt(join(dname,'shore_peaks_time.txt'), (t1-t0,))
    del data

    print('>>> Save peak indices...')

    fpeaks = join(dname, 'dwi_peaks_1x1x1.nii.gz')
    nib.save(nib.Nifti1Image(peaks.peak_indices, affine), fpeaks)