def show_coverage_3d(rg, field, rm=None, vmin=None, sphere_colormap='jet'): """ """ # Resolution with which the small spheres are drawn pt_resolution = 20 # Size factor for small spheres scale_factor = 0.1 # missing directions in red, if any if rm is not None: xm, ym, zm = rm theta, phi, rho = coord.car2sph(xm, ym, zm) plot.scatter_3D(theta, phi, resolution=pt_resolution, scale_factor=scale_factor, name='Bad direction') # 'good' directions in green xg, yg, zg = rg theta, phi, rho = coord.car2sph(xg, yg, zg) # As simple spheres plot.scatter_3D(theta, phi, color=(0, 1, 0), scale_factor=scale_factor, resolution=pt_resolution, name='Good directions') npts_lat, npts_lon = field.shape theta = np.linspace(0, np.pi, npts_lat) phi = np.linspace(0, 2 * np.pi, npts_lon) s = plot.surf_grid_3D(field, theta, phi, vmin=vmin, name='Coverage') mlab = plot.get_mlab() mlab.colorbar(s, orientation='horizontal') mlab.show()
sph_io.savez('odf_coeffs_%03d' % k, beta=beta, kernel_N=kernel_N, separation=gamma, weights=w, mevecs=[R0, R1], signal=E) nnz = np.sum(beta != 0) print 'Compression: %.2f%%' % ((len(beta) - nnz) / len(beta) * 100) print 'Non-zero coefficients: %d/%d' % (nnz, len(beta)) print 'Error (Q-space):', np.linalg.norm(X.dot(beta) - y) ## beta[beta < 1e-5] = 0 # ========================== # ODF-domain: Peak detection # ========================== if visualize_odf: mask = (beta != 0) plot.scatter_3D(theta_odf, phi_odf, color=(0, 0, 1)) plot.scatter_3D(theta_odf[mask], phi_odf[mask], 1 + beta[mask]/beta.max(), transparent=True, color=(1, 0, 0), scale_mode='scalar', scale_factor=0.1, opacity=0.7) f1_theta, f1_phi, f1_r = coord.car2sph(*R0.dot([1, 0, 0])) f2_theta, f2_phi, f2_r = coord.car2sph(*R1.dot([1, 0, 0])) plot.scatter_3D(f1_theta, f1_phi, color=(0, 1, 0), scale_factor=0.15) plot.scatter_3D(f2_theta, f2_phi, color=(0, 1, 0), scale_factor=0.15) plot.show()
def weighted_kmeans(c_theta, c_phi, p_theta, p_phi, v): D = 1 / (eps + sphere.arc_length(c_theta, c_phi, p_theta[:, None], p_phi[:, None]))**2 # D = np.exp(-sphere.arc_length(c_theta, c_phi, p_theta[:, None], p_phi[:, None])**2) D *= v[:, None] D /= D.sum(axis=0) P = np.column_stack((p_theta, p_phi)) C = (D[:, :, None] * P[:, None, :]).sum(axis=0) return C[:, 0], C[:, 1] for i in range(50): c_theta, c_phi = weighted_kmeans(c_theta, c_phi, theta, phi, beta) angles = np.rad2deg(np.arccos(sphere.cos_inc_angle( c_theta, c_phi, c_theta[:, None], c_phi[:, None]))) angles[angles < 1e-3] = 0 print "True separation:", separation print "Calculated:", angles mask = (beta != 0) plot.scatter_3D(theta, phi, color=(0, 0, 1)) plot.scatter_3D(theta[mask], phi[mask], 1 + beta[mask]/beta.max(), transparent=True, color=(1, 0, 0), scale_mode='scalar', scale_factor=0.1, opacity=0.7) plot.scatter_3D(c_theta, c_phi, color=(1, 1, 0)) plot.show()
import sys sys.path.insert(0, '..') import numpy as np from sphdif import plot, coord from dipy.core.subdivide_octahedron import create_unit_sphere s = create_unit_sphere(3) from mayavi import mlab mlab.figure(bgcolor=(1, 1, 1), fgcolor=(0, 0, 0)) mask = s.theta <= np.pi/2. plot.scatter_3D(s.theta[mask], s.phi[mask], color=(0, 0, 1)) #ef = s.edges.ravel() #for e in s.edges: # mlab.plot3d(s.x[e], s.y[e], s.z[e], tube_radius=None) N = 20 mlab.quiver3d([s.x[N]], [s.y[N]], [s.z[N]], color=(1, 0, 0), mode='2darrow') plot.show()