def afficher_tenseurs(fa_,evec,eva) : cfa = dti.color_fa(fa_, evec) sphere = dpd.default_sphere ren = window.Renderer() ren.add(actor.tensor_slicer(eva, evec, scalar_colors=cfa, sphere=sphere, scale=0.5)) window.record(ren, out_path='tensor.png', size=(1200, 1200))
def visualize(evals,evecs,viz_scale=0.5, fname='tensor_ellipsoids.png', size=(1000,1000)): # Do vizualisation interactive = True ren = window.Scene() from dipy.data import get_sphere #sphere = get_sphere('symmetric362') #sphere = get_sphere('repulsion724') sphere = get_sphere('symmetric642') # Calculate the colors. See dipy documentation. from dipy.reconst.dti import fractional_anisotropy, color_fa FA = fractional_anisotropy(evals) #print(FA) FA[np.isnan(FA)] = 0 FA = np.clip(FA, 0, 1) RGB = color_fa(FA, evecs) k=0 cfa = RGB[:, :, k:k+1] # Normalizing like this increases the contrast, but this will make the contrast different across plots #cfa /= cfa.max() # imgplot = plt.imshow(FA, cmap='gray') # plt.show() ren.add(actor.tensor_slicer(evals, evecs, sphere=sphere, scalar_colors=cfa, scale=viz_scale, norm=False)) if interactive: window.show(ren) window.record(ren, n_frames=1, out_path=fname, size=(1000, 1000))
fit_wls = dti_wls.fit(data) fa1 = fit_wls.fa evals1 = fit_wls.evals evecs1 = fit_wls.evecs cfa1 = dti.color_fa(fa1, evecs1) sphere = dpd.default_sphere """ We visualize the ODFs in the ROI using ``dipy.viz`` module: """ ren = window.Renderer() ren.add( actor.tensor_slicer(evals1, evecs1, scalar_colors=cfa1, sphere=sphere, scale=0.3)) print('Saving illustration as tensor_ellipsoids_wls.png') window.record(ren, out_path='tensor_ellipsoids_wls.png', size=(600, 600)) if interactive: window.show(ren) """ .. figure:: tensor_ellipsoids_wls.png :align: center Tensor Ellipsoids. """ window.clear(ren) """
ren = window.Renderer() evals = tenfit.evals[13:43, 44:74, 28:29] evecs = tenfit.evecs[13:43, 44:74, 28:29] """ We can color the ellipsoids using the ``color_fa`` values that we calculated above. In this example we additionally normalize the values to increase the contrast. """ cfa = RGB[13:43, 44:74, 28:29] cfa /= cfa.max() ren.add(actor.tensor_slicer(evals, evecs, scalar_colors=cfa, sphere=sphere, scale=0.3)) print('Saving illustration as tensor_ellipsoids.png') window.record(ren, n_frames=1, out_path='tensor_ellipsoids.png', size=(600, 600)) if interactive: window.show(ren) """ .. figure:: tensor_ellipsoids.png :align: center Tensor Ellipsoids. """ window.clear(ren)
""" fit_wls = dti_wls.fit(data) fa1 = fit_wls.fa evals1 = fit_wls.evals evecs1 = fit_wls.evecs cfa1 = dti.color_fa(fa1, evecs1) sphere = dpd.get_sphere('symmetric724') """ We visualize the ODFs in the ROI using ``dipy.viz`` module: """ ren = window.Renderer() ren.add(actor.tensor_slicer(evals1, evecs1, scalar_colors=cfa1, sphere=sphere, scale=0.3)) print('Saving illustration as tensor_ellipsoids_wls.png') window.record(ren, out_path='tensor_ellipsoids_wls.png', size=(600, 600)) if interactive: window.show(ren) """ .. figure:: tensor_ellipsoids_wls.png :align: center Tensor Ellipsoids. """ window.clear(ren) """
ren = window.Renderer() evals = tenfit.evals[13:43, 44:74, 28:29] evecs = tenfit.evecs[13:43, 44:74, 28:29] """ We can color the ellipsoids using the ``color_fa`` values that we calculated above. In this example we additionally normalize the values to increase the contrast. """ cfa = RGB[13:43, 44:74, 28:29] cfa /= cfa.max() ren.add(actor.tensor_slicer(evals, evecs, scalar_colors=cfa, sphere=sphere, scale=0.3)) print('Saving illustration as tensor_ellipsoids.png') window.record(ren, n_frames=1, out_path='tensor_ellipsoids.png', size=(600, 600)) if interactive: window.show(ren) """ .. figure:: tensor_ellipsoids.png :align: center Tensor Ellipsoids. """ window.clear(ren)
def test_tensor_slicer(interactive=False): evals = np.array([1.4, .35, .35]) * 10 ** (-3) evecs = np.eye(3) mevals = np.zeros((3, 2, 4, 3)) mevecs = np.zeros((3, 2, 4, 3, 3)) mevals[..., :] = evals mevecs[..., :, :] = evecs from dipy.data import get_sphere sphere = get_sphere('symmetric724') affine = np.eye(4) renderer = window.Renderer() tensor_actor = actor.tensor_slicer(mevals, mevecs, affine=affine, sphere=sphere, scale=.3) I, J, K = mevals.shape[:3] renderer.add(tensor_actor) renderer.reset_camera() renderer.reset_clipping_range() tensor_actor.display_extent(0, 1, 0, J, 0, K) tensor_actor.GetProperty().SetOpacity(1.0) if interactive: window.show(renderer, reset_camera=False) npt.assert_equal(renderer.GetActors().GetNumberOfItems(), 1) # Test extent big_extent = renderer.GetActors().GetLastActor().GetBounds() big_extent_x = abs(big_extent[1] - big_extent[0]) tensor_actor.display(x=2) if interactive: window.show(renderer, reset_camera=False) small_extent = renderer.GetActors().GetLastActor().GetBounds() small_extent_x = abs(small_extent[1] - small_extent[0]) npt.assert_equal(big_extent_x > small_extent_x, True) # Test empty mask empty_actor = actor.tensor_slicer(mevals, mevecs, affine=affine, mask=np.zeros(mevals.shape[:3]), sphere=sphere, scale=.3) npt.assert_equal(empty_actor.GetMapper(), None) # Test mask mask = np.ones(mevals.shape[:3]) mask[:2, :3, :3] = 0 cfa = color_fa(fractional_anisotropy(mevals), mevecs) tensor_actor = actor.tensor_slicer(mevals, mevecs, affine=affine, mask=mask, scalar_colors=cfa, sphere=sphere, scale=.3) renderer.clear() renderer.add(tensor_actor) renderer.reset_camera() renderer.reset_clipping_range() if interactive: window.show(renderer, reset_camera=False) mask_extent = renderer.GetActors().GetLastActor().GetBounds() mask_extent_x = abs(mask_extent[1] - mask_extent[0]) npt.assert_equal(big_extent_x > mask_extent_x, True) # test display tensor_actor.display() current_extent = renderer.GetActors().GetLastActor().GetBounds() current_extent_x = abs(current_extent[1] - current_extent[0]) npt.assert_equal(big_extent_x > current_extent_x, True) if interactive: window.show(renderer, reset_camera=False) tensor_actor.display(y=1) current_extent = renderer.GetActors().GetLastActor().GetBounds() current_extent_y = abs(current_extent[3] - current_extent[2]) big_extent_y = abs(big_extent[3] - big_extent[2]) npt.assert_equal(big_extent_y > current_extent_y, True) if interactive: window.show(renderer, reset_camera=False) tensor_actor.display(z=1) current_extent = renderer.GetActors().GetLastActor().GetBounds() current_extent_z = abs(current_extent[5] - current_extent[4]) big_extent_z = abs(big_extent[5] - big_extent[4]) npt.assert_equal(big_extent_z > current_extent_z, True) if interactive: window.show(renderer, reset_camera=False)
def dMRI2ODF_DTI(PATH): ''' Input the dMRI data return the ODF ''' dMRI_path = PATH + 'data.nii.gz' mask_path = PATH + 'nodif_brain_mask.nii.gz' dMRI_img = nib.load(dMRI_path) dMRI_data = dMRI_img.get_fdata() mask_img = nib.load(mask_path) mask = mask_img.get_fdata() ########## subsample ########## # dMRI_data = dMRI_data[45:-48,50:-65,51:-54,...] # mask = mask[45:-48,50:-65,51:-54] # breakpoint() dMRI_data = dMRI_data[:, 87, ...] mask = mask[:, 87, ...] for cnt in range(10): fig = plt.imshow(dMRI_data[:, :, cnt].transpose(1, 0), cmap='Greys', interpolation='nearest') plt.axis('off') # plt.imshow(dMRI_data[:,15,:,cnt].transpose(1,0),cmap='Greys') plt.savefig(str(cnt) + '.png', bbox_inches='tight', dpi=300, transparent=True, pad_inches=0) # breakpoint() bval = PATH + "bvals" bvec = PATH + "bvecs" radial_order = 6 zeta = 700 lambdaN = 1e-8 lambdaL = 1e-8 gtab = gradient_table(bvals=bval, bvecs=bvec) asm = ShoreModel(gtab, radial_order=radial_order, zeta=zeta, lambdaN=lambdaN, lambdaL=lambdaL) asmfit = asm.fit(dMRI_data, mask=mask) sphere = get_sphere('symmetric362') dMRI_odf = asmfit.odf(sphere) dMRI_odf[dMRI_odf <= 0] = 0 tenmodel = dti.TensorModel(gtab) tenfit = tenmodel.fit(dMRI_data, mask) dMRI_dti = tenfit.quadratic_form FA = fractional_anisotropy(tenfit.evals) FA[np.isnan(FA)] = 0 FA = np.clip(FA, 0, 1) RGB = color_fa(FA, tenfit.evecs) evals = tenfit.evals + 1e-20 evecs = tenfit.evecs cfa = RGB + 1e-20 cfa /= cfa.max() evals = np.expand_dims(evals, 2) evecs = np.expand_dims(evecs, 2) cfa = np.expand_dims(cfa, 2) ren = window.Scene() sphere = get_sphere('symmetric362') ren.add( actor.tensor_slicer(evals, evecs, scalar_colors=cfa, sphere=sphere, scale=0.5)) window.record(ren, n_frames=1, out_path='../data/tensor.png', size=(5000, 5000)) odf_ = dMRI_odf ren = window.Scene() sfu = actor.odf_slicer(np.expand_dims(odf_, 2), sphere=sphere, colormap="plasma", scale=0.5) ren.add(sfu) window.record(ren, n_frames=1, out_path='../data/odfs.png', size=(5000, 5000)) return None
def test_tensor_slicer(interactive=False): evals = np.array([1.4, .35, .35]) * 10 ** (-3) evecs = np.eye(3) mevals = np.zeros((3, 2, 4, 3)) mevecs = np.zeros((3, 2, 4, 3, 3)) mevals[..., :] = evals mevecs[..., :, :] = evecs from dipy.data import get_sphere sphere = get_sphere('symmetric724') affine = np.eye(4) renderer = window.Renderer() tensor_actor = actor.tensor_slicer(mevals, mevecs, affine=affine, sphere=sphere, scale=.3) I, J, K = mevals.shape[:3] renderer.add(tensor_actor) renderer.reset_camera() renderer.reset_clipping_range() tensor_actor.display_extent(0, 1, 0, J, 0, K) tensor_actor.GetProperty().SetOpacity(1.0) if interactive: window.show(renderer, reset_camera=False) npt.assert_equal(renderer.GetActors().GetNumberOfItems(), 1) # Test extent big_extent = renderer.GetActors().GetLastActor().GetBounds() big_extent_x = abs(big_extent[1] - big_extent[0]) tensor_actor.display(x=2) if interactive: window.show(renderer, reset_camera=False) small_extent = renderer.GetActors().GetLastActor().GetBounds() small_extent_x = abs(small_extent[1] - small_extent[0]) npt.assert_equal(big_extent_x > small_extent_x, True) # Test empty mask empty_actor = actor.tensor_slicer(mevals, mevecs, affine=affine, mask=np.zeros(mevals.shape[:3]), sphere=sphere, scale=.3) npt.assert_equal(empty_actor.GetMapper(), None) # Test mask mask = np.ones(mevals.shape[:3]) mask[:2, :3, :3] = 0 cfa = color_fa(fractional_anisotropy(mevals), mevecs) tensor_actor = actor.tensor_slicer(mevals, mevecs, affine=affine, mask=mask, scalar_colors=cfa, sphere=sphere, scale=.3) renderer.clear() renderer.add(tensor_actor) renderer.reset_camera() renderer.reset_clipping_range() if interactive: window.show(renderer, reset_camera=False) mask_extent = renderer.GetActors().GetLastActor().GetBounds() mask_extent_x = abs(mask_extent[1] - mask_extent[0]) npt.assert_equal(big_extent_x > mask_extent_x, True) # test display tensor_actor.display() current_extent = renderer.GetActors().GetLastActor().GetBounds() current_extent_x = abs(current_extent[1] - current_extent[0]) npt.assert_equal(big_extent_x > current_extent_x, True) if interactive: window.show(renderer, reset_camera=False) tensor_actor.display(y=1) current_extent = renderer.GetActors().GetLastActor().GetBounds() current_extent_y = abs(current_extent[3] - current_extent[2]) big_extent_y = abs(big_extent[3] - big_extent[2]) npt.assert_equal(big_extent_y > current_extent_y, True) if interactive: window.show(renderer, reset_camera=False) tensor_actor.display(z=1) current_extent = renderer.GetActors().GetLastActor().GetBounds() current_extent_z = abs(current_extent[5] - current_extent[4]) big_extent_z = abs(big_extent[5] - big_extent[4]) npt.assert_equal(big_extent_z > current_extent_z, True) if interactive: window.show(renderer, reset_camera=False)