File: py.py

package info (click to toggle)
python-ase 3.24.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 15,448 kB
  • sloc: python: 144,945; xml: 2,728; makefile: 113; javascript: 47
file content (37 lines) | stat: -rw-r--r-- 1,232 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
import numpy as np


def write_py(fileobj, images):
    """Write to ASE-compatible python script."""
    fileobj.write('import numpy as np\n\n')
    fileobj.write('from ase import Atoms\n\n')

    if hasattr(images, 'get_positions'):
        images = [images]
    fileobj.write('images = [\n')

    for image in images:
        fileobj.write("    Atoms(symbols='%s',\n"
                      "          pbc=np.array(%s),\n"
                      "          cell=np.array(\n%s),\n"
                      "          positions=np.array(\n%s)),\n" % (
                          image.get_chemical_formula(mode='reduce'),
                          array_to_string(image.pbc, 0),
                          array_to_string(image.cell),
                          array_to_string(image.positions)))

    fileobj.write(']\n')


def array_to_string(array, indent=14):
    """Converts given numpy array to a string, which when printed will pass
    flake8 tests."""
    text = np.array2string(
        array,
        separator=', ',
        suppress_small=False,
        formatter={'float': '{:.8f}'.format, 'bool': '{}'.format},
        threshold=np.inf,
    )
    text = ' ' * indent + text.replace('\n', '\n' + ' ' * indent)
    return text