def _on_change_structure(self, change): """Update viewer for new structure""" self.reset() if not change["new"].attributes.species: return self._current_view = self.viewer.add_structure( nglview.ASEStructure(change["new"].convert("ase"))) self.viewer.add_representation("ball+stick", aspectRatio=4) self.viewer.add_representation("unitcell")
def refresh_view(self): viewer = self.viewer # Note: viewer.clear() only removes the 1st component # pylint: disable=protected-access for comp_id in viewer._ngl_component_ids: viewer.remove_component(comp_id) viewer.add_component(nglview.ASEStructure( self.structure_ase)) # adds ball+stick viewer.add_unitcell()
def _update_structure_viewer(self, change): """Update the view if displayed_structure trait was modified.""" with self.hold_trait_notifications(): for comp_id in self._viewer._ngl_component_ids: # pylint: disable=protected-access self._viewer.remove_component(comp_id) self.selection = list() if change['new'] is not None: self._viewer.add_component(nglview.ASEStructure(change['new'])) self._viewer.clear() self._viewer.add_ball_and_stick(aspectRatio=4) # pylint: disable=no-member self._viewer.add_unitcell() # pylint: disable=no-member
def _update_structure_viewer(self, change): """Update the view if displayed_structure trait was modified.""" with self.hold_trait_notifications(): for (comp_id) in self._viewer._ngl_component_ids: # pylint: disable=protected-access self._viewer.remove_component(comp_id) self.selection = list() if change["new"] is not None: self._viewer.add_component(nglview.ASEStructure(change["new"])) self._viewer.clear() self._viewer.stage.set_parameters(clipDist=0) self._viewer.add_representation("unitcell", diffuse="#df0587") self._viewer.add_representation("ball+stick", aspectRatio=3.5)
def visualize_extra(self, vis_list): """ Two visualization options supported: int - defines the atom index to highlight xyz - defines a point in space """ if not hasattr(self.viewer, "component_0"): return self.reset() if hasattr(self.viewer, "component_2"): self.viewer.component_2.clear_representations() self.viewer.component_2.remove_unitcell() cid = self.viewer.component_2.id self.viewer.remove_component(cid) if len(vis_list) > 0: vis_atoms = [x for x in vis_list if isinstance(x, int)] vis_points = [x for x in vis_list if not isinstance(x, int)] if len(vis_atoms) != 0: self.highlight_atoms(vis_atoms, color='red', size=0.2, opacity=0.6) if len(vis_points) != 0: fake_atoms = Atoms('Xe' * len(vis_points), positions=vis_points) self.viewer.add_component(nglview.ASEStructure(fake_atoms), default_representation=False) self.viewer.component_2.add_ball_and_stick(color='blue', aspectRatio=3.1, opacity=0.7)
def setup(self, atoms, details=None): self.atoms = atoms self.details = details # delete all old components while hasattr(self.viewer, "component_0"): self.viewer.component_0.clear_representations() cid = self.viewer.component_0.id self.viewer.remove_component(cid) if details is None: self.mol_inds = [] #list(np.arange(0, len(atoms))) if atoms is None: return else: self.rest_inds = list(np.arange( 0, len(atoms))) # [] #default all big spheres else: if details['system_type'] == 'Bulk': self.mol_inds = [] self.rest_inds = list(np.arange(0, len(atoms))) elif details['system_type'] == 'Wire': self.mol_inds = list(np.arange(0, len(atoms))) self.rest_inds = [] else: self.mol_inds = [ item for sublist in self.details['all_molecules'] for item in sublist ] self.rest_inds = self.details['slabatoms'] + self.details[ 'bottom_H'] + self.details['adatoms'] + self.details[ 'unclassified'] self._gen_translation_indexes() #print('in view mol ',self.mol_inds) #print('in view rest ',self.rest_inds) if len(self.mol_inds) > 0: self.molecules_ase = self.atoms[self.mol_inds] else: self.molecules_ase = Atoms() if len(self.rest_inds) > 0: self.rest_ase = self.atoms[self.rest_inds] else: self.rest_ase = Atoms() # component 0: Molecule self.viewer.add_component(nglview.ASEStructure(self.molecules_ase), default_representation=False) self.viewer.add_ball_and_stick(aspectRatio=MOL_ASPECT, opacity=1.0, component=0) # component 1: Everything else self.viewer.add_component(nglview.ASEStructure(self.rest_ase), default_representation=False) self.viewer.add_ball_and_stick(aspectRatio=REST_ASPECT, opacity=1.0, component=1) self.viewer.add_unitcell() self.viewer.center() #viewer.component_0.add_ball_and_stick(aspectRatio=10.0, opacity=1.0) #for an in set(atoms.numbers): # vdwr=vdw_radii[an] # sel=[s[0] for s in np.argwhere(atoms.numbers==an)] # viewer.add_ball_and_stick(selection=sel,aspectRatio=6.2*vdwr, opacity=1.0,component=0) # Orient camera to look from positive z cell_z = self.atoms.cell[2, 2] com = self.atoms.get_center_of_mass() def_orientation = self.viewer._camera_orientation top_z_orientation = [ 1.0, 0.0, 0.0, 0, 0.0, 1.0, 0.0, 0, 0.0, 0.0, -np.max([cell_z, 30.0]), 0, -com[0], -com[1], -com[2], 1 ] self.viewer._set_camera_orientation(top_z_orientation) self.viewer.observe(self._on_atom_click, names='picked')