Coverage for ase/optimize/test/analyze.py: 10.81%

37 statements  

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

1from collections import defaultdict 

2 

3from numpy import inf 

4 

5import ase.db 

6 

7 

8def analyze(filename, tag='results'): 

9 energies = defaultdict(list) 

10 mintimes = defaultdict(lambda: 999999) 

11 formulas = [] 

12 db = ase.db.connect(filename) 

13 for row in db.select(sort='formula'): 

14 if row.formula not in formulas: 

15 formulas.append(row.formula) 

16 energies[row.formula].append(row.get('energy', inf)) 

17 emin = {formula: min(energies[formula]) for formula in energies} 

18 

19 data = defaultdict(list) 

20 for row in db.select(sort='formula'): 

21 if row.get('energy', inf) - emin[row.formula] < 0.01: 

22 t = row.t 

23 if row.n < 100: 

24 nsteps = row.n 

25 mintimes[row.formula] = min(mintimes[row.formula], t) 

26 else: 

27 nsteps = 9999 

28 t = inf 

29 else: 

30 nsteps = 9999 

31 t = inf 

32 data[row.optimizer].append((nsteps, t)) 

33 

34 print(formulas) 

35 

36 D = sorted(data.items(), key=lambda x: sum(y[0] for y in x[1])) 

37 with open(tag + '-iterations.csv', 'w') as fd: 

38 print('optimizer,' + ','.join(formulas), file=fd) 

39 for o, d in D: 

40 print( 

41 '{:18},{}'.format( 

42 o, ','.join(f'{x[0]:3}' if x[0] < 100 else ' ' for x in d) 

43 ), 

44 file=fd, 

45 ) 

46 

47 data = { 

48 opt: [(n, t / mintimes[f]) for (n, t), f in zip(x, formulas)] 

49 for opt, x in data.items() 

50 } 

51 D = sorted(data.items(), key=lambda x: sum(min(y[1], 999) for y in x[1])) 

52 with open(tag + '-time.csv', 'w') as fd: 

53 print('optimizer,' + ','.join(formulas), file=fd) 

54 for o, d in D: 

55 print( 

56 '{:18},{}'.format( 

57 o, 

58 ','.join( 

59 f'{x[1]:8.1f}' if x[0] < 100 else ' ' for x in d 

60 ), 

61 ), 

62 file=fd, 

63 ) 

64 

65 

66if __name__ == '__main__': 

67 analyze('results.db')