Coverage for ase/gui/ag.py: 55.93%

59 statements  

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

1# Copyright 2008, 2009 

2# CAMd (see accompanying license files for details). 

3import warnings 

4from pathlib import Path 

5 

6from ase.config import ASE_CONFIG_FILE, Config, cfg 

7 

8 

9class CLICommand: 

10 """ASE's graphical user interface. 

11 

12 ASE-GUI. See the online manual 

13 (https://docs.ase-lib.org/ase/gui/gui.html) 

14 for more information. 

15 """ 

16 

17 @staticmethod 

18 def add_arguments(parser): 

19 add = parser.add_argument 

20 add( 

21 'filenames', 

22 nargs='*', 

23 help='Files to open. Append @SLICE to a filename to pick ' 

24 'a subset of images from that file. See --image-number ' 

25 'for SLICE syntax.', 

26 ) 

27 add( 

28 '-n', 

29 '--image-number', 

30 metavar='SLICE', 

31 default=':', 

32 help='Pick individual image or slice from each of the files. ' 

33 'SLICE can be a number or a Python slice-like expression ' 

34 'such as :STOP, START:STOP, or START:STOP:STEP, ' 

35 'where START, STOP, and STEP are integers. ' 

36 'Indexing counts from 0. ' 

37 'Negative numbers count backwards from last image. ' 

38 'Using @SLICE syntax for a filename overrides this option ' 

39 'for that file.', 

40 ) 

41 add( 

42 '-r', 

43 '--repeat', 

44 default='1', 

45 help='Repeat unit cell. Use "-r 2" or "-r 2,3,1".', 

46 ) 

47 add( 

48 '-R', 

49 '--rotations', 

50 default='', 

51 help='Examples: "-R -90x", "-R 90z,-30x".', 

52 ) 

53 add( 

54 '-o', 

55 '--output', 

56 metavar='FILE', 

57 help='Write configurations to FILE.', 

58 ) 

59 add( 

60 '-g', 

61 '--graph', 

62 # TRANSLATORS: EXPR abbreviates 'expression' 

63 metavar='EXPR', 

64 help='Plot x,y1,y2,... graph from configurations or ' 

65 'write data to sdtout in terminal mode. Use the ' 

66 'symbols: i, s, d, fmax, e, ekin, A, R, E and F. See ' 

67 'https://docs.ase-lib.org/ase/gui/gui.html' 

68 '#plotting-data for more details.', 

69 ) 

70 add( 

71 '-t', 

72 '--terminal', 

73 action='store_true', 

74 default=False, 

75 help='Run in terminal window - no GUI.', 

76 ) 

77 add( 

78 '--interpolate', 

79 type=int, 

80 metavar='N', 

81 help='Interpolate N images between 2 given images.', 

82 ) 

83 add( 

84 '-b', 

85 '--bonds', 

86 action='store_true', 

87 default=False, 

88 help='Draw bonds between atoms.', 

89 ) 

90 add( 

91 '-s', 

92 '--scale', 

93 dest='radii_scale', 

94 metavar='FLOAT', 

95 default=None, 

96 type=float, 

97 help='Scale covalent radii.', 

98 ) 

99 add( 

100 '--configfile', 

101 metavar='FILE', 

102 default=None, 

103 type=str, 

104 help='Load GUI settings from the specified file (.ini format). ' 

105 'If not specified, defaults to the global ASE configfile ' 

106 f'{str(ASE_CONFIG_FILE)}.', 

107 ) 

108 

109 @staticmethod 

110 def run(args): 

111 from ase.atoms import Atoms 

112 from ase.gui.images import Images 

113 

114 if args.configfile: 

115 config = Config.read(Path(args.configfile)) 

116 else: 

117 config = cfg 

118 

119 images = Images(config=config) 

120 

121 if args.filenames: 

122 images.read(args.filenames, args.image_number) 

123 else: 

124 images.initialize([Atoms()]) 

125 

126 if args.interpolate: 

127 images.interpolate(args.interpolate) 

128 

129 if args.repeat != '1': 

130 r = args.repeat.split(',') 

131 if len(r) == 1: 

132 r = 3 * r 

133 images.repeat_images([int(c) for c in r]) 

134 

135 if args.radii_scale: 

136 images.scale_radii(args.radii_scale) 

137 

138 if args.output is not None: 

139 warnings.warn('You should be using "ase convert ..." instead!') 

140 images.write(args.output, rotations=args.rotations) 

141 args.terminal = True 

142 

143 if args.terminal: 

144 if args.graph is not None: 

145 data = images.graph(args.graph) 

146 for line in data.T: 

147 for x in line: 

148 print(x, end=' ') 

149 print() 

150 else: 

151 import os 

152 

153 import matplotlib 

154 

155 from ase.gui.gui import GUI 

156 

157 # When using Jupyter or some other interactive interpreter, 

158 # matplotlib is set up to use an inline backend such as: 

159 # 'inline' 

160 # 'module://ipykernel.pylab.backend_inline' 

161 # 'module://matplotlib_inline.backend_inline' 

162 # However, these would cause plots in the GUI to break so 

163 # we need to switch to Tk 

164 backend = matplotlib.get_backend() 

165 if 'inline' in backend: 

166 os.environ['MPLBACKEND'] = 'TkAgg' 

167 

168 gui = GUI( 

169 images, args.rotations, args.bonds, args.graph, config=config 

170 ) 

171 images.notify_new_images = gui.obs.new_images.notify # XXX hack 

172 gui.run() 

173 

174 # Reinstate the backend used before the GUI was opened 

175 os.environ['MPLBACKEND'] = backend