Coverage for ase/visualize/ngl.py: 14.29%

56 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-27 15:21 +0000

1from ase import Atoms 

2 

3 

4class NGLDisplay: 

5 """Structure display class 

6 

7 Provides basic structure/trajectory display 

8 in the notebook and optional gui which can be used to enhance its 

9 usability. It is also possible to extend the functionality of the 

10 particular instance of the viewer by adding further widgets 

11 manipulating the structure. 

12 """ 

13 

14 def __init__(self, atoms, xsize=500, ysize=500): 

15 import nglview 

16 import nglview.color 

17 from ipywidgets import Dropdown, FloatSlider, HBox, IntSlider, VBox 

18 

19 self.atoms = atoms 

20 if isinstance(atoms[0], Atoms): 

21 # Assume this is a trajectory or struct list 

22 self.view = nglview.show_asetraj(atoms, default=False) 

23 self.frm = IntSlider(value=0, min=0, max=len(atoms) - 1) 

24 self.frm.observe(self._update_frame) 

25 self.struct = atoms[0] 

26 else: 

27 # Assume this is just a single structure 

28 self.view = nglview.show_ase(atoms, default=False) 

29 self.struct = atoms 

30 self.frm = None 

31 

32 self.colors = {} 

33 self.view._remote_call( 

34 'setSize', 

35 target='Widget', 

36 args=['%dpx' % (xsize,), '%dpx' % (ysize,)], 

37 ) 

38 self.view.add_unitcell() 

39 self.view.add_spacefill() 

40 self.view.camera = 'orthographic' 

41 self.view.parameters = {'clipDist': 0} 

42 

43 self.view.center() 

44 

45 self.asel = Dropdown( 

46 options=['All'] + list(set(self.struct.get_chemical_symbols())), 

47 value='All', 

48 description='Show', 

49 ) 

50 

51 self.csel = Dropdown( 

52 options=nglview.color.COLOR_SCHEMES, 

53 value='element', 

54 description='Color scheme', 

55 ) 

56 

57 self.rad = FloatSlider( 

58 value=0.5, min=0.0, max=1.5, step=0.01, description='Ball size' 

59 ) 

60 

61 self.asel.observe(self._select_atom) 

62 self.csel.observe(self._update_repr) 

63 self.rad.observe(self._update_repr) 

64 

65 self.view.update_spacefill( 

66 radiusType='covalent', 

67 radiusScale=0.5, 

68 color_scheme=self.csel.value, 

69 color_scale='rainbow', 

70 ) 

71 

72 wdg = [self.asel, self.csel, self.rad] 

73 if self.frm: 

74 wdg.append(self.frm) 

75 

76 self.gui = HBox([self.view, VBox(wdg)]) 

77 # Make useful shortcuts for the user of the class 

78 self.gui.view = self.view 

79 self.gui.control_box = self.gui.children[1] 

80 self.gui.custom_colors = self.custom_colors 

81 

82 def _update_repr(self, chg=None): 

83 self.view.update_spacefill( 

84 radiusType='covalent', 

85 radiusScale=self.rad.value, 

86 color_scheme=self.csel.value, 

87 color_scale='rainbow', 

88 ) 

89 

90 def _update_frame(self, chg=None): 

91 self.view.frame = self.frm.value 

92 

93 def _select_atom(self, chg=None): 

94 sel = self.asel.value 

95 self.view.remove_spacefill() 

96 for e in set(self.struct.get_chemical_symbols()): 

97 if sel == 'All' or e == sel: 

98 if e in self.colors: 

99 self.view.add_spacefill( 

100 selection='#' + e, color=self.colors[e] 

101 ) 

102 else: 

103 self.view.add_spacefill(selection='#' + e) 

104 self._update_repr() 

105 

106 def custom_colors(self, clr=None): 

107 """ 

108 Define custom colors for some atoms. Pass a dictionary of the form 

109 {'Fe':'red', 'Au':'yellow'} to the function. 

110 To reset the map to default call the method without parameters. 

111 """ 

112 if clr: 

113 self.colors = clr 

114 else: 

115 self.colors = {} 

116 self._select_atom() 

117 

118 

119def view_ngl(atoms, data=None, repeat=None, w=500, h=500): 

120 """ 

121 Returns the nglviewer + some control widgets in the VBox ipywidget. 

122 The viewer supports any Atoms object and any sequence of Atoms objects. 

123 The returned object has two shortcut members: 

124 

125 .view: 

126 nglviewer ipywidget for direct interaction 

127 .control_box: 

128 VBox ipywidget containing view control widgets 

129 """ 

130 # TODO: make `data` and `repeat` really available 

131 return NGLDisplay(atoms, w, h).gui