def points(self, size=1.0, colorlist=[]): """Display the system as points. :param float size: the size of the points. """ if not len(colorlist) == len(self.topology['atom_types']): colorlist = [ get_atom_color(t) for t in self.topology['atom_types'] ] sizes = [size] * len(self.topology['atom_types']) points = self.add_representation( 'points', { 'coordinates': self.coordinates.astype('float32'), 'colors': colorlist, 'sizes': sizes }) # Update closure def update(self=self, points=points): self.update_representation( points, {'coordinates': self.coordinates.astype('float32')}) self.update_callbacks.append(update)
def scene(self): from chemview.utils import get_atom_color scene = Scene() scene.add_representation('points', {'coordinates' : self.r_array, 'sizes': [1] * self.n_atoms, 'colors': [get_atom_color(t) for t in self.type_array]}) return scene
def quick_view(structure, bonds=True, conventional=False, transform=None, show_box=True, bond_tol=0.2, stick_radius=0.1): """ A function to visualize pymatgen Structure objects in jupyter notebook using chemview package. Args: structure: pymatgen Structure bonds: (bool) visualize bonds. Bonds are found by comparing distances to added covalent radii of pairs. Defaults to True. conventional: (bool) use conventional cell. Defaults to False. transform: (list) can be used to make supercells with pymatgen.Structure.make_supercell method show_box: (bool) unit cell is shown. Defaults to True. bond_tol: (float) used if bonds=True. Sets the extra distance tolerance when finding bonds. stick_radius: (float) radius of bonds. Returns: A chemview.MolecularViewer object """ s = structure.copy() if conventional: s = SpacegroupAnalyzer(s).get_conventional_standard_structure() if transform: s.make_supercell(transform) atom_types = [i.symbol for i in s.species] if bonds: bonds = [] for i in range(s.num_sites - 1): sym_i = s[i].specie.symbol for j in range(i + 1, s.num_sites): sym_j = s[j].specie.symbol max_d = CovalentRadius.radius[sym_i] + CovalentRadius.radius[sym_j] + bond_tol if s.get_distance(i, j, np.array([0,0,0])) < max_d: bonds.append((i, j)) bonds = bonds if bonds else None mv = MolecularViewer(s.cart_coords, topology={'atom_types': atom_types, 'bonds': bonds}) if bonds: mv.ball_and_sticks(stick_radius=stick_radius) for i in s.sites: el = i.specie.symbol coord = i.coords r = CovalentRadius.radius[el] mv.add_representation('spheres', {'coordinates': coord.astype('float32'), 'colors': [get_atom_color(el)], 'radii': [r * 0.5], 'opacity': 1.0}) if show_box: o = np.array([0, 0, 0]) a, b, c = s.lattice.matrix[0], s.lattice.matrix[1], s.lattice.matrix[2] starts = [o, o, o, a, a, b, b, c, c, a + b, a + c, b + c] ends = [a, b, c, a + b, a + c, b + a, b + c, c + a, c + b, a + b + c, a + b + c, a + b + c] colors = [0xffffff for i in range(12)] mv.add_representation('lines', {'startCoords': np.array(starts), 'endCoords': np.array(ends), 'startColors': colors, 'endColors': colors}) return mv
def ball_and_sticks(self, ball_radius=0.05, stick_radius=0.02, colorlist=[]): """Display the system using a ball and stick representation. """ # Add the spheres if not len(colorlist) == len(self.topology['atom_types']): colorlist = [ get_atom_color(t) for t in self.topology['atom_types'] ] sizes = [ball_radius] * len(self.topology['atom_types']) spheres = self.add_representation( 'spheres', { 'coordinates': self.coordinates.astype('float32'), 'colors': colorlist, 'radii': sizes }) def update(self=self, spheres=spheres): self.update_representation( spheres, {'coordinates': self.coordinates.astype('float32')}) self.update_callbacks.append(update) # Add the cylinders if 'bonds' in self.topology: start_idx, end_idx = zip(*self.topology['bonds']) cylinders = self.add_representation( 'cylinders', { 'startCoords': self.coordinates[list(start_idx)], 'endCoords': self.coordinates[list(end_idx)], 'colors': [0xcccccc] * len(self.coordinates), 'radii': [stick_radius] * len(self.coordinates) }) # Update closure def update(self=self, rep=cylinders, start_idx=start_idx, end_idx=end_idx): self.update_representation( rep, { 'startCoords': self.coordinates[list(start_idx)], 'endCoords': self.coordinates[list(end_idx)] }) self.update_callbacks.append(update)
def display(self, backend='chemview', **kwargs): if backend == 'chemview': from ..notebook import display_system mv = display_system(self, **kwargs) return mv if backend == 'povray': from ..graphics import Scene from chemview.render import render_povray from chemview.utils import get_atom_color scene = Scene() scene.add_representation('points', {'coordinates' : self.r_array, 'sizes': [1] * self.n_atoms, 'colors': [get_atom_color(t) for t in self.type_array]}) extra_opts = {} if "radiosity" in kwargs: extra_opts.update({'radiosity' : kwargs['radiosity']}) scene.camera.autozoom(self.r_array) return render_povray(scene.to_dict(), extra_opts=extra_opts)
def lines(self, colorlist=[]): '''Display the system bonds as lines. ''' if "bonds" not in self.topology: return bond_start, bond_end = zip(*self.topology['bonds']) bond_start = np.array(bond_start) bond_end = np.array(bond_end) if len(colorlist) == len(self.topology['atom_types']): color_array = np.array(colorlist) else: color_array = np.array( [get_atom_color(t) for t in self.topology['atom_types']]) lines = self.add_representation( 'lines', { 'startCoords': self.coordinates[bond_start], 'endCoords': self.coordinates[bond_end], 'startColors': color_array[bond_start].tolist(), 'endColors': color_array[bond_end].tolist() }) def update(self=self, lines=lines): bond_start, bond_end = zip(*self.topology['bonds']) bond_start = np.array(bond_start) bond_end = np.array(bond_end) self.update_representation( lines, { 'startCoords': self.coordinates[bond_start], 'endCoords': self.coordinates[bond_end] }) self.update_callbacks.append(update)
def display(self, backend='chemview', **kwargs): if backend == 'chemview': from ..notebook import display_system mv = display_system(self, **kwargs) return mv if backend == 'povray': from ..graphics import Scene from chemview.render import render_povray from chemview.utils import get_atom_color scene = Scene() scene.add_representation( 'points', { 'coordinates': self.r_array, 'sizes': [1] * self.n_atoms, 'colors': [get_atom_color(t) for t in self.type_array] }) extra_opts = {} if "radiosity" in kwargs: extra_opts.update({'radiosity': kwargs['radiosity']}) scene.camera.autozoom(self.r_array) return render_povray(scene.to_dict(), extra_opts=extra_opts)
def quick_view(structure, bonds=True, conventional=False, transform=None, show_box=True, bond_tol=0.2, stick_radius=0.1): """ A function to visualize pymatgen Structure objects in jupyter notebook using chemview package. Args: structure: pymatgen Structure bonds: (bool) visualize bonds. Bonds are found by comparing distances to added covalent radii of pairs. Defaults to True. conventional: (bool) use conventional cell. Defaults to False. transform: (list) can be used to make supercells with pymatgen.Structure.make_supercell method show_box: (bool) unit cell is shown. Defaults to True. bond_tol: (float) used if bonds=True. Sets the extra distance tolerance when finding bonds. stick_radius: (float) radius of bonds. Returns: A chemview.MolecularViewer object """ s = structure.copy() if conventional: s = SpacegroupAnalyzer(s).get_conventional_standard_structure() if transform: s.make_supercell(transform) atom_types = [i.symbol for i in s.species] if bonds: bonds = [] for i in range(s.num_sites - 1): sym_i = s[i].specie.symbol for j in range(i + 1, s.num_sites): sym_j = s[j].specie.symbol max_d = CovalentRadius.radius[sym_i] + CovalentRadius.radius[ sym_j] + bond_tol if s.get_distance(i, j, np.array([0, 0, 0])) < max_d: bonds.append((i, j)) bonds = bonds if bonds else None mv = MolecularViewer(s.cart_coords, topology={ 'atom_types': atom_types, 'bonds': bonds }) if bonds: mv.ball_and_sticks(stick_radius=stick_radius) for i in s.sites: el = i.specie.symbol coord = i.coords r = CovalentRadius.radius[el] mv.add_representation( 'spheres', { 'coordinates': coord.astype('float32'), 'colors': [get_atom_color(el)], 'radii': [r * 0.5], 'opacity': 1.0 }) if show_box: o = np.array([0, 0, 0]) a, b, c = s.lattice.matrix[0], s.lattice.matrix[1], s.lattice.matrix[2] starts = [o, o, o, a, a, b, b, c, c, a + b, a + c, b + c] ends = [ a, b, c, a + b, a + c, b + a, b + c, c + a, c + b, a + b + c, a + b + c, a + b + c ] colors = [0xffffff for i in range(12)] mv.add_representation( 'lines', { 'startCoords': np.array(starts), 'endCoords': np.array(ends), 'startColors': colors, 'endColors': colors }) return mv