Coverage for /builds/ase/ase/ase/cli/band_structure.py: 92.31%

26 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2025-08-02 00:12 +0000

1# fmt: off 

2 

3# Note: 

4# Try to avoid module level import statements here to reduce 

5# import time during CLI execution 

6from ase.cli.main import CLIError 

7 

8 

9def read_band_structure(filename): 

10 from ase.io.jsonio import read_json 

11 from ase.spectrum.band_structure import BandStructure 

12 

13 bs = read_json(filename) 

14 if not isinstance(bs, BandStructure): 

15 raise CLIError(f'Expected band structure, but file contains: {bs}') 

16 return bs 

17 

18 

19def main(args, parser): 

20 import matplotlib.pyplot as plt 

21 

22 bs = read_band_structure(args.calculation) 

23 emin, emax = (float(e) for e in args.range) 

24 fig = plt.figure(args.calculation) 

25 ax = fig.gca() 

26 

27 bs.plot(ax=ax, 

28 filename=args.output, 

29 emin=emin + bs.reference, 

30 emax=emax + bs.reference) 

31 

32 if args.output is None: 

33 plt.show() 

34 

35 

36class CLICommand: 

37 """Plot band-structure. 

38 

39 Read eigenvalues and k-points from file and plot result from 

40 band-structure calculation or interpolate 

41 from Monkhorst-Pack sampling to a given path (--path=PATH). 

42 

43 Example: 

44 

45 $ ase band-structure bandstructure.json -r -10 10 

46 """ 

47 

48 @staticmethod 

49 def add_arguments(parser): 

50 parser.add_argument('calculation', 

51 help='Path to output file(s) from calculation.') 

52 parser.add_argument('-o', '--output', help='Write image to a file') 

53 parser.add_argument('-r', '--range', nargs=2, default=['-3', '3'], 

54 metavar=('emin', 'emax'), 

55 help='Default: "-3.0 3.0" ' 

56 '(in eV relative to Fermi level).') 

57 

58 @staticmethod 

59 def run(args, parser): 

60 main(args, parser)