def __init__(self, data, xlabel='', title='', N=2**14, lmax=None): self.data = np.array(data) self.N = N # points on the sphere # Plotting self.xlabel = xlabel self.title = title # Setup renderer self.ren, self.renWin, self.iren = utilvtk.setup_render() # Calculate dimensions if lmax is None: self.lmax, mm = utilsh.j2lm(len(self.data) - 1) else: self.lmax = lmax self.J = utilsh.maxl2maxj(self.lmax) # Fill the rest of the last l band with zeros if self.data.shape[-1] != self.J: temp = np.zeros(self.J) temp[:self.data.shape[-1]] = self.data self.data = temp else: self.data = data # Calc points for spherical plotting self.xyz = utilsh.fibonacci_sphere(N, xyz=True) self.B = utilsh.calcB(self.N, self.J) self.Binv = np.linalg.pinv(self.B, rcond=1e-15)
def __init__(self, data_xyz, data_J, shape=[10,10,4], N=2**12, title=''): self.data_xyz = np.array(data_xyz) self.data_J = np.array(data_J) self.M = self.data_xyz.shape[0] self.N = N self.shape = shape # um self.xlabel = utilmpl.shape2xlabel(self.shape) self.title = title # Calculate dimensions self.lmax, mm = utilsh.j2lm(self.data_J.shape[-1] - 1) self.J = utilsh.maxl2maxj(self.lmax) # Fill the rest of the last l band with zeros if self.data_J.shape[-1] != self.J: temp = np.zeros(self.J) temp[:self.data_J.shape[-1]] = np.array(self.data_J) self.data_J = temp # Calc points for spherical plotting self.xyz = utilsh.fibonacci_sphere(N, xyz=True) self.B = utilsh.calcB(self.N, self.J)
def guv(center=[0,0,0], radius=0.5, N=2**10, M=2**10, ellip_ratio=0.5, dist_type='ellipsoid'): xyz_guv = utilsh.fibonacci_sphere(M/8, xyz=True) # Points on guv # Respect D4 symmetry xyz_guv_flip = np.vstack([xyz_guv[:,1], xyz_guv[:,0], xyz_guv[:,2]]).T xyz_guv = np.vstack([xyz_guv, xyz_guv*[-1,1,1], xyz_guv*[1,-1,1], xyz_guv*[-1,-1,1], xyz_guv_flip, xyz_guv_flip*[-1,1,1], xyz_guv_flip*[1,-1,1], xyz_guv_flip*[-1,-1,1]]) xyz_list = (xyz_guv*radius + center) # Scaled and center j_list = np.zeros((M, N)) for m in tqdm(range(M)): if dist_type == 'ellipsoid': j_list[m,:] = uniaxial_ellipsoid(1, ellip_ratio, xyz_guv[m], N=N) elif dist_type == 'uniform': j_list[m,:] = np.ones((N,)) return xyz_list, j_list
def to_R3toR3_xyz(self, N=2**10): xyz = utilsh.fibonacci_sphere(N, xyz=True) max_indices = np.argmax(self.data_j, axis=1) xyz_max = np.einsum('ij,i->ij', xyz[max_indices], np.max(self.data_j, axis=-1)) return R3toR3.xyz_list(self.data_xyz, xyz_max, shape=self.shape, xlabel=self.xlabel, title='Peaks')
def ellipsoid(pradii, paxes, N=2**12): xyz = utilsh.fibonacci_sphere(N, xyz=True) xyz_rot = np.einsum('ij,kj->ik', xyz, paxes) # Rotate return 1/np.linalg.norm(xyz_rot/np.array(pradii), ord=2, axis=-1)
def draw_sphere_field(ren, centers, radii, plot_negative=True): M = radii.shape[0] N = radii.shape[1] vertices = utilsh.fibonacci_sphere(radii.shape[-1], xyz=True) faces = ConvexHull(vertices).simplices # Split into positive and negative if plot_negative: iradiis = [radii.clip(min=0), -radii.clip(max=0)] else: iradiis = [radii.clip(min=0)] # For both positive and negative for i, iradii in enumerate(iradiis): # Calculate vertices xyz_vertices = np.einsum('ij,ki->ikj', vertices, iradii) + centers all_xyz = xyz_vertices.reshape(-1, xyz_vertices.shape[-1], order='F') # Reshape all_xyz_vtk = numpy_support.numpy_to_vtk(all_xyz, deep=True) # Convert to vtk # Calculate faces all_faces = [] for j in range(M): all_faces.append(faces + j*N) all_faces = np.concatenate(all_faces) all_faces = np.hstack((3 * np.ones((len(all_faces), 1)), all_faces)) all_faces = np.ascontiguousarray(all_faces.ravel(), dtype='i8') all_faces_vtk = numpy_support.numpy_to_vtkIdTypeArray(all_faces, deep=True) # Populate points points = vtk.vtkPoints() points.SetData(all_xyz_vtk) # Calculate cells ncells = len(all_faces) cells = vtk.vtkCellArray() cells.SetCells(ncells, all_faces_vtk) # Populate polydata polydata = vtk.vtkPolyData() polydata.SetPoints(points) polydata.SetPolys(cells) # Calculate colors # TODO: Generalize colormaps cols = 255*np.ones(all_xyz.shape) iradiif = radii.flatten() if i == 0: # Red to white cols[:,1] = 255*(1-iradiif/(np.max(iradiif) + 1e-5)) cols[:,2] = 255*(1-iradiif/(np.max(iradiif) + 1e-5)) else: # Blue to white cols[:,0] = 255*(iradiif/(np.max(iradiif) + 1e-5)) cols[:,1] = 255*(iradiif/(np.max(iradiif) + 1e-5)) vtk_colors = numpy_support.numpy_to_vtk(cols, deep=True, array_type=vtk.VTK_UNSIGNED_CHAR) polydata.GetPointData().SetScalars(vtk_colors) mapper = vtk.vtkPolyDataMapper() mapper.SetInputData(polydata) # Create actor actor = vtk.vtkActor() actor.SetMapper(mapper) actor.GetProperty().SetAmbient(0.25) ren.AddActor(actor)