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

26 statements  

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

1# Note: 

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

3# import time during CLI execution 

4from ase.cli.main import CLIError 

5 

6 

7def read_band_structure(filename): 

8 from ase.io.jsonio import read_json 

9 from ase.spectrum.band_structure import BandStructure 

10 

11 bs = read_json(filename) 

12 if not isinstance(bs, BandStructure): 

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

14 return bs 

15 

16 

17def main(args, parser): 

18 import matplotlib.pyplot as plt 

19 

20 bs = read_band_structure(args.calculation) 

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

22 fig = plt.figure(args.calculation) 

23 ax = fig.gca() 

24 

25 bs.plot( 

26 ax=ax, 

27 filename=args.output, 

28 emin=emin + bs.reference, 

29 emax=emax + bs.reference, 

30 ) 

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( 

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

52 ) 

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

54 parser.add_argument( 

55 '-r', 

56 '--range', 

57 nargs=2, 

58 default=['-3', '3'], 

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

60 help='Default: "-3.0 3.0" (in eV relative to Fermi level).', 

61 ) 

62 

63 @staticmethod 

64 def run(args, parser): 

65 main(args, parser)