def test_msonable(self): legend = Legend(self.struct) legend_dict = legend.as_dict() legend_from_dict = Legend.from_dict(legend_dict) assert legend.get_legend() == legend_from_dict.get_legend()
def get_scene_and_legend(self) -> Tuple[Scene, Dict[str, str]]: legend = Legend(self.graph.structure, color_scheme="VESTA", cmap_range=None) legend.uniform_radius = 0.3 scene = get_structure_graph_scene( self.graph, draw_image_atoms=True, bonded_sites_outside_unit_cell=False, hide_incomplete_edges=True, explicitly_calculate_polyhedra_hull=False, group_by_symmetry=True, draw_polyhedra=False, legend=legend) scene.name = "DefectStructureComponentScene" lattice: Lattice = self.graph.structure.lattice origin = -self.graph.structure.lattice.get_cartesian_coords( [0.5, 0.5, 0.5]) scene_json = scene.to_json() for idx, i in enumerate(self.interstitials, 1): site = Site(species=DummySpecie(), coords=lattice.get_cartesian_coords(i.frac_coords)) interstitial_scene = site.get_scene(origin=origin) interstitial_scene.name = f"i{idx}" interstitial_scene.contents[0].contents[0].tooltip = f"i{idx}" scene_json["contents"].append(interstitial_scene.to_json()) return scene_json, legend.get_legend()
def get_scene_and_legend( graph: Optional[Union[StructureGraph, MoleculeGraph]], name, color_scheme=DEFAULTS["color_scheme"], color_scale=None, radius_strategy=DEFAULTS["radius_strategy"], draw_image_atoms=DEFAULTS["draw_image_atoms"], bonded_sites_outside_unit_cell=DEFAULTS[ "bonded_sites_outside_unit_cell"], hide_incomplete_bonds=DEFAULTS["hide_incomplete_bonds"], explicitly_calculate_polyhedra_hull=False, scene_additions=None, show_compass=DEFAULTS["show_compass"], ) -> Tuple[Scene, Dict[str, str]]: # default scene name will be name of component, "_ct_..." # strip leading _ since this will cause problems in JavaScript land scene = Scene(name=name[1:]) if graph is None: return scene, {} struct_or_mol = StructureMoleculeComponent._get_struct_or_mol(graph) # TODO: add radius_scale legend = Legend( struct_or_mol, color_scheme=color_scheme, radius_scheme=radius_strategy, cmap_range=color_scale, ) if isinstance(graph, StructureGraph): scene = graph.get_scene( draw_image_atoms=draw_image_atoms, bonded_sites_outside_unit_cell=bonded_sites_outside_unit_cell, hide_incomplete_edges=hide_incomplete_bonds, explicitly_calculate_polyhedra_hull= explicitly_calculate_polyhedra_hull, legend=legend, ) elif isinstance(graph, MoleculeGraph): scene = graph.get_scene(legend=legend) scene.name = name if hasattr(struct_or_mol, "lattice"): axes = struct_or_mol.lattice._axes_from_lattice() # TODO: fix pop-in ? axes.visible = show_compass scene.contents.append(axes) if scene_additions: # TODO: need a Scene.from_json() to make this work raise NotImplementedError scene["contents"].append(scene_additions) return scene.to_json(), legend.get_legend()
def get_scene_and_legend( graph: Optional[Union[StructureGraph, MoleculeGraph]], color_scheme=DEFAULTS["color_scheme"], color_scale=None, radius_strategy=DEFAULTS["radius_strategy"], draw_image_atoms=DEFAULTS["draw_image_atoms"], bonded_sites_outside_unit_cell=DEFAULTS[ "bonded_sites_outside_unit_cell"], hide_incomplete_bonds=DEFAULTS["hide_incomplete_bonds"], explicitly_calculate_polyhedra_hull=False, scene_additions=None, show_compass=DEFAULTS["show_compass"], group_by_site_property=None, ) -> Tuple[Scene, Dict[str, str]]: scene = Scene(name="StructureMoleculeComponentScene") if graph is None: return scene, {} struct_or_mol = StructureMoleculeComponent._get_struct_or_mol(graph) # TODO: add radius_scale legend = Legend( struct_or_mol, color_scheme=color_scheme, radius_scheme=radius_strategy, cmap_range=color_scale, ) if isinstance(graph, StructureGraph): scene = graph.get_scene( draw_image_atoms=draw_image_atoms, bonded_sites_outside_unit_cell=bonded_sites_outside_unit_cell, hide_incomplete_edges=hide_incomplete_bonds, explicitly_calculate_polyhedra_hull= explicitly_calculate_polyhedra_hull, group_by_site_property=group_by_site_property, legend=legend, ) elif isinstance(graph, MoleculeGraph): scene = graph.get_scene(legend=legend) scene.name = "StructureMoleculeComponentScene" if hasattr(struct_or_mol, "lattice"): axes = struct_or_mol.lattice._axes_from_lattice() axes.visible = show_compass scene.contents.append(axes) scene_json = scene.to_json() if scene_additions: # TODO: this might be cleaner if we had a Scene.from_json() method scene_json["contents"].append(scene_additions) return scene_json, legend.get_legend()
def get_scene_and_legend( graph: Optional[Union[StructureGraph, MoleculeGraph]], color_scale=None, radius_strategy=DEFAULTS["radius_strategy"], draw_image_atoms=DEFAULTS["draw_image_atoms"], bonded_sites_outside_unit_cell=DEFAULTS[ "bonded_sites_outside_unit_cell"], hide_incomplete_bonds=DEFAULTS["hide_incomplete_bonds"], explicitly_calculate_polyhedra_hull=False, scene_additions=None, show_compass=DEFAULTS["show_compass"], ) -> Tuple[Scene, Dict[str, str]]: scene = Scene(name="AceStructureMoleculeComponentScene") if graph is None: return scene, {} structure = StructureComponent._get_structure(graph) # TODO: add radius_scale legend = Legend( structure, color_scheme="VESTA", radius_scheme=radius_strategy, cmap_range=color_scale, ) scene = graph.get_scene( draw_image_atoms=draw_image_atoms, bonded_sites_outside_unit_cell=bonded_sites_outside_unit_cell, hide_incomplete_edges=hide_incomplete_bonds, explicitly_calculate_polyhedra_hull= explicitly_calculate_polyhedra_hull, legend=legend, ) scene.name = "StructureComponentScene" if hasattr(structure, "lattice"): axes = structure.lattice._axes_from_lattice() axes.visible = show_compass scene.contents.append(axes) scene = scene.to_json() if scene_additions: # TODO: need a Scene.from_json() to make this work # raise NotImplementedError scene["contents"].append(scene_additions) return scene, legend.get_legend()
def get_scene_and_legend(self, scene_additions=None ) -> Tuple[Scene, Dict[str, str]]: legend = Legend(self.graph.structure, color_scheme="VESTA", radius_scheme="uniform", cmap_range=None) legend.uniform_radius = 0.2 scene = get_structure_graph_scene( self.graph, draw_image_atoms=True, bonded_sites_outside_unit_cell=False, hide_incomplete_edges=True, explicitly_calculate_polyhedra_hull=False, group_by_symmetry=False, legend=legend) scene.name = "DefectStructureComponentScene" # axes = graph.structure.lattice._axes_from_lattice() # axes.visible = True # scene.contents.append(axes) scene = scene.to_json() if scene_additions: scene["contents"].append(scene_additions) lattice = self.graph.structure.lattice origin = -self.graph.structure.lattice.get_cartesian_coords( [0.5, 0.5, 0.5]) for name, frac_coords in self.vacancy_sites: site = Site(species=DummySpecie(name), coords=lattice.get_cartesian_coords(frac_coords)) vac_scene = site.get_scene(origin=origin) vac_scene.name = f"{name}_{frac_coords}" vac_scene.contents[0].contents[0].tooltip = name scene["contents"].append(vac_scene.to_json()) return scene, legend.get_legend()
def test_get_color(self): # test default legend = Legend(self.struct, color_scheme="VESTA") color = legend.get_color(self.sp0) assert color == "#ffcccc" # element-based color schemes shouldn't change if you supply a site color = legend.get_color(self.sp0, site=self.site0) assert color == "#ffcccc" color = legend.get_color(self.sp2) assert color == "#a67573" assert legend.get_legend()["colors"] == { "#a67573": "In", "#fe0300": "O", "#ffcccc": "H", } # test alternate legend = Legend(self.struct, color_scheme="Jmol") color = legend.get_color(self.sp0) assert color == "#ffffff" assert legend.get_legend()["colors"] == { "#a67573": "In", "#ff0d0d": "O", "#ffffff": "H", } # test coloring by site properties legend = Legend(self.struct, color_scheme="example_site_prop") color = legend.get_color(self.sp0, site=self.site0) assert color == "#b30326" color = legend.get_color(self.sp1, site=self.site1) assert color == "#dddcdb" color = legend.get_color(self.sp2, site=self.site2) assert color == "#7b9ef8" assert legend.get_legend()["colors"] == { "#7b9ef8": "-3.00", "#b30326": "5.00", "#dddcdb": "0.00", } # test accessible legend = Legend(self.struct, color_scheme="accessible") color = legend.get_color(self.sp0, site=self.site0) assert color == "#ffffff" color = legend.get_color(self.sp1, site=self.site1) assert color == "#d55e00" color = legend.get_color(self.sp2, site=self.site2) assert color == "#cc79a7" assert legend.get_legend()["colors"] == { "#cc79a7": "In", "#d55e00": "O", "#ffffff": "H", } # test disordered legend = Legend(self.struct_disordered) color = legend.get_color(self.site_d_sp0, site=self.site_d) assert color == "#a67573" color = legend.get_color(self.site_d_sp1, site=self.site_d) assert color == "#bfa6a6" assert legend.get_legend()["colors"] == { "#a67573": "In", "#bfa6a6": "Al", "#ff0d0d": "O", "#ffffff": "H", } # test categorical legend = Legend(self.struct, color_scheme="example_categorical_site_prop") assert legend.get_legend()["colors"] == { "#377eb8": "8b", "#e41a1c": "4a" } # test pre-defined legend = Legend(self.struct_manual) assert legend.get_legend()["colors"] == { "#0000ff": "O2-", "#00ff00": "In", "#ff0000": "H", }