File: analyze.py

package info (click to toggle)
python-ase 3.22.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,344 kB
  • sloc: python: 126,379; xml: 946; makefile: 111; javascript: 47
file content (59 lines) | stat: -rw-r--r-- 2,021 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from collections import defaultdict
from numpy import inf

import ase.db


def analyze(filename, tag='results'):
    energies = defaultdict(list)
    mintimes = defaultdict(lambda: 999999)
    formulas = []
    db = ase.db.connect(filename)
    for row in db.select(sort='formula'):
        if row.formula not in formulas:
            formulas.append(row.formula)
        energies[row.formula].append(row.get('energy', inf))
    emin = {formula: min(energies[formula]) for formula in energies}

    data = defaultdict(list)
    for row in db.select(sort='formula'):
        if row.get('energy', inf) - emin[row.formula] < 0.01:
            t = row.t
            if row.n < 100:
                nsteps = row.n
                mintimes[row.formula] = min(mintimes[row.formula], t)
            else:
                nsteps = 9999
                t = inf
        else:
            nsteps = 9999
            t = inf
        data[row.optimizer].append((nsteps, t))

    print(formulas)

    D = sorted(data.items(), key=lambda x: sum(y[0] for y in x[1]))
    with open(tag + '-iterations.csv', 'w') as fd:
        print('optimizer,' + ','.join(formulas), file=fd)
        for o, d in D:
            print('{:18},{}'
                  .format(o, ','.join('{:3}'.format(x[0])
                                      if x[0] < 100 else '   '
                                      for x in d)),
                  file=fd)

    data = {opt: [(n, t / mintimes[f]) for (n, t), f in zip(x, formulas)]
            for opt, x in data.items()}
    D = sorted(data.items(), key=lambda x: sum(min(y[1], 999) for y in x[1]))
    with open(tag + '-time.csv', 'w') as fd:
        print('optimizer,' + ','.join(formulas), file=fd)
        for o, d in D:
            print('{:18},{}'
                  .format(o, ','.join('{:8.1f}'.format(x[1])
                                      if x[0] < 100 else '        '
                                      for x in d)),
                  file=fd)


if __name__ == '__main__':
    analyze('results.db')